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
);
288 fclose (privheaderfile
);
289 fclose (templatefile
);
290 fprintf (logfile
, "\n");
295 gen_assign_defval(const char *var
, struct value
*val
)
299 fprintf(codefile
, "if((%s = strdup(\"%s\")) == NULL)\nreturn ENOMEM;\n", var
, val
->u
.stringvalue
);
302 fprintf(codefile
, "%s = %d;\n", var
, val
->u
.integervalue
);
305 if(val
->u
.booleanvalue
)
306 fprintf(codefile
, "%s = TRUE;\n", var
);
308 fprintf(codefile
, "%s = FALSE;\n", var
);
316 gen_compare_defval(const char *var
, struct value
*val
)
320 fprintf(codefile
, "if(strcmp(%s, \"%s\") != 0)\n", var
, val
->u
.stringvalue
);
323 fprintf(codefile
, "if(%s != %d)\n", var
, val
->u
.integervalue
);
326 if(val
->u
.booleanvalue
)
327 fprintf(codefile
, "if(!%s)\n", var
);
329 fprintf(codefile
, "if(%s)\n", var
);
337 generate_header_of_codefile(const char *name
)
339 char *filename
= NULL
;
341 if (codefile
!= NULL
)
344 if (asprintf (&filename
, "%s_%s.x", STEM
, name
) < 0 || filename
== NULL
)
346 codefile
= fopen (filename
, "w");
347 if (codefile
== NULL
)
348 err (1, "fopen %s", filename
);
349 fprintf(logfile
, "%s ", filename
);
353 "/* Generated from %s */\n"
354 "/* Do not edit */\n\n"
355 "#define ASN1_LIB\n\n"
356 "#include <stdio.h>\n"
357 "#include <stdlib.h>\n"
358 "#include <time.h>\n"
359 "#include <string.h>\n"
360 "#include <errno.h>\n"
361 "#include <limits.h>\n"
362 "#include <krb5-types.h>\n",
370 "#include <asn1_err.h>\n"
372 "#include <der-private.h>\n"
373 "#include <asn1-template.h>\n"
374 "#include <parse_units.h>\n\n");
381 if (codefile
== NULL
)
390 generate_constant (const Symbol
*s
)
392 switch(s
->value
->type
) {
396 fprintf (headerfile
, "enum { %s = %d };\n\n",
397 s
->gen_name
, s
->value
->u
.integervalue
);
403 case objectidentifiervalue
: {
404 struct objid
*o
, **list
;
409 generate_header_of_codefile(s
->gen_name
);
412 for (o
= s
->value
->u
.objectidentifiervalue
; o
!= NULL
; o
= o
->next
)
415 printf("s->gen_name: %s",s
->gen_name
);
419 list
= emalloc(sizeof(*list
) * len
);
422 for (o
= s
->value
->u
.objectidentifiervalue
; o
!= NULL
; o
= o
->next
)
425 fprintf (headerfile
, "/* OBJECT IDENTIFIER %s ::= { ", s
->name
);
426 for (i
= len
; i
> 0; i
--) {
428 fprintf(headerfile
, "%s(%d) ",
429 o
->label
? o
->label
: "label-less", o
->value
);
432 fprintf (codefile
, "static unsigned oid_%s_variable_num[%d] = {",
434 for (i
= len
; i
> 0; i
--) {
435 fprintf(codefile
, "%d%s ", list
[i
- 1]->value
, i
> 1 ? "," : "");
437 fprintf(codefile
, "};\n");
439 fprintf (codefile
, "const heim_oid asn1_oid_%s = "
440 "{ %d, oid_%s_variable_num };\n\n",
441 s
->gen_name
, len
, s
->gen_name
);
447 gen_upper
= strdup(s
->gen_name
);
448 len
= strlen(gen_upper
);
449 for (i
= 0; i
< len
; i
++)
450 gen_upper
[i
] = toupper((int)s
->gen_name
[i
]);
452 fprintf (headerfile
, "} */\n");
454 "extern ASN1EXP const heim_oid asn1_oid_%s;\n"
455 "#define ASN1_OID_%s (&asn1_oid_%s)\n\n",
473 is_primitive_type(int type
)
481 case TGeneralizedTime
:
487 case TPrintableString
:
490 case TUniversalString
:
503 fprintf(headerfile
, " ");
507 last_member_p(struct member
*m
)
509 struct member
*n
= ASN1_TAILQ_NEXT(m
, members
);
512 if (n
->ellipsis
&& ASN1_TAILQ_NEXT(n
, members
) == NULL
)
517 static struct member
*
518 have_ellipsis(Type
*t
)
521 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
529 define_asn1 (int level
, Type
*t
)
533 fprintf (headerfile
, "%s", t
->symbol
->name
);
536 if(t
->members
== NULL
) {
537 fprintf (headerfile
, "INTEGER");
539 fprintf (headerfile
, " (%d..%d)",
540 t
->range
->min
, t
->range
->max
);
543 fprintf (headerfile
, "INTEGER {\n");
544 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
546 fprintf(headerfile
, "%s(%d)%s\n", m
->gen_name
, m
->val
,
550 fprintf (headerfile
, "}");
554 fprintf (headerfile
, "BOOLEAN");
557 fprintf (headerfile
, "OCTET STRING");
564 if(t
->type
== TBitString
)
565 fprintf (headerfile
, "BIT STRING {\n");
567 fprintf (headerfile
, "ENUMERATED {\n");
568 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
570 fprintf (headerfile
, "%s(%d)%s\n", m
->name
, m
->val
,
574 fprintf (headerfile
, "}");
583 if(t
->type
== TChoice
)
584 fprintf(headerfile
, "CHOICE {\n");
585 else if(t
->type
== TSet
)
586 fprintf(headerfile
, "SET {\n");
588 fprintf(headerfile
, "SEQUENCE {\n");
589 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
590 if(strlen(m
->name
) > max_width
)
591 max_width
= strlen(m
->name
);
594 if(max_width
< 16) max_width
= 16;
595 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
596 int width
= max_width
;
599 fprintf (headerfile
, "...");
601 width
-= fprintf(headerfile
, "%s", m
->name
);
602 fprintf(headerfile
, "%*s", width
, "");
603 define_asn1(level
+ 1, m
->type
);
605 fprintf(headerfile
, " OPTIONAL");
608 fprintf (headerfile
, ",");
609 fprintf (headerfile
, "\n");
612 fprintf (headerfile
, "}");
616 fprintf (headerfile
, "SEQUENCE OF ");
617 define_asn1 (0, t
->subtype
);
620 fprintf (headerfile
, "SET OF ");
621 define_asn1 (0, t
->subtype
);
623 case TGeneralizedTime
:
624 fprintf (headerfile
, "GeneralizedTime");
627 fprintf (headerfile
, "GeneralString");
630 fprintf (headerfile
, "TeletexString");
633 const char *classnames
[] = { "UNIVERSAL ", "APPLICATION ",
634 "" /* CONTEXT */, "PRIVATE " };
635 if(t
->tag
.tagclass
!= ASN1_C_UNIV
)
636 fprintf (headerfile
, "[%s%d] ",
637 classnames
[t
->tag
.tagclass
],
639 if(t
->tag
.tagenv
== TE_IMPLICIT
)
640 fprintf (headerfile
, "IMPLICIT ");
641 define_asn1 (level
, t
->subtype
);
645 fprintf (headerfile
, "UTCTime");
649 fprintf (headerfile
, "UTF8String");
651 case TPrintableString
:
653 fprintf (headerfile
, "PrintableString");
657 fprintf (headerfile
, "IA5String");
661 fprintf (headerfile
, "BMPString");
663 case TUniversalString
:
665 fprintf (headerfile
, "UniversalString");
669 fprintf (headerfile
, "VisibleString");
673 fprintf(headerfile
, "OBJECT IDENTIFIER");
677 fprintf (headerfile
, "NULL");
685 getnewbasename(char **newbasename
, int typedefp
, const char *basename
, const char *name
)
688 *newbasename
= strdup(name
);
692 if (asprintf(newbasename
, "%s_%s", basename
, name
) < 0)
695 if (*newbasename
== NULL
)
700 define_type (int level
, const char *name
, const char *basename
, Type
*t
, int typedefp
, int preservep
)
702 char *newbasename
= NULL
;
707 fprintf (headerfile
, "%s %s;\n", t
->symbol
->gen_name
, name
);
713 fprintf (headerfile
, "enum %s {\n", typedefp
? name
: "");
714 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
716 fprintf(headerfile
, "%s = %d%s\n", m
->gen_name
, m
->val
,
719 fprintf (headerfile
, "} %s;\n", name
);
720 } else if (t
->range
== NULL
) {
721 fprintf (headerfile
, "heim_integer %s;\n", name
);
722 } else if (t
->range
->min
== INT_MIN
&& t
->range
->max
== INT_MAX
) {
723 fprintf (headerfile
, "int %s;\n", name
);
724 } else if (t
->range
->min
== 0 && t
->range
->max
== UINT_MAX
) {
725 fprintf (headerfile
, "unsigned int %s;\n", name
);
726 } else if (t
->range
->min
== 0 && t
->range
->max
== INT_MAX
) {
727 fprintf (headerfile
, "unsigned int %s;\n", name
);
729 errx(1, "%s: unsupported range %d -> %d",
730 name
, t
->range
->min
, t
->range
->max
);
734 fprintf (headerfile
, "int %s;\n", name
);
738 fprintf (headerfile
, "heim_octet_string %s;\n", name
);
743 struct range range
= { 0, INT_MAX
};
751 if(ASN1_TAILQ_EMPTY(t
->members
))
752 fprintf (headerfile
, "heim_bit_string %s;\n", name
);
755 getnewbasename(&newbasename
, typedefp
, basename
, name
);
757 fprintf (headerfile
, "struct %s {\n", newbasename
);
758 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
762 while (pos
< m
->val
) {
763 if (asprintf (&n
, "_unused%d:1", pos
) < 0 || n
== NULL
)
765 define_type (level
+ 1, n
, newbasename
, &i
, FALSE
, FALSE
);
771 if (asprintf (&n
, "%s:1", m
->gen_name
) < 0 || n
== NULL
)
773 define_type (level
+ 1, n
, newbasename
, &i
, FALSE
, FALSE
);
778 /* pad to 32 elements */
781 if (asprintf (&n
, "_unused%d:1", pos
) < 0 || n
== NULL
)
783 define_type (level
+ 1, n
, newbasename
, &i
, FALSE
, FALSE
);
789 fprintf (headerfile
, "} %s;\n\n", name
);
797 fprintf (headerfile
, "enum %s {\n", typedefp
? name
: "");
798 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
801 fprintf (headerfile
, "/* ... */\n");
803 fprintf (headerfile
, "%s = %d%s\n", m
->gen_name
, m
->val
,
807 fprintf (headerfile
, "} %s;\n\n", name
);
814 getnewbasename(&newbasename
, typedefp
, basename
, name
);
817 fprintf (headerfile
, "struct %s {\n", newbasename
);
818 if (t
->type
== TSequence
&& preservep
) {
820 fprintf(headerfile
, "heim_octet_string _save;\n");
822 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
825 } else if (m
->optional
) {
828 if (asprintf (&n
, "*%s", m
->gen_name
) < 0 || n
== NULL
)
830 define_type (level
+ 1, n
, newbasename
, m
->type
, FALSE
, FALSE
);
833 define_type (level
+ 1, m
->gen_name
, newbasename
, m
->type
, FALSE
, FALSE
);
836 fprintf (headerfile
, "} %s;\n", name
);
842 struct range range
= { 0, INT_MAX
};
844 getnewbasename(&newbasename
, typedefp
, basename
, name
);
852 fprintf (headerfile
, "struct %s {\n", newbasename
);
853 define_type (level
+ 1, "len", newbasename
, &i
, FALSE
, FALSE
);
854 define_type (level
+ 1, "*val", newbasename
, t
->subtype
, FALSE
, FALSE
);
856 fprintf (headerfile
, "} %s;\n", name
);
859 case TGeneralizedTime
:
861 fprintf (headerfile
, "time_t %s;\n", name
);
865 fprintf (headerfile
, "heim_general_string %s;\n", name
);
869 fprintf (headerfile
, "heim_general_string %s;\n", name
);
872 define_type (level
, name
, basename
, t
->subtype
, typedefp
, preservep
);
878 getnewbasename(&newbasename
, typedefp
, basename
, name
);
881 fprintf (headerfile
, "struct %s {\n", newbasename
);
884 fprintf(headerfile
, "heim_octet_string _save;\n");
887 fprintf (headerfile
, "enum {\n");
888 m
= have_ellipsis(t
);
891 fprintf (headerfile
, "%s = 0,\n", m
->label
);
894 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
897 fprintf (headerfile
, "/* ... */\n");
899 fprintf (headerfile
, "%s%s%s\n", m
->label
,
905 fprintf (headerfile
, "} element;\n");
907 fprintf (headerfile
, "union {\n");
908 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
911 fprintf(headerfile
, "heim_octet_string asn1_ellipsis;\n");
912 } else if (m
->optional
) {
915 if (asprintf (&n
, "*%s", m
->gen_name
) < 0 || n
== NULL
)
917 define_type (level
+ 2, n
, newbasename
, m
->type
, FALSE
, FALSE
);
920 define_type (level
+ 2, m
->gen_name
, newbasename
, m
->type
, FALSE
, FALSE
);
923 fprintf (headerfile
, "} u;\n");
925 fprintf (headerfile
, "} %s;\n", name
);
930 fprintf (headerfile
, "time_t %s;\n", name
);
934 fprintf (headerfile
, "heim_utf8_string %s;\n", name
);
936 case TPrintableString
:
938 fprintf (headerfile
, "heim_printable_string %s;\n", name
);
942 fprintf (headerfile
, "heim_ia5_string %s;\n", name
);
946 fprintf (headerfile
, "heim_bmp_string %s;\n", name
);
948 case TUniversalString
:
950 fprintf (headerfile
, "heim_universal_string %s;\n", name
);
954 fprintf (headerfile
, "heim_visible_string %s;\n", name
);
958 fprintf (headerfile
, "heim_oid %s;\n", name
);
962 fprintf (headerfile
, "int %s;\n", name
);
972 generate_type_header (const Symbol
*s
)
974 int preservep
= preserve_type(s
->name
) ? TRUE
: FALSE
;
976 fprintf (headerfile
, "/*\n");
977 fprintf (headerfile
, "%s ::= ", s
->name
);
978 define_asn1 (0, s
->type
);
979 fprintf (headerfile
, "\n*/\n\n");
981 fprintf (headerfile
, "typedef ");
982 define_type (0, s
->gen_name
, s
->gen_name
, s
->type
, TRUE
, preservep
);
984 fprintf (headerfile
, "\n");
988 generate_type (const Symbol
*s
)
994 generate_header_of_codefile(s
->gen_name
);
996 generate_type_header (s
);
999 generate_template(s
);
1001 if (template_flag
== 0 || is_template_compat(s
) == 0) {
1002 generate_type_encode (s
);
1003 generate_type_decode (s
);
1004 generate_type_free (s
);
1005 generate_type_length (s
);
1006 generate_type_copy (s
);
1008 generate_type_seq (s
);
1009 generate_glue (s
->type
, s
->gen_name
);
1011 /* generate prototypes */
1013 if (is_export(s
->name
)) {
1023 "decode_%s(const unsigned char *, size_t, %s *, size_t *);\n",
1025 s
->gen_name
, s
->gen_name
);
1028 "encode_%s(unsigned char *, size_t, const %s *, size_t *);\n",
1030 s
->gen_name
, s
->gen_name
);
1032 "%ssize_t ASN1CALL length_%s(const %s *);\n",
1034 s
->gen_name
, s
->gen_name
);
1036 "%sint ASN1CALL copy_%s (const %s *, %s *);\n",
1038 s
->gen_name
, s
->gen_name
, s
->gen_name
);
1040 "%svoid ASN1CALL free_%s (%s *);\n",
1042 s
->gen_name
, s
->gen_name
);
1046 if (!one_code_file
) {
1047 fprintf(codefile
, "\n\n");