quiet warnings
[heimdal.git] / lib / asn1 / gen.c
blobe216b8b7cb448848fa42e6b88124369482dcf544
1 /*
2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
36 #include "gen_locl.h"
38 RCSID("$Id$");
40 FILE *privheaderfile, *headerfile, *codefile, *logfile, *templatefile;
42 #define STEM "asn1"
44 static const char *orig_filename;
45 static char *privheader, *header, *template;
46 static const char *headerbase = STEM;
49 * list of all IMPORTs
52 struct import {
53 const char *module;
54 struct import *next;
57 static struct import *imports = NULL;
59 void
60 add_import (const char *module)
62 struct import *tmp = emalloc (sizeof(*tmp));
64 tmp->module = module;
65 tmp->next = imports;
66 imports = tmp;
68 fprintf (headerfile, "#include <%s_asn1.h>\n", module);
72 * List of all exported symbols
75 struct sexport {
76 const char *name;
77 int defined;
78 struct sexport *next;
81 static struct sexport *exports = NULL;
83 void
84 add_export (const char *name)
86 struct sexport *tmp = emalloc (sizeof(*tmp));
88 tmp->name = name;
89 tmp->next = exports;
90 exports = tmp;
93 int
94 is_export(const char *name)
96 struct sexport *tmp;
98 if (exports == NULL) /* no export list, all exported */
99 return 1;
101 for (tmp = exports; tmp != NULL; tmp = tmp->next) {
102 if (strcmp(tmp->name, name) == 0) {
103 tmp->defined = 1;
104 return 1;
107 return 0;
110 const char *
111 get_filename (void)
113 return orig_filename;
116 void
117 init_generate (const char *filename, const char *base)
119 char *fn = NULL;
121 orig_filename = filename;
122 if (base != NULL) {
123 headerbase = strdup(base);
124 if (headerbase == NULL)
125 errx(1, "strdup");
128 /* public header file */
129 if (asprintf(&header, "%s.h", headerbase) < 0 || header == NULL)
130 errx(1, "malloc");
131 if (asprintf(&fn, "%s.hx", headerbase) < 0 || fn == NULL)
132 errx(1, "malloc");
133 headerfile = fopen (fn, "w");
134 if (headerfile == NULL)
135 err (1, "open %s", fn);
136 free(fn);
137 fn = NULL;
139 /* private header file */
140 if (asprintf(&privheader, "%s-priv.h", headerbase) < 0 || privheader == NULL)
141 errx(1, "malloc");
142 if (asprintf(&fn, "%s-priv.hx", headerbase) < 0 || fn == NULL)
143 errx(1, "malloc");
144 privheaderfile = fopen (fn, "w");
145 if (privheaderfile == NULL)
146 err (1, "open %s", fn);
147 free(fn);
148 fn = NULL;
150 /* template file */
151 if (asprintf(&template, "%s-template.x", headerbase) < 0 || template == NULL)
152 errx(1, "malloc");
153 fprintf (headerfile,
154 "/* Generated from %s */\n"
155 "/* Do not edit */\n\n",
156 filename);
157 fprintf (headerfile,
158 "#ifndef __%s_h__\n"
159 "#define __%s_h__\n\n", headerbase, headerbase);
160 fprintf (headerfile,
161 "#include <stddef.h>\n"
162 "#include <time.h>\n\n");
163 fprintf (headerfile,
164 "#ifndef __asn1_common_definitions__\n"
165 "#define __asn1_common_definitions__\n\n");
166 fprintf (headerfile,
167 "#ifndef __HEIM_BASE_DATA__\n"
168 "#define __HEIM_BASE_DATA__ 1\n"
169 "struct heim_base_data {\n"
170 " size_t length;\n"
171 " void *data;\n"
172 "};\n"
173 "typedef struct heim_base_data heim_octet_string;\n"
174 "#endif\n\n");
175 fprintf (headerfile,
176 "typedef struct heim_integer {\n"
177 " size_t length;\n"
178 " void *data;\n"
179 " int negative;\n"
180 "} heim_integer;\n\n");
181 fprintf (headerfile,
182 "typedef char *heim_general_string;\n\n"
184 fprintf (headerfile,
185 "typedef char *heim_utf8_string;\n\n"
187 fprintf (headerfile,
188 "typedef struct heim_base_data heim_printable_string;\n\n"
190 fprintf (headerfile,
191 "typedef struct heim_base_data heim_ia5_string;\n\n"
193 fprintf (headerfile,
194 "typedef struct heim_bmp_string {\n"
195 " size_t length;\n"
196 " uint16_t *data;\n"
197 "} heim_bmp_string;\n\n");
198 fprintf (headerfile,
199 "typedef struct heim_universal_string {\n"
200 " size_t length;\n"
201 " uint32_t *data;\n"
202 "} heim_universal_string;\n\n");
203 fprintf (headerfile,
204 "typedef char *heim_visible_string;\n\n"
206 fprintf (headerfile,
207 "typedef struct heim_oid {\n"
208 " size_t length;\n"
209 " unsigned *components;\n"
210 "} heim_oid;\n\n");
211 fprintf (headerfile,
212 "typedef struct heim_bit_string {\n"
213 " size_t length;\n"
214 " void *data;\n"
215 "} heim_bit_string;\n\n");
216 fprintf (headerfile,
217 "typedef struct heim_base_data heim_any;\n"
218 "typedef struct heim_base_data heim_any_set;\n\n");
219 fputs("#define ASN1_MALLOC_ENCODE(T, B, BL, S, L, R) \\\n"
220 " do { \\\n"
221 " (BL) = length_##T((S)); \\\n"
222 " (B) = malloc((BL)); \\\n"
223 " if((B) == NULL) { \\\n"
224 " (R) = ENOMEM; \\\n"
225 " } else { \\\n"
226 " (R) = encode_##T(((unsigned char*)(B)) + (BL) - 1, (BL), \\\n"
227 " (S), (L)); \\\n"
228 " if((R) != 0) { \\\n"
229 " free((B)); \\\n"
230 " (B) = NULL; \\\n"
231 " } \\\n"
232 " } \\\n"
233 " } while (0)\n\n",
234 headerfile);
235 fputs("#ifdef _WIN32\n"
236 "#ifndef ASN1_LIB\n"
237 "#define ASN1EXP __declspec(dllimport)\n"
238 "#else\n"
239 "#define ASN1EXP\n"
240 "#endif\n"
241 "#define ASN1CALL __stdcall\n"
242 "#else\n"
243 "#define ASN1EXP\n"
244 "#define ASN1CALL\n"
245 "#endif\n",
246 headerfile);
247 fprintf (headerfile, "struct units;\n\n");
248 fprintf (headerfile, "#endif\n\n");
249 if (asprintf(&fn, "%s_files", base) < 0 || fn == NULL)
250 errx(1, "malloc");
251 logfile = fopen(fn, "w");
252 if (logfile == NULL)
253 err (1, "open %s", fn);
255 /* if one code file, write into the one codefile */
256 if (one_code_file)
257 return;
259 templatefile = fopen (template, "w");
260 if (templatefile == NULL)
261 err (1, "open %s", template);
263 fprintf (templatefile,
264 "/* Generated from %s */\n"
265 "/* Do not edit */\n\n"
266 "#include <stdio.h>\n"
267 "#include <stdlib.h>\n"
268 "#include <time.h>\n"
269 "#include <string.h>\n"
270 "#include <errno.h>\n"
271 "#include <limits.h>\n"
272 "#include <%s>\n",
273 filename,
274 type_file_string);
276 fprintf (templatefile,
277 "#include <%s>\n"
278 "#include <%s>\n"
279 "#include <der.h>\n"
280 "#include <asn1-template.h>\n",
281 header, privheader);
286 void
287 close_generate (void)
289 fprintf (headerfile, "#endif /* __%s_h__ */\n", headerbase);
291 if (headerfile)
292 fclose (headerfile);
293 if (privheaderfile)
294 fclose (privheaderfile);
295 if (templatefile)
296 fclose (templatefile);
297 if (logfile)
298 fprintf (logfile, "\n");
299 fclose (logfile);
302 void
303 gen_assign_defval(const char *var, struct value *val)
305 switch(val->type) {
306 case stringvalue:
307 fprintf(codefile, "if((%s = strdup(\"%s\")) == NULL)\nreturn ENOMEM;\n", var, val->u.stringvalue);
308 break;
309 case integervalue:
310 fprintf(codefile, "%s = %lld;\n",
311 var, (long long)val->u.integervalue);
312 break;
313 case booleanvalue:
314 if(val->u.booleanvalue)
315 fprintf(codefile, "%s = TRUE;\n", var);
316 else
317 fprintf(codefile, "%s = FALSE;\n", var);
318 break;
319 default:
320 abort();
324 void
325 gen_compare_defval(const char *var, struct value *val)
327 switch(val->type) {
328 case stringvalue:
329 fprintf(codefile, "if(strcmp(%s, \"%s\") != 0)\n", var, val->u.stringvalue);
330 break;
331 case integervalue:
332 fprintf(codefile, "if(%s != %lld)\n",
333 var, (long long)val->u.integervalue);
334 break;
335 case booleanvalue:
336 if(val->u.booleanvalue)
337 fprintf(codefile, "if(!%s)\n", var);
338 else
339 fprintf(codefile, "if(%s)\n", var);
340 break;
341 default:
342 abort();
346 void
347 generate_header_of_codefile(const char *name)
349 char *filename = NULL;
351 if (codefile != NULL)
352 abort();
354 if (asprintf (&filename, "%s_%s.x", STEM, name) < 0 || filename == NULL)
355 errx(1, "malloc");
356 codefile = fopen (filename, "w");
357 if (codefile == NULL)
358 err (1, "fopen %s", filename);
359 fprintf(logfile, "%s ", filename);
360 free(filename);
361 filename = NULL;
362 fprintf (codefile,
363 "/* Generated from %s */\n"
364 "/* Do not edit */\n\n"
365 "#define ASN1_LIB\n\n"
366 "#include <stdio.h>\n"
367 "#include <stdlib.h>\n"
368 "#include <time.h>\n"
369 "#include <string.h>\n"
370 "#include <errno.h>\n"
371 "#include <limits.h>\n"
372 "#include <%s>\n",
373 orig_filename,
374 type_file_string);
376 fprintf (codefile,
377 "#include \"%s\"\n"
378 "#include \"%s\"\n",
379 header, privheader);
380 fprintf (codefile,
381 "#include <asn1_err.h>\n"
382 "#include <der.h>\n"
383 "#include <asn1-template.h>\n\n");
385 if (parse_units_flag)
386 fprintf (codefile,
387 "#include <parse_units.h>\n\n");
391 void
392 close_codefile(void)
394 if (codefile == NULL)
395 abort();
397 fclose(codefile);
398 codefile = NULL;
402 void
403 generate_constant (const Symbol *s)
405 switch(s->value->type) {
406 case booleanvalue:
407 break;
408 case integervalue:
409 fprintf (headerfile, "enum { %s = %lld };\n\n",
410 s->gen_name,
411 (long long)s->value->u.integervalue);
412 break;
413 case nullvalue:
414 break;
415 case stringvalue:
416 break;
417 case objectidentifiervalue: {
418 struct objid *o, **list;
419 size_t i, len;
420 char *gen_upper;
422 if (!one_code_file)
423 generate_header_of_codefile(s->gen_name);
425 len = 0;
426 for (o = s->value->u.objectidentifiervalue; o != NULL; o = o->next)
427 len++;
428 if (len == 0) {
429 printf("s->gen_name: %s",s->gen_name);
430 fflush(stdout);
431 break;
433 list = emalloc(sizeof(*list) * len);
435 i = 0;
436 for (o = s->value->u.objectidentifiervalue; o != NULL; o = o->next)
437 list[i++] = o;
439 fprintf (headerfile, "/* OBJECT IDENTIFIER %s ::= { ", s->name);
440 for (i = len ; i > 0; i--) {
441 o = list[i - 1];
442 fprintf(headerfile, "%s(%d) ",
443 o->label ? o->label : "label-less", o->value);
446 fprintf (codefile, "static unsigned oid_%s_variable_num[%lu] = {",
447 s->gen_name, (unsigned long)len);
448 for (i = len ; i > 0; i--) {
449 fprintf(codefile, "%d%s ", list[i - 1]->value, i > 1 ? "," : "");
451 fprintf(codefile, "};\n");
453 fprintf (codefile, "const heim_oid asn1_oid_%s = "
454 "{ %lu, oid_%s_variable_num };\n\n",
455 s->gen_name, (unsigned long)len, s->gen_name);
457 free(list);
459 /* header file */
461 gen_upper = strdup(s->gen_name);
462 len = strlen(gen_upper);
463 for (i = 0; i < len; i++)
464 gen_upper[i] = toupper((int)s->gen_name[i]);
466 fprintf (headerfile, "} */\n");
467 fprintf (headerfile,
468 "extern ASN1EXP const heim_oid asn1_oid_%s;\n"
469 "#define ASN1_OID_%s (&asn1_oid_%s)\n\n",
470 s->gen_name,
471 gen_upper,
472 s->gen_name);
474 free(gen_upper);
476 if (!one_code_file)
477 close_codefile();
479 break;
481 default:
482 abort();
487 is_primitive_type(int type)
489 switch(type) {
490 case TInteger:
491 case TBoolean:
492 case TOctetString:
493 case TBitString:
494 case TEnumerated:
495 case TGeneralizedTime:
496 case TGeneralString:
497 case TTeletexString:
498 case TOID:
499 case TUTCTime:
500 case TUTF8String:
501 case TPrintableString:
502 case TIA5String:
503 case TBMPString:
504 case TUniversalString:
505 case TVisibleString:
506 case TNull:
507 return 1;
508 default:
509 return 0;
513 static void
514 space(int level)
516 while(level-- > 0)
517 fprintf(headerfile, " ");
520 static const char *
521 last_member_p(struct member *m)
523 struct member *n = ASN1_TAILQ_NEXT(m, members);
524 if (n == NULL)
525 return "";
526 if (n->ellipsis && ASN1_TAILQ_NEXT(n, members) == NULL)
527 return "";
528 return ",";
531 static struct member *
532 have_ellipsis(Type *t)
534 struct member *m;
535 ASN1_TAILQ_FOREACH(m, t->members, members) {
536 if (m->ellipsis)
537 return m;
539 return NULL;
542 static void
543 define_asn1 (int level, Type *t)
545 switch (t->type) {
546 case TType:
547 fprintf (headerfile, "%s", t->symbol->name);
548 break;
549 case TInteger:
550 if(t->members == NULL) {
551 fprintf (headerfile, "INTEGER");
552 if (t->range)
553 fprintf (headerfile, " (%lld..%lld)",
554 (long long)t->range->min,
555 (long long)t->range->max);
556 } else {
557 Member *m;
558 fprintf (headerfile, "INTEGER {\n");
559 ASN1_TAILQ_FOREACH(m, t->members, members) {
560 space (level + 1);
561 fprintf(headerfile, "%s(%d)%s\n", m->gen_name, m->val,
562 last_member_p(m));
564 space(level);
565 fprintf (headerfile, "}");
567 break;
568 case TBoolean:
569 fprintf (headerfile, "BOOLEAN");
570 break;
571 case TOctetString:
572 fprintf (headerfile, "OCTET STRING");
573 break;
574 case TEnumerated :
575 case TBitString: {
576 Member *m;
578 space(level);
579 if(t->type == TBitString)
580 fprintf (headerfile, "BIT STRING {\n");
581 else
582 fprintf (headerfile, "ENUMERATED {\n");
583 ASN1_TAILQ_FOREACH(m, t->members, members) {
584 space(level + 1);
585 fprintf (headerfile, "%s(%d)%s\n", m->name, m->val,
586 last_member_p(m));
588 space(level);
589 fprintf (headerfile, "}");
590 break;
592 case TChoice:
593 case TSet:
594 case TSequence: {
595 Member *m;
596 size_t max_width = 0;
598 if(t->type == TChoice)
599 fprintf(headerfile, "CHOICE {\n");
600 else if(t->type == TSet)
601 fprintf(headerfile, "SET {\n");
602 else
603 fprintf(headerfile, "SEQUENCE {\n");
604 ASN1_TAILQ_FOREACH(m, t->members, members) {
605 if(strlen(m->name) > max_width)
606 max_width = strlen(m->name);
608 max_width += 3;
609 if(max_width < 16) max_width = 16;
610 ASN1_TAILQ_FOREACH(m, t->members, members) {
611 size_t width = max_width;
612 space(level + 1);
613 if (m->ellipsis) {
614 fprintf (headerfile, "...");
615 } else {
616 width -= fprintf(headerfile, "%s", m->name);
617 fprintf(headerfile, "%*s", (int)width, "");
618 define_asn1(level + 1, m->type);
619 if(m->optional)
620 fprintf(headerfile, " OPTIONAL");
622 if(last_member_p(m))
623 fprintf (headerfile, ",");
624 fprintf (headerfile, "\n");
626 space(level);
627 fprintf (headerfile, "}");
628 break;
630 case TSequenceOf:
631 fprintf (headerfile, "SEQUENCE OF ");
632 define_asn1 (0, t->subtype);
633 break;
634 case TSetOf:
635 fprintf (headerfile, "SET OF ");
636 define_asn1 (0, t->subtype);
637 break;
638 case TGeneralizedTime:
639 fprintf (headerfile, "GeneralizedTime");
640 break;
641 case TGeneralString:
642 fprintf (headerfile, "GeneralString");
643 break;
644 case TTeletexString:
645 fprintf (headerfile, "TeletexString");
646 break;
647 case TTag: {
648 const char *classnames[] = { "UNIVERSAL ", "APPLICATION ",
649 "" /* CONTEXT */, "PRIVATE " };
650 if(t->tag.tagclass != ASN1_C_UNIV)
651 fprintf (headerfile, "[%s%d] ",
652 classnames[t->tag.tagclass],
653 t->tag.tagvalue);
654 if(t->tag.tagenv == TE_IMPLICIT)
655 fprintf (headerfile, "IMPLICIT ");
656 define_asn1 (level, t->subtype);
657 break;
659 case TUTCTime:
660 fprintf (headerfile, "UTCTime");
661 break;
662 case TUTF8String:
663 space(level);
664 fprintf (headerfile, "UTF8String");
665 break;
666 case TPrintableString:
667 space(level);
668 fprintf (headerfile, "PrintableString");
669 break;
670 case TIA5String:
671 space(level);
672 fprintf (headerfile, "IA5String");
673 break;
674 case TBMPString:
675 space(level);
676 fprintf (headerfile, "BMPString");
677 break;
678 case TUniversalString:
679 space(level);
680 fprintf (headerfile, "UniversalString");
681 break;
682 case TVisibleString:
683 space(level);
684 fprintf (headerfile, "VisibleString");
685 break;
686 case TOID :
687 space(level);
688 fprintf(headerfile, "OBJECT IDENTIFIER");
689 break;
690 case TNull:
691 space(level);
692 fprintf (headerfile, "NULL");
693 break;
694 default:
695 abort ();
699 static void
700 getnewbasename(char **newbasename, int typedefp, const char *basename, const char *name)
702 if (typedefp)
703 *newbasename = strdup(name);
704 else {
705 if (name[0] == '*')
706 name++;
707 if (asprintf(newbasename, "%s_%s", basename, name) < 0)
708 errx(1, "malloc");
710 if (*newbasename == NULL)
711 err(1, "malloc");
714 static void
715 define_type (int level, const char *name, const char *basename, Type *t, int typedefp, int preservep)
717 char *newbasename = NULL;
719 switch (t->type) {
720 case TType:
721 space(level);
722 fprintf (headerfile, "%s %s;\n", t->symbol->gen_name, name);
723 break;
724 case TInteger:
725 space(level);
726 if(t->members) {
727 Member *m;
728 fprintf (headerfile, "enum %s {\n", typedefp ? name : "");
729 ASN1_TAILQ_FOREACH(m, t->members, members) {
730 space (level + 1);
731 fprintf(headerfile, "%s = %d%s\n", m->gen_name, m->val,
732 last_member_p(m));
734 fprintf (headerfile, "} %s;\n", name);
735 } else if (t->range == NULL) {
736 fprintf (headerfile, "heim_integer %s;\n", name);
737 } else if (t->range->min < INT_MIN && t->range->max <= INT64_MAX) {
738 fprintf (headerfile, "int64_t %s;\n", name);
739 } else if (t->range->min >= 0 && t->range->max > UINT_MAX) {
740 fprintf (headerfile, "uint64_t %s;\n", name);
741 } else if (t->range->min >= INT_MIN && t->range->max <= INT_MAX) {
742 fprintf (headerfile, "int %s;\n", name);
743 } else if (t->range->min >= 0 && t->range->max <= UINT_MAX) {
744 fprintf (headerfile, "unsigned int %s;\n", name);
745 } else
746 errx(1, "%s: unsupported range %lld -> %lld",
747 name, (long long)t->range->min, (long long)t->range->max);
748 break;
749 case TBoolean:
750 space(level);
751 fprintf (headerfile, "int %s;\n", name);
752 break;
753 case TOctetString:
754 space(level);
755 fprintf (headerfile, "heim_octet_string %s;\n", name);
756 break;
757 case TBitString: {
758 Member *m;
759 Type i;
760 struct range range = { 0, UINT_MAX };
762 i.type = TInteger;
763 i.range = &range;
764 i.members = NULL;
765 i.constraint = NULL;
767 space(level);
768 if(ASN1_TAILQ_EMPTY(t->members))
769 fprintf (headerfile, "heim_bit_string %s;\n", name);
770 else {
771 int pos = 0;
772 getnewbasename(&newbasename, typedefp, basename, name);
774 fprintf (headerfile, "struct %s {\n", newbasename);
775 ASN1_TAILQ_FOREACH(m, t->members, members) {
776 char *n = NULL;
778 /* pad unused */
779 while (pos < m->val) {
780 if (asprintf (&n, "_unused%d:1", pos) < 0 || n == NULL)
781 errx(1, "malloc");
782 define_type (level + 1, n, newbasename, &i, FALSE, FALSE);
783 free(n);
784 pos++;
787 n = NULL;
788 if (asprintf (&n, "%s:1", m->gen_name) < 0 || n == NULL)
789 errx(1, "malloc");
790 define_type (level + 1, n, newbasename, &i, FALSE, FALSE);
791 free (n);
792 n = NULL;
793 pos++;
795 /* pad to 32 elements */
796 while (pos < 32) {
797 char *n = NULL;
798 if (asprintf (&n, "_unused%d:1", pos) < 0 || n == NULL)
799 errx(1, "malloc");
800 define_type (level + 1, n, newbasename, &i, FALSE, FALSE);
801 free(n);
802 pos++;
805 space(level);
806 fprintf (headerfile, "} %s;\n\n", name);
808 break;
810 case TEnumerated: {
811 Member *m;
813 space(level);
814 fprintf (headerfile, "enum %s {\n", typedefp ? name : "");
815 ASN1_TAILQ_FOREACH(m, t->members, members) {
816 space(level + 1);
817 if (m->ellipsis)
818 fprintf (headerfile, "/* ... */\n");
819 else
820 fprintf (headerfile, "%s = %d%s\n", m->gen_name, m->val,
821 last_member_p(m));
823 space(level);
824 fprintf (headerfile, "} %s;\n\n", name);
825 break;
827 case TSet:
828 case TSequence: {
829 Member *m;
831 getnewbasename(&newbasename, typedefp, basename, name);
833 space(level);
834 fprintf (headerfile, "struct %s {\n", newbasename);
835 if (t->type == TSequence && preservep) {
836 space(level + 1);
837 fprintf(headerfile, "heim_octet_string _save;\n");
839 ASN1_TAILQ_FOREACH(m, t->members, members) {
840 if (m->ellipsis) {
842 } else if (m->optional) {
843 char *n = NULL;
845 if (asprintf (&n, "*%s", m->gen_name) < 0 || n == NULL)
846 errx(1, "malloc");
847 define_type (level + 1, n, newbasename, m->type, FALSE, FALSE);
848 free (n);
849 } else
850 define_type (level + 1, m->gen_name, newbasename, m->type, FALSE, FALSE);
852 space(level);
853 fprintf (headerfile, "} %s;\n", name);
854 break;
856 case TSetOf:
857 case TSequenceOf: {
858 Type i;
859 struct range range = { 0, UINT_MAX };
861 getnewbasename(&newbasename, typedefp, basename, name);
863 i.type = TInteger;
864 i.range = &range;
865 i.members = NULL;
866 i.constraint = NULL;
868 space(level);
869 fprintf (headerfile, "struct %s {\n", newbasename);
870 define_type (level + 1, "len", newbasename, &i, FALSE, FALSE);
871 define_type (level + 1, "*val", newbasename, t->subtype, FALSE, FALSE);
872 space(level);
873 fprintf (headerfile, "} %s;\n", name);
874 break;
876 case TGeneralizedTime:
877 space(level);
878 fprintf (headerfile, "time_t %s;\n", name);
879 break;
880 case TGeneralString:
881 space(level);
882 fprintf (headerfile, "heim_general_string %s;\n", name);
883 break;
884 case TTeletexString:
885 space(level);
886 fprintf (headerfile, "heim_general_string %s;\n", name);
887 break;
888 case TTag:
889 define_type (level, name, basename, t->subtype, typedefp, preservep);
890 break;
891 case TChoice: {
892 int first = 1;
893 Member *m;
895 getnewbasename(&newbasename, typedefp, basename, name);
897 space(level);
898 fprintf (headerfile, "struct %s {\n", newbasename);
899 if (preservep) {
900 space(level + 1);
901 fprintf(headerfile, "heim_octet_string _save;\n");
903 space(level + 1);
904 fprintf (headerfile, "enum %s_enum {\n", newbasename);
905 m = have_ellipsis(t);
906 if (m) {
907 space(level + 2);
908 fprintf (headerfile, "%s = 0,\n", m->label);
909 first = 0;
911 ASN1_TAILQ_FOREACH(m, t->members, members) {
912 space(level + 2);
913 if (m->ellipsis)
914 fprintf (headerfile, "/* ... */\n");
915 else
916 fprintf (headerfile, "%s%s%s\n", m->label,
917 first ? " = 1" : "",
918 last_member_p(m));
919 first = 0;
921 space(level + 1);
922 fprintf (headerfile, "} element;\n");
923 space(level + 1);
924 fprintf (headerfile, "union {\n");
925 ASN1_TAILQ_FOREACH(m, t->members, members) {
926 if (m->ellipsis) {
927 space(level + 2);
928 fprintf(headerfile, "heim_octet_string asn1_ellipsis;\n");
929 } else if (m->optional) {
930 char *n = NULL;
932 if (asprintf (&n, "*%s", m->gen_name) < 0 || n == NULL)
933 errx(1, "malloc");
934 define_type (level + 2, n, newbasename, m->type, FALSE, FALSE);
935 free (n);
936 } else
937 define_type (level + 2, m->gen_name, newbasename, m->type, FALSE, FALSE);
939 space(level + 1);
940 fprintf (headerfile, "} u;\n");
941 space(level);
942 fprintf (headerfile, "} %s;\n", name);
943 break;
945 case TUTCTime:
946 space(level);
947 fprintf (headerfile, "time_t %s;\n", name);
948 break;
949 case TUTF8String:
950 space(level);
951 fprintf (headerfile, "heim_utf8_string %s;\n", name);
952 break;
953 case TPrintableString:
954 space(level);
955 fprintf (headerfile, "heim_printable_string %s;\n", name);
956 break;
957 case TIA5String:
958 space(level);
959 fprintf (headerfile, "heim_ia5_string %s;\n", name);
960 break;
961 case TBMPString:
962 space(level);
963 fprintf (headerfile, "heim_bmp_string %s;\n", name);
964 break;
965 case TUniversalString:
966 space(level);
967 fprintf (headerfile, "heim_universal_string %s;\n", name);
968 break;
969 case TVisibleString:
970 space(level);
971 fprintf (headerfile, "heim_visible_string %s;\n", name);
972 break;
973 case TOID :
974 space(level);
975 fprintf (headerfile, "heim_oid %s;\n", name);
976 break;
977 case TNull:
978 space(level);
979 fprintf (headerfile, "int %s;\n", name);
980 break;
981 default:
982 abort ();
984 if (newbasename)
985 free(newbasename);
988 static void
989 generate_type_header (const Symbol *s)
991 int preservep = preserve_type(s->name) ? TRUE : FALSE;
993 fprintf (headerfile, "/*\n");
994 fprintf (headerfile, "%s ::= ", s->name);
995 define_asn1 (0, s->type);
996 fprintf (headerfile, "\n*/\n\n");
998 fprintf (headerfile, "typedef ");
999 define_type (0, s->gen_name, s->gen_name, s->type, TRUE, preservep);
1001 fprintf (headerfile, "\n");
1004 void
1005 generate_type (const Symbol *s)
1007 FILE *h;
1008 const char * exp;
1010 if (!one_code_file)
1011 generate_header_of_codefile(s->gen_name);
1013 generate_type_header (s);
1015 if (template_flag)
1016 generate_template(s);
1018 if (template_flag == 0 || is_template_compat(s) == 0) {
1019 generate_type_encode (s);
1020 generate_type_decode (s);
1021 generate_type_free (s);
1022 generate_type_length (s);
1023 generate_type_copy (s);
1025 generate_type_seq (s);
1026 generate_glue (s->type, s->gen_name);
1028 /* generate prototypes */
1030 if (is_export(s->name)) {
1031 h = headerfile;
1032 exp = "ASN1EXP ";
1033 } else {
1034 h = privheaderfile;
1035 exp = "";
1038 fprintf (h,
1039 "%sint ASN1CALL "
1040 "decode_%s(const unsigned char *, size_t, %s *, size_t *);\n",
1041 exp,
1042 s->gen_name, s->gen_name);
1043 fprintf (h,
1044 "%sint ASN1CALL "
1045 "encode_%s(unsigned char *, size_t, const %s *, size_t *);\n",
1046 exp,
1047 s->gen_name, s->gen_name);
1048 fprintf (h,
1049 "%ssize_t ASN1CALL length_%s(const %s *);\n",
1050 exp,
1051 s->gen_name, s->gen_name);
1052 fprintf (h,
1053 "%sint ASN1CALL copy_%s (const %s *, %s *);\n",
1054 exp,
1055 s->gen_name, s->gen_name, s->gen_name);
1056 fprintf (h,
1057 "%svoid ASN1CALL free_%s (%s *);\n",
1058 exp,
1059 s->gen_name, s->gen_name);
1061 fprintf(h, "\n\n");
1063 if (!one_code_file) {
1064 fprintf(codefile, "\n\n");
1065 close_codefile();