vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 1 | #include "asn1c_internal.h" |
| 2 | #include "asn1c_constraint.h" |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 3 | #include "asn1c_misc.h" |
| 4 | #include "asn1c_out.h" |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 5 | |
| 6 | #include <asn1fix_crange.h> /* constraint groker from libasn1fix */ |
| 7 | #include <asn1fix_export.h> /* other exportable stuff from libasn1fix */ |
| 8 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 9 | static int asn1c_emit_constraint_tables(arg_t *arg, int got_size); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 10 | static int emit_alphabet_check_loop(arg_t *arg, asn1cnst_range_t *range); |
| 11 | static int emit_value_determination_code(arg_t *arg, asn1p_expr_type_e etype); |
| 12 | static int emit_size_determination_code(arg_t *arg, asn1p_expr_type_e etype); |
| 13 | static asn1p_expr_type_e _find_terminal_type(arg_t *arg); |
| 14 | static int emit_range_comparison_code(arg_t *arg, asn1cnst_range_t *range, const char *varname, asn1_integer_t natural_start, asn1_integer_t natural_stop); |
| 15 | |
| 16 | #define MKID(id) asn1c_make_identifier(0, (id), 0) |
| 17 | |
| 18 | static int global_compile_mark; |
| 19 | |
| 20 | int |
| 21 | asn1c_emit_constraint_checking_code(arg_t *arg) { |
| 22 | asn1cnst_range_t *r_size; |
| 23 | asn1cnst_range_t *r_value; |
| 24 | asn1p_expr_t *expr = arg->expr; |
| 25 | asn1p_expr_type_e etype; |
| 26 | asn1p_constraint_t *ct; |
| 27 | int got_something = 0; |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 28 | int produce_st = 0; |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 29 | |
| 30 | ct = expr->combined_constraints; |
| 31 | if(ct == NULL) |
| 32 | return 1; /* No additional constraints defined */ |
| 33 | |
| 34 | etype = _find_terminal_type(arg); |
| 35 | |
vlm | 5d6d4b8 | 2004-08-25 02:03:59 +0000 | [diff] [blame] | 36 | r_value=asn1constraint_compute_PER_range(etype, ct, ACT_EL_RANGE,0,0,0); |
| 37 | r_size = asn1constraint_compute_PER_range(etype, ct, ACT_CT_SIZE,0,0,0); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 38 | if(r_value) { |
vlm | 5d6d4b8 | 2004-08-25 02:03:59 +0000 | [diff] [blame] | 39 | if(r_value->incompatible |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 40 | || r_value->empty_constraint |
| 41 | || (r_value->left.type == ARE_MIN |
| 42 | && r_value->right.type == ARE_MAX) |
| 43 | || (etype == ASN_BASIC_BOOLEAN |
| 44 | && r_value->left.value == 0 |
| 45 | && r_value->right.value == 1) |
| 46 | ) { |
| 47 | asn1constraint_range_free(r_value); |
| 48 | r_value = 0; |
| 49 | } |
| 50 | } |
| 51 | if(r_size) { |
vlm | 5d6d4b8 | 2004-08-25 02:03:59 +0000 | [diff] [blame] | 52 | if(r_size->incompatible |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 53 | || r_size->empty_constraint |
| 54 | || (r_size->left.value == 0 /* or .type == MIN */ |
| 55 | && r_size->right.type == ARE_MAX) |
| 56 | ) { |
| 57 | asn1constraint_range_free(r_size); |
| 58 | r_size = 0; |
| 59 | } |
| 60 | } |
| 61 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 62 | /* |
| 63 | * Do we really need an "*st = sptr" pointer? |
| 64 | */ |
| 65 | switch(etype) { |
| 66 | case ASN_BASIC_INTEGER: |
| 67 | case ASN_BASIC_ENUMERATED: |
| 68 | if(!(arg->flags & A1C_USE_NATIVE_INTEGERS)) |
| 69 | produce_st = 1; |
| 70 | break; |
vlm | ffa21d2 | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 71 | case ASN_BASIC_BIT_STRING: |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 72 | case ASN_BASIC_OCTET_STRING: |
| 73 | produce_st = 1; |
| 74 | break; |
| 75 | default: |
| 76 | if(etype & ASN_STRING_MASK) |
| 77 | produce_st = 1; |
| 78 | break; |
| 79 | } |
| 80 | if(produce_st) |
vlm | ffa21d2 | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 81 | OUT("const %s_t *st = sptr;\n", |
| 82 | asn1c_type_name(arg, arg->expr, TNF_SAFE)); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 83 | |
| 84 | if(r_size || r_value) { |
| 85 | if(r_size) { |
| 86 | OUT("size_t size;\n"); |
| 87 | } |
| 88 | if(r_value) |
| 89 | switch(etype) { |
| 90 | case ASN_BASIC_INTEGER: |
| 91 | case ASN_BASIC_ENUMERATED: |
| 92 | OUT("long value;\n"); |
| 93 | break; |
| 94 | case ASN_BASIC_BOOLEAN: |
| 95 | OUT("int value;\n"); |
| 96 | break; |
| 97 | default: |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | OUT("\n"); |
| 103 | |
| 104 | /* |
| 105 | * Protection against null input. |
| 106 | */ |
| 107 | OUT("if(!sptr) {\n"); |
| 108 | INDENT(+1); |
| 109 | OUT("_ASN_ERRLOG(app_errlog, app_key,\n"); |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 110 | OUT("\t\"%%s: value not given (%%s:%%d)\",\n"); |
| 111 | OUT("\ttd->name, __FILE__, __LINE__);\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 112 | OUT("return -1;\n"); |
| 113 | INDENT(-1); |
| 114 | OUT("}\n"); |
| 115 | OUT("\n"); |
| 116 | |
| 117 | if(r_value) |
| 118 | emit_value_determination_code(arg, etype); |
| 119 | if(r_size) |
| 120 | emit_size_determination_code(arg, etype); |
| 121 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 122 | INDENT(-1); |
| 123 | REDIR(OT_CTABLES); |
| 124 | /* Emit FROM() tables */ |
| 125 | asn1c_emit_constraint_tables(arg, r_size?1:0); |
| 126 | REDIR(OT_CODE); |
| 127 | INDENT(+1); |
| 128 | |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 129 | /* |
| 130 | * Here is an if() {} else {} constaint checking code. |
| 131 | */ |
| 132 | OUT("\n"); |
| 133 | OUT("if("); |
| 134 | INDENT(+1); |
| 135 | if(r_size) { |
| 136 | if(got_something++) { OUT("\n"); OUT(" && "); } |
| 137 | OUT("("); |
| 138 | emit_range_comparison_code(arg, r_size, "size", 0, -1); |
| 139 | OUT(")"); |
| 140 | } |
| 141 | if(r_value) { |
| 142 | if(got_something++) { OUT("\n"); OUT(" && "); } |
| 143 | OUT("("); |
| 144 | if(etype == ASN_BASIC_BOOLEAN) |
| 145 | emit_range_comparison_code(arg, r_value, |
| 146 | "value", 0, 1); |
| 147 | else |
| 148 | emit_range_comparison_code(arg, r_value, |
| 149 | "value", -1, -1); |
| 150 | OUT(")"); |
| 151 | } |
| 152 | if(ct->_compile_mark) { |
| 153 | if(got_something++) { OUT("\n"); OUT(" && "); } |
vlm | 7e20dfc | 2004-08-22 13:11:40 +0000 | [diff] [blame] | 154 | OUT("!check_permitted_alphabet_%d(sptr)", |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 155 | ct->_compile_mark); |
| 156 | } |
| 157 | if(!got_something) { |
| 158 | OUT("1 /* No applicable constraints whatsoever */"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 159 | OUT(") {\n"); |
| 160 | INDENT(-1); |
| 161 | INDENTED(OUT("/* Nothing is here. See below */\n")); |
| 162 | OUT("}\n"); |
| 163 | OUT("\n"); |
| 164 | return 1; |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 165 | } |
| 166 | INDENT(-1); |
| 167 | OUT(") {\n"); |
| 168 | INDENT(+1); |
| 169 | OUT("/* Constraint check succeeded */\n"); |
vlm | 22b0311 | 2004-08-22 12:37:35 +0000 | [diff] [blame] | 170 | OUT("return 0;\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 171 | INDENT(-1); |
| 172 | OUT("} else {\n"); |
| 173 | INDENT(+1); |
| 174 | OUT("_ASN_ERRLOG(app_errlog, app_key,\n"); |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 175 | OUT("\t\"%%s: constraint failed (%%s:%%d)\",\n"); |
| 176 | OUT("\ttd->name, __FILE__, __LINE__);\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 177 | OUT("return -1;\n"); |
| 178 | INDENT(-1); |
| 179 | OUT("}\n"); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 184 | static int |
| 185 | asn1c_emit_constraint_tables(arg_t *arg, int got_size) { |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 186 | asn1_integer_t range_start; |
| 187 | asn1_integer_t range_stop; |
| 188 | asn1p_expr_type_e etype; |
| 189 | asn1cnst_range_t *range; |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 190 | asn1p_constraint_t *ct; |
| 191 | int utf8_full_alphabet_check = 0; |
| 192 | int max_table_size = 256; |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 193 | int table[256]; |
| 194 | int use_table; |
| 195 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 196 | ct = arg->expr->combined_constraints; |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 197 | if(!ct) return 0; |
| 198 | |
| 199 | etype = _find_terminal_type(arg); |
| 200 | |
vlm | 5d6d4b8 | 2004-08-25 02:03:59 +0000 | [diff] [blame] | 201 | range = asn1constraint_compute_PER_range(etype, ct, ACT_CT_FROM, 0,0,0); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 202 | if(!range) return 0; |
| 203 | |
vlm | 5d6d4b8 | 2004-08-25 02:03:59 +0000 | [diff] [blame] | 204 | if(range->incompatible |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 205 | || range->empty_constraint) { |
| 206 | asn1constraint_range_free(range); |
| 207 | return 0; |
| 208 | } |
| 209 | |
vlm | 465f3c0 | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 210 | |
| 211 | if(range->left.type == ARE_MIN |
| 212 | && range->right.type == ARE_MAX) { |
| 213 | /* |
| 214 | * The permitted alphabet constraint checker code guarantees |
| 215 | * that either both bounds (left/right) are present, or |
| 216 | * they're absent simultaneously. Thus, this assertion |
| 217 | * legitimately holds true. |
| 218 | */ |
| 219 | assert(range->el_count == 0); |
| 220 | /* The full range is specified. Ignore it. */ |
| 221 | return 0; |
| 222 | } |
| 223 | |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 224 | range_start = range->left.value; |
| 225 | range_stop = range->right.value; |
| 226 | assert(range->left.type == ARE_VALUE); |
| 227 | assert(range->right.type == ARE_VALUE); |
| 228 | assert(range_start <= range_stop); |
| 229 | |
| 230 | range_start = 0; /* Force old behavior */ |
| 231 | |
| 232 | /* |
| 233 | * Check if we need a test table to check the alphabet. |
| 234 | */ |
| 235 | use_table = 1; |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 236 | if(range->el_count == 0) { |
| 237 | /* |
| 238 | * It's better to have a short if() check |
| 239 | * than waste 4k of table space |
| 240 | */ |
| 241 | use_table = 0; |
| 242 | } |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 243 | if((range_stop - range_start) > 255) |
| 244 | use_table = 0; |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 245 | if(etype == ASN_STRING_UTF8String) { |
| 246 | if(range_stop >= 0x80) |
| 247 | use_table = 0; |
| 248 | else |
| 249 | max_table_size = 128; |
| 250 | } |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 251 | |
| 252 | if(!ct->_compile_mark) |
| 253 | ct->_compile_mark = ++global_compile_mark; |
| 254 | |
| 255 | if(use_table) { |
| 256 | int i, n = 0; |
| 257 | int untl; |
| 258 | memset(table, 0, sizeof(table)); |
| 259 | for(i = -1; i < range->el_count; i++) { |
| 260 | asn1cnst_range_t *r; |
| 261 | asn1_integer_t v; |
| 262 | if(i == -1) { |
| 263 | if(range->el_count) continue; |
| 264 | r = range; |
| 265 | } else { |
| 266 | r = range->elements[i]; |
| 267 | } |
| 268 | for(v = r->left.value; v <= r->right.value; v++) { |
| 269 | assert((v - range_start) >= 0); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 270 | assert((v - range_start) < max_table_size); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 271 | table[v - range_start] = ++n; |
| 272 | } |
| 273 | } |
| 274 | |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 275 | untl = (range_stop - range_start) + 1; |
| 276 | untl += (untl % 16)?16 - (untl % 16):0; |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 277 | OUT("static int permitted_alphabet_table_%d[%d] = {\n", |
| 278 | ct->_compile_mark, max_table_size); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 279 | for(n = 0; n < untl; n++) { |
| 280 | OUT("%d,", table[n]?1:0); |
| 281 | if(!((n+1) % 16)) { |
| 282 | int c; |
| 283 | if(!n) { |
| 284 | OUT("\n"); |
| 285 | continue; |
| 286 | } |
| 287 | OUT("\t/* "); |
| 288 | for(c = n - 15; c <= n; c++) { |
| 289 | if(table[c]) { |
| 290 | int a = c + range_start; |
| 291 | if(a > 0x20 && a < 0x80) |
| 292 | OUT("%c", a); |
| 293 | else |
| 294 | OUT("."); |
| 295 | } else { |
| 296 | OUT(" "); |
| 297 | } |
| 298 | } |
| 299 | OUT(" */"); |
| 300 | OUT("\n"); |
| 301 | } |
| 302 | } |
| 303 | OUT("};\n"); |
| 304 | OUT("\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 305 | } else if(etype == ASN_STRING_UTF8String) { |
| 306 | /* |
| 307 | * UTF8String type is a special case in many respects. |
| 308 | */ |
| 309 | assert(range_stop > 255); /* This one's unobvious */ |
| 310 | if(got_size) { |
| 311 | /* |
| 312 | * Size has been already determined. |
| 313 | * The UTF8String length checker also checks |
| 314 | * for the syntax validity, so we don't have |
| 315 | * to repeat this process twice. |
| 316 | */ |
| 317 | ct->_compile_mark = 0; /* Don't generate code */ |
| 318 | asn1constraint_range_free(range); |
| 319 | return 0; |
| 320 | } else { |
| 321 | utf8_full_alphabet_check = 1; |
| 322 | } |
| 323 | } else { |
| 324 | /* |
| 325 | * This permitted alphabet check will be |
| 326 | * expressed using conditional statements |
| 327 | * instead of table lookups. Table would be |
| 328 | * to large or otherwise inappropriate (too sparse?). |
| 329 | */ |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | OUT("static int check_permitted_alphabet_%d(const void *sptr) {\n", |
| 333 | ct->_compile_mark); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 334 | INDENT(+1); |
| 335 | if(utf8_full_alphabet_check) { |
| 336 | OUT("if(UTF8String_length((UTF8String_t *)sptr, td->name, \n"); |
| 337 | OUT("\tapp_errlog, app_key) == -1)\n"); |
vlm | cdf661b | 2004-08-22 12:47:03 +0000 | [diff] [blame] | 338 | OUT("\t\treturn -1; /* Alphabet (sic!) test failed. */\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 339 | OUT("\n"); |
| 340 | } else { |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 341 | if(use_table) { |
| 342 | OUT("int *table = permitted_alphabet_table_%d;\n", |
| 343 | ct->_compile_mark); |
| 344 | emit_alphabet_check_loop(arg, 0); |
| 345 | } else { |
| 346 | emit_alphabet_check_loop(arg, range); |
| 347 | } |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 348 | } |
vlm | cdf661b | 2004-08-22 12:47:03 +0000 | [diff] [blame] | 349 | OUT("return 0;\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 350 | INDENT(-1); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 351 | OUT("}\n"); |
| 352 | OUT("\n"); |
| 353 | |
| 354 | asn1constraint_range_free(range); |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static int |
| 360 | emit_alphabet_check_loop(arg_t *arg, asn1cnst_range_t *range) { |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 361 | asn1_integer_t natural_stop; |
vlm | b08de15 | 2004-08-22 03:30:05 +0000 | [diff] [blame] | 362 | asn1p_expr_t *terminal; |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 363 | |
vlm | b08de15 | 2004-08-22 03:30:05 +0000 | [diff] [blame] | 364 | terminal = asn1f_find_terminal_type_ex(arg->asn, arg->mod, arg->expr); |
| 365 | if(terminal) { |
| 366 | OUT("/* The underlying type is %s */\n", |
| 367 | ASN_EXPR_TYPE2STR(terminal->expr_type)); |
| 368 | } else { |
| 369 | terminal = arg->expr; |
| 370 | } |
vlm | ffa21d2 | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 371 | OUT("const %s_t *st = sptr;\n", |
vlm | b08de15 | 2004-08-22 03:30:05 +0000 | [diff] [blame] | 372 | asn1c_type_name(arg, terminal, TNF_SAFE)); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 373 | |
vlm | b08de15 | 2004-08-22 03:30:05 +0000 | [diff] [blame] | 374 | switch(terminal->expr_type) { |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 375 | case ASN_STRING_UTF8String: |
vlm | ffa21d2 | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 376 | OUT("const uint8_t *ch = st->buf;\n"); |
| 377 | OUT("const uint8_t *end = ch + st->size;\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 378 | OUT("\n"); |
| 379 | OUT("for(; ch < end; ch++) {\n"); |
| 380 | INDENT(+1); |
| 381 | OUT("uint8_t cv = *ch;\n"); |
vlm | cdf661b | 2004-08-22 12:47:03 +0000 | [diff] [blame] | 382 | if(!range) OUT("if(cv >= 0x80) return -1;\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 383 | natural_stop = 0xffffffffUL; |
| 384 | break; |
| 385 | case ASN_STRING_UniversalString: |
vlm | 465f3c0 | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 386 | OUT("const uint8_t *ch = st->buf;\n"); |
| 387 | OUT("const uint8_t *end = ch + st->size;\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 388 | OUT("\n"); |
vlm | 465f3c0 | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 389 | OUT("if(st->size %% 4) return -1; /* (size%%4)! */\n"); |
| 390 | OUT("for(; ch < end; ch += 4) {\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 391 | INDENT(+1); |
vlm | 465f3c0 | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 392 | OUT("uint32_t cv = (ch[0] << 24)\n"); |
| 393 | OUT("\t\t| (ch[1] << 16)\n"); |
| 394 | OUT("\t\t| (ch[2] << 8)\n"); |
| 395 | OUT("\t\t| ch[3];\n"); |
vlm | cdf661b | 2004-08-22 12:47:03 +0000 | [diff] [blame] | 396 | if(!range) OUT("if(cv > 255) return -1;\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 397 | natural_stop = 0xffffffffUL; |
| 398 | break; |
| 399 | case ASN_STRING_BMPString: |
vlm | 465f3c0 | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 400 | OUT("const uint8_t *ch = st->buf;\n"); |
| 401 | OUT("const uint8_t *end = ch + st->size;\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 402 | OUT("\n"); |
vlm | 465f3c0 | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 403 | OUT("if(st->size %% 2) return -1; /* (size%%2)! */\n"); |
| 404 | OUT("for(; ch < end; ch += 2) {\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 405 | INDENT(+1); |
vlm | 465f3c0 | 2004-09-06 08:07:19 +0000 | [diff] [blame] | 406 | OUT("uint16_t cv = (ch[0] << 8)\n"); |
| 407 | OUT("\t\t| ch[1];\n"); |
vlm | cdf661b | 2004-08-22 12:47:03 +0000 | [diff] [blame] | 408 | if(!range) OUT("if(cv > 255) return -1;\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 409 | natural_stop = 0xffff; |
| 410 | break; |
| 411 | case ASN_BASIC_OCTET_STRING: |
| 412 | default: |
vlm | ffa21d2 | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 413 | OUT("const uint8_t *ch = st->buf;\n"); |
| 414 | OUT("const uint8_t *end = ch + st->size;\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 415 | OUT("\n"); |
| 416 | OUT("for(; ch < end; ch++) {\n"); |
| 417 | INDENT(+1); |
| 418 | OUT("uint8_t cv = *ch;\n"); |
| 419 | natural_stop = 0xff; |
| 420 | break; |
| 421 | } |
| 422 | |
| 423 | if(range) { |
| 424 | OUT("if(!("); |
| 425 | emit_range_comparison_code(arg, range, "cv", 0, natural_stop); |
vlm | cdf661b | 2004-08-22 12:47:03 +0000 | [diff] [blame] | 426 | OUT(")) return -1;\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 427 | } else { |
vlm | cdf661b | 2004-08-22 12:47:03 +0000 | [diff] [blame] | 428 | OUT("if(!table[cv]) return -1;\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | INDENT(-1); |
| 432 | OUT("}\n"); |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static int |
| 438 | emit_range_comparison_code(arg_t *arg, asn1cnst_range_t *range, const char *varname, asn1_integer_t natural_start, asn1_integer_t natural_stop) { |
| 439 | int ignore_left; |
| 440 | int ignore_right; |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 441 | int generated_something = 0; |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 442 | int i; |
| 443 | |
| 444 | for(i = -1; i < range->el_count; i++) { |
| 445 | asn1cnst_range_t *r; |
| 446 | if(i == -1) { |
| 447 | if(range->el_count) continue; |
| 448 | r = range; |
| 449 | } else { |
| 450 | if(i) OUT(" || "); |
| 451 | r = range->elements[i]; |
| 452 | } |
| 453 | |
| 454 | if(r != range) OUT("("); |
| 455 | |
| 456 | ignore_left = (r->left.type == ARE_MIN) |
| 457 | || (natural_start != -1 |
| 458 | && r->left.value <= natural_start); |
| 459 | ignore_right = (r->right.type == ARE_MAX) |
| 460 | || (natural_stop != -1 |
| 461 | && r->right.value >= natural_stop); |
| 462 | if(ignore_left && ignore_right) { |
| 463 | OUT("1 /* Constraint matches natural range of %s */", |
| 464 | varname); |
| 465 | continue; |
| 466 | } |
| 467 | |
| 468 | if(ignore_left) { |
| 469 | OUT("%s <= %lld", varname, |
| 470 | (long long)r->right.value); |
| 471 | } else if(ignore_right) { |
| 472 | OUT("%s >= %lld", varname, |
| 473 | (long long)r->left.value); |
| 474 | } else if(r->left.value == r->right.value) { |
| 475 | OUT("%s == %lld", varname, |
| 476 | (long long)r->right.value); |
| 477 | } else { |
| 478 | OUT("%s >= %lld && %s <= %lld", |
| 479 | varname, |
| 480 | (long long)r->left.value, |
| 481 | varname, |
| 482 | (long long)r->right.value); |
| 483 | } |
| 484 | if(r != range) OUT(")"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 485 | generated_something = 1; |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 486 | } |
| 487 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 488 | return generated_something; |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static int |
| 492 | emit_size_determination_code(arg_t *arg, asn1p_expr_type_e etype) { |
| 493 | |
| 494 | switch(etype) { |
| 495 | case ASN_BASIC_BIT_STRING: |
| 496 | OUT("if(st->size > 0) {\n"); |
| 497 | OUT("\t/* Size in bits */\n"); |
vlm | 17dc9eb | 2004-09-05 10:42:19 +0000 | [diff] [blame] | 498 | OUT("\tsize = 8 * (st->size - 1) - (st->buf[0] & 0x7);\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 499 | OUT("} else {\n"); |
| 500 | OUT("\tsize = 0;\n"); |
| 501 | OUT("}\n"); |
| 502 | break; |
| 503 | case ASN_STRING_UniversalString: |
| 504 | OUT("size = st->size >> 2;\t/* 4 byte per character */\n"); |
| 505 | break; |
| 506 | case ASN_STRING_BMPString: |
| 507 | OUT("size = st->size >> 1;\t/* 2 byte per character */\n"); |
| 508 | break; |
| 509 | case ASN_STRING_UTF8String: |
| 510 | OUT("size = UTF8String_length(st, td->name, app_errlog, app_key);\n"); |
| 511 | OUT("if(size == (size_t)-1) return -1;\n"); |
| 512 | break; |
| 513 | case ASN_CONSTR_SET_OF: |
| 514 | case ASN_CONSTR_SEQUENCE_OF: |
| 515 | OUT("{ /* Determine the number of elements */\n"); |
| 516 | INDENT(+1); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 517 | OUT("const A_%s_OF(void) *list;\n", |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 518 | etype==ASN_CONSTR_SET_OF?"SET":"SEQUENCE"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 519 | OUT("(const void *)list = sptr;\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 520 | OUT("size = list->count;\n"); |
| 521 | INDENT(-1); |
| 522 | OUT("}\n"); |
| 523 | break; |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 524 | case ASN_BASIC_OCTET_STRING: |
| 525 | OUT("size = st->size;\n"); |
| 526 | break; |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 527 | default: |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 528 | if(etype & ASN_STRING_MASK) { |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 529 | OUT("size = st->size;\n"); |
| 530 | break; |
| 531 | } else { |
| 532 | const char *type_name = ASN_EXPR_TYPE2STR(etype); |
| 533 | if(!type_name) type_name = arg->expr->Identifier; |
| 534 | WARNING("SizeConstraint is not defined for %s", |
| 535 | type_name); |
| 536 | OUT_NOINDENT("#warning SizeConstraint " |
| 537 | "is not defined for %s!\n", type_name); |
| 538 | OUT("size = st->size;\n"); |
| 539 | } |
| 540 | return -1; |
| 541 | } |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static int |
| 547 | emit_value_determination_code(arg_t *arg, asn1p_expr_type_e etype) { |
| 548 | |
| 549 | switch(etype) { |
| 550 | case ASN_BASIC_INTEGER: |
| 551 | case ASN_BASIC_ENUMERATED: |
| 552 | if(arg->flags & A1C_USE_NATIVE_INTEGERS) { |
vlm | ffa21d2 | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 553 | OUT("value = *(const int *)sptr;\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 554 | } else { |
| 555 | OUT("if(asn1_INTEGER2long(st, &value)) {\n"); |
| 556 | INDENT(+1); |
| 557 | OUT("_ASN_ERRLOG(app_errlog, app_key,\n"); |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 558 | OUT("\t\"%%s: value too large (%%s:%%d)\",\n"); |
| 559 | OUT("\ttd->name, __FILE__, __LINE__);\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 560 | OUT("return -1;\n"); |
| 561 | INDENT(-1); |
| 562 | OUT("}\n"); |
| 563 | } |
| 564 | break; |
| 565 | case ASN_BASIC_BOOLEAN: |
vlm | ffa21d2 | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 566 | OUT("value = (*(const int *)sptr) ? 1 : 0;\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 567 | break; |
| 568 | default: |
vlm | ffa21d2 | 2004-08-21 07:34:58 +0000 | [diff] [blame] | 569 | WARNING("%s:%d: Value cannot be determined " |
| 570 | "for constraint check for %s", |
| 571 | arg->mod->source_file_name, |
| 572 | arg->expr->_lineno, |
| 573 | arg->expr->Identifier |
| 574 | ); |
| 575 | OUT_NOINDENT( |
| 576 | "#error %s:%d: Value of %s cannot be determined\n", |
| 577 | arg->mod->source_file_name, |
| 578 | arg->expr->_lineno, |
| 579 | arg->expr->Identifier |
| 580 | ); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 581 | break; |
| 582 | } |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static asn1p_expr_type_e |
| 588 | _find_terminal_type(arg_t *arg) { |
| 589 | asn1p_expr_t *expr; |
vlm | e8423bc | 2004-08-22 03:10:52 +0000 | [diff] [blame] | 590 | expr = asn1f_find_terminal_type_ex(arg->asn, arg->mod, arg->expr); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 591 | if(expr) return expr->expr_type; |
| 592 | return A1TC_INVALID; |
| 593 | } |
| 594 | |