xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/asn1/gen_decode.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: gen_decode.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "gen_locl.h"
37 #include "lex.h"
38 
39 __RCSID("NetBSD");
40 
41 static void
42 decode_primitive (const char *typename, const char *name, const char *forwstr)
43 {
44 #if 0
45     fprintf (codefile,
46 	     "e = decode_%s(p, len, %s, &l);\n"
47 	     "%s;\n",
48 	     typename,
49 	     name,
50 	     forwstr);
51 #else
52     fprintf (codefile,
53 	     "e = der_get_%s(p, len, %s, &l);\n"
54 	     "if(e) %s;\np += l; len -= l; ret += l;\n",
55 	     typename,
56 	     name,
57 	     forwstr);
58 #endif
59 }
60 
61 static void
62 find_tag (const Type *t,
63 	  Der_class *cl, Der_type *ty, unsigned *tag)
64 {
65     switch (t->type) {
66     case TBitString:
67 	*cl  = ASN1_C_UNIV;
68 	*ty  = PRIM;
69 	*tag = UT_BitString;
70 	break;
71     case TBoolean:
72 	*cl  = ASN1_C_UNIV;
73 	*ty  = PRIM;
74 	*tag = UT_Boolean;
75 	break;
76     case TChoice:
77 	errx(1, "Cannot have recursive CHOICE");
78     case TEnumerated:
79 	*cl  = ASN1_C_UNIV;
80 	*ty  = PRIM;
81 	*tag = UT_Enumerated;
82 	break;
83     case TGeneralString:
84 	*cl  = ASN1_C_UNIV;
85 	*ty  = PRIM;
86 	*tag = UT_GeneralString;
87 	break;
88     case TTeletexString:
89 	*cl  = ASN1_C_UNIV;
90 	*ty  = PRIM;
91 	*tag = UT_TeletexString;
92 	break;
93     case TGeneralizedTime:
94 	*cl  = ASN1_C_UNIV;
95 	*ty  = PRIM;
96 	*tag = UT_GeneralizedTime;
97 	break;
98     case TIA5String:
99 	*cl  = ASN1_C_UNIV;
100 	*ty  = PRIM;
101 	*tag = UT_IA5String;
102 	break;
103     case TInteger:
104 	*cl  = ASN1_C_UNIV;
105 	*ty  = PRIM;
106 	*tag = UT_Integer;
107 	break;
108     case TNull:
109 	*cl  = ASN1_C_UNIV;
110 	*ty  = PRIM;
111 	*tag = UT_Null;
112 	break;
113     case TOID:
114 	*cl  = ASN1_C_UNIV;
115 	*ty  = PRIM;
116 	*tag = UT_OID;
117 	break;
118     case TOctetString:
119 	*cl  = ASN1_C_UNIV;
120 	*ty  = PRIM;
121 	*tag = UT_OctetString;
122 	break;
123     case TPrintableString:
124 	*cl  = ASN1_C_UNIV;
125 	*ty  = PRIM;
126 	*tag = UT_PrintableString;
127 	break;
128     case TSequence:
129     case TSequenceOf:
130 	*cl  = ASN1_C_UNIV;
131 	*ty  = CONS;
132 	*tag = UT_Sequence;
133 	break;
134     case TSet:
135     case TSetOf:
136 	*cl  = ASN1_C_UNIV;
137 	*ty  = CONS;
138 	*tag = UT_Set;
139 	break;
140     case TTag:
141 	*cl  = t->tag.tagclass;
142 	*ty  = is_primitive_type(t->subtype->type) ? PRIM : CONS;
143 	*tag = t->tag.tagvalue;
144 	break;
145     case TType:
146 	if ((t->symbol->stype == Stype && t->symbol->type == NULL)
147 	    || t->symbol->stype == SUndefined) {
148 	    lex_error_message("%s is imported or still undefined, "
149 			      " can't generate tag checking data in CHOICE "
150 			      "without this information",
151 			      t->symbol->name);
152 	    exit(1);
153 	}
154 	find_tag(t->symbol->type, cl, ty, tag);
155 	return;
156     case TUTCTime:
157 	*cl  = ASN1_C_UNIV;
158 	*ty  = PRIM;
159 	*tag = UT_UTCTime;
160 	break;
161     case TUTF8String:
162 	*cl  = ASN1_C_UNIV;
163 	*ty  = PRIM;
164 	*tag = UT_UTF8String;
165 	break;
166     case TBMPString:
167 	*cl  = ASN1_C_UNIV;
168 	*ty  = PRIM;
169 	*tag = UT_BMPString;
170 	break;
171     case TUniversalString:
172 	*cl  = ASN1_C_UNIV;
173 	*ty  = PRIM;
174 	*tag = UT_UniversalString;
175 	break;
176     case TVisibleString:
177 	*cl  = ASN1_C_UNIV;
178 	*ty  = PRIM;
179 	*tag = UT_VisibleString;
180 	break;
181     default:
182 	abort();
183     }
184 }
185 
186 static void
187 range_check(const char *name,
188 	    const char *length,
189 	    const char *forwstr,
190 	    struct range *r)
191 {
192     if (r->min == r->max + 2 || r->min < r->max)
193 	fprintf (codefile,
194 		 "if ((%s)->%s > %d) {\n"
195 		 "e = ASN1_MAX_CONSTRAINT; %s;\n"
196 		 "}\n",
197 		 name, length, r->max, forwstr);
198     if (r->min - 1 == r->max || r->min < r->max)
199 	fprintf (codefile,
200 		 "if ((%s)->%s < %d) {\n"
201 		 "e = ASN1_MIN_CONSTRAINT; %s;\n"
202 		 "}\n",
203 		 name, length, r->min, forwstr);
204     if (r->max == r->min)
205 	fprintf (codefile,
206 		 "if ((%s)->%s != %d) {\n"
207 		 "e = ASN1_EXACT_CONSTRAINT; %s;\n"
208 		 "}\n",
209 		 name, length, r->min, forwstr);
210 }
211 
212 static int
213 decode_type (const char *name, const Type *t, int optional,
214 	     const char *forwstr, const char *tmpstr, const char *dertype,
215 	     unsigned int depth)
216 {
217     switch (t->type) {
218     case TType: {
219 	if (optional)
220 	    fprintf(codefile,
221 		    "%s = calloc(1, sizeof(*%s));\n"
222 		    "if (%s == NULL) %s;\n",
223 		    name, name, name, forwstr);
224 	fprintf (codefile,
225 		 "e = decode_%s(p, len, %s, &l);\n",
226 		 t->symbol->gen_name, name);
227 	if (optional) {
228 	    fprintf (codefile,
229 		     "if(e) {\n"
230 		     "free(%s);\n"
231 		     "%s = NULL;\n"
232 		     "} else {\n"
233 		     "p += l; len -= l; ret += l;\n"
234 		     "}\n",
235 		     name, name);
236 	} else {
237 	    fprintf (codefile,
238 		     "if(e) %s;\n",
239 		     forwstr);
240 	    fprintf (codefile,
241 		     "p += l; len -= l; ret += l;\n");
242 	}
243 	break;
244     }
245     case TInteger:
246 	if(t->members) {
247 	    fprintf(codefile,
248 		    "{\n"
249 		    "int enumint;\n");
250 	    decode_primitive ("integer", "&enumint", forwstr);
251 	    fprintf(codefile,
252 		    "*%s = enumint;\n"
253 		    "}\n",
254 		    name);
255 	} else if (t->range == NULL) {
256 	    decode_primitive ("heim_integer", name, forwstr);
257 	} else if (t->range->min == INT_MIN && t->range->max == INT_MAX) {
258 	    decode_primitive ("integer", name, forwstr);
259 	} else if (t->range->min == 0 && t->range->max == UINT_MAX) {
260 	    decode_primitive ("unsigned", name, forwstr);
261 	} else if (t->range->min == 0 && t->range->max == INT_MAX) {
262 	    decode_primitive ("unsigned", name, forwstr);
263 	} else
264 	    errx(1, "%s: unsupported range %d -> %d",
265 		 name, t->range->min, t->range->max);
266 	break;
267     case TBoolean:
268       decode_primitive ("boolean", name, forwstr);
269       break;
270     case TEnumerated:
271 	decode_primitive ("enumerated", name, forwstr);
272 	break;
273     case TOctetString:
274 	if (dertype) {
275 	    fprintf(codefile,
276 		    "if (%s == CONS) {\n",
277 		    dertype);
278 	    decode_primitive("octet_string_ber", name, forwstr);
279 	    fprintf(codefile,
280 		    "} else {\n");
281 	}
282 	decode_primitive ("octet_string", name, forwstr);
283 	if (dertype)
284 	    fprintf(codefile, "}\n");
285 	if (t->range)
286 	    range_check(name, "length", forwstr, t->range);
287 	break;
288     case TBitString: {
289 	Member *m;
290 	int pos = 0;
291 
292 	if (ASN1_TAILQ_EMPTY(t->members)) {
293 	    decode_primitive ("bit_string", name, forwstr);
294 	    break;
295 	}
296 	fprintf(codefile,
297 		"if (len < 1) return ASN1_OVERRUN;\n"
298 		"p++; len--; ret++;\n");
299 	fprintf(codefile,
300 		"do {\n"
301 		"if (len < 1) break;\n");
302 	ASN1_TAILQ_FOREACH(m, t->members, members) {
303 	    while (m->val / 8 > pos / 8) {
304 		fprintf (codefile,
305 			 "p++; len--; ret++;\n"
306 			 "if (len < 1) break;\n");
307 		pos += 8;
308 	    }
309 	    fprintf (codefile,
310 		     "(%s)->%s = (*p >> %d) & 1;\n",
311 		     name, m->gen_name, 7 - m->val % 8);
312 	}
313 	fprintf(codefile,
314 		"} while(0);\n");
315 	fprintf (codefile,
316 		 "p += len; ret += len;\n");
317 	break;
318     }
319     case TSequence: {
320 	Member *m;
321 
322 	if (t->members == NULL)
323 	    break;
324 
325 	ASN1_TAILQ_FOREACH(m, t->members, members) {
326 	    char *s = NULL;
327 
328 	    if (m->ellipsis)
329 		continue;
330 
331 	    if (asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&",
332 			  name, m->gen_name) < 0 || s == NULL)
333 		errx(1, "malloc");
334 	    decode_type (s, m->type, m->optional, forwstr, m->gen_name, NULL,
335 		depth + 1);
336 	    free (s);
337 	}
338 
339 	break;
340     }
341     case TSet: {
342 	Member *m;
343 	unsigned int memno;
344 
345 	if(t->members == NULL)
346 	    break;
347 
348 	fprintf(codefile, "{\n");
349 	fprintf(codefile, "unsigned int members = 0;\n");
350 	fprintf(codefile, "while(len > 0) {\n");
351 	fprintf(codefile,
352 		"Der_class class;\n"
353 		"Der_type type;\n"
354 		"int tag;\n"
355 		"e = der_get_tag (p, len, &class, &type, &tag, NULL);\n"
356 		"if(e) %s;\n", forwstr);
357 	fprintf(codefile, "switch (MAKE_TAG(class, type, tag)) {\n");
358 	memno = 0;
359 	ASN1_TAILQ_FOREACH(m, t->members, members) {
360 	    char *s;
361 
362 	    assert(m->type->type == TTag);
363 
364 	    fprintf(codefile, "case MAKE_TAG(%s, %s, %s):\n",
365 		    classname(m->type->tag.tagclass),
366 		    is_primitive_type(m->type->subtype->type) ? "PRIM" : "CONS",
367 		    valuename(m->type->tag.tagclass, m->type->tag.tagvalue));
368 
369 	    if (asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name) < 0 || s == NULL)
370 		errx(1, "malloc");
371 	    if(m->optional)
372 		fprintf(codefile,
373 			"%s = calloc(1, sizeof(*%s));\n"
374 			"if (%s == NULL) { e = ENOMEM; %s; }\n",
375 			s, s, s, forwstr);
376 	    decode_type (s, m->type, 0, forwstr, m->gen_name, NULL, depth + 1);
377 	    free (s);
378 
379 	    fprintf(codefile, "members |= (1 << %d);\n", memno);
380 	    memno++;
381 	    fprintf(codefile, "break;\n");
382 	}
383 	fprintf(codefile,
384 		"default:\n"
385 		"return ASN1_MISPLACED_FIELD;\n"
386 		"break;\n");
387 	fprintf(codefile, "}\n");
388 	fprintf(codefile, "}\n");
389 	memno = 0;
390 	ASN1_TAILQ_FOREACH(m, t->members, members) {
391 	    char *s;
392 
393 	    if (asprintf (&s, "%s->%s", name, m->gen_name) < 0 || s == NULL)
394 		errx(1, "malloc");
395 	    fprintf(codefile, "if((members & (1 << %d)) == 0)\n", memno);
396 	    if(m->optional)
397 		fprintf(codefile, "%s = NULL;\n", s);
398 	    else if(m->defval)
399 		gen_assign_defval(s, m->defval);
400 	    else
401 		fprintf(codefile, "return ASN1_MISSING_FIELD;\n");
402 	    free(s);
403 	    memno++;
404 	}
405 	fprintf(codefile, "}\n");
406 	break;
407     }
408     case TSetOf:
409     case TSequenceOf: {
410 	char *n = NULL;
411 	char *sname = NULL;
412 
413 	fprintf (codefile,
414 		 "{\n"
415 		 "size_t %s_origlen = len;\n"
416 		 "size_t %s_oldret = ret;\n"
417 		 "size_t %s_olen = 0;\n"
418 		 "void *%s_tmp;\n"
419 		 "ret = 0;\n"
420 		 "(%s)->len = 0;\n"
421 		 "(%s)->val = NULL;\n",
422 		 tmpstr,
423 		 tmpstr,
424 		 tmpstr,
425 		 tmpstr,
426 		 name,
427 		 name);
428 
429 	fprintf (codefile,
430 		 "while(ret < %s_origlen) {\n"
431 		 "size_t %s_nlen = %s_olen + sizeof(*((%s)->val));\n"
432 		 "if (%s_olen > %s_nlen) { e = ASN1_OVERFLOW; %s; }\n"
433 		 "%s_olen = %s_nlen;\n"
434 		 "%s_tmp = realloc((%s)->val, %s_olen);\n"
435 		 "if (%s_tmp == NULL) { e = ENOMEM; %s; }\n"
436 		 "(%s)->val = %s_tmp;\n",
437 		 tmpstr,
438 		 tmpstr, tmpstr, name,
439 		 tmpstr, tmpstr, forwstr,
440 		 tmpstr, tmpstr,
441 		 tmpstr, name, tmpstr,
442 		 tmpstr, forwstr,
443 		 name, tmpstr);
444 
445 	if (asprintf (&n, "&(%s)->val[(%s)->len]", name, name) < 0 || n == NULL)
446 	    errx(1, "malloc");
447 	if (asprintf (&sname, "%s_s_of", tmpstr) < 0 || sname == NULL)
448 	    errx(1, "malloc");
449 	decode_type (n, t->subtype, 0, forwstr, sname, NULL, depth + 1);
450 	fprintf (codefile,
451 		 "(%s)->len++;\n"
452 		 "len = %s_origlen - ret;\n"
453 		 "}\n"
454 		 "ret += %s_oldret;\n"
455 		 "}\n",
456 		 name,
457 		 tmpstr, tmpstr);
458 	if (t->range)
459 	    range_check(name, "len", forwstr, t->range);
460 	free (n);
461 	free (sname);
462 	break;
463     }
464     case TGeneralizedTime:
465 	decode_primitive ("generalized_time", name, forwstr);
466 	break;
467     case TGeneralString:
468 	decode_primitive ("general_string", name, forwstr);
469 	break;
470     case TTeletexString:
471 	decode_primitive ("general_string", name, forwstr);
472 	break;
473     case TTag:{
474     	char *tname = NULL, *typestring = NULL;
475 	char *ide = NULL;
476 
477 	if (asprintf(&typestring, "%s_type", tmpstr) < 0 || typestring == NULL)
478 	    errx(1, "malloc");
479 
480 	fprintf(codefile,
481 		"{\n"
482 		"size_t %s_datalen, %s_oldlen;\n"
483 		"Der_type %s;\n",
484 		tmpstr, tmpstr, typestring);
485 	if(support_ber)
486 	    fprintf(codefile,
487 		    "int is_indefinite%u;\n", depth);
488 
489 	fprintf(codefile, "e = der_match_tag_and_length(p, len, %s, &%s, %s, "
490 		"&%s_datalen, &l);\n",
491 		classname(t->tag.tagclass),
492 		typestring,
493 		valuename(t->tag.tagclass, t->tag.tagvalue),
494 		tmpstr);
495 
496 	/* XXX hardcode for now */
497 	if (support_ber && t->subtype->type == TOctetString) {
498 	    ide = typestring;
499 	} else {
500 	    fprintf(codefile,
501 		    "if (e == 0 && %s != %s) { e = ASN1_BAD_ID; }\n",
502 		    typestring,
503 		    is_primitive_type(t->subtype->type) ? "PRIM" : "CONS");
504 	}
505 
506 	if(optional) {
507 	    fprintf(codefile,
508 		    "if(e) {\n"
509 		    "%s = NULL;\n"
510 		    "} else {\n"
511 		     "%s = calloc(1, sizeof(*%s));\n"
512 		     "if (%s == NULL) { e = ENOMEM; %s; }\n",
513 		     name, name, name, name, forwstr);
514 	} else {
515 	    fprintf(codefile, "if(e) %s;\n", forwstr);
516 	}
517 	fprintf (codefile,
518 		 "p += l; len -= l; ret += l;\n"
519 		 "%s_oldlen = len;\n",
520 		 tmpstr);
521 	if(support_ber)
522 	    fprintf (codefile,
523 		     "if((is_indefinite%u = _heim_fix_dce(%s_datalen, &len)) < 0)\n"
524 		     "{ e = ASN1_BAD_FORMAT; %s; }\n"
525 		     "if (is_indefinite%u) { if (len < 2) { e = ASN1_OVERRUN; %s; } len -= 2; }",
526 		     depth, tmpstr, forwstr, depth, forwstr);
527 	else
528 	    fprintf(codefile,
529 		    "if (%s_datalen > len) { e = ASN1_OVERRUN; %s; }\n"
530 		    "len = %s_datalen;\n", tmpstr, forwstr, tmpstr);
531 	if (asprintf (&tname, "%s_Tag", tmpstr) < 0 || tname == NULL)
532 	    errx(1, "malloc");
533 	decode_type (name, t->subtype, 0, forwstr, tname, ide, depth + 1);
534 	if(support_ber)
535 	    fprintf(codefile,
536 		    "if(is_indefinite%u){\n"
537 		    "len += 2;\n"
538 		    "e = der_match_tag_and_length(p, len, "
539 		    "(Der_class)0, &%s, UT_EndOfContent, "
540 		    "&%s_datalen, &l);\n"
541 		    "if(e) %s;\n"
542 		    "p += l; len -= l; ret += l;\n"
543 		    "if (%s != (Der_type)0) { e = ASN1_BAD_ID; %s; }\n"
544 		    "} else \n",
545 		    depth,
546 		    typestring,
547 		    tmpstr,
548 		    forwstr,
549 		    typestring, forwstr);
550 	fprintf(codefile,
551 		"len = %s_oldlen - %s_datalen;\n",
552 		tmpstr, tmpstr);
553 	if(optional)
554 	    fprintf(codefile,
555 		    "}\n");
556 	fprintf(codefile,
557 		"}\n");
558 	free(tname);
559 	free(typestring);
560 	break;
561     }
562     case TChoice: {
563 	Member *m, *have_ellipsis = NULL;
564 	const char *els = "";
565 
566 	if (t->members == NULL)
567 	    break;
568 
569 	ASN1_TAILQ_FOREACH(m, t->members, members) {
570 	    const Type *tt = m->type;
571 	    char *s = NULL;
572 	    Der_class cl;
573 	    Der_type  ty;
574 	    unsigned  tag;
575 
576 	    if (m->ellipsis) {
577 		have_ellipsis = m;
578 		continue;
579 	    }
580 
581 	    find_tag(tt, &cl, &ty, &tag);
582 
583 	    fprintf(codefile,
584 		    "%sif (der_match_tag(p, len, %s, %s, %s, NULL) == 0) {\n",
585 		    els,
586 		    classname(cl),
587 		    ty ? "CONS" : "PRIM",
588 		    valuename(cl, tag));
589 	    if (asprintf (&s, "%s(%s)->u.%s", m->optional ? "" : "&",
590 			  name, m->gen_name) < 0 || s == NULL)
591 		errx(1, "malloc");
592 	    decode_type (s, m->type, m->optional, forwstr, m->gen_name, NULL,
593 		depth + 1);
594 	    fprintf(codefile,
595 		    "(%s)->element = %s;\n",
596 		    name, m->label);
597 	    free(s);
598 	    fprintf(codefile,
599 		    "}\n");
600 	    els = "else ";
601 	}
602 	if (have_ellipsis) {
603 	    fprintf(codefile,
604 		    "else {\n"
605 		    "(%s)->u.%s.data = calloc(1, len);\n"
606 		    "if ((%s)->u.%s.data == NULL) {\n"
607 		    "e = ENOMEM; %s;\n"
608 		    "}\n"
609 		    "(%s)->u.%s.length = len;\n"
610 		    "memcpy((%s)->u.%s.data, p, len);\n"
611 		    "(%s)->element = %s;\n"
612 		    "p += len;\n"
613 		    "ret += len;\n"
614 		    "len = 0;\n"
615 		    "}\n",
616 		    name, have_ellipsis->gen_name,
617 		    name, have_ellipsis->gen_name,
618 		    forwstr,
619 		    name, have_ellipsis->gen_name,
620 		    name, have_ellipsis->gen_name,
621 		    name, have_ellipsis->label);
622 	} else {
623 	    fprintf(codefile,
624 		    "else {\n"
625 		    "e = ASN1_PARSE_ERROR;\n"
626 		    "%s;\n"
627 		    "}\n",
628 		    forwstr);
629 	}
630 	break;
631     }
632     case TUTCTime:
633 	decode_primitive ("utctime", name, forwstr);
634 	break;
635     case TUTF8String:
636 	decode_primitive ("utf8string", name, forwstr);
637 	break;
638     case TPrintableString:
639 	decode_primitive ("printable_string", name, forwstr);
640 	break;
641     case TIA5String:
642 	decode_primitive ("ia5_string", name, forwstr);
643 	break;
644     case TBMPString:
645 	decode_primitive ("bmp_string", name, forwstr);
646 	break;
647     case TUniversalString:
648 	decode_primitive ("universal_string", name, forwstr);
649 	break;
650     case TVisibleString:
651 	decode_primitive ("visible_string", name, forwstr);
652 	break;
653     case TNull:
654 	fprintf (codefile, "/* NULL */\n");
655 	break;
656     case TOID:
657 	decode_primitive ("oid", name, forwstr);
658 	break;
659     default :
660 	abort ();
661     }
662     return 0;
663 }
664 
665 void
666 generate_type_decode (const Symbol *s)
667 {
668     int preserve = preserve_type(s->name) ? TRUE : FALSE;
669 
670     fprintf (codefile, "int ASN1CALL\n"
671 	     "decode_%s(const unsigned char *p HEIMDAL_UNUSED_ATTRIBUTE,"
672 	     " size_t len HEIMDAL_UNUSED_ATTRIBUTE, %s *data, size_t *size)\n"
673 	     "{\n",
674 	     s->gen_name, s->gen_name);
675 
676     switch (s->type->type) {
677     case TInteger:
678     case TBoolean:
679     case TOctetString:
680     case TOID:
681     case TGeneralizedTime:
682     case TGeneralString:
683     case TTeletexString:
684     case TUTF8String:
685     case TPrintableString:
686     case TIA5String:
687     case TBMPString:
688     case TUniversalString:
689     case TVisibleString:
690     case TUTCTime:
691     case TNull:
692     case TEnumerated:
693     case TBitString:
694     case TSequence:
695     case TSequenceOf:
696     case TSet:
697     case TSetOf:
698     case TTag:
699     case TType:
700     case TChoice:
701 	fprintf (codefile,
702 		 "size_t ret = 0;\n"
703 		 "size_t l HEIMDAL_UNUSED_ATTRIBUTE;\n"
704 		 "int e HEIMDAL_UNUSED_ATTRIBUTE;\n");
705 	if (preserve)
706 	    fprintf (codefile, "const unsigned char *begin = p;\n");
707 
708 	fprintf (codefile, "\n");
709 	fprintf (codefile, "memset(data, 0, sizeof(*data));\n"); /* hack to avoid `unused variable' */
710 
711 	decode_type ("data", s->type, 0, "goto fail", "Top", NULL, 1);
712 	if (preserve)
713 	    fprintf (codefile,
714 		     "data->_save.data = calloc(1, ret);\n"
715 		     "if (data->_save.data == NULL) { \n"
716 		     "e = ENOMEM; goto fail; \n"
717 		     "}\n"
718 		     "data->_save.length = ret;\n"
719 		     "memcpy(data->_save.data, begin, ret);\n");
720 	fprintf (codefile,
721 		 "if(size) *size = ret;\n"
722 		 "return 0;\n");
723 	fprintf (codefile,
724 		 "fail:\n"
725 		 "free_%s(data);\n"
726 		 "return e;\n",
727 		 s->gen_name);
728 	break;
729     default:
730 	abort ();
731     }
732     fprintf (codefile, "}\n\n");
733 }
734