⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asn1p_y.y

📁 ASN.1解析解码工具,可以解析各种ASN.1格式的文件,并对相应的BER文件解码
💻 Y
📖 第 1 页 / 共 4 页
字号:
%{#include <stdlib.h>#include <stdio.h>#include <string.h>#include <errno.h>#include <assert.h>#include "asn1parser/asn1parser.h"#define yyparse asn1p_parse
#define yylex   asn1p_lex
#define yyerror asn1p_error
#define yylval  asn1p_lval
#define yychar  asn1p_char
#define yydebug asn1p_debug
#define yynerrs asn1p_nerrs
int asn1p_debug;#define YYPARSE_PARAM	param#define YYPARSE_PARAM_TYPE	void **#define YYERROR_VERBOSEint yylex(void);int yyerror(const char *msg);#ifdef	YYBYACCint yyparse(void **param);	/* byacc does not produce a prototype */#endifvoid asn1p_lexer_hack_push_opaque_state(void);void asn1p_lexer_hack_enable_with_syntax(void);void asn1p_lexer_hack_push_encoding_control(void);#define	yylineno	asn1p_linenoextern int asn1p_lineno;/* * Process directives as <ASN1C:RepresentAsPointer> */extern int asn1p_as_pointer;/* * This temporary variable is used to solve the shortcomings of 1-lookahead * parser. */static struct AssignedIdentifier *saved_aid;static asn1p_value_t *_convert_bitstring2binary(char *str, int base);static void _fixup_anonymous_identifier(asn1p_expr_t *expr);static asn1p_module_t *currentModule;#define	NEW_EXPR()	(asn1p_expr_new(yylineno, currentModule))#define	checkmem(ptr)	do {						\		if(!(ptr))						\		return yyerror("Memory failure");			\	} while(0)#define	CONSTRAINT_INSERT(root, constr_type, arg1, arg2) do {		\		if(arg1->type != constr_type) {				\			int __ret;					\			root = asn1p_constraint_new(yylineno);		\			checkmem(root);					\			root->type = constr_type;			\			__ret = asn1p_constraint_insert(root,		\				arg1);					\			checkmem(__ret == 0);				\		} else {						\			root = arg1;					\		}							\		if(arg2) {						\			int __ret					\			= asn1p_constraint_insert(root, arg2);		\			checkmem(__ret == 0);				\		}							\	} while(0)#ifdef	AL_IMPORT#error	AL_IMPORT DEFINED ELSEWHERE!#endif#define	AL_IMPORT(to,where,from,field,_type)	do {				\		if(!(from)) break;					\		while(TQ_FIRST(&((from)->where))) {			\			TQ_ADD_TYPE(_type,&((to)->where),				\				TQ_REMOVE_TYPE(_type,&((from)->where), field),	\				field);					\		}							\		assert(TQ_FIRST(&((from)->where)) == 0);		\	} while(0)%}/* * Token value definition. * a_*:   ASN-specific types. * tv_*:  Locally meaningful types. */%union {	asn1p_t			*a_grammar;	asn1p_module_flags_e	 a_module_flags;	asn1p_module_t		*a_module;	asn1p_expr_type_e	 a_type;	/* ASN.1 Type */	asn1p_expr_t		*a_expr;	/* Constructed collection */	asn1p_constraint_t	*a_constr;	/* Constraint */	enum asn1p_constraint_type_e	a_ctype;/* Constraint type */	asn1p_xports_t		*a_xports;	/* IMports/EXports */	struct AssignedIdentifier a_aid;	/* Assigned Identifier */	asn1p_oid_t		*a_oid;		/* Object Identifier */	asn1p_oid_arc_t		 a_oid_arc;	/* Single OID's arc */	struct asn1p_type_tag_s	 a_tag;		/* A tag */	asn1p_ref_t		*a_ref;		/* Reference to custom type */	asn1p_wsyntx_t		*a_wsynt;	/* WITH SYNTAX contents */	asn1p_wsyntx_chunk_t	*a_wchunk;	/* WITH SYNTAX chunk */	struct asn1p_ref_component_s a_refcomp;	/* Component of a reference */	asn1p_value_t		*a_value;	/* Number, DefinedValue, etc */	struct asn1p_param_s	 a_parg;	/* A parameter argument */	asn1p_paramlist_t	*a_plist;	/* A pargs list */	struct asn1p_expr_marker_s a_marker;	/* OPTIONAL/DEFAULT */	enum asn1p_constr_pres_e a_pres;	/* PRESENT/ABSENT/OPTIONAL */	asn1c_integer_t		 a_int;	double			 a_dbl;	char	*tv_str;	struct {		char *buf;		int len;	}	tv_opaque;	struct {		char *name;		struct asn1p_type_tag_s tag;	} tv_nametag;};/* * Token types returned by scanner. */%token			TOK_PPEQ	/* "::=", Pseudo Pascal EQuality */%token	<tv_opaque>	TOK_whitespace	/* A span of whitespace */%token	<tv_opaque>	TOK_opaque	/* opaque data (driven from .y) */%token	<tv_str>	TOK_bstring%token	<tv_opaque>	TOK_cstring%token	<tv_str>	TOK_hstring%token	<tv_str>	TOK_identifier%token	<a_int>		TOK_number%token	<a_int>		TOK_number_negative%token	<a_dbl>		TOK_realnumber%token	<a_int>		TOK_tuple%token	<a_int>		TOK_quadruple%token	<tv_str>	TOK_typereference%token	<tv_str>	TOK_capitalreference		/* "CLASS1" */%token	<tv_str>	TOK_typefieldreference		/* "&Pork" */%token	<tv_str>	TOK_valuefieldreference		/* "&id" */%token	<tv_str>	TOK_Literal			/* "BY" *//* * Token types representing ASN.1 standard keywords. */%token			TOK_ABSENT%token			TOK_ABSTRACT_SYNTAX%token			TOK_ALL%token			TOK_ANY%token			TOK_APPLICATION%token			TOK_AUTOMATIC%token			TOK_BEGIN%token			TOK_BIT%token			TOK_BMPString%token			TOK_BOOLEAN%token			TOK_BY%token			TOK_CHARACTER%token			TOK_CHOICE%token			TOK_CLASS%token			TOK_COMPONENT%token			TOK_COMPONENTS%token			TOK_CONSTRAINED%token			TOK_CONTAINING%token			TOK_DEFAULT%token			TOK_DEFINITIONS%token			TOK_DEFINED%token			TOK_EMBEDDED%token			TOK_ENCODED%token			TOK_ENCODING_CONTROL%token			TOK_END%token			TOK_ENUMERATED%token			TOK_EXPLICIT%token			TOK_EXPORTS%token			TOK_EXTENSIBILITY%token			TOK_EXTERNAL%token			TOK_FALSE%token			TOK_FROM%token			TOK_GeneralizedTime%token			TOK_GeneralString%token			TOK_GraphicString%token			TOK_IA5String%token			TOK_IDENTIFIER%token			TOK_IMPLICIT%token			TOK_IMPLIED%token			TOK_IMPORTS%token			TOK_INCLUDES%token			TOK_INSTANCE%token			TOK_INSTRUCTIONS%token			TOK_INTEGER%token			TOK_ISO646String%token			TOK_MAX%token			TOK_MIN%token			TOK_MINUS_INFINITY%token			TOK_NULL%token			TOK_NumericString%token			TOK_OBJECT%token			TOK_ObjectDescriptor%token			TOK_OCTET%token			TOK_OF%token			TOK_OPTIONAL%token			TOK_PATTERN%token			TOK_PDV%token			TOK_PLUS_INFINITY%token			TOK_PRESENT%token			TOK_PrintableString%token			TOK_PRIVATE%token			TOK_REAL%token			TOK_RELATIVE_OID%token			TOK_SEQUENCE%token			TOK_SET%token			TOK_SIZE%token			TOK_STRING%token			TOK_SYNTAX%token			TOK_T61String%token			TOK_TAGS%token			TOK_TeletexString%token			TOK_TRUE%token			TOK_TYPE_IDENTIFIER%token			TOK_UNIQUE%token			TOK_UNIVERSAL%token			TOK_UniversalString%token			TOK_UTCTime%token			TOK_UTF8String%token			TOK_VideotexString%token			TOK_VisibleString%token			TOK_WITH%left			TOK_EXCEPT%left			'^' TOK_INTERSECTION%left			'|' TOK_UNION/* Misc tags */%token			TOK_TwoDots		/* .. */%token			TOK_ThreeDots		/* ... *//* * Types defined herein. */%type	<a_grammar>		ModuleList%type	<a_module>		ModuleDefinition%type	<a_module>		ModuleBody%type	<a_module>		AssignmentList%type	<a_module>		Assignment%type	<a_module>		optModuleBody	/* Optional */%type	<a_module_flags>	optModuleDefinitionFlags%type	<a_module_flags>	ModuleDefinitionFlags	/* Set of FL */%type	<a_module_flags>	ModuleDefinitionFlag		/* Single FL */%type	<a_module>		optImports%type	<a_module>		optExports%type	<a_module>		ImportsDefinition%type	<a_module>		ImportsBundleSet%type	<a_xports>		ImportsBundle%type	<a_xports>		ImportsList%type	<a_xports>		ExportsDefinition%type	<a_xports>		ExportsBody%type	<a_expr>		ImportsElement%type	<a_expr>		ExportsElement%type	<a_expr>		ExtensionAndException%type	<a_expr>		Type%type	<a_expr>		TypeDeclaration%type	<a_expr>		TypeDeclarationSet%type	<a_ref>			ComplexTypeReference%type	<a_ref>			ComplexTypeReferenceAmpList%type	<a_refcomp>		ComplexTypeReferenceElement%type	<a_refcomp>		PrimitiveFieldReference%type	<a_expr>		FieldSpec%type	<a_ref>			FieldName%type	<a_ref>			DefinedObjectClass%type	<a_expr>		ClassField%type	<a_expr>		ObjectClass%type	<a_expr>		DataTypeReference	/* Type1 ::= Type2 */%type	<a_expr>		DefinedType%type	<a_constr>		ValueSet		/* {a|b|c}*/%type	<a_expr>		ValueSetTypeAssignment  /* Val INTEGER ::= {1|2} */%type	<a_expr>		ValueDefinition		/* val INTEGER ::= 1*/%type	<a_value>		Value%type	<a_value>		SimpleValue%type	<a_value>		DefinedValue%type	<a_value>		SignedNumber%type	<a_value>		RealValue%type	<a_expr>		optComponentTypeLists%type	<a_expr>		ComponentTypeLists%type	<a_expr>		ComponentType%type	<a_expr>		AlternativeTypeLists%type	<a_expr>		AlternativeType%type	<a_expr>		UniverationDefinition%type	<a_expr>		UniverationList%type	<a_expr>		UniverationElement%type	<tv_str>		TypeRefName%type	<tv_str>		ObjectClassReference%type	<tv_str>		Identifier%type	<tv_str>		optIdentifier%type	<a_parg>		ParameterArgumentName%type	<a_plist>		ParameterArgumentList%type	<a_expr>		ActualParameter%type	<a_expr>		ActualParameterList%type	<a_aid>			AssignedIdentifier	/* OID/DefinedValue */%type	<a_oid>			ObjectIdentifier	/* OID */%type	<a_oid>			optObjectIdentifier	/* Optional OID */%type	<a_oid>			ObjectIdentifierBody%type	<a_oid_arc>		ObjectIdentifierElement%type	<a_expr>		BasicType%type	<a_type>		BasicTypeId%type	<a_type>		BasicTypeId_UniverationCompatible%type	<a_type>		BasicString%type	<tv_opaque>		Opaque%type	<a_tag>			Tag 		/* [UNIVERSAL 0] IMPLICIT */%type	<a_tag>			TagClass TagTypeValue TagPlicit%type	<a_tag>			optTag		/* [UNIVERSAL 0] IMPLICIT */%type	<a_constr>		optConstraints%type	<a_constr>		Constraint%type	<a_constr>		SubtypeConstraint%type	<a_constr>		GeneralConstraint%type	<a_constr>		SetOfConstraints%type	<a_constr>		ElementSetSpecs		/* 1..2,...,3 */%type	<a_constr>		ElementSetSpec		/* 1..2,...,3 */%type	<a_constr>		ConstraintSubtypeElement /* 1..2 */%type	<a_constr>		SimpleTableConstraint%type	<a_constr>		UserDefinedConstraint%type	<a_constr>		TableConstraint%type	<a_constr>		ContentsConstraint%type	<a_constr>		InnerTypeConstraint%type	<a_constr>		WithComponentsList%type	<a_constr>		WithComponentsElement%type	<a_constr>		ComponentRelationConstraint%type	<a_constr>		AtNotationList%type	<a_ref>			AtNotationElement%type	<a_value>		SingleValue%type	<a_value>		ContainedSubtype%type	<a_ctype>		ConstraintSpec%type	<a_ctype>		ConstraintRangeSpec%type	<a_value>		RestrictedCharacterStringValue%type	<a_wsynt>		optWithSyntax%type	<a_wsynt>		WithSyntax%type	<a_wsynt>		WithSyntaxList%type	<a_wchunk>		WithSyntaxToken%type	<a_marker>		optMarker Marker%type	<a_int>			optUnique%type	<a_pres>		optPresenceConstraint PresenceConstraint%type	<tv_str>		ComponentIdList%type	<a_int>			NSTD_IndirectMarker%%ParsedGrammar:	ModuleList {		*(void **)param = $1;	}	;ModuleList:	ModuleDefinition {		$$ = asn1p_new();		checkmem($$);		TQ_ADD_TYPE(asn1p_module_t,&($$->modules), $1, mod_next);	}	| ModuleList ModuleDefinition {		$$ = $1;		TQ_ADD_TYPE(asn1p_module_t,&($$->modules), $2, mod_next);	}	;/* * ASN module definition. * === EXAMPLE === * MySyntax DEFINITIONS AUTOMATIC TAGS ::= * BEGIN * ... * END  * === EOF === */ModuleDefinition:	TypeRefName { currentModule = asn1p_module_new(); }	optObjectIdentifier TOK_DEFINITIONS		optModuleDefinitionFlags		TOK_PPEQ TOK_BEGIN		optModuleBody		TOK_END {		$$ = currentModule;		if($8) {			asn1p_module_t tmp = *($$);			*($$) = *($8);			*($8) = tmp;			asn1p_module_free($8);		} else {			/* There's a chance that a module is just plain empty */		}		$$->ModuleName = $1;		$$->module_oid = $3;		$$->module_flags = $5;	}	;/* * Object Identifier Definition * { iso member-body(2) 3 } */optObjectIdentifier:	{ $$ = 0; }	| ObjectIdentifier { $$ = $1; }	;	ObjectIdentifier:	'{' ObjectIdentifierBody '}' {		$$ = $2;	}	| '{' '}' {		$$ = 0;	}	;ObjectIdentifierBody:	ObjectIdentifierElement {		$$ = asn1p_oid_new();		asn1p_oid_add_arc($$, &$1);		if($1.name)			free($1.name);	}	| ObjectIdentifierBody ObjectIdentifierElement {		$$ = $1;		asn1p_oid_add_arc($$, &$2);		if($2.name)			free($2.name);	}	;ObjectIdentifierElement:	Identifier {					/* iso */		$$.name = $1;		$$.number = -1;	}	| Identifier '(' TOK_number ')' {		/* iso(1) */		$$.name = $1;		$$.number = $3;	}	| TOK_number {					/* 1 */		$$.name = 0;		$$.number = $1;	}	;	/* * Optional module flags. */optModuleDefinitionFlags:	{ $$ = MSF_NOFLAGS; }	| ModuleDefinitionFlags {		$$ = $1;	}	;/* * Module flags. */ModuleDefinitionFlags:	ModuleDefinitionFlag {		$$ = $1;	}	| ModuleDefinitionFlags ModuleDefinitionFlag {		$$ = $1 | $2;	}	;/* * Single module flag. */ModuleDefinitionFlag:	TOK_EXPLICIT TOK_TAGS {		$$ = MSF_EXPLICIT_TAGS;	}	| TOK_IMPLICIT TOK_TAGS {		$$ = MSF_IMPLICIT_TAGS;	}	| TOK_AUTOMATIC TOK_TAGS {		$$ = MSF_AUTOMATIC_TAGS;	}	| TOK_EXTENSIBILITY TOK_IMPLIED {		$$ = MSF_EXTENSIBILITY_IMPLIED;	}	/* EncodingReferenceDefault */	| TOK_capitalreference TOK_INSTRUCTIONS {		/* X.680Amd1 specifies TAG and XER */		if(strcmp($1, "TAG") == 0) {		 	$$ = MSF_TAG_INSTRUCTIONS;		} else if(strcmp($1, "XER") == 0) {		 	$$ = MSF_XER_INSTRUCTIONS;		} else {			fprintf(stderr,				"WARNING: %s INSTRUCTIONS at line %d: "				"Unrecognized encoding reference\n",				$1, yylineno);		 	$$ = MSF_unk_INSTRUCTIONS;		}		free($1);	}	;/* * Optional module body. */optModuleBody:	{ $$ = 0; }	| ModuleBody {		$$ = $1;	}	;/* * ASN.1 Module body. */ModuleBody:	optExports optImports AssignmentList {		$$ = asn1p_module_new();		AL_IMPORT($$, exports, $1, xp_next,asn1p_xports_t);		AL_IMPORT($$, imports, $2, xp_next,asn1p_xports_t);		AL_IMPORT($$, members, $3, next,asn1p_expr_t);	}	;AssignmentList:	Assignment {		$$ = $1;	}	| AssignmentList Assignment {		if($1) {			$$ = $1;		} else {			$$ = $2;			break;		}		AL_IMPORT($$, members, $2, next,asn1p_expr_t);	}	;/* * One of the elements of ASN.1 module specification. */Assignment:	DataTypeReference {		$$ = asn1p_module_new();		checkmem($$);		assert($1->expr_type != A1TC_INVALID);		assert($1->meta_type != AMT_INVALID);		TQ_ADD_TYPE(asn1p_expr_t,&($$->members), $1, next);	}	| ValueDefinition {		$$ = asn1p_module_new();		checkmem($$);		assert($1->expr_type != A1TC_INVALID);		assert($1->meta_type != AMT_INVALID);		TQ_ADD_TYPE(asn1p_expr_t,&($$->members), $1, next);	}	/*	 * Value set definition	 * === EXAMPLE ===	 * EvenNumbers INTEGER ::= { 2 | 4 | 6 | 8 }	 * === EOF ===	 */	| ValueSetTypeAssignment {		$$ = asn1p_module_new();		checkmem($$);		assert($1->expr_type != A1TC_INVALID);		assert($1->meta_type != AMT_INVALID);		TQ_ADD_TYPE(asn1p_expr_t,&($$->members), $1, next);	}	| TOK_ENCODING_CONTROL TOK_capitalreference 		{ asn1p_lexer_hack_push_encoding_control(); }			{		fprintf(stderr,			"WARNING: ENCODING-CONTROL %s "			"specification at line %d ignored\n",			$2, yylineno);		free($2);		$$ = 0;	}	/*	 * Erroneous attemps	 */	| BasicString {		return yyerror(			"Attempt to redefine a standard basic string type, "			"please comment out or remove this type redefinition.");	}	;/* * === EXAMPLE === * IMPORTS Type1, value FROM Module { iso standard(0) } ; * === EOF === */optImports:	{ $$ = 0; }	| ImportsDefinition;ImportsDefinition:	TOK_IMPORTS ImportsBundleSet ';' {		if(!saved_aid && 0)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -