| 1 |
/* |
| 2 |
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this software |
| 19 |
* without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 22 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 25 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 |
* SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "gen_locl.h" |
| 35 |
|
| 36 |
RCSID("$Id$"); |
| 37 |
|
| 38 |
static void |
| 39 |
length_primitive (const char *typename, |
| 40 |
const char *name, |
| 41 |
const char *variable) |
| 42 |
{ |
| 43 |
fprintf (codefile, "%s += der_length_%s(%s);\n", variable, typename, name); |
| 44 |
} |
| 45 |
|
| 46 |
/* XXX same as der_length_tag */ |
| 47 |
static size_t |
| 48 |
length_tag(unsigned int tag) |
| 49 |
{ |
| 50 |
size_t len = 0; |
| 51 |
|
| 52 |
if(tag <= 30) |
| 53 |
return 1; |
| 54 |
while(tag) { |
| 55 |
tag /= 128; |
| 56 |
len++; |
| 57 |
} |
| 58 |
return len + 1; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
static int |
| 63 |
length_type (const char *name, const Type *t, |
| 64 |
const char *variable, const char *tmpstr) |
| 65 |
{ |
| 66 |
switch (t->type) { |
| 67 |
case TType: |
| 68 |
#if 0 |
| 69 |
length_type (name, t->symbol->type); |
| 70 |
#endif |
| 71 |
fprintf (codefile, "%s += length_%s(%s);\n", |
| 72 |
variable, t->symbol->gen_name, name); |
| 73 |
break; |
| 74 |
case TInteger: |
| 75 |
if(t->members) { |
| 76 |
fprintf(codefile, |
| 77 |
"{\n" |
| 78 |
"int enumint = *%s;\n", name); |
| 79 |
length_primitive ("integer", "&enumint", variable); |
| 80 |
fprintf(codefile, "}\n"); |
| 81 |
} else if (t->range == NULL) { |
| 82 |
length_primitive ("heim_integer", name, variable); |
| 83 |
} else if (t->range->min < INT_MIN && t->range->max <= INT64_MAX) { |
| 84 |
length_primitive ("integer64", name, variable); |
| 85 |
} else if (t->range->min >= 0 && t->range->max > UINT_MAX) { |
| 86 |
length_primitive ("unsigned64", name, variable); |
| 87 |
} else if (t->range->min >= INT_MIN && t->range->max <= INT_MAX) { |
| 88 |
length_primitive ("integer", name, variable); |
| 89 |
} else if (t->range->min >= 0 && t->range->max <= UINT_MAX) { |
| 90 |
length_primitive ("unsigned", name, variable); |
| 91 |
} else |
| 92 |
errx(1, "%s: unsupported range %lld -> %lld", |
| 93 |
name, (long long)t->range->min, (long long)t->range->max); |
| 94 |
break; |
| 95 |
case TBoolean: |
| 96 |
fprintf (codefile, "%s += 1;\n", variable); |
| 97 |
break; |
| 98 |
case TEnumerated : |
| 99 |
length_primitive ("enumerated", name, variable); |
| 100 |
break; |
| 101 |
case TOctetString: |
| 102 |
length_primitive ("octet_string", name, variable); |
| 103 |
break; |
| 104 |
case TBitString: { |
| 105 |
if (ASN1_TAILQ_EMPTY(t->members)) |
| 106 |
length_primitive("bit_string", name, variable); |
| 107 |
else { |
| 108 |
if (!rfc1510_bitstring) { |
| 109 |
Member *m; |
| 110 |
int pos = ASN1_TAILQ_LAST(t->members, memhead)->val; |
| 111 |
|
| 112 |
fprintf(codefile, |
| 113 |
"do {\n"); |
| 114 |
ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) { |
| 115 |
while (m->val / 8 < pos / 8) { |
| 116 |
pos -= 8; |
| 117 |
} |
| 118 |
fprintf (codefile, |
| 119 |
"if((%s)->%s) { %s += %d; break; }\n", |
| 120 |
name, m->gen_name, variable, (pos + 8) / 8); |
| 121 |
} |
| 122 |
fprintf(codefile, |
| 123 |
"} while(0);\n"); |
| 124 |
fprintf (codefile, "%s += 1;\n", variable); |
| 125 |
} else { |
| 126 |
fprintf (codefile, "%s += 5;\n", variable); |
| 127 |
} |
| 128 |
} |
| 129 |
break; |
| 130 |
} |
| 131 |
case TSet: |
| 132 |
case TSequence: |
| 133 |
case TChoice: { |
| 134 |
Member *m, *have_ellipsis = NULL; |
| 135 |
|
| 136 |
if (t->members == NULL) |
| 137 |
break; |
| 138 |
|
| 139 |
if(t->type == TChoice) |
| 140 |
fprintf (codefile, "switch((%s)->element) {\n", name); |
| 141 |
|
| 142 |
ASN1_TAILQ_FOREACH(m, t->members, members) { |
| 143 |
char *s; |
| 144 |
|
| 145 |
if (m->ellipsis) { |
| 146 |
have_ellipsis = m; |
| 147 |
continue; |
| 148 |
} |
| 149 |
|
| 150 |
if(t->type == TChoice) |
| 151 |
fprintf(codefile, "case %s:\n", m->label); |
| 152 |
|
| 153 |
if (asprintf (&s, "%s(%s)->%s%s", |
| 154 |
m->optional ? "" : "&", name, |
| 155 |
t->type == TChoice ? "u." : "", m->gen_name) < 0 || s == NULL) |
| 156 |
errx(1, "malloc"); |
| 157 |
if (m->optional) |
| 158 |
fprintf (codefile, "if(%s)", s); |
| 159 |
else if(m->defval) |
| 160 |
gen_compare_defval(s + 1, m->defval); |
| 161 |
fprintf (codefile, "{\n" |
| 162 |
"size_t %s_oldret = %s;\n" |
| 163 |
"%s = 0;\n", tmpstr, variable, variable); |
| 164 |
length_type (s, m->type, "ret", m->gen_name); |
| 165 |
fprintf (codefile, "ret += %s_oldret;\n", tmpstr); |
| 166 |
fprintf (codefile, "}\n"); |
| 167 |
free (s); |
| 168 |
if(t->type == TChoice) |
| 169 |
fprintf(codefile, "break;\n"); |
| 170 |
} |
| 171 |
if(t->type == TChoice) { |
| 172 |
if (have_ellipsis) |
| 173 |
fprintf(codefile, |
| 174 |
"case %s:\n" |
| 175 |
"ret += (%s)->u.%s.length;\n" |
| 176 |
"break;\n", |
| 177 |
have_ellipsis->label, |
| 178 |
name, |
| 179 |
have_ellipsis->gen_name); |
| 180 |
fprintf (codefile, "}\n"); /* switch */ |
| 181 |
} |
| 182 |
break; |
| 183 |
} |
| 184 |
case TSetOf: |
| 185 |
case TSequenceOf: { |
| 186 |
char *n = NULL; |
| 187 |
char *sname = NULL; |
| 188 |
|
| 189 |
fprintf (codefile, |
| 190 |
"{\n" |
| 191 |
"size_t %s_oldret = %s;\n" |
| 192 |
"unsigned int n_%s;\n" |
| 193 |
"%s = 0;\n", |
| 194 |
tmpstr, variable, tmpstr, variable); |
| 195 |
|
| 196 |
fprintf (codefile, "for(n_%s = (%s)->len; n_%s > 0; --n_%s){\n", |
| 197 |
tmpstr, name, tmpstr, tmpstr); |
| 198 |
fprintf (codefile, "size_t %s_for_oldret = %s;\n" |
| 199 |
"%s = 0;\n", tmpstr, variable, variable); |
| 200 |
if (asprintf (&n, "&(%s)->val[n_%s - 1]", name, tmpstr) < 0 || n == NULL) |
| 201 |
errx(1, "malloc"); |
| 202 |
if (asprintf (&sname, "%s_S_Of", tmpstr) < 0 || sname == NULL) |
| 203 |
errx(1, "malloc"); |
| 204 |
length_type(n, t->subtype, variable, sname); |
| 205 |
fprintf (codefile, "%s += %s_for_oldret;\n", |
| 206 |
variable, tmpstr); |
| 207 |
fprintf (codefile, "}\n"); |
| 208 |
|
| 209 |
fprintf (codefile, |
| 210 |
"%s += %s_oldret;\n" |
| 211 |
"}\n", variable, tmpstr); |
| 212 |
free(n); |
| 213 |
free(sname); |
| 214 |
break; |
| 215 |
} |
| 216 |
case TGeneralizedTime: |
| 217 |
length_primitive ("generalized_time", name, variable); |
| 218 |
break; |
| 219 |
case TGeneralString: |
| 220 |
length_primitive ("general_string", name, variable); |
| 221 |
break; |
| 222 |
case TTeletexString: |
| 223 |
length_primitive ("general_string", name, variable); |
| 224 |
break; |
| 225 |
case TUTCTime: |
| 226 |
length_primitive ("utctime", name, variable); |
| 227 |
break; |
| 228 |
case TUTF8String: |
| 229 |
length_primitive ("utf8string", name, variable); |
| 230 |
break; |
| 231 |
case TPrintableString: |
| 232 |
length_primitive ("printable_string", name, variable); |
| 233 |
break; |
| 234 |
case TIA5String: |
| 235 |
length_primitive ("ia5_string", name, variable); |
| 236 |
break; |
| 237 |
case TBMPString: |
| 238 |
length_primitive ("bmp_string", name, variable); |
| 239 |
break; |
| 240 |
case TUniversalString: |
| 241 |
length_primitive ("universal_string", name, variable); |
| 242 |
break; |
| 243 |
case TVisibleString: |
| 244 |
length_primitive ("visible_string", name, variable); |
| 245 |
break; |
| 246 |
case TNull: |
| 247 |
fprintf (codefile, "/* NULL */\n"); |
| 248 |
break; |
| 249 |
case TTag:{ |
| 250 |
char *tname = NULL; |
| 251 |
if (asprintf(&tname, "%s_tag", tmpstr) < 0 || tname == NULL) |
| 252 |
errx(1, "malloc"); |
| 253 |
length_type (name, t->subtype, variable, tname); |
| 254 |
fprintf (codefile, "ret += %lu + der_length_len (ret);\n", |
| 255 |
(unsigned long)length_tag(t->tag.tagvalue)); |
| 256 |
free(tname); |
| 257 |
break; |
| 258 |
} |
| 259 |
case TOID: |
| 260 |
length_primitive ("oid", name, variable); |
| 261 |
break; |
| 262 |
default : |
| 263 |
abort (); |
| 264 |
} |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
void |
| 269 |
generate_type_length (const Symbol *s) |
| 270 |
{ |
| 271 |
fprintf (codefile, |
| 272 |
"size_t ASN1CALL\n" |
| 273 |
"length_%s(const %s *data)\n" |
| 274 |
"{\n" |
| 275 |
"size_t ret = 0;\n", |
| 276 |
s->gen_name, s->gen_name); |
| 277 |
|
| 278 |
length_type ("data", s->type, "ret", "Top"); |
| 279 |
fprintf (codefile, "return ret;\n}\n\n"); |
| 280 |
} |
| 281 |
|