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

📄 asn1p_y.y

📁 ASN.1解析解码工具,可以解析各种ASN.1格式的文件,并对相应的BER文件解码
💻 Y
📖 第 1 页 / 共 4 页
字号:
	;SingleValue:	TOK_FALSE {		$$ = asn1p_value_fromint(0);		checkmem($$);		$$->type = ATV_FALSE;	}	| TOK_TRUE {		$$ = asn1p_value_fromint(1);		checkmem($$);		$$->type = ATV_TRUE;	}	| RealValue	| RestrictedCharacterStringValue	| Identifier {		asn1p_ref_t *ref;		int ret;		ref = asn1p_ref_new(yylineno);		checkmem(ref);		ret = asn1p_ref_add_component(ref, $1, RLT_lowercase);		checkmem(ret == 0);		$$ = asn1p_value_fromref(ref, 0);		checkmem($$);		free($1);	}	;ContainedSubtype:	TypeRefName {		asn1p_ref_t *ref;		int ret;		ref = asn1p_ref_new(yylineno);		checkmem(ref);		ret = asn1p_ref_add_component(ref, $1, RLT_UNKNOWN);		checkmem(ret == 0);		$$ = asn1p_value_fromref(ref, 0);		checkmem($$);		free($1);	}	;InnerTypeConstraint:	TOK_WITH TOK_COMPONENT SetOfConstraints {		CONSTRAINT_INSERT($$, ACT_CT_WCOMP, $3, 0);	}	| TOK_WITH TOK_COMPONENTS '{' WithComponentsList '}' {		CONSTRAINT_INSERT($$, ACT_CT_WCOMPS, $4, 0);	}	;WithComponentsList:	WithComponentsElement {		$$ = $1;	}	| WithComponentsList ',' WithComponentsElement {		CONSTRAINT_INSERT($$, ACT_CT_WCOMPS, $1, $3);	}	;WithComponentsElement:	TOK_ThreeDots {		$$ = asn1p_constraint_new(yylineno);		checkmem($$);		$$->type = ACT_EL_EXT;		$$->value = asn1p_value_frombuf("...", 3, 1);	}	| Identifier optConstraints optPresenceConstraint {		$$ = asn1p_constraint_new(yylineno);		checkmem($$);		$$->type = ACT_EL_VALUE;		$$->value = asn1p_value_frombuf($1, strlen($1), 0);		$$->presence = $3;		if($2) asn1p_constraint_insert($$, $2);	}	;/* * presence constraint for WithComponents */optPresenceConstraint:	{ $$ = ACPRES_DEFAULT; }	| PresenceConstraint { $$ = $1; }	;PresenceConstraint:	TOK_PRESENT {		$$ = ACPRES_PRESENT;	}	| TOK_ABSENT {		$$ = ACPRES_ABSENT;	}	| TOK_OPTIONAL {		$$ = ACPRES_OPTIONAL;	}	;/* X.682 */GeneralConstraint:	UserDefinedConstraint	| TableConstraint	| ContentsConstraint	;UserDefinedConstraint:	TOK_CONSTRAINED TOK_BY '{'		{ asn1p_lexer_hack_push_opaque_state(); } Opaque /* '}' */ {		$$ = asn1p_constraint_new(yylineno);		checkmem($$);		$$->type = ACT_CT_CTDBY;		$$->value = asn1p_value_frombuf($5.buf, $5.len, 0);		checkmem($$->value);		$$->value->type = ATV_UNPARSED;	}	;ContentsConstraint:	TOK_CONTAINING Type {		$$ = asn1p_constraint_new(yylineno);		$$->type = ACT_CT_CTNG;		$$->value = asn1p_value_fromtype($2);	}	;ConstraintRangeSpec:	TOK_TwoDots		{ $$ = ACT_EL_RANGE; }	| TOK_TwoDots '<'	{ $$ = ACT_EL_RLRANGE; }	| '<' TOK_TwoDots	{ $$ = ACT_EL_LLRANGE; }	| '<' TOK_TwoDots '<'	{ $$ = ACT_EL_ULRANGE; }	;TableConstraint:	SimpleTableConstraint {		$$ = $1;	}	| ComponentRelationConstraint {		$$ = $1;	}	;/* * "{ExtensionSet}" */SimpleTableConstraint:	'{' TypeRefName '}' {		asn1p_ref_t *ref = asn1p_ref_new(yylineno);		asn1p_constraint_t *ct;		int ret;		ret = asn1p_ref_add_component(ref, $2, 0);		checkmem(ret == 0);		ct = asn1p_constraint_new(yylineno);		checkmem($$);		ct->type = ACT_EL_VALUE;		ct->value = asn1p_value_fromref(ref, 0);		CONSTRAINT_INSERT($$, ACT_CA_CRC, ct, 0);	}	;ComponentRelationConstraint:	SimpleTableConstraint '{' AtNotationList '}' {		CONSTRAINT_INSERT($$, ACT_CA_CRC, $1, $3);	}	;AtNotationList:	AtNotationElement {		$$ = asn1p_constraint_new(yylineno);		checkmem($$);		$$->type = ACT_EL_VALUE;		$$->value = asn1p_value_fromref($1, 0);	}	| AtNotationList ',' AtNotationElement {		asn1p_constraint_t *ct;		ct = asn1p_constraint_new(yylineno);		checkmem(ct);		ct->type = ACT_EL_VALUE;		ct->value = asn1p_value_fromref($3, 0);		CONSTRAINT_INSERT($$, ACT_CA_CSV, $1, ct);	}	;/* * @blah */AtNotationElement:	'@' ComponentIdList {		char *p = malloc(strlen($2) + 2);		int ret;		*p = '@';		strcpy(p + 1, $2);		$$ = asn1p_ref_new(yylineno);		ret = asn1p_ref_add_component($$, p, 0);		checkmem(ret == 0);		free(p);		free($2);	}	| '@' '.' ComponentIdList {		char *p = malloc(strlen($3) + 3);		int ret;		p[0] = '@';		p[1] = '.';		strcpy(p + 2, $3);		$$ = asn1p_ref_new(yylineno);		ret = asn1p_ref_add_component($$, p, 0);		checkmem(ret == 0);		free(p);		free($3);	}	;/* identifier "." ... */ComponentIdList:	Identifier {		$$ = $1;	}	| ComponentIdList '.' Identifier {		int l1 = strlen($1);		int l3 = strlen($3);		$$ = malloc(l1 + 1 + l3 + 1);		memcpy($$, $1, l1);		$$[l1] = '.';		memcpy($$ + l1 + 1, $3, l3);		$$[l1 + 1 + l3] = '\0';	}	;/* * MARKERS */optMarker:	{		$$.flags = EM_NOMARK;		$$.default_value = 0;	}	| Marker { $$ = $1; }	;Marker:	TOK_OPTIONAL {		$$.flags = EM_OPTIONAL | EM_INDIRECT;		$$.default_value = 0;	}	| TOK_DEFAULT Value {		$$.flags = EM_DEFAULT;		$$.default_value = $2;	}	;/* * Universal enumeration definition to use in INTEGER and ENUMERATED. * === EXAMPLE === * Gender ::= ENUMERATED { unknown(0), male(1), female(2) } * Temperature ::= INTEGER { absolute-zero(-273), freezing(0), boiling(100) } * === EOF === *//*optUniverationDefinition:	{ $$ = 0; }	| UniverationDefinition {		$$ = $1;	}	;*/UniverationDefinition:	'{' '}' {		$$ = NEW_EXPR();		checkmem($$);	}	| '{' UniverationList '}' {		$$ = $2;	}	;UniverationList:	UniverationElement {		$$ = NEW_EXPR();		checkmem($$);		asn1p_expr_add($$, $1);	}	| UniverationList ',' UniverationElement {		$$ = $1;		asn1p_expr_add($$, $3);	}	;UniverationElement:	Identifier {		$$ = NEW_EXPR();		checkmem($$);		$$->expr_type = A1TC_UNIVERVAL;		$$->meta_type = AMT_VALUE;		$$->Identifier = $1;	}	| Identifier '(' SignedNumber ')' {		$$ = NEW_EXPR();		checkmem($$);		$$->expr_type = A1TC_UNIVERVAL;		$$->meta_type = AMT_VALUE;		$$->Identifier = $1;		$$->value = $3;	}	| Identifier '(' DefinedValue ')' {		$$ = NEW_EXPR();		checkmem($$);		$$->expr_type = A1TC_UNIVERVAL;		$$->meta_type = AMT_VALUE;		$$->Identifier = $1;		$$->value = $3;	}	| SignedNumber {		$$ = NEW_EXPR();		checkmem($$);		$$->expr_type = A1TC_UNIVERVAL;		$$->meta_type = AMT_VALUE;		$$->value = $1;	}	| TOK_ThreeDots {		$$ = NEW_EXPR();		checkmem($$);		$$->Identifier = strdup("...");		checkmem($$->Identifier);		$$->expr_type = A1TC_EXTENSIBLE;		$$->meta_type = AMT_VALUE;	}	;SignedNumber:	TOK_number {		$$ = asn1p_value_fromint($1);		checkmem($$);	}	| TOK_number_negative {		$$ = asn1p_value_fromint($1);		checkmem($$);	}	;RealValue:	SignedNumber	| TOK_realnumber {		$$ = asn1p_value_fromdouble($1);		checkmem($$);	}	;/* * SEQUENCE definition. * === EXAMPLE === * Struct1 ::= SEQUENCE { * 	memb1 Struct2, * 	memb2 SEQUENCE OF { * 		memb2-1 Struct 3 * 	} * } * === EOF === *//* * SET definition. * === EXAMPLE === * Person ::= SET { * 	name [0] PrintableString (SIZE(1..20)), * 	country [1] PrintableString (SIZE(1..20)) DEFAULT default-country, * } * === EOF === */optTag:	{ memset(&$$, 0, sizeof($$)); }	| Tag { $$ = $1; }	;Tag:	TagTypeValue TagPlicit {		$$ = $1;		$$.tag_mode = $2.tag_mode;	}	;TagTypeValue:	'[' TagClass TOK_number ']' {		$$ = $2;		$$.tag_value = $3;	};TagClass:	{ $$.tag_class = TC_CONTEXT_SPECIFIC; }	| TOK_UNIVERSAL { $$.tag_class = TC_UNIVERSAL; }	| TOK_APPLICATION { $$.tag_class = TC_APPLICATION; }	| TOK_PRIVATE { $$.tag_class = TC_PRIVATE; }	;TagPlicit:	{ $$.tag_mode = TM_DEFAULT; }	| TOK_IMPLICIT { $$.tag_mode = TM_IMPLICIT; }	| TOK_EXPLICIT { $$.tag_mode = TM_EXPLICIT; }	;TypeRefName:	TOK_typereference {		checkmem($1);		$$ = $1;	}	| TOK_capitalreference {		checkmem($1);		$$ = $1;	}	;ObjectClassReference:	TOK_capitalreference {		checkmem($1);		$$ = $1;	}	;optIdentifier:	{ $$ = 0; }	| Identifier {		$$ = $1;	}	;Identifier:	TOK_identifier {		checkmem($1);		$$ = $1;	}	;%%/* * Convert Xstring ('0101'B or '5'H) to the binary vector. */static asn1p_value_t *_convert_bitstring2binary(char *str, int base) {	asn1p_value_t *val;	int slen;	int memlen;	int baselen;	int bits;	uint8_t *binary_vector;	uint8_t *bv_ptr;	uint8_t cur_val;	assert(str);	assert(str[0] == '\'');	switch(base) {	case 'B':		baselen = 1;		break;	case 'H':		baselen = 4;		break;	default:		assert(base == 'B' || base == 'H');		errno = EINVAL;		return NULL;	}	slen = strlen(str);	assert(str[slen - 1] == base);	assert(str[slen - 2] == '\'');	memlen = slen / (8 / baselen);	/* Conservative estimate */	bv_ptr = binary_vector = malloc(memlen + 1);	if(bv_ptr == NULL)		/* ENOMEM */		return NULL;	cur_val = 0;	bits = 0;	while(*(++str) != '\'') {		switch(baselen) {		case 1:			switch(*str) {			case '1':				cur_val |= 1 << (7 - (bits % 8));			case '0':				break;			default:				assert(!"_y UNREACH1");			case ' ': case '\r': case '\n':				continue;			}			break;		case 4:			switch(*str) {			case '0': case '1': case '2': case '3': case '4':			case '5': case '6': case '7': case '8': case '9':				cur_val |= (*str - '0') << (4 - (bits % 8));				break;			case 'A': case 'B': case 'C':			case 'D': case 'E': case 'F':				cur_val |= ((*str - 'A') + 10)					<< (4 - (bits % 8));				break;			default:				assert(!"_y UNREACH2");			case ' ': case '\r': case '\n':				continue;			}			break;		}		bits += baselen;		if((bits % 8) == 0) {			*bv_ptr++ = cur_val;			cur_val = 0;		}	}	*bv_ptr = cur_val;	assert((bv_ptr - binary_vector) <= memlen);	val = asn1p_value_frombits(binary_vector, bits, 0);	if(val == NULL) {		free(binary_vector);	}	return val;}/* * For unnamed types (used in old X.208 compliant modules) * generate some sort of interim names, to not to force human being to fix * the specification's compliance to modern ASN.1 standards. */static void_fixup_anonymous_identifier(asn1p_expr_t *expr) {	char *p;	assert(expr->Identifier == 0);	/*	 * Try to figure out the type name	 * without going too much into details	 */	expr->Identifier = ASN_EXPR_TYPE2STR(expr->expr_type);	if(expr->reference && expr->reference->comp_count > 0)		expr->Identifier = expr->reference->components[0].name;	fprintf(stderr,		"WARNING: Line %d: expected lower-case member identifier, "		"found an unnamed %s.\n"		"WARNING: Obsolete X.208 syntax detected, "		"please give the member a name.\n",		yylineno, expr->Identifier ? expr->Identifier : "type");	if(!expr->Identifier)		expr->Identifier = "unnamed";	expr->Identifier = strdup(expr->Identifier);	assert(expr->Identifier);	/* Make a lowercase identifier from the type name */	for(p = expr->Identifier; *p; p++) {		switch(*p) {/*				case 'A'...'Z':*/		case 'A':		case 'B':		case 'C':		case 'D':		case 'E':		case 'F':		case 'G':		case 'H':		case 'I':		case 'J':		case 'K':		case 'L':		case 'M':		case 'N':		case 'O':		case 'P':		case 'Q':		case 'R':		case 'S':		case 'T':		case 'U':		case 'V':		case 'W':		case 'X':		case 'Y':		case 'Z': 			*p += 32; 			break;		case ' ': *p = '_'; break;		case '-': *p = '_'; break;		}	}	fprintf(stderr, "NOTE: Assigning temporary identifier \"%s\". "			"Name clash may occur later.\n",		expr->Identifier);}intyyerror(const char *msg) {	extern char *asn1p_text;	fprintf(stderr,		"ASN.1 grammar parse error "		"near line %d (token \"%s\"): %s\n",		yylineno, asn1p_text, msg);	return -1;}

⌨️ 快捷键说明

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