Debian Codegraph @

lib/asn1/asn1parse.y

raw
1 /*
2 * Copyright (c) 1997 - 2007 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 /* $Id$ */
37
38 %{
39
40 #include <config.h>
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <stdint.h>
45 #include <string.h>
46 #include "symbol.h"
47 #include "lex.h"
48 #include "gen_locl.h"
49 #include "der.h"
50
51 static Type *new_type (Typetype t);
52 static struct constraint_spec *new_constraint_spec(enum ctype);
53 static Type *new_tag(int tagclass, int tagvalue, int tagenv, Type *oldtype);
54 void yyerror (const char *);
55 static struct objid *new_objid(const char *label, int value);
56 static void add_oid_to_tail(struct objid *, struct objid *);
57 static void fix_labels(Symbol *s);
58
59 struct string_list {
60 char *string;
61 struct string_list *next;
62 };
63
64 static int default_tag_env = TE_EXPLICIT;
65
66 /* Declarations for Bison */
67 #define YYMALLOC malloc
68 #define YYFREE free
69
70 %}
71
72 %union {
73 int64_t constant;
74 struct value *value;
75 struct range *range;
76 char *name;
77 Type *type;
78 Member *member;
79 struct objid *objid;
80 char *defval;
81 struct string_list *sl;
82 struct tagtype tag;
83 struct memhead *members;
84 struct constraint_spec *constraint_spec;
85 }
86
87 %token kw_ABSENT
88 %token kw_ABSTRACT_SYNTAX
89 %token kw_ALL
90 %token kw_APPLICATION
91 %token kw_AUTOMATIC
92 %token kw_BEGIN
93 %token kw_BIT
94 %token kw_BMPString
95 %token kw_BOOLEAN
96 %token kw_BY
97 %token kw_CHARACTER
98 %token kw_CHOICE
99 %token kw_CLASS
100 %token kw_COMPONENT
101 %token kw_COMPONENTS
102 %token kw_CONSTRAINED
103 %token kw_CONTAINING
104 %token kw_DEFAULT
105 %token kw_DEFINITIONS
106 %token kw_EMBEDDED
107 %token kw_ENCODED
108 %token kw_END
109 %token kw_ENUMERATED
110 %token kw_EXCEPT
111 %token kw_EXPLICIT
112 %token kw_EXPORTS
113 %token kw_EXTENSIBILITY
114 %token kw_EXTERNAL
115 %token kw_FALSE
116 %token kw_FROM
117 %token kw_GeneralString
118 %token kw_GeneralizedTime
119 %token kw_GraphicString
120 %token kw_IA5String
121 %token kw_IDENTIFIER
122 %token kw_IMPLICIT
123 %token kw_IMPLIED
124 %token kw_IMPORTS
125 %token kw_INCLUDES
126 %token kw_INSTANCE
127 %token kw_INTEGER
128 %token kw_INTERSECTION
129 %token kw_ISO646String
130 %token kw_MAX
131 %token kw_MIN
132 %token kw_MINUS_INFINITY
133 %token kw_NULL
134 %token kw_NumericString
135 %token kw_OBJECT
136 %token kw_OCTET
137 %token kw_OF
138 %token kw_OPTIONAL
139 %token kw_ObjectDescriptor
140 %token kw_PATTERN
141 %token kw_PDV
142 %token kw_PLUS_INFINITY
143 %token kw_PRESENT
144 %token kw_PRIVATE
145 %token kw_PrintableString
146 %token kw_REAL
147 %token kw_RELATIVE_OID
148 %token kw_SEQUENCE
149 %token kw_SET
150 %token kw_SIZE
151 %token kw_STRING
152 %token kw_SYNTAX
153 %token kw_T61String
154 %token kw_TAGS
155 %token kw_TRUE
156 %token kw_TYPE_IDENTIFIER
157 %token kw_TeletexString
158 %token kw_UNION
159 %token kw_UNIQUE
160 %token kw_UNIVERSAL
161 %token kw_UTCTime
162 %token kw_UTF8String
163 %token kw_UniversalString
164 %token kw_VideotexString
165 %token kw_VisibleString
166 %token kw_WITH
167
168 %token RANGE
169 %token EEQUAL
170 %token ELLIPSIS
171
172 %token <name> IDENTIFIER referencename
173 %token <name> STRING
174
175 %token <constant> NUMBER
176 %type <constant> SignedNumber
177 %type <constant> Class tagenv
178
179 %type <value> Value
180 %type <value> BuiltinValue
181 %type <value> IntegerValue
182 %type <value> BooleanValue
183 %type <value> ObjectIdentifierValue
184 %type <value> CharacterStringValue
185 %type <value> NullValue
186 %type <value> DefinedValue
187 %type <value> ReferencedValue
188 %type <value> Valuereference
189
190 %type <type> Type
191 %type <type> BuiltinType
192 %type <type> BitStringType
193 %type <type> BooleanType
194 %type <type> ChoiceType
195 %type <type> ConstrainedType
196 %type <type> EnumeratedType
197 %type <type> IntegerType
198 %type <type> NullType
199 %type <type> OctetStringType
200 %type <type> SequenceType
201 %type <type> SequenceOfType
202 %type <type> SetType
203 %type <type> SetOfType
204 %type <type> TaggedType
205 %type <type> ReferencedType
206 %type <type> DefinedType
207 %type <type> UsefulType
208 %type <type> ObjectIdentifierType
209 %type <type> CharacterStringType
210 %type <type> RestrictedCharactedStringType
211
212 %type <tag> Tag
213
214 %type <member> ComponentType
215 %type <member> NamedBit
216 %type <member> NamedNumber
217 %type <member> NamedType
218 %type <members> ComponentTypeList
219 %type <members> Enumerations
220 %type <members> NamedBitList
221 %type <members> NamedNumberList
222
223 %type <objid> objid objid_list objid_element objid_opt
224 %type <range> range size
225
226 %type <sl> referencenames
227
228 %type <constraint_spec> Constraint
229 %type <constraint_spec> ConstraintSpec
230 %type <constraint_spec> GeneralConstraint
231 %type <constraint_spec> ContentsConstraint
232 %type <constraint_spec> UserDefinedConstraint
233
234
235
236 %start ModuleDefinition
237
238 %%
239
240 ModuleDefinition: IDENTIFIER objid_opt kw_DEFINITIONS TagDefault ExtensionDefault
241 EEQUAL kw_BEGIN ModuleBody kw_END
242 {
243 checkundefined();
244 }
245 ;
246
247 TagDefault : kw_EXPLICIT kw_TAGS
248 { default_tag_env = TE_EXPLICIT; }
249 | kw_IMPLICIT kw_TAGS
250 { default_tag_env = TE_IMPLICIT; }
251 | kw_AUTOMATIC kw_TAGS
252 { lex_error_message("automatic tagging is not supported"); }
253 | /* empty */
254 ;
255
256 ExtensionDefault: kw_EXTENSIBILITY kw_IMPLIED
257 { lex_error_message("no extensibility options supported"); }
258 | /* empty */
259 ;
260
261 ModuleBody : Exports Imports AssignmentList
262 | /* empty */
263 ;
264
265 Imports : kw_IMPORTS SymbolsImported ';'
266 | /* empty */
267 ;
268
269 SymbolsImported : SymbolsFromModuleList
270 | /* empty */
271 ;
272
273 SymbolsFromModuleList: SymbolsFromModule
274 | SymbolsFromModuleList SymbolsFromModule
275 ;
276
277 SymbolsFromModule: referencenames kw_FROM IDENTIFIER objid_opt
278 {
279 struct string_list *sl;
280 for(sl = $1; sl != NULL; sl = sl->next) {
281 Symbol *s = addsym(sl->string);
282 s->stype = Stype;
283 gen_template_import(s);
284 }
285 add_import($3);
286 }
287 ;
288
289 Exports : kw_EXPORTS referencenames ';'
290 {
291 struct string_list *sl;
292 for(sl = $2; sl != NULL; sl = sl->next)
293 add_export(sl->string);
294 }
295 | kw_EXPORTS kw_ALL
296 | /* empty */
297 ;
298
299 AssignmentList : Assignment
300 | Assignment AssignmentList
301 ;
302
303 Assignment : TypeAssignment
304 | ValueAssignment
305 ;
306
307 referencenames : IDENTIFIER ',' referencenames
308 {
309 $$ = emalloc(sizeof(*$$));
310 $$->string = $1;
311 $$->next = $3;
312 }
313 | IDENTIFIER
314 {
315 $$ = emalloc(sizeof(*$$));
316 $$->string = $1;
317 $$->next = NULL;
318 }
319 ;
320
321 TypeAssignment : IDENTIFIER EEQUAL Type
322 {
323 Symbol *s = addsym ($1);
324 s->stype = Stype;
325 s->type = $3;
326 fix_labels(s);
327 generate_type (s);
328 }
329 ;
330
331 Type : BuiltinType
332 | ReferencedType
333 | ConstrainedType
334 ;
335
336 BuiltinType : BitStringType
337 | BooleanType
338 | CharacterStringType
339 | ChoiceType
340 | EnumeratedType
341 | IntegerType
342 | NullType
343 | ObjectIdentifierType
344 | OctetStringType
345 | SequenceType
346 | SequenceOfType
347 | SetType
348 | SetOfType
349 | TaggedType
350 ;
351
352 BooleanType : kw_BOOLEAN
353 {
354 $$ = new_tag(ASN1_C_UNIV, UT_Boolean,
355 TE_EXPLICIT, new_type(TBoolean));
356 }
357 ;
358
359 range : '(' Value RANGE Value ')'
360 {
361 if($2->type != integervalue)
362 lex_error_message("Non-integer used in first part of range");
363 if($2->type != integervalue)
364 lex_error_message("Non-integer in second part of range");
365 $$ = ecalloc(1, sizeof(*$$));
366 $$->min = $2->u.integervalue;
367 $$->max = $4->u.integervalue;
368 }
369 | '(' Value RANGE kw_MAX ')'
370 {
371 if($2->type != integervalue)
372 lex_error_message("Non-integer in first part of range");
373 $$ = ecalloc(1, sizeof(*$$));
374 $$->min = $2->u.integervalue;
375 $$->max = INT_MAX;
376 }
377 | '(' kw_MIN RANGE Value ')'
378 {
379 if($4->type != integervalue)
380 lex_error_message("Non-integer in second part of range");
381 $$ = ecalloc(1, sizeof(*$$));
382 $$->min = INT_MIN;
383 $$->max = $4->u.integervalue;
384 }
385 | '(' Value ')'
386 {
387 if($2->type != integervalue)
388 lex_error_message("Non-integer used in limit");
389 $$ = ecalloc(1, sizeof(*$$));
390 $$->min = $2->u.integervalue;
391 $$->max = $2->u.integervalue;
392 }
393 ;
394
395
396 IntegerType : kw_INTEGER
397 {
398 $$ = new_tag(ASN1_C_UNIV, UT_Integer,
399 TE_EXPLICIT, new_type(TInteger));
400 }
401 | kw_INTEGER range
402 {
403 $$ = new_type(TInteger);
404 $$->range = $2;
405 $$ = new_tag(ASN1_C_UNIV, UT_Integer, TE_EXPLICIT, $$);
406 }
407 | kw_INTEGER '{' NamedNumberList '}'
408 {
409 $$ = new_type(TInteger);
410 $$->members = $3;
411 $$ = new_tag(ASN1_C_UNIV, UT_Integer, TE_EXPLICIT, $$);
412 }
413 ;
414
415 NamedNumberList : NamedNumber
416 {
417 $$ = emalloc(sizeof(*$$));
418 ASN1_TAILQ_INIT($$);
419 ASN1_TAILQ_INSERT_HEAD($$, $1, members);
420 }
421 | NamedNumberList ',' NamedNumber
422 {
423 ASN1_TAILQ_INSERT_TAIL($1, $3, members);
424 $$ = $1;
425 }
426 | NamedNumberList ',' ELLIPSIS
427 { $$ = $1; } /* XXX used for Enumerations */
428 ;
429
430 NamedNumber : IDENTIFIER '(' SignedNumber ')'
431 {
432 $$ = emalloc(sizeof(*$$));
433 $$->name = $1;
434 $$->gen_name = estrdup($1);
435 output_name ($$->gen_name);
436 $$->val = $3;
437 $$->optional = 0;
438 $$->ellipsis = 0;
439 $$->type = NULL;
440 }
441 ;
442
443 EnumeratedType : kw_ENUMERATED '{' Enumerations '}'
444 {
445 $$ = new_type(TInteger);
446 $$->members = $3;
447 $$ = new_tag(ASN1_C_UNIV, UT_Enumerated, TE_EXPLICIT, $$);
448 }
449 ;
450
451 Enumerations : NamedNumberList /* XXX */
452 ;
453
454 BitStringType : kw_BIT kw_STRING
455 {
456 $$ = new_type(TBitString);
457 $$->members = emalloc(sizeof(*$$->members));
458 ASN1_TAILQ_INIT($$->members);
459 $$ = new_tag(ASN1_C_UNIV, UT_BitString, TE_EXPLICIT, $$);
460 }
461 | kw_BIT kw_STRING '{' NamedBitList '}'
462 {
463 $$ = new_type(TBitString);
464 $$->members = $4;
465 $$ = new_tag(ASN1_C_UNIV, UT_BitString, TE_EXPLICIT, $$);
466 }
467 ;
468
469 ObjectIdentifierType: kw_OBJECT kw_IDENTIFIER
470 {
471 $$ = new_tag(ASN1_C_UNIV, UT_OID,
472 TE_EXPLICIT, new_type(TOID));
473 }
474 ;
475 OctetStringType : kw_OCTET kw_STRING size
476 {
477 Type *t = new_type(TOctetString);
478 t->range = $3;
479 if (t->range) {
480 if (t->range->min < 0)
481 lex_error_message("can't use a negative SIZE range "
482 "length for OCTET STRING");
483 }
484 $$ = new_tag(ASN1_C_UNIV, UT_OctetString,
485 TE_EXPLICIT, t);
486 }
487 ;
488
489 NullType : kw_NULL
490 {
491 $$ = new_tag(ASN1_C_UNIV, UT_Null,
492 TE_EXPLICIT, new_type(TNull));
493 }
494 ;
495
496 size :
497 { $$ = NULL; }
498 | kw_SIZE range
499 { $$ = $2; }
500 ;
501
502
503 SequenceType : kw_SEQUENCE '{' /* ComponentTypeLists */ ComponentTypeList '}'
504 {
505 $$ = new_type(TSequence);
506 $$->members = $3;
507 $$ = new_tag(ASN1_C_UNIV, UT_Sequence, default_tag_env, $$);
508 }
509 | kw_SEQUENCE '{' '}'
510 {
511 $$ = new_type(TSequence);
512 $$->members = NULL;
513 $$ = new_tag(ASN1_C_UNIV, UT_Sequence, default_tag_env, $$);
514 }
515 ;
516
517 SequenceOfType : kw_SEQUENCE size kw_OF Type
518 {
519 $$ = new_type(TSequenceOf);
520 $$->range = $2;
521 if ($$->range) {
522 if ($$->range->min < 0)
523 lex_error_message("can't use a negative SIZE range "
524 "length for SEQUENCE OF");
525 }
526
527 $$->subtype = $4;
528 $$ = new_tag(ASN1_C_UNIV, UT_Sequence, default_tag_env, $$);
529 }
530 ;
531
532 SetType : kw_SET '{' /* ComponentTypeLists */ ComponentTypeList '}'
533 {
534 $$ = new_type(TSet);
535 $$->members = $3;
536 $$ = new_tag(ASN1_C_UNIV, UT_Set, default_tag_env, $$);
537 }
538 | kw_SET '{' '}'
539 {
540 $$ = new_type(TSet);
541 $$->members = NULL;
542 $$ = new_tag(ASN1_C_UNIV, UT_Set, default_tag_env, $$);
543 }
544 ;
545
546 SetOfType : kw_SET kw_OF Type
547 {
548 $$ = new_type(TSetOf);
549 $$->subtype = $3;
550 $$ = new_tag(ASN1_C_UNIV, UT_Set, default_tag_env, $$);
551 }
552 ;
553
554 ChoiceType : kw_CHOICE '{' /* AlternativeTypeLists */ ComponentTypeList '}'
555 {
556 $$ = new_type(TChoice);
557 $$->members = $3;
558 }
559 ;
560
561 ReferencedType : DefinedType
562 | UsefulType
563 ;
564
565 DefinedType : IDENTIFIER
566 {
567 Symbol *s = addsym($1);
568 $$ = new_type(TType);
569 if(s->stype != Stype && s->stype != SUndefined)
570 lex_error_message ("%s is not a type\n", $1);
571 else
572 $$->symbol = s;
573 }
574 ;
575
576 UsefulType : kw_GeneralizedTime
577 {
578 $$ = new_tag(ASN1_C_UNIV, UT_GeneralizedTime,
579 TE_EXPLICIT, new_type(TGeneralizedTime));
580 }
581 | kw_UTCTime
582 {
583 $$ = new_tag(ASN1_C_UNIV, UT_UTCTime,
584 TE_EXPLICIT, new_type(TUTCTime));
585 }
586 ;
587
588 ConstrainedType : Type Constraint
589 {
590 /* if (Constraint.type == contentConstrant) {
591 assert(Constraint.u.constraint.type == octetstring|bitstring-w/o-NamedBitList); // remember to check type reference too
592 if (Constraint.u.constraint.type) {
593 assert((Constraint.u.constraint.type.length % 8) == 0);
594 }
595 }
596 if (Constraint.u.constraint.encoding) {
597 type == der-oid|ber-oid
598 }
599 */
600 }
601 ;
602
603
604 Constraint : '(' ConstraintSpec ')'
605 {
606 $$ = $2;
607 }
608 ;
609
610 ConstraintSpec : GeneralConstraint
611 ;
612
613 GeneralConstraint: ContentsConstraint
614 | UserDefinedConstraint
615 ;
616
617 ContentsConstraint: kw_CONTAINING Type
618 {
619 $$ = new_constraint_spec(CT_CONTENTS);
620 $$->u.content.type = $2;
621 $$->u.content.encoding = NULL;
622 }
623 | kw_ENCODED kw_BY Value
624 {
625 if ($3->type != objectidentifiervalue)
626 lex_error_message("Non-OID used in ENCODED BY constraint");
627 $$ = new_constraint_spec(CT_CONTENTS);
628 $$->u.content.type = NULL;
629 $$->u.content.encoding = $3;
630 }
631 | kw_CONTAINING Type kw_ENCODED kw_BY Value
632 {
633 if ($5->type != objectidentifiervalue)
634 lex_error_message("Non-OID used in ENCODED BY constraint");
635 $$ = new_constraint_spec(CT_CONTENTS);
636 $$->u.content.type = $2;
637 $$->u.content.encoding = $5;
638 }
639 ;
640
641 UserDefinedConstraint: kw_CONSTRAINED kw_BY '{' '}'
642 {
643 $$ = new_constraint_spec(CT_USER);
644 }
645 ;
646
647 TaggedType : Tag tagenv Type
648 {
649 $$ = new_type(TTag);
650 $$->tag = $1;
651 $$->tag.tagenv = $2;
652 if (template_flag) {
653 $$->subtype = $3;
654 } else {
655 if($3->type == TTag && $2 == TE_IMPLICIT) {
656 $$->subtype = $3->subtype;
657 free($3);
658 } else {
659 $$->subtype = $3;
660 }
661 }
662 }
663 ;
664
665 Tag : '[' Class NUMBER ']'
666 {
667 $$.tagclass = $2;
668 $$.tagvalue = $3;
669 $$.tagenv = default_tag_env;
670 }
671 ;
672
673 Class : /* */
674 {
675 $$ = ASN1_C_CONTEXT;
676 }
677 | kw_UNIVERSAL
678 {
679 $$ = ASN1_C_UNIV;
680 }
681 | kw_APPLICATION
682 {
683 $$ = ASN1_C_APPL;
684 }
685 | kw_PRIVATE
686 {
687 $$ = ASN1_C_PRIVATE;
688 }
689 ;
690
691 tagenv : /* */
692 {
693 $$ = default_tag_env;
694 }
695 | kw_EXPLICIT
696 {
697 $$ = default_tag_env;
698 }
699 | kw_IMPLICIT
700 {
701 $$ = TE_IMPLICIT;
702 }
703 ;
704
705
706 ValueAssignment : IDENTIFIER Type EEQUAL Value
707 {
708 Symbol *s;
709 s = addsym ($1);
710
711 s->stype = SValue;
712 s->value = $4;
713 generate_constant (s);
714 }
715 ;
716
717 CharacterStringType: RestrictedCharactedStringType
718 ;
719
720 RestrictedCharactedStringType: kw_GeneralString
721 {
722 $$ = new_tag(ASN1_C_UNIV, UT_GeneralString,
723 TE_EXPLICIT, new_type(TGeneralString));
724 }
725 | kw_TeletexString
726 {
727 $$ = new_tag(ASN1_C_UNIV, UT_TeletexString,
728 TE_EXPLICIT, new_type(TTeletexString));
729 }
730 | kw_UTF8String
731 {
732 $$ = new_tag(ASN1_C_UNIV, UT_UTF8String,
733 TE_EXPLICIT, new_type(TUTF8String));
734 }
735 | kw_PrintableString
736 {
737 $$ = new_tag(ASN1_C_UNIV, UT_PrintableString,
738 TE_EXPLICIT, new_type(TPrintableString));
739 }
740 | kw_VisibleString
741 {
742 $$ = new_tag(ASN1_C_UNIV, UT_VisibleString,
743 TE_EXPLICIT, new_type(TVisibleString));
744 }
745 | kw_IA5String
746 {
747 $$ = new_tag(ASN1_C_UNIV, UT_IA5String,
748 TE_EXPLICIT, new_type(TIA5String));
749 }
750 | kw_BMPString
751 {
752 $$ = new_tag(ASN1_C_UNIV, UT_BMPString,
753 TE_EXPLICIT, new_type(TBMPString));
754 }
755 | kw_UniversalString
756 {
757 $$ = new_tag(ASN1_C_UNIV, UT_UniversalString,
758 TE_EXPLICIT, new_type(TUniversalString));
759 }
760
761 ;
762
763 ComponentTypeList: ComponentType
764 {
765 $$ = emalloc(sizeof(*$$));
766 ASN1_TAILQ_INIT($$);
767 ASN1_TAILQ_INSERT_HEAD($$, $1, members);
768 }
769 | ComponentTypeList ',' ComponentType
770 {
771 ASN1_TAILQ_INSERT_TAIL($1, $3, members);
772 $$ = $1;
773 }
774 | ComponentTypeList ',' ELLIPSIS
775 {
776 struct member *m = ecalloc(1, sizeof(*m));
777 m->name = estrdup("...");
778 m->gen_name = estrdup("asn1_ellipsis");
779 m->ellipsis = 1;
780 ASN1_TAILQ_INSERT_TAIL($1, m, members);
781 $$ = $1;
782 }
783 ;
784
785 NamedType : IDENTIFIER Type
786 {
787 $$ = emalloc(sizeof(*$$));
788 $$->name = $1;
789 $$->gen_name = estrdup($1);
790 output_name ($$->gen_name);
791 $$->type = $2;
792 $$->ellipsis = 0;
793 }
794 ;
795
796 ComponentType : NamedType
797 {
798 $$ = $1;
799 $$->optional = 0;
800 $$->defval = NULL;
801 }
802 | NamedType kw_OPTIONAL
803 {
804 $$ = $1;
805 $$->optional = 1;
806 $$->defval = NULL;
807 }
808 | NamedType kw_DEFAULT Value
809 {
810 $$ = $1;
811 $$->optional = 0;
812 $$->defval = $3;
813 }
814 ;
815
816 NamedBitList : NamedBit
817 {
818 $$ = emalloc(sizeof(*$$));
819 ASN1_TAILQ_INIT($$);
820 ASN1_TAILQ_INSERT_HEAD($$, $1, members);
821 }
822 | NamedBitList ',' NamedBit
823 {
824 ASN1_TAILQ_INSERT_TAIL($1, $3, members);
825 $$ = $1;
826 }
827 ;
828
829 NamedBit : IDENTIFIER '(' NUMBER ')'
830 {
831 $$ = emalloc(sizeof(*$$));
832 $$->name = $1;
833 $$->gen_name = estrdup($1);
834 output_name ($$->gen_name);
835 $$->val = $3;
836 $$->optional = 0;
837 $$->ellipsis = 0;
838 $$->type = NULL;
839 }
840 ;
841
842 objid_opt : objid
843 | /* empty */ { $$ = NULL; }
844 ;
845
846 objid : '{' objid_list '}'
847 {
848 $$ = $2;
849 }
850 ;
851
852 objid_list : /* empty */
853 {
854 $$ = NULL;
855 }
856 | objid_element objid_list
857 {
858 if ($2) {
859 $$ = $2;
860 add_oid_to_tail($2, $1);
861 } else {
862 $$ = $1;
863 }
864 }
865 ;
866
867 objid_element : IDENTIFIER '(' NUMBER ')'
868 {
869 $$ = new_objid($1, $3);
870 }
871 | IDENTIFIER
872 {
873 Symbol *s = addsym($1);
874 if(s->stype != SValue ||
875 s->value->type != objectidentifiervalue) {
876 lex_error_message("%s is not an object identifier\n",
877 s->name);
878 exit(1);
879 }
880 $$ = s->value->u.objectidentifiervalue;
881 }
882 | NUMBER
883 {
884 $$ = new_objid(NULL, $1);
885 }
886 ;
887
888 Value : BuiltinValue
889 | ReferencedValue
890 ;
891
892 BuiltinValue : BooleanValue
893 | CharacterStringValue
894 | IntegerValue
895 | ObjectIdentifierValue
896 | NullValue
897 ;
898
899 ReferencedValue : DefinedValue
900 ;
901
902 DefinedValue : Valuereference
903 ;
904
905 Valuereference : IDENTIFIER
906 {
907 Symbol *s = addsym($1);
908 if(s->stype != SValue)
909 lex_error_message ("%s is not a value\n",
910 s->name);
911 else
912 $$ = s->value;
913 }
914 ;
915
916 CharacterStringValue: STRING
917 {
918 $$ = emalloc(sizeof(*$$));
919 $$->type = stringvalue;
920 $$->u.stringvalue = $1;
921 }
922 ;
923
924 BooleanValue : kw_TRUE
925 {
926 $$ = emalloc(sizeof(*$$));
927 $$->type = booleanvalue;
928 $$->u.booleanvalue = 0;
929 }
930 | kw_FALSE
931 {
932 $$ = emalloc(sizeof(*$$));
933 $$->type = booleanvalue;
934 $$->u.booleanvalue = 0;
935 }
936 ;
937
938 IntegerValue : SignedNumber
939 {
940 $$ = emalloc(sizeof(*$$));
941 $$->type = integervalue;
942 $$->u.integervalue = $1;
943 }
944 ;
945
946 SignedNumber : NUMBER
947 ;
948
949 NullValue : kw_NULL
950 {
951 }
952 ;
953
954 ObjectIdentifierValue: objid
955 {
956 $$ = emalloc(sizeof(*$$));
957 $$->type = objectidentifiervalue;
958 $$->u.objectidentifiervalue = $1;
959 }
960 ;
961
962 %%
963
964 void
965 yyerror (const char *s)
966 {
967 lex_error_message ("%s\n", s);
968 }
969
970 static Type *
971 new_tag(int tagclass, int tagvalue, int tagenv, Type *oldtype)
972 {
973 Type *t;
974 if(oldtype->type == TTag && oldtype->tag.tagenv == TE_IMPLICIT) {
975 t = oldtype;
976 oldtype = oldtype->subtype; /* XXX */
977 } else
978 t = new_type (TTag);
979
980 t->tag.tagclass = tagclass;
981 t->tag.tagvalue = tagvalue;
982 t->tag.tagenv = tagenv;
983 t->subtype = oldtype;
984 return t;
985 }
986
987 static struct objid *
988 new_objid(const char *label, int value)
989 {
990 struct objid *s;
991 s = emalloc(sizeof(*s));
992 s->label = label;
993 s->value = value;
994 s->next = NULL;
995 return s;
996 }
997
998 static void
999 add_oid_to_tail(struct objid *head, struct objid *tail)
1000 {
1001 struct objid *o;
1002 o = head;
1003 while (o->next)
1004 o = o->next;
1005 o->next = tail;
1006 }
1007
1008 static unsigned long idcounter;
1009
1010 static Type *
1011 new_type (Typetype tt)
1012 {
1013 Type *t = ecalloc(1, sizeof(*t));
1014 t->type = tt;
1015 t->id = idcounter++;
1016 return t;
1017 }
1018
1019 static struct constraint_spec *
1020 new_constraint_spec(enum ctype ct)
1021 {
1022 struct constraint_spec *c = ecalloc(1, sizeof(*c));
1023 c->ctype = ct;
1024 return c;
1025 }
1026
1027 static void fix_labels2(Type *t, const char *prefix);
1028 static void fix_labels1(struct memhead *members, const char *prefix)
1029 {
1030 Member *m;
1031
1032 if(members == NULL)
1033 return;
1034 ASN1_TAILQ_FOREACH(m, members, members) {
1035 if (asprintf(&m->label, "%s_%s", prefix, m->gen_name) < 0)
1036 errx(1, "malloc");
1037 if (m->label == NULL)
1038 errx(1, "malloc");
1039 if(m->type != NULL)
1040 fix_labels2(m->type, m->label);
1041 }
1042 }
1043
1044 static void fix_labels2(Type *t, const char *prefix)
1045 {
1046 for(; t; t = t->subtype)
1047 fix_labels1(t->members, prefix);
1048 }
1049
1050 static void
1051 fix_labels(Symbol *s)
1052 {
1053 char *p = NULL;
1054 if (asprintf(&p, "choice_%s", s->gen_name) < 0 || p == NULL)
1055 errx(1, "malloc");
1056 fix_labels2(s->type, p);
1057 free(p);
1058 }