From 682c7deda691ce902f11e44c998d6e1237f35bf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Fri, 29 Dec 2006 17:29:13 +0000 Subject: [PATCH] Named bit strings have this horrible, disgusting, compress bits until they are no longer really there but stuff in an initial octet anyway encoding scheme. Try to get it right and calculate the initial octet runtime instead of compiletime. git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@19570 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/asn1/gen_encode.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/asn1/gen_encode.c b/lib/asn1/gen_encode.c index 5cc917bea..09888040a 100644 --- a/lib/asn1/gen_encode.c +++ b/lib/asn1/gen_encode.c @@ -151,7 +151,6 @@ encode_type (const char *name, const Type *t, const char *tmpstr) case TBitString: { Member *m; int pos; - int rest; if (ASN1_TAILQ_EMPTY(t->members)) { encode_primitive("bit_string", name); @@ -163,6 +162,7 @@ encode_type (const char *name, const Type *t, const char *tmpstr) "unsigned char c = 0;\n"); if (!rfc1510_bitstring) fprintf (codefile, + "int rest = 0;\n" "int bit_set = 0;\n"); #if 0 pos = t->members->prev->val; @@ -181,9 +181,7 @@ encode_type (const char *name, const Type *t, const char *tmpstr) if (rfc1510_bitstring) { if (pos < 31) pos = 31; - rest = 7 - (pos % 8); - } else - rest = 0; + } ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) { while (m->val / 8 < pos / 8) { @@ -192,20 +190,27 @@ encode_type (const char *name, const Type *t, const char *tmpstr) "if (c != 0 || bit_set) {\n"); fprintf (codefile, "if (len < 1) return ASN1_OVERFLOW;\n" - "*p-- = c; len--; ret++;\n" - "c = 0;\n"); + "*p-- = c; len--; ret++;\n"); if (!rfc1510_bitstring) fprintf (codefile, + "if (!bit_set) {\n" + "rest = 0;\n" + "while(c) { \n" + "if (c & 1) break;\n" + "c = c >> 1;\n" + "rest++;\n" + "}\n" "bit_set = 1;\n" + "}\n" "}\n"); + fprintf (codefile, + "c = 0;\n"); pos -= 8; } fprintf (codefile, "if((%s)->%s) {\n" "c |= 1<<%d;\n", name, m->gen_name, 7 - m->val % 8); - if (!rfc1510_bitstring) - rest = 7 - m->val % 8; fprintf (codefile, "}\n"); } @@ -218,15 +223,25 @@ encode_type (const char *name, const Type *t, const char *tmpstr) "*p-- = c; len--; ret++;\n"); if (!rfc1510_bitstring) fprintf (codefile, + "if (!bit_set) {\n" + "rest = 0;\n" + "if(c) { \n" + "while(c) { \n" + "if (c & 1) break;\n" + "c = c >> 1;\n" + "rest++;\n" + "}\n" + "}\n" + "}\n" "}\n"); - + fprintf (codefile, "if (len < 1) return ASN1_OVERFLOW;\n" - "*p-- = %d;\n" + "*p-- = %s;\n" "len -= 1;\n" "ret += 1;\n" "}\n\n", - rest); + rfc1510_bitstring ? "0" : "rest"); constructed = 0; break; }