blob: ea2fcf1f62197eff476b57dad827f8b3674b0afd [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#include "asn1fix_internal.h"
2
3static int _asn1f_check_if_tag_must_be_explicit(arg_t *arg, asn1p_expr_t *v);
4static int _asn1f_compare_tags(arg_t *arg, asn1p_expr_t *a, asn1p_expr_t *b);
vlm76142452004-09-05 10:36:22 +00005static int _asn1f_fix_type_tag(arg_t *arg, asn1p_expr_t *expr);
vlmfa67ddc2004-06-03 03:38:44 +00006
vlmf645aff2004-08-22 03:09:24 +00007int
8asn1f_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
vlm318bc272005-04-13 13:20:20 +000063 if(0) {
64 asn1p_expr_free(memb); /* Don't need it anymore*/
65 } else {
66 /* Actual removal clashes with constraints... skip. */
67 }
vlmf645aff2004-08-22 03:09:24 +000068
69 /*
70 * Move all components of the cloned structure
71 * into the current one.
72 */
vlm6a02a8a2004-09-08 00:28:11 +000073 while((memb = TQ_REMOVE(&(coft->members), next))) {
vlmf645aff2004-08-22 03:09:24 +000074 TQ_ADD(&list, memb, next);
vlm6a02a8a2004-09-08 00:28:11 +000075 memb->parent_expr = expr;
76 }
vlmf645aff2004-08-22 03:09:24 +000077
78 asn1p_expr_free(coft); /* Remove wrapper */
79 }
80
81 /* Move the stuff back */
vlmb6fd3b22004-08-25 00:42:25 +000082 TQ_MOVE(&(expr->members), &list);
vlmf645aff2004-08-22 03:09:24 +000083
84 return r_value;
85}
vlmfa67ddc2004-06-03 03:38:44 +000086
vlm76142452004-09-05 10:36:22 +000087/*
88 * Fix extensibility parts inside constructed types (SEQUENCE, SET, CHOICE).
89 */
vlmfa67ddc2004-06-03 03:38:44 +000090int
91asn1f_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
vlmfd245932005-03-10 10:02:50 +0000109 DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno);
vlmfa67ddc2004-06-03 03:38:44 +0000110
111 TQ_INIT(&root_list);
112 TQ_INIT(&ext_list);
113 cur_list = (void *)&root_list;
114
vlm76142452004-09-05 10:36:22 +0000115 /*
116 * Split the set of fields into two lists, the root list
117 * and the extensions list.
118 */
vlmfa67ddc2004-06-03 03:38:44 +0000119 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 */
vlmb6fd3b22004-08-25 00:42:25 +0000149 TQ_MOVE(&(expr->members), &root_list);
vlmfa67ddc2004-06-03 03:38:44 +0000150 while((v = TQ_REMOVE(&ext_list, next)))
151 TQ_ADD(&(expr->members), v, next);
152
153 if(arg->mod->module_flags & MSF_EXTENSIBILITY_IMPLIED
vlmb6fd3b22004-08-25 00:42:25 +0000154 && ext_count == 0) {
vlmfa67ddc2004-06-03 03:38:44 +0000155 v = asn1p_expr_new(0);
156 if(v) {
157 v->Identifier = strdup("...");
158 v->expr_type = A1TC_EXTENSIBLE;
159 v->meta_type = AMT_TYPE;
vlm4f4f56d2004-07-01 00:51:31 +0000160 v->_lineno = expr->_lineno; /* The best we can do */
vlmfa67ddc2004-06-03 03:38:44 +0000161 if(v->Identifier == NULL) {
162 asn1p_expr_free(v);
163 r_value = -1;
164 } else {
vlm6a02a8a2004-09-08 00:28:11 +0000165 asn1p_expr_add(expr, v);
vlmfa67ddc2004-06-03 03:38:44 +0000166 }
167 } else {
168 r_value = -1;
169 }
170 }
171
172 return r_value;
173}
174
175
176int
vlm76142452004-09-05 10:36:22 +0000177asn1f_fix_constr_tag(arg_t *arg, int fix_top_level) {
vlmfa67ddc2004-06-03 03:38:44 +0000178 asn1p_expr_t *expr = arg->expr;
179 asn1p_expr_t *v;
vlmfa67ddc2004-06-03 03:38:44 +0000180 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
vlmfd245932005-03-10 10:02:50 +0000185 DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno);
vlm76142452004-09-05 10:36:22 +0000186
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
vlmfa67ddc2004-06-03 03:38:44 +0000200 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
vlmfa67ddc2004-06-03 03:38:44 +0000209 TQ_FOR(v, &(expr->members), next) {
vlmfa67ddc2004-06-03 03:38:44 +0000210
211 if(v->expr_type == A1TC_EXTENSIBLE) {
212 component_number++;
213 continue;
214 }
215
216 if(v->tag.tag_class == TC_NOCLASS) {
217 continue;
vlmfa67ddc2004-06-03 03:38:44 +0000218 }
219
vlm76142452004-09-05 10:36:22 +0000220 switch(component_number) {
221 case 0: case 2:
222 root_tagged = 1; break;
223 default:
224 ext_tagged = 1; break;
vlmfa67ddc2004-06-03 03:38:44 +0000225 }
226
vlm76142452004-09-05 10:36:22 +0000227 if(_asn1f_fix_type_tag(arg, v))
228 r_value = -1;
229
vlmfa67ddc2004-06-03 03:38:44 +0000230 }
231
vlm387a0882004-09-15 11:48:34 +0000232 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 }
vlmfa67ddc2004-06-03 03:38:44 +0000245 }
246
247 return r_value;
248}
249
vlm76142452004-09-05 10:36:22 +0000250static 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);
vlmb012da82005-07-21 01:18:19 +0000253 int module_impl_tags = (arg->mod->module_flags
254 & (MSF_IMPLICIT_TAGS | MSF_AUTOMATIC_TAGS));
vlm76142452004-09-05 10:36:22 +0000255 int r_value = 0;
256
vlm267b7fd2004-10-11 11:43:08 +0000257 if(expr->tag.tag_mode == TM_DEFAULT) {
258 if(must_explicit || module_impl_tags == 0)
vlm76142452004-09-05 10:36:22 +0000259 expr->tag.tag_mode = TM_EXPLICIT;
vlm267b7fd2004-10-11 11:43:08 +0000260 else
261 expr->tag.tag_mode = TM_IMPLICIT;
vlm76142452004-09-05 10:36:22 +0000262 }
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
vlmfa67ddc2004-06-03 03:38:44 +0000281int
282asn1f_fix_constr_autotag(arg_t *arg) {
283 asn1p_expr_t *expr = arg->expr;
284 asn1p_expr_t *v;
vlmd5b3bf32004-09-29 13:17:17 +0000285 asn1c_integer_t tag_value = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000286 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
vlmfd245932005-03-10 10:02:50 +0000300 DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno);
vlmfa67ddc2004-06-03 03:38:44 +0000301
302 TQ_FOR(v, &(expr->members), next) {
303 int must_explicit;
304
vlm0aa86902004-10-12 23:26:53 +0000305 if(v->expr_type == A1TC_EXTENSIBLE) {
306 /* 28.5, d) */
307 continue;
308 }
vlmfa67ddc2004-06-03 03:38:44 +0000309
vlmf645aff2004-08-22 03:09:24 +0000310 if(0) {
311 /* This may be not true in case COMPONENTS OF */
312 assert(v->tag.tag_class == TC_NOCLASS);
313 }
vlmfa67ddc2004-06-03 03:38:44 +0000314
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 */
328int
329asn1f_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:
vlm337167e2005-11-26 11:25:14 +0000338 DEBUG("Checking tags of members of constructed types");
vlmfa67ddc2004-06-03 03:38:44 +0000339 break;
340 default:
341 return 0;
342 }
343
344 TQ_FOR(v, &(expr->members), next) {
345 /*
346 * In every series of non-mandatory components,
347 * the tags must be distinct from each other AND the
348 * tag of the following mandatory component.
349 * For SET and CHOICE treat everything as a big set of
350 * non-mandatory components.
351 */
vlm387a0882004-09-15 11:48:34 +0000352 if(expr->expr_type != ASN_CONSTR_SEQUENCE || v->marker.flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000353 asn1p_expr_t *nv;
354 for(nv = v; (nv = TQ_NEXT(nv, next));) {
vlm337167e2005-11-26 11:25:14 +0000355 DEBUG("S/C comparing tags %s s. %s",
356 v->Identifier, nv->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +0000357 if(_asn1f_compare_tags(arg, v, nv))
358 r_value = -1;
359 if(expr->expr_type == ASN_CONSTR_SEQUENCE
vlm387a0882004-09-15 11:48:34 +0000360 && !nv->marker.flags) break;
vlmfa67ddc2004-06-03 03:38:44 +0000361 }
362 }
363 }
364
365 return r_value;
366}
367
368static int
369_asn1f_check_if_tag_must_be_explicit(arg_t *arg, asn1p_expr_t *v) {
vlm76142452004-09-05 10:36:22 +0000370 struct asn1p_type_tag_s tag;
371 struct asn1p_type_tag_s save_tag;
vlmfa67ddc2004-06-03 03:38:44 +0000372 asn1p_expr_t *reft;
vlm76142452004-09-05 10:36:22 +0000373 int ret;
374
375 /*
376 * Fetch the _next_ tag for this type.
377 */
378 save_tag = v->tag; /* Save existing tag */
379 memset(&v->tag, 0, sizeof(v->tag)); /* Remove it temporarily */
vlm8fbd5622004-09-10 06:07:04 +0000380 ret = asn1f_fetch_outmost_tag(arg->asn, arg->mod, v, &tag, 0);
vlm76142452004-09-05 10:36:22 +0000381 v->tag = save_tag; /* Restore the tag back */
382
383 if(ret == 0) return 0; /* If found tag, it's okay */
vlmfa67ddc2004-06-03 03:38:44 +0000384
vlmf645aff2004-08-22 03:09:24 +0000385 reft = asn1f_find_terminal_type(arg, v);
vlmfa67ddc2004-06-03 03:38:44 +0000386 if(reft) {
387 switch(reft->expr_type) {
vlm76142452004-09-05 10:36:22 +0000388 case ASN_TYPE_ANY:
vlmfa67ddc2004-06-03 03:38:44 +0000389 case ASN_CONSTR_CHOICE:
390 return 1;
391 default:
392 return 0;
393 }
394 }
395
396 return 0;
397}
398
399/*
400 * Check that the tags are distinct.
401 */
402static int
403_asn1f_compare_tags(arg_t *arg, asn1p_expr_t *a, asn1p_expr_t *b) {
404 struct asn1p_type_tag_s ta, tb;
405 int ra, rb;
406 int ret;
407
vlm337167e2005-11-26 11:25:14 +0000408 ra = asn1f_fetch_outmost_tag(arg->asn, arg->mod, a,
409 &ta, AFT_IMAGINARY_ANY);
410 rb = asn1f_fetch_outmost_tag(arg->asn, arg->mod, b,
411 &tb, AFT_IMAGINARY_ANY);
vlmfa67ddc2004-06-03 03:38:44 +0000412
413 /*
414 * If both tags are explicitly or implicitly given, use them.
415 */
vlm337167e2005-11-26 11:25:14 +0000416 DEBUG("Fetching outmost tags: %d, %d", ra, rb);
vlmfa67ddc2004-06-03 03:38:44 +0000417 if(ra == 0 && rb == 0) {
418 /*
419 * Simple case: fetched both tags.
420 */
vlm76142452004-09-05 10:36:22 +0000421
422 if((ta.tag_value == tb.tag_value
423 && ta.tag_class == tb.tag_class)
424 || ta.tag_value == -1 /* Spread IMAGINARY ANY tag... */
425 || tb.tag_value == -1 /* ...it is an evil virus, fear it! */
426 ) {
vlm5b103032005-06-02 05:21:53 +0000427 char tagbuf[2][TAG2STRING_BUFFER_SIZE];
vlmfa67ddc2004-06-03 03:38:44 +0000428 char *p = (a->expr_type == A1TC_EXTENSIBLE)
429 ?"potentially ":"";
vlm387a0882004-09-15 11:48:34 +0000430 FATAL("Processing %s at line %d: component \"%s\" at line %d %shas the same tag "
vlmfa67ddc2004-06-03 03:38:44 +0000431 "with component \"%s\" at line %d",
vlm387a0882004-09-15 11:48:34 +0000432 arg->expr->Identifier,
433 arg->expr->_lineno,
vlmfa67ddc2004-06-03 03:38:44 +0000434 a->Identifier,
435 a->_lineno,
436 p,
437 b->Identifier,
438 b->_lineno
439 );
vlm5b103032005-06-02 05:21:53 +0000440 DEBUG("Tags: %s %s vs. %s %s",
441 asn1p_tag2string(&ta, tagbuf[0]),
442 a->Identifier,
443 asn1p_tag2string(&tb, tagbuf[1]),
444 b->Identifier
445 );
vlm4f4f56d2004-07-01 00:51:31 +0000446 if((arg->mod->module_flags & MSF_EXTENSIBILITY_IMPLIED)
447 && (a->expr_type == A1TC_EXTENSIBLE)
448 && (b->expr_type == A1TC_EXTENSIBLE)) {
449 FATAL("The previous error is due to "
450 "improper use of "
451 "EXTENSIBILITY IMPLIED flag "
452 "of module %s",
vlm931aeed2005-08-12 10:09:10 +0000453 arg->mod->ModuleName);
vlm4f4f56d2004-07-01 00:51:31 +0000454 }
vlmfa67ddc2004-06-03 03:38:44 +0000455 return -1;
456 } else {
457 /* Tags are distinct */
458 return 0;
459 }
460 }
461
462 /**********************************************************
463 * Now we must perform some very funny recursion to check
464 * multiple components of CHOICE type, etc.
465 */
466
467 DEBUG("Comparing tags %s:%x <-> %s:%x",
468 a->Identifier, a->expr_type,
469 b->Identifier, b->expr_type);
470
vlm387a0882004-09-15 11:48:34 +0000471 if(ra && a->meta_type == AMT_TYPEREF) {
vlmfa67ddc2004-06-03 03:38:44 +0000472
473 DEBUG(" %s is a type reference", a->Identifier);
474
vlmf645aff2004-08-22 03:09:24 +0000475 a = asn1f_lookup_symbol(arg, a->module, a->reference);
vlmfa67ddc2004-06-03 03:38:44 +0000476 if(!a) return 0; /* Already FATAL()'ed somewhere else */
vlmf645aff2004-08-22 03:09:24 +0000477 WITH_MODULE(a->module, ret = _asn1f_compare_tags(arg, a, b));
vlmfa67ddc2004-06-03 03:38:44 +0000478 return ret;
479 }
480
vlm387a0882004-09-15 11:48:34 +0000481 if(ra && a->expr_type == ASN_CONSTR_CHOICE) {
vlmfa67ddc2004-06-03 03:38:44 +0000482 asn1p_expr_t *v;
483
484 DEBUG(" %s is a choice type (%d)", a->Identifier, a->_mark);
485
486 /*
487 * Iterate over members of CHOICE.
488 */
489 //if(a->_mark & TM_RECURSION) return 0;
490 TQ_FOR(v, &(a->members), next) {
491 //a->_mark |= TM_RECURSION;
492 ret = _asn1f_compare_tags(arg, v, b);
493 //a->_mark &= ~TM_RECURSION;
494 if(ret) return ret;
495 }
496 return 0;
497 }
498
vlm387a0882004-09-15 11:48:34 +0000499 if(rb && b->expr_type == ASN_CONSTR_CHOICE) {
vlmfa67ddc2004-06-03 03:38:44 +0000500 return _asn1f_compare_tags(arg, b, a);
501 }
502
503 if(a->_mark & TM_RECURSION) return 0;
504 if(b->_mark & TM_RECURSION) return 0;
505 a->_mark |= TM_RECURSION;
506 b->_mark |= TM_RECURSION;
507 ret = _asn1f_compare_tags(arg, b, a);
508 a->_mark &= ~TM_RECURSION;
509 b->_mark &= ~TM_RECURSION;
510
511 return ret;
512}
513