fixed read_125Size and write_125Size according to RFC4880

This commit is contained in:
csoler 2016-08-17 09:40:16 +02:00
parent 4b11fb1729
commit 368a429846

View File

@ -248,16 +248,16 @@ uint64_t PGPKeyParser::read_KeyID(unsigned char *& data)
uint32_t PGPKeyParser::write_125Size(unsigned char *data,uint32_t size)
{
if(size < 0xC0)//192 To know if size is coded with One Char < 0xC0
if(size < 192)//192 To know if size is coded with One Char < 0xC0
{
data[0] = size ;
return 1;
}
if(size < 0x20C0)//8384 To know if size is coded with Two Chars < 0xE0
if(size < 8384)//8384 To know if size is coded with Two Chars < 0xE0. See RFC4880
{
data[0] = (size >> 8) + 0xC0 ;
data[1] = (size & 0xFF) - 0xC0 ;//Warning data[1] could be "negative", recode it using 8bits type
data[1] = (size - 192) & 0xFF ;//Warning data[1] could be "negative", recode it using 8bits type
data[0] = ((size - 192 - data[1]) >> 8) + 192 ;
return 2 ;
}
@ -276,14 +276,14 @@ uint32_t PGPKeyParser::read_125Size(unsigned char *& data)
uint8_t b1 = *data ;
++data ;
if(b1 < 0xC0)//192 Size is coded with One Char
if(b1 < 192) //192 Size is coded with One Char. See RFC4880
return b1 ;
uint8_t b2 = *data ;
++data ;
if(b1 < 0xE0)//224 = 0xC0+0x20 Size is coded with Two Chars
return ((b1-0xC0) << 8) + uint8_t(b2 + 0xC0) ; //Use uint8_t because b2 could be "negative" as 0xC0 was added to it.
if(b1 < 224)//224 = 0xC0+0x20 Size is coded with Two Chars
return ((b1-192) << 8) + b2 + 192 ; // see RFC4880
if(b1 != 0xFF)// Else Coded with 4 Chars but first == 0xFF
throw std::runtime_error("GPG parsing error") ;