Debian Codegraph @

libasn1parser/asn1p_y.y

raw
1 %{
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <assert.h>
8
9 #include "asn1parser.h"
10
11 #define YYPARSE_PARAM param
12 #define YYPARSE_PARAM_TYPE void **
13 #define YYERROR_VERBOSE
14
15 int yylex(void);
16 int yyerror(const char *msg);
17 #ifdef YYBYACC
18 int yyparse(void **param); /* byacc does not produce a prototype */
19 #endif
20 void asn1p_lexer_hack_push_opaque_state(void);
21 void asn1p_lexer_hack_enable_with_syntax(void);
22 void asn1p_lexer_hack_push_encoding_control(void);
23 #define yylineno asn1p_lineno
24 extern int asn1p_lineno;
25
26 /*
27 * Process directives as <ASN1C:RepresentAsPointer>
28 */
29 extern int asn1p_as_pointer;
30
31 /*
32 * This temporary variable is used to solve the shortcomings of 1-lookahead
33 * parser.
34 */
35 static struct AssignedIdentifier *saved_aid;
36
37 static asn1p_value_t *_convert_bitstring2binary(char *str, int base);
38 static void _fixup_anonymous_identifier(asn1p_expr_t *expr);
39
40 static asn1p_module_t *currentModule;
41 #define NEW_EXPR() (asn1p_expr_new(yylineno, currentModule))
42
43 #define checkmem(ptr) do { \
44 if(!(ptr)) \
45 return yyerror("Memory failure"); \
46 } while(0)
47
48 #define CONSTRAINT_INSERT(root, constr_type, arg1, arg2) do { \
49 if(arg1->type != constr_type) { \
50 int __ret; \
51 root = asn1p_constraint_new(yylineno); \
52 checkmem(root); \
53 root->type = constr_type; \
54 __ret = asn1p_constraint_insert(root, \
55 arg1); \
56 checkmem(__ret == 0); \
57 } else { \
58 root = arg1; \
59 } \
60 if(arg2) { \
61 int __ret \
62 = asn1p_constraint_insert(root, arg2); \
63 checkmem(__ret == 0); \
64 } \
65 } while(0)
66
67 #ifdef AL_IMPORT
68 #error AL_IMPORT DEFINED ELSEWHERE!
69 #endif
70 #define AL_IMPORT(to,where,from,field) do { \
71 if(!(from)) break; \
72 while(TQ_FIRST(&((from)->where))) { \
73 TQ_ADD(&((to)->where), \
74 TQ_REMOVE(&((from)->where), field), \
75 field); \
76 } \
77 assert(TQ_FIRST(&((from)->where)) == 0); \
78 } while(0)
79
80 %}
81
82
83 /*
84 * Token value definition.
85 * a_*: ASN-specific types.
86 * tv_*: Locally meaningful types.
87 */
88 %union {
89 asn1p_t *a_grammar;
90 asn1p_module_flags_e a_module_flags;
91 asn1p_module_t *a_module;
92 asn1p_expr_type_e a_type; /* ASN.1 Type */
93 asn1p_expr_t *a_expr; /* Constructed collection */
94 asn1p_constraint_t *a_constr; /* Constraint */
95 enum asn1p_constraint_type_e a_ctype;/* Constraint type */
96 asn1p_xports_t *a_xports; /* IMports/EXports */
97 struct AssignedIdentifier a_aid; /* Assigned Identifier */
98 asn1p_oid_t *a_oid; /* Object Identifier */
99 asn1p_oid_arc_t a_oid_arc; /* Single OID's arc */
100 struct asn1p_type_tag_s a_tag; /* A tag */
101 asn1p_ref_t *a_ref; /* Reference to custom type */
102 asn1p_wsyntx_t *a_wsynt; /* WITH SYNTAX contents */
103 asn1p_wsyntx_chunk_t *a_wchunk; /* WITH SYNTAX chunk */
104 struct asn1p_ref_component_s a_refcomp; /* Component of a reference */
105 asn1p_value_t *a_value; /* Number, DefinedValue, etc */
106 struct asn1p_param_s a_parg; /* A parameter argument */
107 asn1p_paramlist_t *a_plist; /* A pargs list */
108 struct asn1p_expr_marker_s a_marker; /* OPTIONAL/DEFAULT */
109 enum asn1p_constr_pres_e a_pres; /* PRESENT/ABSENT/OPTIONAL */
110 asn1c_integer_t a_int;
111 double a_dbl;
112 char *tv_str;
113 struct {
114 char *buf;
115 int len;
116 } tv_opaque;
117 struct {
118 char *name;
119 struct asn1p_type_tag_s tag;
120 } tv_nametag;
121 };
122
123 /*
124 * Token types returned by scanner.
125 */
126 %token TOK_PPEQ /* "::=", Pseudo Pascal EQuality */
127 %token TOK_VBracketLeft TOK_VBracketRight /* "[[", "]]" */
128 %token <tv_opaque> TOK_whitespace /* A span of whitespace */
129 %token <tv_opaque> TOK_opaque /* opaque data (driven from .y) */
130 %token <tv_str> TOK_bstring
131 %token <tv_opaque> TOK_cstring
132 %token <tv_str> TOK_hstring
133 %token <tv_str> TOK_identifier
134 %token <a_int> TOK_number
135 %token <a_int> TOK_number_negative
136 %token <a_dbl> TOK_realnumber
137 %token <a_int> TOK_tuple
138 %token <a_int> TOK_quadruple
139 %token <tv_str> TOK_typereference
140 %token <tv_str> TOK_capitalreference /* "CLASS1" */
141 %token <tv_str> TOK_typefieldreference /* "&Pork" */
142 %token <tv_str> TOK_valuefieldreference /* "&id" */
143 %token <tv_str> TOK_Literal /* "BY" */
144
145 /*
146 * Token types representing ASN.1 standard keywords.
147 */
148 %token TOK_ABSENT
149 %token TOK_ABSTRACT_SYNTAX
150 %token TOK_ALL
151 %token TOK_ANY
152 %token TOK_APPLICATION
153 %token TOK_AUTOMATIC
154 %token TOK_BEGIN
155 %token TOK_BIT
156 %token TOK_BMPString
157 %token TOK_BOOLEAN
158 %token TOK_BY
159 %token TOK_CHARACTER
160 %token TOK_CHOICE
161 %token TOK_CLASS
162 %token TOK_COMPONENT
163 %token TOK_COMPONENTS
164 %token TOK_CONSTRAINED
165 %token TOK_CONTAINING
166 %token TOK_DEFAULT
167 %token TOK_DEFINITIONS
168 %token TOK_DEFINED
169 %token TOK_EMBEDDED
170 %token TOK_ENCODED
171 %token TOK_ENCODING_CONTROL
172 %token TOK_END
173 %token TOK_ENUMERATED
174 %token TOK_EXPLICIT
175 %token TOK_EXPORTS
176 %token TOK_EXTENSIBILITY
177 %token TOK_EXTERNAL
178 %token TOK_FALSE
179 %token TOK_FROM
180 %token TOK_GeneralizedTime
181 %token TOK_GeneralString
182 %token TOK_GraphicString
183 %token TOK_IA5String
184 %token TOK_IDENTIFIER
185 %token TOK_IMPLICIT
186 %token TOK_IMPLIED
187 %token TOK_IMPORTS
188 %token TOK_INCLUDES
189 %token TOK_INSTANCE
190 %token TOK_INSTRUCTIONS
191 %token TOK_INTEGER
192 %token TOK_ISO646String
193 %token TOK_MAX
194 %token TOK_MIN
195 %token TOK_MINUS_INFINITY
196 %token TOK_NULL
197 %token TOK_NumericString
198 %token TOK_OBJECT
199 %token TOK_ObjectDescriptor
200 %token TOK_OCTET
201 %token TOK_OF
202 %token TOK_OPTIONAL
203 %token TOK_PATTERN
204 %token TOK_PDV
205 %token TOK_PLUS_INFINITY
206 %token TOK_PRESENT
207 %token TOK_PrintableString
208 %token TOK_PRIVATE
209 %token TOK_REAL
210 %token TOK_RELATIVE_OID
211 %token TOK_SEQUENCE
212 %token TOK_SET
213 %token TOK_SIZE
214 %token TOK_STRING
215 %token TOK_SYNTAX
216 %token TOK_T61String
217 %token TOK_TAGS
218 %token TOK_TeletexString
219 %token TOK_TRUE
220 %token TOK_TYPE_IDENTIFIER
221 %token TOK_UNIQUE
222 %token TOK_UNIVERSAL
223 %token TOK_UniversalString
224 %token TOK_UTCTime
225 %token TOK_UTF8String
226 %token TOK_VideotexString
227 %token TOK_VisibleString
228 %token TOK_WITH
229
230 %nonassoc TOK_EXCEPT
231 %left '^' TOK_INTERSECTION
232 %left '|' TOK_UNION
233
234 /* Misc tags */
235 %token TOK_TwoDots /* .. */
236 %token TOK_ThreeDots /* ... */
237
238
239 /*
240 * Types defined herein.
241 */
242 %type <a_grammar> ModuleList
243 %type <a_module> ModuleDefinition
244 %type <a_module> ModuleBody
245 %type <a_module> AssignmentList
246 %type <a_module> Assignment
247 %type <a_module> optModuleBody /* Optional */
248 %type <a_module_flags> optModuleDefinitionFlags
249 %type <a_module_flags> ModuleDefinitionFlags /* Set of FL */
250 %type <a_module_flags> ModuleDefinitionFlag /* Single FL */
251 %type <a_module> optImports
252 %type <a_module> optExports
253 %type <a_module> ImportsDefinition
254 %type <a_module> optImportsBundleSet
255 %type <a_module> ImportsBundleSet
256 %type <a_xports> ImportsBundle
257 %type <a_xports> ImportsList
258 %type <a_xports> ExportsDefinition
259 %type <a_xports> ExportsBody
260 %type <a_expr> ImportsElement
261 %type <a_expr> ExportsElement
262 %type <a_expr> ExtensionAndException
263 %type <a_expr> Type
264 %type <a_expr> TypeDeclaration
265 %type <a_expr> TypeDeclarationSet
266 %type <a_ref> ComplexTypeReference
267 %type <a_ref> ComplexTypeReferenceAmpList
268 %type <a_refcomp> ComplexTypeReferenceElement
269 %type <a_refcomp> PrimitiveFieldReference
270 %type <a_expr> FieldSpec
271 %type <a_ref> FieldName
272 %type <a_ref> DefinedObjectClass
273 %type <a_expr> ClassField
274 %type <a_expr> ObjectClass
275 %type <a_expr> DataTypeReference /* Type1 ::= Type2 */
276 %type <a_expr> DefinedType
277 %type <a_constr> ValueSet /* {a|b|c}*/
278 %type <a_expr> ValueSetTypeAssignment /* Val INTEGER ::= {1|2} */
279 %type <a_expr> ValueAssignment /* val INTEGER ::= 1*/
280 %type <a_value> Value
281 %type <a_value> SimpleValue
282 %type <a_value> DefinedValue
283 %type <a_value> SignedNumber
284 %type <a_value> RealValue
285 %type <a_value> BitStringValue
286 %type <a_expr> optComponentTypeLists
287 %type <a_expr> ComponentTypeLists
288 %type <a_expr> ComponentType
289 %type <a_expr> AlternativeTypeLists
290 %type <a_expr> AlternativeType
291 %type <a_expr> UniverationDefinition
292 %type <a_expr> UniverationList
293 %type <a_expr> UniverationElement
294 %type <tv_str> TypeRefName
295 %type <tv_str> ObjectClassReference
296 %type <tv_str> Identifier
297 %type <tv_str> optIdentifier
298 %type <a_parg> ParameterArgumentName
299 %type <a_plist> ParameterArgumentList
300 %type <a_expr> ActualParameter
301 %type <a_expr> ActualParameterList
302 %type <a_aid> AssignedIdentifier /* OID/DefinedValue */
303 %type <a_oid> ObjectIdentifier /* OID */
304 %type <a_oid> optObjectIdentifier /* Optional OID */
305 %type <a_oid> ObjectIdentifierBody
306 %type <a_oid_arc> ObjectIdentifierElement
307 %type <a_expr> BasicType
308 %type <a_type> BasicTypeId
309 %type <a_type> BasicTypeId_UniverationCompatible
310 %type <a_type> BasicString
311 %type <tv_opaque> Opaque
312 %type <a_tag> Tag /* [UNIVERSAL 0] IMPLICIT */
313 %type <a_tag> TagClass TagTypeValue TagPlicit
314 %type <a_tag> optTag /* [UNIVERSAL 0] IMPLICIT */
315 %type <a_constr> optConstraints
316 %type <a_constr> Constraint
317 %type <a_constr> SubtypeConstraint
318 %type <a_constr> GeneralConstraint
319 %type <a_constr> SetOfConstraints
320 %type <a_constr> ElementSetSpecs /* 1..2,...,3 */
321 %type <a_constr> ElementSetSpec /* 1..2,...,3 */
322 %type <a_constr> Unions
323 %type <a_constr> Intersections
324 %type <a_constr> IntersectionElements
325 %type <a_constr> ConstraintSubtypeElement /* 1..2 */
326 %type <a_constr> SimpleTableConstraint
327 %type <a_constr> UserDefinedConstraint
328 %type <a_constr> TableConstraint
329 %type <a_constr> ContentsConstraint
330 %type <a_constr> PatternConstraint
331 %type <a_constr> InnerTypeConstraint
332 %type <a_constr> WithComponentsList
333 %type <a_constr> WithComponentsElement
334 %type <a_constr> ComponentRelationConstraint
335 %type <a_constr> AtNotationList
336 %type <a_ref> AtNotationElement
337 %type <a_value> SingleValue
338 %type <a_value> ContainedSubtype
339 %type <a_ctype> ConstraintSpec
340 %type <a_ctype> ConstraintRangeSpec
341 %type <a_value> RestrictedCharacterStringValue
342 %type <a_wsynt> optWithSyntax
343 %type <a_wsynt> WithSyntax
344 %type <a_wsynt> WithSyntaxList
345 %type <a_wchunk> WithSyntaxToken
346 %type <a_marker> optMarker Marker
347 %type <a_int> optUnique
348 %type <a_pres> optPresenceConstraint PresenceConstraint
349 %type <tv_str> ComponentIdList
350 %type <a_int> NSTD_IndirectMarker
351
352
353 %%
354
355
356 ParsedGrammar:
357 ModuleList {
358 *(void **)param = $1;
359 }
360 ;
361
362 ModuleList:
363 ModuleDefinition {
364 $$ = asn1p_new();
365 checkmem($$);
366 TQ_ADD(&($$->modules), $1, mod_next);
367 }
368 | ModuleList ModuleDefinition {
369 $$ = $1;
370 TQ_ADD(&($$->modules), $2, mod_next);
371 }
372 ;
373
374 /*
375 * ASN module definition.
376 * === EXAMPLE ===
377 * MySyntax DEFINITIONS AUTOMATIC TAGS ::=
378 * BEGIN
379 * ...
380 * END
381 * === EOF ===
382 */
383
384 ModuleDefinition:
385 TypeRefName { currentModule = asn1p_module_new(); }
386 optObjectIdentifier TOK_DEFINITIONS
387 optModuleDefinitionFlags
388 TOK_PPEQ TOK_BEGIN
389 optModuleBody
390 TOK_END {
391
392 $$ = currentModule;
393
394 if($8) {
395 asn1p_module_t tmp = *($$);
396 *($$) = *($8);
397 *($8) = tmp;
398 asn1p_module_free($8);
399 } else {
400 /* There's a chance that a module is just plain empty */
401 }
402
403 $$->ModuleName = $1;
404 $$->module_oid = $3;
405 $$->module_flags = $5;
406 }
407 ;
408
409 /*
410 * Object Identifier Definition
411 * { iso member-body(2) 3 }
412 */
413 optObjectIdentifier:
414 { $$ = 0; }
415 | ObjectIdentifier { $$ = $1; }
416 ;
417
418 ObjectIdentifier:
419 '{' ObjectIdentifierBody '}' {
420 $$ = $2;
421 }
422 | '{' '}' {
423 $$ = 0;
424 }
425 ;
426
427 ObjectIdentifierBody:
428 ObjectIdentifierElement {
429 $$ = asn1p_oid_new();
430 asn1p_oid_add_arc($$, &$1);
431 if($1.name)
432 free($1.name);
433 }
434 | ObjectIdentifierBody ObjectIdentifierElement {
435 $$ = $1;
436 asn1p_oid_add_arc($$, &$2);
437 if($2.name)
438 free($2.name);
439 }
440 ;
441
442 ObjectIdentifierElement:
443 Identifier { /* iso */
444 $$.name = $1;
445 $$.number = -1;
446 }
447 | Identifier '(' TOK_number ')' { /* iso(1) */
448 $$.name = $1;
449 $$.number = $3;
450 }
451 | TOK_number { /* 1 */
452 $$.name = 0;
453 $$.number = $1;
454 }
455 ;
456
457 /*
458 * Optional module flags.
459 */
460 optModuleDefinitionFlags:
461 { $$ = MSF_NOFLAGS; }
462 | ModuleDefinitionFlags {
463 $$ = $1;
464 }
465 ;
466
467 /*
468 * Module flags.
469 */
470 ModuleDefinitionFlags:
471 ModuleDefinitionFlag {
472 $$ = $1;
473 }
474 | ModuleDefinitionFlags ModuleDefinitionFlag {
475 $$ = $1 | $2;
476 }
477 ;
478
479 /*
480 * Single module flag.
481 */
482 ModuleDefinitionFlag:
483 TOK_EXPLICIT TOK_TAGS {
484 $$ = MSF_EXPLICIT_TAGS;
485 }
486 | TOK_IMPLICIT TOK_TAGS {
487 $$ = MSF_IMPLICIT_TAGS;
488 }
489 | TOK_AUTOMATIC TOK_TAGS {
490 $$ = MSF_AUTOMATIC_TAGS;
491 }
492 | TOK_EXTENSIBILITY TOK_IMPLIED {
493 $$ = MSF_EXTENSIBILITY_IMPLIED;
494 }
495 /* EncodingReferenceDefault */
496 | TOK_capitalreference TOK_INSTRUCTIONS {
497 /* X.680Amd1 specifies TAG and XER */
498 if(strcmp($1, "TAG") == 0) {
499 $$ = MSF_TAG_INSTRUCTIONS;
500 } else if(strcmp($1, "XER") == 0) {
501 $$ = MSF_XER_INSTRUCTIONS;
502 } else {
503 fprintf(stderr,
504 "WARNING: %s INSTRUCTIONS at line %d: "
505 "Unrecognized encoding reference\n",
506 $1, yylineno);
507 $$ = MSF_unk_INSTRUCTIONS;
508 }
509 free($1);
510 }
511 ;
512
513 /*
514 * Optional module body.
515 */
516 optModuleBody:
517 { $$ = 0; }
518 | ModuleBody {
519 $$ = $1;
520 }
521 ;
522
523 /*
524 * ASN.1 Module body.
525 */
526 ModuleBody:
527 optExports optImports AssignmentList {
528 $$ = asn1p_module_new();
529 AL_IMPORT($$, exports, $1, xp_next);
530 AL_IMPORT($$, imports, $2, xp_next);
531 AL_IMPORT($$, members, $3, next);
532 }
533 ;
534
535 AssignmentList:
536 Assignment {
537 $$ = $1;
538 }
539 | AssignmentList Assignment {
540 if($1) {
541 $$ = $1;
542 } else {
543 $$ = $2;
544 break;
545 }
546 AL_IMPORT($$, members, $2, next);
547 }
548 ;
549
550
551 /*
552 * One of the elements of ASN.1 module specification.
553 */
554 Assignment:
555 DataTypeReference {
556 $$ = asn1p_module_new();
557 checkmem($$);
558 assert($1->expr_type != A1TC_INVALID);
559 assert($1->meta_type != AMT_INVALID);
560 TQ_ADD(&($$->members), $1, next);
561 }
562 | ValueAssignment {
563 $$ = asn1p_module_new();
564 checkmem($$);
565 assert($1->expr_type != A1TC_INVALID);
566 assert($1->meta_type != AMT_INVALID);
567 TQ_ADD(&($$->members), $1, next);
568 }
569 /*
570 * Value set definition
571 * === EXAMPLE ===
572 * EvenNumbers INTEGER ::= { 2 | 4 | 6 | 8 }
573 * === EOF ===
574 */
575 | ValueSetTypeAssignment {
576 $$ = asn1p_module_new();
577 checkmem($$);
578 assert($1->expr_type != A1TC_INVALID);
579 assert($1->meta_type != AMT_INVALID);
580 TQ_ADD(&($$->members), $1, next);
581 }
582 | TOK_ENCODING_CONTROL TOK_capitalreference
583 { asn1p_lexer_hack_push_encoding_control(); }
584 {
585 fprintf(stderr,
586 "WARNING: ENCODING-CONTROL %s "
587 "specification at line %d ignored\n",
588 $2, yylineno);
589 free($2);
590 $$ = 0;
591 }
592
593 /*
594 * Erroneous attemps
595 */
596 | BasicString {
597 return yyerror(
598 "Attempt to redefine a standard basic string type, "
599 "please comment out or remove this type redefinition.");
600 }
601 ;
602
603 /*
604 * === EXAMPLE ===
605 * IMPORTS Type1, value FROM Module { iso standard(0) } ;
606 * === EOF ===
607 */
608 optImports:
609 { $$ = 0; }
610 | ImportsDefinition;
611
612 ImportsDefinition:
613 TOK_IMPORTS optImportsBundleSet ';' {
614 if(!saved_aid && 0)
615 return yyerror("Unterminated IMPORTS FROM, "
616 "expected semicolon ';'");
617 saved_aid = 0;
618 $$ = $2;
619 }
620 /*
621 * Some error cases.
622 */
623 | TOK_IMPORTS TOK_FROM /* ... */ {
624 return yyerror("Empty IMPORTS list");
625 }
626 ;
627
628 optImportsBundleSet:
629 { $$ = asn1p_module_new(); }
630 | ImportsBundleSet;
631
632 ImportsBundleSet:
633 ImportsBundle {
634 $$ = asn1p_module_new();
635 checkmem($$);
636 TQ_ADD(&($$->imports), $1, xp_next);
637 }
638 | ImportsBundleSet ImportsBundle {
639 $$ = $1;
640 TQ_ADD(&($$->imports), $2, xp_next);
641 }
642 ;
643
644 AssignedIdentifier:
645 { memset(&$$, 0, sizeof($$)); }
646 | ObjectIdentifier { $$.oid = $1; };
647 /* | DefinedValue { $$.value = $1; }; // Handled through saved_aid */
648
649 ImportsBundle:
650 ImportsList TOK_FROM TypeRefName AssignedIdentifier {
651 $$ = $1;
652 $$->fromModuleName = $3;
653 $$->identifier = $4;
654 /* This stupid thing is used for look-back hack. */
655 saved_aid = $$->identifier.oid ? 0 : &($$->identifier);
656 checkmem($$);
657 }
658 ;
659
660 ImportsList:
661 ImportsElement {
662 $$ = asn1p_xports_new();
663 checkmem($$);
664 TQ_ADD(&($$->members), $1, next);
665 }
666 | ImportsList ',' ImportsElement {
667 $$ = $1;
668 TQ_ADD(&($$->members), $3, next);
669 }
670 ;
671
672 ImportsElement:
673 TypeRefName {
674 $$ = NEW_EXPR();
675 checkmem($$);
676 $$->Identifier = $1;
677 $$->expr_type = A1TC_REFERENCE;
678 }
679 | TypeRefName '{' '}' { /* Completely equivalent to above */
680 $$ = NEW_EXPR();
681 checkmem($$);
682 $$->Identifier = $1;
683 $$->expr_type = A1TC_REFERENCE;
684 }
685 | Identifier {
686 $$ = NEW_EXPR();
687 checkmem($$);
688 $$->Identifier = $1;
689 $$->expr_type = A1TC_REFERENCE;
690 }
691 ;
692
693
694 optExports:
695 { $$ = 0; }
696 | ExportsDefinition {
697 $$ = asn1p_module_new();
698 checkmem($$);
699 if($1) {
700 TQ_ADD(&($$->exports), $1, xp_next);
701 } else {
702 /* "EXPORTS ALL;" */
703 }
704 }
705 ;
706
707 ExportsDefinition:
708 TOK_EXPORTS ExportsBody ';' {
709 $$ = $2;
710 }
711 | TOK_EXPORTS TOK_ALL ';' {
712 $$ = 0;
713 }
714 | TOK_EXPORTS ';' {
715 /* Empty EXPORTS clause effectively prohibits export. */
716 $$ = asn1p_xports_new();
717 checkmem($$);
718 }
719 ;
720
721 ExportsBody:
722 ExportsElement {
723 $$ = asn1p_xports_new();
724 assert($$);
725 TQ_ADD(&($$->members), $1, next);
726 }
727 | ExportsBody ',' ExportsElement {
728 $$ = $1;
729 TQ_ADD(&($$->members), $3, next);
730 }
731 ;
732
733 ExportsElement:
734 TypeRefName {
735 $$ = NEW_EXPR();
736 checkmem($$);
737 $$->Identifier = $1;
738 $$->expr_type = A1TC_EXPORTVAR;
739 }
740 | TypeRefName '{' '}' {
741 $$ = NEW_EXPR();
742 checkmem($$);
743 $$->Identifier = $1;
744 $$->expr_type = A1TC_EXPORTVAR;
745 }
746 | Identifier {
747 $$ = NEW_EXPR();
748 checkmem($$);
749 $$->Identifier = $1;
750 $$->expr_type = A1TC_EXPORTVAR;
751 }
752 ;
753
754
755 ValueSet: '{' ElementSetSpecs '}' { $$ = $2; };
756
757 ValueSetTypeAssignment:
758 TypeRefName DefinedType TOK_PPEQ ValueSet {
759 $$ = $2;
760 assert($$->Identifier == 0);
761 $$->Identifier = $1;
762 $$->meta_type = AMT_VALUESET;
763 $$->constraints = $4;
764 }
765 ;
766
767 DefinedType:
768 BasicType {
769 $$ = $1;
770 }
771 /*
772 * A DefinedType reference.
773 * "CLASS1.&id.&id2"
774 * or
775 * "Module.Type"
776 * or
777 * "Module.identifier"
778 * or
779 * "Type"
780 */
781 | ComplexTypeReference {
782 $$ = NEW_EXPR();
783 checkmem($$);
784 $$->reference = $1;
785 $$->expr_type = A1TC_REFERENCE;
786 $$->meta_type = AMT_TYPEREF;
787 }
788 /*
789 * A parameterized assignment.
790 */
791 | ComplexTypeReference '{' ActualParameterList '}' {
792 $$ = NEW_EXPR();
793 checkmem($$);
794 $$->reference = $1;
795 $$->rhs_pspecs = $3;
796 $$->expr_type = A1TC_REFERENCE;
797 $$->meta_type = AMT_TYPEREF;
798 }
799 ;
800
801 /*
802 * Data Type Reference.
803 * === EXAMPLE ===
804 * Type3 ::= CHOICE { a Type1, b Type 2 }
805 * === EOF ===
806 */
807 DataTypeReference:
808 /*
809 * Optionally tagged type definition.
810 */
811 TypeRefName TOK_PPEQ Type {
812 $$ = $3;
813 $$->Identifier = $1;
814 assert($$->expr_type);
815 assert($$->meta_type);
816 }
817 | TypeRefName TOK_PPEQ ObjectClass {
818 $$ = $3;
819 $$->Identifier = $1;
820 assert($$->expr_type == A1TC_CLASSDEF);
821 assert($$->meta_type == AMT_OBJECTCLASS);
822 }
823 /*
824 * Parameterized <Type> declaration:
825 * === EXAMPLE ===
826 * SIGNED { ToBeSigned } ::= SEQUENCE {
827 * toBeSigned ToBeSigned,
828 * algorithm AlgorithmIdentifier,
829 * signature BIT STRING
830 * }
831 * === EOF ===
832 */
833 | TypeRefName '{' ParameterArgumentList '}' TOK_PPEQ Type {
834 $$ = $6;
835 $$->Identifier = $1;
836 $$->lhs_params = $3;
837 }
838 /* Parameterized CLASS declaration */
839 | TypeRefName '{' ParameterArgumentList '}' TOK_PPEQ ObjectClass {
840 $$ = $6;
841 $$->Identifier = $1;
842 $$->lhs_params = $3;
843 }
844 ;
845
846 ParameterArgumentList:
847 ParameterArgumentName {
848 int ret;
849 $$ = asn1p_paramlist_new(yylineno);
850 checkmem($$);
851 ret = asn1p_paramlist_add_param($$, $1.governor, $1.argument);
852 checkmem(ret == 0);
853 if($1.governor) asn1p_ref_free($1.governor);
854 if($1.argument) free($1.argument);
855 }
856 | ParameterArgumentList ',' ParameterArgumentName {
857 int ret;
858 $$ = $1;
859 ret = asn1p_paramlist_add_param($$, $3.governor, $3.argument);
860 checkmem(ret == 0);
861 if($3.governor) asn1p_ref_free($3.governor);
862 if($3.argument) free($3.argument);
863 }
864 ;
865
866 ParameterArgumentName:
867 TypeRefName {
868 $$.governor = NULL;
869 $$.argument = $1;
870 }
871 | TypeRefName ':' Identifier {
872 int ret;
873 $$.governor = asn1p_ref_new(yylineno);
874 ret = asn1p_ref_add_component($$.governor, $1, 0);
875 checkmem(ret == 0);
876 $$.argument = $3;
877 }
878 | TypeRefName ':' TypeRefName {
879 int ret;
880 $$.governor = asn1p_ref_new(yylineno);
881 ret = asn1p_ref_add_component($$.governor, $1, 0);
882 checkmem(ret == 0);
883 $$.argument = $3;
884 }
885 | BasicTypeId ':' Identifier {
886 int ret;
887 $$.governor = asn1p_ref_new(yylineno);
888 ret = asn1p_ref_add_component($$.governor,
889 ASN_EXPR_TYPE2STR($1), 1);
890 checkmem(ret == 0);
891 $$.argument = $3;
892 }
893 | BasicTypeId ':' TypeRefName {
894 int ret;
895 $$.governor = asn1p_ref_new(yylineno);
896 ret = asn1p_ref_add_component($$.governor,
897 ASN_EXPR_TYPE2STR($1), 1);
898 checkmem(ret == 0);
899 $$.argument = $3;
900 }
901 ;
902
903 ActualParameterList:
904 ActualParameter {
905 $$ = NEW_EXPR();
906 checkmem($$);
907 asn1p_expr_add($$, $1);
908 }
909 | ActualParameterList ',' ActualParameter {
910 $$ = $1;
911 asn1p_expr_add($$, $3);
912 }
913 ;
914
915 ActualParameter:
916 Type {
917 $$ = $1;
918 }
919 | SimpleValue {
920 $$ = NEW_EXPR();
921 checkmem($$);
922 $$->Identifier = "?";
923 $$->expr_type = A1TC_REFERENCE;
924 $$->meta_type = AMT_VALUE;
925 $$->value = $1;
926 }
927 | Identifier {
928 asn1p_ref_t *ref;
929 $$ = NEW_EXPR();
930 checkmem($$);
931 $$->Identifier = $1;
932 $$->expr_type = A1TC_REFERENCE;
933 $$->meta_type = AMT_VALUE;
934 ref = asn1p_ref_new(yylineno);
935 asn1p_ref_add_component(ref, $1, RLT_lowercase);
936 $$->value = asn1p_value_fromref(ref, 0);
937 }
938 | ValueSet {
939 $$ = NEW_EXPR();
940 $$->expr_type = A1TC_VALUESET;
941 $$->meta_type = AMT_VALUESET;
942 $$->constraints = $1;
943 }
944 ;
945
946 /*
947 | '{' ActualParameter '}' {
948 $$ = NEW_EXPR();
949 checkmem($$);
950 asn1p_expr_add($$, $2);
951 $$->expr_type = A1TC_PARAMETRIZED;
952 $$->meta_type = AMT_TYPE;
953 }
954 ;
955 */
956
957 /*
958 * A collection of constructed data type members.
959 */
960 optComponentTypeLists:
961 { $$ = NEW_EXPR(); }
962 | ComponentTypeLists { $$ = $1; };
963
964 ComponentTypeLists:
965 ComponentType {
966 $$ = NEW_EXPR();
967 checkmem($$);
968 asn1p_expr_add($$, $1);
969 }
970 | ComponentTypeLists ',' ComponentType {
971 $$ = $1;
972 asn1p_expr_add($$, $3);
973 }
974 | ComponentTypeLists ',' TOK_VBracketLeft ComponentTypeLists TOK_VBracketRight {
975 $$ = $1;
976 asn1p_expr_add_many($$, $4);
977 }
978 ;
979
980 ComponentType:
981 Identifier Type optMarker {
982 $$ = $2;
983 assert($$->Identifier == 0);
984 $$->Identifier = $1;
985 $3.flags |= $$->marker.flags;
986 $$->marker = $3;
987 }
988 | Type optMarker {
989 $$ = $1;
990 $2.flags |= $$->marker.flags;
991 $$->marker = $2;
992 _fixup_anonymous_identifier($$);
993 }
994 | TOK_COMPONENTS TOK_OF Type {
995 $$ = NEW_EXPR();
996 checkmem($$);
997 $$->meta_type = $3->meta_type;
998 $$->expr_type = A1TC_COMPONENTS_OF;
999 asn1p_expr_add($$, $3);
1000 }
1001 | ExtensionAndException {
1002 $$ = $1;
1003 }
1004 ;
1005
1006 AlternativeTypeLists:
1007 AlternativeType {
1008 $$ = NEW_EXPR();
1009 checkmem($$);
1010 asn1p_expr_add($$, $1);
1011 }
1012 | AlternativeTypeLists ',' AlternativeType {
1013 $$ = $1;
1014 asn1p_expr_add($$, $3);
1015 }
1016 ;
1017
1018 AlternativeType:
1019 Identifier Type {
1020 $$ = $2;
1021 assert($$->Identifier == 0);
1022 $$->Identifier = $1;
1023 }
1024 | ExtensionAndException {
1025 $$ = $1;
1026 }
1027 | Type {
1028 $$ = $1;
1029 _fixup_anonymous_identifier($$);
1030 }
1031 ;
1032
1033 ObjectClass:
1034 TOK_CLASS '{' FieldSpec '}' optWithSyntax {
1035 $$ = $3;
1036 checkmem($$);
1037 $$->with_syntax = $5;
1038 assert($$->expr_type == A1TC_CLASSDEF);
1039 assert($$->meta_type == AMT_OBJECTCLASS);
1040 }
1041 ;
1042
1043 optUnique:
1044 { $$ = 0; }
1045 | TOK_UNIQUE { $$ = 1; }
1046 ;
1047
1048 FieldSpec:
1049 ClassField {
1050 $$ = NEW_EXPR();
1051 checkmem($$);
1052 $$->expr_type = A1TC_CLASSDEF;
1053 $$->meta_type = AMT_OBJECTCLASS;
1054 asn1p_expr_add($$, $1);
1055 }
1056 | FieldSpec ',' ClassField {
1057 $$ = $1;
1058 asn1p_expr_add($$, $3);
1059 }
1060 ;
1061
1062 /* X.681 */
1063 ClassField:
1064
1065 /* TypeFieldSpec ::= typefieldreference TypeOptionalitySpec? */
1066 TOK_typefieldreference optMarker {
1067 $$ = NEW_EXPR();
1068 checkmem($$);
1069 $$->Identifier = $1;
1070 $$->meta_type = AMT_OBJECTFIELD;
1071 $$->expr_type = A1TC_CLASSFIELD_TFS; /* TypeFieldSpec */
1072 $$->marker = $2;
1073 }
1074
1075 /* FixedTypeValueFieldSpec ::= valuefieldreference Type UNIQUE ? ValueOptionalitySpec ? */
1076 | TOK_valuefieldreference Type optUnique optMarker {
1077 $$ = NEW_EXPR();
1078 $$->Identifier = $1;
1079 $$->meta_type = AMT_OBJECTFIELD;
1080 $$->expr_type = A1TC_CLASSFIELD_FTVFS; /* FixedTypeValueFieldSpec */
1081 $$->unique = $3;
1082 $$->marker = $4;
1083 asn1p_expr_add($$, $2);
1084 }
1085
1086 /* VariableTypeValueFieldSpec ::= valuefieldreference FieldName ValueOptionalitySpec ? */
1087 | TOK_valuefieldreference FieldName optMarker {
1088 $$ = NEW_EXPR();
1089 $$->Identifier = $1;
1090 $$->meta_type = AMT_OBJECTFIELD;
1091 $$->expr_type = A1TC_CLASSFIELD_VTVFS;
1092 $$->reference = $2;
1093 $$->marker = $3;
1094 }
1095
1096 /* ObjectFieldSpec ::= objectfieldreference DefinedObjectClass ObjectOptionalitySpec ? */
1097 | TOK_valuefieldreference DefinedObjectClass optMarker {
1098 $$ = NEW_EXPR();
1099 checkmem($$);
1100 $$->Identifier = $1;
1101 $$->reference = $2;
1102 $$->meta_type = AMT_OBJECTFIELD;
1103 $$->expr_type = A1TC_CLASSFIELD_OFS;
1104 $$->marker = $3;
1105 }
1106
1107 /* VariableTypeValueSetFieldSpec ::= valuesetfieldreference FieldName ValueOptionalitySpec ? */
1108 | TOK_typefieldreference FieldName optMarker {
1109 $$ = NEW_EXPR();
1110 $$->Identifier = $1;
1111 $$->meta_type = AMT_OBJECTFIELD;
1112 $$->expr_type = A1TC_CLASSFIELD_VTVSFS;
1113 $$->reference = $2;
1114 $$->marker = $3;
1115 }
1116
1117 /* FixedTypeValueSetFieldSpec ::= valuesetfieldreference Type ValueSetOptionalitySpec ? */
1118 | TOK_typefieldreference Type optMarker {
1119 $$ = NEW_EXPR();
1120 checkmem($$);
1121 $$->Identifier = $1;
1122 $$->meta_type = AMT_OBJECTFIELD;
1123 $$->expr_type = A1TC_CLASSFIELD_FTVSFS;
1124 asn1p_expr_add($$, $2);
1125 $$->marker = $3;
1126 }
1127
1128 /* ObjectSetFieldSpec ::= objectsetfieldreference DefinedObjectClass ObjectOptionalitySpec ? */
1129 | TOK_typefieldreference DefinedObjectClass optMarker {
1130 $$ = NEW_EXPR();
1131 checkmem($$);
1132 $$->Identifier = $1;
1133 $$->reference = $2;
1134 $$->meta_type = AMT_OBJECTFIELD;
1135 $$->expr_type = A1TC_CLASSFIELD_OSFS;
1136 $$->marker = $3;
1137 }
1138 ;
1139
1140 optWithSyntax:
1141 { $$ = 0; }
1142 | WithSyntax {
1143 $$ = $1;
1144 }
1145 ;
1146
1147 WithSyntax:
1148 TOK_WITH TOK_SYNTAX '{'
1149 { asn1p_lexer_hack_enable_with_syntax(); }
1150 WithSyntaxList
1151 '}' {
1152 $$ = $5;
1153 }
1154 ;
1155
1156 WithSyntaxList:
1157 WithSyntaxToken {
1158 $$ = asn1p_wsyntx_new();
1159 TQ_ADD(&($$->chunks), $1, next);
1160 }
1161 | WithSyntaxList WithSyntaxToken {
1162 $$ = $1;
1163 TQ_ADD(&($$->chunks), $2, next);
1164 }
1165 ;
1166
1167 WithSyntaxToken:
1168 TOK_whitespace {
1169 $$ = asn1p_wsyntx_chunk_fromstring($1.buf, 0);
1170 $$->type = WC_WHITESPACE;
1171 }
1172 | TOK_Literal {
1173 $$ = asn1p_wsyntx_chunk_fromstring($1, 0);
1174 }
1175 | PrimitiveFieldReference {
1176 $$ = asn1p_wsyntx_chunk_fromstring($1.name, 0);
1177 $$->type = WC_FIELD;
1178 }
1179 | '[' WithSyntaxList ']' {
1180 $$ = asn1p_wsyntx_chunk_fromsyntax($2);
1181 }
1182 ;
1183
1184 ExtensionAndException:
1185 TOK_ThreeDots {
1186 $$ = NEW_EXPR();
1187 checkmem($$);
1188 $$->Identifier = strdup("...");
1189 checkmem($$->Identifier);
1190 $$->expr_type = A1TC_EXTENSIBLE;
1191 $$->meta_type = AMT_TYPE;
1192 }
1193 | TOK_ThreeDots '!' DefinedValue {
1194 $$ = NEW_EXPR();
1195 checkmem($$);
1196 $$->Identifier = strdup("...");
1197 checkmem($$->Identifier);
1198 $$->value = $3;
1199 $$->expr_type = A1TC_EXTENSIBLE;
1200 $$->meta_type = AMT_TYPE;
1201 }
1202 | TOK_ThreeDots '!' SignedNumber {
1203 $$ = NEW_EXPR();
1204 checkmem($$);
1205 $$->Identifier = strdup("...");
1206 $$->value = $3;
1207 checkmem($$->Identifier);
1208 $$->expr_type = A1TC_EXTENSIBLE;
1209 $$->meta_type = AMT_TYPE;
1210 }
1211 ;
1212
1213 Type:
1214 optTag TypeDeclaration optConstraints {
1215 $$ = $2;
1216 $$->tag = $1;
1217 /*
1218 * Outer constraint for SEQUENCE OF and SET OF applies
1219 * to the inner type.
1220 */
1221 if($$->expr_type == ASN_CONSTR_SEQUENCE_OF
1222 || $$->expr_type == ASN_CONSTR_SET_OF) {
1223 assert(!TQ_FIRST(&($$->members))->constraints);
1224 TQ_FIRST(&($$->members))->constraints = $3;
1225 } else {
1226 if($$->constraints) {
1227 assert(!$2);
1228 } else {
1229 $$->constraints = $3;
1230 }
1231 }
1232 }
1233 ;
1234
1235 NSTD_IndirectMarker:
1236 {
1237 $$ = asn1p_as_pointer ? EM_INDIRECT : 0;
1238 asn1p_as_pointer = 0;
1239 }
1240 ;
1241
1242 TypeDeclaration:
1243 NSTD_IndirectMarker TypeDeclarationSet {
1244 $$ = $2;
1245 $$->marker.flags |= $1;
1246
1247 if(($$->marker.flags & EM_INDIRECT)
1248 && ($$->marker.flags & EM_OPTIONAL) != EM_OPTIONAL) {
1249 fprintf(stderr,
1250 "INFO: Directive <ASN1C:RepresentAsPointer> "
1251 "applied to %s at line %d\n",
1252 ASN_EXPR_TYPE2STR($$->expr_type)
1253 ? ASN_EXPR_TYPE2STR($$->expr_type)
1254 : "member",
1255 $$->_lineno
1256 );
1257 }
1258 }
1259 ;
1260
1261 TypeDeclarationSet:
1262 DefinedType {
1263 $$ = $1;
1264 }
1265 | TOK_CHOICE '{' AlternativeTypeLists '}' {
1266 $$ = $3;
1267 assert($$->expr_type == A1TC_INVALID);
1268 $$->expr_type = ASN_CONSTR_CHOICE;
1269 $$->meta_type = AMT_TYPE;
1270 }
1271 | TOK_SEQUENCE '{' optComponentTypeLists '}' {
1272 $$ = $3;
1273 assert($$->expr_type == A1TC_INVALID);
1274 $$->expr_type = ASN_CONSTR_SEQUENCE;
1275 $$->meta_type = AMT_TYPE;
1276 }
1277 | TOK_SET '{' optComponentTypeLists '}' {
1278 $$ = $3;
1279 assert($$->expr_type == A1TC_INVALID);
1280 $$->expr_type = ASN_CONSTR_SET;
1281 $$->meta_type = AMT_TYPE;
1282 }
1283 | TOK_SEQUENCE optConstraints TOK_OF optIdentifier optTag TypeDeclaration {
1284 $$ = NEW_EXPR();
1285 checkmem($$);
1286 $$->constraints = $2;
1287 $$->expr_type = ASN_CONSTR_SEQUENCE_OF;
1288 $$->meta_type = AMT_TYPE;
1289 $6->Identifier = $4;
1290 $6->tag = $5;
1291 asn1p_expr_add($$, $6);
1292 }
1293 | TOK_SET optConstraints TOK_OF optIdentifier optTag TypeDeclaration {
1294 $$ = NEW_EXPR();
1295 checkmem($$);
1296 $$->constraints = $2;
1297 $$->expr_type = ASN_CONSTR_SET_OF;
1298 $$->meta_type = AMT_TYPE;
1299 $6->Identifier = $4;
1300 $6->tag = $5;
1301 asn1p_expr_add($$, $6);
1302 }
1303 | TOK_ANY {
1304 $$ = NEW_EXPR();
1305 checkmem($$);
1306 $$->expr_type = ASN_TYPE_ANY;
1307 $$->meta_type = AMT_TYPE;
1308 }
1309 | TOK_ANY TOK_DEFINED TOK_BY Identifier {
1310 int ret;
1311 $$ = NEW_EXPR();
1312 checkmem($$);
1313 $$->reference = asn1p_ref_new(yylineno);
1314 ret = asn1p_ref_add_component($$->reference,
1315 $4, RLT_lowercase);
1316 checkmem(ret == 0);
1317 $$->expr_type = ASN_TYPE_ANY;
1318 $$->meta_type = AMT_TYPE;
1319 }
1320 | TOK_INSTANCE TOK_OF ComplexTypeReference {
1321 $$ = NEW_EXPR();
1322 checkmem($$);
1323 $$->reference = $3;
1324 $$->expr_type = A1TC_INSTANCE;
1325 $$->meta_type = AMT_TYPE;
1326 }
1327 ;
1328
1329 /*
1330 * A type name consisting of several components.
1331 * === EXAMPLE ===
1332 * === EOF ===
1333 */
1334 ComplexTypeReference:
1335 TOK_typereference {
1336 int ret;
1337 $$ = asn1p_ref_new(yylineno);
1338 checkmem($$);
1339 ret = asn1p_ref_add_component($$, $1, RLT_UNKNOWN);
1340 checkmem(ret == 0);
1341 free($1);
1342 }
1343 | TOK_typereference '.' TypeRefName {
1344 int ret;
1345 $$ = asn1p_ref_new(yylineno);
1346 checkmem($$);
1347 ret = asn1p_ref_add_component($$, $1, RLT_UNKNOWN);
1348 checkmem(ret == 0);
1349 ret = asn1p_ref_add_component($$, $3, RLT_UNKNOWN);
1350 checkmem(ret == 0);
1351 free($1);
1352 }
1353 | ObjectClassReference '.' TypeRefName {
1354 int ret;
1355 $$ = asn1p_ref_new(yylineno);
1356 checkmem($$);
1357 ret = asn1p_ref_add_component($$, $1, RLT_UNKNOWN);
1358 checkmem(ret == 0);
1359 ret = asn1p_ref_add_component($$, $3, RLT_UNKNOWN);
1360 checkmem(ret == 0);
1361 free($1);
1362 }
1363 | TOK_typereference '.' Identifier {
1364 int ret;
1365 $$ = asn1p_ref_new(yylineno);
1366 checkmem($$);
1367 ret = asn1p_ref_add_component($$, $1, RLT_UNKNOWN);
1368 checkmem(ret == 0);
1369 ret = asn1p_ref_add_component($$, $3, RLT_lowercase);
1370 checkmem(ret == 0);
1371 free($1);
1372 }
1373 | ObjectClassReference {
1374 int ret;
1375 $$ = asn1p_ref_new(yylineno);
1376 checkmem($$);
1377 ret = asn1p_ref_add_component($$, $1, RLT_CAPITALS);
1378 free($1);
1379 checkmem(ret == 0);
1380 }
1381 | ObjectClassReference '.' ComplexTypeReferenceAmpList {
1382 int ret;
1383 $$ = $3;
1384 ret = asn1p_ref_add_component($$, $1, RLT_CAPITALS);
1385 free($1);
1386 checkmem(ret == 0);
1387 /*
1388 * Move the last element infront.
1389 */
1390 {
1391 struct asn1p_ref_component_s tmp_comp;
1392 tmp_comp = $$->components[$$->comp_count-1];
1393 memmove(&$$->components[1],
1394 &$$->components[0],
1395 sizeof($$->components[0])
1396 * ($$->comp_count - 1));
1397 $$->components[0] = tmp_comp;
1398 }
1399 }
1400 ;
1401
1402 ComplexTypeReferenceAmpList:
1403 ComplexTypeReferenceElement {
1404 int ret;
1405 $$ = asn1p_ref_new(yylineno);
1406 checkmem($$);
1407 ret = asn1p_ref_add_component($$, $1.name, $1.lex_type);
1408 free($1.name);
1409 checkmem(ret == 0);
1410 }
1411 | ComplexTypeReferenceAmpList '.' ComplexTypeReferenceElement {
1412 int ret;
1413 $$ = $1;
1414 ret = asn1p_ref_add_component($$, $3.name, $3.lex_type);
1415 free($3.name);
1416 checkmem(ret == 0);
1417 }
1418 ;
1419
1420 ComplexTypeReferenceElement: PrimitiveFieldReference;
1421
1422 PrimitiveFieldReference:
1423 /* "&Type1" */
1424 TOK_typefieldreference {
1425 $$.lex_type = RLT_AmpUppercase;
1426 $$.name = $1;
1427 }
1428 /* "&id" */
1429 | TOK_valuefieldreference {
1430 $$.lex_type = RLT_Amplowercase;
1431 $$.name = $1;
1432 }
1433 ;
1434
1435
1436 FieldName:
1437 /* "&Type1" */
1438 TOK_typefieldreference {
1439 $$ = asn1p_ref_new(yylineno);
1440 asn1p_ref_add_component($$, $1, RLT_AmpUppercase);
1441 }
1442 | FieldName '.' TOK_typefieldreference {
1443 $$ = $$;
1444 asn1p_ref_add_component($$, $3, RLT_AmpUppercase);
1445 }
1446 | FieldName '.' TOK_valuefieldreference {
1447 $$ = $$;
1448 asn1p_ref_add_component($$, $3, RLT_Amplowercase);
1449 }
1450 ;
1451
1452 DefinedObjectClass:
1453 TOK_capitalreference {
1454 $$ = asn1p_ref_new(yylineno);
1455 asn1p_ref_add_component($$, $1, RLT_CAPITALS);
1456 }
1457 /*
1458 | TypeRefName '.' TOK_capitalreference {
1459 $$ = asn1p_ref_new(yylineno);
1460 asn1p_ref_add_component($$, $1, RLT_AmpUppercase);
1461 asn1p_ref_add_component($$, $3, RLT_CAPITALS);
1462 }
1463 */
1464 ;
1465
1466
1467 /*
1468 * === EXAMPLE ===
1469 * value INTEGER ::= 1
1470 * === EOF ===
1471 */
1472 ValueAssignment:
1473 Identifier Type TOK_PPEQ Value {
1474 $$ = $2;
1475 assert($$->Identifier == NULL);
1476 $$->Identifier = $1;
1477 $$->meta_type = AMT_VALUE;
1478 $$->value = $4;
1479 }
1480 ;
1481
1482 Value:
1483 SimpleValue
1484 | DefinedValue
1485 | Identifier ':' Value {
1486 $$ = asn1p_value_fromint(0);
1487 checkmem($$);
1488 $$->type = ATV_CHOICE_IDENTIFIER;
1489 $$->value.choice_identifier.identifier = $1;
1490 $$->value.choice_identifier.value = $3;
1491 }
1492 | '{' { asn1p_lexer_hack_push_opaque_state(); } Opaque /* '}' */ {
1493 $$ = asn1p_value_frombuf($3.buf, $3.len, 0);
1494 checkmem($$);
1495 $$->type = ATV_UNPARSED;
1496 }
1497 | TOK_NULL {
1498 $$ = asn1p_value_fromint(0);
1499 checkmem($$);
1500 $$->type = ATV_NULL;
1501 }
1502 ;
1503
1504 SimpleValue:
1505 TOK_FALSE {
1506 $$ = asn1p_value_fromint(0);
1507 checkmem($$);
1508 $$->type = ATV_FALSE;
1509 }
1510 | TOK_TRUE {
1511 $$ = asn1p_value_fromint(0);
1512 checkmem($$);
1513 $$->type = ATV_TRUE;
1514 }
1515 | TOK_bstring {
1516 $$ = _convert_bitstring2binary($1, 'B');
1517 checkmem($$);
1518 }
1519 | TOK_hstring {
1520 $$ = _convert_bitstring2binary($1, 'H');
1521 checkmem($$);
1522 }
1523 | RestrictedCharacterStringValue {
1524 $$ = $$;
1525 }
1526 | SignedNumber {
1527 $$ = $1;
1528 }
1529 ;
1530
1531 DefinedValue:
1532 Identifier {
1533 asn1p_ref_t *ref;
1534 int ret;
1535 ref = asn1p_ref_new(yylineno);
1536 checkmem(ref);
1537 ret = asn1p_ref_add_component(ref, $1, RLT_lowercase);
1538 checkmem(ret == 0);
1539 $$ = asn1p_value_fromref(ref, 0);
1540 checkmem($$);
1541 free($1);
1542 }
1543 | TypeRefName '.' Identifier {
1544 asn1p_ref_t *ref;
1545 int ret;
1546 ref = asn1p_ref_new(yylineno);
1547 checkmem(ref);
1548 ret = asn1p_ref_add_component(ref, $1, RLT_UNKNOWN);
1549 checkmem(ret == 0);
1550 ret = asn1p_ref_add_component(ref, $3, RLT_lowercase);
1551 checkmem(ret == 0);
1552 $$ = asn1p_value_fromref(ref, 0);
1553 checkmem($$);
1554 free($1);
1555 free($3);
1556 }
1557 ;
1558
1559
1560 RestrictedCharacterStringValue:
1561 TOK_cstring {
1562 $$ = asn1p_value_frombuf($1.buf, $1.len, 0);
1563 checkmem($$);
1564 }
1565 | TOK_tuple {
1566 $$ = asn1p_value_fromint($1);
1567 checkmem($$);
1568 $$->type = ATV_TUPLE;
1569 }
1570 | TOK_quadruple {
1571 $$ = asn1p_value_fromint($1);
1572 checkmem($$);
1573 $$->type = ATV_QUADRUPLE;
1574 }
1575 ;
1576
1577 Opaque:
1578 TOK_opaque {
1579 $$.len = $1.len + 1;
1580 $$.buf = malloc($$.len + 1);
1581 checkmem($$.buf);
1582 $$.buf[0] = '{';
1583 memcpy($$.buf + 1, $1.buf, $1.len);
1584 $$.buf[$$.len] = '\0';
1585 free($1.buf);
1586 }
1587 | Opaque TOK_opaque {
1588 int newsize = $1.len + $2.len;
1589 char *p = malloc(newsize + 1);
1590 checkmem(p);
1591 memcpy(p , $1.buf, $1.len);
1592 memcpy(p + $1.len, $2.buf, $2.len);
1593 p[newsize] = '\0';
1594 free($1.buf);
1595 free($2.buf);
1596 $$.buf = p;
1597 $$.len = newsize;
1598 }
1599 ;
1600
1601 BasicTypeId:
1602 TOK_BOOLEAN { $$ = ASN_BASIC_BOOLEAN; }
1603 | TOK_NULL { $$ = ASN_BASIC_NULL; }
1604 | TOK_REAL { $$ = ASN_BASIC_REAL; }
1605 | BasicTypeId_UniverationCompatible { $$ = $1; }
1606 | TOK_OCTET TOK_STRING { $$ = ASN_BASIC_OCTET_STRING; }
1607 | TOK_OBJECT TOK_IDENTIFIER { $$ = ASN_BASIC_OBJECT_IDENTIFIER; }
1608 | TOK_RELATIVE_OID { $$ = ASN_BASIC_RELATIVE_OID; }
1609 | TOK_EXTERNAL { $$ = ASN_BASIC_EXTERNAL; }
1610 | TOK_EMBEDDED TOK_PDV { $$ = ASN_BASIC_EMBEDDED_PDV; }
1611 | TOK_CHARACTER TOK_STRING { $$ = ASN_BASIC_CHARACTER_STRING; }
1612 | TOK_UTCTime { $$ = ASN_BASIC_UTCTime; }
1613 | TOK_GeneralizedTime { $$ = ASN_BASIC_GeneralizedTime; }
1614 | BasicString { $$ = $1; }
1615 ;
1616
1617 /*
1618 * A type identifier which may be used with "{ a(1), b(2) }" clause.
1619 */
1620 BasicTypeId_UniverationCompatible:
1621 TOK_INTEGER { $$ = ASN_BASIC_INTEGER; }
1622 | TOK_ENUMERATED { $$ = ASN_BASIC_ENUMERATED; }
1623 | TOK_BIT TOK_STRING { $$ = ASN_BASIC_BIT_STRING; }
1624 ;
1625
1626 BasicType:
1627 BasicTypeId {
1628 $$ = NEW_EXPR();
1629 checkmem($$);
1630 $$->expr_type = $1;
1631 $$->meta_type = AMT_TYPE;
1632 }
1633 | BasicTypeId_UniverationCompatible UniverationDefinition {
1634 if($2) {
1635 $$ = $2;
1636 } else {
1637 $$ = NEW_EXPR();
1638 checkmem($$);
1639 }
1640 $$->expr_type = $1;
1641 $$->meta_type = AMT_TYPE;
1642 }
1643 ;
1644
1645 BasicString:
1646 TOK_BMPString { $$ = ASN_STRING_BMPString; }
1647 | TOK_GeneralString {
1648 $$ = ASN_STRING_GeneralString;
1649 fprintf(stderr, "WARNING: GeneralString is not fully supported\n");
1650 }
1651 | TOK_GraphicString {
1652 $$ = ASN_STRING_GraphicString;
1653 fprintf(stderr, "WARNING: GraphicString is not fully supported\n");
1654 }
1655 | TOK_IA5String { $$ = ASN_STRING_IA5String; }
1656 | TOK_ISO646String { $$ = ASN_STRING_ISO646String; }
1657 | TOK_NumericString { $$ = ASN_STRING_NumericString; }
1658 | TOK_PrintableString { $$ = ASN_STRING_PrintableString; }
1659 | TOK_T61String {
1660 $$ = ASN_STRING_T61String;
1661 fprintf(stderr, "WARNING: T61String is not fully supported\n");
1662 }
1663 | TOK_TeletexString { $$ = ASN_STRING_TeletexString; }
1664 | TOK_UniversalString { $$ = ASN_STRING_UniversalString; }
1665 | TOK_UTF8String { $$ = ASN_STRING_UTF8String; }
1666 | TOK_VideotexString {
1667 $$ = ASN_STRING_VideotexString;
1668 fprintf(stderr, "WARNING: VideotexString is not fully supported\n");
1669 }
1670 | TOK_VisibleString { $$ = ASN_STRING_VisibleString; }
1671 | TOK_ObjectDescriptor { $$ = ASN_STRING_ObjectDescriptor; }
1672 ;
1673
1674
1675 /*
1676 * Data type constraints.
1677 */
1678 UnionMark: '|' | TOK_UNION;
1679 IntersectionMark: '^' | TOK_INTERSECTION;
1680
1681 optConstraints:
1682 { $$ = 0; }
1683 | Constraint {
1684 $$ = $1;
1685 }
1686 ;
1687
1688 Constraint:
1689 SubtypeConstraint
1690 ;
1691
1692 SubtypeConstraint:
1693 SetOfConstraints {
1694 CONSTRAINT_INSERT($$, ACT_CA_SET, $1, 0);
1695 }
1696 | TOK_SIZE '(' ElementSetSpecs ')' {
1697 /*
1698 * This is a special case, for compatibility purposes.
1699 * It goes without parentheses.
1700 */
1701 CONSTRAINT_INSERT($$, ACT_CT_SIZE, $3, 0);
1702 }
1703 ;
1704
1705 SetOfConstraints:
1706 '(' ElementSetSpecs ')' {
1707 $$ = $2;
1708 }
1709 | SetOfConstraints '(' ElementSetSpecs ')' {
1710 CONSTRAINT_INSERT($$, ACT_CA_SET, $1, $3);
1711 }
1712 ;
1713
1714 ElementSetSpecs:
1715 TOK_ThreeDots {
1716 $$ = asn1p_constraint_new(yylineno);
1717 $$->type = ACT_EL_EXT;
1718 }
1719 | ElementSetSpec {
1720 $$ = $1;
1721 }
1722 | ElementSetSpec ',' TOK_ThreeDots {
1723 asn1p_constraint_t *ct;
1724 ct = asn1p_constraint_new(yylineno);
1725 ct->type = ACT_EL_EXT;
1726 CONSTRAINT_INSERT($$, ACT_CA_CSV, $1, ct);
1727 }
1728 | ElementSetSpec ',' TOK_ThreeDots ',' ElementSetSpec {
1729 asn1p_constraint_t *ct;
1730 ct = asn1p_constraint_new(yylineno);
1731 ct->type = ACT_EL_EXT;
1732 CONSTRAINT_INSERT($$, ACT_CA_CSV, $1, ct);
1733 ct = $$;
1734 CONSTRAINT_INSERT($$, ACT_CA_CSV, ct, $5);
1735 }
1736 | GeneralConstraint {
1737 $$ = $1;
1738 }
1739 ;
1740
1741 ElementSetSpec:
1742 Unions
1743 | TOK_ALL TOK_EXCEPT ConstraintSubtypeElement {
1744 CONSTRAINT_INSERT($$, ACT_CA_AEX, $3, 0);
1745 }
1746 ;
1747
1748 Unions:
1749 Intersections
1750 | Unions UnionMark Intersections {
1751 CONSTRAINT_INSERT($$, ACT_CA_UNI, $1, $3);
1752 }
1753 ;
1754
1755 Intersections:
1756 IntersectionElements
1757 | Intersections IntersectionMark IntersectionElements {
1758 CONSTRAINT_INSERT($$, ACT_CA_INT, $1, $3);
1759 }
1760 ;
1761
1762
1763 IntersectionElements:
1764 ConstraintSubtypeElement
1765 | ConstraintSubtypeElement TOK_EXCEPT ConstraintSubtypeElement {
1766 CONSTRAINT_INSERT($$, ACT_CA_EXC, $1, $3);
1767 }
1768 ;
1769
1770 ConstraintSubtypeElement:
1771 ConstraintSpec '(' ElementSetSpecs ')' {
1772 int ret;
1773 $$ = asn1p_constraint_new(yylineno);
1774 checkmem($$);
1775 $$->type = $1;
1776 ret = asn1p_constraint_insert($$, $3);
1777 checkmem(ret == 0);
1778 }
1779 | '(' ElementSetSpecs ')' {
1780 int ret;
1781 $$ = asn1p_constraint_new(yylineno);
1782 checkmem($$);
1783 $$->type = ACT_CA_SET;
1784 ret = asn1p_constraint_insert($$, $2);
1785 checkmem(ret == 0);
1786 }
1787 | SingleValue {
1788 $$ = asn1p_constraint_new(yylineno);
1789 checkmem($$);
1790 $$->type = ACT_EL_VALUE;
1791 $$->value = $1;
1792 }
1793 | ContainedSubtype {
1794 $$ = asn1p_constraint_new(yylineno);
1795 checkmem($$);
1796 $$->type = ACT_EL_TYPE;
1797 $$->containedSubtype = $1;
1798 }
1799 | SingleValue ConstraintRangeSpec SingleValue {
1800 $$ = asn1p_constraint_new(yylineno);
1801 checkmem($$);
1802 $$->type = $2;
1803 $$->range_start = $1;
1804 $$->range_stop = $3;
1805 }
1806 | TOK_MIN ConstraintRangeSpec SingleValue {
1807 $$ = asn1p_constraint_new(yylineno);
1808 checkmem($$);
1809 $$->type = $2;
1810 $$->range_start = asn1p_value_fromint(-123);
1811 $$->range_stop = $3;
1812 $$->range_start->type = ATV_MIN;
1813 }
1814 | SingleValue ConstraintRangeSpec TOK_MAX {
1815 $$ = asn1p_constraint_new(yylineno);
1816 checkmem($$);
1817 $$->type = $2;
1818 $$->range_start = $1;
1819 $$->range_stop = asn1p_value_fromint(321);
1820 $$->range_stop->type = ATV_MAX;
1821 }
1822 | TOK_MIN ConstraintRangeSpec TOK_MAX {
1823 $$ = asn1p_constraint_new(yylineno);
1824 checkmem($$);
1825 $$->type = $2;
1826 $$->range_start = asn1p_value_fromint(-123);
1827 $$->range_stop = asn1p_value_fromint(321);
1828 $$->range_start->type = ATV_MIN;
1829 $$->range_stop->type = ATV_MAX;
1830 }
1831 | InnerTypeConstraint {
1832 $$ = $1;
1833 }
1834 | PatternConstraint {
1835 $$ = $1;
1836 }
1837 ;
1838
1839 PatternConstraint:
1840 TOK_PATTERN TOK_cstring {
1841 $$ = asn1p_constraint_new(yylineno);
1842 $$->type = ACT_CT_PATTERN;
1843 $$->value = asn1p_value_frombuf($2.buf, $2.len, 0);
1844 }
1845 | TOK_PATTERN Identifier {
1846 asn1p_ref_t *ref;
1847 $$ = asn1p_constraint_new(yylineno);
1848 $$->type = ACT_CT_PATTERN;
1849 ref = asn1p_ref_new(yylineno);
1850 asn1p_ref_add_component(ref, $2, RLT_lowercase);
1851 $$->value = asn1p_value_fromref(ref, 0);
1852 }
1853 ;
1854
1855 ConstraintSpec:
1856 TOK_SIZE {
1857 $$ = ACT_CT_SIZE;
1858 }
1859 | TOK_FROM {
1860 $$ = ACT_CT_FROM;
1861 }
1862 ;
1863
1864 SingleValue:
1865 TOK_FALSE {
1866 $$ = asn1p_value_fromint(0);
1867 checkmem($$);
1868 $$->type = ATV_FALSE;
1869 }
1870 | TOK_TRUE {
1871 $$ = asn1p_value_fromint(1);
1872 checkmem($$);
1873 $$->type = ATV_TRUE;
1874 }
1875 | RealValue
1876 | RestrictedCharacterStringValue
1877 | BitStringValue
1878 | Identifier {
1879 asn1p_ref_t *ref;
1880 int ret;
1881 ref = asn1p_ref_new(yylineno);
1882 checkmem(ref);
1883 ret = asn1p_ref_add_component(ref, $1, RLT_lowercase);
1884 checkmem(ret == 0);
1885 $$ = asn1p_value_fromref(ref, 0);
1886 checkmem($$);
1887 free($1);
1888 }
1889 ;
1890
1891 BitStringValue:
1892 TOK_bstring {
1893 $$ = _convert_bitstring2binary($1, 'B');
1894 checkmem($$);
1895 }
1896 | TOK_hstring {
1897 $$ = _convert_bitstring2binary($1, 'H');
1898 checkmem($$);
1899 }
1900 ;
1901
1902 ContainedSubtype:
1903 TypeRefName {
1904 asn1p_ref_t *ref;
1905 int ret;
1906 ref = asn1p_ref_new(yylineno);
1907 checkmem(ref);
1908 ret = asn1p_ref_add_component(ref, $1, RLT_UNKNOWN);
1909 checkmem(ret == 0);
1910 $$ = asn1p_value_fromref(ref, 0);
1911 checkmem($$);
1912 free($1);
1913 }
1914 ;
1915
1916 InnerTypeConstraint:
1917 TOK_WITH TOK_COMPONENT SetOfConstraints {
1918 CONSTRAINT_INSERT($$, ACT_CT_WCOMP, $3, 0);
1919 }
1920 | TOK_WITH TOK_COMPONENTS '{' WithComponentsList '}' {
1921 CONSTRAINT_INSERT($$, ACT_CT_WCOMPS, $4, 0);
1922 }
1923 ;
1924
1925 WithComponentsList:
1926 WithComponentsElement {
1927 $$ = $1;
1928 }
1929 | WithComponentsList ',' WithComponentsElement {
1930 CONSTRAINT_INSERT($$, ACT_CT_WCOMPS, $1, $3);
1931 }
1932 ;
1933
1934 WithComponentsElement:
1935 TOK_ThreeDots {
1936 $$ = asn1p_constraint_new(yylineno);
1937 checkmem($$);
1938 $$->type = ACT_EL_EXT;
1939 $$->value = asn1p_value_frombuf("...", 3, 1);
1940 }
1941 | Identifier optConstraints optPresenceConstraint {
1942 $$ = asn1p_constraint_new(yylineno);
1943 checkmem($$);
1944 $$->type = ACT_EL_VALUE;
1945 $$->value = asn1p_value_frombuf($1, strlen($1), 0);
1946 $$->presence = $3;
1947 if($2) asn1p_constraint_insert($$, $2);
1948 }
1949 ;
1950
1951 /*
1952 * presence constraint for WithComponents
1953 */
1954 optPresenceConstraint:
1955 { $$ = ACPRES_DEFAULT; }
1956 | PresenceConstraint { $$ = $1; }
1957 ;
1958
1959 PresenceConstraint:
1960 TOK_PRESENT {
1961 $$ = ACPRES_PRESENT;
1962 }
1963 | TOK_ABSENT {
1964 $$ = ACPRES_ABSENT;
1965 }
1966 | TOK_OPTIONAL {
1967 $$ = ACPRES_OPTIONAL;
1968 }
1969 ;
1970
1971
1972 /* X.682 */
1973 GeneralConstraint:
1974 UserDefinedConstraint
1975 | TableConstraint
1976 | ContentsConstraint
1977 ;
1978
1979 UserDefinedConstraint:
1980 TOK_CONSTRAINED TOK_BY '{'
1981 { asn1p_lexer_hack_push_opaque_state(); } Opaque /* '}' */ {
1982 $$ = asn1p_constraint_new(yylineno);
1983 checkmem($$);
1984 $$->type = ACT_CT_CTDBY;
1985 $$->value = asn1p_value_frombuf($5.buf, $5.len, 0);
1986 checkmem($$->value);
1987 $$->value->type = ATV_UNPARSED;
1988 }
1989 ;
1990
1991 ContentsConstraint:
1992 TOK_CONTAINING Type {
1993 $$ = asn1p_constraint_new(yylineno);
1994 $$->type = ACT_CT_CTNG;
1995 $$->value = asn1p_value_fromtype($2);
1996 }
1997 ;
1998
1999 ConstraintRangeSpec:
2000 TOK_TwoDots { $$ = ACT_EL_RANGE; }
2001 | TOK_TwoDots '<' { $$ = ACT_EL_RLRANGE; }
2002 | '<' TOK_TwoDots { $$ = ACT_EL_LLRANGE; }
2003 | '<' TOK_TwoDots '<' { $$ = ACT_EL_ULRANGE; }
2004 ;
2005 TableConstraint:
2006 SimpleTableConstraint {
2007 $$ = $1;
2008 }
2009 | ComponentRelationConstraint {
2010 $$ = $1;
2011 }
2012 ;
2013
2014 /*
2015 * "{ExtensionSet}"
2016 */
2017 SimpleTableConstraint:
2018 '{' TypeRefName '}' {
2019 asn1p_ref_t *ref = asn1p_ref_new(yylineno);
2020 asn1p_constraint_t *ct;
2021 int ret;
2022 ret = asn1p_ref_add_component(ref, $2, 0);
2023 checkmem(ret == 0);
2024 ct = asn1p_constraint_new(yylineno);
2025 checkmem($$);
2026 ct->type = ACT_EL_VALUE;
2027 ct->value = asn1p_value_fromref(ref, 0);
2028 CONSTRAINT_INSERT($$, ACT_CA_CRC, ct, 0);
2029 }
2030 ;
2031
2032 ComponentRelationConstraint:
2033 SimpleTableConstraint '{' AtNotationList '}' {
2034 CONSTRAINT_INSERT($$, ACT_CA_CRC, $1, $3);
2035 }
2036 ;
2037
2038 AtNotationList:
2039 AtNotationElement {
2040 $$ = asn1p_constraint_new(yylineno);
2041 checkmem($$);
2042 $$->type = ACT_EL_VALUE;
2043 $$->value = asn1p_value_fromref($1, 0);
2044 }
2045 | AtNotationList ',' AtNotationElement {
2046 asn1p_constraint_t *ct;
2047 ct = asn1p_constraint_new(yylineno);
2048 checkmem(ct);
2049 ct->type = ACT_EL_VALUE;
2050 ct->value = asn1p_value_fromref($3, 0);
2051 CONSTRAINT_INSERT($$, ACT_CA_CSV, $1, ct);
2052 }
2053 ;
2054
2055 /*
2056 * @blah
2057 */
2058 AtNotationElement:
2059 '@' ComponentIdList {
2060 char *p = malloc(strlen($2) + 2);
2061 int ret;
2062 *p = '@';
2063 strcpy(p + 1, $2);
2064 $$ = asn1p_ref_new(yylineno);
2065 ret = asn1p_ref_add_component($$, p, 0);
2066 checkmem(ret == 0);
2067 free(p);
2068 free($2);
2069 }
2070 | '@' '.' ComponentIdList {
2071 char *p = malloc(strlen($3) + 3);
2072 int ret;
2073 p[0] = '@';
2074 p[1] = '.';
2075 strcpy(p + 2, $3);
2076 $$ = asn1p_ref_new(yylineno);
2077 ret = asn1p_ref_add_component($$, p, 0);
2078 checkmem(ret == 0);
2079 free(p);
2080 free($3);
2081 }
2082 ;
2083
2084 /* identifier "." ... */
2085 ComponentIdList:
2086 Identifier {
2087 $$ = $1;
2088 }
2089 | ComponentIdList '.' Identifier {
2090 int l1 = strlen($1);
2091 int l3 = strlen($3);
2092 $$ = malloc(l1 + 1 + l3 + 1);
2093 memcpy($$, $1, l1);
2094 $$[l1] = '.';
2095 memcpy($$ + l1 + 1, $3, l3);
2096 $$[l1 + 1 + l3] = '\0';
2097 }
2098 ;
2099
2100
2101
2102 /*
2103 * MARKERS
2104 */
2105
2106 optMarker:
2107 {
2108 $$.flags = EM_NOMARK;
2109 $$.default_value = 0;
2110 }
2111 | Marker { $$ = $1; }
2112 ;
2113
2114 Marker:
2115 TOK_OPTIONAL {
2116 $$.flags = EM_OPTIONAL | EM_INDIRECT;
2117 $$.default_value = 0;
2118 }
2119 | TOK_DEFAULT Value {
2120 $$.flags = EM_DEFAULT;
2121 $$.default_value = $2;
2122 }
2123 ;
2124
2125 /*
2126 * Universal enumeration definition to use in INTEGER and ENUMERATED.
2127 * === EXAMPLE ===
2128 * Gender ::= ENUMERATED { unknown(0), male(1), female(2) }
2129 * Temperature ::= INTEGER { absolute-zero(-273), freezing(0), boiling(100) }
2130 * === EOF ===
2131 */
2132 /*
2133 optUniverationDefinition:
2134 { $$ = 0; }
2135 | UniverationDefinition {
2136 $$ = $1;
2137 }
2138 ;
2139 */
2140
2141 UniverationDefinition:
2142 '{' '}' {
2143 $$ = NEW_EXPR();
2144 checkmem($$);
2145 }
2146 | '{' UniverationList '}' {
2147 $$ = $2;
2148 }
2149 ;
2150
2151 UniverationList:
2152 UniverationElement {
2153 $$ = NEW_EXPR();
2154 checkmem($$);
2155 asn1p_expr_add($$, $1);
2156 }
2157 | UniverationList ',' UniverationElement {
2158 $$ = $1;
2159 asn1p_expr_add($$, $3);
2160 }
2161 ;
2162
2163 UniverationElement:
2164 Identifier {
2165 $$ = NEW_EXPR();
2166 checkmem($$);
2167 $$->expr_type = A1TC_UNIVERVAL;
2168 $$->meta_type = AMT_VALUE;
2169 $$->Identifier = $1;
2170 }
2171 | Identifier '(' SignedNumber ')' {
2172 $$ = NEW_EXPR();
2173 checkmem($$);
2174 $$->expr_type = A1TC_UNIVERVAL;
2175 $$->meta_type = AMT_VALUE;
2176 $$->Identifier = $1;
2177 $$->value = $3;
2178 }
2179 | Identifier '(' DefinedValue ')' {
2180 $$ = NEW_EXPR();
2181 checkmem($$);
2182 $$->expr_type = A1TC_UNIVERVAL;
2183 $$->meta_type = AMT_VALUE;
2184 $$->Identifier = $1;
2185 $$->value = $3;
2186 }
2187 | SignedNumber {
2188 $$ = NEW_EXPR();
2189 checkmem($$);
2190 $$->expr_type = A1TC_UNIVERVAL;
2191 $$->meta_type = AMT_VALUE;
2192 $$->value = $1;
2193 }
2194 | TOK_ThreeDots {
2195 $$ = NEW_EXPR();
2196 checkmem($$);
2197 $$->Identifier = strdup("...");
2198 checkmem($$->Identifier);
2199 $$->expr_type = A1TC_EXTENSIBLE;
2200 $$->meta_type = AMT_VALUE;
2201 }
2202 ;
2203
2204 SignedNumber:
2205 TOK_number {
2206 $$ = asn1p_value_fromint($1);
2207 checkmem($$);
2208 }
2209 | TOK_number_negative {
2210 $$ = asn1p_value_fromint($1);
2211 checkmem($$);
2212 }
2213 ;
2214
2215 RealValue:
2216 SignedNumber
2217 | TOK_realnumber {
2218 $$ = asn1p_value_fromdouble($1);
2219 checkmem($$);
2220 }
2221 ;
2222
2223 /*
2224 * SEQUENCE definition.
2225 * === EXAMPLE ===
2226 * Struct1 ::= SEQUENCE {
2227 * memb1 Struct2,
2228 * memb2 SEQUENCE OF {
2229 * memb2-1 Struct 3
2230 * }
2231 * }
2232 * === EOF ===
2233 */
2234
2235
2236
2237 /*
2238 * SET definition.
2239 * === EXAMPLE ===
2240 * Person ::= SET {
2241 * name [0] PrintableString (SIZE(1..20)),
2242 * country [1] PrintableString (SIZE(1..20)) DEFAULT default-country,
2243 * }
2244 * === EOF ===
2245 */
2246
2247 optTag:
2248 { memset(&$$, 0, sizeof($$)); }
2249 | Tag { $$ = $1; }
2250 ;
2251
2252 Tag:
2253 TagTypeValue TagPlicit {
2254 $$ = $1;
2255 $$.tag_mode = $2.tag_mode;
2256 }
2257 ;
2258
2259 TagTypeValue:
2260 '[' TagClass TOK_number ']' {
2261 $$ = $2;
2262 $$.tag_value = $3;
2263 };
2264
2265 TagClass:
2266 { $$.tag_class = TC_CONTEXT_SPECIFIC; }
2267 | TOK_UNIVERSAL { $$.tag_class = TC_UNIVERSAL; }
2268 | TOK_APPLICATION { $$.tag_class = TC_APPLICATION; }
2269 | TOK_PRIVATE { $$.tag_class = TC_PRIVATE; }
2270 ;
2271
2272 TagPlicit:
2273 { $$.tag_mode = TM_DEFAULT; }
2274 | TOK_IMPLICIT { $$.tag_mode = TM_IMPLICIT; }
2275 | TOK_EXPLICIT { $$.tag_mode = TM_EXPLICIT; }
2276 ;
2277
2278 TypeRefName:
2279 TOK_typereference {
2280 checkmem($1);
2281 $$ = $1;
2282 }
2283 | TOK_capitalreference {
2284 checkmem($1);
2285 $$ = $1;
2286 }
2287 ;
2288
2289
2290 ObjectClassReference:
2291 TOK_capitalreference {
2292 checkmem($1);
2293 $$ = $1;
2294 }
2295 ;
2296
2297 optIdentifier:
2298 { $$ = 0; }
2299 | Identifier {
2300 $$ = $1;
2301 }
2302 ;
2303
2304 Identifier:
2305 TOK_identifier {
2306 checkmem($1);
2307 $$ = $1;
2308 }
2309 ;
2310
2311 %%
2312
2313
2314 /*
2315 * Convert Xstring ('0101'B or '5'H) to the binary vector.
2316 */
2317 static asn1p_value_t *
2318 _convert_bitstring2binary(char *str, int base) {
2319 asn1p_value_t *val;
2320 int slen;
2321 int memlen;
2322 int baselen;
2323 int bits;
2324 uint8_t *binary_vector;
2325 uint8_t *bv_ptr;
2326 uint8_t cur_val;
2327
2328 assert(str);
2329 assert(str[0] == '\'');
2330
2331 switch(base) {
2332 case 'B':
2333 baselen = 1;
2334 break;
2335 case 'H':
2336 baselen = 4;
2337 break;
2338 default:
2339 assert(base == 'B' || base == 'H');
2340 errno = EINVAL;
2341 return NULL;
2342 }
2343
2344 slen = strlen(str);
2345 assert(str[slen - 1] == base);
2346 assert(str[slen - 2] == '\'');
2347
2348 memlen = slen / (8 / baselen); /* Conservative estimate */
2349
2350 bv_ptr = binary_vector = malloc(memlen + 1);
2351 if(bv_ptr == NULL)
2352 /* ENOMEM */
2353 return NULL;
2354
2355 cur_val = 0;
2356 bits = 0;
2357 while(*(++str) != '\'') {
2358 switch(baselen) {
2359 case 1:
2360 switch(*str) {
2361 case '1':
2362 cur_val |= 1 << (7 - (bits % 8));
2363 case '0':
2364 break;
2365 default:
2366 assert(!"_y UNREACH1");
2367 case ' ': case '\r': case '\n':
2368 continue;
2369 }
2370 break;
2371 case 4:
2372 switch(*str) {
2373 case '0': case '1': case '2': case '3': case '4':
2374 case '5': case '6': case '7': case '8': case '9':
2375 cur_val |= (*str - '0') << (4 - (bits % 8));
2376 break;
2377 case 'A': case 'B': case 'C':
2378 case 'D': case 'E': case 'F':
2379 cur_val |= ((*str - 'A') + 10)
2380 << (4 - (bits % 8));
2381 break;
2382 default:
2383 assert(!"_y UNREACH2");
2384 case ' ': case '\r': case '\n':
2385 continue;
2386 }
2387 break;
2388 }
2389
2390 bits += baselen;
2391 if((bits % 8) == 0) {
2392 *bv_ptr++ = cur_val;
2393 cur_val = 0;
2394 }
2395 }
2396
2397 *bv_ptr = cur_val;
2398 assert((bv_ptr - binary_vector) <= memlen);
2399
2400 val = asn1p_value_frombits(binary_vector, bits, 0);
2401 if(val == NULL) {
2402 free(binary_vector);
2403 }
2404
2405 return val;
2406 }
2407
2408 /*
2409 * For unnamed types (used in old X.208 compliant modules)
2410 * generate some sort of interim names, to not to force human being to fix
2411 * the specification's compliance to modern ASN.1 standards.
2412 */
2413 static void
2414 _fixup_anonymous_identifier(asn1p_expr_t *expr) {
2415 char *p;
2416 assert(expr->Identifier == 0);
2417
2418 /*
2419 * Try to figure out the type name
2420 * without going too much into details
2421 */
2422 expr->Identifier = ASN_EXPR_TYPE2STR(expr->expr_type);
2423 if(expr->reference && expr->reference->comp_count > 0)
2424 expr->Identifier = expr->reference->components[0].name;
2425
2426 fprintf(stderr,
2427 "WARNING: Line %d: expected lower-case member identifier, "
2428 "found an unnamed %s.\n"
2429 "WARNING: Obsolete X.208 syntax detected, "
2430 "please give the member a name.\n",
2431 yylineno, expr->Identifier ? expr->Identifier : "type");
2432
2433 if(!expr->Identifier)
2434 expr->Identifier = "unnamed";
2435 expr->Identifier = strdup(expr->Identifier);
2436 assert(expr->Identifier);
2437 /* Make a lowercase identifier from the type name */
2438 for(p = expr->Identifier; *p; p++) {
2439 switch(*p) {
2440 case 'A' ... 'Z': *p += 32; break;
2441 case ' ': *p = '_'; break;
2442 case '-': *p = '_'; break;
2443 }
2444 }
2445 fprintf(stderr, "NOTE: Assigning temporary identifier \"%s\". "
2446 "Name clash may occur later.\n",
2447 expr->Identifier);
2448 }
2449
2450 int
2451 yyerror(const char *msg) {
2452 extern char *asn1p_text;
2453 fprintf(stderr,
2454 "ASN.1 grammar parse error "
2455 "near line %d (token \"%s\"): %s\n",
2456 yylineno, asn1p_text, msg);
2457 return -1;
2458 }
2459