Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1fix_internal.h" |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 2 | #include "asn1fix.h" |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 3 | |
| 4 | /* Print everything to stderr */ |
| 5 | static void _default_error_logger(int _severity, const char *fmt, ...); |
| 6 | |
| 7 | /* |
| 8 | * Internal check functions. |
| 9 | */ |
Lev Walkin | d24c62d | 2004-09-15 11:47:23 +0000 | [diff] [blame] | 10 | static int asn1f_fix_module__phase_1(arg_t *arg); |
| 11 | static int asn1f_fix_module__phase_2(arg_t *arg); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 12 | static int asn1f_fix_simple(arg_t *arg); /* For INTEGER/ENUMERATED */ |
| 13 | static int asn1f_fix_constructed(arg_t *arg); /* For SEQUENCE/SET/CHOICE */ |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 14 | static int asn1f_resolve_constraints(arg_t *arg); /* For subtype constraints */ |
| 15 | static int asn1f_check_constraints(arg_t *arg); /* For subtype constraints */ |
Lev Walkin | f593f10 | 2005-02-22 07:59:59 +0000 | [diff] [blame] | 16 | static int asn1f_check_duplicate(arg_t *arg); |
Lev Walkin | 1dec580 | 2005-03-04 09:02:27 +0000 | [diff] [blame] | 17 | static int asn1f_apply_unique_index(arg_t *arg); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 18 | static int phase_1_1(arg_t *arg, int prm2); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 19 | |
Lev Walkin | b45e067 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 20 | arg_t a1f_replace_me_with_proper_interface_arg; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * Scan every module defined here in search for inconsistences. |
| 24 | */ |
| 25 | int |
| 26 | asn1f_process(asn1p_t *asn, enum asn1f_flags flags, |
| 27 | error_logger_f error_logger) { |
| 28 | arg_t arg; |
| 29 | int fatals = 0; |
| 30 | int warnings = 0; |
Lev Walkin | 4533a5f | 2005-03-18 04:22:41 +0000 | [diff] [blame] | 31 | int ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Check validity of arguments. |
| 35 | */ |
| 36 | if(asn == NULL) { |
| 37 | errno = EINVAL; |
| 38 | return -1; |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | * If errors handler is not specified, default to internal one. |
| 43 | */ |
| 44 | if(error_logger == 0) { |
| 45 | error_logger = _default_error_logger; |
| 46 | } |
| 47 | |
| 48 | memset(&arg, 0, sizeof(arg)); |
| 49 | arg.asn = asn; |
| 50 | arg.eh = error_logger; |
| 51 | |
| 52 | if(flags & A1F_DEBUG) { |
| 53 | arg.debug = arg.eh; |
| 54 | arg.debug(-1, "Called %s() with flags %d", __func__, flags); |
| 55 | flags &= ~A1F_DEBUG; |
| 56 | } |
| 57 | |
Lev Walkin | 5253da4 | 2004-08-20 13:25:56 +0000 | [diff] [blame] | 58 | /* Allow SIZE() constraint for INTEGER and other types */ |
| 59 | if(flags & A1F_EXTENDED_SizeConstraint) { |
| 60 | arg.flags |= A1F_EXTENDED_SizeConstraint; |
| 61 | flags &= ~A1F_EXTENDED_SizeConstraint; |
| 62 | if(arg.debug) { |
| 63 | arg.debug(-1, |
| 64 | "Extended SizeConstraint support enabled"); |
| 65 | } |
| 66 | } |
| 67 | |
Lev Walkin | b45e067 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 68 | a1f_replace_me_with_proper_interface_arg = arg; |
| 69 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 70 | /* |
| 71 | * Check that we haven't missed an unknown flag. |
| 72 | */ |
| 73 | if(flags) { |
| 74 | errno = EINVAL; |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Process each module in the list. |
Lev Walkin | f593f10 | 2005-02-22 07:59:59 +0000 | [diff] [blame] | 80 | * PHASE I. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | */ |
| 82 | TQ_FOR(arg.mod, &(asn->modules), mod_next) { |
Lev Walkin | 4533a5f | 2005-03-18 04:22:41 +0000 | [diff] [blame] | 83 | ret = asn1f_fix_module__phase_1(&arg); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | /* |
| 85 | * These lines are used for illustration purposes. |
| 86 | * RET2RVAL() is used everywhere else. |
| 87 | */ |
| 88 | if(ret == -1) fatals++; |
| 89 | if(ret == 1) warnings++; |
| 90 | } |
Lev Walkin | f593f10 | 2005-02-22 07:59:59 +0000 | [diff] [blame] | 91 | /* PHASE II. */ |
Lev Walkin | d24c62d | 2004-09-15 11:47:23 +0000 | [diff] [blame] | 92 | TQ_FOR(arg.mod, &(asn->modules), mod_next) { |
Lev Walkin | 4533a5f | 2005-03-18 04:22:41 +0000 | [diff] [blame] | 93 | ret = asn1f_fix_module__phase_2(&arg); |
Lev Walkin | d24c62d | 2004-09-15 11:47:23 +0000 | [diff] [blame] | 94 | if(ret == -1) fatals++; |
| 95 | if(ret == 1) warnings++; |
| 96 | } |
| 97 | |
| 98 | memset(&a1f_replace_me_with_proper_interface_arg, 0, sizeof(arg_t)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * Compute a return value. |
| 102 | */ |
| 103 | return fatals?-1:warnings?1:0; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Check the internals of a single module. |
| 108 | */ |
| 109 | static int |
Lev Walkin | d24c62d | 2004-09-15 11:47:23 +0000 | [diff] [blame] | 110 | asn1f_fix_module__phase_1(arg_t *arg) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | asn1p_expr_t *expr; |
| 112 | int rvalue = 0; |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 113 | int ret; |
Lev Walkin | 4533a5f | 2005-03-18 04:22:41 +0000 | [diff] [blame] | 114 | asn1p_module_t *omod; |
| 115 | |
| 116 | /* |
| 117 | * Check that we don't have a similarly named module. |
| 118 | */ |
| 119 | TQ_FOR(omod, &arg->asn->modules, mod_next) { |
| 120 | int sameNames; |
| 121 | if(omod == arg->mod) break; |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 122 | sameNames = strcmp(omod->ModuleName, arg->mod->ModuleName)?0:1; |
Lev Walkin | 4533a5f | 2005-03-18 04:22:41 +0000 | [diff] [blame] | 123 | if(omod->module_oid && arg->mod->module_oid) { |
| 124 | /* Compare only the OID. */ |
| 125 | if(asn1p_oid_compare(omod->module_oid, |
| 126 | arg->mod->module_oid) == 0) { |
Lev Walkin | 1a44795 | 2005-03-18 04:26:12 +0000 | [diff] [blame] | 127 | FATAL("ASN.1 module %s in %s " |
Lev Walkin | 4533a5f | 2005-03-18 04:22:41 +0000 | [diff] [blame] | 128 | "has the same OBJECT IDENTIFIER" |
Lev Walkin | 1a44795 | 2005-03-18 04:26:12 +0000 | [diff] [blame] | 129 | " as module %s", |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 130 | omod->ModuleName, |
Lev Walkin | 1a44795 | 2005-03-18 04:26:12 +0000 | [diff] [blame] | 131 | omod->source_file_name, |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 132 | arg->mod->ModuleName |
Lev Walkin | 4533a5f | 2005-03-18 04:22:41 +0000 | [diff] [blame] | 133 | ); |
| 134 | RET2RVAL(-1, rvalue); |
| 135 | } else if(sameNames) { |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 136 | WARNING("ASN.1 module %s is defined more than once, with different OIDs", omod->ModuleName); |
Lev Walkin | 4533a5f | 2005-03-18 04:22:41 +0000 | [diff] [blame] | 137 | RET2RVAL(1, rvalue); |
| 138 | } |
| 139 | } else if(sameNames) { |
| 140 | FATAL("ASN.1 module %s is defined more than once", |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 141 | omod->ModuleName); |
Lev Walkin | 4533a5f | 2005-03-18 04:22:41 +0000 | [diff] [blame] | 142 | RET2RVAL(-1, rvalue); |
| 143 | } |
| 144 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 145 | |
Lev Walkin | b45e067 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 146 | switch((arg->mod->module_flags & MSF_MASK_TAGS)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 147 | case MSF_NOFLAGS: |
| 148 | case MSF_EXPLICIT_TAGS: |
| 149 | case MSF_IMPLICIT_TAGS: |
| 150 | case MSF_AUTOMATIC_TAGS: |
| 151 | break; |
| 152 | default: |
| 153 | FATAL("Module %s defined with ambiguous global tagging mode", |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 154 | arg->mod->ModuleName); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 155 | RET2RVAL(-1, rvalue); |
| 156 | } |
| 157 | |
Lev Walkin | b45e067 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 158 | switch((arg->mod->module_flags & MSF_MASK_INSTRUCTIONS)) { |
| 159 | case MSF_NOFLAGS: |
Lev Walkin | 4ec3b4c | 2004-08-22 03:09:24 +0000 | [diff] [blame] | 160 | /* |
| 161 | * arg->mod->module_flags |= MSF_TAG_INSTRUCTIONS; |
| 162 | */ |
Lev Walkin | b45e067 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 163 | break; |
| 164 | case MSF_unk_INSTRUCTIONS: |
| 165 | WARNING("Module %s defined with unrecognized " |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 166 | "encoding reference", arg->mod->ModuleName); |
Lev Walkin | b45e067 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 167 | RET2RVAL(1, rvalue); |
| 168 | /* Fall through */ |
| 169 | case MSF_TAG_INSTRUCTIONS: |
| 170 | case MSF_XER_INSTRUCTIONS: |
| 171 | break; |
| 172 | default: |
| 173 | FATAL("Module %s defined with ambiguous encoding reference", |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 174 | arg->mod->ModuleName); |
Lev Walkin | b45e067 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 175 | RET2RVAL(-1, rvalue); |
| 176 | } |
| 177 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 178 | /* |
| 179 | * Do various non-recursive transformations. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 180 | */ |
| 181 | TQ_FOR(expr, &(arg->mod->members), next) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 182 | arg->expr = expr; |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 183 | ret = phase_1_1(arg, 0); |
Lev Walkin | f593f10 | 2005-02-22 07:59:59 +0000 | [diff] [blame] | 184 | RET2RVAL(ret, rvalue); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 185 | /* |
| 186 | * Make sure everybody's behaving well. |
| 187 | */ |
| 188 | assert(arg->expr == expr); |
| 189 | } |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 190 | TQ_FOR(expr, &(arg->mod->members), next) { |
| 191 | arg->expr = expr; |
| 192 | ret = phase_1_1(arg, 1); |
| 193 | RET2RVAL(ret, rvalue); |
| 194 | assert(arg->expr == expr); |
| 195 | } |
| 196 | |
| 197 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * 5. Automatic tagging |
| 201 | */ |
| 202 | TQ_FOR(expr, &(arg->mod->members), next) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 203 | |
| 204 | arg->expr = expr; |
| 205 | |
| 206 | ret = asn1f_recurse_expr(arg, asn1f_fix_constr_autotag); |
| 207 | RET2RVAL(ret, rvalue); |
| 208 | |
| 209 | assert(arg->expr == expr); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * 8. fix BIT STRING |
| 214 | * 9. fix spaces in cstrings |
| 215 | */ |
| 216 | TQ_FOR(expr, &(arg->mod->members), next) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 217 | arg->expr = expr; |
| 218 | |
| 219 | ret = asn1f_recurse_expr(arg, asn1f_fix_bit_string); |
| 220 | RET2RVAL(ret, rvalue); |
| 221 | |
| 222 | ret = asn1f_recurse_expr(arg, asn1f_fix_cstring); |
| 223 | RET2RVAL(ret, rvalue); |
| 224 | |
| 225 | assert(arg->expr == expr); |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * ... Check for tags distinctness. |
| 230 | */ |
| 231 | TQ_FOR(expr, &(arg->mod->members), next) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 232 | arg->expr = expr; |
| 233 | |
| 234 | ret = asn1f_recurse_expr(arg, asn1f_check_constr_tags_distinct); |
| 235 | RET2RVAL(ret, rvalue); |
| 236 | |
| 237 | assert(arg->expr == expr); |
| 238 | } |
| 239 | |
Lev Walkin | d24c62d | 2004-09-15 11:47:23 +0000 | [diff] [blame] | 240 | return rvalue; |
| 241 | } |
| 242 | |
| 243 | static int |
| 244 | asn1f_fix_module__phase_2(arg_t *arg) { |
| 245 | asn1p_expr_t *expr; |
| 246 | int rvalue = 0; |
| 247 | int ret; |
| 248 | |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 249 | TQ_FOR(expr, &(arg->mod->members), next) { |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 250 | |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 251 | arg->expr = expr; |
| 252 | |
Lev Walkin | b25fa06 | 2004-09-29 13:17:06 +0000 | [diff] [blame] | 253 | /* |
Lev Walkin | 3645c1c | 2004-10-31 00:11:50 +0000 | [diff] [blame] | 254 | * Dereference DEFAULT values. |
| 255 | */ |
| 256 | ret = asn1f_recurse_expr(arg, asn1f_fix_dereference_defaults); |
| 257 | RET2RVAL(ret, rvalue); |
| 258 | |
| 259 | /* |
Lev Walkin | b25fa06 | 2004-09-29 13:17:06 +0000 | [diff] [blame] | 260 | * Check semantic validity of constraints. |
| 261 | */ |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 262 | ret = asn1f_recurse_expr(arg, asn1f_check_constraints); |
| 263 | RET2RVAL(ret, rvalue); |
| 264 | |
Lev Walkin | 1dec580 | 2005-03-04 09:02:27 +0000 | [diff] [blame] | 265 | /* |
| 266 | * Uniquely tag each inner type. |
| 267 | */ |
| 268 | asn1f_apply_unique_index(0); |
| 269 | ret = asn1f_recurse_expr(arg, asn1f_apply_unique_index); |
| 270 | RET2RVAL(ret, rvalue); |
| 271 | |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 272 | assert(arg->expr == expr); |
| 273 | } |
| 274 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 275 | return rvalue; |
| 276 | } |
| 277 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 278 | static int |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 279 | phase_1_1(arg_t *arg, int prm2) { |
| 280 | asn1p_expr_t *expr = arg->expr; |
| 281 | int rvalue = 0; |
| 282 | int ret; |
| 283 | |
| 284 | if(expr->lhs_params && expr->spec_index == -1) { |
| 285 | int i; |
| 286 | if(!prm2) |
| 287 | /* Do not process the parameterized type just yet */ |
| 288 | return 0; |
| 289 | for(i = 0; i < expr->specializations.pspecs_count; i++) { |
| 290 | arg->expr = expr->specializations.pspec[i].my_clone; |
| 291 | ret = phase_1_1(arg, 0); |
| 292 | RET2RVAL(ret, rvalue); |
| 293 | } |
| 294 | arg->expr = expr; /* revert */ |
| 295 | return rvalue; |
| 296 | } else if(prm2) { |
| 297 | return 0; /* Already done! */ |
| 298 | } |
| 299 | |
| 300 | /* Check whether this type is a duplicate */ |
| 301 | if(!expr->lhs_params) { |
| 302 | ret = asn1f_check_duplicate(arg); |
| 303 | RET2RVAL(ret, rvalue); |
| 304 | } |
| 305 | |
| 306 | DEBUG("=== Now processing \"%s\" (%d/0x%x) at line %d ===", |
| 307 | expr->Identifier, expr->meta_type, expr->expr_type, |
| 308 | expr->_lineno); |
| 309 | assert(expr->meta_type != AMT_INVALID); |
| 310 | |
| 311 | /* |
| 312 | * 2.1 Pre-process simple types (ENUMERATED, INTEGER, etc). |
| 313 | */ |
| 314 | ret = asn1f_recurse_expr(arg, asn1f_fix_simple); |
| 315 | RET2RVAL(ret, rvalue); |
| 316 | |
| 317 | /* |
| 318 | * 2.5.4 |
| 319 | */ |
| 320 | ret = asn1f_recurse_expr(arg, asn1f_fix_dereference_types); |
| 321 | RET2RVAL(ret, rvalue); |
| 322 | |
| 323 | /* |
| 324 | * Fix tagging of top-level types. |
| 325 | */ |
| 326 | ret = asn1f_fix_constr_tag(arg, 1); |
| 327 | RET2RVAL(ret, rvalue); |
| 328 | |
| 329 | /* |
| 330 | * 2.[234] Process SEQUENCE/SET/CHOICE types. |
| 331 | */ |
| 332 | ret = asn1f_recurse_expr(arg, asn1f_fix_constructed); |
| 333 | RET2RVAL(ret, rvalue); |
| 334 | |
| 335 | /* |
| 336 | * 2.5.5 |
| 337 | */ |
| 338 | ret = asn1f_recurse_expr(arg, asn1f_fix_dereference_values); |
| 339 | RET2RVAL(ret, rvalue); |
| 340 | |
| 341 | /* |
| 342 | * Parse class objects and fill up the object class with data. |
| 343 | */ |
| 344 | ret = asn1f_parse_class_object(arg); |
| 345 | RET2RVAL(ret, rvalue); |
| 346 | |
| 347 | /* |
| 348 | * Resolve references in constraints. |
| 349 | */ |
| 350 | ret = asn1f_recurse_expr(arg, asn1f_resolve_constraints); |
| 351 | RET2RVAL(ret, rvalue); |
| 352 | |
| 353 | /* |
| 354 | * 6. INTEGER value processed at 2.5.4. |
| 355 | */ |
| 356 | |
| 357 | return rvalue; |
| 358 | } |
| 359 | |
| 360 | static int |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 361 | asn1f_fix_simple(arg_t *arg) { |
| 362 | int rvalue = 0; |
| 363 | int ret; |
| 364 | |
| 365 | ret = asn1f_fix_enum(arg); |
| 366 | RET2RVAL(ret, rvalue); |
| 367 | |
| 368 | ret = asn1f_fix_integer(arg); |
| 369 | RET2RVAL(ret, rvalue); |
| 370 | |
| 371 | return rvalue; |
| 372 | } |
| 373 | |
| 374 | static int |
| 375 | asn1f_fix_constructed(arg_t *arg) { |
| 376 | int rvalue = 0; |
| 377 | int ret; |
| 378 | |
| 379 | switch(arg->expr->expr_type) { |
| 380 | case ASN_CONSTR_SEQUENCE: |
| 381 | case ASN_CONSTR_SET: |
| 382 | case ASN_CONSTR_CHOICE: |
| 383 | break; |
| 384 | default: |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | /* Check identifier distinctness */ |
Lev Walkin | fbfc7bc | 2006-08-28 02:45:44 +0000 | [diff] [blame] | 389 | ret = asn1f_check_unique_expr(arg); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 390 | RET2RVAL(ret, rvalue); |
| 391 | |
| 392 | /* Fix extensibility */ |
| 393 | ret = asn1f_fix_constr_ext(arg); |
| 394 | RET2RVAL(ret, rvalue); |
| 395 | |
| 396 | /* Fix tagging */ |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 397 | ret = asn1f_fix_constr_tag(arg, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 398 | RET2RVAL(ret, rvalue); |
| 399 | |
Lev Walkin | 4ec3b4c | 2004-08-22 03:09:24 +0000 | [diff] [blame] | 400 | /* Import COMPONENTS OF stuff */ |
| 401 | ret = asn1f_pull_components_of(arg); |
| 402 | RET2RVAL(ret, rvalue); |
| 403 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 404 | return rvalue; |
| 405 | } |
| 406 | |
| 407 | static int |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 408 | asn1f_resolve_constraints(arg_t *arg) { |
Lev Walkin | b45e067 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 409 | asn1p_expr_t *top_parent; |
Lev Walkin | 5253da4 | 2004-08-20 13:25:56 +0000 | [diff] [blame] | 410 | asn1p_expr_type_e etype; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 411 | int rvalue = 0; |
| 412 | int ret; |
| 413 | |
Lev Walkin | 4ec3b4c | 2004-08-22 03:09:24 +0000 | [diff] [blame] | 414 | top_parent = asn1f_find_terminal_type(arg, arg->expr); |
Lev Walkin | 5253da4 | 2004-08-20 13:25:56 +0000 | [diff] [blame] | 415 | if(top_parent) |
| 416 | etype = top_parent->expr_type; |
| 417 | else etype = A1TC_INVALID; |
| 418 | |
Lev Walkin | 0385018 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 419 | DEBUG("(%s)", arg->expr->Identifier); |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 420 | |
Lev Walkin | 7ec9b4c | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 421 | ret = asn1constraint_resolve(arg, arg->expr->constraints, etype, 0); |
Lev Walkin | b45e067 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 422 | RET2RVAL(ret, rvalue); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 423 | |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 424 | return rvalue; |
| 425 | } |
| 426 | |
| 427 | static int |
| 428 | asn1f_check_constraints(arg_t *arg) { |
| 429 | static enum asn1p_constraint_type_e test_types[] = { |
| 430 | ACT_EL_RANGE, ACT_CT_SIZE, ACT_CT_FROM }; |
| 431 | asn1p_expr_t *top_parent; |
| 432 | asn1cnst_range_t *range; |
| 433 | asn1p_expr_type_e etype; |
| 434 | unsigned int i; |
| 435 | int rvalue = 0; |
| 436 | int ret; |
| 437 | |
Lev Walkin | 0385018 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 438 | DEBUG("(%s{%d/%d})", |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 439 | arg->expr->Identifier, |
| 440 | arg->expr->meta_type, arg->expr->expr_type); |
| 441 | |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 442 | top_parent = asn1f_find_terminal_type(arg, arg->expr); |
| 443 | if(!top_parent) |
| 444 | return 0; |
| 445 | etype = top_parent->expr_type; |
| 446 | |
Lev Walkin | b45e067 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 447 | ret = asn1constraint_pullup(arg); |
| 448 | RET2RVAL(ret, rvalue); |
| 449 | |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 450 | for(i = 0; i < sizeof(test_types)/sizeof(test_types[0]); i++) { |
Lev Walkin | a28cbb9 | 2017-07-31 20:20:17 -0700 | [diff] [blame] | 451 | range = asn1constraint_compute_constraint_range( |
| 452 | arg->expr->Identifier, |
Lev Walkin | b9fa780 | 2004-08-25 02:04:39 +0000 | [diff] [blame] | 453 | etype, |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 454 | arg->expr->combined_constraints, |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 455 | test_types[i], 0, 0, |
| 456 | CPR_noflags /* ignore -fbless-SIZE */); |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 457 | if(!range && errno == EPERM) { |
Lev Walkin | 9288d1c | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 458 | FATAL("This error happened for \"%s\" (meta %d) " |
| 459 | "at line %d", |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 460 | arg->expr->Identifier, |
| 461 | arg->expr->meta_type, |
| 462 | arg->expr->_lineno); |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 463 | return -1; |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 464 | } |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 465 | asn1constraint_range_free(range); |
Lev Walkin | b45e067 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 466 | } |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 467 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 468 | return rvalue; |
| 469 | } |
| 470 | |
Lev Walkin | f593f10 | 2005-02-22 07:59:59 +0000 | [diff] [blame] | 471 | static int |
| 472 | asn1f_check_duplicate(arg_t *arg) { |
| 473 | arg_t tmparg = *arg; |
Lev Walkin | 9c2285a | 2006-03-09 08:49:26 +0000 | [diff] [blame] | 474 | int rvalue = 0; |
Lev Walkin | f593f10 | 2005-02-22 07:59:59 +0000 | [diff] [blame] | 475 | |
| 476 | /* |
| 477 | * This is a linear scan in search of a similar type. |
| 478 | * The linear scan is just fine for the task, no need to over-optimize. |
| 479 | */ |
| 480 | TQ_FOR(tmparg.mod, &arg->asn->modules, mod_next) { |
Lev Walkin | 1a7cbd7 | 2006-03-07 10:39:42 +0000 | [diff] [blame] | 481 | int critical = 1; /* FATAL */ |
| 482 | |
| 483 | if((arg->mod->_tags & MT_STANDARD_MODULE) |
| 484 | != (tmparg.mod->_tags & MT_STANDARD_MODULE)) { |
| 485 | /* Ignore clashes with standard module */ |
| 486 | critical = 0; /* WARNING */ |
| 487 | } |
| 488 | |
Lev Walkin | f593f10 | 2005-02-22 07:59:59 +0000 | [diff] [blame] | 489 | TQ_FOR(tmparg.expr, &(tmparg.mod->members), next) { |
Lev Walkin | 1a7cbd7 | 2006-03-07 10:39:42 +0000 | [diff] [blame] | 490 | int diff_files; /* different files */ |
| 491 | |
Lev Walkin | f593f10 | 2005-02-22 07:59:59 +0000 | [diff] [blame] | 492 | assert(tmparg.expr->Identifier); |
| 493 | assert(arg->expr->Identifier); |
Lev Walkin | 1a7cbd7 | 2006-03-07 10:39:42 +0000 | [diff] [blame] | 494 | |
Lev Walkin | 0c0bca6 | 2006-03-21 04:48:15 +0000 | [diff] [blame] | 495 | if(arg->expr->spec_index != -1) |
| 496 | continue; |
| 497 | |
Lev Walkin | f593f10 | 2005-02-22 07:59:59 +0000 | [diff] [blame] | 498 | if(tmparg.expr == arg->expr) break; |
| 499 | |
| 500 | if(strcmp(tmparg.expr->Identifier, |
Lev Walkin | 1a7cbd7 | 2006-03-07 10:39:42 +0000 | [diff] [blame] | 501 | arg->expr->Identifier)) |
| 502 | continue; |
| 503 | |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 504 | /* resolve clash of Identifier in different modules */ |
| 505 | int oid_exist = (tmparg.expr->module->module_oid && arg->expr->module->module_oid); |
| 506 | if ((!oid_exist && strcmp(tmparg.expr->module->ModuleName, arg->expr->module->ModuleName)) || |
| 507 | (oid_exist && !asn1p_oid_compare(tmparg.expr->module->module_oid, arg->expr->module->module_oid))) { |
| 508 | |
| 509 | tmparg.expr->_mark |= TM_NAMECLASH; |
| 510 | arg->expr->_mark |= TM_NAMECLASH; |
| 511 | continue; |
| 512 | } |
| 513 | |
Lev Walkin | 1a7cbd7 | 2006-03-07 10:39:42 +0000 | [diff] [blame] | 514 | diff_files = strcmp(arg->mod->source_file_name, |
| 515 | tmparg.mod->source_file_name) ? 1 : 0; |
| 516 | |
| 517 | LOG(critical, |
| 518 | "ASN.1 expression \"%s\" at line %d of module %s\n" |
| 519 | "clashes with expression \"%s\" at line %d of module %s" |
| 520 | "%s%s%s.\n" |
Lev Walkin | 41635d3 | 2006-03-18 05:06:57 +0000 | [diff] [blame] | 521 | "Rename or remove either instance " |
| 522 | "to resolve the conflict", |
Lev Walkin | 1a7cbd7 | 2006-03-07 10:39:42 +0000 | [diff] [blame] | 523 | arg->expr->Identifier, |
| 524 | arg->expr->_lineno, |
| 525 | arg->mod->ModuleName, |
| 526 | tmparg.expr->Identifier, |
| 527 | tmparg.expr->_lineno, |
| 528 | tmparg.mod->ModuleName, |
| 529 | diff_files ? " (" : "", |
| 530 | diff_files ? tmparg.mod->source_file_name : "", |
| 531 | diff_files ? ")" : ""); |
| 532 | if(critical) |
Lev Walkin | f593f10 | 2005-02-22 07:59:59 +0000 | [diff] [blame] | 533 | return -1; |
Lev Walkin | 9c2285a | 2006-03-09 08:49:26 +0000 | [diff] [blame] | 534 | RET2RVAL(1, rvalue); |
Lev Walkin | f593f10 | 2005-02-22 07:59:59 +0000 | [diff] [blame] | 535 | } |
| 536 | if(tmparg.mod == arg->mod) break; |
| 537 | } |
| 538 | |
Lev Walkin | 9c2285a | 2006-03-09 08:49:26 +0000 | [diff] [blame] | 539 | return rvalue; |
Lev Walkin | f593f10 | 2005-02-22 07:59:59 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Lev Walkin | 1dec580 | 2005-03-04 09:02:27 +0000 | [diff] [blame] | 542 | static int |
| 543 | asn1f_apply_unique_index(arg_t *arg) { |
| 544 | static int unique_index; |
| 545 | if(!arg) { unique_index = 0; return 0; } |
| 546 | |
Lev Walkin | 1dec580 | 2005-03-04 09:02:27 +0000 | [diff] [blame] | 547 | arg->expr->_type_unique_index = ++unique_index; |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 552 | /* |
| 553 | * Print everything to stderr |
| 554 | */ |
| 555 | static void |
| 556 | _default_error_logger(int _severity, const char *fmt, ...) { |
| 557 | va_list ap; |
| 558 | char *pfx = ""; |
| 559 | |
| 560 | switch(_severity) { |
| 561 | case -1: pfx = "DEBUG: "; break; |
| 562 | case 0: pfx = "WARNING: "; break; |
| 563 | case 1: pfx = "FATAL: "; break; |
| 564 | } |
| 565 | |
| 566 | fprintf(stderr, "%s", pfx); |
| 567 | va_start(ap, fmt); |
| 568 | vfprintf(stderr, fmt, ap); |
| 569 | va_end(ap); |
| 570 | fprintf(stderr, "\n"); |
| 571 | } |