1 #include "asn1c_internal.h"
2 #include "asn1c_constraint.h"
3 #include "asn1c_misc.h"
5 #include "asn1c_naming.h"
7 #include <asn1fix_crange.h> /* constraint groker from libasn1fix */
8 #include <asn1fix_export.h> /* other exportables from libasn1fix */
10 static int asn1c_emit_constraint_tables(arg_t *arg, int got_size);
11 static int emit_alphabet_check_loop(arg_t *arg, asn1cnst_range_t *range);
12 static int emit_value_determination_code(arg_t *arg, asn1p_expr_type_e etype, asn1cnst_range_t *r_value);
13 static int emit_size_determination_code(arg_t *arg, asn1p_expr_type_e etype);
14 static asn1p_expr_type_e _find_terminal_type(arg_t *arg);
15 static abuf *emit_range_comparison_code(asn1cnst_range_t *range,
17 asn1c_integer_t natural_start,
18 asn1c_integer_t natural_stop);
19 static int native_long_sign(arg_t *arg, asn1cnst_range_t *r); /* -1, 0, 1 */
22 ulong_optimization(arg_t *arg, asn1p_expr_type_e etype, asn1cnst_range_t *r_size,
23 asn1cnst_range_t *r_value)
25 return (!r_size && r_value
26 && (etype == ASN_BASIC_INTEGER
27 || etype == ASN_BASIC_ENUMERATED)
28 && native_long_sign(arg, r_value) == 0);
32 asn1c_emit_constraint_checking_code(arg_t *arg) {
33 asn1cnst_range_t *r_size;
34 asn1cnst_range_t *r_value;
35 asn1p_expr_t *expr = arg->expr;
36 asn1p_expr_type_e etype;
37 asn1p_constraint_t *ct;
38 int alphabet_table_compiled;
40 int ulong_optimize = 0;
41 int value_unsigned = 0;
44 ct = expr->combined_constraints;
46 return 1; /* No additional constraints defined */
48 etype = _find_terminal_type(arg);
50 r_value=asn1constraint_compute_constraint_range(expr->Identifier, etype, ct, ACT_EL_RANGE,0,0,0);
51 r_size =asn1constraint_compute_constraint_range(expr->Identifier, etype, ct, ACT_CT_SIZE, 0,0,0);
53 if(r_value->incompatible
54 || r_value->empty_constraint
55 || (r_value->left.type == ARE_MIN
56 && r_value->right.type == ARE_MAX)
57 || (etype == ASN_BASIC_BOOLEAN
58 && r_value->left.value == 0
59 && r_value->right.value == 1)
61 asn1constraint_range_free(r_value);
66 if(r_size->incompatible
67 || r_size->empty_constraint
68 || (r_size->left.value == 0 /* or .type == MIN */
69 && r_size->right.type == ARE_MAX)
71 asn1constraint_range_free(r_size);
77 * Do we really need an "*st = sptr" pointer?
80 case ASN_BASIC_INTEGER:
81 case ASN_BASIC_ENUMERATED:
82 if(asn1c_type_fits_long(arg, arg->expr) == FL_NOTFIT)
86 if((arg->flags & A1C_USE_WIDE_TYPES)
87 && asn1c_REAL_fits(arg, arg->expr) == RL_NOTFIT)
90 case ASN_BASIC_BIT_STRING:
91 case ASN_BASIC_OCTET_STRING:
95 if(etype & ASN_STRING_MASK)
100 const char *tname = asn1c_type_name(arg, arg->expr, TNF_SAFE);
101 OUT("const %s_t *st = (const %s_t *)sptr;\n", tname, tname);
104 if(r_size || r_value) {
106 OUT("size_t size;\n");
110 case ASN_BASIC_INTEGER:
111 case ASN_BASIC_ENUMERATED:
112 if(native_long_sign(arg, r_value) >= 0) {
113 ulong_optimize = ulong_optimization(arg, etype, r_size, r_value);
114 if(!ulong_optimize) {
116 OUT("unsigned long value;\n");
119 OUT("long value;\n");
123 OUT("%s value;\n", c_name(arg).type.constrained_c_name);
125 case ASN_BASIC_BOOLEAN:
126 OUT("BOOLEAN_t value;\n");
136 * Protection against null input.
138 OUT("if(!sptr) {\n");
140 OUT("ASN__CTFAIL(app_key, td, sptr,\n");
141 OUT("\t\"%%s: value not given (%%s:%%d)\",\n");
142 OUT("\ttd->name, __FILE__, __LINE__);\n");
148 if((r_value) && (!ulong_optimize))
149 emit_value_determination_code(arg, etype, r_value);
151 emit_size_determination_code(arg, etype);
155 /* Emit FROM() tables */
156 alphabet_table_compiled =
157 (asn1c_emit_constraint_tables(arg, r_size?1:0) == 1);
162 * Optimization for unsigned longs.
166 OUT("/* Constraint check succeeded */\n");
172 * Here is an if() {} else {} consrtaint checking code.
174 int got_something = 0;
175 int value_unused = 0;
180 abuf *ab = emit_range_comparison_code(r_size, "size", 0, -1);
182 OUT("(%s)", ab->buffer);
188 if(got_something) { OUT("\n"); OUT(" && "); }
190 if(etype == ASN_BASIC_BOOLEAN)
191 ab = emit_range_comparison_code(r_value, "value", 0, 1);
193 ab = emit_range_comparison_code(r_value, "value",
194 value_unsigned ? 0 : -1, -1);
196 OUT("(%s)", ab->buffer);
203 if(alphabet_table_compiled) {
204 if(got_something) { OUT("\n"); OUT(" && "); }
205 OUT("!check_permitted_alphabet_%d(%s)",
206 arg->expr->_type_unique_index,
207 produce_st ? "st" : "sptr");
211 OUT("1 /* No applicable constraints whatsoever */");
215 INDENTED(OUT("(void)st; /* Unused variable */\n"));
218 INDENTED(OUT("(void)value; /* Unused variable */\n"));
220 INDENTED(OUT("/* Nothing is here. See below */\n"));
230 case ASN_CONSTR_SEQUENCE_OF:
231 case ASN_CONSTR_SET_OF:
232 OUT("/* Perform validation of the inner elements */\n");
233 OUT("return td->encoding_constraints.general_constraints(td, sptr, ctfailcb, app_key);\n");
236 OUT("/* Constraint check succeeded */\n");
242 OUT("ASN__CTFAIL(app_key, td, sptr,\n");
243 OUT("\t\"%%s: constraint failed (%%s:%%d)\",\n");
244 OUT("\ttd->name, __FILE__, __LINE__);\n");
250 if (r_value) asn1constraint_range_free(r_value);
251 if (r_size) asn1constraint_range_free(r_size);
257 asn1c_emit_constraint_tables(arg_t *arg, int got_size) {
258 asn1c_integer_t range_start;
259 asn1c_integer_t range_stop;
260 asn1p_expr_type_e etype;
261 asn1cnst_range_t *range;
262 asn1p_constraint_t *ct;
263 int utf8_full_alphabet_check = 0;
264 int max_table_size = 256;
268 ct = arg->expr->combined_constraints;
271 etype = _find_terminal_type(arg);
273 range = asn1constraint_compute_constraint_range(arg->expr->Identifier, etype, ct, ACT_CT_FROM, 0,0,0);
276 if(range->incompatible
277 || range->empty_constraint) {
278 asn1constraint_range_free(range);
282 if(range->left.type == ARE_MIN
283 && range->right.type == ARE_MAX) {
285 * The permitted alphabet constraint checker code guarantees
286 * that either both bounds (left/right) are present, or
287 * they're absent simultaneously. Thus, this assertion
288 * legitimately holds true.
290 assert(range->el_count == 0);
291 /* The full range is specified. Ignore it. */
292 asn1constraint_range_free(range);
296 range_start = range->left.value;
297 range_stop = range->right.value;
298 assert(range->left.type == ARE_VALUE);
299 assert(range->right.type == ARE_VALUE);
300 assert(range_start <= range_stop);
302 range_start = 0; /* Force old behavior */
305 * Check if we need a test table to check the alphabet.
308 if(range->el_count == 0) {
310 * It's better to have a short if() check
311 * than waste 1k of table space
315 if((range_stop - range_start) > 255)
317 if(etype == ASN_STRING_UTF8String) {
318 if(range_stop >= 0x80)
321 max_table_size = 128;
328 memset(table, 0, sizeof(table));
329 for(i = -1; i < range->el_count; i++) {
333 if(range->el_count) continue;
336 r = range->elements[i];
338 for(v = r->left.value; v <= r->right.value; v++) {
339 assert((v - range_start) >= 0);
340 assert((v - range_start) < max_table_size);
341 table[v - range_start] = ++n;
345 untl = (range_stop - range_start) + 1;
346 untl += (untl % 16)?16 - (untl % 16):0;
347 OUT("static const int permitted_alphabet_table_%d[%d] = {\n",
348 arg->expr->_type_unique_index, max_table_size);
349 for(n = 0; n < untl; n++) {
350 cardinal += table[n] ? 1 : 0;
351 OUT("%2d,", table[n]);
354 if(!n || (n-15) + range_start >= 0x80) {
359 for(c = n - 15; c <= n; c++) {
361 int a = c + range_start;
362 if(a > 0x20 && a < 0x80)
376 if((arg->flags & A1C_GEN_PER)
377 && (etype & ASN_STRING_KM_MASK)) {
379 OUT("static const int permitted_alphabet_code2value_%d[%d] = {\n",
380 arg->expr->_type_unique_index, cardinal);
381 for(n = c = 0; c < max_table_size; c++) {
384 if(!((++n) % 16)) OUT("\n");
389 DEBUG("code2value map gen for %s", arg->expr->Identifier);
390 arg->expr->_mark |= TM_PERFROMCT;
394 } else if(etype == ASN_STRING_UTF8String) {
396 * UTF8String type is a special case in many respects.
400 * Size has been already determined.
401 * The UTF8String length checker also checks
402 * for the syntax validity, so we don't have
403 * to repeat this process twice.
405 asn1constraint_range_free(range);
408 utf8_full_alphabet_check = 1;
412 * This permitted alphabet check will be
413 * expressed using conditional statements
414 * instead of table lookups. Table would be
415 * to large or otherwise inappropriate (too sparse?).
419 OUT("static int check_permitted_alphabet_%d(const void *sptr) {\n",
420 arg->expr->_type_unique_index);
422 if(utf8_full_alphabet_check) {
423 OUT("if(UTF8String_length((const UTF8String_t *)sptr) < 0)\n");
424 OUT("\treturn -1; /* Alphabet (sic!) test failed. */\n");
428 OUT("const int *table = permitted_alphabet_table_%d;\n",
429 arg->expr->_type_unique_index);
430 emit_alphabet_check_loop(arg, 0);
432 emit_alphabet_check_loop(arg, range);
440 asn1constraint_range_free(range);
446 emit_alphabet_check_loop(arg_t *arg, asn1cnst_range_t *range) {
447 asn1c_integer_t natural_stop;
448 asn1p_expr_t *terminal;
451 terminal = asn1f_find_terminal_type_ex(arg->asn, arg->ns, arg->expr);
453 OUT("/* The underlying type is %s */\n",
454 ASN_EXPR_TYPE2STR(terminal->expr_type));
456 terminal = arg->expr;
458 tname = asn1c_type_name(arg, terminal, TNF_SAFE);
459 OUT("const %s_t *st = (const %s_t *)sptr;\n", tname, tname);
461 switch(terminal->expr_type) {
462 case ASN_STRING_UTF8String:
463 OUT("const uint8_t *ch = st->buf;\n");
464 OUT("const uint8_t *end = ch + st->size;\n");
466 OUT("for(; ch < end; ch++) {\n");
468 OUT("uint8_t cv = *ch;\n");
469 if(!range) OUT("if(cv >= 0x80) return -1;\n");
470 natural_stop = 0xffffffffUL;
472 case ASN_STRING_UniversalString:
473 OUT("const uint8_t *ch = st->buf;\n");
474 OUT("const uint8_t *end = ch + st->size;\n");
476 OUT("if(st->size %% 4) return -1; /* (size%%4)! */\n");
477 OUT("for(; ch < end; ch += 4) {\n");
479 OUT("uint32_t cv = (ch[0] << 24)\n");
480 OUT("\t\t| (ch[1] << 16)\n");
481 OUT("\t\t| (ch[2] << 8)\n");
482 OUT("\t\t| ch[3];\n");
483 if(!range) OUT("if(cv > 255) return -1;\n");
484 natural_stop = 0xffffffffUL;
486 case ASN_STRING_BMPString:
487 OUT("const uint8_t *ch = st->buf;\n");
488 OUT("const uint8_t *end = ch + st->size;\n");
490 OUT("if(st->size %% 2) return -1; /* (size%%2)! */\n");
491 OUT("for(; ch < end; ch += 2) {\n");
493 OUT("uint16_t cv = (ch[0] << 8)\n");
494 OUT("\t\t| ch[1];\n");
495 if(!range) OUT("if(cv > 255) return -1;\n");
496 natural_stop = 0xffff;
498 case ASN_BASIC_OCTET_STRING:
500 OUT("const uint8_t *ch = st->buf;\n");
501 OUT("const uint8_t *end = ch + st->size;\n");
503 OUT("for(; ch < end; ch++) {\n");
505 OUT("uint8_t cv = *ch;\n");
511 abuf *ab = emit_range_comparison_code(range, "cv", 0, natural_stop);
513 OUT("if(!(%s)) return -1;\n", ab->buffer);
515 OUT("(void)cv; /* Unused variable */\n");
518 OUT("if(!table[cv]) return -1;\n");
528 abuf_oint(abuf *ab, asn1c_integer_t v) {
529 if(v == (-2147483647L - 1)) {
530 abuf_printf(ab, "(-2147483647L - 1)");
532 abuf_printf(ab, "%s", asn1p_itoa(v));
537 emit_range_comparison_code(asn1cnst_range_t *range, const char *varname,
538 asn1c_integer_t natural_start,
539 asn1c_integer_t natural_stop) {
540 abuf *ab = abuf_new();
542 if(range->el_count == 0) {
544 (range->left.type == ARE_MIN)
545 || (natural_start != -1 && range->left.value <= natural_start);
547 (range->right.type == ARE_MAX)
548 || (natural_stop != -1 && range->right.value >= natural_stop);
550 if(ignore_left && ignore_right) {
551 /* Empty constraint comparison */
552 } else if(ignore_left) {
553 abuf_printf(ab, "%s <= ", varname);
554 abuf_oint(ab, range->right.value);
555 } else if(ignore_right) {
556 abuf_printf(ab, "%s >= ", varname);
557 abuf_oint(ab, range->left.value);
558 } else if(range->left.value == range->right.value) {
559 abuf_printf(ab, "%s == ", varname);
560 abuf_oint(ab, range->right.value);
562 abuf_printf(ab, "%s >= ", varname);
563 abuf_oint(ab, range->left.value);
564 abuf_printf(ab, " && ");
565 abuf_printf(ab, "%s <= ", varname);
566 abuf_oint(ab, range->right.value);
569 for(int i = 0; i < range->el_count; i++) {
570 asn1cnst_range_t *r = range->elements[i];
572 abuf *rec = emit_range_comparison_code(r, varname, natural_start,
576 abuf_str(ab, " || ");
582 /* Ignore this part */
592 emit_size_determination_code(arg_t *arg, asn1p_expr_type_e etype) {
595 case ASN_BASIC_BIT_STRING:
596 OUT("if(st->size > 0) {\n");
597 OUT("\t/* Size in bits */\n");
598 OUT("\tsize = 8 * st->size - (st->bits_unused & 0x07);\n");
600 OUT("\tsize = 0;\n");
603 case ASN_STRING_UniversalString:
604 OUT("size = st->size >> 2;\t/* 4 byte per character */\n");
606 case ASN_STRING_BMPString:
607 OUT("size = st->size >> 1;\t/* 2 byte per character */\n");
609 case ASN_STRING_UTF8String:
610 OUT("size = UTF8String_length(st);\n");
611 OUT("if((ssize_t)size < 0) {\n");
612 OUT("\tASN__CTFAIL(app_key, td, sptr,\n");
613 OUT("\t\t\"%%s: UTF-8: broken encoding (%%s:%%d)\",\n");
614 OUT("\t\ttd->name, __FILE__, __LINE__);\n");
615 OUT("\treturn -1;\n");
618 case ASN_CONSTR_SET_OF:
619 case ASN_CONSTR_SEQUENCE_OF:
620 OUT("/* Determine the number of elements */\n");
621 OUT("size = _A_C%s_FROM_VOID(sptr)->count;\n",
622 etype==ASN_CONSTR_SET_OF?"SET":"SEQUENCE");
624 case ASN_BASIC_OCTET_STRING:
625 OUT("size = st->size;\n");
628 if(etype & ASN_STRING_MASK) {
629 OUT("size = st->size;\n");
632 const char *type_name = ASN_EXPR_TYPE2STR(etype);
633 if(!type_name) type_name = arg->expr->Identifier;
634 WARNING("SizeConstraint is not defined for %s",
636 OUT_NOINDENT("#warning SizeConstraint "
637 "is not defined for %s!\n", type_name);
638 OUT("size = st->size;\n");
647 emit_value_determination_code(arg_t *arg, asn1p_expr_type_e etype, asn1cnst_range_t *r_value) {
650 case ASN_BASIC_INTEGER:
651 case ASN_BASIC_ENUMERATED:
652 if(asn1c_type_fits_long(arg, arg->expr) == FL_FITS_UNSIGN) {
653 OUT("value = *(const unsigned long *)sptr;\n");
654 } else if(asn1c_type_fits_long(arg, arg->expr) != FL_NOTFIT) {
655 OUT("value = *(const long *)sptr;\n");
658 * In some cases we can explore our knowledge of
659 * underlying INTEGER_t->buf format.
661 if(r_value->el_count == 0
663 /* Speed-up common case: (0..MAX) */
664 (r_value->left.type == ARE_VALUE
665 && r_value->left.value == 0
666 && r_value->right.type == ARE_MAX)
668 /* Speed-up common case: (MIN..-1) */
669 (r_value->left.type == ARE_MIN
670 && r_value->right.type == ARE_VALUE
671 && r_value->right.value == -1)
673 OUT("/* Check if the sign bit is present */\n");
674 OUT("value = st->buf ? ((st->buf[0] & 0x80) ? -1 : 1) : 0;\n");
678 if(native_long_sign(arg, r_value) >= 0) {
679 /* Special case for treating unsigned longs */
680 OUT("if(asn_INTEGER2ulong(st, &value)) {\n");
682 OUT("ASN__CTFAIL(app_key, td, sptr,\n");
683 OUT("\t\"%%s: value too large (%%s:%%d)\",\n");
684 OUT("\ttd->name, __FILE__, __LINE__);\n");
689 OUT("if(asn_INTEGER2long(st, &value)) {\n");
691 OUT("ASN__CTFAIL(app_key, td, sptr,\n");
692 OUT("\t\"%%s: value too large (%%s:%%d)\",\n");
693 OUT("\ttd->name, __FILE__, __LINE__);\n");
701 if(arg->flags & A1C_USE_WIDE_TYPES) {
702 OUT("if(asn_REAL2double(st, &value)) {\n");
704 OUT("ASN__CTFAIL(app_key, td, sptr,\n");
705 OUT("\t\"%%s: value too large (%%s:%%d)\",\n");
706 OUT("\ttd->name, __FILE__, __LINE__);\n");
711 OUT("value = *(const %s *)sptr;\n", c_name(arg).type.c_name);
714 case ASN_BASIC_BOOLEAN:
715 OUT("value = (*(const long *)sptr) ? 1 : 0;\n");
718 WARNING("%s:%d: Value cannot be determined "
719 "for constraint check for %s",
720 arg->expr->module->source_file_name,
722 arg->expr->Identifier
725 "#error %s:%d: Value of %s cannot be determined\n",
726 arg->expr->module->source_file_name,
728 arg->expr->Identifier
736 static asn1p_expr_type_e
737 _find_terminal_type(arg_t *arg) {
739 expr = asn1f_find_terminal_type_ex(arg->asn, arg->ns, arg->expr);
740 if(expr) return expr->expr_type;
745 native_long_sign(arg_t *arg, asn1cnst_range_t *r) {
746 if(!(arg->flags & A1C_USE_WIDE_TYPES) && r->left.type == ARE_VALUE
747 && r->left.value >= 0 && r->left.value <= 2147483647
748 && r->right.type == ARE_MAX) {
751 if(r->left.type == ARE_VALUE
752 && r->left.value >= 0
753 && r->right.type == ARE_VALUE
754 && r->right.value > 2147483647
755 && r->right.value <= (asn1c_integer_t)(4294967295UL)) {
757 && r->left.value == 0
758 && r->right.value == 4294967295UL)