Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 1 | #include "asn1c_internal.h" |
| 2 | #include "asn1c_constraint.h" |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 3 | #include "asn1c_misc.h" |
| 4 | #include "asn1c_out.h" |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 5 | #include "asn1c_naming.h" |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 6 | |
| 7 | #include <asn1fix_crange.h> /* constraint groker from libasn1fix */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 8 | #include <asn1fix_export.h> /* other exportables from libasn1fix */ |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 9 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 10 | static int asn1c_emit_constraint_tables(arg_t *arg, int got_size); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 11 | static int emit_alphabet_check_loop(arg_t *arg, asn1cnst_range_t *range); |
Lev Walkin | 6ea088f | 2004-09-07 06:31:15 +0000 | [diff] [blame] | 12 | static int emit_value_determination_code(arg_t *arg, asn1p_expr_type_e etype, asn1cnst_range_t *r_value); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 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); |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 15 | static 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 Walkin | e0f2a5b | 2017-08-30 20:02:29 -0700 | [diff] [blame] | 16 | static int native_long_sign(arg_t *arg, asn1cnst_range_t *r); /* -1, 0, 1 */ |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 17 | |
Santiago Carot-Nemesio | 8da8e73 | 2011-04-22 13:21:33 +0200 | [diff] [blame] | 18 | static int |
Lev Walkin | e0f2a5b | 2017-08-30 20:02:29 -0700 | [diff] [blame] | 19 | ulong_optimization(arg_t *arg, asn1p_expr_type_e etype, asn1cnst_range_t *r_size, |
Santiago Carot-Nemesio | 8da8e73 | 2011-04-22 13:21:33 +0200 | [diff] [blame] | 20 | asn1cnst_range_t *r_value) |
| 21 | { |
| 22 | return (!r_size && r_value |
| 23 | && (etype == ASN_BASIC_INTEGER |
| 24 | || etype == ASN_BASIC_ENUMERATED) |
Lev Walkin | e0f2a5b | 2017-08-30 20:02:29 -0700 | [diff] [blame] | 25 | && native_long_sign(arg, r_value) == 0); |
Santiago Carot-Nemesio | 8da8e73 | 2011-04-22 13:21:33 +0200 | [diff] [blame] | 26 | } |
| 27 | |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 28 | int |
| 29 | asn1c_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 Walkin | 6938d04 | 2005-03-04 23:23:50 +0000 | [diff] [blame] | 36 | int alphabet_table_compiled; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 37 | int produce_st = 0; |
Santiago Carot-Nemesio | 8da8e73 | 2011-04-22 13:21:33 +0200 | [diff] [blame] | 38 | int ulong_optimize = 0; |
Bi-Ruei, Chiu | 64505ef | 2016-11-08 13:59:29 +0800 | [diff] [blame] | 39 | int ret = 0; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 40 | |
| 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 Walkin | a28cbb9 | 2017-07-31 20:20:17 -0700 | [diff] [blame] | 47 | 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 Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 49 | if(r_value) { |
Lev Walkin | be717ec | 2004-08-25 02:03:59 +0000 | [diff] [blame] | 50 | if(r_value->incompatible |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 51 | || 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 Walkin | be717ec | 2004-08-25 02:03:59 +0000 | [diff] [blame] | 63 | if(r_size->incompatible |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 64 | || 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 Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 73 | /* |
| 74 | * Do we really need an "*st = sptr" pointer? |
| 75 | */ |
| 76 | switch(etype) { |
| 77 | case ASN_BASIC_INTEGER: |
| 78 | case ASN_BASIC_ENUMERATED: |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 79 | if(asn1c_type_fits_long(arg, arg->expr) == FL_NOTFIT) |
| 80 | produce_st = 1; |
| 81 | break; |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 82 | case ASN_BASIC_REAL: |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 83 | if((arg->flags & A1C_USE_WIDE_TYPES) |
| 84 | && asn1c_REAL_fits(arg, arg->expr) == RL_NOTFIT) |
| 85 | produce_st = 1; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 86 | break; |
Lev Walkin | 02b137d | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 87 | case ASN_BASIC_BIT_STRING: |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 88 | case ASN_BASIC_OCTET_STRING: |
| 89 | produce_st = 1; |
Lev Walkin | 3a4689a | 2006-11-24 11:20:27 +0000 | [diff] [blame] | 90 | break; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 91 | default: |
| 92 | if(etype & ASN_STRING_MASK) |
| 93 | produce_st = 1; |
| 94 | break; |
| 95 | } |
Lev Walkin | ce31cdb | 2005-02-15 03:37:42 +0000 | [diff] [blame] | 96 | if(produce_st) { |
Lev Walkin | cf3f6eb | 2017-08-23 04:34:15 -0700 | [diff] [blame] | 97 | const char *tname = asn1c_type_name(arg, arg->expr, TNF_SAFE); |
Lev Walkin | ce31cdb | 2005-02-15 03:37:42 +0000 | [diff] [blame] | 98 | OUT("const %s_t *st = (const %s_t *)sptr;\n", tname, tname); |
| 99 | } |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 100 | |
| 101 | if(r_size || r_value) { |
| 102 | if(r_size) { |
| 103 | OUT("size_t size;\n"); |
| 104 | } |
| 105 | if(r_value) |
| 106 | switch(etype) { |
| 107 | case ASN_BASIC_INTEGER: |
| 108 | case ASN_BASIC_ENUMERATED: |
Lev Walkin | e0f2a5b | 2017-08-30 20:02:29 -0700 | [diff] [blame] | 109 | if(native_long_sign(arg, r_value) >= 0) { |
| 110 | ulong_optimize = ulong_optimization(arg, etype, r_size, r_value); |
Santiago Carot-Nemesio | 8da8e73 | 2011-04-22 13:21:33 +0200 | [diff] [blame] | 111 | if(!ulong_optimize) { |
| 112 | OUT("unsigned long value;\n"); |
| 113 | } |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 114 | } else { |
| 115 | OUT("long value;\n"); |
| 116 | } |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 117 | break; |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 118 | case ASN_BASIC_REAL: |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 119 | OUT("%s value;\n", c_name(arg).type.constrained_c_name); |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 120 | break; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 121 | case ASN_BASIC_BOOLEAN: |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 122 | OUT("BOOLEAN_t value;\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 123 | break; |
| 124 | default: |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | OUT("\n"); |
| 130 | |
| 131 | /* |
| 132 | * Protection against null input. |
| 133 | */ |
| 134 | OUT("if(!sptr) {\n"); |
| 135 | INDENT(+1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 136 | OUT("ASN__CTFAIL(app_key, td, sptr,\n"); |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 137 | OUT("\t\"%%s: value not given (%%s:%%d)\",\n"); |
| 138 | OUT("\ttd->name, __FILE__, __LINE__);\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 139 | OUT("return -1;\n"); |
| 140 | INDENT(-1); |
| 141 | OUT("}\n"); |
| 142 | OUT("\n"); |
| 143 | |
Santiago Carot-Nemesio | 8da8e73 | 2011-04-22 13:21:33 +0200 | [diff] [blame] | 144 | if((r_value) && (!ulong_optimize)) |
Lev Walkin | 6ea088f | 2004-09-07 06:31:15 +0000 | [diff] [blame] | 145 | emit_value_determination_code(arg, etype, r_value); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 146 | if(r_size) |
| 147 | emit_size_determination_code(arg, etype); |
| 148 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 149 | INDENT(-1); |
| 150 | REDIR(OT_CTABLES); |
| 151 | /* Emit FROM() tables */ |
Lev Walkin | 6938d04 | 2005-03-04 23:23:50 +0000 | [diff] [blame] | 152 | alphabet_table_compiled = |
| 153 | (asn1c_emit_constraint_tables(arg, r_size?1:0) == 1); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 154 | REDIR(OT_CODE); |
| 155 | INDENT(+1); |
| 156 | |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 157 | /* |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 158 | * Optimization for unsigned longs. |
| 159 | */ |
Santiago Carot-Nemesio | 8da8e73 | 2011-04-22 13:21:33 +0200 | [diff] [blame] | 160 | if(ulong_optimize) { |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 161 | OUT("\n"); |
| 162 | OUT("/* Constraint check succeeded */\n"); |
| 163 | OUT("return 0;\n"); |
Bi-Ruei, Chiu | 64505ef | 2016-11-08 13:59:29 +0800 | [diff] [blame] | 164 | goto end; |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* |
Lev Walkin | 729eb86 | 2006-09-21 01:50:13 +0000 | [diff] [blame] | 168 | * Here is an if() {} else {} consrtaint checking code. |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 169 | */ |
| 170 | OUT("\n"); |
| 171 | OUT("if("); |
| 172 | INDENT(+1); |
| 173 | if(r_size) { |
| 174 | if(got_something++) { OUT("\n"); OUT(" && "); } |
| 175 | OUT("("); |
| 176 | emit_range_comparison_code(arg, r_size, "size", 0, -1); |
| 177 | OUT(")"); |
| 178 | } |
| 179 | if(r_value) { |
| 180 | if(got_something++) { OUT("\n"); OUT(" && "); } |
| 181 | OUT("("); |
| 182 | if(etype == ASN_BASIC_BOOLEAN) |
| 183 | emit_range_comparison_code(arg, r_value, |
| 184 | "value", 0, 1); |
| 185 | else |
| 186 | emit_range_comparison_code(arg, r_value, |
| 187 | "value", -1, -1); |
| 188 | OUT(")"); |
| 189 | } |
Lev Walkin | 6938d04 | 2005-03-04 23:23:50 +0000 | [diff] [blame] | 190 | if(alphabet_table_compiled) { |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 191 | if(got_something++) { OUT("\n"); OUT(" && "); } |
Lev Walkin | 6938d04 | 2005-03-04 23:23:50 +0000 | [diff] [blame] | 192 | OUT("!check_permitted_alphabet_%d(%s)", |
| 193 | arg->expr->_type_unique_index, |
| 194 | produce_st ? "st" : "sptr"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 195 | } |
| 196 | if(!got_something) { |
| 197 | OUT("1 /* No applicable constraints whatsoever */"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 198 | OUT(") {\n"); |
| 199 | INDENT(-1); |
johvik | bd3dea9 | 2017-05-09 10:20:51 +0200 | [diff] [blame] | 200 | if(produce_st) { |
| 201 | INDENTED(OUT("(void)st; /* Unused variable */\n")); |
| 202 | } |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 203 | INDENTED(OUT("/* Nothing is here. See below */\n")); |
| 204 | OUT("}\n"); |
| 205 | OUT("\n"); |
Bi-Ruei, Chiu | 64505ef | 2016-11-08 13:59:29 +0800 | [diff] [blame] | 206 | ret = 1; |
| 207 | goto end; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 208 | } |
| 209 | INDENT(-1); |
| 210 | OUT(") {\n"); |
| 211 | INDENT(+1); |
Lev Walkin | 7ef83a4 | 2005-03-29 19:04:24 +0000 | [diff] [blame] | 212 | switch(etype) { |
| 213 | case ASN_CONSTR_SEQUENCE_OF: |
Lev Walkin | 7ef83a4 | 2005-03-29 19:04:24 +0000 | [diff] [blame] | 214 | case ASN_CONSTR_SET_OF: |
| 215 | OUT("/* Perform validation of the inner elements */\n"); |
Lev Walkin | 14e75ed | 2017-09-29 23:15:02 -0700 | [diff] [blame] | 216 | OUT("return td->encoding_constraints.general_constraints(td, sptr, ctfailcb, app_key);\n"); |
Lev Walkin | 7ef83a4 | 2005-03-29 19:04:24 +0000 | [diff] [blame] | 217 | break; |
| 218 | default: |
| 219 | OUT("/* Constraint check succeeded */\n"); |
| 220 | OUT("return 0;\n"); |
| 221 | } |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 222 | INDENT(-1); |
| 223 | OUT("} else {\n"); |
| 224 | INDENT(+1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 225 | OUT("ASN__CTFAIL(app_key, td, sptr,\n"); |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 226 | OUT("\t\"%%s: constraint failed (%%s:%%d)\",\n"); |
| 227 | OUT("\ttd->name, __FILE__, __LINE__);\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 228 | OUT("return -1;\n"); |
| 229 | INDENT(-1); |
| 230 | OUT("}\n"); |
| 231 | |
Bi-Ruei, Chiu | 64505ef | 2016-11-08 13:59:29 +0800 | [diff] [blame] | 232 | end: |
| 233 | if (r_value) asn1constraint_range_free(r_value); |
| 234 | if (r_size) asn1constraint_range_free(r_size); |
| 235 | |
| 236 | return ret; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 239 | static int |
| 240 | asn1c_emit_constraint_tables(arg_t *arg, int got_size) { |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 241 | asn1c_integer_t range_start; |
| 242 | asn1c_integer_t range_stop; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 243 | asn1p_expr_type_e etype; |
| 244 | asn1cnst_range_t *range; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 245 | asn1p_constraint_t *ct; |
| 246 | int utf8_full_alphabet_check = 0; |
| 247 | int max_table_size = 256; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 248 | int table[256]; |
| 249 | int use_table; |
| 250 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 251 | ct = arg->expr->combined_constraints; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 252 | if(!ct) return 0; |
| 253 | |
| 254 | etype = _find_terminal_type(arg); |
| 255 | |
Lev Walkin | a28cbb9 | 2017-07-31 20:20:17 -0700 | [diff] [blame] | 256 | range = asn1constraint_compute_constraint_range(arg->expr->Identifier, etype, ct, ACT_CT_FROM, 0,0,0); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 257 | if(!range) return 0; |
| 258 | |
Lev Walkin | be717ec | 2004-08-25 02:03:59 +0000 | [diff] [blame] | 259 | if(range->incompatible |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 260 | || range->empty_constraint) { |
| 261 | asn1constraint_range_free(range); |
| 262 | return 0; |
| 263 | } |
| 264 | |
Lev Walkin | fd71d1e | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 265 | if(range->left.type == ARE_MIN |
| 266 | && range->right.type == ARE_MAX) { |
| 267 | /* |
| 268 | * The permitted alphabet constraint checker code guarantees |
| 269 | * that either both bounds (left/right) are present, or |
| 270 | * they're absent simultaneously. Thus, this assertion |
| 271 | * legitimately holds true. |
| 272 | */ |
| 273 | assert(range->el_count == 0); |
| 274 | /* The full range is specified. Ignore it. */ |
Bi-Ruei, Chiu | 3dcf05b | 2017-05-04 21:45:05 +0800 | [diff] [blame] | 275 | asn1constraint_range_free(range); |
Lev Walkin | fd71d1e | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 279 | range_start = range->left.value; |
| 280 | range_stop = range->right.value; |
| 281 | assert(range->left.type == ARE_VALUE); |
| 282 | assert(range->right.type == ARE_VALUE); |
| 283 | assert(range_start <= range_stop); |
| 284 | |
| 285 | range_start = 0; /* Force old behavior */ |
| 286 | |
| 287 | /* |
| 288 | * Check if we need a test table to check the alphabet. |
| 289 | */ |
| 290 | use_table = 1; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 291 | if(range->el_count == 0) { |
| 292 | /* |
| 293 | * It's better to have a short if() check |
Lev Walkin | 729eb86 | 2006-09-21 01:50:13 +0000 | [diff] [blame] | 294 | * than waste 1k of table space |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 295 | */ |
| 296 | use_table = 0; |
| 297 | } |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 298 | if((range_stop - range_start) > 255) |
| 299 | use_table = 0; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 300 | if(etype == ASN_STRING_UTF8String) { |
| 301 | if(range_stop >= 0x80) |
| 302 | use_table = 0; |
| 303 | else |
| 304 | max_table_size = 128; |
| 305 | } |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 306 | |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 307 | if(use_table) { |
Lev Walkin | 729eb86 | 2006-09-21 01:50:13 +0000 | [diff] [blame] | 308 | int cardinal = 0; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 309 | int i, n = 0; |
| 310 | int untl; |
| 311 | memset(table, 0, sizeof(table)); |
| 312 | for(i = -1; i < range->el_count; i++) { |
| 313 | asn1cnst_range_t *r; |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 314 | asn1c_integer_t v; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 315 | if(i == -1) { |
| 316 | if(range->el_count) continue; |
| 317 | r = range; |
| 318 | } else { |
| 319 | r = range->elements[i]; |
| 320 | } |
| 321 | for(v = r->left.value; v <= r->right.value; v++) { |
| 322 | assert((v - range_start) >= 0); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 323 | assert((v - range_start) < max_table_size); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 324 | table[v - range_start] = ++n; |
| 325 | } |
| 326 | } |
| 327 | |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 328 | untl = (range_stop - range_start) + 1; |
| 329 | untl += (untl % 16)?16 - (untl % 16):0; |
Wim Lewis | a73ae67 | 2014-07-22 19:55:30 -0700 | [diff] [blame] | 330 | OUT("static const int permitted_alphabet_table_%d[%d] = {\n", |
Lev Walkin | 6938d04 | 2005-03-04 23:23:50 +0000 | [diff] [blame] | 331 | arg->expr->_type_unique_index, max_table_size); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 332 | for(n = 0; n < untl; n++) { |
Lev Walkin | 729eb86 | 2006-09-21 01:50:13 +0000 | [diff] [blame] | 333 | cardinal += table[n] ? 1 : 0; |
| 334 | OUT("%2d,", table[n]); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 335 | if(!((n+1) % 16)) { |
| 336 | int c; |
Lev Walkin | 729eb86 | 2006-09-21 01:50:13 +0000 | [diff] [blame] | 337 | if(!n || (n-15) + range_start >= 0x80) { |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 338 | OUT("\n"); |
| 339 | continue; |
| 340 | } |
| 341 | OUT("\t/* "); |
| 342 | for(c = n - 15; c <= n; c++) { |
| 343 | if(table[c]) { |
| 344 | int a = c + range_start; |
| 345 | if(a > 0x20 && a < 0x80) |
| 346 | OUT("%c", a); |
| 347 | else |
| 348 | OUT("."); |
| 349 | } else { |
| 350 | OUT(" "); |
| 351 | } |
| 352 | } |
| 353 | OUT(" */"); |
| 354 | OUT("\n"); |
| 355 | } |
| 356 | } |
| 357 | OUT("};\n"); |
Lev Walkin | 729eb86 | 2006-09-21 01:50:13 +0000 | [diff] [blame] | 358 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 359 | if((arg->flags & A1C_GEN_PER) |
| 360 | && (etype & ASN_STRING_KM_MASK)) { |
Lev Walkin | 729eb86 | 2006-09-21 01:50:13 +0000 | [diff] [blame] | 361 | int c; |
Wim Lewis | a73ae67 | 2014-07-22 19:55:30 -0700 | [diff] [blame] | 362 | OUT("static const int permitted_alphabet_code2value_%d[%d] = {\n", |
Lev Walkin | 729eb86 | 2006-09-21 01:50:13 +0000 | [diff] [blame] | 363 | arg->expr->_type_unique_index, cardinal); |
| 364 | for(n = c = 0; c < max_table_size; c++) { |
| 365 | if(table[c]) { |
| 366 | OUT("%d,", c); |
| 367 | if(!((++n) % 16)) OUT("\n"); |
| 368 | } |
| 369 | } |
| 370 | OUT("};\n"); |
| 371 | OUT("\n"); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 372 | DEBUG("code2value map gen for %s", arg->expr->Identifier); |
| 373 | arg->expr->_mark |= TM_PERFROMCT; |
Lev Walkin | 729eb86 | 2006-09-21 01:50:13 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 376 | OUT("\n"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 377 | } else if(etype == ASN_STRING_UTF8String) { |
| 378 | /* |
| 379 | * UTF8String type is a special case in many respects. |
| 380 | */ |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 381 | if(got_size) { |
| 382 | /* |
| 383 | * Size has been already determined. |
| 384 | * The UTF8String length checker also checks |
| 385 | * for the syntax validity, so we don't have |
| 386 | * to repeat this process twice. |
| 387 | */ |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 388 | asn1constraint_range_free(range); |
| 389 | return 0; |
| 390 | } else { |
| 391 | utf8_full_alphabet_check = 1; |
| 392 | } |
| 393 | } else { |
| 394 | /* |
| 395 | * This permitted alphabet check will be |
| 396 | * expressed using conditional statements |
| 397 | * instead of table lookups. Table would be |
| 398 | * to large or otherwise inappropriate (too sparse?). |
| 399 | */ |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | OUT("static int check_permitted_alphabet_%d(const void *sptr) {\n", |
Lev Walkin | 6938d04 | 2005-03-04 23:23:50 +0000 | [diff] [blame] | 403 | arg->expr->_type_unique_index); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 404 | INDENT(+1); |
| 405 | if(utf8_full_alphabet_check) { |
Lev Walkin | 29c4168 | 2004-10-02 16:44:55 +0000 | [diff] [blame] | 406 | OUT("if(UTF8String_length((const UTF8String_t *)sptr) < 0)\n"); |
| 407 | OUT("\treturn -1; /* Alphabet (sic!) test failed. */\n"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 408 | OUT("\n"); |
| 409 | } else { |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 410 | if(use_table) { |
Wim Lewis | a73ae67 | 2014-07-22 19:55:30 -0700 | [diff] [blame] | 411 | OUT("const int *table = permitted_alphabet_table_%d;\n", |
Lev Walkin | 6938d04 | 2005-03-04 23:23:50 +0000 | [diff] [blame] | 412 | arg->expr->_type_unique_index); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 413 | emit_alphabet_check_loop(arg, 0); |
| 414 | } else { |
| 415 | emit_alphabet_check_loop(arg, range); |
| 416 | } |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 417 | } |
Lev Walkin | 775885e | 2004-08-22 12:47:03 +0000 | [diff] [blame] | 418 | OUT("return 0;\n"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 419 | INDENT(-1); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 420 | OUT("}\n"); |
| 421 | OUT("\n"); |
| 422 | |
| 423 | asn1constraint_range_free(range); |
| 424 | |
Lev Walkin | 6938d04 | 2005-03-04 23:23:50 +0000 | [diff] [blame] | 425 | return 1; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static int |
| 429 | emit_alphabet_check_loop(arg_t *arg, asn1cnst_range_t *range) { |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 430 | asn1c_integer_t natural_stop; |
Lev Walkin | 634a3b8 | 2004-08-22 03:30:05 +0000 | [diff] [blame] | 431 | asn1p_expr_t *terminal; |
Lev Walkin | cf3f6eb | 2017-08-23 04:34:15 -0700 | [diff] [blame] | 432 | const char *tname; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 433 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 434 | terminal = asn1f_find_terminal_type_ex(arg->asn, arg->ns, arg->expr); |
Lev Walkin | 634a3b8 | 2004-08-22 03:30:05 +0000 | [diff] [blame] | 435 | if(terminal) { |
| 436 | OUT("/* The underlying type is %s */\n", |
| 437 | ASN_EXPR_TYPE2STR(terminal->expr_type)); |
| 438 | } else { |
| 439 | terminal = arg->expr; |
| 440 | } |
Lev Walkin | ce31cdb | 2005-02-15 03:37:42 +0000 | [diff] [blame] | 441 | tname = asn1c_type_name(arg, terminal, TNF_SAFE); |
| 442 | OUT("const %s_t *st = (const %s_t *)sptr;\n", tname, tname); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 443 | |
Lev Walkin | 634a3b8 | 2004-08-22 03:30:05 +0000 | [diff] [blame] | 444 | switch(terminal->expr_type) { |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 445 | case ASN_STRING_UTF8String: |
Lev Walkin | 02b137d | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 446 | OUT("const uint8_t *ch = st->buf;\n"); |
| 447 | OUT("const uint8_t *end = ch + st->size;\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 448 | OUT("\n"); |
| 449 | OUT("for(; ch < end; ch++) {\n"); |
| 450 | INDENT(+1); |
| 451 | OUT("uint8_t cv = *ch;\n"); |
Lev Walkin | 775885e | 2004-08-22 12:47:03 +0000 | [diff] [blame] | 452 | if(!range) OUT("if(cv >= 0x80) return -1;\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 453 | natural_stop = 0xffffffffUL; |
| 454 | break; |
| 455 | case ASN_STRING_UniversalString: |
Lev Walkin | fd71d1e | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 456 | OUT("const uint8_t *ch = st->buf;\n"); |
| 457 | OUT("const uint8_t *end = ch + st->size;\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 458 | OUT("\n"); |
Lev Walkin | fd71d1e | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 459 | OUT("if(st->size %% 4) return -1; /* (size%%4)! */\n"); |
| 460 | OUT("for(; ch < end; ch += 4) {\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 461 | INDENT(+1); |
Lev Walkin | fd71d1e | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 462 | OUT("uint32_t cv = (ch[0] << 24)\n"); |
| 463 | OUT("\t\t| (ch[1] << 16)\n"); |
| 464 | OUT("\t\t| (ch[2] << 8)\n"); |
| 465 | OUT("\t\t| ch[3];\n"); |
Lev Walkin | 775885e | 2004-08-22 12:47:03 +0000 | [diff] [blame] | 466 | if(!range) OUT("if(cv > 255) return -1;\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 467 | natural_stop = 0xffffffffUL; |
| 468 | break; |
| 469 | case ASN_STRING_BMPString: |
Lev Walkin | fd71d1e | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 470 | OUT("const uint8_t *ch = st->buf;\n"); |
| 471 | OUT("const uint8_t *end = ch + st->size;\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 472 | OUT("\n"); |
Lev Walkin | fd71d1e | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 473 | OUT("if(st->size %% 2) return -1; /* (size%%2)! */\n"); |
| 474 | OUT("for(; ch < end; ch += 2) {\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 475 | INDENT(+1); |
Lev Walkin | fd71d1e | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 476 | OUT("uint16_t cv = (ch[0] << 8)\n"); |
| 477 | OUT("\t\t| ch[1];\n"); |
Lev Walkin | 775885e | 2004-08-22 12:47:03 +0000 | [diff] [blame] | 478 | if(!range) OUT("if(cv > 255) return -1;\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 479 | natural_stop = 0xffff; |
| 480 | break; |
| 481 | case ASN_BASIC_OCTET_STRING: |
| 482 | default: |
Lev Walkin | 02b137d | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 483 | OUT("const uint8_t *ch = st->buf;\n"); |
| 484 | OUT("const uint8_t *end = ch + st->size;\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 485 | OUT("\n"); |
| 486 | OUT("for(; ch < end; ch++) {\n"); |
| 487 | INDENT(+1); |
| 488 | OUT("uint8_t cv = *ch;\n"); |
| 489 | natural_stop = 0xff; |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | if(range) { |
| 494 | OUT("if(!("); |
| 495 | emit_range_comparison_code(arg, range, "cv", 0, natural_stop); |
Lev Walkin | 775885e | 2004-08-22 12:47:03 +0000 | [diff] [blame] | 496 | OUT(")) return -1;\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 497 | } else { |
Lev Walkin | 775885e | 2004-08-22 12:47:03 +0000 | [diff] [blame] | 498 | OUT("if(!table[cv]) return -1;\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | INDENT(-1); |
| 502 | OUT("}\n"); |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 508 | 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 Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 509 | int ignore_left; |
| 510 | int ignore_right; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 511 | int generated_something = 0; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 512 | int i; |
| 513 | |
| 514 | for(i = -1; i < range->el_count; i++) { |
| 515 | asn1cnst_range_t *r; |
| 516 | if(i == -1) { |
| 517 | if(range->el_count) continue; |
| 518 | r = range; |
| 519 | } else { |
| 520 | if(i) OUT(" || "); |
| 521 | r = range->elements[i]; |
| 522 | } |
| 523 | |
| 524 | if(r != range) OUT("("); |
| 525 | |
| 526 | ignore_left = (r->left.type == ARE_MIN) |
| 527 | || (natural_start != -1 |
| 528 | && r->left.value <= natural_start); |
| 529 | ignore_right = (r->right.type == ARE_MAX) |
| 530 | || (natural_stop != -1 |
| 531 | && r->right.value >= natural_stop); |
| 532 | if(ignore_left && ignore_right) { |
| 533 | OUT("1 /* Constraint matches natural range of %s */", |
| 534 | varname); |
| 535 | continue; |
| 536 | } |
| 537 | |
| 538 | if(ignore_left) { |
Lev Walkin | 63b4126 | 2007-11-06 01:48:46 +0000 | [diff] [blame] | 539 | OUT("%s <= ", varname); |
| 540 | OINT(r->right.value); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 541 | } else if(ignore_right) { |
Lev Walkin | 63b4126 | 2007-11-06 01:48:46 +0000 | [diff] [blame] | 542 | OUT("%s >= ", varname); |
| 543 | OINT(r->left.value); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 544 | } else if(r->left.value == r->right.value) { |
Lev Walkin | 63b4126 | 2007-11-06 01:48:46 +0000 | [diff] [blame] | 545 | OUT("%s == ", varname); |
| 546 | OINT(r->right.value); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 547 | } else { |
Lev Walkin | 63b4126 | 2007-11-06 01:48:46 +0000 | [diff] [blame] | 548 | OUT("%s >= ", varname); |
| 549 | OINT(r->left.value); |
| 550 | OUT(" && "); |
| 551 | OUT("%s <= ", varname); |
| 552 | OINT(r->right.value); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 553 | } |
| 554 | if(r != range) OUT(")"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 555 | generated_something = 1; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 558 | return generated_something; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static int |
| 562 | emit_size_determination_code(arg_t *arg, asn1p_expr_type_e etype) { |
| 563 | |
| 564 | switch(etype) { |
| 565 | case ASN_BASIC_BIT_STRING: |
| 566 | OUT("if(st->size > 0) {\n"); |
| 567 | OUT("\t/* Size in bits */\n"); |
Lev Walkin | dcf1e35 | 2006-09-08 19:34:22 +0000 | [diff] [blame] | 568 | OUT("\tsize = 8 * st->size - (st->bits_unused & 0x07);\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 569 | OUT("} else {\n"); |
| 570 | OUT("\tsize = 0;\n"); |
| 571 | OUT("}\n"); |
| 572 | break; |
| 573 | case ASN_STRING_UniversalString: |
| 574 | OUT("size = st->size >> 2;\t/* 4 byte per character */\n"); |
| 575 | break; |
| 576 | case ASN_STRING_BMPString: |
| 577 | OUT("size = st->size >> 1;\t/* 2 byte per character */\n"); |
| 578 | break; |
| 579 | case ASN_STRING_UTF8String: |
Lev Walkin | 29c4168 | 2004-10-02 16:44:55 +0000 | [diff] [blame] | 580 | OUT("size = UTF8String_length(st);\n"); |
| 581 | OUT("if((ssize_t)size < 0) {\n"); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 582 | OUT("\tASN__CTFAIL(app_key, td, sptr,\n"); |
Lev Walkin | 29c4168 | 2004-10-02 16:44:55 +0000 | [diff] [blame] | 583 | OUT("\t\t\"%%s: UTF-8: broken encoding (%%s:%%d)\",\n"); |
| 584 | OUT("\t\ttd->name, __FILE__, __LINE__);\n"); |
| 585 | OUT("\treturn -1;\n"); |
| 586 | OUT("}\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 587 | break; |
| 588 | case ASN_CONSTR_SET_OF: |
| 589 | case ASN_CONSTR_SEQUENCE_OF: |
Lev Walkin | f1a5123 | 2005-07-04 02:01:03 +0000 | [diff] [blame] | 590 | OUT("/* Determine the number of elements */\n"); |
| 591 | OUT("size = _A_C%s_FROM_VOID(sptr)->count;\n", |
| 592 | etype==ASN_CONSTR_SET_OF?"SET":"SEQUENCE"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 593 | break; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 594 | case ASN_BASIC_OCTET_STRING: |
| 595 | OUT("size = st->size;\n"); |
| 596 | break; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 597 | default: |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 598 | if(etype & ASN_STRING_MASK) { |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 599 | OUT("size = st->size;\n"); |
| 600 | break; |
| 601 | } else { |
| 602 | const char *type_name = ASN_EXPR_TYPE2STR(etype); |
| 603 | if(!type_name) type_name = arg->expr->Identifier; |
| 604 | WARNING("SizeConstraint is not defined for %s", |
| 605 | type_name); |
| 606 | OUT_NOINDENT("#warning SizeConstraint " |
| 607 | "is not defined for %s!\n", type_name); |
| 608 | OUT("size = st->size;\n"); |
| 609 | } |
| 610 | return -1; |
| 611 | } |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | static int |
Lev Walkin | 6ea088f | 2004-09-07 06:31:15 +0000 | [diff] [blame] | 617 | emit_value_determination_code(arg_t *arg, asn1p_expr_type_e etype, asn1cnst_range_t *r_value) { |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 618 | |
| 619 | switch(etype) { |
| 620 | case ASN_BASIC_INTEGER: |
| 621 | case ASN_BASIC_ENUMERATED: |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 622 | if(asn1c_type_fits_long(arg, arg->expr) == FL_FITS_UNSIGN) { |
| 623 | OUT("value = *(const unsigned long *)sptr;\n"); |
| 624 | } else if(asn1c_type_fits_long(arg, arg->expr) != FL_NOTFIT) { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 625 | OUT("value = *(const long *)sptr;\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 626 | } else { |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 627 | /* |
| 628 | * In some cases we can explore our knowledge of |
| 629 | * underlying INTEGER_t->buf format. |
| 630 | */ |
Lev Walkin | 6ea088f | 2004-09-07 06:31:15 +0000 | [diff] [blame] | 631 | if(r_value->el_count == 0 |
| 632 | && ( |
| 633 | /* Speed-up common case: (0..MAX) */ |
| 634 | (r_value->left.type == ARE_VALUE |
| 635 | && r_value->left.value == 0 |
| 636 | && r_value->right.type == ARE_MAX) |
| 637 | || |
| 638 | /* Speed-up common case: (MIN..-1) */ |
| 639 | (r_value->left.type == ARE_MIN |
| 640 | && r_value->right.type == ARE_VALUE |
| 641 | && r_value->right.value == -1) |
| 642 | )) { |
| 643 | OUT("/* Check if the sign bit is present */\n"); |
| 644 | OUT("value = st->buf ? ((st->buf[0] & 0x80) ? -1 : 1) : 0;\n"); |
| 645 | break; |
| 646 | } |
| 647 | |
Lev Walkin | e0f2a5b | 2017-08-30 20:02:29 -0700 | [diff] [blame] | 648 | if(native_long_sign(arg, r_value) >= 0) { |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 649 | /* Special case for treating unsigned longs */ |
| 650 | OUT("if(asn_INTEGER2ulong(st, &value)) {\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 651 | INDENT(+1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 652 | OUT("ASN__CTFAIL(app_key, td, sptr,\n"); |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 653 | OUT("\t\"%%s: value too large (%%s:%%d)\",\n"); |
| 654 | OUT("\ttd->name, __FILE__, __LINE__);\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 655 | OUT("return -1;\n"); |
| 656 | INDENT(-1); |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 657 | OUT("}\n"); |
| 658 | } else { |
| 659 | OUT("if(asn_INTEGER2long(st, &value)) {\n"); |
| 660 | INDENT(+1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 661 | OUT("ASN__CTFAIL(app_key, td, sptr,\n"); |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 662 | OUT("\t\"%%s: value too large (%%s:%%d)\",\n"); |
| 663 | OUT("\ttd->name, __FILE__, __LINE__);\n"); |
| 664 | OUT("return -1;\n"); |
| 665 | INDENT(-1); |
| 666 | OUT("}\n"); |
| 667 | } |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 668 | } |
| 669 | break; |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 670 | case ASN_BASIC_REAL: |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 671 | if(arg->flags & A1C_USE_WIDE_TYPES) { |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 672 | OUT("if(asn_REAL2double(st, &value)) {\n"); |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 673 | INDENT(+1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 674 | OUT("ASN__CTFAIL(app_key, td, sptr,\n"); |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 675 | OUT("\t\"%%s: value too large (%%s:%%d)\",\n"); |
| 676 | OUT("\ttd->name, __FILE__, __LINE__);\n"); |
| 677 | OUT("return -1;\n"); |
| 678 | INDENT(-1); |
| 679 | OUT("}\n"); |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 680 | } else { |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 681 | OUT("value = *(const %s *)sptr;\n", c_name(arg).type.c_name); |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 682 | } |
| 683 | break; |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 684 | case ASN_BASIC_BOOLEAN: |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 685 | OUT("value = (*(const long *)sptr) ? 1 : 0;\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 686 | break; |
| 687 | default: |
Lev Walkin | 02b137d | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 688 | WARNING("%s:%d: Value cannot be determined " |
| 689 | "for constraint check for %s", |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 690 | arg->expr->module->source_file_name, |
Lev Walkin | 02b137d | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 691 | arg->expr->_lineno, |
| 692 | arg->expr->Identifier |
| 693 | ); |
| 694 | OUT_NOINDENT( |
| 695 | "#error %s:%d: Value of %s cannot be determined\n", |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 696 | arg->expr->module->source_file_name, |
Lev Walkin | 02b137d | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 697 | arg->expr->_lineno, |
| 698 | arg->expr->Identifier |
| 699 | ); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 700 | break; |
| 701 | } |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | static asn1p_expr_type_e |
| 707 | _find_terminal_type(arg_t *arg) { |
| 708 | asn1p_expr_t *expr; |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 709 | expr = asn1f_find_terminal_type_ex(arg->asn, arg->ns, arg->expr); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 710 | if(expr) return expr->expr_type; |
| 711 | return A1TC_INVALID; |
| 712 | } |
| 713 | |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 714 | static int |
Lev Walkin | e0f2a5b | 2017-08-30 20:02:29 -0700 | [diff] [blame] | 715 | native_long_sign(arg_t *arg, asn1cnst_range_t *r) { |
| 716 | if(!(arg->flags & A1C_USE_WIDE_TYPES) && r->left.type == ARE_VALUE |
| 717 | && r->left.value >= 0 && r->left.value <= 2147483647 |
| 718 | && r->right.type == ARE_MAX) { |
| 719 | return 1; |
| 720 | } |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 721 | if(r->left.type == ARE_VALUE |
| 722 | && r->left.value >= 0 |
| 723 | && r->right.type == ARE_VALUE |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 724 | && r->right.value > 2147483647 |
Lev Walkin | 1b3a135 | 2016-01-10 13:15:02 -0800 | [diff] [blame] | 725 | && r->right.value <= (asn1c_integer_t)(4294967295UL)) { |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 726 | if(r->el_count == 0 |
| 727 | && r->left.value == 0 |
| 728 | && r->right.value == 4294967295UL) |
| 729 | return 0; |
| 730 | else |
| 731 | return 1; |
| 732 | } else { |
| 733 | return -1; |
| 734 | } |
| 735 | } |