aboutsummaryrefslogtreecommitdiffstats
path: root/libasn1fix/asn1fix_compat.c
blob: 064e38e522d3013f054a6c0a949dab4c180332b7 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#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:%x@%d, %s:%x@%d)",
		a->Identifier, atype, a->_lineno,
		b->Identifier, btype, b->_lineno);

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

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

	if(atype != btype) {
		/*
		 * Limited cross-compatibility of integer types.
		 */
		if((atype == A1TC_UNIVERVAL && btype == ASN_BASIC_INTEGER)
		|| (atype == A1TC_UNIVERVAL && btype == ASN_BASIC_ENUMERATED)
		)
			return 0;

		/* Limited cross-compatibility of string types */
		if((atype & ASN_STRING_MASK)
		&& (btype & ASN_STRING_MASK)) {
			/* X.680, B.5 */
			int akm = (atype & ASN_STRING_KM_MASK)
				|| atype == ASN_STRING_UTF8String;
			int bkm = (btype & ASN_STRING_KM_MASK)
				|| btype == ASN_STRING_UTF8String;
			return (akm == bkm) ? 0 : -1;
		}

		DEBUG("\t%s and %s are not compatible",
			a->Identifier, b->Identifier);
		return -1;	/* Fairly obviously */
	}

	switch(atype) {
	case ASN_BASIC_INTEGER:
		/* All integers are compatible, X.680, B.4.5 */
		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:
		if((atype & ASN_STRING_MASK)
		&& (btype & ASN_STRING_MASK)) {
			/* String type is compatible with the same type */
			return 0;
		}
		/* 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;
}