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.x", 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 "#ifndef __HEIM_BASE_DATA__\n"
168 "#define __HEIM_BASE_DATA__ 1\n"
169 "struct heim_base_data {\n"
173 "typedef struct heim_base_data heim_octet_string;\n"
176 "typedef struct heim_integer {\n"
180 "} heim_integer;\n\n");
182 "typedef char *heim_general_string;\n\n"
185 "typedef char *heim_utf8_string;\n\n"
188 "typedef struct heim_base_data heim_printable_string;\n\n"
191 "typedef struct heim_base_data heim_ia5_string;\n\n"
194 "typedef struct heim_bmp_string {\n"
197 "} heim_bmp_string;\n\n");
199 "typedef struct heim_universal_string {\n"
202 "} heim_universal_string;\n\n");
204 "typedef char *heim_visible_string;\n\n"
207 "typedef struct heim_oid {\n"
209 " unsigned *components;\n"
212 "typedef struct heim_bit_string {\n"
215 "} heim_bit_string;\n\n");
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"
221 " (BL) = length_##T((S)); \\\n"
222 " (B) = malloc((BL)); \\\n"
223 " if((B) == NULL) { \\\n"
224 " (R) = ENOMEM; \\\n"
226 " (R) = encode_##T(((unsigned char*)(B)) + (BL) - 1, (BL), \\\n"
228 " if((R) != 0) { \\\n"
235 fputs("#ifdef _WIN32\n"
237 "#define ASN1EXP __declspec(dllimport)\n"
241 "#define ASN1CALL __stdcall\n"
247 fprintf (headerfile
, "struct units;\n\n");
248 fprintf (headerfile
, "#endif\n\n");
249 if (asprintf(&fn
, "%s_files", base
) < 0 || fn
== NULL
)
251 logfile
= fopen(fn
, "w");
253 err (1, "open %s", fn
);
255 /* if one code file, write into the one codefile */
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"
276 fprintf (templatefile
,
280 "#include <asn1-template.h>\n",
287 close_generate (void)
289 fprintf (headerfile
, "#endif /* __%s_h__ */\n", headerbase
);
294 fclose (privheaderfile
);
296 fclose (templatefile
);
298 fprintf (logfile
, "\n");
303 gen_assign_defval(const char *var
, struct value
*val
)
307 fprintf(codefile
, "if((%s = strdup(\"%s\")) == NULL)\nreturn ENOMEM;\n", var
, val
->u
.stringvalue
);
310 fprintf(codefile
, "%s = %lld;\n",
311 var
, (long long)val
->u
.integervalue
);
314 if(val
->u
.booleanvalue
)
315 fprintf(codefile
, "%s = TRUE;\n", var
);
317 fprintf(codefile
, "%s = FALSE;\n", var
);
325 gen_compare_defval(const char *var
, struct value
*val
)
329 fprintf(codefile
, "if(strcmp(%s, \"%s\") != 0)\n", var
, val
->u
.stringvalue
);
332 fprintf(codefile
, "if(%s != %lld)\n",
333 var
, (long long)val
->u
.integervalue
);
336 if(val
->u
.booleanvalue
)
337 fprintf(codefile
, "if(!%s)\n", var
);
339 fprintf(codefile
, "if(%s)\n", var
);
347 generate_header_of_codefile(const char *name
)
349 char *filename
= NULL
;
351 if (codefile
!= NULL
)
354 if (asprintf (&filename
, "%s_%s.x", STEM
, name
) < 0 || filename
== NULL
)
356 codefile
= fopen (filename
, "w");
357 if (codefile
== NULL
)
358 err (1, "fopen %s", filename
);
359 fprintf(logfile
, "%s ", filename
);
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"
381 "#include <asn1_err.h>\n"
383 "#include <asn1-template.h>\n\n");
385 if (parse_units_flag
)
387 "#include <parse_units.h>\n\n");
394 if (codefile
== NULL
)
403 generate_constant (const Symbol
*s
)
405 switch(s
->value
->type
) {
409 fprintf (headerfile
, "enum { %s = %lld };\n\n",
411 (long long)s
->value
->u
.integervalue
);
417 case objectidentifiervalue
: {
418 struct objid
*o
, **list
;
423 generate_header_of_codefile(s
->gen_name
);
426 for (o
= s
->value
->u
.objectidentifiervalue
; o
!= NULL
; o
= o
->next
)
429 printf("s->gen_name: %s",s
->gen_name
);
433 list
= emalloc(sizeof(*list
) * len
);
436 for (o
= s
->value
->u
.objectidentifiervalue
; o
!= NULL
; o
= o
->next
)
439 fprintf (headerfile
, "/* OBJECT IDENTIFIER %s ::= { ", s
->name
);
440 for (i
= len
; i
> 0; i
--) {
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
);
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");
468 "extern ASN1EXP const heim_oid asn1_oid_%s;\n"
469 "#define ASN1_OID_%s (&asn1_oid_%s)\n\n",
487 is_primitive_type(int type
)
495 case TGeneralizedTime
:
501 case TPrintableString
:
504 case TUniversalString
:
517 fprintf(headerfile
, " ");
521 last_member_p(struct member
*m
)
523 struct member
*n
= ASN1_TAILQ_NEXT(m
, members
);
526 if (n
->ellipsis
&& ASN1_TAILQ_NEXT(n
, members
) == NULL
)
531 static struct member
*
532 have_ellipsis(Type
*t
)
535 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
543 define_asn1 (int level
, Type
*t
)
547 fprintf (headerfile
, "%s", t
->symbol
->name
);
550 if(t
->members
== NULL
) {
551 fprintf (headerfile
, "INTEGER");
553 fprintf (headerfile
, " (%lld..%lld)",
554 (long long)t
->range
->min
,
555 (long long)t
->range
->max
);
558 fprintf (headerfile
, "INTEGER {\n");
559 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
561 fprintf(headerfile
, "%s(%d)%s\n", m
->gen_name
, m
->val
,
565 fprintf (headerfile
, "}");
569 fprintf (headerfile
, "BOOLEAN");
572 fprintf (headerfile
, "OCTET STRING");
579 if(t
->type
== TBitString
)
580 fprintf (headerfile
, "BIT STRING {\n");
582 fprintf (headerfile
, "ENUMERATED {\n");
583 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
585 fprintf (headerfile
, "%s(%d)%s\n", m
->name
, m
->val
,
589 fprintf (headerfile
, "}");
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");
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
);
609 if(max_width
< 16) max_width
= 16;
610 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
611 size_t width
= max_width
;
614 fprintf (headerfile
, "...");
616 width
-= fprintf(headerfile
, "%s", m
->name
);
617 fprintf(headerfile
, "%*s", (int)width
, "");
618 define_asn1(level
+ 1, m
->type
);
620 fprintf(headerfile
, " OPTIONAL");
623 fprintf (headerfile
, ",");
624 fprintf (headerfile
, "\n");
627 fprintf (headerfile
, "}");
631 fprintf (headerfile
, "SEQUENCE OF ");
632 define_asn1 (0, t
->subtype
);
635 fprintf (headerfile
, "SET OF ");
636 define_asn1 (0, t
->subtype
);
638 case TGeneralizedTime
:
639 fprintf (headerfile
, "GeneralizedTime");
642 fprintf (headerfile
, "GeneralString");
645 fprintf (headerfile
, "TeletexString");
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
],
654 if(t
->tag
.tagenv
== TE_IMPLICIT
)
655 fprintf (headerfile
, "IMPLICIT ");
656 define_asn1 (level
, t
->subtype
);
660 fprintf (headerfile
, "UTCTime");
664 fprintf (headerfile
, "UTF8String");
666 case TPrintableString
:
668 fprintf (headerfile
, "PrintableString");
672 fprintf (headerfile
, "IA5String");
676 fprintf (headerfile
, "BMPString");
678 case TUniversalString
:
680 fprintf (headerfile
, "UniversalString");
684 fprintf (headerfile
, "VisibleString");
688 fprintf(headerfile
, "OBJECT IDENTIFIER");
692 fprintf (headerfile
, "NULL");
700 getnewbasename(char **newbasename
, int typedefp
, const char *basename
, const char *name
)
703 *newbasename
= strdup(name
);
707 if (asprintf(newbasename
, "%s_%s", basename
, name
) < 0)
710 if (*newbasename
== NULL
)
715 define_type (int level
, const char *name
, const char *basename
, Type
*t
, int typedefp
, int preservep
)
717 char *newbasename
= NULL
;
722 fprintf (headerfile
, "%s %s;\n", t
->symbol
->gen_name
, name
);
728 fprintf (headerfile
, "enum %s {\n", typedefp
? name
: "");
729 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
731 fprintf(headerfile
, "%s = %d%s\n", m
->gen_name
, m
->val
,
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
);
746 errx(1, "%s: unsupported range %lld -> %lld",
747 name
, (long long)t
->range
->min
, (long long)t
->range
->max
);
751 fprintf (headerfile
, "int %s;\n", name
);
755 fprintf (headerfile
, "heim_octet_string %s;\n", name
);
760 struct range range
= { 0, UINT_MAX
};
768 if(ASN1_TAILQ_EMPTY(t
->members
))
769 fprintf (headerfile
, "heim_bit_string %s;\n", name
);
772 getnewbasename(&newbasename
, typedefp
, basename
, name
);
774 fprintf (headerfile
, "struct %s {\n", newbasename
);
775 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
779 while (pos
< m
->val
) {
780 if (asprintf (&n
, "_unused%d:1", pos
) < 0 || n
== NULL
)
782 define_type (level
+ 1, n
, newbasename
, &i
, FALSE
, FALSE
);
788 if (asprintf (&n
, "%s:1", m
->gen_name
) < 0 || n
== NULL
)
790 define_type (level
+ 1, n
, newbasename
, &i
, FALSE
, FALSE
);
795 /* pad to 32 elements */
798 if (asprintf (&n
, "_unused%d:1", pos
) < 0 || n
== NULL
)
800 define_type (level
+ 1, n
, newbasename
, &i
, FALSE
, FALSE
);
806 fprintf (headerfile
, "} %s;\n\n", name
);
814 fprintf (headerfile
, "enum %s {\n", typedefp
? name
: "");
815 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
818 fprintf (headerfile
, "/* ... */\n");
820 fprintf (headerfile
, "%s = %d%s\n", m
->gen_name
, m
->val
,
824 fprintf (headerfile
, "} %s;\n\n", name
);
831 getnewbasename(&newbasename
, typedefp
, basename
, name
);
834 fprintf (headerfile
, "struct %s {\n", newbasename
);
835 if (t
->type
== TSequence
&& preservep
) {
837 fprintf(headerfile
, "heim_octet_string _save;\n");
839 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
842 } else if (m
->optional
) {
845 if (asprintf (&n
, "*%s", m
->gen_name
) < 0 || n
== NULL
)
847 define_type (level
+ 1, n
, newbasename
, m
->type
, FALSE
, FALSE
);
850 define_type (level
+ 1, m
->gen_name
, newbasename
, m
->type
, FALSE
, FALSE
);
853 fprintf (headerfile
, "} %s;\n", name
);
859 struct range range
= { 0, UINT_MAX
};
861 getnewbasename(&newbasename
, typedefp
, basename
, name
);
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
);
873 fprintf (headerfile
, "} %s;\n", name
);
876 case TGeneralizedTime
:
878 fprintf (headerfile
, "time_t %s;\n", name
);
882 fprintf (headerfile
, "heim_general_string %s;\n", name
);
886 fprintf (headerfile
, "heim_general_string %s;\n", name
);
889 define_type (level
, name
, basename
, t
->subtype
, typedefp
, preservep
);
895 getnewbasename(&newbasename
, typedefp
, basename
, name
);
898 fprintf (headerfile
, "struct %s {\n", newbasename
);
901 fprintf(headerfile
, "heim_octet_string _save;\n");
904 fprintf (headerfile
, "enum %s_enum {\n", newbasename
);
905 m
= have_ellipsis(t
);
908 fprintf (headerfile
, "%s = 0,\n", m
->label
);
911 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
914 fprintf (headerfile
, "/* ... */\n");
916 fprintf (headerfile
, "%s%s%s\n", m
->label
,
922 fprintf (headerfile
, "} element;\n");
924 fprintf (headerfile
, "union {\n");
925 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
928 fprintf(headerfile
, "heim_octet_string asn1_ellipsis;\n");
929 } else if (m
->optional
) {
932 if (asprintf (&n
, "*%s", m
->gen_name
) < 0 || n
== NULL
)
934 define_type (level
+ 2, n
, newbasename
, m
->type
, FALSE
, FALSE
);
937 define_type (level
+ 2, m
->gen_name
, newbasename
, m
->type
, FALSE
, FALSE
);
940 fprintf (headerfile
, "} u;\n");
942 fprintf (headerfile
, "} %s;\n", name
);
947 fprintf (headerfile
, "time_t %s;\n", name
);
951 fprintf (headerfile
, "heim_utf8_string %s;\n", name
);
953 case TPrintableString
:
955 fprintf (headerfile
, "heim_printable_string %s;\n", name
);
959 fprintf (headerfile
, "heim_ia5_string %s;\n", name
);
963 fprintf (headerfile
, "heim_bmp_string %s;\n", name
);
965 case TUniversalString
:
967 fprintf (headerfile
, "heim_universal_string %s;\n", name
);
971 fprintf (headerfile
, "heim_visible_string %s;\n", name
);
975 fprintf (headerfile
, "heim_oid %s;\n", name
);
979 fprintf (headerfile
, "int %s;\n", name
);
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");
1005 generate_type (const Symbol
*s
)
1011 generate_header_of_codefile(s
->gen_name
);
1013 generate_type_header (s
);
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
)) {
1040 "decode_%s(const unsigned char *, size_t, %s *, size_t *);\n",
1042 s
->gen_name
, s
->gen_name
);
1045 "encode_%s(unsigned char *, size_t, const %s *, size_t *);\n",
1047 s
->gen_name
, s
->gen_name
);
1049 "%ssize_t ASN1CALL length_%s(const %s *);\n",
1051 s
->gen_name
, s
->gen_name
);
1053 "%sint ASN1CALL copy_%s (const %s *, %s *);\n",
1055 s
->gen_name
, s
->gen_name
, s
->gen_name
);
1057 "%svoid ASN1CALL free_%s (%s *);\n",
1059 s
->gen_name
, s
->gen_name
);
1063 if (!one_code_file
) {
1064 fprintf(codefile
, "\n\n");