blob: 372730fc736f8b8e29505a73d44ce2fdd0a6f84d [file] [log] [blame]
Lev Walkin84cd58e2004-08-19 13:29:46 +00001#include "asn1c_internal.h"
2#include "asn1c_constraint.h"
Lev Walkin59004fa2004-08-20 13:37:01 +00003#include "asn1c_misc.h"
4#include "asn1c_out.h"
Lev Walkin84cd58e2004-08-19 13:29:46 +00005
6#include <asn1fix_crange.h> /* constraint groker from libasn1fix */
7#include <asn1fix_export.h> /* other exportable stuff from libasn1fix */
8
Lev Walkin59004fa2004-08-20 13:37:01 +00009static int asn1c_emit_constraint_tables(arg_t *arg, int got_size);
Lev Walkin84cd58e2004-08-19 13:29:46 +000010static int emit_alphabet_check_loop(arg_t *arg, asn1cnst_range_t *range);
Lev Walkin6ea088f2004-09-07 06:31:15 +000011static int emit_value_determination_code(arg_t *arg, asn1p_expr_type_e etype, asn1cnst_range_t *r_value);
Lev Walkin84cd58e2004-08-19 13:29:46 +000012static int emit_size_determination_code(arg_t *arg, asn1p_expr_type_e etype);
13static asn1p_expr_type_e _find_terminal_type(arg_t *arg);
Lev Walkin05363a72004-09-29 13:16:40 +000014static int emit_range_comparison_code(arg_t *arg, asn1cnst_range_t *range, const char *varname, asn1c_integer_t natural_start, asn1c_integer_t natural_stop);
Lev Walkin84cd58e2004-08-19 13:29:46 +000015
Lev Walkin84cd58e2004-08-19 13:29:46 +000016int
17asn1c_emit_constraint_checking_code(arg_t *arg) {
18 asn1cnst_range_t *r_size;
19 asn1cnst_range_t *r_value;
20 asn1p_expr_t *expr = arg->expr;
21 asn1p_expr_type_e etype;
22 asn1p_constraint_t *ct;
23 int got_something = 0;
Lev Walkin6938d042005-03-04 23:23:50 +000024 int alphabet_table_compiled;
Lev Walkin59004fa2004-08-20 13:37:01 +000025 int produce_st = 0;
Lev Walkin84cd58e2004-08-19 13:29:46 +000026
27 ct = expr->combined_constraints;
28 if(ct == NULL)
29 return 1; /* No additional constraints defined */
30
31 etype = _find_terminal_type(arg);
32
Lev Walkinbe717ec2004-08-25 02:03:59 +000033 r_value=asn1constraint_compute_PER_range(etype, ct, ACT_EL_RANGE,0,0,0);
34 r_size = asn1constraint_compute_PER_range(etype, ct, ACT_CT_SIZE,0,0,0);
Lev Walkin84cd58e2004-08-19 13:29:46 +000035 if(r_value) {
Lev Walkinbe717ec2004-08-25 02:03:59 +000036 if(r_value->incompatible
Lev Walkin84cd58e2004-08-19 13:29:46 +000037 || r_value->empty_constraint
38 || (r_value->left.type == ARE_MIN
39 && r_value->right.type == ARE_MAX)
40 || (etype == ASN_BASIC_BOOLEAN
41 && r_value->left.value == 0
42 && r_value->right.value == 1)
43 ) {
44 asn1constraint_range_free(r_value);
45 r_value = 0;
46 }
47 }
48 if(r_size) {
Lev Walkinbe717ec2004-08-25 02:03:59 +000049 if(r_size->incompatible
Lev Walkin84cd58e2004-08-19 13:29:46 +000050 || r_size->empty_constraint
51 || (r_size->left.value == 0 /* or .type == MIN */
52 && r_size->right.type == ARE_MAX)
53 ) {
54 asn1constraint_range_free(r_size);
55 r_size = 0;
56 }
57 }
58
Lev Walkin59004fa2004-08-20 13:37:01 +000059 /*
60 * Do we really need an "*st = sptr" pointer?
61 */
62 switch(etype) {
63 case ASN_BASIC_INTEGER:
64 case ASN_BASIC_ENUMERATED:
Lev Walkin082cadc2005-08-14 02:18:27 +000065 if(asn1c_type_fits_long(arg, arg->expr) == FL_NOTFIT)
66 produce_st = 1;
67 break;
Lev Walkinc78cbfb2004-09-14 12:47:45 +000068 case ASN_BASIC_REAL:
69 if(!(arg->flags & A1C_USE_NATIVE_TYPES))
Lev Walkin59004fa2004-08-20 13:37:01 +000070 produce_st = 1;
71 break;
Lev Walkin02b137d2004-08-21 07:34:58 +000072 case ASN_BASIC_BIT_STRING:
Lev Walkin59004fa2004-08-20 13:37:01 +000073 case ASN_BASIC_OCTET_STRING:
74 produce_st = 1;
75 break;
76 default:
77 if(etype & ASN_STRING_MASK)
78 produce_st = 1;
79 break;
80 }
Lev Walkince31cdb2005-02-15 03:37:42 +000081 if(produce_st) {
82 char *tname = asn1c_type_name(arg, arg->expr, TNF_SAFE);
83 OUT("const %s_t *st = (const %s_t *)sptr;\n", tname, tname);
84 }
Lev Walkin84cd58e2004-08-19 13:29:46 +000085
86 if(r_size || r_value) {
87 if(r_size) {
88 OUT("size_t size;\n");
89 }
90 if(r_value)
91 switch(etype) {
92 case ASN_BASIC_INTEGER:
93 case ASN_BASIC_ENUMERATED:
94 OUT("long value;\n");
95 break;
Lev Walkinc78cbfb2004-09-14 12:47:45 +000096 case ASN_BASIC_REAL:
97 OUT("double value;\n");
98 break;
Lev Walkin84cd58e2004-08-19 13:29:46 +000099 case ASN_BASIC_BOOLEAN:
Lev Walkine0b56e02005-02-25 12:10:27 +0000100 OUT("BOOLEAN_t value;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000101 break;
102 default:
103 break;
104 }
105 }
106
107 OUT("\n");
108
109 /*
110 * Protection against null input.
111 */
112 OUT("if(!sptr) {\n");
113 INDENT(+1);
114 OUT("_ASN_ERRLOG(app_errlog, app_key,\n");
Lev Walkin16835b62004-08-22 13:47:59 +0000115 OUT("\t\"%%s: value not given (%%s:%%d)\",\n");
116 OUT("\ttd->name, __FILE__, __LINE__);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000117 OUT("return -1;\n");
118 INDENT(-1);
119 OUT("}\n");
120 OUT("\n");
121
122 if(r_value)
Lev Walkin6ea088f2004-09-07 06:31:15 +0000123 emit_value_determination_code(arg, etype, r_value);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000124 if(r_size)
125 emit_size_determination_code(arg, etype);
126
Lev Walkin59004fa2004-08-20 13:37:01 +0000127 INDENT(-1);
128 REDIR(OT_CTABLES);
129 /* Emit FROM() tables */
Lev Walkin6938d042005-03-04 23:23:50 +0000130 alphabet_table_compiled =
131 (asn1c_emit_constraint_tables(arg, r_size?1:0) == 1);
Lev Walkin59004fa2004-08-20 13:37:01 +0000132 REDIR(OT_CODE);
133 INDENT(+1);
134
Lev Walkin84cd58e2004-08-19 13:29:46 +0000135 /*
136 * Here is an if() {} else {} constaint checking code.
137 */
138 OUT("\n");
139 OUT("if(");
140 INDENT(+1);
141 if(r_size) {
142 if(got_something++) { OUT("\n"); OUT(" && "); }
143 OUT("(");
144 emit_range_comparison_code(arg, r_size, "size", 0, -1);
145 OUT(")");
146 }
147 if(r_value) {
148 if(got_something++) { OUT("\n"); OUT(" && "); }
149 OUT("(");
150 if(etype == ASN_BASIC_BOOLEAN)
151 emit_range_comparison_code(arg, r_value,
152 "value", 0, 1);
153 else
154 emit_range_comparison_code(arg, r_value,
155 "value", -1, -1);
156 OUT(")");
157 }
Lev Walkin6938d042005-03-04 23:23:50 +0000158 if(alphabet_table_compiled) {
Lev Walkin84cd58e2004-08-19 13:29:46 +0000159 if(got_something++) { OUT("\n"); OUT(" && "); }
Lev Walkin6938d042005-03-04 23:23:50 +0000160 OUT("!check_permitted_alphabet_%d(%s)",
161 arg->expr->_type_unique_index,
162 produce_st ? "st" : "sptr");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000163 }
164 if(!got_something) {
165 OUT("1 /* No applicable constraints whatsoever */");
Lev Walkin59004fa2004-08-20 13:37:01 +0000166 OUT(") {\n");
167 INDENT(-1);
168 INDENTED(OUT("/* Nothing is here. See below */\n"));
169 OUT("}\n");
170 OUT("\n");
171 return 1;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000172 }
173 INDENT(-1);
174 OUT(") {\n");
175 INDENT(+1);
Lev Walkin7ef83a42005-03-29 19:04:24 +0000176 switch(etype) {
177 case ASN_CONSTR_SEQUENCE_OF:
Lev Walkin7ef83a42005-03-29 19:04:24 +0000178 case ASN_CONSTR_SET_OF:
179 OUT("/* Perform validation of the inner elements */\n");
Lev Walkin56784c02005-03-30 06:03:41 +0000180 OUT("return td->check_constraints(td, sptr, app_errlog, app_key);\n");
Lev Walkin7ef83a42005-03-29 19:04:24 +0000181 break;
182 default:
183 OUT("/* Constraint check succeeded */\n");
184 OUT("return 0;\n");
185 }
Lev Walkin84cd58e2004-08-19 13:29:46 +0000186 INDENT(-1);
187 OUT("} else {\n");
188 INDENT(+1);
189 OUT("_ASN_ERRLOG(app_errlog, app_key,\n");
Lev Walkin16835b62004-08-22 13:47:59 +0000190 OUT("\t\"%%s: constraint failed (%%s:%%d)\",\n");
191 OUT("\ttd->name, __FILE__, __LINE__);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000192 OUT("return -1;\n");
193 INDENT(-1);
194 OUT("}\n");
195
196 return 0;
197}
198
Lev Walkin59004fa2004-08-20 13:37:01 +0000199static int
200asn1c_emit_constraint_tables(arg_t *arg, int got_size) {
Lev Walkin05363a72004-09-29 13:16:40 +0000201 asn1c_integer_t range_start;
202 asn1c_integer_t range_stop;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000203 asn1p_expr_type_e etype;
204 asn1cnst_range_t *range;
Lev Walkin59004fa2004-08-20 13:37:01 +0000205 asn1p_constraint_t *ct;
206 int utf8_full_alphabet_check = 0;
207 int max_table_size = 256;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000208 int table[256];
209 int use_table;
210
Lev Walkin59004fa2004-08-20 13:37:01 +0000211 ct = arg->expr->combined_constraints;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000212 if(!ct) return 0;
213
214 etype = _find_terminal_type(arg);
215
Lev Walkinbe717ec2004-08-25 02:03:59 +0000216 range = asn1constraint_compute_PER_range(etype, ct, ACT_CT_FROM, 0,0,0);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000217 if(!range) return 0;
218
Lev Walkinbe717ec2004-08-25 02:03:59 +0000219 if(range->incompatible
Lev Walkin84cd58e2004-08-19 13:29:46 +0000220 || range->empty_constraint) {
221 asn1constraint_range_free(range);
222 return 0;
223 }
224
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000225
226 if(range->left.type == ARE_MIN
227 && range->right.type == ARE_MAX) {
228 /*
229 * The permitted alphabet constraint checker code guarantees
230 * that either both bounds (left/right) are present, or
231 * they're absent simultaneously. Thus, this assertion
232 * legitimately holds true.
233 */
234 assert(range->el_count == 0);
235 /* The full range is specified. Ignore it. */
236 return 0;
237 }
238
Lev Walkin84cd58e2004-08-19 13:29:46 +0000239 range_start = range->left.value;
240 range_stop = range->right.value;
241 assert(range->left.type == ARE_VALUE);
242 assert(range->right.type == ARE_VALUE);
243 assert(range_start <= range_stop);
244
245 range_start = 0; /* Force old behavior */
246
247 /*
248 * Check if we need a test table to check the alphabet.
249 */
250 use_table = 1;
Lev Walkin59004fa2004-08-20 13:37:01 +0000251 if(range->el_count == 0) {
252 /*
253 * It's better to have a short if() check
254 * than waste 4k of table space
255 */
256 use_table = 0;
257 }
Lev Walkin84cd58e2004-08-19 13:29:46 +0000258 if((range_stop - range_start) > 255)
259 use_table = 0;
Lev Walkin59004fa2004-08-20 13:37:01 +0000260 if(etype == ASN_STRING_UTF8String) {
261 if(range_stop >= 0x80)
262 use_table = 0;
263 else
264 max_table_size = 128;
265 }
Lev Walkin84cd58e2004-08-19 13:29:46 +0000266
Lev Walkin84cd58e2004-08-19 13:29:46 +0000267 if(use_table) {
268 int i, n = 0;
269 int untl;
270 memset(table, 0, sizeof(table));
271 for(i = -1; i < range->el_count; i++) {
272 asn1cnst_range_t *r;
Lev Walkin05363a72004-09-29 13:16:40 +0000273 asn1c_integer_t v;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000274 if(i == -1) {
275 if(range->el_count) continue;
276 r = range;
277 } else {
278 r = range->elements[i];
279 }
280 for(v = r->left.value; v <= r->right.value; v++) {
281 assert((v - range_start) >= 0);
Lev Walkin59004fa2004-08-20 13:37:01 +0000282 assert((v - range_start) < max_table_size);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000283 table[v - range_start] = ++n;
284 }
285 }
286
Lev Walkin84cd58e2004-08-19 13:29:46 +0000287 untl = (range_stop - range_start) + 1;
288 untl += (untl % 16)?16 - (untl % 16):0;
Lev Walkin59004fa2004-08-20 13:37:01 +0000289 OUT("static int permitted_alphabet_table_%d[%d] = {\n",
Lev Walkin6938d042005-03-04 23:23:50 +0000290 arg->expr->_type_unique_index, max_table_size);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000291 for(n = 0; n < untl; n++) {
292 OUT("%d,", table[n]?1:0);
293 if(!((n+1) % 16)) {
294 int c;
295 if(!n) {
296 OUT("\n");
297 continue;
298 }
299 OUT("\t/* ");
300 for(c = n - 15; c <= n; c++) {
301 if(table[c]) {
302 int a = c + range_start;
303 if(a > 0x20 && a < 0x80)
304 OUT("%c", a);
305 else
306 OUT(".");
307 } else {
308 OUT(" ");
309 }
310 }
311 OUT(" */");
312 OUT("\n");
313 }
314 }
315 OUT("};\n");
316 OUT("\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000317 } else if(etype == ASN_STRING_UTF8String) {
318 /*
319 * UTF8String type is a special case in many respects.
320 */
Lev Walkin59004fa2004-08-20 13:37:01 +0000321 if(got_size) {
322 /*
323 * Size has been already determined.
324 * The UTF8String length checker also checks
325 * for the syntax validity, so we don't have
326 * to repeat this process twice.
327 */
Lev Walkin59004fa2004-08-20 13:37:01 +0000328 asn1constraint_range_free(range);
329 return 0;
330 } else {
331 utf8_full_alphabet_check = 1;
332 }
333 } else {
334 /*
335 * This permitted alphabet check will be
336 * expressed using conditional statements
337 * instead of table lookups. Table would be
338 * to large or otherwise inappropriate (too sparse?).
339 */
Lev Walkin84cd58e2004-08-19 13:29:46 +0000340 }
341
342 OUT("static int check_permitted_alphabet_%d(const void *sptr) {\n",
Lev Walkin6938d042005-03-04 23:23:50 +0000343 arg->expr->_type_unique_index);
Lev Walkin59004fa2004-08-20 13:37:01 +0000344 INDENT(+1);
345 if(utf8_full_alphabet_check) {
Lev Walkin29c41682004-10-02 16:44:55 +0000346 OUT("if(UTF8String_length((const UTF8String_t *)sptr) < 0)\n");
347 OUT("\treturn -1; /* Alphabet (sic!) test failed. */\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000348 OUT("\n");
349 } else {
Lev Walkin84cd58e2004-08-19 13:29:46 +0000350 if(use_table) {
351 OUT("int *table = permitted_alphabet_table_%d;\n",
Lev Walkin6938d042005-03-04 23:23:50 +0000352 arg->expr->_type_unique_index);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000353 emit_alphabet_check_loop(arg, 0);
354 } else {
355 emit_alphabet_check_loop(arg, range);
356 }
Lev Walkin59004fa2004-08-20 13:37:01 +0000357 }
Lev Walkin775885e2004-08-22 12:47:03 +0000358 OUT("return 0;\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000359 INDENT(-1);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000360 OUT("}\n");
361 OUT("\n");
362
363 asn1constraint_range_free(range);
364
Lev Walkin6938d042005-03-04 23:23:50 +0000365 return 1;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000366}
367
368static int
369emit_alphabet_check_loop(arg_t *arg, asn1cnst_range_t *range) {
Lev Walkin05363a72004-09-29 13:16:40 +0000370 asn1c_integer_t natural_stop;
Lev Walkin634a3b82004-08-22 03:30:05 +0000371 asn1p_expr_t *terminal;
Lev Walkince31cdb2005-02-15 03:37:42 +0000372 char *tname;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000373
Lev Walkine4d6ab82004-09-22 16:05:13 +0000374 terminal = asn1f_find_terminal_type_ex(arg->asn, arg->expr);
Lev Walkin634a3b82004-08-22 03:30:05 +0000375 if(terminal) {
376 OUT("/* The underlying type is %s */\n",
377 ASN_EXPR_TYPE2STR(terminal->expr_type));
378 } else {
379 terminal = arg->expr;
380 }
Lev Walkince31cdb2005-02-15 03:37:42 +0000381 tname = asn1c_type_name(arg, terminal, TNF_SAFE);
382 OUT("const %s_t *st = (const %s_t *)sptr;\n", tname, tname);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000383
Lev Walkin634a3b82004-08-22 03:30:05 +0000384 switch(terminal->expr_type) {
Lev Walkin84cd58e2004-08-19 13:29:46 +0000385 case ASN_STRING_UTF8String:
Lev Walkin02b137d2004-08-21 07:34:58 +0000386 OUT("const uint8_t *ch = st->buf;\n");
387 OUT("const uint8_t *end = ch + st->size;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000388 OUT("\n");
389 OUT("for(; ch < end; ch++) {\n");
390 INDENT(+1);
391 OUT("uint8_t cv = *ch;\n");
Lev Walkin775885e2004-08-22 12:47:03 +0000392 if(!range) OUT("if(cv >= 0x80) return -1;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000393 natural_stop = 0xffffffffUL;
394 break;
395 case ASN_STRING_UniversalString:
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000396 OUT("const uint8_t *ch = st->buf;\n");
397 OUT("const uint8_t *end = ch + st->size;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000398 OUT("\n");
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000399 OUT("if(st->size %% 4) return -1; /* (size%%4)! */\n");
400 OUT("for(; ch < end; ch += 4) {\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000401 INDENT(+1);
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000402 OUT("uint32_t cv = (ch[0] << 24)\n");
403 OUT("\t\t| (ch[1] << 16)\n");
404 OUT("\t\t| (ch[2] << 8)\n");
405 OUT("\t\t| ch[3];\n");
Lev Walkin775885e2004-08-22 12:47:03 +0000406 if(!range) OUT("if(cv > 255) return -1;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000407 natural_stop = 0xffffffffUL;
408 break;
409 case ASN_STRING_BMPString:
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000410 OUT("const uint8_t *ch = st->buf;\n");
411 OUT("const uint8_t *end = ch + st->size;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000412 OUT("\n");
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000413 OUT("if(st->size %% 2) return -1; /* (size%%2)! */\n");
414 OUT("for(; ch < end; ch += 2) {\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000415 INDENT(+1);
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000416 OUT("uint16_t cv = (ch[0] << 8)\n");
417 OUT("\t\t| ch[1];\n");
Lev Walkin775885e2004-08-22 12:47:03 +0000418 if(!range) OUT("if(cv > 255) return -1;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000419 natural_stop = 0xffff;
420 break;
421 case ASN_BASIC_OCTET_STRING:
422 default:
Lev Walkin02b137d2004-08-21 07:34:58 +0000423 OUT("const uint8_t *ch = st->buf;\n");
424 OUT("const uint8_t *end = ch + st->size;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000425 OUT("\n");
426 OUT("for(; ch < end; ch++) {\n");
427 INDENT(+1);
428 OUT("uint8_t cv = *ch;\n");
429 natural_stop = 0xff;
430 break;
431 }
432
433 if(range) {
434 OUT("if(!(");
435 emit_range_comparison_code(arg, range, "cv", 0, natural_stop);
Lev Walkin775885e2004-08-22 12:47:03 +0000436 OUT(")) return -1;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000437 } else {
Lev Walkin775885e2004-08-22 12:47:03 +0000438 OUT("if(!table[cv]) return -1;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000439 }
440
441 INDENT(-1);
442 OUT("}\n");
443
444 return 0;
445}
446
447static int
Lev Walkin05363a72004-09-29 13:16:40 +0000448emit_range_comparison_code(arg_t *arg, asn1cnst_range_t *range, const char *varname, asn1c_integer_t natural_start, asn1c_integer_t natural_stop) {
Lev Walkin84cd58e2004-08-19 13:29:46 +0000449 int ignore_left;
450 int ignore_right;
Lev Walkin59004fa2004-08-20 13:37:01 +0000451 int generated_something = 0;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000452 int i;
453
454 for(i = -1; i < range->el_count; i++) {
455 asn1cnst_range_t *r;
456 if(i == -1) {
457 if(range->el_count) continue;
458 r = range;
459 } else {
460 if(i) OUT(" || ");
461 r = range->elements[i];
462 }
463
464 if(r != range) OUT("(");
465
466 ignore_left = (r->left.type == ARE_MIN)
467 || (natural_start != -1
468 && r->left.value <= natural_start);
469 ignore_right = (r->right.type == ARE_MAX)
470 || (natural_stop != -1
471 && r->right.value >= natural_stop);
472 if(ignore_left && ignore_right) {
473 OUT("1 /* Constraint matches natural range of %s */",
474 varname);
475 continue;
476 }
477
478 if(ignore_left) {
Lev Walkin33c16ba2004-09-24 21:01:43 +0000479 OUT("%s <= %" PRIdASN, varname,
480 r->right.value);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000481 } else if(ignore_right) {
Lev Walkin33c16ba2004-09-24 21:01:43 +0000482 OUT("%s >= %" PRIdASN, varname,
483 r->left.value);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000484 } else if(r->left.value == r->right.value) {
Lev Walkin33c16ba2004-09-24 21:01:43 +0000485 OUT("%s == %" PRIdASN, varname,
486 r->right.value);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000487 } else {
Lev Walkin33c16ba2004-09-24 21:01:43 +0000488 OUT("%s >= %" PRIdASN " && %s <= %" PRIdASN,
Lev Walkin84cd58e2004-08-19 13:29:46 +0000489 varname,
Lev Walkin33c16ba2004-09-24 21:01:43 +0000490 r->left.value,
Lev Walkin84cd58e2004-08-19 13:29:46 +0000491 varname,
Lev Walkin33c16ba2004-09-24 21:01:43 +0000492 r->right.value);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000493 }
494 if(r != range) OUT(")");
Lev Walkin59004fa2004-08-20 13:37:01 +0000495 generated_something = 1;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000496 }
497
Lev Walkin59004fa2004-08-20 13:37:01 +0000498 return generated_something;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000499}
500
501static int
502emit_size_determination_code(arg_t *arg, asn1p_expr_type_e etype) {
503
504 switch(etype) {
505 case ASN_BASIC_BIT_STRING:
506 OUT("if(st->size > 0) {\n");
507 OUT("\t/* Size in bits */\n");
Lev Walkincb2b2d12004-09-05 10:42:19 +0000508 OUT("\tsize = 8 * (st->size - 1) - (st->buf[0] & 0x7);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000509 OUT("} else {\n");
510 OUT("\tsize = 0;\n");
511 OUT("}\n");
512 break;
513 case ASN_STRING_UniversalString:
514 OUT("size = st->size >> 2;\t/* 4 byte per character */\n");
515 break;
516 case ASN_STRING_BMPString:
517 OUT("size = st->size >> 1;\t/* 2 byte per character */\n");
518 break;
519 case ASN_STRING_UTF8String:
Lev Walkin29c41682004-10-02 16:44:55 +0000520 OUT("size = UTF8String_length(st);\n");
521 OUT("if((ssize_t)size < 0) {\n");
522 OUT("\t_ASN_ERRLOG(app_errlog, app_key,\n");
523 OUT("\t\t\"%%s: UTF-8: broken encoding (%%s:%%d)\",\n");
524 OUT("\t\ttd->name, __FILE__, __LINE__);\n");
525 OUT("\treturn -1;\n");
526 OUT("}\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000527 break;
528 case ASN_CONSTR_SET_OF:
529 case ASN_CONSTR_SEQUENCE_OF:
Lev Walkinf1a51232005-07-04 02:01:03 +0000530 OUT("/* Determine the number of elements */\n");
531 OUT("size = _A_C%s_FROM_VOID(sptr)->count;\n",
532 etype==ASN_CONSTR_SET_OF?"SET":"SEQUENCE");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000533 break;
Lev Walkin59004fa2004-08-20 13:37:01 +0000534 case ASN_BASIC_OCTET_STRING:
535 OUT("size = st->size;\n");
536 break;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000537 default:
Lev Walkin59004fa2004-08-20 13:37:01 +0000538 if(etype & ASN_STRING_MASK) {
Lev Walkin84cd58e2004-08-19 13:29:46 +0000539 OUT("size = st->size;\n");
540 break;
541 } else {
542 const char *type_name = ASN_EXPR_TYPE2STR(etype);
543 if(!type_name) type_name = arg->expr->Identifier;
544 WARNING("SizeConstraint is not defined for %s",
545 type_name);
546 OUT_NOINDENT("#warning SizeConstraint "
547 "is not defined for %s!\n", type_name);
548 OUT("size = st->size;\n");
549 }
550 return -1;
551 }
552
553 return 0;
554}
555
556static int
Lev Walkin6ea088f2004-09-07 06:31:15 +0000557emit_value_determination_code(arg_t *arg, asn1p_expr_type_e etype, asn1cnst_range_t *r_value) {
Lev Walkin84cd58e2004-08-19 13:29:46 +0000558
559 switch(etype) {
560 case ASN_BASIC_INTEGER:
561 case ASN_BASIC_ENUMERATED:
Lev Walkin082cadc2005-08-14 02:18:27 +0000562 if(asn1c_type_fits_long(arg, arg->expr) != FL_NOTFIT) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000563 OUT("value = *(const long *)sptr;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000564 } else {
Lev Walkin6ea088f2004-09-07 06:31:15 +0000565 if(r_value->el_count == 0
566 && (
567 /* Speed-up common case: (0..MAX) */
568 (r_value->left.type == ARE_VALUE
569 && r_value->left.value == 0
570 && r_value->right.type == ARE_MAX)
571 ||
572 /* Speed-up common case: (MIN..-1) */
573 (r_value->left.type == ARE_MIN
574 && r_value->right.type == ARE_VALUE
575 && r_value->right.value == -1)
576 )) {
577 OUT("/* Check if the sign bit is present */\n");
578 OUT("value = st->buf ? ((st->buf[0] & 0x80) ? -1 : 1) : 0;\n");
579 break;
580 }
581
Lev Walkin05363a72004-09-29 13:16:40 +0000582 OUT("if(asn_INTEGER2long(st, &value)) {\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000583 INDENT(+1);
584 OUT("_ASN_ERRLOG(app_errlog, app_key,\n");
Lev Walkin16835b62004-08-22 13:47:59 +0000585 OUT("\t\"%%s: value too large (%%s:%%d)\",\n");
586 OUT("\ttd->name, __FILE__, __LINE__);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000587 OUT("return -1;\n");
588 INDENT(-1);
589 OUT("}\n");
590 }
591 break;
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000592 case ASN_BASIC_REAL:
593 if(arg->flags & A1C_USE_NATIVE_TYPES) {
594 OUT("value = *(const double *)sptr;\n");
595 } else {
Lev Walkin05363a72004-09-29 13:16:40 +0000596 OUT("if(asn_REAL2double(st, &value)) {\n");
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000597 INDENT(+1);
598 OUT("_ASN_ERRLOG(app_errlog, app_key,\n");
599 OUT("\t\"%%s: value too large (%%s:%%d)\",\n");
600 OUT("\ttd->name, __FILE__, __LINE__);\n");
601 OUT("return -1;\n");
602 INDENT(-1);
603 OUT("}\n");
604 }
605 break;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000606 case ASN_BASIC_BOOLEAN:
Lev Walkine0b56e02005-02-25 12:10:27 +0000607 OUT("value = (*(const long *)sptr) ? 1 : 0;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000608 break;
609 default:
Lev Walkin02b137d2004-08-21 07:34:58 +0000610 WARNING("%s:%d: Value cannot be determined "
611 "for constraint check for %s",
612 arg->mod->source_file_name,
613 arg->expr->_lineno,
614 arg->expr->Identifier
615 );
616 OUT_NOINDENT(
617 "#error %s:%d: Value of %s cannot be determined\n",
618 arg->mod->source_file_name,
619 arg->expr->_lineno,
620 arg->expr->Identifier
621 );
Lev Walkin84cd58e2004-08-19 13:29:46 +0000622 break;
623 }
624
625 return 0;
626}
627
628static asn1p_expr_type_e
629_find_terminal_type(arg_t *arg) {
630 asn1p_expr_t *expr;
Lev Walkine4d6ab82004-09-22 16:05:13 +0000631 expr = asn1f_find_terminal_type_ex(arg->asn, arg->expr);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000632 if(expr) return expr->expr_type;
633 return A1TC_INVALID;
634}
635