2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
36 __RCSID("$Heimdal: gen.c 22429 2008-01-13 10:25:50Z lha $"
37 "$NetBSD: gen.c,v 1.10 2008/08/03 07:16:58 veego Exp $");
39 FILE *headerfile
, *codefile
, *logfile
;
43 static const char *orig_filename
;
45 static const char *headerbase
= STEM
;
56 static struct import
*imports
= NULL
;
59 add_import (const char *module
)
61 struct import
*tmp
= emalloc (sizeof(*tmp
));
67 fprintf (headerfile
, "#include <%s_asn1.h>\n", module
);
77 init_generate (const char *filename
, const char *base
)
81 orig_filename
= filename
;
83 headerbase
= strdup(base
);
84 if (headerbase
== NULL
)
87 asprintf(&header
, "%s.hx", headerbase
);
90 headerfile
= fopen (header
, "w");
91 if (headerfile
== NULL
)
92 err (1, "open %s", header
);
94 "/* Generated from %s */\n"
95 "/* Do not edit */\n\n",
99 "#define __%s_h__\n\n", headerbase
, headerbase
);
101 "#include <stddef.h>\n"
102 "#include <time.h>\n\n");
104 "#ifndef __asn1_common_definitions__\n"
105 "#define __asn1_common_definitions__\n\n");
107 "typedef struct heim_integer {\n"
111 "} heim_integer;\n\n");
113 "typedef struct heim_octet_string {\n"
116 "} heim_octet_string;\n\n");
118 "typedef char *heim_general_string;\n\n"
121 "typedef char *heim_utf8_string;\n\n"
124 "typedef char *heim_printable_string;\n\n"
127 "typedef char *heim_ia5_string;\n\n"
130 "typedef struct heim_bmp_string {\n"
133 "} heim_bmp_string;\n\n");
135 "typedef struct heim_universal_string {\n"
138 "} heim_universal_string;\n\n");
140 "typedef char *heim_visible_string;\n\n"
143 "typedef struct heim_oid {\n"
145 " unsigned *components;\n"
148 "typedef struct heim_bit_string {\n"
151 "} heim_bit_string;\n\n");
153 "typedef struct heim_octet_string heim_any;\n"
154 "typedef struct heim_octet_string heim_any_set;\n\n");
155 fputs("#define ASN1_MALLOC_ENCODE(T, B, BL, S, L, R) \\\n"
157 " (BL) = length_##T((S)); \\\n"
158 " (B) = malloc((BL)); \\\n"
159 " if((B) == NULL) { \\\n"
160 " (R) = ENOMEM; \\\n"
162 " (R) = encode_##T(((unsigned char*)(B)) + (BL) - 1, (BL), \\\n"
164 " if((R) != 0) { \\\n"
171 fprintf (headerfile
, "struct units;\n\n");
172 fprintf (headerfile
, "#endif\n\n");
173 asprintf(&fn
, "%s_files", base
);
176 logfile
= fopen(fn
, "w");
178 err (1, "open %s", fn
);
182 close_generate (void)
184 fprintf (headerfile
, "#endif /* __%s_h__ */\n", headerbase
);
187 fprintf (logfile
, "\n");
192 gen_assign_defval(const char *var
, struct value
*val
)
196 fprintf(codefile
, "if((%s = strdup(\"%s\")) == NULL)\nreturn ENOMEM;\n", var
, val
->u
.stringvalue
);
199 fprintf(codefile
, "%s = %d;\n", var
, val
->u
.integervalue
);
202 if(val
->u
.booleanvalue
)
203 fprintf(codefile
, "%s = TRUE;\n", var
);
205 fprintf(codefile
, "%s = FALSE;\n", var
);
213 gen_compare_defval(const char *var
, struct value
*val
)
217 fprintf(codefile
, "if(strcmp(%s, \"%s\") != 0)\n", var
, val
->u
.stringvalue
);
220 fprintf(codefile
, "if(%s != %d)\n", var
, val
->u
.integervalue
);
223 if(val
->u
.booleanvalue
)
224 fprintf(codefile
, "if(!%s)\n", var
);
226 fprintf(codefile
, "if(%s)\n", var
);
234 generate_header_of_codefile(const char *name
)
238 if (codefile
!= NULL
)
241 asprintf (&filename
, "%s_%s.x", STEM
, name
);
242 if (filename
== NULL
)
244 codefile
= fopen (filename
, "w");
245 if (codefile
== NULL
)
246 err (1, "fopen %s", filename
);
247 fprintf(logfile
, "%s ", filename
);
250 "/* Generated from %s */\n"
251 "/* Do not edit */\n\n"
252 "#include <stdio.h>\n"
253 "#include <stdlib.h>\n"
254 "#include <time.h>\n"
255 "#include <string.h>\n"
256 "#include <errno.h>\n"
257 "#include <limits.h>\n"
258 "#include <krb5-types.h>\n",
265 "#include <asn1_err.h>\n"
267 "#include <parse_units.h>\n\n");
274 if (codefile
== NULL
)
283 generate_constant (const Symbol
*s
)
285 switch(s
->value
->type
) {
289 fprintf (headerfile
, "enum { %s = %d };\n\n",
290 s
->gen_name
, s
->value
->u
.integervalue
);
296 case objectidentifiervalue
: {
297 struct objid
*o
, **list
;
300 generate_header_of_codefile(s
->gen_name
);
303 for (o
= s
->value
->u
.objectidentifiervalue
; o
!= NULL
; o
= o
->next
)
305 list
= emalloc(sizeof(*list
) * len
);
308 for (o
= s
->value
->u
.objectidentifiervalue
; o
!= NULL
; o
= o
->next
)
311 fprintf (headerfile
, "/* OBJECT IDENTIFIER %s ::= { ", s
->name
);
312 for (i
= len
- 1 ; i
>= 0; i
--) {
314 fprintf(headerfile
, "%s(%d) ",
315 o
->label
? o
->label
: "label-less", o
->value
);
318 fprintf (headerfile
, "} */\n");
319 fprintf (headerfile
, "const heim_oid *oid_%s(void);\n\n",
322 fprintf (codefile
, "static unsigned oid_%s_variable_num[%d] = {",
324 for (i
= len
- 1 ; i
>= 0; i
--) {
325 fprintf(codefile
, "%d%s ", list
[i
]->value
, i
> 0 ? "," : "");
327 fprintf(codefile
, "};\n");
329 fprintf (codefile
, "static const heim_oid oid_%s_variable = "
330 "{ %d, oid_%s_variable_num };\n\n",
331 s
->gen_name
, len
, s
->gen_name
);
333 fprintf (codefile
, "const heim_oid *oid_%s(void)\n"
335 "return &oid_%s_variable;\n"
337 s
->gen_name
, s
->gen_name
);
353 fprintf(headerfile
, " ");
357 last_member_p(struct member
*m
)
359 struct member
*n
= ASN1_TAILQ_NEXT(m
, members
);
362 if (n
->ellipsis
&& ASN1_TAILQ_NEXT(n
, members
) == NULL
)
367 static struct member
*
368 have_ellipsis(Type
*t
)
371 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
379 define_asn1 (int level
, Type
*t
)
383 fprintf (headerfile
, "%s", t
->symbol
->name
);
386 if(t
->members
== NULL
) {
387 fprintf (headerfile
, "INTEGER");
389 fprintf (headerfile
, " (%d..%d)",
390 t
->range
->min
, t
->range
->max
);
393 fprintf (headerfile
, "INTEGER {\n");
394 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
396 fprintf(headerfile
, "%s(%d)%s\n", m
->gen_name
, m
->val
,
400 fprintf (headerfile
, "}");
404 fprintf (headerfile
, "BOOLEAN");
407 fprintf (headerfile
, "OCTET STRING");
414 if(t
->type
== TBitString
)
415 fprintf (headerfile
, "BIT STRING {\n");
417 fprintf (headerfile
, "ENUMERATED {\n");
418 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
420 fprintf (headerfile
, "%s(%d)%s\n", m
->name
, m
->val
,
424 fprintf (headerfile
, "}");
433 if(t
->type
== TChoice
)
434 fprintf(headerfile
, "CHOICE {\n");
435 else if(t
->type
== TSet
)
436 fprintf(headerfile
, "SET {\n");
438 fprintf(headerfile
, "SEQUENCE {\n");
439 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
440 if(strlen(m
->name
) > max_width
)
441 max_width
= strlen(m
->name
);
444 if(max_width
< 16) max_width
= 16;
445 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
446 int width
= max_width
;
449 fprintf (headerfile
, "...");
451 width
-= fprintf(headerfile
, "%s", m
->name
);
452 fprintf(headerfile
, "%*s", width
, "");
453 define_asn1(level
+ 1, m
->type
);
455 fprintf(headerfile
, " OPTIONAL");
458 fprintf (headerfile
, ",");
459 fprintf (headerfile
, "\n");
462 fprintf (headerfile
, "}");
466 fprintf (headerfile
, "SEQUENCE OF ");
467 define_asn1 (0, t
->subtype
);
470 fprintf (headerfile
, "SET OF ");
471 define_asn1 (0, t
->subtype
);
473 case TGeneralizedTime
:
474 fprintf (headerfile
, "GeneralizedTime");
477 fprintf (headerfile
, "GeneralString");
480 const char *classnames
[] = { "UNIVERSAL ", "APPLICATION ",
481 "" /* CONTEXT */, "PRIVATE " };
482 if(t
->tag
.tagclass
!= ASN1_C_UNIV
)
483 fprintf (headerfile
, "[%s%d] ",
484 classnames
[t
->tag
.tagclass
],
486 if(t
->tag
.tagenv
== TE_IMPLICIT
)
487 fprintf (headerfile
, "IMPLICIT ");
488 define_asn1 (level
, t
->subtype
);
492 fprintf (headerfile
, "UTCTime");
496 fprintf (headerfile
, "UTF8String");
498 case TPrintableString
:
500 fprintf (headerfile
, "PrintableString");
504 fprintf (headerfile
, "IA5String");
508 fprintf (headerfile
, "BMPString");
510 case TUniversalString
:
512 fprintf (headerfile
, "UniversalString");
516 fprintf (headerfile
, "VisibleString");
520 fprintf(headerfile
, "OBJECT IDENTIFIER");
524 fprintf (headerfile
, "NULL");
532 define_type (int level
, const char *name
, Type
*t
, int typedefp
, int preservep
)
537 fprintf (headerfile
, "%s %s;\n", t
->symbol
->gen_name
, name
);
543 fprintf (headerfile
, "enum %s {\n", typedefp
? name
: "");
544 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
546 fprintf(headerfile
, "%s = %d%s\n", m
->gen_name
, m
->val
,
549 fprintf (headerfile
, "} %s;\n", name
);
550 } else if (t
->range
== NULL
) {
551 fprintf (headerfile
, "heim_integer %s;\n", name
);
552 } else if (t
->range
->min
== INT_MIN
&& t
->range
->max
== INT_MAX
) {
553 fprintf (headerfile
, "int %s;\n", name
);
554 } else if (t
->range
->min
== 0 && t
->range
->max
== UINT_MAX
) {
555 fprintf (headerfile
, "unsigned int %s;\n", name
);
556 } else if (t
->range
->min
== 0 && t
->range
->max
== INT_MAX
) {
557 fprintf (headerfile
, "unsigned int %s;\n", name
);
559 errx(1, "%s: unsupported range %d -> %d",
560 name
, t
->range
->min
, t
->range
->max
);
564 fprintf (headerfile
, "int %s;\n", name
);
568 fprintf (headerfile
, "heim_octet_string %s;\n", name
);
573 struct range range
= { 0, INT_MAX
};
581 if(ASN1_TAILQ_EMPTY(t
->members
))
582 fprintf (headerfile
, "heim_bit_string %s;\n", name
);
584 fprintf (headerfile
, "struct %s {\n", typedefp
? name
: "");
585 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
588 asprintf (&n
, "%s:1", m
->gen_name
);
591 define_type (level
+ 1, n
, &i
, FALSE
, FALSE
);
595 fprintf (headerfile
, "} %s;\n\n", name
);
603 fprintf (headerfile
, "enum %s {\n", typedefp
? name
: "");
604 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
607 fprintf (headerfile
, "/* ... */\n");
609 fprintf (headerfile
, "%s = %d%s\n", m
->gen_name
, m
->val
,
613 fprintf (headerfile
, "} %s;\n\n", name
);
621 fprintf (headerfile
, "struct %s {\n", typedefp
? name
: "");
622 if (t
->type
== TSequence
&& preservep
) {
624 fprintf(headerfile
, "heim_octet_string _save;\n");
626 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
629 } else if (m
->optional
) {
632 asprintf (&n
, "*%s", m
->gen_name
);
635 define_type (level
+ 1, n
, m
->type
, FALSE
, FALSE
);
638 define_type (level
+ 1, m
->gen_name
, m
->type
, FALSE
, FALSE
);
641 fprintf (headerfile
, "} %s;\n", name
);
647 struct range range
= { 0, INT_MAX
};
655 fprintf (headerfile
, "struct %s {\n", typedefp
? name
: "");
656 define_type (level
+ 1, "len", &i
, FALSE
, FALSE
);
657 define_type (level
+ 1, "*val", t
->subtype
, FALSE
, FALSE
);
659 fprintf (headerfile
, "} %s;\n", name
);
662 case TGeneralizedTime
:
664 fprintf (headerfile
, "time_t %s;\n", name
);
668 fprintf (headerfile
, "heim_general_string %s;\n", name
);
671 define_type (level
, name
, t
->subtype
, typedefp
, preservep
);
678 fprintf (headerfile
, "struct %s {\n", typedefp
? name
: "");
681 fprintf(headerfile
, "heim_octet_string _save;\n");
684 fprintf (headerfile
, "enum {\n");
685 m
= have_ellipsis(t
);
688 fprintf (headerfile
, "%s = 0,\n", m
->label
);
691 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
694 fprintf (headerfile
, "/* ... */\n");
696 fprintf (headerfile
, "%s%s%s\n", m
->label
,
702 fprintf (headerfile
, "} element;\n");
704 fprintf (headerfile
, "union {\n");
705 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
708 fprintf(headerfile
, "heim_octet_string asn1_ellipsis;\n");
709 } else if (m
->optional
) {
712 asprintf (&n
, "*%s", m
->gen_name
);
715 define_type (level
+ 2, n
, m
->type
, FALSE
, FALSE
);
718 define_type (level
+ 2, m
->gen_name
, m
->type
, FALSE
, FALSE
);
721 fprintf (headerfile
, "} u;\n");
723 fprintf (headerfile
, "} %s;\n", name
);
728 fprintf (headerfile
, "time_t %s;\n", name
);
732 fprintf (headerfile
, "heim_utf8_string %s;\n", name
);
734 case TPrintableString
:
736 fprintf (headerfile
, "heim_printable_string %s;\n", name
);
740 fprintf (headerfile
, "heim_ia5_string %s;\n", name
);
744 fprintf (headerfile
, "heim_bmp_string %s;\n", name
);
746 case TUniversalString
:
748 fprintf (headerfile
, "heim_universal_string %s;\n", name
);
752 fprintf (headerfile
, "heim_visible_string %s;\n", name
);
756 fprintf (headerfile
, "heim_oid %s;\n", name
);
760 fprintf (headerfile
, "int %s;\n", name
);
768 generate_type_header (const Symbol
*s
)
770 int preservep
= preserve_type(s
->name
) ? TRUE
: FALSE
;
772 fprintf (headerfile
, "/*\n");
773 fprintf (headerfile
, "%s ::= ", s
->name
);
774 define_asn1 (0, s
->type
);
775 fprintf (headerfile
, "\n*/\n\n");
777 fprintf (headerfile
, "typedef ");
778 define_type (0, s
->gen_name
, s
->type
, TRUE
, preservep
);
780 fprintf (headerfile
, "\n");
785 generate_type (const Symbol
*s
)
787 generate_header_of_codefile(s
->gen_name
);
789 generate_type_header (s
);
790 generate_type_encode (s
);
791 generate_type_decode (s
);
792 generate_type_free (s
);
793 generate_type_length (s
);
794 generate_type_copy (s
);
795 generate_type_seq (s
);
796 generate_glue (s
->type
, s
->gen_name
);
797 fprintf(headerfile
, "\n\n");