blob: 7b2c136687e130bb36de99249421b7483dc22d6b [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001%{
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 YYERROR_VERBOSE
13
14int yylex(void);
15int yyerror(const char *msg);
16void asn1p_lexer_hack_push_opaque_state(void);
17void asn1p_lexer_hack_enable_with_syntax(void);
18#define yylineno asn1p_lineno
19extern int asn1p_lineno;
20
21
22static asn1p_value_t *
23 _convert_bitstring2binary(char *str, int base);
24
25#define checkmem(ptr) do { \
26 if(!(ptr)) \
27 return yyerror("Memory failure"); \
28 } while(0)
29
30#define CONSTRAINT_INSERT(root, constr_type, arg1, arg2) do { \
31 if(arg1->type != constr_type) { \
32 int __ret; \
33 root = asn1p_constraint_new(yylineno); \
34 checkmem(root); \
35 root->type = constr_type; \
36 __ret = asn1p_constraint_insert(root, \
37 arg1); \
38 checkmem(__ret == 0); \
39 } else { \
40 root = arg1; \
41 } \
42 if(arg2) { \
43 int __ret \
44 = asn1p_constraint_insert(root, arg2); \
45 checkmem(__ret == 0); \
46 } \
47 } while(0)
48
49%}
50
51
52/*
53 * Token value definition.
54 * a_*: ASN-specific types.
55 * tv_*: Locally meaningful types.
56 */
57%union {
58 asn1p_t *a_grammar;
59 asn1p_module_flags_e a_module_flags;
60 asn1p_module_t *a_module;
61 asn1p_expr_type_e a_type; /* ASN.1 Type */
62 asn1p_expr_t *a_expr; /* Constructed collection */
63 asn1p_constraint_t *a_constr; /* Constraint */
64 enum asn1p_constraint_type_e a_ctype;/* Constraint type */
65 asn1p_xports_t *a_xports; /* IMports/EXports */
66 asn1p_oid_t *a_oid; /* Object Identifier */
67 asn1p_oid_arc_t a_oid_arc; /* Single OID's arc */
68 struct asn1p_type_tag_s a_tag; /* A tag */
69 asn1p_ref_t *a_ref; /* Reference to custom type */
70 asn1p_wsyntx_t *a_wsynt; /* WITH SYNTAX contents */
71 asn1p_wsyntx_chunk_t *a_wchunk; /* WITH SYNTAX chunk */
72 struct asn1p_ref_component_s a_refcomp; /* Component of a reference */
73 asn1p_value_t *a_value; /* Number, DefinedValue, etc */
74 struct asn1p_param_s a_parg; /* A parameter argument */
75 asn1p_paramlist_t *a_plist; /* A pargs list */
76 enum asn1p_expr_marker_e a_marker; /* OPTIONAL/DEFAULT */
77 enum asn1p_constr_pres_e a_pres; /* PRESENT/ABSENT/OPTIONAL */
78 asn1_integer_t a_int;
79 char *tv_str;
80 struct {
81 char *buf;
82 int len;
83 } tv_opaque;
84 struct {
85 char *name;
86 struct asn1p_type_tag_s tag;
87 } tv_nametag;
88};
89
90/*
91 * Token types returned by scanner.
92 */
93%token TOK_PPEQ /* "::=", Pseudo Pascal EQuality */
94%token <tv_opaque> TOK_opaque /* opaque data (driven from .y) */
95%token <tv_str> TOK_bstring
96%token <tv_opaque> TOK_cstring
97%token <tv_str> TOK_hstring
98%token <tv_str> TOK_identifier
99%token <a_int> TOK_number
100%token <a_int> TOK_number_negative
101%token <tv_str> TOK_typereference
102%token <tv_str> TOK_objectclassreference /* "CLASS1" */
103%token <tv_str> TOK_typefieldreference /* "&Pork" */
104%token <tv_str> TOK_valuefieldreference /* "&id" */
105
106/*
107 * Token types representing ASN.1 standard keywords.
108 */
109%token TOK_ABSENT
110%token TOK_ABSTRACT_SYNTAX
111%token TOK_ALL
112%token TOK_ANY
113%token TOK_APPLICATION
114%token TOK_AUTOMATIC
115%token TOK_BEGIN
116%token TOK_BIT
117%token TOK_BMPString
118%token TOK_BOOLEAN
119%token TOK_BY
120%token TOK_CHARACTER
121%token TOK_CHOICE
122%token TOK_CLASS
123%token TOK_COMPONENT
124%token TOK_COMPONENTS
125%token TOK_CONSTRAINED
126%token TOK_CONTAINING
127%token TOK_DEFAULT
128%token TOK_DEFINITIONS
129%token TOK_DEFINED
130%token TOK_EMBEDDED
131%token TOK_ENCODED
132%token TOK_END
133%token TOK_ENUMERATED
134%token TOK_EXPLICIT
135%token TOK_EXPORTS
136%token TOK_EXTENSIBILITY
137%token TOK_EXTERNAL
138%token TOK_FALSE
139%token TOK_FROM
140%token TOK_GeneralizedTime
141%token TOK_GeneralString
142%token TOK_GraphicString
143%token TOK_IA5String
144%token TOK_IDENTIFIER
145%token TOK_IMPLICIT
146%token TOK_IMPLIED
147%token TOK_IMPORTS
148%token TOK_INCLUDES
149%token TOK_INSTANCE
150%token TOK_INTEGER
151%token TOK_ISO646String
152%token TOK_MAX
153%token TOK_MIN
154%token TOK_MINUS_INFINITY
155%token TOK_NULL
156%token TOK_NumericString
157%token TOK_OBJECT
158%token TOK_ObjectDescriptor
159%token TOK_OCTET
160%token TOK_OF
161%token TOK_OPTIONAL
162%token TOK_PATTERN
163%token TOK_PDV
164%token TOK_PLUS_INFINITY
165%token TOK_PRESENT
166%token TOK_PrintableString
167%token TOK_PRIVATE
168%token TOK_REAL
169%token TOK_RELATIVE_OID
170%token TOK_SEQUENCE
171%token TOK_SET
172%token TOK_SIZE
173%token TOK_STRING
174%token TOK_SYNTAX
175%token TOK_T61String
176%token TOK_TAGS
177%token TOK_TeletexString
178%token TOK_TRUE
179%token TOK_TYPE_IDENTIFIER
180%token TOK_UNIQUE
181%token TOK_UNIVERSAL
182%token TOK_UniversalString
183%token TOK_UTCTime
184%token TOK_UTF8String
185%token TOK_VideotexString
186%token TOK_VisibleString
187%token TOK_WITH
188
189%left '|' TOK_UNION
190%left '^' TOK_INTERSECTION
191%left TOK_EXCEPT
192
193/* Misc tags */
194%token TOK_TwoDots /* .. */
195%token TOK_ThreeDots /* ... */
196%token <a_tag> TOK_tag /* [0] */
197
198
199/*
200 * Types defined herein.
201 */
202%type <a_grammar> ModuleList
203%type <a_module> ModuleSpecification
204%type <a_module> ModuleSpecificationBody
205%type <a_module> ModuleSpecificationElement
206%type <a_module> optModuleSpecificationBody /* Optional */
207%type <a_module_flags> optModuleSpecificationFlags
208%type <a_module_flags> ModuleSpecificationFlags /* Set of FL */
209%type <a_module_flags> ModuleSpecificationFlag /* Single FL */
210%type <a_module> ImportsDefinition
211%type <a_module> ImportsBundleSet
212%type <a_xports> ImportsBundle
213%type <a_xports> ImportsList
214%type <a_xports> ExportsDefinition
215%type <a_xports> ExportsBody
216%type <a_expr> ImportsElement
217%type <a_expr> ExportsElement
218%type <a_expr> DataTypeMember
219%type <a_expr> ExtensionAndException
220%type <a_expr> TypeDeclaration
221%type <a_ref> ComplexTypeReference
222%type <a_ref> ComplexTypeReferenceAmpList
223%type <a_refcomp> ComplexTypeReferenceElement
224%type <a_refcomp> ClassFieldIdentifier
225%type <a_refcomp> ClassFieldName
226%type <a_expr> ClassFieldList
227%type <a_expr> ClassField
228%type <a_expr> ClassDeclaration
229%type <a_expr> ConstrainedTypeDeclaration
230%type <a_expr> DataTypeReference /* Type1 ::= Type2 */
231%type <a_expr> DefinedTypeRef
232%type <a_expr> ValueSetDefinition /* Val INTEGER ::= {1|2} */
233%type <a_expr> ValueDefinition /* val INTEGER ::= 1*/
234%type <a_value> optValueSetBody
235%type <a_value> InlineOrDefinedValue
236%type <a_value> DefinedValue
237%type <a_value> SignedNumber
238%type <a_expr> ConstructedType
239%type <a_expr> ConstructedDataTypeDefinition
240//%type <a_expr> optUniverationDefinition
241%type <a_expr> UniverationDefinition
242%type <a_expr> UniverationList
243%type <a_expr> UniverationElement
244%type <tv_str> TypeRefName
245%type <tv_str> ObjectClassReference
246%type <tv_nametag> TaggedIdentifier
247%type <tv_str> Identifier
248%type <a_parg> ParameterArgumentName
249%type <a_plist> ParameterArgumentList
250%type <a_expr> ActualParameter
251%type <a_expr> ActualParameterList
252%type <a_oid> ObjectIdentifier /* OID */
253%type <a_oid> optObjectIdentifier /* Optional OID */
254%type <a_oid> ObjectIdentifierBody
255%type <a_oid_arc> ObjectIdentifierElement
256%type <a_expr> BasicType
257%type <a_type> BasicTypeId
258%type <a_type> BasicTypeId_UniverationCompatible
259%type <a_type> BasicString
260%type <tv_opaque> Opaque
261//%type <tv_opaque> StringValue
262%type <a_tag> Tag /* [UNIVERSAL 0] IMPLICIT */
263%type <a_tag> optTag /* [UNIVERSAL 0] IMPLICIT */
264%type <a_constr> optConstraints
265%type <a_constr> Constraints
266%type <a_constr> SingleConstraint /* (SIZE(2)) */
267%type <a_constr> ConstraintElementSet /* 1..2,...,3 */
268%type <a_constr> ConstraintSubtypeElement /* 1..2 */
269%type <a_constr> ConstraintElementIntersection
270%type <a_constr> ConstraintElementException
271%type <a_constr> ConstraintElementUnion
272%type <a_constr> ConstraintElement
273%type <a_constr> SimpleTableConstraint
274%type <a_constr> TableConstraint
275%type <a_constr> WithComponents
276%type <a_constr> WithComponentsList
277%type <a_constr> WithComponentsElement
278%type <a_constr> ComponentRelationConstraint
279%type <a_constr> AtNotationList
280%type <a_ref> AtNotationElement
281%type <a_value> ConstraintValue
282%type <a_ctype> ConstraintSpec
283%type <a_ctype> ConstraintRangeSpec
284%type <a_wsynt> optWithSyntax
285%type <a_wsynt> WithSyntax
286%type <a_wsynt> WithSyntaxFormat
287%type <a_wchunk> WithSyntaxFormatToken
288%type <a_marker> optMarker Marker
289%type <a_int> optUnique
290%type <a_pres> optPresenceConstraint PresenceConstraint
291%type <tv_str> ComponentIdList
292
293
294%%
295
296
297ParsedGrammar:
298 ModuleList {
299 *(void **)param = $1;
300 }
301 ;
302
303ModuleList:
304 ModuleSpecification {
305 $$ = asn1p_new();
306 checkmem($$);
307 TQ_ADD(&($$->modules), $1, mod_next);
308 }
309 | ModuleList ModuleSpecification {
310 $$ = $1;
311 TQ_ADD(&($$->modules), $2, mod_next);
312 }
313 ;
314
315/*
316 * ASN module definition.
317 * === EXAMPLE ===
318 * MySyntax DEFINITIONS AUTOMATIC TAGS ::=
319 * BEGIN
320 * ...
321 * END
322 * === EOF ===
323 */
324
325ModuleSpecification:
326 TypeRefName optObjectIdentifier TOK_DEFINITIONS
327 optModuleSpecificationFlags
328 TOK_PPEQ TOK_BEGIN
329 optModuleSpecificationBody
330 TOK_END {
331
332 if($7) {
333 $$ = $7;
334 } else {
335 /* There's a chance that a module is just plain empty */
336 $$ = asn1p_module_new();
337 }
338 checkmem($$);
339
340 $$->Identifier = $1;
341 $$->module_oid = $2;
342 $$->module_flags = $4;
343 }
344 ;
345
346/*
347 * Object Identifier Definition
348 * { iso member-body(2) 3 }
349 */
350optObjectIdentifier:
351 { $$ = 0; }
352 | ObjectIdentifier { $$ = $1; }
353 ;
354
355ObjectIdentifier:
356 '{' ObjectIdentifierBody '}' {
357 $$ = $2;
358 }
359 | '{' '}' {
360 $$ = 0;
361 }
362 ;
363
364ObjectIdentifierBody:
365 ObjectIdentifierElement {
366 $$ = asn1p_oid_new();
367 asn1p_oid_add_arc($$, &$1);
368 if($1.name)
369 free($1.name);
370 }
371 | ObjectIdentifierBody ObjectIdentifierElement {
372 $$ = $1;
373 asn1p_oid_add_arc($$, &$2);
374 if($2.name)
375 free($2.name);
376 }
377 ;
378
379ObjectIdentifierElement:
380 Identifier { /* iso */
381 $$.name = $1;
382 $$.number = -1;
383 }
384 | Identifier '(' TOK_number ')' { /* iso(1) */
385 $$.name = $1;
386 $$.number = $3;
387 }
388 | TOK_number { /* 1 */
389 $$.name = 0;
390 $$.number = $1;
391 }
392 ;
393
394/*
395 * Optional module flags.
396 */
397optModuleSpecificationFlags:
398 { $$ = MSF_NOFLAGS; }
399 | ModuleSpecificationFlags {
400 $$ = $1;
401 }
402 ;
403
404/*
405 * Module flags.
406 */
407ModuleSpecificationFlags:
408 ModuleSpecificationFlag {
409 $$ = $1;
410 }
411 | ModuleSpecificationFlags ModuleSpecificationFlag {
412 $$ = $1 | $2;
413 }
414 ;
415
416/*
417 * Single module flag.
418 */
419ModuleSpecificationFlag:
420 TOK_EXPLICIT TOK_TAGS {
421 $$ = MSF_EXPLICIT_TAGS;
422 }
423 | TOK_IMPLICIT TOK_TAGS {
424 $$ = MSF_IMPLICIT_TAGS;
425 }
426 | TOK_AUTOMATIC TOK_TAGS {
427 $$ = MSF_AUTOMATIC_TAGS;
428 }
429 | TOK_EXTENSIBILITY TOK_IMPLIED {
430 $$ = MSF_EXTENSIBILITY_IMPLIED;
431 }
432 ;
433
434/*
435 * Optional module body.
436 */
437optModuleSpecificationBody:
438 { $$ = 0; }
439 | ModuleSpecificationBody {
440 assert($1);
441 $$ = $1;
442 }
443 ;
444
445/*
446 * ASN.1 Module body.
447 */
448ModuleSpecificationBody:
449 ModuleSpecificationElement {
450 $$ = $1;
451 }
452 | ModuleSpecificationBody ModuleSpecificationElement {
453 $$ = $1;
454
455#ifdef MY_IMPORT
456#error MY_IMPORT DEFINED ELSEWHERE!
457#endif
458#define MY_IMPORT(foo,field) do { \
vlm97ed7152004-08-13 12:31:09 +0000459 while(TQ_FIRST(&($2->foo))) { \
vlmfa67ddc2004-06-03 03:38:44 +0000460 TQ_ADD(&($$->foo), \
461 TQ_REMOVE(&($2->foo), field), \
462 field); \
vlm97ed7152004-08-13 12:31:09 +0000463 } \
464 assert(TQ_FIRST(&($2->foo)) == 0); \
465 } while(0)
vlmfa67ddc2004-06-03 03:38:44 +0000466
467 MY_IMPORT(imports, xp_next);
468 MY_IMPORT(exports, xp_next);
469 MY_IMPORT(members, next);
470#undef MY_IMPORT
471
472 }
473 ;
474
475/*
476 * One of the elements of ASN.1 module specification.
477 */
478ModuleSpecificationElement:
479 ImportsDefinition {
480 $$ = $1;
481 }
482 | ExportsDefinition {
483 $$ = asn1p_module_new();
484 checkmem($$);
485 if($1) {
486 TQ_ADD(&($$->exports), $1, xp_next);
487 } else {
488 /* "EXPORTS ALL;" ? */
489 }
490 }
491 | DataTypeReference {
492 $$ = asn1p_module_new();
493 checkmem($$);
494 assert($1->expr_type != A1TC_INVALID);
495 assert($1->meta_type != AMT_INVALID);
496 TQ_ADD(&($$->members), $1, next);
497 }
498 | ValueDefinition {
499 $$ = asn1p_module_new();
500 checkmem($$);
501 assert($1->expr_type != A1TC_INVALID);
502 assert($1->meta_type != AMT_INVALID);
503 TQ_ADD(&($$->members), $1, next);
504 }
505 /*
506 * Value set definition
507 * === EXAMPLE ===
508 * EvenNumbers INTEGER ::= { 2 | 4 | 6 | 8 }
509 * === EOF ===
510 */
511 | ValueSetDefinition {
512 $$ = asn1p_module_new();
513 checkmem($$);
514 assert($1->expr_type != A1TC_INVALID);
515 assert($1->meta_type != AMT_INVALID);
516 TQ_ADD(&($$->members), $1, next);
517 }
518
519 /*
520 * Erroneous attemps
521 */
522 | BasicString {
523 return yyerror(
524 "Attempt to redefine a standard basic type, "
525 "use -ftypesXY to switch back "
526 "to older version of ASN.1 standard");
527 }
528 ;
529
530/*
531 * === EXAMPLE ===
532 * IMPORTS Type1, value FROM Module { iso standard(0) } ;
533 * === EOF ===
534 */
535ImportsDefinition:
536 TOK_IMPORTS ImportsBundleSet ';' {
537 $$ = $2;
538 }
539 /*
540 * Some error cases.
541 */
542 | TOK_IMPORTS TOK_FROM /* ... */ {
543 return yyerror("Empty IMPORTS list");
544 }
545 ;
546
547ImportsBundleSet:
548 ImportsBundle {
549 $$ = asn1p_module_new();
550 checkmem($$);
551 TQ_ADD(&($$->imports), $1, xp_next);
552 }
553 | ImportsBundleSet ImportsBundle {
554 $$ = $1;
555 TQ_ADD(&($$->imports), $2, xp_next);
556 }
557 ;
558
559ImportsBundle:
560 ImportsList TOK_FROM TypeRefName optObjectIdentifier {
561 $$ = $1;
562 $$->from = $3;
563 $$->from_oid = $4;
564 checkmem($$);
565 }
566 ;
567
568ImportsList:
569 ImportsElement {
570 $$ = asn1p_xports_new();
571 checkmem($$);
572 TQ_ADD(&($$->members), $1, next);
573 }
574 | ImportsList ',' ImportsElement {
575 $$ = $1;
576 TQ_ADD(&($$->members), $3, next);
577 }
578 ;
579
580ImportsElement:
581 TypeRefName {
582 $$ = asn1p_expr_new(yylineno);
583 checkmem($$);
584 $$->Identifier = $1;
585 $$->expr_type = A1TC_REFERENCE;
586 }
587 | Identifier {
588 $$ = asn1p_expr_new(yylineno);
589 checkmem($$);
590 $$->Identifier = $1;
591 $$->expr_type = A1TC_REFERENCE;
592 }
593 ;
594
595ExportsDefinition:
596 TOK_EXPORTS ExportsBody ';' {
597 $$ = $2;
598 }
599 | TOK_EXPORTS TOK_ALL ';' {
600 $$ = 0;
601 }
602 | TOK_EXPORTS ';' {
603 /* Empty EXPORTS clause effectively prohibits export. */
604 $$ = asn1p_xports_new();
605 checkmem($$);
606 }
607 ;
608
609ExportsBody:
610 ExportsElement {
611 $$ = asn1p_xports_new();
612 assert($$);
613 TQ_ADD(&($$->members), $1, next);
614 }
615 | ExportsBody ',' ExportsElement {
616 $$ = $1;
617 TQ_ADD(&($$->members), $3, next);
618 }
619 ;
620
621ExportsElement:
622 TypeRefName {
623 $$ = asn1p_expr_new(yylineno);
624 checkmem($$);
625 $$->Identifier = $1;
626 $$->expr_type = A1TC_EXPORTVAR;
627 }
628 | Identifier {
629 $$ = asn1p_expr_new(yylineno);
630 checkmem($$);
631 $$->Identifier = $1;
632 $$->expr_type = A1TC_EXPORTVAR;
633 }
634 ;
635
636
637ValueSetDefinition:
638 TypeRefName DefinedTypeRef TOK_PPEQ '{' optValueSetBody '}' {
639 $$ = $2;
640 assert($$->Identifier == 0);
641 $$->Identifier = $1;
642 $$->meta_type = AMT_VALUESET;
643 // take care of optValueSetBody
644 }
645 ;
646
647DefinedTypeRef:
648 ComplexTypeReference {
649 $$ = asn1p_expr_new(yylineno);
650 checkmem($$);
651 $$->reference = $1;
652 $$->expr_type = A1TC_REFERENCE;
653 $$->meta_type = AMT_TYPEREF;
654 }
655 | BasicTypeId {
656 $$ = asn1p_expr_new(yylineno);
657 checkmem($$);
658 $$->expr_type = $1;
659 $$->meta_type = AMT_TYPE;
660 }
661 ;
662
663optValueSetBody:
664 { }
665 | ConstraintElementSet {
666 }
667 ;
668
669
670/*
671 * Data Type Reference.
672 * === EXAMPLE ===
673 * Type3 ::= CHOICE { a Type1, b Type 2 }
674 * === EOF ===
675 */
676
677DataTypeReference:
678 /*
679 * Optionally tagged type definition.
680 */
681 TypeRefName TOK_PPEQ optTag TOK_TYPE_IDENTIFIER {
682 $$ = asn1p_expr_new(yylineno);
683 checkmem($$);
684 $$->Identifier = $1;
685 $$->tag = $3;
686 $$->expr_type = A1TC_TYPEID;
687 $$->meta_type = AMT_TYPE;
688 }
689 | TypeRefName TOK_PPEQ optTag ConstrainedTypeDeclaration {
690 $$ = $4;
691 $$->Identifier = $1;
692 $$->tag = $3;
693 assert($$->expr_type);
694 assert($$->meta_type);
695 }
696 | TypeRefName TOK_PPEQ ClassDeclaration {
697 $$ = $3;
698 $$->Identifier = $1;
699 assert($$->expr_type == A1TC_CLASSDEF);
700 assert($$->meta_type == AMT_OBJECT);
701 }
702 /*
703 * Parametrized <Type> declaration:
704 * === EXAMPLE ===
705 * SIGNED { ToBeSigned } ::= SEQUENCE {
706 * toBeSigned ToBeSigned,
707 * algorithm AlgorithmIdentifier,
708 * signature BIT STRING
709 * }
710 * === EOF ===
711 */
712 | TypeRefName '{' ParameterArgumentList '}'
713 TOK_PPEQ ConstrainedTypeDeclaration {
714 $$ = $6;
715 assert($$->Identifier == 0);
716 $$->Identifier = $1;
717 $$->params = $3;
718 $$->meta_type = AMT_PARAMTYPE;
719 }
720 ;
721
722ParameterArgumentList:
723 ParameterArgumentName {
724 int ret;
725 $$ = asn1p_paramlist_new(yylineno);
726 checkmem($$);
727 ret = asn1p_paramlist_add_param($$, $1.governor, $1.argument);
728 checkmem(ret == 0);
729 if($1.governor) asn1p_ref_free($1.governor);
730 if($1.argument) free($1.argument);
731 }
732 | ParameterArgumentList ',' ParameterArgumentName {
733 int ret;
734 $$ = $1;
735 ret = asn1p_paramlist_add_param($$, $3.governor, $3.argument);
736 checkmem(ret == 0);
737 if($3.governor) asn1p_ref_free($3.governor);
738 if($3.argument) free($3.argument);
739 }
740 ;
741
742ParameterArgumentName:
743 TypeRefName {
744 $$.governor = NULL;
745 $$.argument = $1;
746 }
747 | TypeRefName ':' Identifier {
748 int ret;
749 $$.governor = asn1p_ref_new(yylineno);
750 ret = asn1p_ref_add_component($$.governor, $1, 0);
751 checkmem(ret == 0);
752 $$.argument = $3;
753 }
754 | BasicTypeId ':' Identifier {
755 int ret;
756 $$.governor = asn1p_ref_new(yylineno);
757 ret = asn1p_ref_add_component($$.governor,
758 ASN_EXPR_TYPE2STR($1), 1);
759 checkmem(ret == 0);
760 $$.argument = $3;
761 }
762 ;
763
764ActualParameterList:
765 ActualParameter {
766 $$ = asn1p_expr_new(yylineno);
767 checkmem($$);
768 TQ_ADD(&($$->members), $1, next);
769 }
770 | ActualParameterList ',' ActualParameter {
771 $$ = $1;
772 TQ_ADD(&($$->members), $3, next);
773 }
774 ;
775
776ActualParameter:
777 TypeDeclaration {
778 $$ = $1;
779 }
780 | Identifier {
781 $$ = asn1p_expr_new(yylineno);
782 checkmem($$);
783 $$->Identifier = $1;
784 $$->expr_type = A1TC_REFERENCE;
785 $$->meta_type = AMT_VALUE;
786 }
787 ;
788
789/*
790 * A collection of constructed data type members.
791 */
792ConstructedDataTypeDefinition:
793 DataTypeMember {
794 $$ = asn1p_expr_new(yylineno);
795 checkmem($$);
796 TQ_ADD(&($$->members), $1, next);
797 }
798 | ConstructedDataTypeDefinition ',' DataTypeMember {
799 $$ = $1;
800 TQ_ADD(&($$->members), $3, next);
801 }
802 ;
803
804ClassDeclaration:
805 TOK_CLASS '{' ClassFieldList '}' optWithSyntax {
806 $$ = $3;
807 checkmem($$);
808 $$->with_syntax = $5;
809 assert($$->expr_type == A1TC_CLASSDEF);
810 assert($$->meta_type == AMT_OBJECT);
811 }
812 ;
813
814optUnique:
815 { $$ = 0; }
816 | TOK_UNIQUE { $$ = 1; }
817 ;
818
819ClassFieldList:
820 ClassField {
821 $$ = asn1p_expr_new(yylineno);
822 checkmem($$);
823 $$->expr_type = A1TC_CLASSDEF;
824 $$->meta_type = AMT_OBJECT;
825 TQ_ADD(&($$->members), $1, next);
826 }
827 | ClassFieldList ',' ClassField {
828 $$ = $1;
829 TQ_ADD(&($$->members), $3, next);
830 }
831 ;
832
833ClassField:
834 ClassFieldIdentifier optMarker {
835 $$ = asn1p_expr_new(yylineno);
836 checkmem($$);
837 $$->Identifier = $1.name;
838 $$->expr_type = A1TC_CLASSFIELD;
839 $$->meta_type = AMT_OBJECTFIELD;
840 $$->marker = $2;
841 }
842 | ClassFieldIdentifier ConstrainedTypeDeclaration optUnique {
843 $$ = $2;
844 $$->Identifier = $1.name;
845 $$->unique = $3;
846 }
847 | ClassFieldIdentifier ClassFieldIdentifier optMarker optUnique {
848 int ret;
849 $$ = asn1p_expr_new(yylineno);
850 checkmem($$);
851 $$->Identifier = $1.name;
852 $$->reference = asn1p_ref_new(yylineno);
853 checkmem($$->reference);
854 ret = asn1p_ref_add_component($$->reference,
855 $2.name, $2.lex_type);
856 checkmem(ret == 0);
857 $$->expr_type = A1TC_CLASSFIELD;
858 $$->meta_type = AMT_OBJECTFIELD;
859 $$->marker = $3;
860 $$->unique = $4;
861 }
862 ;
863
864optWithSyntax:
865 { $$ = 0; }
866 | WithSyntax {
867 $$ = $1;
868 }
869 ;
870
871WithSyntax:
872 TOK_WITH TOK_SYNTAX '{'
873 { asn1p_lexer_hack_enable_with_syntax(); }
874 WithSyntaxFormat
875 '}' {
876 $$ = $5;
877 }
878 ;
879
880WithSyntaxFormat:
881 WithSyntaxFormatToken {
882 $$ = asn1p_wsyntx_new();
883 TQ_ADD(&($$->chunks), $1, next);
884 }
885 | WithSyntaxFormat WithSyntaxFormatToken {
886 $$ = $1;
887 TQ_ADD(&($$->chunks), $2, next);
888 }
889 ;
890
891WithSyntaxFormatToken:
892 TOK_opaque {
893 $$ = asn1p_wsyntx_chunk_frombuf($1.buf, $1.len, 0);
894 }
895 | ClassFieldIdentifier {
896 asn1p_ref_t *ref;
897 int ret;
898 ref = asn1p_ref_new(yylineno);
899 checkmem(ref);
900 ret = asn1p_ref_add_component(ref, $1.name, $1.lex_type);
901 checkmem(ret == 0);
902 $$ = asn1p_wsyntx_chunk_fromref(ref, 0);
903 }
904 ;
905
906/*
907 * A data type member goes like this
908 * ===
909 * memb1 [0] Type1 { a(1), b(2) } (2)
910 * ===
911 * Therefore, we propose a split.
912 * ^^^^^^^^^ ^^^^^^^^^^
913 * ^TaggedIdentifier ^TypeDeclaration
914 * ^ConstrainedTypeDeclaration
915 */
916
917/*
918 * A member of a constructed data type ("a" or "b" in above example).
919 */
920DataTypeMember:
921 TaggedIdentifier ConstrainedTypeDeclaration {
922 $$ = $2;
923 assert($$->Identifier == 0);
924 $$->Identifier = $1.name;
925 $$->tag = $1.tag;
926 }
927 | ExtensionAndException {
928 $$ = $1;
929 }
930 ;
931
932ConstrainedTypeDeclaration:
933 TypeDeclaration optConstraints optMarker {
934 $$ = $1;
935 $$->constraints = $2;
936 $$->marker = $3;
937 }
938 ;
939
940ExtensionAndException:
941 TOK_ThreeDots {
942 $$ = asn1p_expr_new(asn1p_lineno);
943 checkmem($$);
944 $$->Identifier = strdup("...");
945 checkmem($$->Identifier);
946 $$->expr_type = A1TC_EXTENSIBLE;
947 $$->meta_type = AMT_TYPE;
948 }
949 | TOK_ThreeDots '!' DefinedValue {
950 $$ = asn1p_expr_new(asn1p_lineno);
951 checkmem($$);
952 $$->Identifier = strdup("...");
953 checkmem($$->Identifier);
954 $$->value = $3;
955 $$->expr_type = A1TC_EXTENSIBLE;
956 $$->meta_type = AMT_TYPE;
957 }
958 | TOK_ThreeDots '!' SignedNumber {
959 $$ = asn1p_expr_new(asn1p_lineno);
960 checkmem($$);
961 $$->Identifier = strdup("...");
962 $$->value = $3;
963 checkmem($$->Identifier);
964 $$->expr_type = A1TC_EXTENSIBLE;
965 $$->meta_type = AMT_TYPE;
966 }
967 ;
968
969TypeDeclaration:
970 BasicType {
971 $$ = $1;
972 }
973 | BasicString {
974 $$ = asn1p_expr_new(yylineno);
975 checkmem($$);
976 $$->expr_type = $1;
977 $$->meta_type = AMT_TYPE;
978 }
979 | ConstructedType {
980 $$ = $1;
981 checkmem($$);
982 assert($$->meta_type);
983 }
984 /*
985 * A parametrized assignment.
986 */
987 | TypeRefName '{' ActualParameterList '}' {
988 int ret;
989 $$ = $3;
990 assert($$->expr_type == 0);
991 assert($$->meta_type == 0);
992 assert($$->reference == 0);
993 $$->reference = asn1p_ref_new(yylineno);
994 checkmem($$->reference);
995 ret = asn1p_ref_add_component($$->reference, $1, RLT_UNKNOWN);
996 checkmem(ret == 0);
997 free($1);
998 $$->expr_type = A1TC_PARAMETRIZED;
999 $$->meta_type = AMT_TYPE;
1000 }
1001 /*
1002 * A DefinedType reference.
1003 * "CLASS1.&id.&id2"
1004 * or
1005 * "Module.Type"
1006 * or
1007 * "Module.identifier"
1008 * or
1009 * "Type"
1010 */
1011 | ComplexTypeReference {
1012 $$ = asn1p_expr_new(yylineno);
1013 checkmem($$);
1014 $$->reference = $1;
1015 $$->expr_type = A1TC_REFERENCE;
1016 $$->meta_type = AMT_TYPEREF;
1017 }
1018 | TOK_INSTANCE TOK_OF ComplexTypeReference {
1019 $$ = asn1p_expr_new(yylineno);
1020 checkmem($$);
1021 $$->reference = $3;
1022 $$->expr_type = A1TC_INSTANCE;
1023 $$->meta_type = AMT_TYPE;
1024 }
1025 ;
1026
1027/*
1028 * A type name consisting of several components.
1029 * === EXAMPLE ===
1030 * === EOF ===
1031 */
1032ComplexTypeReference:
1033 TOK_typereference {
1034 int ret;
1035 $$ = asn1p_ref_new(yylineno);
1036 checkmem($$);
1037 ret = asn1p_ref_add_component($$, $1, RLT_UNKNOWN);
1038 checkmem(ret == 0);
1039 free($1);
1040 }
1041 | TOK_typereference '.' TypeRefName {
1042 int ret;
1043 $$ = asn1p_ref_new(yylineno);
1044 checkmem($$);
1045 ret = asn1p_ref_add_component($$, $1, RLT_UNKNOWN);
1046 checkmem(ret == 0);
1047 ret = asn1p_ref_add_component($$, $3, RLT_UNKNOWN);
1048 checkmem(ret == 0);
1049 free($1);
1050 }
1051 | TOK_typereference '.' Identifier {
1052 int ret;
1053 $$ = asn1p_ref_new(yylineno);
1054 checkmem($$);
1055 ret = asn1p_ref_add_component($$, $1, RLT_UNKNOWN);
1056 checkmem(ret == 0);
1057 ret = asn1p_ref_add_component($$, $3, RLT_lowercase);
1058 checkmem(ret == 0);
1059 free($1);
1060 }
1061 | ObjectClassReference {
1062 int ret;
1063 $$ = asn1p_ref_new(yylineno);
1064 checkmem($$);
1065 ret = asn1p_ref_add_component($$, $1, RLT_CAPITALS);
1066 free($1);
1067 checkmem(ret == 0);
1068 }
1069 | ObjectClassReference '.' ComplexTypeReferenceAmpList {
1070 int ret;
1071 $$ = $3;
1072 ret = asn1p_ref_add_component($$, $1, RLT_CAPITALS);
1073 free($1);
1074 checkmem(ret == 0);
1075 /*
1076 * Move the last element infront.
1077 */
1078 {
1079 struct asn1p_ref_component_s tmp_comp;
1080 tmp_comp = $$->components[$$->comp_count-1];
1081 memmove(&$$->components[1],
1082 &$$->components[0],
1083 sizeof($$->components[0])
1084 * ($$->comp_count - 1));
1085 $$->components[0] = tmp_comp;
1086 }
1087 }
1088 ;
1089
1090ComplexTypeReferenceAmpList:
1091 ComplexTypeReferenceElement {
1092 int ret;
1093 $$ = asn1p_ref_new(yylineno);
1094 checkmem($$);
1095 ret = asn1p_ref_add_component($$, $1.name, $1.lex_type);
1096 free($1.name);
1097 checkmem(ret == 0);
1098 }
1099 | ComplexTypeReferenceAmpList '.' ComplexTypeReferenceElement {
1100 int ret;
1101 $$ = $1;
1102 ret = asn1p_ref_add_component($$, $3.name, $3.lex_type);
1103 free($3.name);
1104 checkmem(ret == 0);
1105 }
1106 ;
1107
1108ComplexTypeReferenceElement: ClassFieldName;
1109ClassFieldIdentifier: ClassFieldName;
1110
1111ClassFieldName:
1112 /* "&Type1" */
1113 TOK_typefieldreference {
1114 $$.lex_type = RLT_AmpUppercase;
1115 $$.name = $1;
1116 }
1117 /* "&id" */
1118 | TOK_valuefieldreference {
1119 $$.lex_type = RLT_Amplowercase;
1120 $$.name = $1;
1121 }
1122 ;
1123
1124
1125/*
1126 * === EXAMPLE ===
1127 * value INTEGER ::= 1
1128 * === EOF ===
1129 */
1130ValueDefinition:
1131 Identifier DefinedTypeRef TOK_PPEQ InlineOrDefinedValue {
1132 $$ = $2;
1133 assert($$->Identifier == NULL);
1134 $$->Identifier = $1;
1135 $$->meta_type = AMT_VALUE;
1136 $$->value = $4;
1137 }
1138 ;
1139
1140InlineOrDefinedValue:
1141 '{' { asn1p_lexer_hack_push_opaque_state(); }
1142 Opaque /* '}' */ {
1143 $$ = asn1p_value_frombuf($3.buf, $3.len, 0);
1144 checkmem($$);
1145 $$->type = ATV_UNPARSED;
1146 }
1147 | TOK_bstring {
1148 $$ = _convert_bitstring2binary($1, 'B');
1149 checkmem($$);
1150 }
1151 | TOK_hstring {
1152 $$ = _convert_bitstring2binary($1, 'H');
1153 checkmem($$);
1154 }
1155 | TOK_cstring {
1156 $$ = asn1p_value_frombuf($1.buf, $1.len, 0);
1157 checkmem($$);
1158 }
1159 | SignedNumber {
1160 $$ = $1;
1161 }
1162 | DefinedValue {
1163 $$ = $1;
1164 }
1165 ;
1166
1167DefinedValue:
1168 Identifier {
1169 asn1p_ref_t *ref;
1170 int ret;
1171 ref = asn1p_ref_new(yylineno);
1172 checkmem(ref);
1173 ret = asn1p_ref_add_component(ref, $1, RLT_lowercase);
1174 checkmem(ret == 0);
1175 $$ = asn1p_value_fromref(ref, 0);
1176 checkmem($$);
1177 free($1);
1178 }
1179 | TypeRefName '.' Identifier {
1180 asn1p_ref_t *ref;
1181 int ret;
1182 ref = asn1p_ref_new(yylineno);
1183 checkmem(ref);
1184 ret = asn1p_ref_add_component(ref, $1, RLT_UNKNOWN);
1185 checkmem(ret == 0);
1186 ret = asn1p_ref_add_component(ref, $3, RLT_lowercase);
1187 checkmem(ret == 0);
1188 $$ = asn1p_value_fromref(ref, 0);
1189 checkmem($$);
1190 free($1);
1191 free($3);
1192 }
1193 ;
1194
1195Opaque:
1196 TOK_opaque {
1197 $$.len = $1.len + 2;
1198 $$.buf = malloc($$.len + 1);
1199 checkmem($$.buf);
1200 $$.buf[0] = '{';
1201 $$.buf[1] = ' ';
1202 memcpy($$.buf + 2, $1.buf, $1.len);
1203 $$.buf[$$.len] = '\0';
1204 free($1.buf);
1205 }
1206 | Opaque TOK_opaque {
1207 int newsize = $1.len + $2.len;
1208 char *p = malloc(newsize + 1);
1209 checkmem(p);
1210 memcpy(p , $1.buf, $1.len);
1211 memcpy(p + $1.len, $2.buf, $2.len);
1212 p[newsize] = '\0';
1213 free($1.buf);
1214 free($2.buf);
1215 $$.buf = p;
1216 $$.len = newsize;
1217 }
1218 ;
1219
1220BasicTypeId:
1221 TOK_BOOLEAN { $$ = ASN_BASIC_BOOLEAN; }
1222 | TOK_NULL { $$ = ASN_BASIC_NULL; }
1223 | TOK_REAL { $$ = ASN_BASIC_REAL; }
1224 | BasicTypeId_UniverationCompatible { $$ = $1; }
1225 | TOK_OCTET TOK_STRING { $$ = ASN_BASIC_OCTET_STRING; }
1226 | TOK_OBJECT TOK_IDENTIFIER { $$ = ASN_BASIC_OBJECT_IDENTIFIER; }
1227 | TOK_RELATIVE_OID { $$ = ASN_BASIC_RELATIVE_OID; }
1228 | TOK_EXTERNAL { $$ = ASN_BASIC_EXTERNAL; }
1229 | TOK_EMBEDDED TOK_PDV { $$ = ASN_BASIC_EMBEDDED_PDV; }
1230 | TOK_CHARACTER TOK_STRING { $$ = ASN_BASIC_CHARACTER_STRING; }
1231 | TOK_UTCTime { $$ = ASN_BASIC_UTCTime; }
1232 | TOK_GeneralizedTime { $$ = ASN_BASIC_GeneralizedTime; }
1233 ;
1234
1235/*
1236 * A type identifier which may be used with "{ a(1), b(2) }" clause.
1237 */
1238BasicTypeId_UniverationCompatible:
1239 TOK_INTEGER { $$ = ASN_BASIC_INTEGER; }
1240 | TOK_ENUMERATED { $$ = ASN_BASIC_ENUMERATED; }
1241 | TOK_BIT TOK_STRING { $$ = ASN_BASIC_BIT_STRING; }
1242 ;
1243
1244BasicType:
1245 BasicTypeId {
1246 $$ = asn1p_expr_new(asn1p_lineno);
1247 checkmem($$);
1248 $$->expr_type = $1;
1249 $$->meta_type = AMT_TYPE;
1250 }
1251 | BasicTypeId_UniverationCompatible UniverationDefinition {
1252 if($2) {
1253 $$ = $2;
1254 } else {
1255 $$ = asn1p_expr_new(asn1p_lineno);
1256 checkmem($$);
1257 }
1258 $$->expr_type = $1;
1259 $$->meta_type = AMT_TYPE;
1260 }
1261 ;
1262
1263BasicString:
1264 TOK_BMPString { $$ = ASN_STRING_BMPString; }
1265 | TOK_GeneralString {
1266 $$ = ASN_STRING_GeneralString;
1267 return yyerror("GeneralString is not supported");
1268 }
1269 | TOK_GraphicString {
1270 $$ = ASN_STRING_GraphicString;
1271 return yyerror("GraphicString is not supported");
1272 }
1273 | TOK_IA5String { $$ = ASN_STRING_IA5String; }
1274 | TOK_ISO646String { $$ = ASN_STRING_ISO646String; }
1275 | TOK_NumericString { $$ = ASN_STRING_NumericString; }
1276 | TOK_PrintableString { $$ = ASN_STRING_PrintableString; }
1277 | TOK_T61String {
1278 $$ = ASN_STRING_T61String;
1279 return yyerror("T61String not implemented yet");
1280 }
1281 | TOK_TeletexString { $$ = ASN_STRING_TeletexString; }
1282 | TOK_UniversalString { $$ = ASN_STRING_UniversalString; }
1283 | TOK_UTF8String { $$ = ASN_STRING_UTF8String; }
1284 | TOK_VideotexString {
1285 $$ = ASN_STRING_VideotexString;
1286 return yyerror("VideotexString is no longer supported");
1287 }
1288 | TOK_VisibleString { $$ = ASN_STRING_VisibleString; }
1289 | TOK_ObjectDescriptor { $$ = ASN_STRING_ObjectDescriptor; }
1290 ;
1291
1292ConstructedType:
1293 TOK_CHOICE '{' ConstructedDataTypeDefinition '}' {
1294 $$ = $3;
1295 assert($$->expr_type == A1TC_INVALID);
1296 $$->expr_type = ASN_CONSTR_CHOICE;
1297 $$->meta_type = AMT_TYPE;
1298 }
1299 | TOK_SEQUENCE '{' ConstructedDataTypeDefinition '}' {
1300 $$ = $3;
1301 assert($$->expr_type == A1TC_INVALID);
1302 $$->expr_type = ASN_CONSTR_SEQUENCE;
1303 $$->meta_type = AMT_TYPE;
1304 }
1305 | TOK_SET '{' ConstructedDataTypeDefinition '}' {
1306 $$ = $3;
1307 assert($$->expr_type == A1TC_INVALID);
1308 $$->expr_type = ASN_CONSTR_SET;
1309 $$->meta_type = AMT_TYPE;
1310 }
1311 | TOK_SEQUENCE optConstraints TOK_OF TypeDeclaration {
1312 $$ = asn1p_expr_new(asn1p_lineno);
1313 checkmem($$);
1314 $$->constraints = $2;
1315 $$->expr_type = ASN_CONSTR_SEQUENCE_OF;
1316 $$->meta_type = AMT_TYPE;
1317 TQ_ADD(&($$->members), $4, next);
1318 }
1319 | TOK_SET optConstraints TOK_OF TypeDeclaration {
1320 $$ = asn1p_expr_new(asn1p_lineno);
1321 checkmem($$);
1322 $$->constraints = $2;
1323 $$->expr_type = ASN_CONSTR_SET_OF;
1324 $$->meta_type = AMT_TYPE;
1325 TQ_ADD(&($$->members), $4, next);
1326 }
1327 | TOK_ANY {
1328 $$ = asn1p_expr_new(asn1p_lineno);
1329 checkmem($$);
1330 $$->expr_type = ASN_CONSTR_ANY;
1331 $$->meta_type = AMT_TYPE;
1332 }
1333 | TOK_ANY TOK_DEFINED TOK_BY Identifier {
1334 int ret;
1335 $$ = asn1p_expr_new(asn1p_lineno);
1336 checkmem($$);
1337 $$->reference = asn1p_ref_new(yylineno);
1338 ret = asn1p_ref_add_component($$->reference,
1339 $4, RLT_lowercase);
1340 checkmem(ret == 0);
1341 $$->expr_type = ASN_CONSTR_ANY;
1342 $$->meta_type = AMT_TYPE;
1343 }
1344 ;
1345
1346/*
1347 * Data type constraints.
1348 */
1349optConstraints:
1350 { $$ = 0; }
1351 | Constraints { $$ = $1; }
1352 ;
1353
1354Union: '|' | TOK_UNION;
1355Intersection: '^' | TOK_INTERSECTION;
1356Except: TOK_EXCEPT;
1357
1358Constraints:
1359 TOK_SIZE '(' ConstraintElementSet ')' {
1360 /*
1361 * This is a special case, for compatibility purposes.
1362 * It goes without parenthesis.
1363 */
1364 int ret;
1365 $$ = asn1p_constraint_new(yylineno);
1366 checkmem($$);
1367 $$->type = ACT_CT_SIZE;
1368 ret = asn1p_constraint_insert($$, $3);
1369 checkmem(ret == 0);
1370 }
1371 | SingleConstraint {
1372 CONSTRAINT_INSERT($$, ACT_CA_SET, $1, 0);
1373 }
1374 | Constraints SingleConstraint {
1375 CONSTRAINT_INSERT($$, ACT_CA_SET, $1, $2);
1376 }
1377 ;
1378
1379SingleConstraint:
1380 '(' ConstraintElementSet ')' {
1381 $$ = $2;
1382 }
1383 ;
1384
1385ConstraintElementSet:
1386 ConstraintElement {
1387 $$ = $1;
1388 }
1389 | ConstraintElement ',' TOK_ThreeDots {
1390 asn1p_constraint_t *ct;
1391 ct = asn1p_constraint_new(yylineno);
1392 checkmem(ct);
1393 ct->type = ACT_EL_EXT;
1394 CONSTRAINT_INSERT($$, ACT_CA_CSV, $1, ct);
1395 }
1396 | ConstraintElement ',' TOK_ThreeDots ',' ConstraintElement {
1397 asn1p_constraint_t *ct;
1398 ct = asn1p_constraint_new(yylineno);
1399 checkmem(ct);
1400 ct->type = ACT_EL_EXT;
1401 CONSTRAINT_INSERT($$, ACT_CA_CSV, $1, ct);
vlm6f5eb0b2004-08-13 12:35:09 +00001402 ct = $$;
1403 CONSTRAINT_INSERT($$, ACT_CA_CSV, ct, $5);
vlmfa67ddc2004-06-03 03:38:44 +00001404 }
1405 | TOK_ThreeDots {
1406 $$ = asn1p_constraint_new(yylineno);
1407 checkmem($$);
1408 $$->type = ACT_EL_EXT;
1409 }
1410 | TOK_ThreeDots ',' ConstraintElement {
1411 asn1p_constraint_t *ct;
1412 ct = asn1p_constraint_new(yylineno);
1413 checkmem(ct);
1414 ct->type = ACT_EL_EXT;
1415 CONSTRAINT_INSERT($$, ACT_CA_CSV, ct, $3);
1416 }
1417 ;
1418
1419ConstraintElement: ConstraintElementUnion { $$ = $1; } ;
1420
1421ConstraintElementUnion:
1422 ConstraintElementIntersection { $$ = $1; }
1423 | ConstraintElementUnion Union ConstraintElementIntersection {
1424 CONSTRAINT_INSERT($$, ACT_CA_UNI, $1, $3);
1425 }
1426 ;
1427
1428ConstraintElementIntersection:
1429 ConstraintElementException { $$ = $1; }
1430 | ConstraintElementIntersection Intersection
1431 ConstraintElementException {
1432 CONSTRAINT_INSERT($$, ACT_CA_INT, $1, $3);
1433 }
1434 ;
1435
1436ConstraintElementException:
1437 ConstraintSubtypeElement { $$ = $1; }
1438 | ConstraintElementException Except ConstraintSubtypeElement {
1439 CONSTRAINT_INSERT($$, ACT_CA_EXC, $1, $3);
1440 }
1441 ;
1442
1443ConstraintSubtypeElement:
1444 ConstraintValue {
1445 $$ = asn1p_constraint_new(yylineno);
1446 checkmem($$);
1447 $$->type = ACT_EL_VALUE;
1448 $$->value = $1;
1449 }
1450 | ConstraintValue ConstraintRangeSpec ConstraintValue {
1451 $$ = asn1p_constraint_new(yylineno);
1452 checkmem($$);
1453 $$->type = $2;
1454 $$->range_start = $1;
1455 $$->range_stop = $3;
1456 }
1457 | ConstraintSpec '(' ConstraintElementSet ')' {
1458 int ret;
1459 $$ = asn1p_constraint_new(yylineno);
1460 checkmem($$);
1461 $$->type = $1;
1462 ret = asn1p_constraint_insert($$, $3);
1463 checkmem(ret == 0);
1464 }
1465 | TableConstraint {
1466 $$ = $1;
1467 }
1468 | WithComponents {
1469 $$ = $1;
1470 }
1471 ;
1472
1473ConstraintRangeSpec:
1474 TOK_TwoDots { $$ = ACT_EL_RANGE; }
1475 | TOK_TwoDots '<' { $$ = ACT_EL_RLRANGE; }
1476 | '<' TOK_TwoDots { $$ = ACT_EL_LLRANGE; }
1477 | '<' TOK_TwoDots '<' { $$ = ACT_EL_ULRANGE; }
1478 ;
1479
1480ConstraintSpec:
1481 TOK_SIZE {
1482 $$ = ACT_CT_SIZE;
1483 }
1484 | TOK_FROM {
1485 $$ = ACT_CT_FROM;
1486 }
1487 ;
1488
1489ConstraintValue:
1490 SignedNumber {
1491 $$ = $1;
1492 }
1493 | Identifier {
1494 asn1p_ref_t *ref;
1495 int ret;
1496 ref = asn1p_ref_new(yylineno);
1497 checkmem(ref);
1498 ret = asn1p_ref_add_component(ref, $1, RLT_lowercase);
1499 checkmem(ret == 0);
1500 $$ = asn1p_value_fromref(ref, 0);
1501 checkmem($$);
1502 free($1);
1503 }
1504 | TOK_cstring {
1505 $$ = asn1p_value_frombuf($1.buf, $1.len, 0);
1506 checkmem($$);
1507 }
1508 | TOK_MIN {
1509 $$ = asn1p_value_fromint(123);
1510 checkmem($$);
1511 $$->type = ATV_MIN;
1512 }
1513 | TOK_MAX {
1514 $$ = asn1p_value_fromint(321);
1515 checkmem($$);
1516 $$->type = ATV_MAX;
1517 }
1518 | TOK_FALSE {
1519 $$ = asn1p_value_fromint(0);
1520 checkmem($$);
1521 $$->type = ATV_FALSE;
1522 }
1523 | TOK_TRUE {
1524 $$ = asn1p_value_fromint(1);
1525 checkmem($$);
1526 $$->type = ATV_TRUE;
1527 }
1528 ;
1529
1530WithComponents:
1531 TOK_WITH TOK_COMPONENTS '{' WithComponentsList '}' {
1532 CONSTRAINT_INSERT($$, ACT_CT_WCOMPS, $4, 0);
1533 }
1534 ;
1535
1536WithComponentsList:
1537 WithComponentsElement {
1538 $$ = $1;
1539 }
1540 | WithComponentsList ',' WithComponentsElement {
1541 CONSTRAINT_INSERT($$, ACT_CT_WCOMPS, $1, $3);
1542 }
1543 ;
1544
1545WithComponentsElement:
1546 TOK_ThreeDots {
1547 $$ = asn1p_constraint_new(yylineno);
1548 checkmem($$);
1549 $$->type = ACT_EL_EXT;
1550 }
1551 | Identifier optConstraints optPresenceConstraint {
1552 $$ = asn1p_constraint_new(yylineno);
1553 checkmem($$);
1554 $$->type = ACT_EL_VALUE;
1555 $$->value = asn1p_value_frombuf($1, strlen($1), 0);
1556 $$->presence = $3;
1557 }
1558 ;
1559
1560/*
1561 * presence constraint for WithComponents
1562 */
1563optPresenceConstraint:
1564 { $$ = ACPRES_DEFAULT; }
1565 | PresenceConstraint { $$ = $1; }
1566 ;
1567
1568PresenceConstraint:
1569 TOK_PRESENT {
1570 $$ = ACPRES_PRESENT;
1571 }
1572 | TOK_ABSENT {
1573 $$ = ACPRES_ABSENT;
1574 }
1575 | TOK_OPTIONAL {
1576 $$ = ACPRES_OPTIONAL;
1577 }
1578 ;
1579
1580TableConstraint:
1581 SimpleTableConstraint {
1582 $$ = $1;
1583 }
1584 | ComponentRelationConstraint {
1585 $$ = $1;
1586 }
1587 ;
1588
1589/*
1590 * "{ExtensionSet}"
1591 */
1592SimpleTableConstraint:
1593 '{' TypeRefName '}' {
1594 asn1p_ref_t *ref = asn1p_ref_new(yylineno);
1595 asn1p_constraint_t *ct;
1596 int ret;
1597 ret = asn1p_ref_add_component(ref, $2, 0);
1598 checkmem(ret == 0);
1599 ct = asn1p_constraint_new(yylineno);
1600 checkmem($$);
1601 ct->type = ACT_EL_VALUE;
1602 ct->value = asn1p_value_fromref(ref, 0);
1603 CONSTRAINT_INSERT($$, ACT_CA_CRC, ct, 0);
1604 }
1605 ;
1606
1607ComponentRelationConstraint:
1608 SimpleTableConstraint '{' AtNotationList '}' {
1609 CONSTRAINT_INSERT($$, ACT_CA_CRC, $1, $3);
1610 }
1611 ;
1612
1613AtNotationList:
1614 AtNotationElement {
1615 $$ = asn1p_constraint_new(yylineno);
1616 checkmem($$);
1617 $$->type = ACT_EL_VALUE;
1618 $$->value = asn1p_value_fromref($1, 0);
1619 }
1620 | AtNotationList ',' AtNotationElement {
1621 asn1p_constraint_t *ct;
1622 ct = asn1p_constraint_new(yylineno);
1623 checkmem(ct);
1624 ct->type = ACT_EL_VALUE;
1625 ct->value = asn1p_value_fromref($3, 0);
1626 CONSTRAINT_INSERT($$, ACT_CA_CSV, $1, ct);
1627 }
1628 ;
1629
1630/*
1631 * @blah
1632 */
1633AtNotationElement:
1634 '@' ComponentIdList {
1635 char *p = malloc(strlen($2) + 2);
1636 int ret;
1637 *p = '@';
1638 strcpy(p + 1, $2);
1639 $$ = asn1p_ref_new(yylineno);
1640 ret = asn1p_ref_add_component($$, p, 0);
1641 checkmem(ret == 0);
1642 free(p);
1643 free($2);
1644 }
1645 | '@' '.' ComponentIdList {
1646 char *p = malloc(strlen($3) + 3);
1647 int ret;
1648 p[0] = '@';
1649 p[1] = '.';
1650 strcpy(p + 2, $3);
1651 $$ = asn1p_ref_new(yylineno);
1652 ret = asn1p_ref_add_component($$, p, 0);
1653 checkmem(ret == 0);
1654 free(p);
1655 free($3);
1656 }
1657 ;
1658
1659/* identifier "." ... */
1660ComponentIdList:
1661 Identifier {
1662 $$ = $1;
1663 }
1664 | ComponentIdList '.' Identifier {
1665 int l1 = strlen($1);
1666 int l3 = strlen($3);
1667 $$ = malloc(l1 + 1 + l3 + 1);
1668 memcpy($$, $1, l1);
1669 $$[l1] = '.';
1670 memcpy($$ + l1 + 1, $3, l3);
1671 $$[l1 + 1 + l3] = '\0';
1672 }
1673 ;
1674
1675
1676
1677/*
1678 * MARKERS
1679 */
1680
1681optMarker:
1682 { $$ = EM_NOMARK; }
1683 | Marker { $$ = $1; }
1684 ;
1685
1686Marker:
1687 TOK_OPTIONAL {
1688 $$ = EM_OPTIONAL;
1689 }
1690 | TOK_DEFAULT DefaultValue {
1691 $$ = EM_DEFAULT;
1692 /* FIXME: store DefaultValue somewhere */
1693 }
1694 ;
1695
1696DefaultValue:
1697 ConstraintValue {
1698 }
1699 | BasicTypeId {
1700 }
1701 | '{' { asn1p_lexer_hack_push_opaque_state(); } Opaque /* '}' */ {
1702 }
1703 ;
1704
1705/*
1706 * Universal enumeration definition to use in INTEGER and ENUMERATED.
1707 * === EXAMPLE ===
1708 * Gender ::= ENUMERATED { unknown(0), male(1), female(2) }
1709 * Temperature ::= INTEGER { absolute-zero(-273), freezing(0), boiling(100) }
1710 * === EOF ===
1711 */
1712/*
1713optUniverationDefinition:
1714 { $$ = 0; }
1715 | UniverationDefinition {
1716 $$ = $1;
1717 }
1718 ;
1719*/
1720
1721UniverationDefinition:
1722 '{' '}' {
1723 $$ = asn1p_expr_new(asn1p_lineno);
1724 checkmem($$);
1725 }
1726 | '{' UniverationList '}' {
1727 $$ = $2;
1728 }
1729 ;
1730
1731UniverationList:
1732 UniverationElement {
1733 $$ = asn1p_expr_new(asn1p_lineno);
1734 checkmem($$);
1735 TQ_ADD(&($$->members), $1, next);
1736 }
1737 | UniverationList ',' UniverationElement {
1738 $$ = $1;
1739 TQ_ADD(&($$->members), $3, next);
1740 }
1741 ;
1742
1743UniverationElement:
1744 Identifier {
1745 $$ = asn1p_expr_new(asn1p_lineno);
1746 checkmem($$);
1747 $$->expr_type = A1TC_UNIVERVAL;
1748 $$->meta_type = AMT_VALUE;
1749 $$->Identifier = $1;
1750 }
1751 | Identifier '(' SignedNumber ')' {
1752 $$ = asn1p_expr_new(asn1p_lineno);
1753 checkmem($$);
1754 $$->expr_type = A1TC_UNIVERVAL;
1755 $$->meta_type = AMT_VALUE;
1756 $$->Identifier = $1;
1757 $$->value = $3;
1758 }
1759 | Identifier '(' DefinedValue ')' {
1760 $$ = asn1p_expr_new(asn1p_lineno);
1761 checkmem($$);
1762 $$->expr_type = A1TC_UNIVERVAL;
1763 $$->meta_type = AMT_VALUE;
1764 $$->Identifier = $1;
1765 $$->value = $3;
1766 }
1767 | SignedNumber {
1768 $$ = asn1p_expr_new(asn1p_lineno);
1769 checkmem($$);
1770 $$->expr_type = A1TC_UNIVERVAL;
1771 $$->meta_type = AMT_VALUE;
1772 $$->value = $1;
1773 }
1774 | TOK_ThreeDots {
1775 $$ = asn1p_expr_new(asn1p_lineno);
1776 checkmem($$);
1777 $$->Identifier = strdup("...");
1778 checkmem($$->Identifier);
1779 $$->expr_type = A1TC_EXTENSIBLE;
1780 $$->meta_type = AMT_VALUE;
1781 }
1782 ;
1783
1784SignedNumber:
1785 TOK_number {
1786 $$ = asn1p_value_fromint($1);
1787 checkmem($$);
1788 }
1789 | TOK_number_negative {
1790 $$ = asn1p_value_fromint($1);
1791 checkmem($$);
1792 }
1793 ;
1794
1795/*
1796 * SEQUENCE definition.
1797 * === EXAMPLE ===
1798 * Struct1 ::= SEQUENCE {
1799 * memb1 Struct2,
1800 * memb2 SEQUENCE OF {
1801 * memb2-1 Struct 3
1802 * }
1803 * }
1804 * === EOF ===
1805 */
1806
1807
1808
1809/*
1810 * SET definition.
1811 * === EXAMPLE ===
1812 * Person ::= SET {
1813 * name [0] PrintableString (SIZE(1..20)),
1814 * country [1] PrintableString (SIZE(1..20)) DEFAULT default-country,
1815 * }
1816 * === EOF ===
1817 */
1818
1819optTag:
1820 { memset(&$$, 0, sizeof($$)); }
1821 | Tag { $$ = $1; }
1822 ;
1823
1824Tag:
1825 TOK_tag {
1826 $$ = $1;
1827 $$.tag_mode = TM_DEFAULT;
1828 }
1829 | TOK_tag TOK_IMPLICIT {
1830 $$ = $1;
1831 $$.tag_mode = TM_IMPLICIT;
1832 }
1833 | TOK_tag TOK_EXPLICIT {
1834 $$ = $1;
1835 $$.tag_mode = TM_EXPLICIT;
1836 }
1837 ;
1838
1839TypeRefName:
1840 TOK_typereference {
1841 checkmem($1);
1842 $$ = $1;
1843 }
1844 | TOK_objectclassreference {
1845 checkmem($1);
1846 $$ = $1;
1847 }
1848 ;
1849
1850ObjectClassReference:
1851 TOK_objectclassreference {
1852 checkmem($1);
1853 $$ = $1;
1854 }
1855 ;
1856
1857Identifier:
1858 TOK_identifier {
1859 checkmem($1);
1860 $$ = $1;
1861 }
1862 ;
1863
1864TaggedIdentifier:
1865 Identifier {
1866 memset(&$$, 0, sizeof($$));
1867 $$.name = $1;
1868 }
1869 | Identifier Tag {
1870 $$.name = $1;
1871 $$.tag = $2;
1872 }
1873 ;
1874
1875
1876%%
1877
1878
1879/*
1880 * Convert Xstring ('0101'B or '5'H) to the binary vector.
1881 */
1882static asn1p_value_t *
1883_convert_bitstring2binary(char *str, int base) {
1884 asn1p_value_t *val;
1885 int slen;
1886 int memlen;
1887 int baselen;
1888 int bits;
1889 uint8_t *binary_vector;
1890 uint8_t *bv_ptr;
1891 uint8_t cur_val;
1892
1893 assert(str);
1894 assert(str[0] == '\'');
1895
1896 switch(base) {
1897 case 'B':
1898 baselen = 1;
1899 break;
1900 case 'H':
1901 baselen = 4;
1902 break;
1903 default:
1904 assert(base == 'B' || base == 'H');
1905 errno = EINVAL;
1906 return NULL;
1907 }
1908
1909 slen = strlen(str);
1910 assert(str[slen - 1] == base);
1911 assert(str[slen - 2] == '\'');
1912
1913 memlen = slen / (8 / baselen); /* Conservative estimate */
1914
1915 bv_ptr = binary_vector = malloc(memlen + 1);
1916 if(bv_ptr == NULL)
1917 /* ENOMEM */
1918 return NULL;
1919
1920 cur_val = 0;
1921 bits = 0;
1922 while(*(++str) != '\'') {
1923 switch(baselen) {
1924 case 1:
1925 switch(*str) {
1926 case '1':
1927 cur_val |= 1 << (7 - (bits % 8));
1928 case '0':
1929 break;
1930 default:
1931 assert(!"_y UNREACH1");
1932 case ' ': case '\r': case '\n':
1933 continue;
1934 }
1935 break;
1936 case 4:
1937 switch(*str) {
1938 case '0': case '1': case '2': case '3': case '4':
1939 case '5': case '6': case '7': case '8': case '9':
1940 cur_val |= (*str - '0') << (4 - (bits % 8));
1941 break;
1942 case 'A': case 'B': case 'C':
1943 case 'D': case 'E': case 'F':
1944 cur_val |= ((*str - 'A') + 10)
1945 << (4 - (bits % 8));
1946 break;
1947 default:
1948 assert(!"_y UNREACH2");
1949 case ' ': case '\r': case '\n':
1950 continue;
1951 }
1952 break;
1953 }
1954
1955 bits += baselen;
1956 if((bits % 8) == 0) {
1957 *bv_ptr++ = cur_val;
1958 cur_val = 0;
1959 }
1960 }
1961
1962 *bv_ptr = cur_val;
1963 assert((bv_ptr - binary_vector) <= memlen);
1964
1965 val = asn1p_value_frombits(binary_vector, bits, 0);
1966 if(val == NULL) {
1967 free(binary_vector);
1968 }
1969
1970 return val;
1971}
1972
1973extern char *asn1p_text;
1974
1975int
1976yyerror(const char *msg) {
1977 fprintf(stderr,
1978 "ASN.1 grammar parse error "
1979 "near line %d (token \"%s\"): %s\n",
1980 asn1p_lineno, asn1p_text, msg);
1981 return -1;
1982}
1983
1984