blob: 36e5466652750468205a2148a0b98f12e2470f06 [file] [log] [blame]
Lev Walkinf15320b2004-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
Lev Walkin03850182005-03-10 10:02:50 +000026 DEBUG("(\"%s\", %x) for line %d",
Lev Walkinf15320b2004-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) {
Lev Walkina0c92902006-08-28 02:24:24 +000041 FATAL("INTEGER %s at line %d: "
Lev Walkinf15320b2004-06-03 03:38:44 +000042 "Extension marker is not allowed",
43 expr->Identifier,
Lev Walkina0c92902006-08-28 02:24:24 +000044 iv->_lineno);
Lev Walkinf15320b2004-06-03 03:38:44 +000045 rvalue = -1;
46 continue;
47 }
48
49 if(iv->Identifier == NULL
50 || iv->expr_type != A1TC_UNIVERVAL) {
Lev Walkina0c92902006-08-28 02:24:24 +000051 FATAL("INTEGER %s at line %d: "
Lev Walkinf15320b2004-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) {
Lev Walkina0c92902006-08-28 02:24:24 +000062 FATAL("INTEGER %s at line %d: "
Lev Walkinf15320b2004-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 */
Lev Walkin9288d1c2005-03-10 11:27:13 +000075 if(asn1f_value_resolve(arg, iv, 0)) {
Lev Walkinf15320b2004-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) {
Lev Walkina0c92902006-08-28 02:24:24 +000083 FATAL("INTEGER %s at line %d: "
Lev Walkinf15320b2004-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 */
Lev Walkinfbfc7bc2006-08-28 02:45:44 +000095 ret = asn1f_check_unique_expr_child(arg, iv, 0, "identifier");
Lev Walkinf15320b2004-06-03 03:38:44 +000096 RET2RVAL(ret, rvalue);
97 /*
98 * Check that all values are distinct.
99 */
Lev Walkinfbfc7bc2006-08-28 02:45:44 +0000100 ret = asn1f_check_unique_expr_child(arg, iv,
101 _compare_value, "value");
Lev Walkinf15320b2004-06-03 03:38:44 +0000102 RET2RVAL(ret, rvalue);
103 }
104
105
106 return rvalue;
107}
108
Lev Walkin1715b322013-03-28 04:02:13 -0700109#if 0
Lev Walkinf15320b2004-06-03 03:38:44 +0000110static int
111_asn1f_make_sure_type_is(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_type_e type) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000112 asn1p_expr_t *next_expr;
Lev Walkind9bd7752004-06-05 08:17:50 +0000113 asn1p_expr_type_e expr_type;
Lev Walkinf15320b2004-06-03 03:38:44 +0000114 int ret;
115
116 expr_type = expr->expr_type;
117
118 /*
119 * Here we're trying to make sure that the type of the given
120 * expression is really what is expected.
121 * This is ensured in two ways.
122 * First, if the immediate type matches the provided one,
123 * this is a clear hit.
124 */
125 if(expr_type == type)
126 return 0;
127
128 /*
129 * Otherwise, it must be either a reference or a different type.
130 */
131 if(expr_type != A1TC_REFERENCE) {
132 errno = EPERM;
133 return -1;
134 }
135
136 assert(expr_type == A1TC_REFERENCE);
137 assert(expr->reference);
138
139 /*
140 * Then, it is a reference. For a reference, try to resolve type
141 * and try again.
142 */
Lev Walkina00d6b32006-03-21 03:40:38 +0000143 next_expr = asn1f_lookup_symbol(arg, expr->module,
144 expr->rhs_pspecs, expr->reference);
Lev Walkinf15320b2004-06-03 03:38:44 +0000145 if(next_expr == NULL) {
146 errno = ESRCH;
147 return -1;
148 }
149
150 /*
151 * If symbol is here, recursively check that it conforms to the type.
152 */
Lev Walkin6fec44d2004-08-22 03:10:23 +0000153 WITH_MODULE(next_expr->module,
154 ret = _asn1f_make_sure_type_is(arg, next_expr, type));
Lev Walkinf15320b2004-06-03 03:38:44 +0000155
156 return ret;
157}
Lev Walkin1715b322013-03-28 04:02:13 -0700158#endif
Lev Walkinf15320b2004-06-03 03:38:44 +0000159
160