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