xref: /freebsd/crypto/heimdal/lib/asn1/gen_template.c (revision cd0d51baaa4509a1db83251a601d34404d20c990)
1 /*
2  * Copyright (c) 1997 - 2005 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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
33  * SUCH DAMAGE.
34  */
35 
36 #include "gen_locl.h"
37 
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);
41 
42 static const char *
43 ttype_symbol(const char *basename, const Type *t)
44 {
45     return t->symbol->gen_name;
46 }
47 
48 static const char *
49 integer_symbol(const char *basename, const Type *t)
50 {
51     if (t->members)
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)
56 	return "int";
57     else if (t->range->min == 0 && t->range->max == UINT_MAX)
58 	return "unsigned";
59     else if (t->range->min == 0 && t->range->max == INT_MAX)
60 	return "unsigned";
61     else {
62 	abort();
63         UNREACHABLE(return NULL);
64     }
65 }
66 
67 static const char *
68 boolean_symbol(const char *basename, const Type *t)
69 {
70     return "int";
71 }
72 
73 
74 static const char *
75 octetstring_symbol(const char *basename, const Type *t)
76 {
77     return "heim_octet_string";
78 }
79 
80 static const char *
81 sequence_symbol(const char *basename, const Type *t)
82 {
83     return basename;
84 }
85 
86 static const char *
87 time_symbol(const char *basename, const Type *t)
88 {
89     return "time_t";
90 }
91 
92 static const char *
93 tag_symbol(const char *basename, const Type *t)
94 {
95     return symbol_name(basename, t->subtype);
96 }
97 
98 static const char *
99 generalstring_symbol(const char *basename, const Type *t)
100 {
101     return "heim_general_string";
102 }
103 
104 static const char *
105 printablestring_symbol(const char *basename, const Type *t)
106 {
107     return "heim_printable_string";
108 }
109 
110 static const char *
111 ia5string_symbol(const char *basename, const Type *t)
112 {
113     return "heim_ia5_string";
114 }
115 
116 static const char *
117 visiblestring_symbol(const char *basename, const Type *t)
118 {
119     return "heim_visible_string";
120 }
121 
122 static const char *
123 utf8string_symbol(const char *basename, const Type *t)
124 {
125     return "heim_utf8_string";
126 }
127 
128 static const char *
129 bmpstring_symbol(const char *basename, const Type *t)
130 {
131     return "heim_bmp_string";
132 }
133 
134 static const char *
135 universalstring_symbol(const char *basename, const Type *t)
136 {
137     return "heim_universal_string";
138 }
139 
140 static const char *
141 oid_symbol(const char *basename, const Type *t)
142 {
143     return "heim_oid";
144 }
145 
146 static const char *
147 bitstring_symbol(const char *basename, const Type *t)
148 {
149     if (t->members)
150 	return basename;
151     return "heim_bit_string";
152 }
153 
154 
155 
156 struct {
157     enum typetype type;
158     const char *(*symbol_name)(const char *, const Type *);
159     int is_struct;
160 } types[] =  {
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 }
182 };
183 
184 static FILE *
185 get_code_file(void)
186 {
187     if (!one_code_file)
188 	return templatefile;
189     return codefile;
190 }
191 
192 
193 static int
194 is_supported_type_p(const Type *t)
195 {
196     size_t i;
197 
198     for (i = 0; i < sizeof(types)/sizeof(types[0]); i++)
199 	if (t->type == types[i].type)
200 	    return 1;
201     return 0;
202 }
203 
204 int
205 is_template_compat (const Symbol *s)
206 {
207     return is_supported_type_p(s->type);
208 }
209 
210 static const char *
211 symbol_name(const char *basename, const Type *t)
212 {
213     size_t i;
214 
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);
219     exit(1);
220 }
221 
222 
223 static char *
224 partial_offset(const char *basetype, const char *name, int need_offset)
225 {
226     char *str;
227     if (name == NULL || need_offset == 0)
228 	return strdup("0");
229     if (asprintf(&str, "offsetof(struct %s, %s)", basetype, name) < 0 || str == NULL)
230 	errx(1, "malloc");
231     return str;
232 }
233 
234 struct template {
235     char *line;
236     char *tt;
237     char *offset;
238     char *ptr;
239     ASN1_TAILQ_ENTRY(template) members;
240 };
241 
242 ASN1_TAILQ_HEAD(templatehead, template);
243 
244 struct tlist {
245     char *name;
246     char *header;
247     struct templatehead template;
248     ASN1_TAILQ_ENTRY(tlist) tmembers;
249 };
250 
251 ASN1_TAILQ_HEAD(tlisthead, tlist);
252 
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 *);
257 
258 static void add_line_pointer(struct templatehead *, const char *, const char *, const char *, ...)
259     __attribute__((__format__(__printf__, 4, 5)));
260 
261 
262 static struct tlisthead tlistmaster = ASN1_TAILQ_HEAD_INITIALIZER(tlistmaster);
263 static unsigned long numdups = 0;
264 
265 static struct tlist *
266 tlist_new(const char *name)
267 {
268     struct tlist *tl = calloc(1, sizeof(*tl));
269     tl->name = strdup(name);
270     ASN1_TAILQ_INIT(&tl->template);
271     return tl;
272 }
273 
274 static void
275 tlist_header(struct tlist *t, const char *fmt, ...)
276 {
277     va_list ap;
278     va_start(ap, fmt);
279     if (vasprintf(&t->header, fmt, ap) < 0 || t->header == NULL)
280 	errx(1, "malloc");
281     va_end(ap);
282 }
283 
284 static unsigned long
285 tlist_count(struct tlist *tl)
286 {
287     unsigned int count = 0;
288     struct template *q;
289 
290     ASN1_TAILQ_FOREACH(q, &tl->template, members) {
291 	count++;
292     }
293     return count;
294 }
295 
296 static void
297 tlist_add(struct tlist *tl)
298 {
299     ASN1_TAILQ_INSERT_TAIL(&tlistmaster, tl, tmembers);
300 }
301 
302 static void
303 tlist_print(struct tlist *tl)
304 {
305     struct template *q;
306     unsigned int i = 1;
307     FILE *f = get_code_file();
308 
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 ? "" : ",");
314     }
315     fprintf(f, "};\n");
316 }
317 
318 static struct tlist *
319 tlist_find_by_name(const char *name)
320 {
321     struct tlist *ql;
322     ASN1_TAILQ_FOREACH(ql, &tlistmaster, tmembers) {
323 	if (strcmp(ql->name, name) == 0)
324 	    return ql;
325     }
326     return NULL;
327 }
328 
329 static int
330 tlist_cmp_name(const char *tname, const char *qname)
331 {
332     struct tlist *tl = tlist_find_by_name(tname);
333     struct tlist *ql = tlist_find_by_name(qname);
334     return tlist_cmp(tl, ql);
335 }
336 
337 static int
338 tlist_cmp(const struct tlist *tl, const struct tlist *ql)
339 {
340     int ret;
341     struct template *t, *q;
342 
343     ret = strcmp(tl->header, ql->header);
344     if (ret) return ret;
345 
346     q = ASN1_TAILQ_FIRST(&ql->template);
347     ASN1_TAILQ_FOREACH(t, &tl->template, members) {
348 	if (q == NULL) return 1;
349 
350 	if (t->ptr == NULL || q->ptr == NULL) {
351 	    ret = strcmp(t->line, q->line);
352 	    if (ret) return ret;
353 	} else {
354 	    ret = strcmp(t->tt, q->tt);
355 	    if (ret) return ret;
356 
357 	    ret = strcmp(t->offset, q->offset);
358 	    if (ret) return ret;
359 
360 	    if ((ret = strcmp(t->ptr, q->ptr)) != 0 ||
361 		(ret = tlist_cmp_name(t->ptr, q->ptr)) != 0)
362 		return ret;
363 	}
364 	q = ASN1_TAILQ_NEXT(q, members);
365     }
366     if (q != NULL) return -1;
367     return 0;
368 }
369 
370 
371 static const char *
372 tlist_find_dup(const struct tlist *tl)
373 {
374     struct tlist *ql;
375 
376     ASN1_TAILQ_FOREACH(ql, &tlistmaster, tmembers) {
377 	if (tlist_cmp(ql, tl) == 0) {
378 	    numdups++;
379 	    return ql->name;
380 	}
381     }
382     return NULL;
383 }
384 
385 
386 /*
387  *
388  */
389 
390 static struct template *
391 add_line(struct templatehead *t, const char *fmt, ...)
392 {
393     struct template *q = calloc(1, sizeof(*q));
394     va_list ap;
395     va_start(ap, fmt);
396     if (vasprintf(&q->line, fmt, ap) < 0 || q->line == NULL)
397 	errx(1, "malloc");
398     va_end(ap);
399     ASN1_TAILQ_INSERT_TAIL(t, q, members);
400     return q;
401 }
402 
403 static void
404 add_line_pointer(struct templatehead *t,
405 		 const char *ptr,
406 		 const char *offset,
407 		 const char *ttfmt,
408 		 ...)
409 {
410     struct template *q;
411     va_list ap;
412     char *tt = NULL;
413 
414     va_start(ap, ttfmt);
415     if (vasprintf(&tt, ttfmt, ap) < 0 || tt == NULL)
416 	errx(1, "malloc");
417     va_end(ap);
418 
419     q = add_line(t, "{ %s, %s, asn1_%s }", tt, offset, ptr);
420     q->tt = tt;
421     q->offset = strdup(offset);
422     q->ptr = strdup(ptr);
423 }
424 
425 static int
426 use_extern(const Symbol *s)
427 {
428     if (s->type == NULL)
429 	return 1;
430     return 0;
431 }
432 
433 static int
434 is_struct(Type *t, int isstruct)
435 {
436     size_t i;
437 
438     if (t->type == TType)
439 	return 0;
440     if (t->type == TSequence || t->type == TSet || t->type == TChoice)
441 	return 1;
442     if (t->type == TTag)
443 	return is_struct(t->subtype, isstruct);
444 
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)
448 		return 0;
449 	    else
450 		break;
451 	}
452     }
453 
454     return isstruct;
455 }
456 
457 static const Type *
458 compact_tag(const Type *t)
459 {
460     while (t->type == TTag)
461 	t = t->subtype;
462     return t;
463 }
464 
465 static void
466 template_members(struct templatehead *temp, const char *basetype, const char *name, const Type *t, int optional, int isstruct, int need_offset)
467 {
468     char *poffset = NULL;
469 
470     if (optional && t->type != TTag && t->type != TType)
471 	errx(1, "%s...%s is optional and not a (TTag or TType)", basetype, name);
472 
473     poffset = partial_offset(basetype, name, need_offset);
474 
475     switch (t->type) {
476     case TType:
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);
481 	} else {
482 	    add_line_pointer(temp, t->symbol->gen_name, poffset,
483 			     "A1_OP_TYPE %s", optional ? "|A1_FLAG_OPTIONAL" : "");
484 	}
485 	break;
486     case TInteger: {
487 	char *itype = NULL;
488 
489 	if (t->members)
490 	    itype = "IMEMBER";
491 	else if (t->range == NULL)
492 	    itype = "HEIM_INTEGER";
493 	else if (t->range->min == INT_MIN && t->range->max == INT_MAX)
494 	    itype = "INTEGER";
495 	else if (t->range->min == 0 && t->range->max == UINT_MAX)
496 	    itype = "UNSIGNED";
497 	else if (t->range->min == 0 && t->range->max == INT_MAX)
498 	    itype = "UNSIGNED";
499 	else
500 	    errx(1, "%s: unsupported range %d -> %d",
501 		 name, t->range->min, t->range->max);
502 
503 	add_line(temp, "{ A1_PARSE_T(A1T_%s), %s, NULL }", itype, poffset);
504 	break;
505     }
506     case TGeneralString:
507 	add_line(temp, "{ A1_PARSE_T(A1T_GENERAL_STRING), %s, NULL }", poffset);
508 	break;
509     case TTeletexString:
510 	add_line(temp, "{ A1_PARSE_T(A1T_TELETEX_STRING), %s, NULL }", poffset);
511 	break;
512     case TPrintableString:
513 	add_line(temp, "{ A1_PARSE_T(A1T_PRINTABLE_STRING), %s, NULL }", poffset);
514 	break;
515     case TOctetString:
516 	add_line(temp, "{ A1_PARSE_T(A1T_OCTET_STRING), %s, NULL }", poffset);
517 	break;
518     case TIA5String:
519 	add_line(temp, "{ A1_PARSE_T(A1T_IA5_STRING), %s, NULL }", poffset);
520 	break;
521     case TBMPString:
522 	add_line(temp, "{ A1_PARSE_T(A1T_BMP_STRING), %s, NULL }", poffset);
523 	break;
524     case TUniversalString:
525 	add_line(temp, "{ A1_PARSE_T(A1T_UNIVERSAL_STRING), %s, NULL }", poffset);
526 	break;
527     case TVisibleString:
528 	add_line(temp, "{ A1_PARSE_T(A1T_VISIBLE_STRING), %s, NULL }", poffset);
529 	break;
530     case TUTF8String:
531 	add_line(temp, "{ A1_PARSE_T(A1T_UTF8_STRING), %s, NULL }", poffset);
532 	break;
533     case TGeneralizedTime:
534 	add_line(temp, "{ A1_PARSE_T(A1T_GENERALIZED_TIME), %s, NULL }", poffset);
535 	break;
536     case TUTCTime:
537 	add_line(temp, "{ A1_PARSE_T(A1T_UTC_TIME), %s, NULL }", poffset);
538 	break;
539     case TBoolean:
540 	add_line(temp, "{ A1_PARSE_T(A1T_BOOLEAN), %s, NULL }", poffset);
541 	break;
542     case TOID:
543 	add_line(temp, "{ A1_PARSE_T(A1T_OID), %s, NULL }", poffset);
544 	break;
545     case TNull:
546 	break;
547     case TBitString: {
548 	struct templatehead template = ASN1_TAILQ_HEAD_INITIALIZER(template);
549 	struct template *q;
550 	Member *m;
551 	size_t count = 0, i;
552 	char *bname = NULL;
553 	FILE *f = get_code_file();
554 
555 	if (ASN1_TAILQ_EMPTY(t->members)) {
556 	    add_line(temp, "{ A1_PARSE_T(A1T_HEIM_BIT_STRING), %s, NULL }", poffset);
557 	    break;
558 	}
559 
560 	if (asprintf(&bname, "bmember_%s_%p", name ? name : "", t) < 0 || bname == NULL)
561 	    errx(1, "malloc");
562 	output_name(bname);
563 
564 	ASN1_TAILQ_FOREACH(m, t->members, members) {
565 	    add_line(&template, "{ 0, %d, 0 } /* %s */", m->val, m->gen_name);
566 	}
567 
568 	ASN1_TAILQ_FOREACH(q, &template, members) {
569 	    count++;
570 	}
571 
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);
576 	i = 1;
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 ? "" : ",");
580 	}
581 	fprintf(f, "};\n");
582 
583 	add_line(temp, "{ A1_OP_BMEMBER, %s, asn1_%s_%s }", poffset, basetype, bname);
584 
585 	free(bname);
586 
587 	break;
588     }
589     case TSequence: {
590 	Member *m;
591 
592 	ASN1_TAILQ_FOREACH(m, t->members, members) {
593 	    char *newbasename = NULL;
594 
595 	    if (m->ellipsis)
596 		continue;
597 
598 	    if (name) {
599 		if (asprintf(&newbasename, "%s_%s", basetype, name) < 0)
600 		    errx(1, "malloc");
601 	    } else
602 		newbasename = strdup(basetype);
603 	    if (newbasename == NULL)
604 		errx(1, "malloc");
605 
606 	    template_members(temp, newbasename, m->gen_name, m->type, m->optional, isstruct, 1);
607 
608 	    free(newbasename);
609 	}
610 
611 	break;
612     }
613     case TTag: {
614 	char *tname = NULL, *elname = NULL;
615 	const char *sename, *dupname;
616 	int subtype_is_struct = is_struct(t->subtype, isstruct);
617 
618 	if (subtype_is_struct)
619 	    sename = basetype;
620 	else
621 	    sename = symbol_name(basetype, t->subtype);
622 
623 	if (asprintf(&tname, "tag_%s_%p", name ? name : "", t) < 0 || tname == NULL)
624 	    errx(1, "malloc");
625 	output_name(tname);
626 
627 	if (asprintf(&elname, "%s_%s", basetype, tname) < 0 || elname == NULL)
628 	    errx(1, "malloc");
629 
630 	generate_template_type(elname, &dupname, NULL, sename, name,
631 			       t->subtype, 0, subtype_is_struct, 0);
632 
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" : "");
639 
640 	free(tname);
641 	free(elname);
642 
643 	break;
644     }
645     case TSetOf:
646     case TSequenceOf: {
647 	const char *type = NULL, *tname, *dupname;
648 	char *sename = NULL, *elname = NULL;
649 	int subtype_is_struct = is_struct(t->subtype, 0);
650 
651 	if (name && subtype_is_struct) {
652 	    tname = "seofTstruct";
653 	    if (asprintf(&sename, "%s_%s_val", basetype, name) < 0)
654 		errx(1, "malloc");
655 	} else if (subtype_is_struct) {
656 	    tname = "seofTstruct";
657 	    if (asprintf(&sename, "%s_val", symbol_name(basetype, t->subtype)) < 0)
658 		errx(1, "malloc");
659 	} else {
660 	    if (name)
661 		tname = name;
662 	    else
663 		tname = "seofTstruct";
664 	    sename = strdup(symbol_name(basetype, t->subtype));
665 	}
666 	if (sename == NULL)
667 	    errx(1, "malloc");
668 
669 	if (t->type == TSetOf) type = "A1_OP_SETOF";
670 	else if (t->type == TSequenceOf) type = "A1_OP_SEQOF";
671 	else abort();
672 
673 	if (asprintf(&elname, "%s_%s_%p", basetype, tname, t) < 0 || elname == NULL)
674 	    errx(1, "malloc");
675 
676 	generate_template_type(elname, &dupname, NULL, sename, NULL, t->subtype,
677 			       0, subtype_is_struct, need_offset);
678 
679 	add_line(temp, "{ %s, %s, asn1_%s }", type, poffset, dupname);
680 	free(sename);
681 	break;
682     }
683     case TChoice: {
684 	struct templatehead template = ASN1_TAILQ_HEAD_INITIALIZER(template);
685 	struct template *q;
686 	size_t count = 0, i;
687 	char *tname = NULL;
688 	FILE *f = get_code_file();
689 	Member *m;
690 	int ellipsis = 0;
691 	char *e;
692 
693 	if (asprintf(&tname, "asn1_choice_%s_%s%x",
694 		     basetype, name ? name : "", (unsigned int)(uintptr_t)t) < 0 || tname == NULL)
695 	    errx(1, "malloc");
696 
697 	ASN1_TAILQ_FOREACH(m, t->members, members) {
698 	    const char *dupname;
699 	    char *elname = NULL;
700 	    char *newbasename = NULL;
701 	    int subtype_is_struct;
702 
703 	    if (m->ellipsis) {
704 		ellipsis = 1;
705 		continue;
706 	    }
707 
708 	    subtype_is_struct = is_struct(m->type, 0);
709 
710 	    if (asprintf(&elname, "%s_choice_%s", basetype, m->gen_name) < 0 || elname == NULL)
711 		errx(1, "malloc");
712 
713 	    if (subtype_is_struct) {
714 		if (asprintf(&newbasename, "%s_%s", basetype, m->gen_name) < 0)
715 		    errx(1, "malloc");
716 	    } else
717 		newbasename = strdup(basetype);
718 
719 	    if (newbasename == NULL)
720 		errx(1, "malloc");
721 
722 
723 	    generate_template_type(elname, &dupname, NULL,
724 				   symbol_name(newbasename, m->type),
725 				   NULL, m->type, 0, subtype_is_struct, 1);
726 
727 	    add_line(&template, "{ %s, offsetof(%s%s, u.%s), asn1_%s }",
728 		     m->label, isstruct ? "struct " : "",
729 		     basetype, m->gen_name,
730 		     dupname);
731 
732 	    free(elname);
733 	    free(newbasename);
734 	}
735 
736 	e = NULL;
737 	if (ellipsis) {
738 	    if (asprintf(&e, "offsetof(%s%s, u.asn1_ellipsis)", isstruct ? "struct " : "", basetype) < 0 || e == NULL)
739 		errx(1, "malloc");
740 	}
741 
742 	ASN1_TAILQ_FOREACH(q, &template, members) {
743 	    count++;
744 	}
745 
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);
749 	i = 1;
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 ? "" : ",");
753 	}
754 	fprintf(f, "};\n");
755 
756 	add_line(temp, "{ A1_OP_CHOICE, %s, %s }", poffset, tname);
757 
758 	free(e);
759 	free(tname);
760 	break;
761     }
762     default:
763 	abort ();
764     }
765     if (poffset)
766 	free(poffset);
767 }
768 
769 static void
770 gen_extern_stubs(FILE *f, const char *name)
771 {
772     fprintf(f,
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"
779 	    "\tsizeof(%s)\n"
780 	    "};\n",
781 	    name, name, name, name,
782 	    name, name, name);
783 }
784 
785 void
786 gen_template_import(const Symbol *s)
787 {
788     FILE *f = get_code_file();
789 
790     if (template_flag == 0)
791 	return;
792 
793     gen_extern_stubs(f, s->gen_name);
794 }
795 
796 static void
797 generate_template_type(const char *varname,
798 		       const char **dupname,
799 		       const char *symname,
800 		       const char *basetype,
801 		       const char *name,
802 		       Type *type,
803 		       int optional, int isstruct, int need_offset)
804 {
805     struct tlist *tl;
806     const char *dup;
807     int have_ellipsis = 0;
808 
809     tl = tlist_new(varname);
810 
811     template_members(&tl->template, basetype, name, type, optional, isstruct, need_offset);
812 
813     /* if its a sequence or set type, check if there is a ellipsis */
814     if (type->type == TSequence || type->type == TSet) {
815 	Member *m;
816 	ASN1_TAILQ_FOREACH(m, type->members, members) {
817 	    if (m->ellipsis)
818 		have_ellipsis = 1;
819 	}
820     }
821 
822     if (ASN1_TAILQ_EMPTY(&tl->template) && compact_tag(type)->type != TNull)
823 	errx(1, "Tag %s...%s with no content ?", basetype, name ? name : "");
824 
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));
829 
830     dup = tlist_find_dup(tl);
831     if (dup) {
832 	if (strcmp(dup, tl->name) == 0)
833 	    errx(1, "found dup of ourself");
834 	*dupname = dup;
835     } else {
836 	*dupname = tl->name;
837 	tlist_print(tl);
838 	tlist_add(tl);
839     }
840 }
841 
842 
843 void
844 generate_template(const Symbol *s)
845 {
846     FILE *f = get_code_file();
847     const char *dupname;
848 
849     if (use_extern(s)) {
850 	gen_extern_stubs(f, s->gen_name);
851 	return;
852     }
853 
854     generate_template_type(s->gen_name, &dupname, s->name, s->gen_name, NULL, s->type, 0, 0, 1);
855 
856     fprintf(f,
857 	    "\n"
858 	    "int\n"
859 	    "decode_%s(const unsigned char *p, size_t len, %s *data, size_t *size)\n"
860 	    "{\n"
861 	    "    return _asn1_decode_top(asn1_%s, 0|%s, p, len, data, size);\n"
862 	    "}\n"
863 	    "\n",
864 	    s->gen_name,
865 	    s->gen_name,
866 	    dupname,
867 	    support_ber ? "A1_PF_ALLOW_BER" : "0");
868 
869     fprintf(f,
870 	    "\n"
871 	    "int\n"
872 	    "encode_%s(unsigned char *p, size_t len, const %s *data, size_t *size)\n"
873 	    "{\n"
874 	    "    return _asn1_encode(asn1_%s, p, len, data, size);\n"
875 	    "}\n"
876 	    "\n",
877 	    s->gen_name,
878 	    s->gen_name,
879 	    dupname);
880 
881     fprintf(f,
882 	    "\n"
883 	    "size_t\n"
884 	    "length_%s(const %s *data)\n"
885 	    "{\n"
886 	    "    return _asn1_length(asn1_%s, data);\n"
887 	    "}\n"
888 	    "\n",
889 	    s->gen_name,
890 	    s->gen_name,
891 	    dupname);
892 
893 
894     fprintf(f,
895 	    "\n"
896 	    "void\n"
897 	    "free_%s(%s *data)\n"
898 	    "{\n"
899 	    "    _asn1_free(asn1_%s, data);\n"
900 	    "}\n"
901 	    "\n",
902 	    s->gen_name,
903 	    s->gen_name,
904 	    dupname);
905 
906     fprintf(f,
907 	    "\n"
908 	    "int\n"
909 	    "copy_%s(const %s *from, %s *to)\n"
910 	    "{\n"
911 	    "    return _asn1_copy_top(asn1_%s, from, to);\n"
912 	    "}\n"
913 	    "\n",
914 	    s->gen_name,
915 	    s->gen_name,
916 	    s->gen_name,
917 	    dupname);
918 }
919