Remove building with NOCRYPTO option
[minix.git] / crypto / external / bsd / heimdal / dist / lib / asn1 / gen_template.c
blobfb6a8943ad3dc44352ba4022da97439a0586f742
1 /* $NetBSD: gen_template.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
3 /*
4 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
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
12 * are met:
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
35 * SUCH DAMAGE.
38 #include "gen_locl.h"
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);
44 static const char *
45 ttype_symbol(const char *basename, const Type *t)
47 return t->symbol->gen_name;
50 static const char *
51 integer_symbol(const char *basename, const Type *t)
53 if (t->members)
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)
58 return "int";
59 else if (t->range->min == 0 && t->range->max == UINT_MAX)
60 return "unsigned";
61 else if (t->range->min == 0 && t->range->max == INT_MAX)
62 return "unsigned";
63 else {
64 abort();
65 UNREACHABLE(return NULL);
69 static const char *
70 boolean_symbol(const char *basename, const Type *t)
72 return "int";
76 static const char *
77 octetstring_symbol(const char *basename, const Type *t)
79 return "heim_octet_string";
82 static const char *
83 sequence_symbol(const char *basename, const Type *t)
85 return basename;
88 static const char *
89 time_symbol(const char *basename, const Type *t)
91 return "time_t";
94 static const char *
95 tag_symbol(const char *basename, const Type *t)
97 return symbol_name(basename, t->subtype);
100 static const char *
101 generalstring_symbol(const char *basename, const Type *t)
103 return "heim_general_string";
106 static const char *
107 printablestring_symbol(const char *basename, const Type *t)
109 return "heim_printable_string";
112 static const char *
113 ia5string_symbol(const char *basename, const Type *t)
115 return "heim_ia5_string";
118 static const char *
119 visiblestring_symbol(const char *basename, const Type *t)
121 return "heim_visible_string";
124 static const char *
125 utf8string_symbol(const char *basename, const Type *t)
127 return "heim_utf8_string";
130 static const char *
131 bmpstring_symbol(const char *basename, const Type *t)
133 return "heim_bmp_string";
136 static const char *
137 universalstring_symbol(const char *basename, const Type *t)
139 return "heim_universal_string";
142 static const char *
143 oid_symbol(const char *basename, const Type *t)
145 return "heim_oid";
148 static const char *
149 bitstring_symbol(const char *basename, const Type *t)
151 if (t->members)
152 return basename;
153 return "heim_bit_string";
158 struct {
159 enum typetype type;
160 const char *(*symbol_name)(const char *, const Type *);
161 int is_struct;
162 } types[] = {
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 }
186 static FILE *
187 get_code_file(void)
189 if (!one_code_file)
190 return templatefile;
191 return codefile;
195 static int
196 is_supported_type_p(const Type *t)
198 size_t i;
200 for (i = 0; i < sizeof(types)/sizeof(types[0]); i++)
201 if (t->type == types[i].type)
202 return 1;
203 return 0;
207 is_template_compat (const Symbol *s)
209 return is_supported_type_p(s->type);
212 static const char *
213 symbol_name(const char *basename, const Type *t)
215 size_t i;
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);
221 exit(1);
225 static char *
226 partial_offset(const char *basetype, const char *name, int need_offset)
228 char *str;
229 if (name == NULL || need_offset == 0)
230 return strdup("0");
231 if (asprintf(&str, "offsetof(struct %s, %s)", basetype, name) < 0 || str == NULL)
232 errx(1, "malloc");
233 return str;
236 struct template {
237 char *line;
238 char *tt;
239 char *offset;
240 char *ptr;
241 ASN1_TAILQ_ENTRY(template) members;
244 ASN1_TAILQ_HEAD(templatehead, template);
246 struct tlist {
247 char *name;
248 char *header;
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);
273 return tl;
276 static void
277 tlist_header(struct tlist *t, const char *fmt, ...)
279 va_list ap;
280 va_start(ap, fmt);
281 if (vasprintf(&t->header, fmt, ap) < 0 || t->header == NULL)
282 errx(1, "malloc");
283 va_end(ap);
286 static unsigned long
287 tlist_count(struct tlist *tl)
289 unsigned int count = 0;
290 struct template *q;
292 ASN1_TAILQ_FOREACH(q, &tl->template, members) {
293 count++;
295 return count;
298 static void
299 tlist_add(struct tlist *tl)
301 ASN1_TAILQ_INSERT_TAIL(&tlistmaster, tl, tmembers);
304 static void
305 tlist_print(struct tlist *tl)
307 struct template *q;
308 unsigned int i = 1;
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 ? "" : ",");
317 fprintf(f, "};\n");
320 static struct tlist *
321 tlist_find_by_name(const char *name)
323 struct tlist *ql;
324 ASN1_TAILQ_FOREACH(ql, &tlistmaster, tmembers) {
325 if (strcmp(ql->name, name) == 0)
326 return ql;
328 return NULL;
331 static int
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);
339 static int
340 tlist_cmp(const struct tlist *tl, const struct tlist *ql)
342 int ret;
343 struct template *t, *q;
345 ret = strcmp(tl->header, ql->header);
346 if (ret) return ret;
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);
354 if (ret) return ret;
355 } else {
356 ret = strcmp(t->tt, q->tt);
357 if (ret) return ret;
359 ret = strcmp(t->offset, q->offset);
360 if (ret) return ret;
362 if ((ret = strcmp(t->ptr, q->ptr)) != 0 ||
363 (ret = tlist_cmp_name(t->ptr, q->ptr)) != 0)
364 return ret;
366 q = ASN1_TAILQ_NEXT(q, members);
368 if (q != NULL) return -1;
369 return 0;
373 static const char *
374 tlist_find_dup(const struct tlist *tl)
376 struct tlist *ql;
378 ASN1_TAILQ_FOREACH(ql, &tlistmaster, tmembers) {
379 if (tlist_cmp(ql, tl) == 0) {
380 numdups++;
381 return ql->name;
384 return NULL;
392 static struct template *
393 add_line(struct templatehead *t, const char *fmt, ...)
395 struct template *q = calloc(1, sizeof(*q));
396 va_list ap;
397 va_start(ap, fmt);
398 if (vasprintf(&q->line, fmt, ap) < 0 || q->line == NULL)
399 errx(1, "malloc");
400 va_end(ap);
401 ASN1_TAILQ_INSERT_TAIL(t, q, members);
402 return q;
405 static void
406 add_line_pointer(struct templatehead *t,
407 const char *ptr,
408 const char *offset,
409 const char *ttfmt,
410 ...)
412 struct template *q;
413 va_list ap;
414 char *tt = NULL;
416 va_start(ap, ttfmt);
417 if (vasprintf(&tt, ttfmt, ap) < 0 || tt == NULL)
418 errx(1, "malloc");
419 va_end(ap);
421 q = add_line(t, "{ %s, %s, asn1_%s }", tt, offset, ptr);
422 q->tt = tt;
423 q->offset = strdup(offset);
424 q->ptr = strdup(ptr);
427 static int
428 use_extern(const Symbol *s)
430 if (s->type == NULL)
431 return 1;
432 return 0;
435 static int
436 is_struct(Type *t, int isstruct)
438 size_t i;
440 if (t->type == TType)
441 return 0;
442 if (t->type == TSequence || t->type == TSet || t->type == TChoice)
443 return 1;
444 if (t->type == TTag)
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)
450 return 0;
451 else
452 break;
456 return isstruct;
459 static const Type *
460 compact_tag(const Type *t)
462 while (t->type == TTag)
463 t = t->subtype;
464 return t;
467 static void
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);
477 switch (t->type) {
478 case TType:
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);
483 } else {
484 add_line_pointer(temp, t->symbol->gen_name, poffset,
485 "A1_OP_TYPE %s", optional ? "|A1_FLAG_OPTIONAL" : "");
487 break;
488 case TInteger: {
489 char *itype = NULL;
491 if (t->members)
492 itype = "IMEMBER";
493 else if (t->range == NULL)
494 itype = "HEIM_INTEGER";
495 else if (t->range->min == INT_MIN && t->range->max == INT_MAX)
496 itype = "INTEGER";
497 else if (t->range->min == 0 && t->range->max == UINT_MAX)
498 itype = "UNSIGNED";
499 else if (t->range->min == 0 && t->range->max == INT_MAX)
500 itype = "UNSIGNED";
501 else
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);
506 break;
508 case TGeneralString:
509 add_line(temp, "{ A1_PARSE_T(A1T_GENERAL_STRING), %s, NULL }", poffset);
510 break;
511 case TTeletexString:
512 add_line(temp, "{ A1_PARSE_T(A1T_TELETEX_STRING), %s, NULL }", poffset);
513 break;
514 case TPrintableString:
515 add_line(temp, "{ A1_PARSE_T(A1T_PRINTABLE_STRING), %s, NULL }", poffset);
516 break;
517 case TOctetString:
518 add_line(temp, "{ A1_PARSE_T(A1T_OCTET_STRING), %s, NULL }", poffset);
519 break;
520 case TIA5String:
521 add_line(temp, "{ A1_PARSE_T(A1T_IA5_STRING), %s, NULL }", poffset);
522 break;
523 case TBMPString:
524 add_line(temp, "{ A1_PARSE_T(A1T_BMP_STRING), %s, NULL }", poffset);
525 break;
526 case TUniversalString:
527 add_line(temp, "{ A1_PARSE_T(A1T_UNIVERSAL_STRING), %s, NULL }", poffset);
528 break;
529 case TVisibleString:
530 add_line(temp, "{ A1_PARSE_T(A1T_VISIBLE_STRING), %s, NULL }", poffset);
531 break;
532 case TUTF8String:
533 add_line(temp, "{ A1_PARSE_T(A1T_UTF8_STRING), %s, NULL }", poffset);
534 break;
535 case TGeneralizedTime:
536 add_line(temp, "{ A1_PARSE_T(A1T_GENERALIZED_TIME), %s, NULL }", poffset);
537 break;
538 case TUTCTime:
539 add_line(temp, "{ A1_PARSE_T(A1T_UTC_TIME), %s, NULL }", poffset);
540 break;
541 case TBoolean:
542 add_line(temp, "{ A1_PARSE_T(A1T_BOOLEAN), %s, NULL }", poffset);
543 break;
544 case TOID:
545 add_line(temp, "{ A1_PARSE_T(A1T_OID), %s, NULL }", poffset);
546 break;
547 case TNull:
548 break;
549 case TBitString: {
550 struct templatehead template = ASN1_TAILQ_HEAD_INITIALIZER(template);
551 struct template *q;
552 Member *m;
553 size_t count = 0, i;
554 char *bname = NULL;
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);
559 break;
562 if (asprintf(&bname, "bmember_%s_%p", name ? name : "", t) < 0 || bname == NULL)
563 errx(1, "malloc");
564 output_name(bname);
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) {
571 count++;
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);
578 i = 1;
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 ? "" : ",");
583 fprintf(f, "};\n");
585 add_line(temp, "{ A1_OP_BMEMBER, %s, asn1_%s_%s }", poffset, basetype, bname);
587 free(bname);
589 break;
591 case TSequence: {
592 Member *m;
594 ASN1_TAILQ_FOREACH(m, t->members, members) {
595 char *newbasename = NULL;
597 if (m->ellipsis)
598 continue;
600 if (name) {
601 if (asprintf(&newbasename, "%s_%s", basetype, name) < 0)
602 errx(1, "malloc");
603 } else
604 newbasename = strdup(basetype);
605 if (newbasename == NULL)
606 errx(1, "malloc");
608 template_members(temp, newbasename, m->gen_name, m->type, m->optional, isstruct, 1);
610 free(newbasename);
613 break;
615 case TTag: {
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)
621 sename = basetype;
622 else
623 sename = symbol_name(basetype, t->subtype);
625 if (asprintf(&tname, "tag_%s_%p", name ? name : "", t) < 0 || tname == NULL)
626 errx(1, "malloc");
627 output_name(tname);
629 if (asprintf(&elname, "%s_%s", basetype, tname) < 0 || elname == NULL)
630 errx(1, "malloc");
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" : "");
642 free(tname);
643 free(elname);
645 break;
647 case TSetOf:
648 case TSequenceOf: {
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)
656 errx(1, "malloc");
657 } else if (subtype_is_struct) {
658 tname = "seofTstruct";
659 if (asprintf(&sename, "%s_val", symbol_name(basetype, t->subtype)) < 0)
660 errx(1, "malloc");
661 } else {
662 if (name)
663 tname = name;
664 else
665 tname = "seofTstruct";
666 sename = strdup(symbol_name(basetype, t->subtype));
668 if (sename == NULL)
669 errx(1, "malloc");
671 if (t->type == TSetOf) type = "A1_OP_SETOF";
672 else if (t->type == TSequenceOf) type = "A1_OP_SEQOF";
673 else abort();
675 if (asprintf(&elname, "%s_%s_%p", basetype, tname, t) < 0 || elname == NULL)
676 errx(1, "malloc");
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);
682 free(sename);
683 break;
685 case TChoice: {
686 struct templatehead template = ASN1_TAILQ_HEAD_INITIALIZER(template);
687 struct template *q;
688 size_t count = 0, i;
689 char *tname = NULL;
690 FILE *f = get_code_file();
691 Member *m;
692 int ellipsis = 0;
693 char *e;
695 if (asprintf(&tname, "asn1_choice_%s_%s%x",
696 basetype, name ? name : "", (unsigned int)(uintptr_t)t) < 0 || tname == NULL)
697 errx(1, "malloc");
699 ASN1_TAILQ_FOREACH(m, t->members, members) {
700 const char *dupname;
701 char *elname = NULL;
702 char *newbasename = NULL;
703 int subtype_is_struct;
705 if (m->ellipsis) {
706 ellipsis = 1;
707 continue;
710 subtype_is_struct = is_struct(m->type, 0);
712 if (asprintf(&elname, "%s_choice_%s", basetype, m->gen_name) < 0 || elname == NULL)
713 errx(1, "malloc");
715 if (subtype_is_struct) {
716 if (asprintf(&newbasename, "%s_%s", basetype, m->gen_name) < 0)
717 errx(1, "malloc");
718 } else
719 newbasename = strdup(basetype);
721 if (newbasename == NULL)
722 errx(1, "malloc");
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,
732 dupname);
734 free(elname);
735 free(newbasename);
738 e = NULL;
739 if (ellipsis) {
740 if (asprintf(&e, "offsetof(%s%s, u.asn1_ellipsis)", isstruct ? "struct " : "", basetype) < 0 || e == NULL)
741 errx(1, "malloc");
744 ASN1_TAILQ_FOREACH(q, &template, members) {
745 count++;
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);
751 i = 1;
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 ? "" : ",");
756 fprintf(f, "};\n");
758 add_line(temp, "{ A1_OP_CHOICE, %s, %s }", poffset, tname);
760 free(e);
761 free(tname);
762 break;
764 default:
765 abort ();
767 if (poffset)
768 free(poffset);
771 static void
772 gen_extern_stubs(FILE *f, const char *name)
774 fprintf(f,
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"
781 "\tsizeof(%s)\n"
782 "};\n",
783 name, name, name, name,
784 name, name, name);
787 void
788 gen_template_import(const Symbol *s)
790 FILE *f = get_code_file();
792 if (template_flag == 0)
793 return;
795 gen_extern_stubs(f, s->gen_name);
798 static void
799 generate_template_type(const char *varname,
800 const char **dupname,
801 const char *symname,
802 const char *basetype,
803 const char *name,
804 Type *type,
805 int optional, int isstruct, int need_offset)
807 struct tlist *tl;
808 const char *dup;
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) {
817 Member *m;
818 ASN1_TAILQ_FOREACH(m, type->members, members) {
819 if (m->ellipsis)
820 have_ellipsis = 1;
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);
833 if (dup) {
834 if (strcmp(dup, tl->name) == 0)
835 errx(1, "found dup of ourself");
836 *dupname = dup;
837 } else {
838 *dupname = tl->name;
839 tlist_print(tl);
840 tlist_add(tl);
845 void
846 generate_template(const Symbol *s)
848 FILE *f = get_code_file();
849 const char *dupname;
851 if (use_extern(s)) {
852 gen_extern_stubs(f, s->gen_name);
853 return;
856 generate_template_type(s->gen_name, &dupname, s->name, s->gen_name, NULL, s->type, 0, 0, 1);
858 fprintf(f,
859 "\n"
860 "int\n"
861 "decode_%s(const unsigned char *p, size_t len, %s *data, size_t *size)\n"
862 "{\n"
863 " return _asn1_decode_top(asn1_%s, 0|%s, p, len, data, size);\n"
864 "}\n"
865 "\n",
866 s->gen_name,
867 s->gen_name,
868 dupname,
869 support_ber ? "A1_PF_ALLOW_BER" : "0");
871 fprintf(f,
872 "\n"
873 "int\n"
874 "encode_%s(unsigned char *p, size_t len, const %s *data, size_t *size)\n"
875 "{\n"
876 " return _asn1_encode(asn1_%s, p, len, data, size);\n"
877 "}\n"
878 "\n",
879 s->gen_name,
880 s->gen_name,
881 dupname);
883 fprintf(f,
884 "\n"
885 "size_t\n"
886 "length_%s(const %s *data)\n"
887 "{\n"
888 " return _asn1_length(asn1_%s, data);\n"
889 "}\n"
890 "\n",
891 s->gen_name,
892 s->gen_name,
893 dupname);
896 fprintf(f,
897 "\n"
898 "void\n"
899 "free_%s(%s *data)\n"
900 "{\n"
901 " _asn1_free(asn1_%s, data);\n"
902 "}\n"
903 "\n",
904 s->gen_name,
905 s->gen_name,
906 dupname);
908 fprintf(f,
909 "\n"
910 "int\n"
911 "copy_%s(const %s *from, %s *to)\n"
912 "{\n"
913 " return _asn1_copy_top(asn1_%s, from, to);\n"
914 "}\n"
915 "\n",
916 s->gen_name,
917 s->gen_name,
918 s->gen_name,
919 dupname);