1 /* $NetBSD: gen_template.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
4 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 static const char *symbol_name(const char *, const Type
*);
41 static void generate_template_type(const char *, const char **, const char *, const char *, const char *,
42 Type
*, int, int, int);
45 ttype_symbol(const char *basename
, const Type
*t
)
47 return t
->symbol
->gen_name
;
51 integer_symbol(const char *basename
, const Type
*t
)
54 return "int"; /* XXX enum foo */
55 else if (t
->range
== NULL
)
56 return "heim_integer";
57 else if (t
->range
->min
== INT_MIN
&& t
->range
->max
== INT_MAX
)
59 else if (t
->range
->min
== 0 && t
->range
->max
== UINT_MAX
)
61 else if (t
->range
->min
== 0 && t
->range
->max
== INT_MAX
)
65 UNREACHABLE(return NULL
);
70 boolean_symbol(const char *basename
, const Type
*t
)
77 octetstring_symbol(const char *basename
, const Type
*t
)
79 return "heim_octet_string";
83 sequence_symbol(const char *basename
, const Type
*t
)
89 time_symbol(const char *basename
, const Type
*t
)
95 tag_symbol(const char *basename
, const Type
*t
)
97 return symbol_name(basename
, t
->subtype
);
101 generalstring_symbol(const char *basename
, const Type
*t
)
103 return "heim_general_string";
107 printablestring_symbol(const char *basename
, const Type
*t
)
109 return "heim_printable_string";
113 ia5string_symbol(const char *basename
, const Type
*t
)
115 return "heim_ia5_string";
119 visiblestring_symbol(const char *basename
, const Type
*t
)
121 return "heim_visible_string";
125 utf8string_symbol(const char *basename
, const Type
*t
)
127 return "heim_utf8_string";
131 bmpstring_symbol(const char *basename
, const Type
*t
)
133 return "heim_bmp_string";
137 universalstring_symbol(const char *basename
, const Type
*t
)
139 return "heim_universal_string";
143 oid_symbol(const char *basename
, const Type
*t
)
149 bitstring_symbol(const char *basename
, const Type
*t
)
153 return "heim_bit_string";
160 const char *(*symbol_name
)(const char *, const Type
*);
163 { TBMPString
, bmpstring_symbol
, 0 },
164 { TBitString
, bitstring_symbol
, 0 },
165 { TBoolean
, boolean_symbol
, 0 },
166 { TGeneralString
, generalstring_symbol
, 0 },
167 { TGeneralizedTime
, time_symbol
, 0 },
168 { TIA5String
, ia5string_symbol
, 0 },
169 { TInteger
, integer_symbol
, 0 },
170 { TOID
, oid_symbol
, 0 },
171 { TOctetString
, octetstring_symbol
, 0 },
172 { TPrintableString
, printablestring_symbol
, 0 },
173 { TSequence
, sequence_symbol
, 1 },
174 { TSequenceOf
, tag_symbol
, 1 },
175 { TSetOf
, tag_symbol
, 1 },
176 { TTag
, tag_symbol
, 1 },
177 { TType
, ttype_symbol
, 1 },
178 { TUTCTime
, time_symbol
, 0 },
179 { TUniversalString
, universalstring_symbol
, 0 },
180 { TVisibleString
, visiblestring_symbol
, 0 },
181 { TUTF8String
, utf8string_symbol
, 0 },
182 { TChoice
, sequence_symbol
, 1 },
183 { TNull
, integer_symbol
, 1 }
196 is_supported_type_p(const Type
*t
)
200 for (i
= 0; i
< sizeof(types
)/sizeof(types
[0]); i
++)
201 if (t
->type
== types
[i
].type
)
207 is_template_compat (const Symbol
*s
)
209 return is_supported_type_p(s
->type
);
213 symbol_name(const char *basename
, const Type
*t
)
217 for (i
= 0; i
< sizeof(types
)/sizeof(types
[0]); i
++)
218 if (t
->type
== types
[i
].type
)
219 return (types
[i
].symbol_name
)(basename
, t
);
220 printf("unknown der type: %d\n", t
->type
);
226 partial_offset(const char *basetype
, const char *name
, int need_offset
)
229 if (name
== NULL
|| need_offset
== 0)
231 if (asprintf(&str
, "offsetof(struct %s, %s)", basetype
, name
) < 0 || str
== NULL
)
241 ASN1_TAILQ_ENTRY(template) members
;
244 ASN1_TAILQ_HEAD(templatehead
, template);
249 struct templatehead
template;
250 ASN1_TAILQ_ENTRY(tlist
) tmembers
;
253 ASN1_TAILQ_HEAD(tlisthead
, tlist
);
255 static void tlist_header(struct tlist
*, const char *, ...) __attribute__((__format__(__printf__
, 2, 3)));
256 static struct template *
257 add_line(struct templatehead
*, const char *, ...) __attribute__((__format__(__printf__
, 2, 3)));
258 static int tlist_cmp(const struct tlist
*, const struct tlist
*);
260 static void add_line_pointer(struct templatehead
*, const char *, const char *, const char *, ...)
261 __attribute__((__format__(__printf__
, 4, 5)));
264 static struct tlisthead tlistmaster
= ASN1_TAILQ_HEAD_INITIALIZER(tlistmaster
);
265 static unsigned long numdups
= 0;
267 static struct tlist
*
268 tlist_new(const char *name
)
270 struct tlist
*tl
= calloc(1, sizeof(*tl
));
271 tl
->name
= strdup(name
);
272 ASN1_TAILQ_INIT(&tl
->template);
277 tlist_header(struct tlist
*t
, const char *fmt
, ...)
281 if (vasprintf(&t
->header
, fmt
, ap
) < 0 || t
->header
== NULL
)
287 tlist_count(struct tlist
*tl
)
289 unsigned int count
= 0;
292 ASN1_TAILQ_FOREACH(q
, &tl
->template, members
) {
299 tlist_add(struct tlist
*tl
)
301 ASN1_TAILQ_INSERT_TAIL(&tlistmaster
, tl
, tmembers
);
305 tlist_print(struct tlist
*tl
)
309 FILE *f
= get_code_file();
311 fprintf(f
, "static const struct asn1_template asn1_%s[] = {\n", tl
->name
);
312 fprintf(f
, "/* 0 */ %s,\n", tl
->header
);
313 ASN1_TAILQ_FOREACH(q
, &tl
->template, members
) {
314 int last
= (ASN1_TAILQ_LAST(&tl
->template, templatehead
) == q
);
315 fprintf(f
, "/* %lu */ %s%s\n", (unsigned long)i
++, q
->line
, last
? "" : ",");
320 static struct tlist
*
321 tlist_find_by_name(const char *name
)
324 ASN1_TAILQ_FOREACH(ql
, &tlistmaster
, tmembers
) {
325 if (strcmp(ql
->name
, name
) == 0)
332 tlist_cmp_name(const char *tname
, const char *qname
)
334 struct tlist
*tl
= tlist_find_by_name(tname
);
335 struct tlist
*ql
= tlist_find_by_name(qname
);
336 return tlist_cmp(tl
, ql
);
340 tlist_cmp(const struct tlist
*tl
, const struct tlist
*ql
)
343 struct template *t
, *q
;
345 ret
= strcmp(tl
->header
, ql
->header
);
348 q
= ASN1_TAILQ_FIRST(&ql
->template);
349 ASN1_TAILQ_FOREACH(t
, &tl
->template, members
) {
350 if (q
== NULL
) return 1;
352 if (t
->ptr
== NULL
|| q
->ptr
== NULL
) {
353 ret
= strcmp(t
->line
, q
->line
);
356 ret
= strcmp(t
->tt
, q
->tt
);
359 ret
= strcmp(t
->offset
, q
->offset
);
362 if ((ret
= strcmp(t
->ptr
, q
->ptr
)) != 0 ||
363 (ret
= tlist_cmp_name(t
->ptr
, q
->ptr
)) != 0)
366 q
= ASN1_TAILQ_NEXT(q
, members
);
368 if (q
!= NULL
) return -1;
374 tlist_find_dup(const struct tlist
*tl
)
378 ASN1_TAILQ_FOREACH(ql
, &tlistmaster
, tmembers
) {
379 if (tlist_cmp(ql
, tl
) == 0) {
392 static struct template *
393 add_line(struct templatehead
*t
, const char *fmt
, ...)
395 struct template *q
= calloc(1, sizeof(*q
));
398 if (vasprintf(&q
->line
, fmt
, ap
) < 0 || q
->line
== NULL
)
401 ASN1_TAILQ_INSERT_TAIL(t
, q
, members
);
406 add_line_pointer(struct templatehead
*t
,
417 if (vasprintf(&tt
, ttfmt
, ap
) < 0 || tt
== NULL
)
421 q
= add_line(t
, "{ %s, %s, asn1_%s }", tt
, offset
, ptr
);
423 q
->offset
= strdup(offset
);
424 q
->ptr
= strdup(ptr
);
428 use_extern(const Symbol
*s
)
436 is_struct(Type
*t
, int isstruct
)
440 if (t
->type
== TType
)
442 if (t
->type
== TSequence
|| t
->type
== TSet
|| t
->type
== TChoice
)
445 return is_struct(t
->subtype
, isstruct
);
447 for (i
= 0; i
< sizeof(types
)/sizeof(types
[0]); i
++) {
448 if (t
->type
== types
[i
].type
) {
449 if (types
[i
].is_struct
== 0)
460 compact_tag(const Type
*t
)
462 while (t
->type
== TTag
)
468 template_members(struct templatehead
*temp
, const char *basetype
, const char *name
, const Type
*t
, int optional
, int isstruct
, int need_offset
)
470 char *poffset
= NULL
;
472 if (optional
&& t
->type
!= TTag
&& t
->type
!= TType
)
473 errx(1, "%s...%s is optional and not a (TTag or TType)", basetype
, name
);
475 poffset
= partial_offset(basetype
, name
, need_offset
);
479 if (use_extern(t
->symbol
)) {
480 add_line(temp
, "{ A1_OP_TYPE_EXTERN %s, %s, &asn1_extern_%s}",
481 optional
? "|A1_FLAG_OPTIONAL" : "",
482 poffset
, t
->symbol
->gen_name
);
484 add_line_pointer(temp
, t
->symbol
->gen_name
, poffset
,
485 "A1_OP_TYPE %s", optional
? "|A1_FLAG_OPTIONAL" : "");
493 else if (t
->range
== NULL
)
494 itype
= "HEIM_INTEGER";
495 else if (t
->range
->min
== INT_MIN
&& t
->range
->max
== INT_MAX
)
497 else if (t
->range
->min
== 0 && t
->range
->max
== UINT_MAX
)
499 else if (t
->range
->min
== 0 && t
->range
->max
== INT_MAX
)
502 errx(1, "%s: unsupported range %d -> %d",
503 name
, t
->range
->min
, t
->range
->max
);
505 add_line(temp
, "{ A1_PARSE_T(A1T_%s), %s, NULL }", itype
, poffset
);
509 add_line(temp
, "{ A1_PARSE_T(A1T_GENERAL_STRING), %s, NULL }", poffset
);
512 add_line(temp
, "{ A1_PARSE_T(A1T_TELETEX_STRING), %s, NULL }", poffset
);
514 case TPrintableString
:
515 add_line(temp
, "{ A1_PARSE_T(A1T_PRINTABLE_STRING), %s, NULL }", poffset
);
518 add_line(temp
, "{ A1_PARSE_T(A1T_OCTET_STRING), %s, NULL }", poffset
);
521 add_line(temp
, "{ A1_PARSE_T(A1T_IA5_STRING), %s, NULL }", poffset
);
524 add_line(temp
, "{ A1_PARSE_T(A1T_BMP_STRING), %s, NULL }", poffset
);
526 case TUniversalString
:
527 add_line(temp
, "{ A1_PARSE_T(A1T_UNIVERSAL_STRING), %s, NULL }", poffset
);
530 add_line(temp
, "{ A1_PARSE_T(A1T_VISIBLE_STRING), %s, NULL }", poffset
);
533 add_line(temp
, "{ A1_PARSE_T(A1T_UTF8_STRING), %s, NULL }", poffset
);
535 case TGeneralizedTime
:
536 add_line(temp
, "{ A1_PARSE_T(A1T_GENERALIZED_TIME), %s, NULL }", poffset
);
539 add_line(temp
, "{ A1_PARSE_T(A1T_UTC_TIME), %s, NULL }", poffset
);
542 add_line(temp
, "{ A1_PARSE_T(A1T_BOOLEAN), %s, NULL }", poffset
);
545 add_line(temp
, "{ A1_PARSE_T(A1T_OID), %s, NULL }", poffset
);
550 struct templatehead
template = ASN1_TAILQ_HEAD_INITIALIZER(template);
555 FILE *f
= get_code_file();
557 if (ASN1_TAILQ_EMPTY(t
->members
)) {
558 add_line(temp
, "{ A1_PARSE_T(A1T_HEIM_BIT_STRING), %s, NULL }", poffset
);
562 if (asprintf(&bname
, "bmember_%s_%p", name
? name
: "", t
) < 0 || bname
== NULL
)
566 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
567 add_line(&template, "{ 0, %d, 0 } /* %s */", m
->val
, m
->gen_name
);
570 ASN1_TAILQ_FOREACH(q
, &template, members
) {
574 fprintf(f
, "static const struct asn1_template asn1_%s_%s[] = {\n", basetype
, bname
);
575 fprintf(f
, "/* 0 */ { 0%s, sizeof(%s), ((void *)%lu) },\n",
576 rfc1510_bitstring
? "|A1_HBF_RFC1510" : "",
577 basetype
, (unsigned long)count
);
579 ASN1_TAILQ_FOREACH(q
, &template, members
) {
580 int last
= (ASN1_TAILQ_LAST(&template, templatehead
) == q
);
581 fprintf(f
, "/* %lu */ %s%s\n", (unsigned long)i
++, q
->line
, last
? "" : ",");
585 add_line(temp
, "{ A1_OP_BMEMBER, %s, asn1_%s_%s }", poffset
, basetype
, bname
);
594 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
595 char *newbasename
= NULL
;
601 if (asprintf(&newbasename
, "%s_%s", basetype
, name
) < 0)
604 newbasename
= strdup(basetype
);
605 if (newbasename
== NULL
)
608 template_members(temp
, newbasename
, m
->gen_name
, m
->type
, m
->optional
, isstruct
, 1);
616 char *tname
= NULL
, *elname
= NULL
;
617 const char *sename
, *dupname
;
618 int subtype_is_struct
= is_struct(t
->subtype
, isstruct
);
620 if (subtype_is_struct
)
623 sename
= symbol_name(basetype
, t
->subtype
);
625 if (asprintf(&tname
, "tag_%s_%p", name
? name
: "", t
) < 0 || tname
== NULL
)
629 if (asprintf(&elname
, "%s_%s", basetype
, tname
) < 0 || elname
== NULL
)
632 generate_template_type(elname
, &dupname
, NULL
, sename
, name
,
633 t
->subtype
, 0, subtype_is_struct
, 0);
635 add_line_pointer(temp
, dupname
, poffset
,
636 "A1_TAG_T(%s,%s,%s)%s",
637 classname(t
->tag
.tagclass
),
638 is_primitive_type(t
->subtype
->type
) ? "PRIM" : "CONS",
639 valuename(t
->tag
.tagclass
, t
->tag
.tagvalue
),
640 optional
? "|A1_FLAG_OPTIONAL" : "");
649 const char *type
= NULL
, *tname
, *dupname
;
650 char *sename
= NULL
, *elname
= NULL
;
651 int subtype_is_struct
= is_struct(t
->subtype
, 0);
653 if (name
&& subtype_is_struct
) {
654 tname
= "seofTstruct";
655 if (asprintf(&sename
, "%s_%s_val", basetype
, name
) < 0)
657 } else if (subtype_is_struct
) {
658 tname
= "seofTstruct";
659 if (asprintf(&sename
, "%s_val", symbol_name(basetype
, t
->subtype
)) < 0)
665 tname
= "seofTstruct";
666 sename
= strdup(symbol_name(basetype
, t
->subtype
));
671 if (t
->type
== TSetOf
) type
= "A1_OP_SETOF";
672 else if (t
->type
== TSequenceOf
) type
= "A1_OP_SEQOF";
675 if (asprintf(&elname
, "%s_%s_%p", basetype
, tname
, t
) < 0 || elname
== NULL
)
678 generate_template_type(elname
, &dupname
, NULL
, sename
, NULL
, t
->subtype
,
679 0, subtype_is_struct
, need_offset
);
681 add_line(temp
, "{ %s, %s, asn1_%s }", type
, poffset
, dupname
);
686 struct templatehead
template = ASN1_TAILQ_HEAD_INITIALIZER(template);
690 FILE *f
= get_code_file();
695 if (asprintf(&tname
, "asn1_choice_%s_%s%x",
696 basetype
, name
? name
: "", (unsigned int)(uintptr_t)t
) < 0 || tname
== NULL
)
699 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
702 char *newbasename
= NULL
;
703 int subtype_is_struct
;
710 subtype_is_struct
= is_struct(m
->type
, 0);
712 if (asprintf(&elname
, "%s_choice_%s", basetype
, m
->gen_name
) < 0 || elname
== NULL
)
715 if (subtype_is_struct
) {
716 if (asprintf(&newbasename
, "%s_%s", basetype
, m
->gen_name
) < 0)
719 newbasename
= strdup(basetype
);
721 if (newbasename
== NULL
)
725 generate_template_type(elname
, &dupname
, NULL
,
726 symbol_name(newbasename
, m
->type
),
727 NULL
, m
->type
, 0, subtype_is_struct
, 1);
729 add_line(&template, "{ %s, offsetof(%s%s, u.%s), asn1_%s }",
730 m
->label
, isstruct
? "struct " : "",
731 basetype
, m
->gen_name
,
740 if (asprintf(&e
, "offsetof(%s%s, u.asn1_ellipsis)", isstruct
? "struct " : "", basetype
) < 0 || e
== NULL
)
744 ASN1_TAILQ_FOREACH(q
, &template, members
) {
748 fprintf(f
, "static const struct asn1_template %s[] = {\n", tname
);
749 fprintf(f
, "/* 0 */ { %s, offsetof(%s%s, element), ((void *)%lu) },\n",
750 e
? e
: "0", isstruct
? "struct " : "", basetype
, (unsigned long)count
);
752 ASN1_TAILQ_FOREACH(q
, &template, members
) {
753 int last
= (ASN1_TAILQ_LAST(&template, templatehead
) == q
);
754 fprintf(f
, "/* %lu */ %s%s\n", (unsigned long)i
++, q
->line
, last
? "" : ",");
758 add_line(temp
, "{ A1_OP_CHOICE, %s, %s }", poffset
, tname
);
772 gen_extern_stubs(FILE *f
, const char *name
)
775 "static const struct asn1_type_func asn1_extern_%s = {\n"
776 "\t(asn1_type_encode)encode_%s,\n"
777 "\t(asn1_type_decode)decode_%s,\n"
778 "\t(asn1_type_length)length_%s,\n"
779 "\t(asn1_type_copy)copy_%s,\n"
780 "\t(asn1_type_release)free_%s,\n"
783 name
, name
, name
, name
,
788 gen_template_import(const Symbol
*s
)
790 FILE *f
= get_code_file();
792 if (template_flag
== 0)
795 gen_extern_stubs(f
, s
->gen_name
);
799 generate_template_type(const char *varname
,
800 const char **dupname
,
802 const char *basetype
,
805 int optional
, int isstruct
, int need_offset
)
809 int have_ellipsis
= 0;
811 tl
= tlist_new(varname
);
813 template_members(&tl
->template, basetype
, name
, type
, optional
, isstruct
, need_offset
);
815 /* if its a sequence or set type, check if there is a ellipsis */
816 if (type
->type
== TSequence
|| type
->type
== TSet
) {
818 ASN1_TAILQ_FOREACH(m
, type
->members
, members
) {
824 if (ASN1_TAILQ_EMPTY(&tl
->template) && compact_tag(type
)->type
!= TNull
)
825 errx(1, "Tag %s...%s with no content ?", basetype
, name
? name
: "");
827 tlist_header(tl
, "{ 0%s%s, sizeof(%s%s), ((void *)%lu) }",
828 (symname
&& preserve_type(symname
)) ? "|A1_HF_PRESERVE" : "",
829 have_ellipsis
? "|A1_HF_ELLIPSIS" : "",
830 isstruct
? "struct " : "", basetype
, tlist_count(tl
));
832 dup
= tlist_find_dup(tl
);
834 if (strcmp(dup
, tl
->name
) == 0)
835 errx(1, "found dup of ourself");
846 generate_template(const Symbol
*s
)
848 FILE *f
= get_code_file();
852 gen_extern_stubs(f
, s
->gen_name
);
856 generate_template_type(s
->gen_name
, &dupname
, s
->name
, s
->gen_name
, NULL
, s
->type
, 0, 0, 1);
861 "decode_%s(const unsigned char *p, size_t len, %s *data, size_t *size)\n"
863 " return _asn1_decode_top(asn1_%s, 0|%s, p, len, data, size);\n"
869 support_ber
? "A1_PF_ALLOW_BER" : "0");
874 "encode_%s(unsigned char *p, size_t len, const %s *data, size_t *size)\n"
876 " return _asn1_encode(asn1_%s, p, len, data, size);\n"
886 "length_%s(const %s *data)\n"
888 " return _asn1_length(asn1_%s, data);\n"
899 "free_%s(%s *data)\n"
901 " _asn1_free(asn1_%s, data);\n"
911 "copy_%s(const %s *from, %s *to)\n"
913 " return _asn1_copy_top(asn1_%s, from, to);\n"