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