Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1fix_internal.h" |
| 2 | |
| 3 | static int _asn1f_check_if_tag_must_be_explicit(arg_t *arg, asn1p_expr_t *v); |
| 4 | static int _asn1f_compare_tags(arg_t *arg, asn1p_expr_t *a, asn1p_expr_t *b); |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 5 | static int _asn1f_fix_type_tag(arg_t *arg, asn1p_expr_t *expr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | |
Lev Walkin | 4ec3b4c | 2004-08-22 03:09:24 +0000 | [diff] [blame] | 7 | int |
| 8 | asn1f_pull_components_of(arg_t *arg) { |
| 9 | TQ_HEAD(asn1p_expr_t) list; |
| 10 | asn1p_expr_t *expr = arg->expr; |
| 11 | asn1p_expr_t *memb; |
| 12 | int r_value = 0; |
| 13 | |
| 14 | switch(expr->expr_type) { |
| 15 | case ASN_CONSTR_SEQUENCE: |
| 16 | case ASN_CONSTR_SET: |
| 17 | break; |
| 18 | default: |
| 19 | return 0; |
| 20 | } |
| 21 | |
| 22 | TQ_INIT(&list); |
| 23 | |
| 24 | /* |
| 25 | * Look into |
| 26 | */ |
| 27 | while((memb = TQ_REMOVE(&(expr->members), next))) { |
| 28 | asn1p_expr_t *coft; /* COMPONENTS OF thing itself */ |
| 29 | asn1p_expr_t *terminal; /* Terminal of the referenced type */ |
| 30 | |
| 31 | if(memb->expr_type != A1TC_COMPONENTS_OF) { |
| 32 | TQ_ADD(&list, memb, next); |
| 33 | continue; |
| 34 | } |
| 35 | |
| 36 | coft = TQ_FIRST(&memb->members); |
| 37 | assert(coft); |
| 38 | assert(!TQ_NEXT(coft, next)); |
| 39 | |
| 40 | /* |
| 41 | * Find the referenced type. |
| 42 | */ |
| 43 | terminal = asn1f_find_terminal_type(arg, coft); |
| 44 | if(!terminal || (terminal->expr_type != expr->expr_type)) { |
| 45 | FATAL("COMPONENTS OF at line %d " |
| 46 | "must reference a %s type", |
| 47 | coft->_lineno, |
| 48 | expr->expr_type==ASN_CONSTR_SET |
| 49 | ? "SET" : "SEQUENCE" |
| 50 | ); |
| 51 | TQ_ADD(&list, memb, next); |
| 52 | r_value = -1; |
| 53 | continue; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Clone the final structure. |
| 58 | */ |
| 59 | |
| 60 | coft = asn1p_expr_clone(terminal, 1 /* Skip extensions */); |
| 61 | if(!coft) return -1; /* ENOMEM */ |
| 62 | |
Lev Walkin | 75d76b4 | 2005-04-13 13:20:20 +0000 | [diff] [blame] | 63 | if(0) { |
| 64 | asn1p_expr_free(memb); /* Don't need it anymore*/ |
| 65 | } else { |
| 66 | /* Actual removal clashes with constraints... skip. */ |
| 67 | } |
Lev Walkin | 4ec3b4c | 2004-08-22 03:09:24 +0000 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * Move all components of the cloned structure |
| 71 | * into the current one. |
| 72 | */ |
Lev Walkin | 1004aa9 | 2004-09-08 00:28:11 +0000 | [diff] [blame] | 73 | while((memb = TQ_REMOVE(&(coft->members), next))) { |
Lev Walkin | 4ec3b4c | 2004-08-22 03:09:24 +0000 | [diff] [blame] | 74 | TQ_ADD(&list, memb, next); |
Lev Walkin | 1004aa9 | 2004-09-08 00:28:11 +0000 | [diff] [blame] | 75 | memb->parent_expr = expr; |
| 76 | } |
Lev Walkin | 4ec3b4c | 2004-08-22 03:09:24 +0000 | [diff] [blame] | 77 | |
| 78 | asn1p_expr_free(coft); /* Remove wrapper */ |
| 79 | } |
| 80 | |
| 81 | /* Move the stuff back */ |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 82 | TQ_MOVE(&(expr->members), &list); |
Lev Walkin | 4ec3b4c | 2004-08-22 03:09:24 +0000 | [diff] [blame] | 83 | |
| 84 | return r_value; |
| 85 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 86 | |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 87 | /* |
| 88 | * Fix extensibility parts inside constructed types (SEQUENCE, SET, CHOICE). |
| 89 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 90 | int |
| 91 | asn1f_fix_constr_ext(arg_t *arg) { |
| 92 | asn1p_expr_t *expr = arg->expr; |
| 93 | asn1p_expr_t *v; |
| 94 | TQ_HEAD(asn1p_expr_t) root_list; |
| 95 | TQ_HEAD(asn1p_expr_t) ext_list; |
| 96 | TQ_HEAD(asn1p_expr_t) *cur_list; |
| 97 | int r_value = 0; |
| 98 | int ext_count = 0; |
| 99 | |
| 100 | switch(expr->expr_type) { |
| 101 | case ASN_CONSTR_SEQUENCE: |
| 102 | case ASN_CONSTR_SET: |
| 103 | case ASN_CONSTR_CHOICE: |
| 104 | break; |
| 105 | default: |
| 106 | return 0; |
| 107 | } |
| 108 | |
Lev Walkin | 0385018 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 109 | DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | |
| 111 | TQ_INIT(&root_list); |
| 112 | TQ_INIT(&ext_list); |
| 113 | cur_list = (void *)&root_list; |
| 114 | |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 115 | /* |
| 116 | * Split the set of fields into two lists, the root list |
| 117 | * and the extensions list. |
| 118 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 119 | while((v = TQ_REMOVE(&(expr->members), next))) { |
| 120 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 121 | ext_count++; |
| 122 | switch(ext_count) { |
| 123 | case 1: cur_list = (void *)&ext_list; break; |
| 124 | case 2: |
| 125 | cur_list = (void *)&root_list; |
| 126 | if(v->value) { |
| 127 | FATAL("Optional extension marker " |
| 128 | "must not contain " |
| 129 | "an exception mark " |
| 130 | "at line %d", v->_lineno); |
| 131 | r_value = -1; |
| 132 | } |
| 133 | asn1p_expr_free(v); |
| 134 | continue; |
| 135 | case 3: |
| 136 | FATAL("Third extension marker " |
| 137 | "is not allowed at line %d", v->_lineno); |
| 138 | default: |
| 139 | r_value = -1; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | TQ_ADD(cur_list, v, next); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Copy the root list and extension list back into the main list. |
| 148 | */ |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 149 | TQ_MOVE(&(expr->members), &root_list); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 150 | while((v = TQ_REMOVE(&ext_list, next))) |
| 151 | TQ_ADD(&(expr->members), v, next); |
| 152 | |
| 153 | if(arg->mod->module_flags & MSF_EXTENSIBILITY_IMPLIED |
Lev Walkin | 1ef0516 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 154 | && ext_count == 0) { |
Lev Walkin | a9532f4 | 2006-09-17 04:52:50 +0000 | [diff] [blame] | 155 | v = asn1p_expr_new(0, arg->mod); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 156 | if(v) { |
| 157 | v->Identifier = strdup("..."); |
| 158 | v->expr_type = A1TC_EXTENSIBLE; |
| 159 | v->meta_type = AMT_TYPE; |
Lev Walkin | 7ccf2a4 | 2004-07-01 00:51:31 +0000 | [diff] [blame] | 160 | v->_lineno = expr->_lineno; /* The best we can do */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 161 | if(v->Identifier == NULL) { |
| 162 | asn1p_expr_free(v); |
| 163 | r_value = -1; |
| 164 | } else { |
Lev Walkin | 1004aa9 | 2004-09-08 00:28:11 +0000 | [diff] [blame] | 165 | asn1p_expr_add(expr, v); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 166 | } |
| 167 | } else { |
| 168 | r_value = -1; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return r_value; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | int |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 177 | asn1f_fix_constr_tag(arg_t *arg, int fix_top_level) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 178 | asn1p_expr_t *expr = arg->expr; |
| 179 | asn1p_expr_t *v; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 180 | int root_tagged = 0; /* The root component is manually tagged */ |
| 181 | int ext_tagged = 0; /* The extensions are manually tagged */ |
| 182 | int component_number = 0; |
| 183 | int r_value = 0; |
| 184 | |
Lev Walkin | 0385018 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 185 | DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno); |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * Fix the top-level type itself first. |
| 189 | */ |
| 190 | if(fix_top_level) { |
| 191 | if(expr->tag.tag_class == TC_NOCLASS) |
| 192 | return r_value; |
| 193 | |
| 194 | if(_asn1f_fix_type_tag(arg, expr)) |
| 195 | r_value = -1; |
| 196 | |
| 197 | return r_value; |
| 198 | } |
| 199 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 200 | switch(expr->expr_type) { |
| 201 | case ASN_CONSTR_SEQUENCE: |
| 202 | case ASN_CONSTR_SET: |
| 203 | case ASN_CONSTR_CHOICE: |
| 204 | break; |
| 205 | default: |
| 206 | return 0; |
| 207 | } |
| 208 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 209 | TQ_FOR(v, &(expr->members), next) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | |
| 211 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 212 | component_number++; |
| 213 | continue; |
| 214 | } |
| 215 | |
| 216 | if(v->tag.tag_class == TC_NOCLASS) { |
| 217 | continue; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 220 | switch(component_number) { |
| 221 | case 0: case 2: |
| 222 | root_tagged = 1; break; |
| 223 | default: |
| 224 | ext_tagged = 1; break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 227 | if(_asn1f_fix_type_tag(arg, v)) |
| 228 | r_value = -1; |
| 229 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Lev Walkin | d43915b | 2004-09-15 11:48:34 +0000 | [diff] [blame] | 232 | if((arg->mod->module_flags & MSF_AUTOMATIC_TAGS) |
| 233 | && !root_tagged) { |
| 234 | if(ext_tagged) { |
| 235 | /* X.690: 28.4 */ |
| 236 | FATAL("In %s at line %d: " |
| 237 | "extensions are tagged " |
| 238 | "but root components are not", |
| 239 | expr->Identifier, expr->_lineno); |
| 240 | r_value = -1; |
| 241 | } else { |
| 242 | /* Make a decision on automatic tagging */ |
| 243 | expr->auto_tags_OK = 1; |
| 244 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | return r_value; |
| 248 | } |
| 249 | |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 250 | static int |
| 251 | _asn1f_fix_type_tag(arg_t *arg, asn1p_expr_t *expr) { |
| 252 | int must_explicit = _asn1f_check_if_tag_must_be_explicit(arg, expr); |
Lev Walkin | e54e942 | 2005-07-21 01:18:19 +0000 | [diff] [blame] | 253 | int module_impl_tags = (arg->mod->module_flags |
| 254 | & (MSF_IMPLICIT_TAGS | MSF_AUTOMATIC_TAGS)); |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 255 | int r_value = 0; |
| 256 | |
Lev Walkin | 07f388c | 2004-10-11 11:43:08 +0000 | [diff] [blame] | 257 | if(expr->tag.tag_mode == TM_DEFAULT) { |
| 258 | if(must_explicit || module_impl_tags == 0) |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 259 | expr->tag.tag_mode = TM_EXPLICIT; |
Lev Walkin | 07f388c | 2004-10-11 11:43:08 +0000 | [diff] [blame] | 260 | else |
| 261 | expr->tag.tag_mode = TM_IMPLICIT; |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Perform a final sanity check. |
| 266 | */ |
| 267 | if(must_explicit) { |
| 268 | if(expr->tag.tag_mode == TM_IMPLICIT) { |
| 269 | FATAL("%s tagged in IMPLICIT mode " |
| 270 | "but must be EXPLICIT at line %d", |
| 271 | expr->Identifier, expr->_lineno); |
| 272 | r_value = -1; |
| 273 | } else { |
| 274 | expr->tag.tag_mode = TM_EXPLICIT; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return r_value; |
| 279 | } |
| 280 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 281 | int |
| 282 | asn1f_fix_constr_autotag(arg_t *arg) { |
| 283 | asn1p_expr_t *expr = arg->expr; |
| 284 | asn1p_expr_t *v; |
Lev Walkin | b8108ec | 2004-09-29 13:17:17 +0000 | [diff] [blame] | 285 | asn1c_integer_t tag_value = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 286 | int r_value = 0; |
| 287 | |
| 288 | switch(expr->expr_type) { |
| 289 | case ASN_CONSTR_SEQUENCE: |
| 290 | case ASN_CONSTR_SET: |
| 291 | case ASN_CONSTR_CHOICE: |
| 292 | if(expr->auto_tags_OK) |
| 293 | break; |
| 294 | /* Automatic tagging is not applicable */ |
| 295 | /* Fall through */ |
| 296 | default: |
| 297 | return 0; |
| 298 | } |
| 299 | |
Lev Walkin | 0385018 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 300 | DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 301 | |
| 302 | TQ_FOR(v, &(expr->members), next) { |
| 303 | int must_explicit; |
| 304 | |
Lev Walkin | 144db9b | 2004-10-12 23:26:53 +0000 | [diff] [blame] | 305 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 306 | /* 28.5, d) */ |
| 307 | continue; |
| 308 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 309 | |
Lev Walkin | 4ec3b4c | 2004-08-22 03:09:24 +0000 | [diff] [blame] | 310 | if(0) { |
| 311 | /* This may be not true in case COMPONENTS OF */ |
| 312 | assert(v->tag.tag_class == TC_NOCLASS); |
| 313 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 314 | |
| 315 | must_explicit = _asn1f_check_if_tag_must_be_explicit(arg, v); |
| 316 | |
| 317 | v->tag.tag_class = TC_CONTEXT_SPECIFIC; |
| 318 | v->tag.tag_mode = must_explicit ? TM_EXPLICIT : TM_IMPLICIT; |
| 319 | v->tag.tag_value = tag_value++; |
| 320 | } |
| 321 | |
| 322 | return r_value; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Check that tags are distinct. |
| 327 | */ |
| 328 | int |
| 329 | asn1f_check_constr_tags_distinct(arg_t *arg) { |
| 330 | asn1p_expr_t *expr = arg->expr; |
| 331 | asn1p_expr_t *v; |
| 332 | int r_value = 0; |
| 333 | |
| 334 | switch(expr->expr_type) { |
| 335 | case ASN_CONSTR_SEQUENCE: |
| 336 | case ASN_CONSTR_SET: |
| 337 | case ASN_CONSTR_CHOICE: |
| 338 | break; |
| 339 | default: |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | TQ_FOR(v, &(expr->members), next) { |
| 344 | /* |
| 345 | * In every series of non-mandatory components, |
| 346 | * the tags must be distinct from each other AND the |
| 347 | * tag of the following mandatory component. |
| 348 | * For SET and CHOICE treat everything as a big set of |
| 349 | * non-mandatory components. |
| 350 | */ |
Lev Walkin | d43915b | 2004-09-15 11:48:34 +0000 | [diff] [blame] | 351 | if(expr->expr_type != ASN_CONSTR_SEQUENCE || v->marker.flags) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 352 | asn1p_expr_t *nv; |
| 353 | for(nv = v; (nv = TQ_NEXT(nv, next));) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 354 | DEBUG("S/C comparing tags %s s. %s", |
| 355 | v->Identifier, nv->Identifier); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 356 | if(_asn1f_compare_tags(arg, v, nv)) |
| 357 | r_value = -1; |
| 358 | if(expr->expr_type == ASN_CONSTR_SEQUENCE |
Lev Walkin | d43915b | 2004-09-15 11:48:34 +0000 | [diff] [blame] | 359 | && !nv->marker.flags) break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | return r_value; |
| 365 | } |
| 366 | |
| 367 | static int |
| 368 | _asn1f_check_if_tag_must_be_explicit(arg_t *arg, asn1p_expr_t *v) { |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 369 | struct asn1p_type_tag_s tag; |
| 370 | struct asn1p_type_tag_s save_tag; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 371 | asn1p_expr_t *reft; |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 372 | int ret; |
| 373 | |
| 374 | /* |
| 375 | * Fetch the _next_ tag for this type. |
| 376 | */ |
| 377 | save_tag = v->tag; /* Save existing tag */ |
| 378 | memset(&v->tag, 0, sizeof(v->tag)); /* Remove it temporarily */ |
Lev Walkin | 8945e0e | 2004-09-10 06:07:04 +0000 | [diff] [blame] | 379 | ret = asn1f_fetch_outmost_tag(arg->asn, arg->mod, v, &tag, 0); |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 380 | v->tag = save_tag; /* Restore the tag back */ |
| 381 | |
| 382 | if(ret == 0) return 0; /* If found tag, it's okay */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 383 | |
Lev Walkin | 4ec3b4c | 2004-08-22 03:09:24 +0000 | [diff] [blame] | 384 | reft = asn1f_find_terminal_type(arg, v); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 385 | if(reft) { |
| 386 | switch(reft->expr_type) { |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 387 | case ASN_TYPE_ANY: |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 388 | case ASN_CONSTR_CHOICE: |
| 389 | return 1; |
| 390 | default: |
| 391 | return 0; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Check that the tags are distinct. |
| 400 | */ |
| 401 | static int |
| 402 | _asn1f_compare_tags(arg_t *arg, asn1p_expr_t *a, asn1p_expr_t *b) { |
| 403 | struct asn1p_type_tag_s ta, tb; |
| 404 | int ra, rb; |
| 405 | int ret; |
| 406 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 407 | ra = asn1f_fetch_outmost_tag(arg->asn, arg->mod, a, |
| 408 | &ta, AFT_IMAGINARY_ANY); |
| 409 | rb = asn1f_fetch_outmost_tag(arg->asn, arg->mod, b, |
| 410 | &tb, AFT_IMAGINARY_ANY); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * If both tags are explicitly or implicitly given, use them. |
| 414 | */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 415 | DEBUG("Fetching outmost tags: %d, %d", ra, rb); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 416 | if(ra == 0 && rb == 0) { |
| 417 | /* |
| 418 | * Simple case: fetched both tags. |
| 419 | */ |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 420 | |
| 421 | if((ta.tag_value == tb.tag_value |
| 422 | && ta.tag_class == tb.tag_class) |
| 423 | || ta.tag_value == -1 /* Spread IMAGINARY ANY tag... */ |
| 424 | || tb.tag_value == -1 /* ...it is an evil virus, fear it! */ |
| 425 | ) { |
Lev Walkin | 7116096 | 2005-06-02 05:21:53 +0000 | [diff] [blame] | 426 | char tagbuf[2][TAG2STRING_BUFFER_SIZE]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 427 | char *p = (a->expr_type == A1TC_EXTENSIBLE) |
| 428 | ?"potentially ":""; |
Lev Walkin | d43915b | 2004-09-15 11:48:34 +0000 | [diff] [blame] | 429 | FATAL("Processing %s at line %d: component \"%s\" at line %d %shas the same tag " |
Lev Walkin | 7bb4ad0 | 2008-01-01 20:04:52 +0000 | [diff] [blame] | 430 | "as component \"%s\" at line %d", |
Lev Walkin | d43915b | 2004-09-15 11:48:34 +0000 | [diff] [blame] | 431 | arg->expr->Identifier, |
| 432 | arg->expr->_lineno, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 433 | a->Identifier, |
| 434 | a->_lineno, |
| 435 | p, |
| 436 | b->Identifier, |
| 437 | b->_lineno |
| 438 | ); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 439 | FATAL("Consider adding AUTOMATIC TAGS " |
| 440 | "after module %s DEFINITIONS, " |
| 441 | "or manually tag components", |
| 442 | arg->expr->module->ModuleName); |
Lev Walkin | 7116096 | 2005-06-02 05:21:53 +0000 | [diff] [blame] | 443 | DEBUG("Tags: %s %s vs. %s %s", |
| 444 | asn1p_tag2string(&ta, tagbuf[0]), |
| 445 | a->Identifier, |
| 446 | asn1p_tag2string(&tb, tagbuf[1]), |
| 447 | b->Identifier |
| 448 | ); |
Lev Walkin | 7ccf2a4 | 2004-07-01 00:51:31 +0000 | [diff] [blame] | 449 | if((arg->mod->module_flags & MSF_EXTENSIBILITY_IMPLIED) |
| 450 | && (a->expr_type == A1TC_EXTENSIBLE) |
| 451 | && (b->expr_type == A1TC_EXTENSIBLE)) { |
| 452 | FATAL("The previous error is due to " |
| 453 | "improper use of " |
| 454 | "EXTENSIBILITY IMPLIED flag " |
| 455 | "of module %s", |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 456 | arg->mod->ModuleName); |
Lev Walkin | 7ccf2a4 | 2004-07-01 00:51:31 +0000 | [diff] [blame] | 457 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 458 | return -1; |
| 459 | } else { |
| 460 | /* Tags are distinct */ |
| 461 | return 0; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /********************************************************** |
| 466 | * Now we must perform some very funny recursion to check |
| 467 | * multiple components of CHOICE type, etc. |
| 468 | */ |
| 469 | |
| 470 | DEBUG("Comparing tags %s:%x <-> %s:%x", |
| 471 | a->Identifier, a->expr_type, |
| 472 | b->Identifier, b->expr_type); |
| 473 | |
Lev Walkin | d43915b | 2004-09-15 11:48:34 +0000 | [diff] [blame] | 474 | if(ra && a->meta_type == AMT_TYPEREF) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 475 | |
| 476 | DEBUG(" %s is a type reference", a->Identifier); |
| 477 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 478 | a = asn1f_lookup_symbol(arg, |
| 479 | a->module, a->rhs_pspecs, a->reference); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 480 | if(!a) return 0; /* Already FATAL()'ed somewhere else */ |
Lev Walkin | 4ec3b4c | 2004-08-22 03:09:24 +0000 | [diff] [blame] | 481 | WITH_MODULE(a->module, ret = _asn1f_compare_tags(arg, a, b)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 482 | return ret; |
| 483 | } |
| 484 | |
Lev Walkin | d43915b | 2004-09-15 11:48:34 +0000 | [diff] [blame] | 485 | if(ra && a->expr_type == ASN_CONSTR_CHOICE) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 486 | asn1p_expr_t *v; |
| 487 | |
| 488 | DEBUG(" %s is a choice type (%d)", a->Identifier, a->_mark); |
| 489 | |
| 490 | /* |
| 491 | * Iterate over members of CHOICE. |
| 492 | */ |
| 493 | //if(a->_mark & TM_RECURSION) return 0; |
| 494 | TQ_FOR(v, &(a->members), next) { |
| 495 | //a->_mark |= TM_RECURSION; |
| 496 | ret = _asn1f_compare_tags(arg, v, b); |
| 497 | //a->_mark &= ~TM_RECURSION; |
| 498 | if(ret) return ret; |
| 499 | } |
| 500 | return 0; |
| 501 | } |
| 502 | |
Lev Walkin | d43915b | 2004-09-15 11:48:34 +0000 | [diff] [blame] | 503 | if(rb && b->expr_type == ASN_CONSTR_CHOICE) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 504 | return _asn1f_compare_tags(arg, b, a); |
| 505 | } |
| 506 | |
| 507 | if(a->_mark & TM_RECURSION) return 0; |
| 508 | if(b->_mark & TM_RECURSION) return 0; |
| 509 | a->_mark |= TM_RECURSION; |
| 510 | b->_mark |= TM_RECURSION; |
| 511 | ret = _asn1f_compare_tags(arg, b, a); |
| 512 | a->_mark &= ~TM_RECURSION; |
| 513 | b->_mark &= ~TM_RECURSION; |
| 514 | |
| 515 | return ret; |
| 516 | } |
| 517 | |