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