sencode: ensure bijective bvector serialization

This commit is contained in:
Mirek Kratochvil 2013-04-19 12:28:15 +02:00
parent 645cdb96ee
commit 765c553e97

View file

@ -66,6 +66,7 @@ sencode* bvector::serialize()
bool bvector::unserialize (sencode* s) bool bvector::unserialize (sencode* s)
{ {
uint i;
sencode_list*l = dynamic_cast<sencode_list*> (s); sencode_list*l = dynamic_cast<sencode_list*> (s);
if (!l) return false; if (!l) return false;
if (l->items.size() != 2) return false; if (l->items.size() != 2) return false;
@ -75,9 +76,18 @@ bool bvector::unserialize (sencode* s)
if (bytes->b.size() != ( (size->i + 7) / 8) ) return false; if (bytes->b.size() != ( (size->i + 7) / 8) ) return false;
clear(); clear();
resize (size->i, 0); resize (size->i, 0);
for (uint i = 0; i < size->i; ++i) for (i = 0; i < size->i; ++i)
if ( (bytes->b[i / 8] >> (i % 8) ) & 1) if ( (bytes->b[i / 8] >> (i % 8) ) & 1)
item (i) = 1; item (i) = 1;
/*
* the important part. verify that padding is always zero, because
* sencode serialization must be bijective
*/
for (; i < 8 * bytes->b.size(); ++i)
if ( (bytes->b[i / 8] >> (i % 8) ) & 1)
return false;
return true; return true;
} }