blob: 7adbcaca66e7ae7830747c3ec8684f2539276125 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#include "asn1fix_internal.h"
2
vlm8fbd5622004-09-10 06:07:04 +00003#define AFT_MAGIC_ANY 1 /* _fetch_tag() flag */
vlm76142452004-09-05 10:36:22 +00004
vlmfa67ddc2004-06-03 03:38:44 +00005static int _asn1f_check_if_tag_must_be_explicit(arg_t *arg, asn1p_expr_t *v);
6static int _asn1f_compare_tags(arg_t *arg, asn1p_expr_t *a, asn1p_expr_t *b);
vlm76142452004-09-05 10:36:22 +00007static int _asn1f_fix_type_tag(arg_t *arg, asn1p_expr_t *expr);
vlmfa67ddc2004-06-03 03:38:44 +00008
vlmf645aff2004-08-22 03:09:24 +00009int
10asn1f_pull_components_of(arg_t *arg) {
11 TQ_HEAD(asn1p_expr_t) list;
12 asn1p_expr_t *expr = arg->expr;
13 asn1p_expr_t *memb;
14 int r_value = 0;
15
16 switch(expr->expr_type) {
17 case ASN_CONSTR_SEQUENCE:
18 case ASN_CONSTR_SET:
19 break;
20 default:
21 return 0;
22 }
23
24 TQ_INIT(&list);
25
26 /*
27 * Look into
28 */
29 while((memb = TQ_REMOVE(&(expr->members), next))) {
30 asn1p_expr_t *coft; /* COMPONENTS OF thing itself */
31 asn1p_expr_t *terminal; /* Terminal of the referenced type */
32
33 if(memb->expr_type != A1TC_COMPONENTS_OF) {
34 TQ_ADD(&list, memb, next);
35 continue;
36 }
37
38 coft = TQ_FIRST(&memb->members);
39 assert(coft);
40 assert(!TQ_NEXT(coft, next));
41
42 /*
43 * Find the referenced type.
44 */
45 terminal = asn1f_find_terminal_type(arg, coft);
46 if(!terminal || (terminal->expr_type != expr->expr_type)) {
47 FATAL("COMPONENTS OF at line %d "
48 "must reference a %s type",
49 coft->_lineno,
50 expr->expr_type==ASN_CONSTR_SET
51 ? "SET" : "SEQUENCE"
52 );
53 TQ_ADD(&list, memb, next);
54 r_value = -1;
55 continue;
56 }
57
58 /*
59 * Clone the final structure.
60 */
61
62 coft = asn1p_expr_clone(terminal, 1 /* Skip extensions */);
63 if(!coft) return -1; /* ENOMEM */
64
vlm318bc272005-04-13 13:20:20 +000065 if(0) {
66 asn1p_expr_free(memb); /* Don't need it anymore*/
67 } else {
68 /* Actual removal clashes with constraints... skip. */
69 }
vlmf645aff2004-08-22 03:09:24 +000070
71 /*
72 * Move all components of the cloned structure
73 * into the current one.
74 */
vlm6a02a8a2004-09-08 00:28:11 +000075 while((memb = TQ_REMOVE(&(coft->members), next))) {
vlmf645aff2004-08-22 03:09:24 +000076 TQ_ADD(&list, memb, next);
vlm6a02a8a2004-09-08 00:28:11 +000077 memb->parent_expr = expr;
78 }
vlmf645aff2004-08-22 03:09:24 +000079
80 asn1p_expr_free(coft); /* Remove wrapper */
81 }
82
83 /* Move the stuff back */
vlmb6fd3b22004-08-25 00:42:25 +000084 TQ_MOVE(&(expr->members), &list);
vlmf645aff2004-08-22 03:09:24 +000085
86 return r_value;
87}
vlmfa67ddc2004-06-03 03:38:44 +000088
vlm76142452004-09-05 10:36:22 +000089/*
90 * Fix extensibility parts inside constructed types (SEQUENCE, SET, CHOICE).
91 */
vlmfa67ddc2004-06-03 03:38:44 +000092int
93asn1f_fix_constr_ext(arg_t *arg) {
94 asn1p_expr_t *expr = arg->expr;
95 asn1p_expr_t *v;
96 TQ_HEAD(asn1p_expr_t) root_list;
97 TQ_HEAD(asn1p_expr_t) ext_list;
98 TQ_HEAD(asn1p_expr_t) *cur_list;
99 int r_value = 0;
100 int ext_count = 0;
101
102 switch(expr->expr_type) {
103 case ASN_CONSTR_SEQUENCE:
104 case ASN_CONSTR_SET:
105 case ASN_CONSTR_CHOICE:
106 break;
107 default:
108 return 0;
109 }
110
vlmfd245932005-03-10 10:02:50 +0000111 DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno);
vlmfa67ddc2004-06-03 03:38:44 +0000112
113 TQ_INIT(&root_list);
114 TQ_INIT(&ext_list);
115 cur_list = (void *)&root_list;
116
vlm76142452004-09-05 10:36:22 +0000117 /*
118 * Split the set of fields into two lists, the root list
119 * and the extensions list.
120 */
vlmfa67ddc2004-06-03 03:38:44 +0000121 while((v = TQ_REMOVE(&(expr->members), next))) {
122 if(v->expr_type == A1TC_EXTENSIBLE) {
123 ext_count++;
124 switch(ext_count) {
125 case 1: cur_list = (void *)&ext_list; break;
126 case 2:
127 cur_list = (void *)&root_list;
128 if(v->value) {
129 FATAL("Optional extension marker "
130 "must not contain "
131 "an exception mark "
132 "at line %d", v->_lineno);
133 r_value = -1;
134 }
135 asn1p_expr_free(v);
136 continue;
137 case 3:
138 FATAL("Third extension marker "
139 "is not allowed at line %d", v->_lineno);
140 default:
141 r_value = -1;
142 }
143 }
144
145 TQ_ADD(cur_list, v, next);
146 }
147
148 /*
149 * Copy the root list and extension list back into the main list.
150 */
vlmb6fd3b22004-08-25 00:42:25 +0000151 TQ_MOVE(&(expr->members), &root_list);
vlmfa67ddc2004-06-03 03:38:44 +0000152 while((v = TQ_REMOVE(&ext_list, next)))
153 TQ_ADD(&(expr->members), v, next);
154
155 if(arg->mod->module_flags & MSF_EXTENSIBILITY_IMPLIED
vlmb6fd3b22004-08-25 00:42:25 +0000156 && ext_count == 0) {
vlmfa67ddc2004-06-03 03:38:44 +0000157 v = asn1p_expr_new(0);
158 if(v) {
159 v->Identifier = strdup("...");
160 v->expr_type = A1TC_EXTENSIBLE;
161 v->meta_type = AMT_TYPE;
vlm4f4f56d2004-07-01 00:51:31 +0000162 v->_lineno = expr->_lineno; /* The best we can do */
vlmfa67ddc2004-06-03 03:38:44 +0000163 if(v->Identifier == NULL) {
164 asn1p_expr_free(v);
165 r_value = -1;
166 } else {
vlm6a02a8a2004-09-08 00:28:11 +0000167 asn1p_expr_add(expr, v);
vlmfa67ddc2004-06-03 03:38:44 +0000168 }
169 } else {
170 r_value = -1;
171 }
172 }
173
174 return r_value;
175}
176
177
178int
vlm76142452004-09-05 10:36:22 +0000179asn1f_fix_constr_tag(arg_t *arg, int fix_top_level) {
vlmfa67ddc2004-06-03 03:38:44 +0000180 asn1p_expr_t *expr = arg->expr;
181 asn1p_expr_t *v;
vlmfa67ddc2004-06-03 03:38:44 +0000182 int root_tagged = 0; /* The root component is manually tagged */
183 int ext_tagged = 0; /* The extensions are manually tagged */
184 int component_number = 0;
185 int r_value = 0;
186
vlmfd245932005-03-10 10:02:50 +0000187 DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno);
vlm76142452004-09-05 10:36:22 +0000188
189 /*
190 * Fix the top-level type itself first.
191 */
192 if(fix_top_level) {
193 if(expr->tag.tag_class == TC_NOCLASS)
194 return r_value;
195
196 if(_asn1f_fix_type_tag(arg, expr))
197 r_value = -1;
198
199 return r_value;
200 }
201
vlmfa67ddc2004-06-03 03:38:44 +0000202 switch(expr->expr_type) {
203 case ASN_CONSTR_SEQUENCE:
204 case ASN_CONSTR_SET:
205 case ASN_CONSTR_CHOICE:
206 break;
207 default:
208 return 0;
209 }
210
vlmfa67ddc2004-06-03 03:38:44 +0000211 TQ_FOR(v, &(expr->members), next) {
vlmfa67ddc2004-06-03 03:38:44 +0000212
213 if(v->expr_type == A1TC_EXTENSIBLE) {
214 component_number++;
215 continue;
216 }
217
218 if(v->tag.tag_class == TC_NOCLASS) {
219 continue;
vlmfa67ddc2004-06-03 03:38:44 +0000220 }
221
vlm76142452004-09-05 10:36:22 +0000222 switch(component_number) {
223 case 0: case 2:
224 root_tagged = 1; break;
225 default:
226 ext_tagged = 1; break;
vlmfa67ddc2004-06-03 03:38:44 +0000227 }
228
vlm76142452004-09-05 10:36:22 +0000229 if(_asn1f_fix_type_tag(arg, v))
230 r_value = -1;
231
vlmfa67ddc2004-06-03 03:38:44 +0000232 }
233
vlm387a0882004-09-15 11:48:34 +0000234 if((arg->mod->module_flags & MSF_AUTOMATIC_TAGS)
235 && !root_tagged) {
236 if(ext_tagged) {
237 /* X.690: 28.4 */
238 FATAL("In %s at line %d: "
239 "extensions are tagged "
240 "but root components are not",
241 expr->Identifier, expr->_lineno);
242 r_value = -1;
243 } else {
244 /* Make a decision on automatic tagging */
245 expr->auto_tags_OK = 1;
246 }
vlmfa67ddc2004-06-03 03:38:44 +0000247 }
248
249 return r_value;
250}
251
vlm76142452004-09-05 10:36:22 +0000252static int
253_asn1f_fix_type_tag(arg_t *arg, asn1p_expr_t *expr) {
254 int must_explicit = _asn1f_check_if_tag_must_be_explicit(arg, expr);
vlmb012da82005-07-21 01:18:19 +0000255 int module_impl_tags = (arg->mod->module_flags
256 & (MSF_IMPLICIT_TAGS | MSF_AUTOMATIC_TAGS));
vlm76142452004-09-05 10:36:22 +0000257 int r_value = 0;
258
vlm267b7fd2004-10-11 11:43:08 +0000259 if(expr->tag.tag_mode == TM_DEFAULT) {
260 if(must_explicit || module_impl_tags == 0)
vlm76142452004-09-05 10:36:22 +0000261 expr->tag.tag_mode = TM_EXPLICIT;
vlm267b7fd2004-10-11 11:43:08 +0000262 else
263 expr->tag.tag_mode = TM_IMPLICIT;
vlm76142452004-09-05 10:36:22 +0000264 }
265
266 /*
267 * Perform a final sanity check.
268 */
269 if(must_explicit) {
270 if(expr->tag.tag_mode == TM_IMPLICIT) {
271 FATAL("%s tagged in IMPLICIT mode "
272 "but must be EXPLICIT at line %d",
273 expr->Identifier, expr->_lineno);
274 r_value = -1;
275 } else {
276 expr->tag.tag_mode = TM_EXPLICIT;
277 }
278 }
279
280 return r_value;
281}
282
vlmfa67ddc2004-06-03 03:38:44 +0000283int
284asn1f_fix_constr_autotag(arg_t *arg) {
285 asn1p_expr_t *expr = arg->expr;
286 asn1p_expr_t *v;
vlmd5b3bf32004-09-29 13:17:17 +0000287 asn1c_integer_t tag_value = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000288 int r_value = 0;
289
290 switch(expr->expr_type) {
291 case ASN_CONSTR_SEQUENCE:
292 case ASN_CONSTR_SET:
293 case ASN_CONSTR_CHOICE:
294 if(expr->auto_tags_OK)
295 break;
296 /* Automatic tagging is not applicable */
297 /* Fall through */
298 default:
299 return 0;
300 }
301
vlmfd245932005-03-10 10:02:50 +0000302 DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno);
vlmfa67ddc2004-06-03 03:38:44 +0000303
304 TQ_FOR(v, &(expr->members), next) {
305 int must_explicit;
306
vlm0aa86902004-10-12 23:26:53 +0000307 if(v->expr_type == A1TC_EXTENSIBLE) {
308 /* 28.5, d) */
309 continue;
310 }
vlmfa67ddc2004-06-03 03:38:44 +0000311
vlmf645aff2004-08-22 03:09:24 +0000312 if(0) {
313 /* This may be not true in case COMPONENTS OF */
314 assert(v->tag.tag_class == TC_NOCLASS);
315 }
vlmfa67ddc2004-06-03 03:38:44 +0000316
317 must_explicit = _asn1f_check_if_tag_must_be_explicit(arg, v);
318
319 v->tag.tag_class = TC_CONTEXT_SPECIFIC;
320 v->tag.tag_mode = must_explicit ? TM_EXPLICIT : TM_IMPLICIT;
321 v->tag.tag_value = tag_value++;
322 }
323
324 return r_value;
325}
326
327/*
328 * Check that tags are distinct.
329 */
330int
331asn1f_check_constr_tags_distinct(arg_t *arg) {
332 asn1p_expr_t *expr = arg->expr;
333 asn1p_expr_t *v;
334 int r_value = 0;
335
336 switch(expr->expr_type) {
337 case ASN_CONSTR_SEQUENCE:
338 case ASN_CONSTR_SET:
339 case ASN_CONSTR_CHOICE:
340 break;
341 default:
342 return 0;
343 }
344
345 TQ_FOR(v, &(expr->members), next) {
346 /*
347 * In every series of non-mandatory components,
348 * the tags must be distinct from each other AND the
349 * tag of the following mandatory component.
350 * For SET and CHOICE treat everything as a big set of
351 * non-mandatory components.
352 */
vlm387a0882004-09-15 11:48:34 +0000353 if(expr->expr_type != ASN_CONSTR_SEQUENCE || v->marker.flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000354 asn1p_expr_t *nv;
355 for(nv = v; (nv = TQ_NEXT(nv, next));) {
356 if(_asn1f_compare_tags(arg, v, nv))
357 r_value = -1;
358 if(expr->expr_type == ASN_CONSTR_SEQUENCE
vlm387a0882004-09-15 11:48:34 +0000359 && !nv->marker.flags) break;
vlmfa67ddc2004-06-03 03:38:44 +0000360 }
361 }
362 }
363
364 return r_value;
365}
366
367static int
368_asn1f_check_if_tag_must_be_explicit(arg_t *arg, asn1p_expr_t *v) {
vlm76142452004-09-05 10:36:22 +0000369 struct asn1p_type_tag_s tag;
370 struct asn1p_type_tag_s save_tag;
vlmfa67ddc2004-06-03 03:38:44 +0000371 asn1p_expr_t *reft;
vlm76142452004-09-05 10:36:22 +0000372 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 */
vlm8fbd5622004-09-10 06:07:04 +0000379 ret = asn1f_fetch_outmost_tag(arg->asn, arg->mod, v, &tag, 0);
vlm76142452004-09-05 10:36:22 +0000380 v->tag = save_tag; /* Restore the tag back */
381
382 if(ret == 0) return 0; /* If found tag, it's okay */
vlmfa67ddc2004-06-03 03:38:44 +0000383
vlmf645aff2004-08-22 03:09:24 +0000384 reft = asn1f_find_terminal_type(arg, v);
vlmfa67ddc2004-06-03 03:38:44 +0000385 if(reft) {
386 switch(reft->expr_type) {
vlm76142452004-09-05 10:36:22 +0000387 case ASN_TYPE_ANY:
vlmfa67ddc2004-06-03 03:38:44 +0000388 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 */
401static 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
vlm8fbd5622004-09-10 06:07:04 +0000407 ra = asn1f_fetch_outmost_tag(arg->asn, arg->mod, a, &ta, AFT_MAGIC_ANY);
408 rb = asn1f_fetch_outmost_tag(arg->asn, arg->mod, b, &tb, AFT_MAGIC_ANY);
vlmfa67ddc2004-06-03 03:38:44 +0000409
410 /*
411 * If both tags are explicitly or implicitly given, use them.
412 */
413 if(ra == 0 && rb == 0) {
414 /*
415 * Simple case: fetched both tags.
416 */
vlm76142452004-09-05 10:36:22 +0000417
418 if((ta.tag_value == tb.tag_value
419 && ta.tag_class == tb.tag_class)
420 || ta.tag_value == -1 /* Spread IMAGINARY ANY tag... */
421 || tb.tag_value == -1 /* ...it is an evil virus, fear it! */
422 ) {
vlm5b103032005-06-02 05:21:53 +0000423 char tagbuf[2][TAG2STRING_BUFFER_SIZE];
vlmfa67ddc2004-06-03 03:38:44 +0000424 char *p = (a->expr_type == A1TC_EXTENSIBLE)
425 ?"potentially ":"";
vlm387a0882004-09-15 11:48:34 +0000426 FATAL("Processing %s at line %d: component \"%s\" at line %d %shas the same tag "
vlmfa67ddc2004-06-03 03:38:44 +0000427 "with component \"%s\" at line %d",
vlm387a0882004-09-15 11:48:34 +0000428 arg->expr->Identifier,
429 arg->expr->_lineno,
vlmfa67ddc2004-06-03 03:38:44 +0000430 a->Identifier,
431 a->_lineno,
432 p,
433 b->Identifier,
434 b->_lineno
435 );
vlm5b103032005-06-02 05:21:53 +0000436 DEBUG("Tags: %s %s vs. %s %s",
437 asn1p_tag2string(&ta, tagbuf[0]),
438 a->Identifier,
439 asn1p_tag2string(&tb, tagbuf[1]),
440 b->Identifier
441 );
vlm4f4f56d2004-07-01 00:51:31 +0000442 if((arg->mod->module_flags & MSF_EXTENSIBILITY_IMPLIED)
443 && (a->expr_type == A1TC_EXTENSIBLE)
444 && (b->expr_type == A1TC_EXTENSIBLE)) {
445 FATAL("The previous error is due to "
446 "improper use of "
447 "EXTENSIBILITY IMPLIED flag "
448 "of module %s",
vlm931aeed2005-08-12 10:09:10 +0000449 arg->mod->ModuleName);
vlm4f4f56d2004-07-01 00:51:31 +0000450 }
vlmfa67ddc2004-06-03 03:38:44 +0000451 return -1;
452 } else {
453 /* Tags are distinct */
454 return 0;
455 }
456 }
457
458 /**********************************************************
459 * Now we must perform some very funny recursion to check
460 * multiple components of CHOICE type, etc.
461 */
462
463 DEBUG("Comparing tags %s:%x <-> %s:%x",
464 a->Identifier, a->expr_type,
465 b->Identifier, b->expr_type);
466
vlm387a0882004-09-15 11:48:34 +0000467 if(ra && a->meta_type == AMT_TYPEREF) {
vlmfa67ddc2004-06-03 03:38:44 +0000468
469 DEBUG(" %s is a type reference", a->Identifier);
470
vlmf645aff2004-08-22 03:09:24 +0000471 a = asn1f_lookup_symbol(arg, a->module, a->reference);
vlmfa67ddc2004-06-03 03:38:44 +0000472 if(!a) return 0; /* Already FATAL()'ed somewhere else */
vlmf645aff2004-08-22 03:09:24 +0000473 WITH_MODULE(a->module, ret = _asn1f_compare_tags(arg, a, b));
vlmfa67ddc2004-06-03 03:38:44 +0000474 return ret;
475 }
476
vlm387a0882004-09-15 11:48:34 +0000477 if(ra && a->expr_type == ASN_CONSTR_CHOICE) {
vlmfa67ddc2004-06-03 03:38:44 +0000478 asn1p_expr_t *v;
479
480 DEBUG(" %s is a choice type (%d)", a->Identifier, a->_mark);
481
482 /*
483 * Iterate over members of CHOICE.
484 */
485 //if(a->_mark & TM_RECURSION) return 0;
486 TQ_FOR(v, &(a->members), next) {
487 //a->_mark |= TM_RECURSION;
488 ret = _asn1f_compare_tags(arg, v, b);
489 //a->_mark &= ~TM_RECURSION;
490 if(ret) return ret;
491 }
492 return 0;
493 }
494
vlm387a0882004-09-15 11:48:34 +0000495 if(rb && b->expr_type == ASN_CONSTR_CHOICE) {
vlmfa67ddc2004-06-03 03:38:44 +0000496 return _asn1f_compare_tags(arg, b, a);
497 }
498
499 if(a->_mark & TM_RECURSION) return 0;
500 if(b->_mark & TM_RECURSION) return 0;
501 a->_mark |= TM_RECURSION;
502 b->_mark |= TM_RECURSION;
503 ret = _asn1f_compare_tags(arg, b, a);
504 a->_mark &= ~TM_RECURSION;
505 b->_mark &= ~TM_RECURSION;
506
507 return ret;
508}
509