blob: a5d0b6e7487f947a16b6f035d522218d4c3b1867 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#include "asn1fix_internal.h"
2
3static int _compare_value(asn1p_expr_t *expr1, asn1p_expr_t *expr2) {
4 if(expr2->value->type == ATV_INTEGER
5 && expr1->value->type == ATV_INTEGER) {
6 return expr2->value->value.v_integer
7 - expr1->value->value.v_integer;
8 } else {
9 return -1;
10 }
11}
12
13/*
14 * Check the validity of an INTEGER type.
15 */
16int
17asn1f_fix_integer(arg_t *arg) {
18 asn1p_expr_t *expr = arg->expr;
19 asn1p_expr_t *iv;
20 int rvalue = 0;
21 int ret;
22
23 if(expr->expr_type != ASN_BASIC_INTEGER)
24 return 0; /* Just ignore it */
25
vlmfd245932005-03-10 10:02:50 +000026 DEBUG("(\"%s\", %x) for line %d",
vlmfa67ddc2004-06-03 03:38:44 +000027 expr->Identifier, expr->expr_type, expr->_lineno);
28
29 /*
30 * Scan the integer values in search for inconsistencies.
31 */
32 TQ_FOR(iv, &(expr->members), next) {
33
34 DEBUG("\tItem %s(%s)", iv->Identifier,
35 asn1f_printable_value(iv->value));
36
37 /*
38 * Found "...", check correctness.
39 */
40 if(iv->expr_type == A1TC_EXTENSIBLE) {
vlmadf419b2006-08-28 02:24:24 +000041 FATAL("INTEGER %s at line %d: "
vlmfa67ddc2004-06-03 03:38:44 +000042 "Extension marker is not allowed",
43 expr->Identifier,
vlmadf419b2006-08-28 02:24:24 +000044 iv->_lineno);
vlmfa67ddc2004-06-03 03:38:44 +000045 rvalue = -1;
46 continue;
47 }
48
49 if(iv->Identifier == NULL
50 || iv->expr_type != A1TC_UNIVERVAL) {
vlmadf419b2006-08-28 02:24:24 +000051 FATAL("INTEGER %s at line %d: "
vlmfa67ddc2004-06-03 03:38:44 +000052 "Unsupported enumeration element %s",
53 expr->Identifier,
54 iv->_lineno,
55 iv->Identifier?iv->Identifier:"<Anonymous>"
56 );
57 rvalue = -1;
58 continue;
59 }
60
61 if(iv->value == NULL) {
vlmadf419b2006-08-28 02:24:24 +000062 FATAL("INTEGER %s at line %d: "
vlmfa67ddc2004-06-03 03:38:44 +000063 "Value for the identifier %s "
64 "must be set explicitly",
65 expr->Identifier,
66 iv->_lineno,
67 iv->Identifier
68 );
69 rvalue = -1;
70 continue;
71 } else if(iv->value->type == ATV_REFERENCED) {
72 /*
73 * Resolve the value, once and for all.
74 */
vlm8fba2572005-03-10 11:27:13 +000075 if(asn1f_value_resolve(arg, iv, 0)) {
vlmfa67ddc2004-06-03 03:38:44 +000076 /* This function will emit messages */
77 rvalue = -1;
78 continue;
79 }
80 }
81
82 if(iv->value->type != ATV_INTEGER) {
vlmadf419b2006-08-28 02:24:24 +000083 FATAL("INTEGER %s at line %d: "
vlmfa67ddc2004-06-03 03:38:44 +000084 "Value for the identifier %s "
85 "is not compatible with INTEGER type",
86 expr->Identifier,
87 iv->_lineno);
88 rvalue = -1;
89 continue;
90 }
91
92 /*
93 * Check that all identifiers are distinct.
94 */
vlm073a7c72006-08-28 02:45:44 +000095 ret = asn1f_check_unique_expr_child(arg, iv, 0, "identifier");
vlmfa67ddc2004-06-03 03:38:44 +000096 RET2RVAL(ret, rvalue);
97 /*
98 * Check that all values are distinct.
99 */
vlm073a7c72006-08-28 02:45:44 +0000100 ret = asn1f_check_unique_expr_child(arg, iv,
101 _compare_value, "value");
vlmfa67ddc2004-06-03 03:38:44 +0000102 RET2RVAL(ret, rvalue);
103 }
104
105
106 return rvalue;
107}
108
109static int
110_asn1f_make_sure_type_is(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_type_e type) {
vlmfa67ddc2004-06-03 03:38:44 +0000111 asn1p_expr_t *next_expr;
vlmb42843a2004-06-05 08:17:50 +0000112 asn1p_expr_type_e expr_type;
vlmfa67ddc2004-06-03 03:38:44 +0000113 int ret;
114
115 expr_type = expr->expr_type;
116
117 /*
118 * Here we're trying to make sure that the type of the given
119 * expression is really what is expected.
120 * This is ensured in two ways.
121 * First, if the immediate type matches the provided one,
122 * this is a clear hit.
123 */
124 if(expr_type == type)
125 return 0;
126
127 /*
128 * Otherwise, it must be either a reference or a different type.
129 */
130 if(expr_type != A1TC_REFERENCE) {
131 errno = EPERM;
132 return -1;
133 }
134
135 assert(expr_type == A1TC_REFERENCE);
136 assert(expr->reference);
137
138 /*
139 * Then, it is a reference. For a reference, try to resolve type
140 * and try again.
141 */
vlm0c6d3812006-03-21 03:40:38 +0000142 next_expr = asn1f_lookup_symbol(arg, expr->module,
143 expr->rhs_pspecs, expr->reference);
vlmfa67ddc2004-06-03 03:38:44 +0000144 if(next_expr == NULL) {
145 errno = ESRCH;
146 return -1;
147 }
148
149 /*
150 * If symbol is here, recursively check that it conforms to the type.
151 */
vlm2e0c1942004-08-22 03:10:23 +0000152 WITH_MODULE(next_expr->module,
153 ret = _asn1f_make_sure_type_is(arg, next_expr, type));
vlmfa67ddc2004-06-03 03:38:44 +0000154
155 return ret;
156}
157
158