serialization: use macros for hard work

This commit is contained in:
Mirek Kratochvil 2014-04-05 14:54:54 +02:00
parent eb47cce9ad
commit 654b2f58ef

View file

@ -31,6 +31,14 @@
#include "hashfile.h" #include "hashfile.h"
#include "symkey.h" #include "symkey.h"
#define CAST(IN,OUT,TYPE) \
OUT=dynamic_cast<TYPE>(IN); \
if(!OUT) return false;
#define CAST_LIST(IN,OUT) CAST(IN,OUT,sencode_list*)
#define CAST_BYTES(IN,OUT) CAST(IN,OUT,sencode_bytes*)
#define CAST_INT(IN,OUT) CAST(IN,OUT,sencode_int*)
static sencode* serialize_uint_vector (std::vector<uint>*v) static sencode* serialize_uint_vector (std::vector<uint>*v)
{ {
sencode_list*l = new sencode_list; sencode_list*l = new sencode_list;
@ -42,13 +50,12 @@ static sencode* serialize_uint_vector (std::vector<uint>*v)
static bool unserialize_uint_vector (std::vector<uint>*v, sencode*s) static bool unserialize_uint_vector (std::vector<uint>*v, sencode*s)
{ {
sencode_list*l = dynamic_cast<sencode_list*> (s); sencode_list* CAST_LIST (s, l);
if (!l) return false;
v->clear(); v->clear();
v->resize (l->items.size() ); v->resize (l->items.size() );
for (uint i = 0; i < v->size(); ++i) { for (uint i = 0; i < v->size(); ++i) {
sencode_int*x = dynamic_cast<sencode_int*> (l->items[i]); sencode_int*CAST_INT (l->items[i], x);
if (!x) return false;
(*v) [i] = x->i; (*v) [i] = x->i;
} }
return true; return true;
@ -70,12 +77,10 @@ sencode* bvector::serialize()
bool bvector::unserialize (sencode* s) bool bvector::unserialize (sencode* s)
{ {
uint i; uint i;
sencode_list*l = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, l);
if (!l) return false;
if (l->items.size() != 2) return false; if (l->items.size() != 2) return false;
sencode_int*size = dynamic_cast<sencode_int*> (l->items[0]); sencode_int*CAST_INT (l->items[0], size);
sencode_bytes*bytes = dynamic_cast<sencode_bytes*> (l->items[1]); sencode_bytes*CAST_BYTES (l->items[1], bytes);
if (! (size && bytes) ) return false;
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);
@ -111,13 +116,11 @@ sencode* matrix::serialize()
bool matrix::unserialize (sencode* s) bool matrix::unserialize (sencode* s)
{ {
sencode_list*l = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, l);
if (!l) return false;
if (l->items.size() != 3) return false; if (l->items.size() != 3) return false;
sencode_int*w = dynamic_cast<sencode_int*> (l->items[0]); sencode_int*CAST_INT (l->items[0], w);
sencode_int*h = dynamic_cast<sencode_int*> (l->items[1]); sencode_int*CAST_INT (l->items[1], h);
sencode_bytes*bytes = dynamic_cast<sencode_bytes*> (l->items[2]); sencode_bytes*CAST_BYTES (l->items[2], bytes);
if (! (h && w && bytes) ) return false;
if (bytes->b.size() != ( ( (h->i * w->i) + 7) / 8) ) return false; if (bytes->b.size() != ( ( (h->i * w->i) + 7) / 8) ) return false;
clear(); clear();
resize2 (w->i, h->i, 0); resize2 (w->i, h->i, 0);
@ -149,8 +152,7 @@ sencode* gf2m::serialize()
bool gf2m::unserialize (sencode* s) bool gf2m::unserialize (sencode* s)
{ {
sencode_int*p = dynamic_cast<sencode_int*> (s); sencode_int*CAST_INT (s, p);
if (!p) return false;
return create (p->i); return create (p->i);
} }
@ -182,12 +184,10 @@ sencode* mce::privkey::serialize()
bool mce::privkey::unserialize (sencode* s) bool mce::privkey::unserialize (sencode* s)
{ {
sencode_list*l = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, l);
if (!l) return false;
if (l->items.size() != 6) return false; if (l->items.size() != 6) return false;
sencode_bytes*ident = dynamic_cast<sencode_bytes*> (l->items[0]); sencode_bytes*CAST_BYTES (l->items[0], ident);
if (!ident) return false;
if (ident->b.compare (PRIVKEY_IDENT "MCE") ) return false; if (ident->b.compare (PRIVKEY_IDENT "MCE") ) return false;
if (! (fld.unserialize (l->items[1]) && if (! (fld.unserialize (l->items[1]) &&
@ -211,16 +211,13 @@ sencode* mce::pubkey::serialize()
bool mce::pubkey::unserialize (sencode* s) bool mce::pubkey::unserialize (sencode* s)
{ {
sencode_list*l = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, l);
if (!l) return false;
if (l->items.size() != 3) return false; if (l->items.size() != 3) return false;
sencode_bytes*ident = dynamic_cast<sencode_bytes*> (l->items[0]); sencode_bytes*CAST_BYTES (l->items[0], ident);
if (!ident) return false;
if (ident->b.compare (PUBKEY_IDENT "MCE") ) return false; if (ident->b.compare (PUBKEY_IDENT "MCE") ) return false;
sencode_int*p = dynamic_cast<sencode_int*> (l->items[0]); sencode_int*CAST_INT (l->items[0], p);
if (!p) return false;
t = p->i; t = p->i;
if (!G.unserialize (l->items[1]) ) return false; if (!G.unserialize (l->items[1]) ) return false;
@ -242,12 +239,10 @@ sencode* nd::privkey::serialize()
bool nd::privkey::unserialize (sencode* s) bool nd::privkey::unserialize (sencode* s)
{ {
sencode_list*l = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, l);
if (!l) return false;
if (l->items.size() != 5) return false; if (l->items.size() != 5) return false;
sencode_bytes*ident = dynamic_cast<sencode_bytes*> (l->items[0]); sencode_bytes*CAST_BYTES (l->items[0], ident);
if (!ident) return false;
if (ident->b.compare (PRIVKEY_IDENT "ND") ) return false; if (ident->b.compare (PRIVKEY_IDENT "ND") ) return false;
if (! (fld.unserialize (l->items[1]) && if (! (fld.unserialize (l->items[1]) &&
@ -270,16 +265,13 @@ sencode* nd::pubkey::serialize()
bool nd::pubkey::unserialize (sencode* s) bool nd::pubkey::unserialize (sencode* s)
{ {
sencode_list*l = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, l);
if (!l) return false;
if (l->items.size() != 3) return false; if (l->items.size() != 3) return false;
sencode_bytes*ident = dynamic_cast<sencode_bytes*> (l->items[0]); sencode_bytes*CAST_BYTES (l->items[0], ident);
if (!ident) return false;
if (ident->b.compare (PRIVKEY_IDENT "ND") ) return false; if (ident->b.compare (PRIVKEY_IDENT "ND") ) return false;
sencode_int*p = dynamic_cast<sencode_int*> (l->items[1]); sencode_int*CAST_INT (l->items[1], p);
if (!p) return false;
t = p->i; t = p->i;
if (!H.unserialize (l->items[2]) ) return false; if (!H.unserialize (l->items[2]) ) return false;
@ -303,16 +295,13 @@ sencode* mce_qd::privkey::serialize()
bool mce_qd::privkey::unserialize (sencode* s) bool mce_qd::privkey::unserialize (sencode* s)
{ {
sencode_list*l = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, l);
if (!l) return false;
if (l->items.size() != 7) return false; if (l->items.size() != 7) return false;
sencode_bytes*ident = dynamic_cast<sencode_bytes*> (l->items[0]); sencode_bytes*CAST_BYTES (l->items[0], ident);
if (!ident) return false;
if (ident->b.compare (PRIVKEY_IDENT "QD-MCE") ) return false; if (ident->b.compare (PRIVKEY_IDENT "QD-MCE") ) return false;
sencode_int*p = dynamic_cast<sencode_int*> (l->items[2]); sencode_int*CAST_INT (l->items[2], p);
if (!p) return false;
T = p->i; T = p->i;
if (! (fld.unserialize (l->items[1]) && if (! (fld.unserialize (l->items[1]) &&
@ -336,16 +325,13 @@ sencode* mce_qd::pubkey::serialize()
bool mce_qd::pubkey::unserialize (sencode* s) bool mce_qd::pubkey::unserialize (sencode* s)
{ {
sencode_list*l = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, l);
if (!l) return false;
if (l->items.size() != 3) return false; if (l->items.size() != 3) return false;
sencode_bytes*ident = dynamic_cast<sencode_bytes*> (l->items[0]); sencode_bytes*CAST_BYTES (l->items[0], ident);
if (!ident) return false;
if (ident->b.compare (PUBKEY_IDENT "QD-MCE") ) return false; if (ident->b.compare (PUBKEY_IDENT "QD-MCE") ) return false;
sencode_int*p = dynamic_cast<sencode_int*> (l->items[1]); sencode_int*CAST_INT (l->items[1], p);
if (!p) return false;
T = p->i; T = p->i;
if (!qd_sigs.unserialize (l->items[2]) ) return false; if (!qd_sigs.unserialize (l->items[2]) ) return false;
@ -365,21 +351,17 @@ sencode* fmtseq::privkey::tree_stk_item::serialize()
bool fmtseq::privkey::tree_stk_item::unserialize (sencode*s) bool fmtseq::privkey::tree_stk_item::unserialize (sencode*s)
{ {
sencode_list*l = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, l);
if (!l) return false;
if (l->items.size() != 3) return false; if (l->items.size() != 3) return false;
sencode_int*p; sencode_int*p;
p = dynamic_cast<sencode_int*> (l->items[0]); CAST_INT (l->items[0], p);
if (!p) return false;
level = p->i; level = p->i;
p = dynamic_cast<sencode_int*> (l->items[1]); CAST_INT (l->items[1], p);
if (!p) return false;
pos = p->i; pos = p->i;
sencode_bytes* a = dynamic_cast<sencode_bytes*> (l->items[2]); sencode_bytes* CAST_BYTES (l->items[2], a);
if (!a) return false;
item = std::vector<byte> (a->b.begin(), a->b.end() ); item = std::vector<byte> (a->b.begin(), a->b.end() );
return true; return true;
@ -458,52 +440,41 @@ sencode* fmtseq::privkey::serialize()
bool fmtseq::privkey::unserialize (sencode*s) bool fmtseq::privkey::unserialize (sencode*s)
{ {
uint i, j; uint i, j;
sencode_list*L = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, L);
if (!L) return false;
if (L->items.size() != 10) return false; if (L->items.size() != 10) return false;
sencode_bytes*ident = dynamic_cast<sencode_bytes*> (L->items[0]); sencode_bytes*CAST_BYTES (L->items[0], ident);
if (!ident) return false;
if (ident->b.compare (PRIVKEY_IDENT "FMTSEQ") ) return false; if (ident->b.compare (PRIVKEY_IDENT "FMTSEQ") ) return false;
sencode_bytes*B; sencode_bytes*B;
sencode_int*I; sencode_int*I;
B = dynamic_cast<sencode_bytes*> (L->items[1]); CAST_BYTES (L->items[1], B);
if (!B) return false;
SK = std::vector<byte> (B->b.begin(), B->b.end() ); SK = std::vector<byte> (B->b.begin(), B->b.end() );
I = dynamic_cast<sencode_int*> (L->items[2]); CAST_INT (L->items[2], I);
if (!I) return false;
h = I->i; h = I->i;
I = dynamic_cast<sencode_int*> (L->items[3]); CAST_INT (L->items[3], I);
if (!I) return false;
l = I->i; l = I->i;
I = dynamic_cast<sencode_int*> (L->items[4]); CAST_INT (L->items[4], I);
if (!I) return false;
hs = I->i; hs = I->i;
I = dynamic_cast<sencode_int*> (L->items[5]); CAST_INT (L->items[5], I);
if (!I) return false;
sigs_used = I->i; sigs_used = I->i;
sencode_list*A; sencode_list*A;
//exist subtrees //exist subtrees
A = dynamic_cast<sencode_list*> (L->items[6]); CAST_LIST (L->items[6], A);
if (!A) return false;
exist.clear(); exist.clear();
exist.resize (A->items.size() ); exist.resize (A->items.size() );
for (i = 0; i < exist.size(); ++i) { for (i = 0; i < exist.size(); ++i) {
sencode_list*e = dynamic_cast<sencode_list*> (A->items[i]); sencode_list*CAST_LIST (A->items[i], e);
if (!e) return false;
exist[i].resize (e->items.size() ); exist[i].resize (e->items.size() );
for (j = 0; j < exist[i].size(); ++j) { for (j = 0; j < exist[i].size(); ++j) {
sencode_bytes*item = dynamic_cast<sencode_bytes*> sencode_bytes*CAST_BYTES (e->items[j], item);
(e->items[j]);
if (!item) return false;
exist[i][j] = std::vector<byte> exist[i][j] = std::vector<byte>
(item->b.begin(), (item->b.begin(),
item->b.end() ); item->b.end() );
@ -511,18 +482,14 @@ bool fmtseq::privkey::unserialize (sencode*s)
} }
//desired subtrees //desired subtrees
A = dynamic_cast<sencode_list*> (L->items[7]); CAST_LIST (L->items[7], A);
if (!A) return false;
desired.clear(); desired.clear();
desired.resize (A->items.size() ); desired.resize (A->items.size() );
for (i = 0; i < desired.size(); ++i) { for (i = 0; i < desired.size(); ++i) {
sencode_list*d = dynamic_cast<sencode_list*> (A->items[i]); sencode_list*CAST_LIST (A->items[i], d);
if (!d) return false;
desired[i].resize (d->items.size() ); desired[i].resize (d->items.size() );
for (j = 0; j < desired[i].size(); ++j) { for (j = 0; j < desired[i].size(); ++j) {
sencode_bytes*item = dynamic_cast<sencode_bytes*> sencode_bytes*CAST_BYTES (d->items[j], item);
(d->items[j]);
if (!item) return false;
desired[i][j] = std::vector<byte> desired[i][j] = std::vector<byte>
(item->b.begin(), (item->b.begin(),
item->b.end() ); item->b.end() );
@ -530,13 +497,11 @@ bool fmtseq::privkey::unserialize (sencode*s)
} }
//desired stacks //desired stacks
A = dynamic_cast<sencode_list*> (L->items[8]); CAST_LIST (L->items[8], A);
if (!A) return false;
desired_stack.clear(); desired_stack.clear();
desired_stack.resize (A->items.size() ); desired_stack.resize (A->items.size() );
for (i = 0; i < desired_stack.size(); ++i) { for (i = 0; i < desired_stack.size(); ++i) {
sencode_list*d = dynamic_cast<sencode_list*> (A->items[i]); sencode_list*CAST_LIST (A->items[i], d);
if (!d) return false;
desired_stack[i].resize (d->items.size() ); desired_stack[i].resize (d->items.size() );
for (j = 0; j < desired_stack[i].size(); ++j) for (j = 0; j < desired_stack[i].size(); ++j)
if (!desired_stack[i][j].unserialize (d->items[j]) ) if (!desired_stack[i][j].unserialize (d->items[j]) )
@ -544,13 +509,11 @@ bool fmtseq::privkey::unserialize (sencode*s)
} }
//desired progress //desired progress
A = dynamic_cast<sencode_list*> (L->items[9]); CAST_LIST (L->items[9], A);
if (!A) return false;
desired_progress.clear(); desired_progress.clear();
desired_progress.resize (A->items.size() ); desired_progress.resize (A->items.size() );
for (i = 0; i < desired_progress.size(); ++i) { for (i = 0; i < desired_progress.size(); ++i) {
I = dynamic_cast<sencode_int*> (A->items[i]); CAST_INT (A->items[i], I);
if (!I) return false;
desired_progress[i] = I->i; desired_progress[i] = I->i;
} }
@ -572,25 +535,20 @@ sencode* fmtseq::pubkey::serialize()
bool fmtseq::pubkey::unserialize (sencode*s) bool fmtseq::pubkey::unserialize (sencode*s)
{ {
sencode_list*l = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, l);
if (!l) return false;
if (l->items.size() != 4) return false; if (l->items.size() != 4) return false;
sencode_bytes*ident = dynamic_cast<sencode_bytes*> (l->items[0]); sencode_bytes*CAST_BYTES (l->items[0], ident);
if (!ident) return false;
if (ident->b.compare (PUBKEY_IDENT "FMTSEQ") ) return false; if (ident->b.compare (PUBKEY_IDENT "FMTSEQ") ) return false;
sencode_int*p; sencode_int*p;
p = dynamic_cast<sencode_int*> (l->items[1]); CAST_INT (l->items[1], p);
if (!p) return false;
H = p->i; H = p->i;
p = dynamic_cast<sencode_int*> (l->items[2]); CAST_INT (l->items[2], p);
if (!p) return false;
hs = p->i; hs = p->i;
sencode_bytes* a = dynamic_cast<sencode_bytes*> (l->items[3]); sencode_bytes* CAST_BYTES (l->items[3], a);
if (!a) return false;
check = std::vector<byte> (a->b.begin(), a->b.end() ); check = std::vector<byte> (a->b.begin(), a->b.end() );
return true; return true;
@ -612,22 +570,18 @@ sencode* encrypted_msg::serialize()
bool encrypted_msg::unserialize (sencode*s) bool encrypted_msg::unserialize (sencode*s)
{ {
sencode_list*L = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, L);
if (!L) return false;
if (L->items.size() != 4) return false; if (L->items.size() != 4) return false;
sencode_bytes*B; sencode_bytes*B;
B = dynamic_cast<sencode_bytes*> (L->items[0]); CAST_BYTES (L->items[0], B);
if (!B) return false;
if (B->b != ENC_MSG_IDENT) return false; if (B->b != ENC_MSG_IDENT) return false;
B = dynamic_cast<sencode_bytes*> (L->items[1]); CAST_BYTES (L->items[1], B);
if (!B) return false;
alg_id = B->b; alg_id = B->b;
B = dynamic_cast<sencode_bytes*> (L->items[2]); CAST_BYTES (L->items[2], B);
if (!B) return false;
key_id = B->b; key_id = B->b;
return ciphertext.unserialize (L->items[3]); return ciphertext.unserialize (L->items[3]);
@ -647,22 +601,18 @@ sencode* signed_msg::serialize()
bool signed_msg::unserialize (sencode*s) bool signed_msg::unserialize (sencode*s)
{ {
sencode_list*L = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, L);
if (!L) return false;
if (L->items.size() != 5) return false; if (L->items.size() != 5) return false;
sencode_bytes*B; sencode_bytes*B;
B = dynamic_cast<sencode_bytes*> (L->items[0]); CAST_BYTES (L->items[0], B);
if (!B) return false;
if (B->b != SIG_MSG_IDENT) return false; if (B->b != SIG_MSG_IDENT) return false;
B = dynamic_cast<sencode_bytes*> (L->items[1]); CAST_BYTES (L->items[1], B);
if (!B) return false;
alg_id = B->b; alg_id = B->b;
B = dynamic_cast<sencode_bytes*> (L->items[2]); CAST_BYTES (L->items[2], B);
if (!B) return false;
key_id = B->b; key_id = B->b;
return message.unserialize (L->items[3]) && return message.unserialize (L->items[3]) &&
@ -700,25 +650,20 @@ sencode* hashfile::serialize()
bool hashfile::unserialize (sencode*s) bool hashfile::unserialize (sencode*s)
{ {
sencode_list*L = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, L);
if (!L) return false;
if (L->items.size() < 1) return false; if (L->items.size() < 1) return false;
sencode_bytes*ID; sencode_bytes*ID;
ID = dynamic_cast<sencode_bytes*> (L->items[0]); CAST_BYTES (L->items[0], ID);
if (!ID) return false;
if (ID->b != HASHFILE_IDENT) return false; if (ID->b != HASHFILE_IDENT) return false;
for (uint pos = 1; pos < L->items.size(); ++pos) { for (uint pos = 1; pos < L->items.size(); ++pos) {
sencode_list*hash = dynamic_cast<sencode_list*> (L->items[pos]); sencode_list*CAST_LIST (L->items[pos], hash);
if (hash->items.size() != 2) return false; if (hash->items.size() != 2) return false;
sencode_bytes sencode_bytes*CAST_BYTES (hash->items[0], name);
*name = dynamic_cast<sencode_bytes*> (hash->items[0]), sencode_bytes*CAST_BYTES (hash->items[1], value);
*value = dynamic_cast<sencode_bytes*> (hash->items[1]);
if (!name || !value) return false;
//prevent multiple hash entries of same hash //prevent multiple hash entries of same hash
if (hashes.count (name->b) ) return false; if (hashes.count (name->b) ) return false;
@ -775,46 +720,38 @@ sencode* symkey::serialize()
bool symkey::unserialize (sencode*s) bool symkey::unserialize (sencode*s)
{ {
sencode_list*L = dynamic_cast<sencode_list*> (s); sencode_list*CAST_LIST (s, L);
if (!L) return false;
if (L->items.size() != 5) return false; if (L->items.size() != 5) return false;
sencode_bytes*ID; sencode_bytes*ID;
ID = dynamic_cast<sencode_bytes*> (L->items[0]); CAST_BYTES (L->items[0], ID);
if (!ID) return false;
if (ID->b != SYMKEY_IDENT) return false; if (ID->b != SYMKEY_IDENT) return false;
sencode_int*bs = dynamic_cast<sencode_int*> (L->items[3]); sencode_int*CAST_INT (L->items[3], bs);
if (!bs) return false;
blocksize = bs->i; blocksize = bs->i;
sencode_bytes*B; sencode_bytes*B;
B = dynamic_cast<sencode_bytes*> (L->items[4]); CAST_BYTES (L->items[4], B);
if (!B) return false;
seed.clear(); seed.clear();
seed.insert (seed.begin(), B->b.begin(), B->b.end() ); seed.insert (seed.begin(), B->b.begin(), B->b.end() );
sencode_list*LL; sencode_list*LL;
uint i; uint i;
LL = dynamic_cast<sencode_list*> (L->items[1]); CAST_LIST (L->items[1], LL);
if (!LL) return false;
ciphers.clear(); ciphers.clear();
for (i = 0; i < LL->items.size(); ++i) { for (i = 0; i < LL->items.size(); ++i) {
B = dynamic_cast<sencode_bytes*> (LL->items[i]); CAST_BYTES (LL->items[i], B);
if (!B) return false;
if (ciphers.count (B->b) ) return false; if (ciphers.count (B->b) ) return false;
ciphers.insert (B->b); ciphers.insert (B->b);
} }
LL = dynamic_cast<sencode_list*> (L->items[2]); CAST_LIST (L->items[2], LL);
if (!LL) return false;
hashes.clear(); hashes.clear();
for (i = 0; i < LL->items.size(); ++i) { for (i = 0; i < LL->items.size(); ++i) {
B = dynamic_cast<sencode_bytes*> (LL->items[i]); CAST_BYTES (LL->items[i], B);
if (!B) return false;
hashes.push_back (B->b); hashes.push_back (B->b);
} }