📄 parse.y
字号:
{ $$ = new_type(TSet); $$->members = NULL; $$ = new_tag(ASN1_C_UNIV, UT_Set, TE_EXPLICIT, $$); } ;SetOfType : kw_SET kw_OF Type { $$ = new_type(TSetOf); $$->subtype = $3; $$ = new_tag(ASN1_C_UNIV, UT_Set, TE_EXPLICIT, $$); } ;ChoiceType : kw_CHOICE '{' /* AlternativeTypeLists */ ComponentTypeList '}' { $$ = new_type(TChoice); $$->members = $3; } ;ReferencedType : DefinedType | UsefulType ;DefinedType : IDENTIFIER { Symbol *s = addsym($1); $$ = new_type(TType); if(s->stype != Stype && s->stype != SUndefined) error_message ("%s is not a type\n", $1); else $$->symbol = s; } ;UsefulType : kw_GeneralizedTime { $$ = new_tag(ASN1_C_UNIV, UT_GeneralizedTime, TE_EXPLICIT, new_type(TGeneralizedTime)); } | kw_UTCTime { $$ = new_tag(ASN1_C_UNIV, UT_UTCTime, TE_EXPLICIT, new_type(TUTCTime)); } ;ConstrainedType : Type Constraint { /* if (Constraint.type == contentConstrant) { assert(Constraint.u.constraint.type == octetstring|bitstring-w/o-NamedBitList); // remember to check type reference too if (Constraint.u.constraint.type) { assert((Constraint.u.constraint.type.length % 8) == 0); } } if (Constraint.u.constraint.encoding) { type == der-oid|ber-oid } */ } ;Constraint : '(' ConstraintSpec ')' { $$ = $2; } ;ConstraintSpec : GeneralConstraint ;GeneralConstraint: ContentsConstraint | UserDefinedConstraint ;ContentsConstraint: kw_CONTAINING Type { $$ = new_constraint_spec(CT_CONTENTS); $$->u.content.type = $2; $$->u.content.encoding = NULL; } | kw_ENCODED kw_BY Value { if ($3->type != objectidentifiervalue) error_message("Non-OID used in ENCODED BY constraint"); $$ = new_constraint_spec(CT_CONTENTS); $$->u.content.type = NULL; $$->u.content.encoding = $3; } | kw_CONTAINING Type kw_ENCODED kw_BY Value { if ($5->type != objectidentifiervalue) error_message("Non-OID used in ENCODED BY constraint"); $$ = new_constraint_spec(CT_CONTENTS); $$->u.content.type = $2; $$->u.content.encoding = $5; } ;UserDefinedConstraint: kw_CONSTRAINED kw_BY '{' '}' { $$ = new_constraint_spec(CT_USER); } ;TaggedType : Tag tagenv Type { $$ = new_type(TTag); $$->tag = $1; $$->tag.tagenv = $2; if($3->type == TTag && $2 == TE_IMPLICIT) { $$->subtype = $3->subtype; free($3); } else $$->subtype = $3; } ;Tag : '[' Class NUMBER ']' { $$.tagclass = $2; $$.tagvalue = $3; $$.tagenv = TE_EXPLICIT; } ;Class : /* */ { $$ = ASN1_C_CONTEXT; } | kw_UNIVERSAL { $$ = ASN1_C_UNIV; } | kw_APPLICATION { $$ = ASN1_C_APPL; } | kw_PRIVATE { $$ = ASN1_C_PRIVATE; } ;tagenv : /* */ { $$ = TE_EXPLICIT; } | kw_EXPLICIT { $$ = TE_EXPLICIT; } | kw_IMPLICIT { $$ = TE_IMPLICIT; } ;ValueAssignment : IDENTIFIER Type EEQUAL Value { Symbol *s; s = addsym ($1); s->stype = SValue; s->value = $4; generate_constant (s); } ;CharacterStringType: RestrictedCharactedStringType ;RestrictedCharactedStringType: kw_GeneralString { $$ = new_tag(ASN1_C_UNIV, UT_GeneralString, TE_EXPLICIT, new_type(TGeneralString)); } | kw_UTF8String { $$ = new_tag(ASN1_C_UNIV, UT_UTF8String, TE_EXPLICIT, new_type(TUTF8String)); } | kw_PrintableString { $$ = new_tag(ASN1_C_UNIV, UT_PrintableString, TE_EXPLICIT, new_type(TPrintableString)); } | kw_VisibleString { $$ = new_tag(ASN1_C_UNIV, UT_VisibleString, TE_EXPLICIT, new_type(TVisibleString)); } | kw_IA5String { $$ = new_tag(ASN1_C_UNIV, UT_IA5String, TE_EXPLICIT, new_type(TIA5String)); } | kw_BMPString { $$ = new_tag(ASN1_C_UNIV, UT_BMPString, TE_EXPLICIT, new_type(TBMPString)); } | kw_UniversalString { $$ = new_tag(ASN1_C_UNIV, UT_UniversalString, TE_EXPLICIT, new_type(TUniversalString)); } ;ComponentTypeList: ComponentType { $$ = emalloc(sizeof(*$$)); ASN1_TAILQ_INIT($$); ASN1_TAILQ_INSERT_HEAD($$, $1, members); } | ComponentTypeList ',' ComponentType { ASN1_TAILQ_INSERT_TAIL($1, $3, members); $$ = $1; } | ComponentTypeList ',' ELLIPSIS { struct member *m = ecalloc(1, sizeof(*m)); m->name = estrdup("..."); m->gen_name = estrdup("asn1_ellipsis"); m->ellipsis = 1; ASN1_TAILQ_INSERT_TAIL($1, m, members); $$ = $1; } ;NamedType : IDENTIFIER Type { $$ = emalloc(sizeof(*$$)); $$->name = $1; $$->gen_name = estrdup($1); output_name ($$->gen_name); $$->type = $2; $$->ellipsis = 0; } ;ComponentType : NamedType { $$ = $1; $$->optional = 0; $$->defval = NULL; } | NamedType kw_OPTIONAL { $$ = $1; $$->optional = 1; $$->defval = NULL; } | NamedType kw_DEFAULT Value { $$ = $1; $$->optional = 0; $$->defval = $3; } ;NamedBitList : NamedBit { $$ = emalloc(sizeof(*$$)); ASN1_TAILQ_INIT($$); ASN1_TAILQ_INSERT_HEAD($$, $1, members); } | NamedBitList ',' NamedBit { ASN1_TAILQ_INSERT_TAIL($1, $3, members); $$ = $1; } ;NamedBit : IDENTIFIER '(' NUMBER ')' { $$ = emalloc(sizeof(*$$)); $$->name = $1; $$->gen_name = estrdup($1); output_name ($$->gen_name); $$->val = $3; $$->optional = 0; $$->ellipsis = 0; $$->type = NULL; } ;objid_opt : objid | /* empty */ { $$ = NULL; } ;objid : '{' objid_list '}' { $$ = $2; } ;objid_list : /* empty */ { $$ = NULL; } | objid_element objid_list { if ($2) { $$ = $2; add_oid_to_tail($2, $1); } else { $$ = $1; } } ;objid_element : IDENTIFIER '(' NUMBER ')' { $$ = new_objid($1, $3); } | IDENTIFIER { Symbol *s = addsym($1); if(s->stype != SValue || s->value->type != objectidentifiervalue) { error_message("%s is not an object identifier\n", s->name); exit(1); } $$ = s->value->u.objectidentifiervalue; } | NUMBER { $$ = new_objid(NULL, $1); } ;Value : BuiltinValue | ReferencedValue ;BuiltinValue : BooleanValue | CharacterStringValue | IntegerValue | ObjectIdentifierValue | NullValue ;ReferencedValue : DefinedValue ;DefinedValue : Valuereference ;Valuereference : IDENTIFIER { Symbol *s = addsym($1); if(s->stype != SValue) error_message ("%s is not a value\n", s->name); else $$ = s->value; } ;CharacterStringValue: STRING { $$ = emalloc(sizeof(*$$)); $$->type = stringvalue; $$->u.stringvalue = $1; } ;BooleanValue : kw_TRUE { $$ = emalloc(sizeof(*$$)); $$->type = booleanvalue; $$->u.booleanvalue = 0; } | kw_FALSE { $$ = emalloc(sizeof(*$$)); $$->type = booleanvalue; $$->u.booleanvalue = 0; } ;IntegerValue : SignedNumber { $$ = emalloc(sizeof(*$$)); $$->type = integervalue; $$->u.integervalue = $1; } ;SignedNumber : NUMBER ;NullValue : kw_NULL { } ;ObjectIdentifierValue: objid { $$ = emalloc(sizeof(*$$)); $$->type = objectidentifiervalue; $$->u.objectidentifiervalue = $1; } ;%%voidyyerror (const char *s){ error_message ("%s\n", s);}static Type *new_tag(int tagclass, int tagvalue, int tagenv, Type *oldtype){ Type *t; if(oldtype->type == TTag && oldtype->tag.tagenv == TE_IMPLICIT) { t = oldtype; oldtype = oldtype->subtype; /* XXX */ } else t = new_type (TTag); t->tag.tagclass = tagclass; t->tag.tagvalue = tagvalue; t->tag.tagenv = tagenv; t->subtype = oldtype; return t;}static struct objid *new_objid(const char *label, int value){ struct objid *s; s = emalloc(sizeof(*s)); s->label = label; s->value = value; s->next = NULL; return s;}static voidadd_oid_to_tail(struct objid *head, struct objid *tail){ struct objid *o; o = head; while (o->next) o = o->next; o->next = tail;}static Type *new_type (Typetype tt){ Type *t = ecalloc(1, sizeof(*t)); t->type = tt; return t;}static struct constraint_spec *new_constraint_spec(enum ctype ct){ struct constraint_spec *c = ecalloc(1, sizeof(*c)); c->ctype = ct; return c;}static void fix_labels2(Type *t, const char *prefix);static void fix_labels1(struct memhead *members, const char *prefix){ Member *m; if(members == NULL) return; ASN1_TAILQ_FOREACH(m, members, members) { asprintf(&m->label, "%s_%s", prefix, m->gen_name); if (m->label == NULL) errx(1, "malloc"); if(m->type != NULL) fix_labels2(m->type, m->label); }}static void fix_labels2(Type *t, const char *prefix){ for(; t; t = t->subtype) fix_labels1(t->members, prefix);}static voidfix_labels(Symbol *s){ char *p; asprintf(&p, "choice_%s", s->gen_name); if (p == NULL) errx(1, "malloc"); fix_labels2(s->type, p); free(p);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -