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
38 static const char *symbol_name(const char *, const Type
*);
39 static void generate_template_type(const char *, const char **, const char *, const char *, const char *,
40 Type
*, int, int, int);
43 ttype_symbol(const char *basename
, const Type
*t
)
45 return t
->symbol
->gen_name
;
49 integer_symbol(const char *basename
, const Type
*t
)
52 return "int"; /* XXX enum foo */
53 else if (t
->range
== NULL
)
54 return "heim_integer";
55 else if (t
->range
->min
== INT_MIN
&& t
->range
->max
== INT_MAX
)
57 else if (t
->range
->min
== 0 && t
->range
->max
== UINT_MAX
)
59 else if (t
->range
->min
== 0 && t
->range
->max
== INT_MAX
)
63 UNREACHABLE(return NULL
);
68 boolean_symbol(const char *basename
, const Type
*t
)
75 octetstring_symbol(const char *basename
, const Type
*t
)
77 return "heim_octet_string";
81 sequence_symbol(const char *basename
, const Type
*t
)
87 time_symbol(const char *basename
, const Type
*t
)
93 tag_symbol(const char *basename
, const Type
*t
)
95 return symbol_name(basename
, t
->subtype
);
99 generalstring_symbol(const char *basename
, const Type
*t
)
101 return "heim_general_string";
105 printablestring_symbol(const char *basename
, const Type
*t
)
107 return "heim_printable_string";
111 ia5string_symbol(const char *basename
, const Type
*t
)
113 return "heim_ia5_string";
117 visiblestring_symbol(const char *basename
, const Type
*t
)
119 return "heim_visible_string";
123 utf8string_symbol(const char *basename
, const Type
*t
)
125 return "heim_utf8_string";
129 bmpstring_symbol(const char *basename
, const Type
*t
)
131 return "heim_bmp_string";
135 universalstring_symbol(const char *basename
, const Type
*t
)
137 return "heim_universal_string";
141 oid_symbol(const char *basename
, const Type
*t
)
147 bitstring_symbol(const char *basename
, const Type
*t
)
151 return "heim_bit_string";
158 const char *(*symbol_name
)(const char *, const Type
*);
161 { TBMPString
, bmpstring_symbol
, 0 },
162 { TBitString
, bitstring_symbol
, 0 },
163 { TBoolean
, boolean_symbol
, 0 },
164 { TGeneralString
, generalstring_symbol
, 0 },
165 { TGeneralizedTime
, time_symbol
, 0 },
166 { TIA5String
, ia5string_symbol
, 0 },
167 { TInteger
, integer_symbol
, 0 },
168 { TOID
, oid_symbol
, 0 },
169 { TOctetString
, octetstring_symbol
, 0 },
170 { TPrintableString
, printablestring_symbol
, 0 },
171 { TSequence
, sequence_symbol
, 1 },
172 { TSequenceOf
, tag_symbol
, 1 },
173 { TSetOf
, tag_symbol
, 1 },
174 { TTag
, tag_symbol
, 1 },
175 { TType
, ttype_symbol
, 1 },
176 { TUTCTime
, time_symbol
, 0 },
177 { TUniversalString
, universalstring_symbol
, 0 },
178 { TVisibleString
, visiblestring_symbol
, 0 },
179 { TUTF8String
, utf8string_symbol
, 0 },
180 { TChoice
, sequence_symbol
, 1 },
181 { TNull
, integer_symbol
, 1 }
194 is_supported_type_p(const Type
*t
)
198 for (i
= 0; i
< sizeof(types
)/sizeof(types
[0]); i
++)
199 if (t
->type
== types
[i
].type
)
205 is_template_compat (const Symbol
*s
)
207 return is_supported_type_p(s
->type
);
211 symbol_name(const char *basename
, const Type
*t
)
215 for (i
= 0; i
< sizeof(types
)/sizeof(types
[0]); i
++)
216 if (t
->type
== types
[i
].type
)
217 return (types
[i
].symbol_name
)(basename
, t
);
218 printf("unknown der type: %d\n", t
->type
);
224 partial_offset(const char *basetype
, const char *name
, int need_offset
)
227 if (name
== NULL
|| need_offset
== 0)
229 if (asprintf(&str
, "offsetof(struct %s, %s)", basetype
, name
) < 0 || str
== NULL
)
239 ASN1_TAILQ_ENTRY(template) members
;
242 ASN1_TAILQ_HEAD(templatehead
, template);
247 struct templatehead
template;
248 ASN1_TAILQ_ENTRY(tlist
) tmembers
;
251 ASN1_TAILQ_HEAD(tlisthead
, tlist
);
253 static void tlist_header(struct tlist
*, const char *, ...) __attribute__((__format__(__printf__
, 2, 3)));
254 static struct template *
255 add_line(struct templatehead
*, const char *, ...) __attribute__((__format__(__printf__
, 2, 3)));
256 static int tlist_cmp(const struct tlist
*, const struct tlist
*);
258 static void add_line_pointer(struct templatehead
*, const char *, const char *, const char *, ...)
259 __attribute__((__format__(__printf__
, 4, 5)));
262 static struct tlisthead tlistmaster
= ASN1_TAILQ_HEAD_INITIALIZER(tlistmaster
);
263 static unsigned long numdups
= 0;
265 static struct tlist
*
266 tlist_new(const char *name
)
268 struct tlist
*tl
= calloc(1, sizeof(*tl
));
269 tl
->name
= strdup(name
);
270 ASN1_TAILQ_INIT(&tl
->template);
275 tlist_header(struct tlist
*t
, const char *fmt
, ...)
279 if (vasprintf(&t
->header
, fmt
, ap
) < 0 || t
->header
== NULL
)
285 tlist_count(struct tlist
*tl
)
287 unsigned int count
= 0;
290 ASN1_TAILQ_FOREACH(q
, &tl
->template, members
) {
297 tlist_add(struct tlist
*tl
)
299 ASN1_TAILQ_INSERT_TAIL(&tlistmaster
, tl
, tmembers
);
303 tlist_print(struct tlist
*tl
)
307 FILE *f
= get_code_file();
309 fprintf(f
, "static const struct asn1_template asn1_%s[] = {\n", tl
->name
);
310 fprintf(f
, "/* 0 */ %s,\n", tl
->header
);
311 ASN1_TAILQ_FOREACH(q
, &tl
->template, members
) {
312 int last
= (ASN1_TAILQ_LAST(&tl
->template, templatehead
) == q
);
313 fprintf(f
, "/* %lu */ %s%s\n", (unsigned long)i
++, q
->line
, last
? "" : ",");
318 static struct tlist
*
319 tlist_find_by_name(const char *name
)
322 ASN1_TAILQ_FOREACH(ql
, &tlistmaster
, tmembers
) {
323 if (strcmp(ql
->name
, name
) == 0)
330 tlist_cmp_name(const char *tname
, const char *qname
)
332 struct tlist
*tl
= tlist_find_by_name(tname
);
333 struct tlist
*ql
= tlist_find_by_name(qname
);
334 return tlist_cmp(tl
, ql
);
338 tlist_cmp(const struct tlist
*tl
, const struct tlist
*ql
)
341 struct template *t
, *q
;
343 ret
= strcmp(tl
->header
, ql
->header
);
346 q
= ASN1_TAILQ_FIRST(&ql
->template);
347 ASN1_TAILQ_FOREACH(t
, &tl
->template, members
) {
348 if (q
== NULL
) return 1;
350 if (t
->ptr
== NULL
|| q
->ptr
== NULL
) {
351 ret
= strcmp(t
->line
, q
->line
);
354 ret
= strcmp(t
->tt
, q
->tt
);
357 ret
= strcmp(t
->offset
, q
->offset
);
360 if ((ret
= strcmp(t
->ptr
, q
->ptr
)) != 0 ||
361 (ret
= tlist_cmp_name(t
->ptr
, q
->ptr
)) != 0)
364 q
= ASN1_TAILQ_NEXT(q
, members
);
366 if (q
!= NULL
) return -1;
372 tlist_find_dup(const struct tlist
*tl
)
376 ASN1_TAILQ_FOREACH(ql
, &tlistmaster
, tmembers
) {
377 if (tlist_cmp(ql
, tl
) == 0) {
390 static struct template *
391 add_line(struct templatehead
*t
, const char *fmt
, ...)
393 struct template *q
= calloc(1, sizeof(*q
));
396 if (vasprintf(&q
->line
, fmt
, ap
) < 0 || q
->line
== NULL
)
399 ASN1_TAILQ_INSERT_TAIL(t
, q
, members
);
404 add_line_pointer(struct templatehead
*t
,
415 if (vasprintf(&tt
, ttfmt
, ap
) < 0 || tt
== NULL
)
419 q
= add_line(t
, "{ %s, %s, asn1_%s }", tt
, offset
, ptr
);
421 q
->offset
= strdup(offset
);
422 q
->ptr
= strdup(ptr
);
426 use_extern(const Symbol
*s
)
434 is_struct(Type
*t
, int isstruct
)
438 if (t
->type
== TType
)
440 if (t
->type
== TSequence
|| t
->type
== TSet
|| t
->type
== TChoice
)
443 return is_struct(t
->subtype
, isstruct
);
445 for (i
= 0; i
< sizeof(types
)/sizeof(types
[0]); i
++) {
446 if (t
->type
== types
[i
].type
) {
447 if (types
[i
].is_struct
== 0)
458 compact_tag(const Type
*t
)
460 while (t
->type
== TTag
)
466 template_members(struct templatehead
*temp
, const char *basetype
, const char *name
, const Type
*t
, int optional
, int isstruct
, int need_offset
)
468 char *poffset
= NULL
;
470 if (optional
&& t
->type
!= TTag
&& t
->type
!= TType
)
471 errx(1, "%s...%s is optional and not a (TTag or TType)", basetype
, name
);
473 poffset
= partial_offset(basetype
, name
, need_offset
);
477 if (use_extern(t
->symbol
)) {
478 add_line(temp
, "{ A1_OP_TYPE_EXTERN %s, %s, &asn1_extern_%s}",
479 optional
? "|A1_FLAG_OPTIONAL" : "",
480 poffset
, t
->symbol
->gen_name
);
482 add_line_pointer(temp
, t
->symbol
->gen_name
, poffset
,
483 "A1_OP_TYPE %s", optional
? "|A1_FLAG_OPTIONAL" : "");
491 else if (t
->range
== NULL
)
492 itype
= "HEIM_INTEGER";
493 else if (t
->range
->min
== INT_MIN
&& t
->range
->max
== INT_MAX
)
495 else if (t
->range
->min
== 0 && t
->range
->max
== UINT_MAX
)
497 else if (t
->range
->min
== 0 && t
->range
->max
== INT_MAX
)
500 errx(1, "%s: unsupported range %d -> %d",
501 name
, t
->range
->min
, t
->range
->max
);
503 add_line(temp
, "{ A1_PARSE_T(A1T_%s), %s, NULL }", itype
, poffset
);
507 add_line(temp
, "{ A1_PARSE_T(A1T_GENERAL_STRING), %s, NULL }", poffset
);
510 add_line(temp
, "{ A1_PARSE_T(A1T_TELETEX_STRING), %s, NULL }", poffset
);
512 case TPrintableString
:
513 add_line(temp
, "{ A1_PARSE_T(A1T_PRINTABLE_STRING), %s, NULL }", poffset
);
516 add_line(temp
, "{ A1_PARSE_T(A1T_OCTET_STRING), %s, NULL }", poffset
);
519 add_line(temp
, "{ A1_PARSE_T(A1T_IA5_STRING), %s, NULL }", poffset
);
522 add_line(temp
, "{ A1_PARSE_T(A1T_BMP_STRING), %s, NULL }", poffset
);
524 case TUniversalString
:
525 add_line(temp
, "{ A1_PARSE_T(A1T_UNIVERSAL_STRING), %s, NULL }", poffset
);
528 add_line(temp
, "{ A1_PARSE_T(A1T_VISIBLE_STRING), %s, NULL }", poffset
);
531 add_line(temp
, "{ A1_PARSE_T(A1T_UTF8_STRING), %s, NULL }", poffset
);
533 case TGeneralizedTime
:
534 add_line(temp
, "{ A1_PARSE_T(A1T_GENERALIZED_TIME), %s, NULL }", poffset
);
537 add_line(temp
, "{ A1_PARSE_T(A1T_UTC_TIME), %s, NULL }", poffset
);
540 add_line(temp
, "{ A1_PARSE_T(A1T_BOOLEAN), %s, NULL }", poffset
);
543 add_line(temp
, "{ A1_PARSE_T(A1T_OID), %s, NULL }", poffset
);
548 struct templatehead
template = ASN1_TAILQ_HEAD_INITIALIZER(template);
553 FILE *f
= get_code_file();
555 if (ASN1_TAILQ_EMPTY(t
->members
)) {
556 add_line(temp
, "{ A1_PARSE_T(A1T_HEIM_BIT_STRING), %s, NULL }", poffset
);
560 if (asprintf(&bname
, "bmember_%s_%p", name
? name
: "", t
) < 0 || bname
== NULL
)
564 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
565 add_line(&template, "{ 0, %d, 0 } /* %s */", m
->val
, m
->gen_name
);
568 ASN1_TAILQ_FOREACH(q
, &template, members
) {
572 fprintf(f
, "static const struct asn1_template asn1_%s_%s[] = {\n", basetype
, bname
);
573 fprintf(f
, "/* 0 */ { 0%s, sizeof(%s), ((void *)%lu) },\n",
574 rfc1510_bitstring
? "|A1_HBF_RFC1510" : "",
575 basetype
, (unsigned long)count
);
577 ASN1_TAILQ_FOREACH(q
, &template, members
) {
578 int last
= (ASN1_TAILQ_LAST(&template, templatehead
) == q
);
579 fprintf(f
, "/* %lu */ %s%s\n", (unsigned long)i
++, q
->line
, last
? "" : ",");
583 add_line(temp
, "{ A1_OP_BMEMBER, %s, asn1_%s_%s }", poffset
, basetype
, bname
);
592 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
593 char *newbasename
= NULL
;
599 if (asprintf(&newbasename
, "%s_%s", basetype
, name
) < 0)
602 newbasename
= strdup(basetype
);
603 if (newbasename
== NULL
)
606 template_members(temp
, newbasename
, m
->gen_name
, m
->type
, m
->optional
, isstruct
, 1);
614 char *tname
= NULL
, *elname
= NULL
;
615 const char *sename
, *dupname
;
616 int subtype_is_struct
= is_struct(t
->subtype
, isstruct
);
618 if (subtype_is_struct
)
621 sename
= symbol_name(basetype
, t
->subtype
);
623 if (asprintf(&tname
, "tag_%s_%p", name
? name
: "", t
) < 0 || tname
== NULL
)
627 if (asprintf(&elname
, "%s_%s", basetype
, tname
) < 0 || elname
== NULL
)
630 generate_template_type(elname
, &dupname
, NULL
, sename
, name
,
631 t
->subtype
, 0, subtype_is_struct
, 0);
633 add_line_pointer(temp
, dupname
, poffset
,
634 "A1_TAG_T(%s,%s,%s)%s",
635 classname(t
->tag
.tagclass
),
636 is_primitive_type(t
->subtype
->type
) ? "PRIM" : "CONS",
637 valuename(t
->tag
.tagclass
, t
->tag
.tagvalue
),
638 optional
? "|A1_FLAG_OPTIONAL" : "");
647 const char *type
= NULL
, *tname
, *dupname
;
648 char *sename
= NULL
, *elname
= NULL
;
649 int subtype_is_struct
= is_struct(t
->subtype
, 0);
651 if (name
&& subtype_is_struct
) {
652 tname
= "seofTstruct";
653 if (asprintf(&sename
, "%s_%s_val", basetype
, name
) < 0)
655 } else if (subtype_is_struct
) {
656 tname
= "seofTstruct";
657 if (asprintf(&sename
, "%s_val", symbol_name(basetype
, t
->subtype
)) < 0)
663 tname
= "seofTstruct";
664 sename
= strdup(symbol_name(basetype
, t
->subtype
));
669 if (t
->type
== TSetOf
) type
= "A1_OP_SETOF";
670 else if (t
->type
== TSequenceOf
) type
= "A1_OP_SEQOF";
673 if (asprintf(&elname
, "%s_%s_%p", basetype
, tname
, t
) < 0 || elname
== NULL
)
676 generate_template_type(elname
, &dupname
, NULL
, sename
, NULL
, t
->subtype
,
677 0, subtype_is_struct
, need_offset
);
679 add_line(temp
, "{ %s, %s, asn1_%s }", type
, poffset
, dupname
);
684 struct templatehead
template = ASN1_TAILQ_HEAD_INITIALIZER(template);
688 FILE *f
= get_code_file();
693 if (asprintf(&tname
, "asn1_choice_%s_%s%x",
694 basetype
, name
? name
: "", (unsigned int)(uintptr_t)t
) < 0 || tname
== NULL
)
697 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
700 char *newbasename
= NULL
;
701 int subtype_is_struct
;
708 subtype_is_struct
= is_struct(m
->type
, 0);
710 if (asprintf(&elname
, "%s_choice_%s", basetype
, m
->gen_name
) < 0 || elname
== NULL
)
713 if (subtype_is_struct
) {
714 if (asprintf(&newbasename
, "%s_%s", basetype
, m
->gen_name
) < 0)
717 newbasename
= strdup(basetype
);
719 if (newbasename
== NULL
)
723 generate_template_type(elname
, &dupname
, NULL
,
724 symbol_name(newbasename
, m
->type
),
725 NULL
, m
->type
, 0, subtype_is_struct
, 1);
727 add_line(&template, "{ %s, offsetof(%s%s, u.%s), asn1_%s }",
728 m
->label
, isstruct
? "struct " : "",
729 basetype
, m
->gen_name
,
738 if (asprintf(&e
, "offsetof(%s%s, u.asn1_ellipsis)", isstruct
? "struct " : "", basetype
) < 0 || e
== NULL
)
742 ASN1_TAILQ_FOREACH(q
, &template, members
) {
746 fprintf(f
, "static const struct asn1_template %s[] = {\n", tname
);
747 fprintf(f
, "/* 0 */ { %s, offsetof(%s%s, element), ((void *)%lu) },\n",
748 e
? e
: "0", isstruct
? "struct " : "", basetype
, (unsigned long)count
);
750 ASN1_TAILQ_FOREACH(q
, &template, members
) {
751 int last
= (ASN1_TAILQ_LAST(&template, templatehead
) == q
);
752 fprintf(f
, "/* %lu */ %s%s\n", (unsigned long)i
++, q
->line
, last
? "" : ",");
756 add_line(temp
, "{ A1_OP_CHOICE, %s, %s }", poffset
, tname
);
770 gen_extern_stubs(FILE *f
, const char *name
)
773 "static const struct asn1_type_func asn1_extern_%s = {\n"
774 "\t(asn1_type_encode)encode_%s,\n"
775 "\t(asn1_type_decode)decode_%s,\n"
776 "\t(asn1_type_length)length_%s,\n"
777 "\t(asn1_type_copy)copy_%s,\n"
778 "\t(asn1_type_release)free_%s,\n"
781 name
, name
, name
, name
,
786 gen_template_import(const Symbol
*s
)
788 FILE *f
= get_code_file();
790 if (template_flag
== 0)
793 gen_extern_stubs(f
, s
->gen_name
);
797 generate_template_type(const char *varname
,
798 const char **dupname
,
800 const char *basetype
,
803 int optional
, int isstruct
, int need_offset
)
807 int have_ellipsis
= 0;
809 tl
= tlist_new(varname
);
811 template_members(&tl
->template, basetype
, name
, type
, optional
, isstruct
, need_offset
);
813 /* if its a sequence or set type, check if there is a ellipsis */
814 if (type
->type
== TSequence
|| type
->type
== TSet
) {
816 ASN1_TAILQ_FOREACH(m
, type
->members
, members
) {
822 if (ASN1_TAILQ_EMPTY(&tl
->template) && compact_tag(type
)->type
!= TNull
)
823 errx(1, "Tag %s...%s with no content ?", basetype
, name
? name
: "");
825 tlist_header(tl
, "{ 0%s%s, sizeof(%s%s), ((void *)%lu) }",
826 (symname
&& preserve_type(symname
)) ? "|A1_HF_PRESERVE" : "",
827 have_ellipsis
? "|A1_HF_ELLIPSIS" : "",
828 isstruct
? "struct " : "", basetype
, tlist_count(tl
));
830 dup
= tlist_find_dup(tl
);
832 if (strcmp(dup
, tl
->name
) == 0)
833 errx(1, "found dup of ourself");
844 generate_template(const Symbol
*s
)
846 FILE *f
= get_code_file();
850 gen_extern_stubs(f
, s
->gen_name
);
854 generate_template_type(s
->gen_name
, &dupname
, s
->name
, s
->gen_name
, NULL
, s
->type
, 0, 0, 1);
859 "decode_%s(const unsigned char *p, size_t len, %s *data, size_t *size)\n"
861 " return _asn1_decode_top(asn1_%s, 0|%s, p, len, data, size);\n"
867 support_ber
? "A1_PF_ALLOW_BER" : "0");
872 "encode_%s(unsigned char *p, size_t len, const %s *data, size_t *size)\n"
874 " return _asn1_encode(asn1_%s, p, len, data, size);\n"
884 "length_%s(const %s *data)\n"
886 " return _asn1_length(asn1_%s, data);\n"
897 "free_%s(%s *data)\n"
899 " _asn1_free(asn1_%s, data);\n"
909 "copy_%s(const %s *from, %s *to)\n"
911 " return _asn1_copy_top(asn1_%s, from, to);\n"