aboutsummaryrefslogtreecommitdiffstats
path: root/libasn1fix/asn1fix_compat.c
blob: aef26c0b2fd6576a01f8b204d7dc59e11becb299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "asn1fix_internal.h"

static int asn1f_check_same_children(arg_t *arg, asn1p_expr_t *a, asn1p_expr_t *b);

/*
 * Check that the expressions given are compatible in their type.
 * ORDER DOES MATTER! (See .h).
 */
int
asn1f_check_type_compatibility(arg_t *arg, asn1p_expr_t *a, asn1p_expr_t *b) {
	asn1p_expr_type_e atype, btype;

	atype = a->expr_type;
	btype = b->expr_type;

	DEBUG("%s(%s:%x@%d, %s:%x@%d)", __func__,
		a->Identifier, atype, a->_lineno,
		b->Identifier, btype, b->_lineno);

	/*
	 * Expected terminal type!
	 */
	assert(atype != A1TC_REFERENCE);
	assert(btype != A1TC_REFERENCE);

	if(atype != btype) {
		/*
		 * Limited compatibility.
		 */
		if((atype == A1TC_UNIVERVAL && btype == ASN_BASIC_INTEGER)
		|| (atype == A1TC_UNIVERVAL && btype == ASN_BASIC_ENUMERATED)
		)
			return 0;
		DEBUG("\t%s and %s are not compatible",
			a->Identifier, b->Identifier);
		return -1;	/* Fairly obviously */
	}

	if(a == b)
		return 0;	/* Fairly obviously */

	switch(atype) {
	case ASN_BASIC_INTEGER:
		/* All integers are compatible */
		return 0;
	case ASN_BASIC_ENUMERATED:
		/*
		 * Enumerations are not compatible
		 * unless their definitions are the same.
		 */
		if(asn1f_check_same_children(arg, a, b)) {
			DEBUG("\tEnumerations are different %s and %s",
				a->Identifier, b->Identifier);
			return -1;
		}
		return 0;
	default:
		/* Compatibility is not defined yet */
		DEBUG("\tCompatibility rule is not defined for %s and %s",
			a->Identifier, b->Identifier);
		return -1;
	}

	return 0;
}

/*
 * Check that the children are exactly same.
 */
static int
asn1f_check_same_children(arg_t *arg, asn1p_expr_t *a, asn1p_expr_t *b) {
	asn1p_expr_t *achild;
	asn1p_expr_t *bchild;

	achild = TQ_FIRST(&(a->members));
	bchild = TQ_FIRST(&(b->members));

	while(1) {
		if(achild->expr_type != bchild->expr_type)
			return -1;

		if(achild->Identifier && bchild->Identifier) {
			if(strcmp(achild->Identifier, bchild->Identifier))
				return -1;
		} else if(!(!achild->Identifier && !bchild->Identifier)) {
			return -1;
		}

		if(achild->value && bchild->value) {
			if(achild->value->type != bchild->value->type)
				return -1;
			switch(achild->value->type) {
			case ATV_INTEGER:
				if(achild->value->value.v_integer
				!= bchild->value->value.v_integer)
					return -1;
				break;
			case ATV_REFERENCED:
			default:
				DEBUG("Value %s at lines %d and "
					"%d cannot be used in "
					"semantical equality check",
					asn1f_printable_value(achild->value),
					achild->value->value.reference->_lineno,
					bchild->value->value.reference->_lineno
				);
				return -1;
			}
		} else if(!(!achild->value && !bchild->value)) {
			/* One of values is defined, and another is not */
			return -1;
		}

		achild = TQ_NEXT(achild, next);
		bchild = TQ_NEXT(bchild, next);

		if(achild && bchild)
			continue;
		else if(!achild && !bchild)
			break;
		else
			return -1;
	}

	DEBUG("\t%s:%x@%d and %s:%x@%d are semantically equivalent",
		a->Identifier, a->expr_type, a->_lineno,
		b->Identifier, b->expr_type, b->_lineno);

	return 0;
}