2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
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
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
40 FILE *privheaderfile
, *headerfile
, *codefile
, *logfile
, *templatefile
;
44 static const char *orig_filename
;
45 static char *privheader
, *header
, *template;
46 static const char *headerbase
= STEM
;
57 static struct import
*imports
= NULL
;
60 add_import (const char *module
)
62 struct import
*tmp
= emalloc (sizeof(*tmp
));
68 fprintf (headerfile
, "#include <%s_asn1.h>\n", module
);
72 * List of all exported symbols
81 static struct sexport
*exports
= NULL
;
84 add_export (const char *name
)
86 struct sexport
*tmp
= emalloc (sizeof(*tmp
));
94 is_export(const char *name
)
98 if (exports
== NULL
) /* no export list, all exported */
101 for (tmp
= exports
; tmp
!= NULL
; tmp
= tmp
->next
) {
102 if (strcmp(tmp
->name
, name
) == 0) {
113 return orig_filename
;
117 init_generate (const char *filename
, const char *base
)
121 orig_filename
= filename
;
123 headerbase
= strdup(base
);
124 if (headerbase
== NULL
)
128 /* public header file */
129 if (asprintf(&header
, "%s.h", headerbase
) < 0 || header
== NULL
)
131 if (asprintf(&fn
, "%s.hx", headerbase
) < 0 || fn
== NULL
)
133 headerfile
= fopen (fn
, "w");
134 if (headerfile
== NULL
)
135 err (1, "open %s", fn
);
139 /* private header file */
140 if (asprintf(&privheader
, "%s-priv.h", headerbase
) < 0 || privheader
== NULL
)
142 if (asprintf(&fn
, "%s-priv.hx", headerbase
) < 0 || fn
== NULL
)
144 privheaderfile
= fopen (fn
, "w");
145 if (privheaderfile
== NULL
)
146 err (1, "open %s", fn
);
151 if (asprintf(&template, "%s-template.c", headerbase
) < 0 || template == NULL
)
154 "/* Generated from %s */\n"
155 "/* Do not edit */\n\n",
159 "#define __%s_h__\n\n", headerbase
, headerbase
);
161 "#include <stddef.h>\n"
162 "#include <time.h>\n\n");
164 "#ifndef __asn1_common_definitions__\n"
165 "#define __asn1_common_definitions__\n\n");
167 "typedef struct heim_integer {\n"
171 "} heim_integer;\n\n");
173 "typedef struct heim_octet_string {\n"
176 "} heim_octet_string;\n\n");
178 "typedef char *heim_general_string;\n\n"
181 "typedef char *heim_utf8_string;\n\n"
184 "typedef struct heim_octet_string heim_printable_string;\n\n"
187 "typedef struct heim_octet_string heim_ia5_string;\n\n"
190 "typedef struct heim_bmp_string {\n"
193 "} heim_bmp_string;\n\n");
195 "typedef struct heim_universal_string {\n"
198 "} heim_universal_string;\n\n");
200 "typedef char *heim_visible_string;\n\n"
203 "typedef struct heim_oid {\n"
205 " unsigned *components;\n"
208 "typedef struct heim_bit_string {\n"
211 "} heim_bit_string;\n\n");
213 "typedef struct heim_octet_string heim_any;\n"
214 "typedef struct heim_octet_string heim_any_set;\n\n");
215 fputs("#define ASN1_MALLOC_ENCODE(T, B, BL, S, L, R) \\\n"
217 " (BL) = length_##T((S)); \\\n"
218 " (B) = malloc((BL)); \\\n"
219 " if((B) == NULL) { \\\n"
220 " (R) = ENOMEM; \\\n"
222 " (R) = encode_##T(((unsigned char*)(B)) + (BL) - 1, (BL), \\\n"
224 " if((R) != 0) { \\\n"
231 fputs("#ifdef _WIN32\n"
233 "#define ASN1EXP __declspec(dllimport)\n"
237 "#define ASN1CALL __stdcall\n"
243 fprintf (headerfile
, "struct units;\n\n");
244 fprintf (headerfile
, "#endif\n\n");
245 if (asprintf(&fn
, "%s_files", base
) < 0 || fn
== NULL
)
247 logfile
= fopen(fn
, "w");
249 err (1, "open %s", fn
);
251 /* if one code file, write into the one codefile */
255 templatefile
= fopen (template, "w");
256 if (templatefile
== NULL
)
257 err (1, "open %s", template);
259 fprintf (templatefile
,
260 "/* Generated from %s */\n"
261 "/* Do not edit */\n\n"
262 "#include <stdio.h>\n"
263 "#include <stdlib.h>\n"
264 "#include <time.h>\n"
265 "#include <string.h>\n"
266 "#include <errno.h>\n"
267 "#include <limits.h>\n"
268 "#include <krb5-types.h>\n",
271 fprintf (templatefile
,
275 "#include <der-private.h>\n"
276 "#include <asn1-template.h>\n",
283 close_generate (void)
285 fprintf (headerfile
, "#endif /* __%s_h__ */\n", headerbase
);
290 fclose (privheaderfile
);
292 fclose (templatefile
);
294 fprintf (logfile
, "\n");
299 gen_assign_defval(const char *var
, struct value
*val
)
303 fprintf(codefile
, "if((%s = strdup(\"%s\")) == NULL)\nreturn ENOMEM;\n", var
, val
->u
.stringvalue
);
306 fprintf(codefile
, "%s = %d;\n", var
, val
->u
.integervalue
);
309 if(val
->u
.booleanvalue
)
310 fprintf(codefile
, "%s = TRUE;\n", var
);
312 fprintf(codefile
, "%s = FALSE;\n", var
);
320 gen_compare_defval(const char *var
, struct value
*val
)
324 fprintf(codefile
, "if(strcmp(%s, \"%s\") != 0)\n", var
, val
->u
.stringvalue
);
327 fprintf(codefile
, "if(%s != %d)\n", var
, val
->u
.integervalue
);
330 if(val
->u
.booleanvalue
)
331 fprintf(codefile
, "if(!%s)\n", var
);
333 fprintf(codefile
, "if(%s)\n", var
);
341 generate_header_of_codefile(const char *name
)
343 char *filename
= NULL
;
345 if (codefile
!= NULL
)
348 if (asprintf (&filename
, "%s_%s.x", STEM
, name
) < 0 || filename
== NULL
)
350 codefile
= fopen (filename
, "w");
351 if (codefile
== NULL
)
352 err (1, "fopen %s", filename
);
353 fprintf(logfile
, "%s ", filename
);
357 "/* Generated from %s */\n"
358 "/* Do not edit */\n\n"
359 "#define ASN1_LIB\n\n"
360 "#include <stdio.h>\n"
361 "#include <stdlib.h>\n"
362 "#include <time.h>\n"
363 "#include <string.h>\n"
364 "#include <errno.h>\n"
365 "#include <limits.h>\n"
366 "#include <krb5-types.h>\n",
374 "#include <asn1_err.h>\n"
376 "#include <der-private.h>\n"
377 "#include <asn1-template.h>\n"
378 "#include <parse_units.h>\n\n");
385 if (codefile
== NULL
)
394 generate_constant (const Symbol
*s
)
396 switch(s
->value
->type
) {
400 fprintf (headerfile
, "enum { %s = %d };\n\n",
401 s
->gen_name
, s
->value
->u
.integervalue
);
407 case objectidentifiervalue
: {
408 struct objid
*o
, **list
;
413 generate_header_of_codefile(s
->gen_name
);
416 for (o
= s
->value
->u
.objectidentifiervalue
; o
!= NULL
; o
= o
->next
)
419 printf("s->gen_name: %s",s
->gen_name
);
423 list
= emalloc(sizeof(*list
) * len
);
426 for (o
= s
->value
->u
.objectidentifiervalue
; o
!= NULL
; o
= o
->next
)
429 fprintf (headerfile
, "/* OBJECT IDENTIFIER %s ::= { ", s
->name
);
430 for (i
= len
; i
> 0; i
--) {
432 fprintf(headerfile
, "%s(%d) ",
433 o
->label
? o
->label
: "label-less", o
->value
);
436 fprintf (codefile
, "static unsigned oid_%s_variable_num[%d] = {",
438 for (i
= len
; i
> 0; i
--) {
439 fprintf(codefile
, "%d%s ", list
[i
- 1]->value
, i
> 1 ? "," : "");
441 fprintf(codefile
, "};\n");
443 fprintf (codefile
, "const heim_oid asn1_oid_%s = "
444 "{ %d, oid_%s_variable_num };\n\n",
445 s
->gen_name
, len
, s
->gen_name
);
451 gen_upper
= strdup(s
->gen_name
);
452 len
= strlen(gen_upper
);
453 for (i
= 0; i
< len
; i
++)
454 gen_upper
[i
] = toupper((int)s
->gen_name
[i
]);
456 fprintf (headerfile
, "} */\n");
458 "extern ASN1EXP const heim_oid asn1_oid_%s;\n"
459 "#define ASN1_OID_%s (&asn1_oid_%s)\n\n",
477 is_primitive_type(int type
)
485 case TGeneralizedTime
:
491 case TPrintableString
:
494 case TUniversalString
:
507 fprintf(headerfile
, " ");
511 last_member_p(struct member
*m
)
513 struct member
*n
= ASN1_TAILQ_NEXT(m
, members
);
516 if (n
->ellipsis
&& ASN1_TAILQ_NEXT(n
, members
) == NULL
)
521 static struct member
*
522 have_ellipsis(Type
*t
)
525 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
533 define_asn1 (int level
, Type
*t
)
537 fprintf (headerfile
, "%s", t
->symbol
->name
);
540 if(t
->members
== NULL
) {
541 fprintf (headerfile
, "INTEGER");
543 fprintf (headerfile
, " (%d..%d)",
544 t
->range
->min
, t
->range
->max
);
547 fprintf (headerfile
, "INTEGER {\n");
548 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
550 fprintf(headerfile
, "%s(%d)%s\n", m
->gen_name
, m
->val
,
554 fprintf (headerfile
, "}");
558 fprintf (headerfile
, "BOOLEAN");
561 fprintf (headerfile
, "OCTET STRING");
568 if(t
->type
== TBitString
)
569 fprintf (headerfile
, "BIT STRING {\n");
571 fprintf (headerfile
, "ENUMERATED {\n");
572 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
574 fprintf (headerfile
, "%s(%d)%s\n", m
->name
, m
->val
,
578 fprintf (headerfile
, "}");
587 if(t
->type
== TChoice
)
588 fprintf(headerfile
, "CHOICE {\n");
589 else if(t
->type
== TSet
)
590 fprintf(headerfile
, "SET {\n");
592 fprintf(headerfile
, "SEQUENCE {\n");
593 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
594 if(strlen(m
->name
) > max_width
)
595 max_width
= strlen(m
->name
);
598 if(max_width
< 16) max_width
= 16;
599 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
600 int width
= max_width
;
603 fprintf (headerfile
, "...");
605 width
-= fprintf(headerfile
, "%s", m
->name
);
606 fprintf(headerfile
, "%*s", width
, "");
607 define_asn1(level
+ 1, m
->type
);
609 fprintf(headerfile
, " OPTIONAL");
612 fprintf (headerfile
, ",");
613 fprintf (headerfile
, "\n");
616 fprintf (headerfile
, "}");
620 fprintf (headerfile
, "SEQUENCE OF ");
621 define_asn1 (0, t
->subtype
);
624 fprintf (headerfile
, "SET OF ");
625 define_asn1 (0, t
->subtype
);
627 case TGeneralizedTime
:
628 fprintf (headerfile
, "GeneralizedTime");
631 fprintf (headerfile
, "GeneralString");
634 fprintf (headerfile
, "TeletexString");
637 const char *classnames
[] = { "UNIVERSAL ", "APPLICATION ",
638 "" /* CONTEXT */, "PRIVATE " };
639 if(t
->tag
.tagclass
!= ASN1_C_UNIV
)
640 fprintf (headerfile
, "[%s%d] ",
641 classnames
[t
->tag
.tagclass
],
643 if(t
->tag
.tagenv
== TE_IMPLICIT
)
644 fprintf (headerfile
, "IMPLICIT ");
645 define_asn1 (level
, t
->subtype
);
649 fprintf (headerfile
, "UTCTime");
653 fprintf (headerfile
, "UTF8String");
655 case TPrintableString
:
657 fprintf (headerfile
, "PrintableString");
661 fprintf (headerfile
, "IA5String");
665 fprintf (headerfile
, "BMPString");
667 case TUniversalString
:
669 fprintf (headerfile
, "UniversalString");
673 fprintf (headerfile
, "VisibleString");
677 fprintf(headerfile
, "OBJECT IDENTIFIER");
681 fprintf (headerfile
, "NULL");
689 getnewbasename(char **newbasename
, int typedefp
, const char *basename
, const char *name
)
692 *newbasename
= strdup(name
);
696 if (asprintf(newbasename
, "%s_%s", basename
, name
) < 0)
699 if (*newbasename
== NULL
)
704 define_type (int level
, const char *name
, const char *basename
, Type
*t
, int typedefp
, int preservep
)
706 char *newbasename
= NULL
;
711 fprintf (headerfile
, "%s %s;\n", t
->symbol
->gen_name
, name
);
717 fprintf (headerfile
, "enum %s {\n", typedefp
? name
: "");
718 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
720 fprintf(headerfile
, "%s = %d%s\n", m
->gen_name
, m
->val
,
723 fprintf (headerfile
, "} %s;\n", name
);
724 } else if (t
->range
== NULL
) {
725 fprintf (headerfile
, "heim_integer %s;\n", name
);
726 } else if (t
->range
->min
== INT_MIN
&& t
->range
->max
== INT_MAX
) {
727 fprintf (headerfile
, "int %s;\n", name
);
728 } else if (t
->range
->min
== 0 && t
->range
->max
== UINT_MAX
) {
729 fprintf (headerfile
, "unsigned int %s;\n", name
);
730 } else if (t
->range
->min
== 0 && t
->range
->max
== INT_MAX
) {
731 fprintf (headerfile
, "unsigned int %s;\n", name
);
733 errx(1, "%s: unsupported range %d -> %d",
734 name
, t
->range
->min
, t
->range
->max
);
738 fprintf (headerfile
, "int %s;\n", name
);
742 fprintf (headerfile
, "heim_octet_string %s;\n", name
);
747 struct range range
= { 0, INT_MAX
};
755 if(ASN1_TAILQ_EMPTY(t
->members
))
756 fprintf (headerfile
, "heim_bit_string %s;\n", name
);
759 getnewbasename(&newbasename
, typedefp
, basename
, name
);
761 fprintf (headerfile
, "struct %s {\n", newbasename
);
762 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
766 while (pos
< m
->val
) {
767 if (asprintf (&n
, "_unused%d:1", pos
) < 0 || n
== NULL
)
769 define_type (level
+ 1, n
, newbasename
, &i
, FALSE
, FALSE
);
775 if (asprintf (&n
, "%s:1", m
->gen_name
) < 0 || n
== NULL
)
777 define_type (level
+ 1, n
, newbasename
, &i
, FALSE
, FALSE
);
782 /* pad to 32 elements */
785 if (asprintf (&n
, "_unused%d:1", pos
) < 0 || n
== NULL
)
787 define_type (level
+ 1, n
, newbasename
, &i
, FALSE
, FALSE
);
793 fprintf (headerfile
, "} %s;\n\n", name
);
801 fprintf (headerfile
, "enum %s {\n", typedefp
? name
: "");
802 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
805 fprintf (headerfile
, "/* ... */\n");
807 fprintf (headerfile
, "%s = %d%s\n", m
->gen_name
, m
->val
,
811 fprintf (headerfile
, "} %s;\n\n", name
);
818 getnewbasename(&newbasename
, typedefp
, basename
, name
);
821 fprintf (headerfile
, "struct %s {\n", newbasename
);
822 if (t
->type
== TSequence
&& preservep
) {
824 fprintf(headerfile
, "heim_octet_string _save;\n");
826 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
829 } else if (m
->optional
) {
832 if (asprintf (&n
, "*%s", m
->gen_name
) < 0 || n
== NULL
)
834 define_type (level
+ 1, n
, newbasename
, m
->type
, FALSE
, FALSE
);
837 define_type (level
+ 1, m
->gen_name
, newbasename
, m
->type
, FALSE
, FALSE
);
840 fprintf (headerfile
, "} %s;\n", name
);
846 struct range range
= { 0, INT_MAX
};
848 getnewbasename(&newbasename
, typedefp
, basename
, name
);
856 fprintf (headerfile
, "struct %s {\n", newbasename
);
857 define_type (level
+ 1, "len", newbasename
, &i
, FALSE
, FALSE
);
858 define_type (level
+ 1, "*val", newbasename
, t
->subtype
, FALSE
, FALSE
);
860 fprintf (headerfile
, "} %s;\n", name
);
863 case TGeneralizedTime
:
865 fprintf (headerfile
, "time_t %s;\n", name
);
869 fprintf (headerfile
, "heim_general_string %s;\n", name
);
873 fprintf (headerfile
, "heim_general_string %s;\n", name
);
876 define_type (level
, name
, basename
, t
->subtype
, typedefp
, preservep
);
882 getnewbasename(&newbasename
, typedefp
, basename
, name
);
885 fprintf (headerfile
, "struct %s {\n", newbasename
);
888 fprintf(headerfile
, "heim_octet_string _save;\n");
891 fprintf (headerfile
, "enum {\n");
892 m
= have_ellipsis(t
);
895 fprintf (headerfile
, "%s = 0,\n", m
->label
);
898 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
901 fprintf (headerfile
, "/* ... */\n");
903 fprintf (headerfile
, "%s%s%s\n", m
->label
,
909 fprintf (headerfile
, "} element;\n");
911 fprintf (headerfile
, "union {\n");
912 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
915 fprintf(headerfile
, "heim_octet_string asn1_ellipsis;\n");
916 } else if (m
->optional
) {
919 if (asprintf (&n
, "*%s", m
->gen_name
) < 0 || n
== NULL
)
921 define_type (level
+ 2, n
, newbasename
, m
->type
, FALSE
, FALSE
);
924 define_type (level
+ 2, m
->gen_name
, newbasename
, m
->type
, FALSE
, FALSE
);
927 fprintf (headerfile
, "} u;\n");
929 fprintf (headerfile
, "} %s;\n", name
);
934 fprintf (headerfile
, "time_t %s;\n", name
);
938 fprintf (headerfile
, "heim_utf8_string %s;\n", name
);
940 case TPrintableString
:
942 fprintf (headerfile
, "heim_printable_string %s;\n", name
);
946 fprintf (headerfile
, "heim_ia5_string %s;\n", name
);
950 fprintf (headerfile
, "heim_bmp_string %s;\n", name
);
952 case TUniversalString
:
954 fprintf (headerfile
, "heim_universal_string %s;\n", name
);
958 fprintf (headerfile
, "heim_visible_string %s;\n", name
);
962 fprintf (headerfile
, "heim_oid %s;\n", name
);
966 fprintf (headerfile
, "int %s;\n", name
);
976 generate_type_header (const Symbol
*s
)
978 int preservep
= preserve_type(s
->name
) ? TRUE
: FALSE
;
980 fprintf (headerfile
, "/*\n");
981 fprintf (headerfile
, "%s ::= ", s
->name
);
982 define_asn1 (0, s
->type
);
983 fprintf (headerfile
, "\n*/\n\n");
985 fprintf (headerfile
, "typedef ");
986 define_type (0, s
->gen_name
, s
->gen_name
, s
->type
, TRUE
, preservep
);
988 fprintf (headerfile
, "\n");
992 generate_type (const Symbol
*s
)
998 generate_header_of_codefile(s
->gen_name
);
1000 generate_type_header (s
);
1003 generate_template(s
);
1005 if (template_flag
== 0 || is_template_compat(s
) == 0) {
1006 generate_type_encode (s
);
1007 generate_type_decode (s
);
1008 generate_type_free (s
);
1009 generate_type_length (s
);
1010 generate_type_copy (s
);
1012 generate_type_seq (s
);
1013 generate_glue (s
->type
, s
->gen_name
);
1015 /* generate prototypes */
1017 if (is_export(s
->name
)) {
1027 "decode_%s(const unsigned char *, size_t, %s *, size_t *);\n",
1029 s
->gen_name
, s
->gen_name
);
1032 "encode_%s(unsigned char *, size_t, const %s *, size_t *);\n",
1034 s
->gen_name
, s
->gen_name
);
1036 "%ssize_t ASN1CALL length_%s(const %s *);\n",
1038 s
->gen_name
, s
->gen_name
);
1040 "%sint ASN1CALL copy_%s (const %s *, %s *);\n",
1042 s
->gen_name
, s
->gen_name
, s
->gen_name
);
1044 "%svoid ASN1CALL free_%s (%s *);\n",
1046 s
->gen_name
, s
->gen_name
);
1050 if (!one_code_file
) {
1051 fprintf(codefile
, "\n\n");