blob: bd025d55b737ecdd0f355415371ef15d41305595 [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 Walkinb5450702017-10-04 02:52:57 -07005#include "asn1c_naming.h"
Lev Walkin84cd58e2004-08-19 13:29:46 +00006
7#include <asn1fix_crange.h> /* constraint groker from libasn1fix */
Lev Walkin59b176e2005-11-26 11:25:14 +00008#include <asn1fix_export.h> /* other exportables from libasn1fix */
Lev Walkin84cd58e2004-08-19 13:29:46 +00009
Lev Walkin59004fa2004-08-20 13:37:01 +000010static int asn1c_emit_constraint_tables(arg_t *arg, int got_size);
Lev Walkin84cd58e2004-08-19 13:29:46 +000011static int emit_alphabet_check_loop(arg_t *arg, asn1cnst_range_t *range);
Lev Walkin6ea088f2004-09-07 06:31:15 +000012static 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 +000013static int emit_size_determination_code(arg_t *arg, asn1p_expr_type_e etype);
14static asn1p_expr_type_e _find_terminal_type(arg_t *arg);
Lev Walkin05363a72004-09-29 13:16:40 +000015static 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 Walkine0f2a5b2017-08-30 20:02:29 -070016static int native_long_sign(arg_t *arg, asn1cnst_range_t *r); /* -1, 0, 1 */
Lev Walkin84cd58e2004-08-19 13:29:46 +000017
Santiago Carot-Nemesio8da8e732011-04-22 13:21:33 +020018static int
Lev Walkine0f2a5b2017-08-30 20:02:29 -070019ulong_optimization(arg_t *arg, asn1p_expr_type_e etype, asn1cnst_range_t *r_size,
Santiago Carot-Nemesio8da8e732011-04-22 13:21:33 +020020 asn1cnst_range_t *r_value)
21{
22 return (!r_size && r_value
23 && (etype == ASN_BASIC_INTEGER
24 || etype == ASN_BASIC_ENUMERATED)
Lev Walkine0f2a5b2017-08-30 20:02:29 -070025 && native_long_sign(arg, r_value) == 0);
Santiago Carot-Nemesio8da8e732011-04-22 13:21:33 +020026}
27
Lev Walkin84cd58e2004-08-19 13:29:46 +000028int
29asn1c_emit_constraint_checking_code(arg_t *arg) {
30 asn1cnst_range_t *r_size;
31 asn1cnst_range_t *r_value;
32 asn1p_expr_t *expr = arg->expr;
33 asn1p_expr_type_e etype;
34 asn1p_constraint_t *ct;
35 int got_something = 0;
Lev Walkin6938d042005-03-04 23:23:50 +000036 int alphabet_table_compiled;
Lev Walkin59004fa2004-08-20 13:37:01 +000037 int produce_st = 0;
Santiago Carot-Nemesio8da8e732011-04-22 13:21:33 +020038 int ulong_optimize = 0;
Bi-Ruei, Chiu64505ef2016-11-08 13:59:29 +080039 int ret = 0;
Lev Walkin84cd58e2004-08-19 13:29:46 +000040
41 ct = expr->combined_constraints;
42 if(ct == NULL)
43 return 1; /* No additional constraints defined */
44
45 etype = _find_terminal_type(arg);
46
Lev Walkina28cbb92017-07-31 20:20:17 -070047 r_value=asn1constraint_compute_constraint_range(expr->Identifier, etype, ct, ACT_EL_RANGE,0,0,0);
48 r_size =asn1constraint_compute_constraint_range(expr->Identifier, etype, ct, ACT_CT_SIZE, 0,0,0);
Lev Walkin84cd58e2004-08-19 13:29:46 +000049 if(r_value) {
Lev Walkinbe717ec2004-08-25 02:03:59 +000050 if(r_value->incompatible
Lev Walkin84cd58e2004-08-19 13:29:46 +000051 || r_value->empty_constraint
52 || (r_value->left.type == ARE_MIN
53 && r_value->right.type == ARE_MAX)
54 || (etype == ASN_BASIC_BOOLEAN
55 && r_value->left.value == 0
56 && r_value->right.value == 1)
57 ) {
58 asn1constraint_range_free(r_value);
59 r_value = 0;
60 }
61 }
62 if(r_size) {
Lev Walkinbe717ec2004-08-25 02:03:59 +000063 if(r_size->incompatible
Lev Walkin84cd58e2004-08-19 13:29:46 +000064 || r_size->empty_constraint
65 || (r_size->left.value == 0 /* or .type == MIN */
66 && r_size->right.type == ARE_MAX)
67 ) {
68 asn1constraint_range_free(r_size);
69 r_size = 0;
70 }
71 }
72
Lev Walkin59004fa2004-08-20 13:37:01 +000073 /*
74 * Do we really need an "*st = sptr" pointer?
75 */
76 switch(etype) {
77 case ASN_BASIC_INTEGER:
78 case ASN_BASIC_ENUMERATED:
Lev Walkin082cadc2005-08-14 02:18:27 +000079 if(asn1c_type_fits_long(arg, arg->expr) == FL_NOTFIT)
80 produce_st = 1;
81 break;
Lev Walkinc78cbfb2004-09-14 12:47:45 +000082 case ASN_BASIC_REAL:
Lev Walkin2a744a72013-03-27 01:56:23 -070083 if((arg->flags & A1C_USE_WIDE_TYPES))
Lev Walkin59004fa2004-08-20 13:37:01 +000084 produce_st = 1;
85 break;
Lev Walkin02b137d2004-08-21 07:34:58 +000086 case ASN_BASIC_BIT_STRING:
Lev Walkin59004fa2004-08-20 13:37:01 +000087 case ASN_BASIC_OCTET_STRING:
88 produce_st = 1;
Lev Walkin3a4689a2006-11-24 11:20:27 +000089 break;
Lev Walkin59004fa2004-08-20 13:37:01 +000090 default:
91 if(etype & ASN_STRING_MASK)
92 produce_st = 1;
93 break;
94 }
Lev Walkince31cdb2005-02-15 03:37:42 +000095 if(produce_st) {
Lev Walkincf3f6eb2017-08-23 04:34:15 -070096 const char *tname = asn1c_type_name(arg, arg->expr, TNF_SAFE);
Lev Walkince31cdb2005-02-15 03:37:42 +000097 OUT("const %s_t *st = (const %s_t *)sptr;\n", tname, tname);
98 }
Lev Walkin84cd58e2004-08-19 13:29:46 +000099
100 if(r_size || r_value) {
101 if(r_size) {
102 OUT("size_t size;\n");
103 }
104 if(r_value)
105 switch(etype) {
106 case ASN_BASIC_INTEGER:
107 case ASN_BASIC_ENUMERATED:
Lev Walkine0f2a5b2017-08-30 20:02:29 -0700108 if(native_long_sign(arg, r_value) >= 0) {
109 ulong_optimize = ulong_optimization(arg, etype, r_size, r_value);
Santiago Carot-Nemesio8da8e732011-04-22 13:21:33 +0200110 if(!ulong_optimize) {
111 OUT("unsigned long value;\n");
112 }
Lev Walkin8bb57a22007-12-03 13:41:36 +0000113 } else {
114 OUT("long value;\n");
115 }
Lev Walkin84cd58e2004-08-19 13:29:46 +0000116 break;
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000117 case ASN_BASIC_REAL:
Lev Walkinb5450702017-10-04 02:52:57 -0700118 OUT("%s value;\n", c_name(arg).type.constrained_c_name);
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000119 break;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000120 case ASN_BASIC_BOOLEAN:
Lev Walkine0b56e02005-02-25 12:10:27 +0000121 OUT("BOOLEAN_t value;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000122 break;
123 default:
124 break;
125 }
126 }
127
128 OUT("\n");
129
130 /*
131 * Protection against null input.
132 */
133 OUT("if(!sptr) {\n");
134 INDENT(+1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700135 OUT("ASN__CTFAIL(app_key, td, sptr,\n");
Lev Walkin16835b62004-08-22 13:47:59 +0000136 OUT("\t\"%%s: value not given (%%s:%%d)\",\n");
137 OUT("\ttd->name, __FILE__, __LINE__);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000138 OUT("return -1;\n");
139 INDENT(-1);
140 OUT("}\n");
141 OUT("\n");
142
Santiago Carot-Nemesio8da8e732011-04-22 13:21:33 +0200143 if((r_value) && (!ulong_optimize))
Lev Walkin6ea088f2004-09-07 06:31:15 +0000144 emit_value_determination_code(arg, etype, r_value);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000145 if(r_size)
146 emit_size_determination_code(arg, etype);
147
Lev Walkin59004fa2004-08-20 13:37:01 +0000148 INDENT(-1);
149 REDIR(OT_CTABLES);
150 /* Emit FROM() tables */
Lev Walkin6938d042005-03-04 23:23:50 +0000151 alphabet_table_compiled =
152 (asn1c_emit_constraint_tables(arg, r_size?1:0) == 1);
Lev Walkin59004fa2004-08-20 13:37:01 +0000153 REDIR(OT_CODE);
154 INDENT(+1);
155
Lev Walkin84cd58e2004-08-19 13:29:46 +0000156 /*
Lev Walkin8bb57a22007-12-03 13:41:36 +0000157 * Optimization for unsigned longs.
158 */
Santiago Carot-Nemesio8da8e732011-04-22 13:21:33 +0200159 if(ulong_optimize) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000160 OUT("\n");
161 OUT("/* Constraint check succeeded */\n");
162 OUT("return 0;\n");
Bi-Ruei, Chiu64505ef2016-11-08 13:59:29 +0800163 goto end;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000164 }
165
166 /*
Lev Walkin729eb862006-09-21 01:50:13 +0000167 * Here is an if() {} else {} consrtaint checking code.
Lev Walkin84cd58e2004-08-19 13:29:46 +0000168 */
169 OUT("\n");
170 OUT("if(");
171 INDENT(+1);
172 if(r_size) {
173 if(got_something++) { OUT("\n"); OUT(" && "); }
174 OUT("(");
175 emit_range_comparison_code(arg, r_size, "size", 0, -1);
176 OUT(")");
177 }
178 if(r_value) {
179 if(got_something++) { OUT("\n"); OUT(" && "); }
180 OUT("(");
181 if(etype == ASN_BASIC_BOOLEAN)
182 emit_range_comparison_code(arg, r_value,
183 "value", 0, 1);
184 else
185 emit_range_comparison_code(arg, r_value,
186 "value", -1, -1);
187 OUT(")");
188 }
Lev Walkin6938d042005-03-04 23:23:50 +0000189 if(alphabet_table_compiled) {
Lev Walkin84cd58e2004-08-19 13:29:46 +0000190 if(got_something++) { OUT("\n"); OUT(" && "); }
Lev Walkin6938d042005-03-04 23:23:50 +0000191 OUT("!check_permitted_alphabet_%d(%s)",
192 arg->expr->_type_unique_index,
193 produce_st ? "st" : "sptr");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000194 }
195 if(!got_something) {
196 OUT("1 /* No applicable constraints whatsoever */");
Lev Walkin59004fa2004-08-20 13:37:01 +0000197 OUT(") {\n");
198 INDENT(-1);
johvikbd3dea92017-05-09 10:20:51 +0200199 if(produce_st) {
200 INDENTED(OUT("(void)st; /* Unused variable */\n"));
201 }
Lev Walkin59004fa2004-08-20 13:37:01 +0000202 INDENTED(OUT("/* Nothing is here. See below */\n"));
203 OUT("}\n");
204 OUT("\n");
Bi-Ruei, Chiu64505ef2016-11-08 13:59:29 +0800205 ret = 1;
206 goto end;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000207 }
208 INDENT(-1);
209 OUT(") {\n");
210 INDENT(+1);
Lev Walkin7ef83a42005-03-29 19:04:24 +0000211 switch(etype) {
212 case ASN_CONSTR_SEQUENCE_OF:
Lev Walkin7ef83a42005-03-29 19:04:24 +0000213 case ASN_CONSTR_SET_OF:
214 OUT("/* Perform validation of the inner elements */\n");
Lev Walkin14e75ed2017-09-29 23:15:02 -0700215 OUT("return td->encoding_constraints.general_constraints(td, sptr, ctfailcb, app_key);\n");
Lev Walkin7ef83a42005-03-29 19:04:24 +0000216 break;
217 default:
218 OUT("/* Constraint check succeeded */\n");
219 OUT("return 0;\n");
220 }
Lev Walkin84cd58e2004-08-19 13:29:46 +0000221 INDENT(-1);
222 OUT("} else {\n");
223 INDENT(+1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700224 OUT("ASN__CTFAIL(app_key, td, sptr,\n");
Lev Walkin16835b62004-08-22 13:47:59 +0000225 OUT("\t\"%%s: constraint failed (%%s:%%d)\",\n");
226 OUT("\ttd->name, __FILE__, __LINE__);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000227 OUT("return -1;\n");
228 INDENT(-1);
229 OUT("}\n");
230
Bi-Ruei, Chiu64505ef2016-11-08 13:59:29 +0800231end:
232 if (r_value) asn1constraint_range_free(r_value);
233 if (r_size) asn1constraint_range_free(r_size);
234
235 return ret;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000236}
237
Lev Walkin59004fa2004-08-20 13:37:01 +0000238static int
239asn1c_emit_constraint_tables(arg_t *arg, int got_size) {
Lev Walkin05363a72004-09-29 13:16:40 +0000240 asn1c_integer_t range_start;
241 asn1c_integer_t range_stop;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000242 asn1p_expr_type_e etype;
243 asn1cnst_range_t *range;
Lev Walkin59004fa2004-08-20 13:37:01 +0000244 asn1p_constraint_t *ct;
245 int utf8_full_alphabet_check = 0;
246 int max_table_size = 256;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000247 int table[256];
248 int use_table;
249
Lev Walkin59004fa2004-08-20 13:37:01 +0000250 ct = arg->expr->combined_constraints;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000251 if(!ct) return 0;
252
253 etype = _find_terminal_type(arg);
254
Lev Walkina28cbb92017-07-31 20:20:17 -0700255 range = asn1constraint_compute_constraint_range(arg->expr->Identifier, etype, ct, ACT_CT_FROM, 0,0,0);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000256 if(!range) return 0;
257
Lev Walkinbe717ec2004-08-25 02:03:59 +0000258 if(range->incompatible
Lev Walkin84cd58e2004-08-19 13:29:46 +0000259 || range->empty_constraint) {
260 asn1constraint_range_free(range);
261 return 0;
262 }
263
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000264 if(range->left.type == ARE_MIN
265 && range->right.type == ARE_MAX) {
266 /*
267 * The permitted alphabet constraint checker code guarantees
268 * that either both bounds (left/right) are present, or
269 * they're absent simultaneously. Thus, this assertion
270 * legitimately holds true.
271 */
272 assert(range->el_count == 0);
273 /* The full range is specified. Ignore it. */
Bi-Ruei, Chiu3dcf05b2017-05-04 21:45:05 +0800274 asn1constraint_range_free(range);
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000275 return 0;
276 }
277
Lev Walkin84cd58e2004-08-19 13:29:46 +0000278 range_start = range->left.value;
279 range_stop = range->right.value;
280 assert(range->left.type == ARE_VALUE);
281 assert(range->right.type == ARE_VALUE);
282 assert(range_start <= range_stop);
283
284 range_start = 0; /* Force old behavior */
285
286 /*
287 * Check if we need a test table to check the alphabet.
288 */
289 use_table = 1;
Lev Walkin59004fa2004-08-20 13:37:01 +0000290 if(range->el_count == 0) {
291 /*
292 * It's better to have a short if() check
Lev Walkin729eb862006-09-21 01:50:13 +0000293 * than waste 1k of table space
Lev Walkin59004fa2004-08-20 13:37:01 +0000294 */
295 use_table = 0;
296 }
Lev Walkin84cd58e2004-08-19 13:29:46 +0000297 if((range_stop - range_start) > 255)
298 use_table = 0;
Lev Walkin59004fa2004-08-20 13:37:01 +0000299 if(etype == ASN_STRING_UTF8String) {
300 if(range_stop >= 0x80)
301 use_table = 0;
302 else
303 max_table_size = 128;
304 }
Lev Walkin84cd58e2004-08-19 13:29:46 +0000305
Lev Walkin84cd58e2004-08-19 13:29:46 +0000306 if(use_table) {
Lev Walkin729eb862006-09-21 01:50:13 +0000307 int cardinal = 0;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000308 int i, n = 0;
309 int untl;
310 memset(table, 0, sizeof(table));
311 for(i = -1; i < range->el_count; i++) {
312 asn1cnst_range_t *r;
Lev Walkin05363a72004-09-29 13:16:40 +0000313 asn1c_integer_t v;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000314 if(i == -1) {
315 if(range->el_count) continue;
316 r = range;
317 } else {
318 r = range->elements[i];
319 }
320 for(v = r->left.value; v <= r->right.value; v++) {
321 assert((v - range_start) >= 0);
Lev Walkin59004fa2004-08-20 13:37:01 +0000322 assert((v - range_start) < max_table_size);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000323 table[v - range_start] = ++n;
324 }
325 }
326
Lev Walkin84cd58e2004-08-19 13:29:46 +0000327 untl = (range_stop - range_start) + 1;
328 untl += (untl % 16)?16 - (untl % 16):0;
Wim Lewisa73ae672014-07-22 19:55:30 -0700329 OUT("static const int permitted_alphabet_table_%d[%d] = {\n",
Lev Walkin6938d042005-03-04 23:23:50 +0000330 arg->expr->_type_unique_index, max_table_size);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000331 for(n = 0; n < untl; n++) {
Lev Walkin729eb862006-09-21 01:50:13 +0000332 cardinal += table[n] ? 1 : 0;
333 OUT("%2d,", table[n]);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000334 if(!((n+1) % 16)) {
335 int c;
Lev Walkin729eb862006-09-21 01:50:13 +0000336 if(!n || (n-15) + range_start >= 0x80) {
Lev Walkin84cd58e2004-08-19 13:29:46 +0000337 OUT("\n");
338 continue;
339 }
340 OUT("\t/* ");
341 for(c = n - 15; c <= n; c++) {
342 if(table[c]) {
343 int a = c + range_start;
344 if(a > 0x20 && a < 0x80)
345 OUT("%c", a);
346 else
347 OUT(".");
348 } else {
349 OUT(" ");
350 }
351 }
352 OUT(" */");
353 OUT("\n");
354 }
355 }
356 OUT("};\n");
Lev Walkin729eb862006-09-21 01:50:13 +0000357
Lev Walkin725883b2006-10-09 12:07:58 +0000358 if((arg->flags & A1C_GEN_PER)
359 && (etype & ASN_STRING_KM_MASK)) {
Lev Walkin729eb862006-09-21 01:50:13 +0000360 int c;
Wim Lewisa73ae672014-07-22 19:55:30 -0700361 OUT("static const int permitted_alphabet_code2value_%d[%d] = {\n",
Lev Walkin729eb862006-09-21 01:50:13 +0000362 arg->expr->_type_unique_index, cardinal);
363 for(n = c = 0; c < max_table_size; c++) {
364 if(table[c]) {
365 OUT("%d,", c);
366 if(!((++n) % 16)) OUT("\n");
367 }
368 }
369 OUT("};\n");
370 OUT("\n");
Lev Walkin725883b2006-10-09 12:07:58 +0000371 DEBUG("code2value map gen for %s", arg->expr->Identifier);
372 arg->expr->_mark |= TM_PERFROMCT;
Lev Walkin729eb862006-09-21 01:50:13 +0000373 }
374
Lev Walkin84cd58e2004-08-19 13:29:46 +0000375 OUT("\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000376 } else if(etype == ASN_STRING_UTF8String) {
377 /*
378 * UTF8String type is a special case in many respects.
379 */
Lev Walkin59004fa2004-08-20 13:37:01 +0000380 if(got_size) {
381 /*
382 * Size has been already determined.
383 * The UTF8String length checker also checks
384 * for the syntax validity, so we don't have
385 * to repeat this process twice.
386 */
Lev Walkin59004fa2004-08-20 13:37:01 +0000387 asn1constraint_range_free(range);
388 return 0;
389 } else {
390 utf8_full_alphabet_check = 1;
391 }
392 } else {
393 /*
394 * This permitted alphabet check will be
395 * expressed using conditional statements
396 * instead of table lookups. Table would be
397 * to large or otherwise inappropriate (too sparse?).
398 */
Lev Walkin84cd58e2004-08-19 13:29:46 +0000399 }
400
401 OUT("static int check_permitted_alphabet_%d(const void *sptr) {\n",
Lev Walkin6938d042005-03-04 23:23:50 +0000402 arg->expr->_type_unique_index);
Lev Walkin59004fa2004-08-20 13:37:01 +0000403 INDENT(+1);
404 if(utf8_full_alphabet_check) {
Lev Walkin29c41682004-10-02 16:44:55 +0000405 OUT("if(UTF8String_length((const UTF8String_t *)sptr) < 0)\n");
406 OUT("\treturn -1; /* Alphabet (sic!) test failed. */\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000407 OUT("\n");
408 } else {
Lev Walkin84cd58e2004-08-19 13:29:46 +0000409 if(use_table) {
Wim Lewisa73ae672014-07-22 19:55:30 -0700410 OUT("const int *table = permitted_alphabet_table_%d;\n",
Lev Walkin6938d042005-03-04 23:23:50 +0000411 arg->expr->_type_unique_index);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000412 emit_alphabet_check_loop(arg, 0);
413 } else {
414 emit_alphabet_check_loop(arg, range);
415 }
Lev Walkin59004fa2004-08-20 13:37:01 +0000416 }
Lev Walkin775885e2004-08-22 12:47:03 +0000417 OUT("return 0;\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000418 INDENT(-1);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000419 OUT("}\n");
420 OUT("\n");
421
422 asn1constraint_range_free(range);
423
Lev Walkin6938d042005-03-04 23:23:50 +0000424 return 1;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000425}
426
427static int
428emit_alphabet_check_loop(arg_t *arg, asn1cnst_range_t *range) {
Lev Walkin05363a72004-09-29 13:16:40 +0000429 asn1c_integer_t natural_stop;
Lev Walkin634a3b82004-08-22 03:30:05 +0000430 asn1p_expr_t *terminal;
Lev Walkincf3f6eb2017-08-23 04:34:15 -0700431 const char *tname;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000432
Lev Walkinc0e03b92017-08-22 01:48:23 -0700433 terminal = asn1f_find_terminal_type_ex(arg->asn, arg->ns, arg->expr);
Lev Walkin634a3b82004-08-22 03:30:05 +0000434 if(terminal) {
435 OUT("/* The underlying type is %s */\n",
436 ASN_EXPR_TYPE2STR(terminal->expr_type));
437 } else {
438 terminal = arg->expr;
439 }
Lev Walkince31cdb2005-02-15 03:37:42 +0000440 tname = asn1c_type_name(arg, terminal, TNF_SAFE);
441 OUT("const %s_t *st = (const %s_t *)sptr;\n", tname, tname);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000442
Lev Walkin634a3b82004-08-22 03:30:05 +0000443 switch(terminal->expr_type) {
Lev Walkin84cd58e2004-08-19 13:29:46 +0000444 case ASN_STRING_UTF8String:
Lev Walkin02b137d2004-08-21 07:34:58 +0000445 OUT("const uint8_t *ch = st->buf;\n");
446 OUT("const uint8_t *end = ch + st->size;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000447 OUT("\n");
448 OUT("for(; ch < end; ch++) {\n");
449 INDENT(+1);
450 OUT("uint8_t cv = *ch;\n");
Lev Walkin775885e2004-08-22 12:47:03 +0000451 if(!range) OUT("if(cv >= 0x80) return -1;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000452 natural_stop = 0xffffffffUL;
453 break;
454 case ASN_STRING_UniversalString:
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000455 OUT("const uint8_t *ch = st->buf;\n");
456 OUT("const uint8_t *end = ch + st->size;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000457 OUT("\n");
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000458 OUT("if(st->size %% 4) return -1; /* (size%%4)! */\n");
459 OUT("for(; ch < end; ch += 4) {\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000460 INDENT(+1);
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000461 OUT("uint32_t cv = (ch[0] << 24)\n");
462 OUT("\t\t| (ch[1] << 16)\n");
463 OUT("\t\t| (ch[2] << 8)\n");
464 OUT("\t\t| ch[3];\n");
Lev Walkin775885e2004-08-22 12:47:03 +0000465 if(!range) OUT("if(cv > 255) return -1;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000466 natural_stop = 0xffffffffUL;
467 break;
468 case ASN_STRING_BMPString:
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000469 OUT("const uint8_t *ch = st->buf;\n");
470 OUT("const uint8_t *end = ch + st->size;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000471 OUT("\n");
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000472 OUT("if(st->size %% 2) return -1; /* (size%%2)! */\n");
473 OUT("for(; ch < end; ch += 2) {\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000474 INDENT(+1);
Lev Walkinfd71d1e2004-09-06 08:07:19 +0000475 OUT("uint16_t cv = (ch[0] << 8)\n");
476 OUT("\t\t| ch[1];\n");
Lev Walkin775885e2004-08-22 12:47:03 +0000477 if(!range) OUT("if(cv > 255) return -1;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000478 natural_stop = 0xffff;
479 break;
480 case ASN_BASIC_OCTET_STRING:
481 default:
Lev Walkin02b137d2004-08-21 07:34:58 +0000482 OUT("const uint8_t *ch = st->buf;\n");
483 OUT("const uint8_t *end = ch + st->size;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000484 OUT("\n");
485 OUT("for(; ch < end; ch++) {\n");
486 INDENT(+1);
487 OUT("uint8_t cv = *ch;\n");
488 natural_stop = 0xff;
489 break;
490 }
491
492 if(range) {
493 OUT("if(!(");
494 emit_range_comparison_code(arg, range, "cv", 0, natural_stop);
Lev Walkin775885e2004-08-22 12:47:03 +0000495 OUT(")) return -1;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000496 } else {
Lev Walkin775885e2004-08-22 12:47:03 +0000497 OUT("if(!table[cv]) return -1;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000498 }
499
500 INDENT(-1);
501 OUT("}\n");
502
503 return 0;
504}
505
506static int
Lev Walkin05363a72004-09-29 13:16:40 +0000507emit_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 +0000508 int ignore_left;
509 int ignore_right;
Lev Walkin59004fa2004-08-20 13:37:01 +0000510 int generated_something = 0;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000511 int i;
512
513 for(i = -1; i < range->el_count; i++) {
514 asn1cnst_range_t *r;
515 if(i == -1) {
516 if(range->el_count) continue;
517 r = range;
518 } else {
519 if(i) OUT(" || ");
520 r = range->elements[i];
521 }
522
523 if(r != range) OUT("(");
524
525 ignore_left = (r->left.type == ARE_MIN)
526 || (natural_start != -1
527 && r->left.value <= natural_start);
528 ignore_right = (r->right.type == ARE_MAX)
529 || (natural_stop != -1
530 && r->right.value >= natural_stop);
531 if(ignore_left && ignore_right) {
532 OUT("1 /* Constraint matches natural range of %s */",
533 varname);
534 continue;
535 }
536
537 if(ignore_left) {
Lev Walkin63b41262007-11-06 01:48:46 +0000538 OUT("%s <= ", varname);
539 OINT(r->right.value);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000540 } else if(ignore_right) {
Lev Walkin63b41262007-11-06 01:48:46 +0000541 OUT("%s >= ", varname);
542 OINT(r->left.value);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000543 } else if(r->left.value == r->right.value) {
Lev Walkin63b41262007-11-06 01:48:46 +0000544 OUT("%s == ", varname);
545 OINT(r->right.value);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000546 } else {
Lev Walkin63b41262007-11-06 01:48:46 +0000547 OUT("%s >= ", varname);
548 OINT(r->left.value);
549 OUT(" && ");
550 OUT("%s <= ", varname);
551 OINT(r->right.value);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000552 }
553 if(r != range) OUT(")");
Lev Walkin59004fa2004-08-20 13:37:01 +0000554 generated_something = 1;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000555 }
556
Lev Walkin59004fa2004-08-20 13:37:01 +0000557 return generated_something;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000558}
559
560static int
561emit_size_determination_code(arg_t *arg, asn1p_expr_type_e etype) {
562
563 switch(etype) {
564 case ASN_BASIC_BIT_STRING:
565 OUT("if(st->size > 0) {\n");
566 OUT("\t/* Size in bits */\n");
Lev Walkindcf1e352006-09-08 19:34:22 +0000567 OUT("\tsize = 8 * st->size - (st->bits_unused & 0x07);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000568 OUT("} else {\n");
569 OUT("\tsize = 0;\n");
570 OUT("}\n");
571 break;
572 case ASN_STRING_UniversalString:
573 OUT("size = st->size >> 2;\t/* 4 byte per character */\n");
574 break;
575 case ASN_STRING_BMPString:
576 OUT("size = st->size >> 1;\t/* 2 byte per character */\n");
577 break;
578 case ASN_STRING_UTF8String:
Lev Walkin29c41682004-10-02 16:44:55 +0000579 OUT("size = UTF8String_length(st);\n");
580 OUT("if((ssize_t)size < 0) {\n");
Lev Walkin7c1dc052016-03-14 03:08:15 -0700581 OUT("\tASN__CTFAIL(app_key, td, sptr,\n");
Lev Walkin29c41682004-10-02 16:44:55 +0000582 OUT("\t\t\"%%s: UTF-8: broken encoding (%%s:%%d)\",\n");
583 OUT("\t\ttd->name, __FILE__, __LINE__);\n");
584 OUT("\treturn -1;\n");
585 OUT("}\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000586 break;
587 case ASN_CONSTR_SET_OF:
588 case ASN_CONSTR_SEQUENCE_OF:
Lev Walkinf1a51232005-07-04 02:01:03 +0000589 OUT("/* Determine the number of elements */\n");
590 OUT("size = _A_C%s_FROM_VOID(sptr)->count;\n",
591 etype==ASN_CONSTR_SET_OF?"SET":"SEQUENCE");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000592 break;
Lev Walkin59004fa2004-08-20 13:37:01 +0000593 case ASN_BASIC_OCTET_STRING:
594 OUT("size = st->size;\n");
595 break;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000596 default:
Lev Walkin59004fa2004-08-20 13:37:01 +0000597 if(etype & ASN_STRING_MASK) {
Lev Walkin84cd58e2004-08-19 13:29:46 +0000598 OUT("size = st->size;\n");
599 break;
600 } else {
601 const char *type_name = ASN_EXPR_TYPE2STR(etype);
602 if(!type_name) type_name = arg->expr->Identifier;
603 WARNING("SizeConstraint is not defined for %s",
604 type_name);
605 OUT_NOINDENT("#warning SizeConstraint "
606 "is not defined for %s!\n", type_name);
607 OUT("size = st->size;\n");
608 }
609 return -1;
610 }
611
612 return 0;
613}
614
615static int
Lev Walkin6ea088f2004-09-07 06:31:15 +0000616emit_value_determination_code(arg_t *arg, asn1p_expr_type_e etype, asn1cnst_range_t *r_value) {
Lev Walkin84cd58e2004-08-19 13:29:46 +0000617
618 switch(etype) {
619 case ASN_BASIC_INTEGER:
620 case ASN_BASIC_ENUMERATED:
Lev Walkin8bb57a22007-12-03 13:41:36 +0000621 if(asn1c_type_fits_long(arg, arg->expr) == FL_FITS_UNSIGN) {
622 OUT("value = *(const unsigned long *)sptr;\n");
623 } else if(asn1c_type_fits_long(arg, arg->expr) != FL_NOTFIT) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000624 OUT("value = *(const long *)sptr;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000625 } else {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000626 /*
627 * In some cases we can explore our knowledge of
628 * underlying INTEGER_t->buf format.
629 */
Lev Walkin6ea088f2004-09-07 06:31:15 +0000630 if(r_value->el_count == 0
631 && (
632 /* Speed-up common case: (0..MAX) */
633 (r_value->left.type == ARE_VALUE
634 && r_value->left.value == 0
635 && r_value->right.type == ARE_MAX)
636 ||
637 /* Speed-up common case: (MIN..-1) */
638 (r_value->left.type == ARE_MIN
639 && r_value->right.type == ARE_VALUE
640 && r_value->right.value == -1)
641 )) {
642 OUT("/* Check if the sign bit is present */\n");
643 OUT("value = st->buf ? ((st->buf[0] & 0x80) ? -1 : 1) : 0;\n");
644 break;
645 }
646
Lev Walkine0f2a5b2017-08-30 20:02:29 -0700647 if(native_long_sign(arg, r_value) >= 0) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000648 /* Special case for treating unsigned longs */
649 OUT("if(asn_INTEGER2ulong(st, &value)) {\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000650 INDENT(+1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700651 OUT("ASN__CTFAIL(app_key, td, sptr,\n");
Lev Walkin16835b62004-08-22 13:47:59 +0000652 OUT("\t\"%%s: value too large (%%s:%%d)\",\n");
653 OUT("\ttd->name, __FILE__, __LINE__);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000654 OUT("return -1;\n");
655 INDENT(-1);
Lev Walkin8bb57a22007-12-03 13:41:36 +0000656 OUT("}\n");
657 } else {
658 OUT("if(asn_INTEGER2long(st, &value)) {\n");
659 INDENT(+1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700660 OUT("ASN__CTFAIL(app_key, td, sptr,\n");
Lev Walkin8bb57a22007-12-03 13:41:36 +0000661 OUT("\t\"%%s: value too large (%%s:%%d)\",\n");
662 OUT("\ttd->name, __FILE__, __LINE__);\n");
663 OUT("return -1;\n");
664 INDENT(-1);
665 OUT("}\n");
666 }
Lev Walkin84cd58e2004-08-19 13:29:46 +0000667 }
668 break;
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000669 case ASN_BASIC_REAL:
Lev Walkin2a744a72013-03-27 01:56:23 -0700670 if(arg->flags & A1C_USE_WIDE_TYPES) {
Lev Walkin05363a72004-09-29 13:16:40 +0000671 OUT("if(asn_REAL2double(st, &value)) {\n");
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000672 INDENT(+1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700673 OUT("ASN__CTFAIL(app_key, td, sptr,\n");
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000674 OUT("\t\"%%s: value too large (%%s:%%d)\",\n");
675 OUT("\ttd->name, __FILE__, __LINE__);\n");
676 OUT("return -1;\n");
677 INDENT(-1);
678 OUT("}\n");
Lev Walkin2a744a72013-03-27 01:56:23 -0700679 } else {
Lev Walkinb5450702017-10-04 02:52:57 -0700680 OUT("value = *(const %s *)sptr;\n", c_name(arg).type.c_name);
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000681 }
682 break;
Lev Walkin84cd58e2004-08-19 13:29:46 +0000683 case ASN_BASIC_BOOLEAN:
Lev Walkine0b56e02005-02-25 12:10:27 +0000684 OUT("value = (*(const long *)sptr) ? 1 : 0;\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +0000685 break;
686 default:
Lev Walkin02b137d2004-08-21 07:34:58 +0000687 WARNING("%s:%d: Value cannot be determined "
688 "for constraint check for %s",
Lev Walkinb85a8132005-08-18 13:38:19 +0000689 arg->expr->module->source_file_name,
Lev Walkin02b137d2004-08-21 07:34:58 +0000690 arg->expr->_lineno,
691 arg->expr->Identifier
692 );
693 OUT_NOINDENT(
694 "#error %s:%d: Value of %s cannot be determined\n",
Lev Walkinb85a8132005-08-18 13:38:19 +0000695 arg->expr->module->source_file_name,
Lev Walkin02b137d2004-08-21 07:34:58 +0000696 arg->expr->_lineno,
697 arg->expr->Identifier
698 );
Lev Walkin84cd58e2004-08-19 13:29:46 +0000699 break;
700 }
701
702 return 0;
703}
704
705static asn1p_expr_type_e
706_find_terminal_type(arg_t *arg) {
707 asn1p_expr_t *expr;
Lev Walkinc0e03b92017-08-22 01:48:23 -0700708 expr = asn1f_find_terminal_type_ex(arg->asn, arg->ns, arg->expr);
Lev Walkin84cd58e2004-08-19 13:29:46 +0000709 if(expr) return expr->expr_type;
710 return A1TC_INVALID;
711}
712
Lev Walkin8bb57a22007-12-03 13:41:36 +0000713static int
Lev Walkine0f2a5b2017-08-30 20:02:29 -0700714native_long_sign(arg_t *arg, asn1cnst_range_t *r) {
715 if(!(arg->flags & A1C_USE_WIDE_TYPES) && r->left.type == ARE_VALUE
716 && r->left.value >= 0 && r->left.value <= 2147483647
717 && r->right.type == ARE_MAX) {
718 return 1;
719 }
Lev Walkin8bb57a22007-12-03 13:41:36 +0000720 if(r->left.type == ARE_VALUE
721 && r->left.value >= 0
722 && r->right.type == ARE_VALUE
Lev Walkin5b63acf2014-01-14 01:48:37 -0800723 && r->right.value > 2147483647
Lev Walkin1b3a1352016-01-10 13:15:02 -0800724 && r->right.value <= (asn1c_integer_t)(4294967295UL)) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000725 if(r->el_count == 0
726 && r->left.value == 0
727 && r->right.value == 4294967295UL)
728 return 0;
729 else
730 return 1;
731 } else {
732 return -1;
733 }
734}