1226031Sstas/*
2226031Sstas * Copyright (c) 1997 - 2005 Kungliga Tekniska H�gskolan
3226031Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4226031Sstas * All rights reserved.
5226031Sstas *
6226031Sstas * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7226031Sstas *
8226031Sstas * Redistribution and use in source and binary forms, with or without
9226031Sstas * modification, are permitted provided that the following conditions
10226031Sstas * are met:
11226031Sstas *
12226031Sstas * 1. Redistributions of source code must retain the above copyright
13226031Sstas *    notice, this list of conditions and the following disclaimer.
14226031Sstas *
15226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
16226031Sstas *    notice, this list of conditions and the following disclaimer in the
17226031Sstas *    documentation and/or other materials provided with the distribution.
18226031Sstas *
19226031Sstas * 3. Neither the name of the Institute nor the names of its contributors
20226031Sstas *    may be used to endorse or promote products derived from this software
21226031Sstas *    without specific prior written permission.
22226031Sstas *
23226031Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33226031Sstas * SUCH DAMAGE.
34226031Sstas */
35226031Sstas
36226031Sstas#include "gen_locl.h"
37226031Sstas
38226031Sstasstatic const char *symbol_name(const char *, const Type *);
39226031Sstasstatic void generate_template_type(const char *, const char **, const char *, const char *, const char *,
40226031Sstas				   Type *, int, int, int);
41226031Sstas
42226031Sstasstatic const char *
43226031Sstasttype_symbol(const char *basename, const Type *t)
44226031Sstas{
45226031Sstas    return t->symbol->gen_name;
46226031Sstas}
47226031Sstas
48226031Sstasstatic const char *
49226031Sstasinteger_symbol(const char *basename, const Type *t)
50226031Sstas{
51226031Sstas    if (t->members)
52226031Sstas	return "int"; /* XXX enum foo */
53226031Sstas    else if (t->range == NULL)
54226031Sstas	return "heim_integer";
55226031Sstas    else if (t->range->min == INT_MIN && t->range->max == INT_MAX)
56226031Sstas	return "int";
57226031Sstas    else if (t->range->min == 0 && t->range->max == UINT_MAX)
58226031Sstas	return "unsigned";
59226031Sstas    else if (t->range->min == 0 && t->range->max == INT_MAX)
60226031Sstas	return "unsigned";
61226031Sstas    else {
62226031Sstas	abort();
63226031Sstas        UNREACHABLE(return NULL);
64226031Sstas    }
65226031Sstas}
66226031Sstas
67226031Sstasstatic const char *
68226031Sstasboolean_symbol(const char *basename, const Type *t)
69226031Sstas{
70226031Sstas    return "int";
71226031Sstas}
72226031Sstas
73226031Sstas
74226031Sstasstatic const char *
75226031Sstasoctetstring_symbol(const char *basename, const Type *t)
76226031Sstas{
77226031Sstas    return "heim_octet_string";
78226031Sstas}
79226031Sstas
80226031Sstasstatic const char *
81226031Sstassequence_symbol(const char *basename, const Type *t)
82226031Sstas{
83226031Sstas    return basename;
84226031Sstas}
85226031Sstas
86226031Sstasstatic const char *
87226031Sstastime_symbol(const char *basename, const Type *t)
88226031Sstas{
89226031Sstas    return "time_t";
90226031Sstas}
91226031Sstas
92226031Sstasstatic const char *
93226031Sstastag_symbol(const char *basename, const Type *t)
94226031Sstas{
95226031Sstas    return symbol_name(basename, t->subtype);
96226031Sstas}
97226031Sstas
98226031Sstasstatic const char *
99226031Sstasgeneralstring_symbol(const char *basename, const Type *t)
100226031Sstas{
101226031Sstas    return "heim_general_string";
102226031Sstas}
103226031Sstas
104226031Sstasstatic const char *
105226031Sstasprintablestring_symbol(const char *basename, const Type *t)
106226031Sstas{
107226031Sstas    return "heim_printable_string";
108226031Sstas}
109226031Sstas
110226031Sstasstatic const char *
111226031Sstasia5string_symbol(const char *basename, const Type *t)
112226031Sstas{
113226031Sstas    return "heim_ia5_string";
114226031Sstas}
115226031Sstas
116226031Sstasstatic const char *
117226031Sstasvisiblestring_symbol(const char *basename, const Type *t)
118226031Sstas{
119226031Sstas    return "heim_visible_string";
120226031Sstas}
121226031Sstas
122226031Sstasstatic const char *
123226031Sstasutf8string_symbol(const char *basename, const Type *t)
124226031Sstas{
125226031Sstas    return "heim_utf8_string";
126226031Sstas}
127226031Sstas
128226031Sstasstatic const char *
129226031Sstasbmpstring_symbol(const char *basename, const Type *t)
130226031Sstas{
131226031Sstas    return "heim_bmp_string";
132226031Sstas}
133226031Sstas
134226031Sstasstatic const char *
135226031Sstasuniversalstring_symbol(const char *basename, const Type *t)
136226031Sstas{
137226031Sstas    return "heim_universal_string";
138226031Sstas}
139226031Sstas
140226031Sstasstatic const char *
141226031Sstasoid_symbol(const char *basename, const Type *t)
142226031Sstas{
143226031Sstas    return "heim_oid";
144226031Sstas}
145226031Sstas
146226031Sstasstatic const char *
147226031Sstasbitstring_symbol(const char *basename, const Type *t)
148226031Sstas{
149226031Sstas    if (t->members)
150226031Sstas	return basename;
151226031Sstas    return "heim_bit_string";
152226031Sstas}
153226031Sstas
154226031Sstas
155226031Sstas
156226031Sstasstruct {
157226031Sstas    enum typetype type;
158226031Sstas    const char *(*symbol_name)(const char *, const Type *);
159226031Sstas    int is_struct;
160226031Sstas} types[] =  {
161226031Sstas    { TBMPString, bmpstring_symbol, 0 },
162226031Sstas    { TBitString, bitstring_symbol, 0 },
163226031Sstas    { TBoolean, boolean_symbol, 0 },
164226031Sstas    { TGeneralString, generalstring_symbol, 0 },
165226031Sstas    { TGeneralizedTime, time_symbol, 0 },
166226031Sstas    { TIA5String, ia5string_symbol, 0 },
167226031Sstas    { TInteger, integer_symbol, 0 },
168226031Sstas    { TOID, oid_symbol, 0 },
169226031Sstas    { TOctetString, octetstring_symbol, 0 },
170226031Sstas    { TPrintableString, printablestring_symbol, 0 },
171226031Sstas    { TSequence, sequence_symbol, 1 },
172226031Sstas    { TSequenceOf, tag_symbol, 1 },
173226031Sstas    { TSetOf, tag_symbol, 1 },
174226031Sstas    { TTag, tag_symbol, 1 },
175226031Sstas    { TType, ttype_symbol, 1 },
176226031Sstas    { TUTCTime, time_symbol, 0 },
177226031Sstas    { TUniversalString, universalstring_symbol, 0 },
178226031Sstas    { TVisibleString,  visiblestring_symbol, 0 },
179226031Sstas    { TUTF8String, utf8string_symbol, 0 },
180226031Sstas    { TChoice, sequence_symbol, 1 },
181226031Sstas    { TNull, integer_symbol, 1 }
182226031Sstas};
183226031Sstas
184226031Sstasstatic FILE *
185226031Sstasget_code_file(void)
186226031Sstas{
187226031Sstas    if (!one_code_file)
188226031Sstas	return templatefile;
189226031Sstas    return codefile;
190226031Sstas}
191226031Sstas
192226031Sstas
193226031Sstasstatic int
194226031Sstasis_supported_type_p(const Type *t)
195226031Sstas{
196226031Sstas    size_t i;
197226031Sstas
198226031Sstas    for (i = 0; i < sizeof(types)/sizeof(types[0]); i++)
199226031Sstas	if (t->type == types[i].type)
200226031Sstas	    return 1;
201226031Sstas    return 0;
202226031Sstas}
203226031Sstas
204226031Sstasint
205226031Sstasis_template_compat (const Symbol *s)
206226031Sstas{
207226031Sstas    return is_supported_type_p(s->type);
208226031Sstas}
209226031Sstas
210226031Sstasstatic const char *
211226031Sstassymbol_name(const char *basename, const Type *t)
212226031Sstas{
213226031Sstas    size_t i;
214226031Sstas
215226031Sstas    for (i = 0; i < sizeof(types)/sizeof(types[0]); i++)
216226031Sstas	if (t->type == types[i].type)
217226031Sstas	    return (types[i].symbol_name)(basename, t);
218226031Sstas    printf("unknown der type: %d\n", t->type);
219226031Sstas    exit(1);
220226031Sstas}
221226031Sstas
222226031Sstas
223226031Sstasstatic char *
224226031Sstaspartial_offset(const char *basetype, const char *name, int need_offset)
225226031Sstas{
226226031Sstas    char *str;
227226031Sstas    if (name == NULL || need_offset == 0)
228226031Sstas	return strdup("0");
229226031Sstas    if (asprintf(&str, "offsetof(struct %s, %s)", basetype, name) < 0 || str == NULL)
230226031Sstas	errx(1, "malloc");
231226031Sstas    return str;
232226031Sstas}
233226031Sstas
234226031Sstasstruct template {
235226031Sstas    char *line;
236226031Sstas    char *tt;
237226031Sstas    char *offset;
238226031Sstas    char *ptr;
239226031Sstas    ASN1_TAILQ_ENTRY(template) members;
240226031Sstas};
241226031Sstas
242226031SstasASN1_TAILQ_HEAD(templatehead, template);
243226031Sstas
244226031Sstasstruct tlist {
245226031Sstas    char *name;
246226031Sstas    char *header;
247226031Sstas    struct templatehead template;
248226031Sstas    ASN1_TAILQ_ENTRY(tlist) tmembers;
249226031Sstas};
250226031Sstas
251226031SstasASN1_TAILQ_HEAD(tlisthead, tlist);
252226031Sstas
253226031Sstasstatic void tlist_header(struct tlist *, const char *, ...) __attribute__((__format__(__printf__, 2, 3)));
254226031Sstasstatic struct template *
255226031Sstas    add_line(struct templatehead *, const char *, ...) __attribute__((__format__(__printf__, 2, 3)));
256226031Sstasstatic int tlist_cmp(const struct tlist *, const struct tlist *);
257226031Sstas
258226031Sstasstatic void add_line_pointer(struct templatehead *, const char *, const char *, const char *, ...)
259226031Sstas    __attribute__((__format__(__printf__, 4, 5)));
260226031Sstas
261226031Sstas
262226031Sstasstatic struct tlisthead tlistmaster = ASN1_TAILQ_HEAD_INITIALIZER(tlistmaster);
263226031Sstasstatic unsigned long numdups = 0;
264226031Sstas
265226031Sstasstatic struct tlist *
266226031Sstastlist_new(const char *name)
267226031Sstas{
268226031Sstas    struct tlist *tl = calloc(1, sizeof(*tl));
269226031Sstas    tl->name = strdup(name);
270226031Sstas    ASN1_TAILQ_INIT(&tl->template);
271226031Sstas    return tl;
272226031Sstas}
273226031Sstas
274226031Sstasstatic void
275226031Sstastlist_header(struct tlist *t, const char *fmt, ...)
276226031Sstas{
277226031Sstas    va_list ap;
278226031Sstas    va_start(ap, fmt);
279226031Sstas    if (vasprintf(&t->header, fmt, ap) < 0 || t->header == NULL)
280226031Sstas	errx(1, "malloc");
281226031Sstas    va_end(ap);
282226031Sstas}
283226031Sstas
284226031Sstasstatic unsigned long
285226031Sstastlist_count(struct tlist *tl)
286226031Sstas{
287226031Sstas    unsigned int count = 0;
288226031Sstas    struct template *q;
289226031Sstas
290226031Sstas    ASN1_TAILQ_FOREACH(q, &tl->template, members) {
291226031Sstas	count++;
292226031Sstas    }
293226031Sstas    return count;
294226031Sstas}
295226031Sstas
296226031Sstasstatic void
297226031Sstastlist_add(struct tlist *tl)
298226031Sstas{
299226031Sstas    ASN1_TAILQ_INSERT_TAIL(&tlistmaster, tl, tmembers);
300226031Sstas}
301226031Sstas
302226031Sstasstatic void
303226031Sstastlist_print(struct tlist *tl)
304226031Sstas{
305226031Sstas    struct template *q;
306226031Sstas    unsigned int i = 1;
307226031Sstas    FILE *f = get_code_file();
308226031Sstas
309226031Sstas    fprintf(f, "static const struct asn1_template asn1_%s[] = {\n", tl->name);
310226031Sstas    fprintf(f, "/* 0 */ %s,\n", tl->header);
311226031Sstas    ASN1_TAILQ_FOREACH(q, &tl->template, members) {
312226031Sstas	int last = (ASN1_TAILQ_LAST(&tl->template, templatehead) == q);
313226031Sstas	fprintf(f, "/* %lu */ %s%s\n", (unsigned long)i++, q->line, last ? "" : ",");
314226031Sstas    }
315226031Sstas    fprintf(f, "};\n");
316226031Sstas}
317226031Sstas
318226031Sstasstatic struct tlist *
319226031Sstastlist_find_by_name(const char *name)
320226031Sstas{
321226031Sstas    struct tlist *ql;
322226031Sstas    ASN1_TAILQ_FOREACH(ql, &tlistmaster, tmembers) {
323226031Sstas	if (strcmp(ql->name, name) == 0)
324226031Sstas	    return ql;
325226031Sstas    }
326226031Sstas    return NULL;
327226031Sstas}
328226031Sstas
329226031Sstasstatic int
330226031Sstastlist_cmp_name(const char *tname, const char *qname)
331226031Sstas{
332226031Sstas    struct tlist *tl = tlist_find_by_name(tname);
333226031Sstas    struct tlist *ql = tlist_find_by_name(qname);
334226031Sstas    return tlist_cmp(tl, ql);
335226031Sstas}
336226031Sstas
337226031Sstasstatic int
338226031Sstastlist_cmp(const struct tlist *tl, const struct tlist *ql)
339226031Sstas{
340226031Sstas    int ret;
341226031Sstas    struct template *t, *q;
342226031Sstas
343226031Sstas    ret = strcmp(tl->header, ql->header);
344226031Sstas    if (ret) return ret;
345226031Sstas
346226031Sstas    q = ASN1_TAILQ_FIRST(&ql->template);
347226031Sstas    ASN1_TAILQ_FOREACH(t, &tl->template, members) {
348226031Sstas	if (q == NULL) return 1;
349226031Sstas
350226031Sstas	if (t->ptr == NULL || q->ptr == NULL) {
351226031Sstas	    ret = strcmp(t->line, q->line);
352226031Sstas	    if (ret) return ret;
353226031Sstas	} else {
354226031Sstas	    ret = strcmp(t->tt, q->tt);
355226031Sstas	    if (ret) return ret;
356226031Sstas
357226031Sstas	    ret = strcmp(t->offset, q->offset);
358226031Sstas	    if (ret) return ret;
359226031Sstas
360226031Sstas	    if ((ret = strcmp(t->ptr, q->ptr)) != 0 ||
361226031Sstas		(ret = tlist_cmp_name(t->ptr, q->ptr)) != 0)
362226031Sstas		return ret;
363226031Sstas	}
364226031Sstas	q = ASN1_TAILQ_NEXT(q, members);
365226031Sstas    }
366226031Sstas    if (q != NULL) return -1;
367226031Sstas    return 0;
368226031Sstas}
369226031Sstas
370226031Sstas
371226031Sstasstatic const char *
372226031Sstastlist_find_dup(const struct tlist *tl)
373226031Sstas{
374226031Sstas    struct tlist *ql;
375226031Sstas
376226031Sstas    ASN1_TAILQ_FOREACH(ql, &tlistmaster, tmembers) {
377226031Sstas	if (tlist_cmp(ql, tl) == 0) {
378226031Sstas	    numdups++;
379226031Sstas	    return ql->name;
380226031Sstas	}
381226031Sstas    }
382226031Sstas    return NULL;
383226031Sstas}
384226031Sstas
385226031Sstas
386226031Sstas/*
387226031Sstas *
388226031Sstas */
389226031Sstas
390226031Sstasstatic struct template *
391226031Sstasadd_line(struct templatehead *t, const char *fmt, ...)
392226031Sstas{
393226031Sstas    struct template *q = calloc(1, sizeof(*q));
394226031Sstas    va_list ap;
395226031Sstas    va_start(ap, fmt);
396226031Sstas    if (vasprintf(&q->line, fmt, ap) < 0 || q->line == NULL)
397226031Sstas	errx(1, "malloc");
398226031Sstas    va_end(ap);
399226031Sstas    ASN1_TAILQ_INSERT_TAIL(t, q, members);
400226031Sstas    return q;
401226031Sstas}
402226031Sstas
403226031Sstasstatic void
404226031Sstasadd_line_pointer(struct templatehead *t,
405226031Sstas		 const char *ptr,
406226031Sstas		 const char *offset,
407226031Sstas		 const char *ttfmt,
408226031Sstas		 ...)
409226031Sstas{
410226031Sstas    struct template *q;
411226031Sstas    va_list ap;
412226031Sstas    char *tt = NULL;
413226031Sstas
414226031Sstas    va_start(ap, ttfmt);
415226031Sstas    if (vasprintf(&tt, ttfmt, ap) < 0 || tt == NULL)
416226031Sstas	errx(1, "malloc");
417226031Sstas    va_end(ap);
418226031Sstas
419226031Sstas    q = add_line(t, "{ %s, %s, asn1_%s }", tt, offset, ptr);
420226031Sstas    q->tt = tt;
421226031Sstas    q->offset = strdup(offset);
422226031Sstas    q->ptr = strdup(ptr);
423226031Sstas}
424226031Sstas
425226031Sstasstatic int
426226031Sstasuse_extern(const Symbol *s)
427226031Sstas{
428226031Sstas    if (s->type == NULL)
429226031Sstas	return 1;
430226031Sstas    return 0;
431226031Sstas}
432226031Sstas
433226031Sstasstatic int
434226031Sstasis_struct(Type *t, int isstruct)
435226031Sstas{
436226031Sstas    size_t i;
437226031Sstas
438226031Sstas    if (t->type == TType)
439226031Sstas	return 0;
440226031Sstas    if (t->type == TSequence || t->type == TSet || t->type == TChoice)
441226031Sstas	return 1;
442226031Sstas    if (t->type == TTag)
443226031Sstas	return is_struct(t->subtype, isstruct);
444226031Sstas
445226031Sstas    for (i = 0; i < sizeof(types)/sizeof(types[0]); i++) {
446226031Sstas	if (t->type == types[i].type) {
447226031Sstas	    if (types[i].is_struct == 0)
448226031Sstas		return 0;
449226031Sstas	    else
450226031Sstas		break;
451226031Sstas	}
452226031Sstas    }
453226031Sstas
454226031Sstas    return isstruct;
455226031Sstas}
456226031Sstas
457226031Sstasstatic const Type *
458226031Sstascompact_tag(const Type *t)
459226031Sstas{
460226031Sstas    while (t->type == TTag)
461226031Sstas	t = t->subtype;
462226031Sstas    return t;
463226031Sstas}
464226031Sstas
465226031Sstasstatic void
466226031Sstastemplate_members(struct templatehead *temp, const char *basetype, const char *name, const Type *t, int optional, int isstruct, int need_offset)
467226031Sstas{
468226031Sstas    char *poffset = NULL;
469226031Sstas
470226031Sstas    if (optional && t->type != TTag && t->type != TType)
471226031Sstas	errx(1, "%s...%s is optional and not a (TTag or TType)", basetype, name);
472226031Sstas
473226031Sstas    poffset = partial_offset(basetype, name, need_offset);
474226031Sstas
475226031Sstas    switch (t->type) {
476226031Sstas    case TType:
477226031Sstas	if (use_extern(t->symbol)) {
478226031Sstas	    add_line(temp, "{ A1_OP_TYPE_EXTERN %s, %s, &asn1_extern_%s}",
479226031Sstas		     optional ? "|A1_FLAG_OPTIONAL" : "",
480226031Sstas		     poffset, t->symbol->gen_name);
481226031Sstas	} else {
482226031Sstas	    add_line_pointer(temp, t->symbol->gen_name, poffset,
483226031Sstas			     "A1_OP_TYPE %s", optional ? "|A1_FLAG_OPTIONAL" : "");
484226031Sstas	}
485226031Sstas	break;
486226031Sstas    case TInteger: {
487226031Sstas	char *itype = NULL;
488226031Sstas
489226031Sstas	if (t->members)
490226031Sstas	    itype = "IMEMBER";
491226031Sstas	else if (t->range == NULL)
492226031Sstas	    itype = "HEIM_INTEGER";
493226031Sstas	else if (t->range->min == INT_MIN && t->range->max == INT_MAX)
494226031Sstas	    itype = "INTEGER";
495226031Sstas	else if (t->range->min == 0 && t->range->max == UINT_MAX)
496226031Sstas	    itype = "UNSIGNED";
497226031Sstas	else if (t->range->min == 0 && t->range->max == INT_MAX)
498226031Sstas	    itype = "UNSIGNED";
499226031Sstas	else
500226031Sstas	    errx(1, "%s: unsupported range %d -> %d",
501226031Sstas		 name, t->range->min, t->range->max);
502226031Sstas
503226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_%s), %s, NULL }", itype, poffset);
504226031Sstas	break;
505226031Sstas    }
506226031Sstas    case TGeneralString:
507226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_GENERAL_STRING), %s, NULL }", poffset);
508226031Sstas	break;
509226031Sstas    case TTeletexString:
510226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_TELETEX_STRING), %s, NULL }", poffset);
511226031Sstas	break;
512226031Sstas    case TPrintableString:
513226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_PRINTABLE_STRING), %s, NULL }", poffset);
514226031Sstas	break;
515226031Sstas    case TOctetString:
516226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_OCTET_STRING), %s, NULL }", poffset);
517226031Sstas	break;
518226031Sstas    case TIA5String:
519226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_IA5_STRING), %s, NULL }", poffset);
520226031Sstas	break;
521226031Sstas    case TBMPString:
522226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_BMP_STRING), %s, NULL }", poffset);
523226031Sstas	break;
524226031Sstas    case TUniversalString:
525226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_UNIVERSAL_STRING), %s, NULL }", poffset);
526226031Sstas	break;
527226031Sstas    case TVisibleString:
528226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_VISIBLE_STRING), %s, NULL }", poffset);
529226031Sstas	break;
530226031Sstas    case TUTF8String:
531226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_UTF8_STRING), %s, NULL }", poffset);
532226031Sstas	break;
533226031Sstas    case TGeneralizedTime:
534226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_GENERALIZED_TIME), %s, NULL }", poffset);
535226031Sstas	break;
536226031Sstas    case TUTCTime:
537226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_UTC_TIME), %s, NULL }", poffset);
538226031Sstas	break;
539226031Sstas    case TBoolean:
540226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_BOOLEAN), %s, NULL }", poffset);
541226031Sstas	break;
542226031Sstas    case TOID:
543226031Sstas	add_line(temp, "{ A1_PARSE_T(A1T_OID), %s, NULL }", poffset);
544226031Sstas	break;
545226031Sstas    case TNull:
546226031Sstas	break;
547226031Sstas    case TBitString: {
548226031Sstas	struct templatehead template = ASN1_TAILQ_HEAD_INITIALIZER(template);
549226031Sstas	struct template *q;
550226031Sstas	Member *m;
551226031Sstas	size_t count = 0, i;
552226031Sstas	char *bname = NULL;
553226031Sstas	FILE *f = get_code_file();
554226031Sstas
555226031Sstas	if (ASN1_TAILQ_EMPTY(t->members)) {
556226031Sstas	    add_line(temp, "{ A1_PARSE_T(A1T_HEIM_BIT_STRING), %s, NULL }", poffset);
557226031Sstas	    break;
558226031Sstas	}
559226031Sstas
560226031Sstas	if (asprintf(&bname, "bmember_%s_%p", name ? name : "", t) < 0 || bname == NULL)
561226031Sstas	    errx(1, "malloc");
562226031Sstas	output_name(bname);
563226031Sstas
564226031Sstas	ASN1_TAILQ_FOREACH(m, t->members, members) {
565226031Sstas	    add_line(&template, "{ 0, %d, 0 } /* %s */", m->val, m->gen_name);
566226031Sstas	}
567226031Sstas
568226031Sstas	ASN1_TAILQ_FOREACH(q, &template, members) {
569226031Sstas	    count++;
570226031Sstas	}
571226031Sstas
572226031Sstas	fprintf(f, "static const struct asn1_template asn1_%s_%s[] = {\n", basetype, bname);
573226031Sstas	fprintf(f, "/* 0 */ { 0%s, sizeof(%s), ((void *)%lu) },\n",
574226031Sstas		rfc1510_bitstring ? "|A1_HBF_RFC1510" : "",
575226031Sstas		basetype, (unsigned long)count);
576226031Sstas	i = 1;
577226031Sstas	ASN1_TAILQ_FOREACH(q, &template, members) {
578226031Sstas	    int last = (ASN1_TAILQ_LAST(&template, templatehead) == q);
579226031Sstas	    fprintf(f, "/* %lu */ %s%s\n", (unsigned long)i++, q->line, last ? "" : ",");
580226031Sstas	}
581226031Sstas	fprintf(f, "};\n");
582226031Sstas
583226031Sstas	add_line(temp, "{ A1_OP_BMEMBER, %s, asn1_%s_%s }", poffset, basetype, bname);
584226031Sstas
585226031Sstas	free(bname);
586226031Sstas
587226031Sstas	break;
588226031Sstas    }
589226031Sstas    case TSequence: {
590226031Sstas	Member *m;
591226031Sstas
592226031Sstas	ASN1_TAILQ_FOREACH(m, t->members, members) {
593226031Sstas	    char *newbasename = NULL;
594226031Sstas
595226031Sstas	    if (m->ellipsis)
596226031Sstas		continue;
597226031Sstas
598226031Sstas	    if (name) {
599226031Sstas		if (asprintf(&newbasename, "%s_%s", basetype, name) < 0)
600226031Sstas		    errx(1, "malloc");
601226031Sstas	    } else
602226031Sstas		newbasename = strdup(basetype);
603226031Sstas	    if (newbasename == NULL)
604226031Sstas		errx(1, "malloc");
605226031Sstas
606226031Sstas	    template_members(temp, newbasename, m->gen_name, m->type, m->optional, isstruct, 1);
607226031Sstas
608226031Sstas	    free(newbasename);
609226031Sstas	}
610226031Sstas
611226031Sstas	break;
612226031Sstas    }
613226031Sstas    case TTag: {
614226031Sstas	char *tname = NULL, *elname = NULL;
615226031Sstas	const char *sename, *dupname;
616226031Sstas	int subtype_is_struct = is_struct(t->subtype, isstruct);
617226031Sstas
618226031Sstas	if (subtype_is_struct)
619226031Sstas	    sename = basetype;
620226031Sstas	else
621226031Sstas	    sename = symbol_name(basetype, t->subtype);
622226031Sstas
623226031Sstas	if (asprintf(&tname, "tag_%s_%p", name ? name : "", t) < 0 || tname == NULL)
624226031Sstas	    errx(1, "malloc");
625226031Sstas	output_name(tname);
626226031Sstas
627226031Sstas	if (asprintf(&elname, "%s_%s", basetype, tname) < 0 || elname == NULL)
628226031Sstas	    errx(1, "malloc");
629226031Sstas
630226031Sstas	generate_template_type(elname, &dupname, NULL, sename, name,
631226031Sstas			       t->subtype, 0, subtype_is_struct, 0);
632226031Sstas
633226031Sstas	add_line_pointer(temp, dupname, poffset,
634226031Sstas			 "A1_TAG_T(%s,%s,%s)%s",
635226031Sstas			 classname(t->tag.tagclass),
636226031Sstas			 is_primitive_type(t->subtype->type)  ? "PRIM" : "CONS",
637226031Sstas			 valuename(t->tag.tagclass, t->tag.tagvalue),
638226031Sstas			 optional ? "|A1_FLAG_OPTIONAL" : "");
639226031Sstas
640226031Sstas	free(tname);
641226031Sstas	free(elname);
642226031Sstas
643226031Sstas	break;
644226031Sstas    }
645226031Sstas    case TSetOf:
646226031Sstas    case TSequenceOf: {
647226031Sstas	const char *type = NULL, *tname, *dupname;
648226031Sstas	char *sename = NULL, *elname = NULL;
649226031Sstas	int subtype_is_struct = is_struct(t->subtype, 0);
650226031Sstas
651226031Sstas	if (name && subtype_is_struct) {
652226031Sstas	    tname = "seofTstruct";
653226031Sstas	    if (asprintf(&sename, "%s_%s_val", basetype, name) < 0)
654226031Sstas		errx(1, "malloc");
655226031Sstas	} else if (subtype_is_struct) {
656226031Sstas	    tname = "seofTstruct";
657226031Sstas	    if (asprintf(&sename, "%s_val", symbol_name(basetype, t->subtype)) < 0)
658226031Sstas		errx(1, "malloc");
659226031Sstas	} else {
660226031Sstas	    if (name)
661226031Sstas		tname = name;
662226031Sstas	    else
663226031Sstas		tname = "seofTstruct";
664226031Sstas	    sename = strdup(symbol_name(basetype, t->subtype));
665226031Sstas	}
666226031Sstas	if (sename == NULL)
667226031Sstas	    errx(1, "malloc");
668226031Sstas
669226031Sstas	if (t->type == TSetOf) type = "A1_OP_SETOF";
670226031Sstas	else if (t->type == TSequenceOf) type = "A1_OP_SEQOF";
671226031Sstas	else abort();
672226031Sstas
673226031Sstas	if (asprintf(&elname, "%s_%s_%p", basetype, tname, t) < 0 || elname == NULL)
674226031Sstas	    errx(1, "malloc");
675226031Sstas
676226031Sstas	generate_template_type(elname, &dupname, NULL, sename, NULL, t->subtype,
677226031Sstas			       0, subtype_is_struct, need_offset);
678226031Sstas
679226031Sstas	add_line(temp, "{ %s, %s, asn1_%s }", type, poffset, dupname);
680226031Sstas	free(sename);
681226031Sstas	break;
682226031Sstas    }
683226031Sstas    case TChoice: {
684226031Sstas	struct templatehead template = ASN1_TAILQ_HEAD_INITIALIZER(template);
685226031Sstas	struct template *q;
686226031Sstas	size_t count = 0, i;
687226031Sstas	char *tname = NULL;
688226031Sstas	FILE *f = get_code_file();
689226031Sstas	Member *m;
690226031Sstas	int ellipsis = 0;
691226031Sstas	char *e;
692226031Sstas
693226031Sstas	if (asprintf(&tname, "asn1_choice_%s_%s%x",
694226031Sstas		     basetype, name ? name : "", (unsigned int)(uintptr_t)t) < 0 || tname == NULL)
695226031Sstas	    errx(1, "malloc");
696226031Sstas
697226031Sstas	ASN1_TAILQ_FOREACH(m, t->members, members) {
698226031Sstas	    const char *dupname;
699226031Sstas	    char *elname = NULL;
700226031Sstas	    char *newbasename = NULL;
701226031Sstas	    int subtype_is_struct;
702226031Sstas
703226031Sstas	    if (m->ellipsis) {
704226031Sstas		ellipsis = 1;
705226031Sstas		continue;
706226031Sstas	    }
707226031Sstas
708226031Sstas	    subtype_is_struct = is_struct(m->type, 0);
709226031Sstas
710226031Sstas	    if (asprintf(&elname, "%s_choice_%s", basetype, m->gen_name) < 0 || elname == NULL)
711226031Sstas		errx(1, "malloc");
712226031Sstas
713226031Sstas	    if (subtype_is_struct) {
714226031Sstas		if (asprintf(&newbasename, "%s_%s", basetype, m->gen_name) < 0)
715226031Sstas		    errx(1, "malloc");
716226031Sstas	    } else
717226031Sstas		newbasename = strdup(basetype);
718226031Sstas
719226031Sstas	    if (newbasename == NULL)
720226031Sstas		errx(1, "malloc");
721226031Sstas
722226031Sstas
723226031Sstas	    generate_template_type(elname, &dupname, NULL,
724226031Sstas				   symbol_name(newbasename, m->type),
725226031Sstas				   NULL, m->type, 0, subtype_is_struct, 1);
726226031Sstas
727226031Sstas	    add_line(&template, "{ %s, offsetof(%s%s, u.%s), asn1_%s }",
728226031Sstas		     m->label, isstruct ? "struct " : "",
729226031Sstas		     basetype, m->gen_name,
730226031Sstas		     dupname);
731226031Sstas
732226031Sstas	    free(elname);
733226031Sstas	    free(newbasename);
734226031Sstas	}
735226031Sstas
736226031Sstas	e = NULL;
737226031Sstas	if (ellipsis) {
738226031Sstas	    if (asprintf(&e, "offsetof(%s%s, u.asn1_ellipsis)", isstruct ? "struct " : "", basetype) < 0 || e == NULL)
739226031Sstas		errx(1, "malloc");
740226031Sstas	}
741226031Sstas
742226031Sstas	ASN1_TAILQ_FOREACH(q, &template, members) {
743226031Sstas	    count++;
744226031Sstas	}
745226031Sstas
746226031Sstas	fprintf(f, "static const struct asn1_template %s[] = {\n", tname);
747226031Sstas	fprintf(f, "/* 0 */ { %s, offsetof(%s%s, element), ((void *)%lu) },\n",
748226031Sstas		e ? e : "0", isstruct ? "struct " : "", basetype, (unsigned long)count);
749226031Sstas	i = 1;
750226031Sstas	ASN1_TAILQ_FOREACH(q, &template, members) {
751226031Sstas	    int last = (ASN1_TAILQ_LAST(&template, templatehead) == q);
752226031Sstas	    fprintf(f, "/* %lu */ %s%s\n", (unsigned long)i++, q->line, last ? "" : ",");
753226031Sstas	}
754226031Sstas	fprintf(f, "};\n");
755226031Sstas
756226031Sstas	add_line(temp, "{ A1_OP_CHOICE, %s, %s }", poffset, tname);
757226031Sstas
758226031Sstas	free(e);
759226031Sstas	free(tname);
760226031Sstas	break;
761226031Sstas    }
762226031Sstas    default:
763226031Sstas	abort ();
764226031Sstas    }
765226031Sstas    if (poffset)
766226031Sstas	free(poffset);
767226031Sstas}
768226031Sstas
769226031Sstasstatic void
770226031Sstasgen_extern_stubs(FILE *f, const char *name)
771226031Sstas{
772226031Sstas    fprintf(f,
773226031Sstas	    "static const struct asn1_type_func asn1_extern_%s = {\n"
774226031Sstas	    "\t(asn1_type_encode)encode_%s,\n"
775226031Sstas	    "\t(asn1_type_decode)decode_%s,\n"
776226031Sstas	    "\t(asn1_type_length)length_%s,\n"
777226031Sstas	    "\t(asn1_type_copy)copy_%s,\n"
778226031Sstas	    "\t(asn1_type_release)free_%s,\n"
779226031Sstas	    "\tsizeof(%s)\n"
780226031Sstas	    "};\n",
781226031Sstas	    name, name, name, name,
782226031Sstas	    name, name, name);
783226031Sstas}
784226031Sstas
785226031Sstasvoid
786226031Sstasgen_template_import(const Symbol *s)
787226031Sstas{
788226031Sstas    FILE *f = get_code_file();
789226031Sstas
790226031Sstas    if (template_flag == 0)
791226031Sstas	return;
792226031Sstas
793226031Sstas    gen_extern_stubs(f, s->gen_name);
794226031Sstas}
795226031Sstas
796226031Sstasstatic void
797226031Sstasgenerate_template_type(const char *varname,
798226031Sstas		       const char **dupname,
799226031Sstas		       const char *symname,
800226031Sstas		       const char *basetype,
801226031Sstas		       const char *name,
802226031Sstas		       Type *type,
803226031Sstas		       int optional, int isstruct, int need_offset)
804226031Sstas{
805226031Sstas    struct tlist *tl;
806226031Sstas    const char *dup;
807226031Sstas    int have_ellipsis = 0;
808226031Sstas
809226031Sstas    tl = tlist_new(varname);
810226031Sstas
811226031Sstas    template_members(&tl->template, basetype, name, type, optional, isstruct, need_offset);
812226031Sstas
813226031Sstas    /* if its a sequence or set type, check if there is a ellipsis */
814226031Sstas    if (type->type == TSequence || type->type == TSet) {
815226031Sstas	Member *m;
816226031Sstas	ASN1_TAILQ_FOREACH(m, type->members, members) {
817226031Sstas	    if (m->ellipsis)
818226031Sstas		have_ellipsis = 1;
819226031Sstas	}
820226031Sstas    }
821226031Sstas
822226031Sstas    if (ASN1_TAILQ_EMPTY(&tl->template) && compact_tag(type)->type != TNull)
823226031Sstas	errx(1, "Tag %s...%s with no content ?", basetype, name ? name : "");
824226031Sstas
825226031Sstas    tlist_header(tl, "{ 0%s%s, sizeof(%s%s), ((void *)%lu) }",
826226031Sstas		 (symname && preserve_type(symname)) ? "|A1_HF_PRESERVE" : "",
827226031Sstas		 have_ellipsis ? "|A1_HF_ELLIPSIS" : "",
828226031Sstas		 isstruct ? "struct " : "", basetype, tlist_count(tl));
829226031Sstas
830226031Sstas    dup = tlist_find_dup(tl);
831226031Sstas    if (dup) {
832226031Sstas	if (strcmp(dup, tl->name) == 0)
833226031Sstas	    errx(1, "found dup of ourself");
834226031Sstas	*dupname = dup;
835226031Sstas    } else {
836226031Sstas	*dupname = tl->name;
837226031Sstas	tlist_print(tl);
838226031Sstas	tlist_add(tl);
839226031Sstas    }
840226031Sstas}
841226031Sstas
842226031Sstas
843226031Sstasvoid
844226031Sstasgenerate_template(const Symbol *s)
845226031Sstas{
846226031Sstas    FILE *f = get_code_file();
847226031Sstas    const char *dupname;
848226031Sstas
849226031Sstas    if (use_extern(s)) {
850226031Sstas	gen_extern_stubs(f, s->gen_name);
851226031Sstas	return;
852226031Sstas    }
853226031Sstas
854226031Sstas    generate_template_type(s->gen_name, &dupname, s->name, s->gen_name, NULL, s->type, 0, 0, 1);
855226031Sstas
856226031Sstas    fprintf(f,
857226031Sstas	    "\n"
858226031Sstas	    "int\n"
859226031Sstas	    "decode_%s(const unsigned char *p, size_t len, %s *data, size_t *size)\n"
860226031Sstas	    "{\n"
861226031Sstas	    "    return _asn1_decode_top(asn1_%s, 0|%s, p, len, data, size);\n"
862226031Sstas	    "}\n"
863226031Sstas	    "\n",
864226031Sstas	    s->gen_name,
865226031Sstas	    s->gen_name,
866226031Sstas	    dupname,
867226031Sstas	    support_ber ? "A1_PF_ALLOW_BER" : "0");
868226031Sstas
869226031Sstas    fprintf(f,
870226031Sstas	    "\n"
871226031Sstas	    "int\n"
872226031Sstas	    "encode_%s(unsigned char *p, size_t len, const %s *data, size_t *size)\n"
873226031Sstas	    "{\n"
874226031Sstas	    "    return _asn1_encode(asn1_%s, p, len, data, size);\n"
875226031Sstas	    "}\n"
876226031Sstas	    "\n",
877226031Sstas	    s->gen_name,
878226031Sstas	    s->gen_name,
879226031Sstas	    dupname);
880226031Sstas
881226031Sstas    fprintf(f,
882226031Sstas	    "\n"
883226031Sstas	    "size_t\n"
884226031Sstas	    "length_%s(const %s *data)\n"
885226031Sstas	    "{\n"
886226031Sstas	    "    return _asn1_length(asn1_%s, data);\n"
887226031Sstas	    "}\n"
888226031Sstas	    "\n",
889226031Sstas	    s->gen_name,
890226031Sstas	    s->gen_name,
891226031Sstas	    dupname);
892226031Sstas
893226031Sstas
894226031Sstas    fprintf(f,
895226031Sstas	    "\n"
896226031Sstas	    "void\n"
897226031Sstas	    "free_%s(%s *data)\n"
898226031Sstas	    "{\n"
899226031Sstas	    "    _asn1_free(asn1_%s, data);\n"
900226031Sstas	    "}\n"
901226031Sstas	    "\n",
902226031Sstas	    s->gen_name,
903226031Sstas	    s->gen_name,
904226031Sstas	    dupname);
905226031Sstas
906226031Sstas    fprintf(f,
907226031Sstas	    "\n"
908226031Sstas	    "int\n"
909226031Sstas	    "copy_%s(const %s *from, %s *to)\n"
910226031Sstas	    "{\n"
911226031Sstas	    "    return _asn1_copy_top(asn1_%s, from, to);\n"
912226031Sstas	    "}\n"
913226031Sstas	    "\n",
914226031Sstas	    s->gen_name,
915226031Sstas	    s->gen_name,
916226031Sstas	    s->gen_name,
917226031Sstas	    dupname);
918226031Sstas}
919