blob: 794845b7378c756a7dffa834fa5e05febd1a7835 [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
65 asn1p_expr_free(memb); /* Don't need it anymore*/
66
67 /*
68 * Move all components of the cloned structure
69 * into the current one.
70 */
vlm6a02a8a2004-09-08 00:28:11 +000071 while((memb = TQ_REMOVE(&(coft->members), next))) {
vlmf645aff2004-08-22 03:09:24 +000072 TQ_ADD(&list, memb, next);
vlm6a02a8a2004-09-08 00:28:11 +000073 memb->parent_expr = expr;
74 }
vlmf645aff2004-08-22 03:09:24 +000075
76 asn1p_expr_free(coft); /* Remove wrapper */
77 }
78
79 /* Move the stuff back */
vlmb6fd3b22004-08-25 00:42:25 +000080 TQ_MOVE(&(expr->members), &list);
vlmf645aff2004-08-22 03:09:24 +000081
82 return r_value;
83}
vlmfa67ddc2004-06-03 03:38:44 +000084
vlm76142452004-09-05 10:36:22 +000085/*
86 * Fix extensibility parts inside constructed types (SEQUENCE, SET, CHOICE).
87 */
vlmfa67ddc2004-06-03 03:38:44 +000088int
89asn1f_fix_constr_ext(arg_t *arg) {
90 asn1p_expr_t *expr = arg->expr;
91 asn1p_expr_t *v;
92 TQ_HEAD(asn1p_expr_t) root_list;
93 TQ_HEAD(asn1p_expr_t) ext_list;
94 TQ_HEAD(asn1p_expr_t) *cur_list;
95 int r_value = 0;
96 int ext_count = 0;
97
98 switch(expr->expr_type) {
99 case ASN_CONSTR_SEQUENCE:
100 case ASN_CONSTR_SET:
101 case ASN_CONSTR_CHOICE:
102 break;
103 default:
104 return 0;
105 }
106
vlmfd245932005-03-10 10:02:50 +0000107 DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno);
vlmfa67ddc2004-06-03 03:38:44 +0000108
109 TQ_INIT(&root_list);
110 TQ_INIT(&ext_list);
111 cur_list = (void *)&root_list;
112
vlm76142452004-09-05 10:36:22 +0000113 /*
114 * Split the set of fields into two lists, the root list
115 * and the extensions list.
116 */
vlmfa67ddc2004-06-03 03:38:44 +0000117 while((v = TQ_REMOVE(&(expr->members), next))) {
118 if(v->expr_type == A1TC_EXTENSIBLE) {
119 ext_count++;
120 switch(ext_count) {
121 case 1: cur_list = (void *)&ext_list; break;
122 case 2:
123 cur_list = (void *)&root_list;
124 if(v->value) {
125 FATAL("Optional extension marker "
126 "must not contain "
127 "an exception mark "
128 "at line %d", v->_lineno);
129 r_value = -1;
130 }
131 asn1p_expr_free(v);
132 continue;
133 case 3:
134 FATAL("Third extension marker "
135 "is not allowed at line %d", v->_lineno);
136 default:
137 r_value = -1;
138 }
139 }
140
141 TQ_ADD(cur_list, v, next);
142 }
143
144 /*
145 * Copy the root list and extension list back into the main list.
146 */
vlmb6fd3b22004-08-25 00:42:25 +0000147 TQ_MOVE(&(expr->members), &root_list);
vlmfa67ddc2004-06-03 03:38:44 +0000148 while((v = TQ_REMOVE(&ext_list, next)))
149 TQ_ADD(&(expr->members), v, next);
150
151 if(arg->mod->module_flags & MSF_EXTENSIBILITY_IMPLIED
vlmb6fd3b22004-08-25 00:42:25 +0000152 && ext_count == 0) {
vlmfa67ddc2004-06-03 03:38:44 +0000153 v = asn1p_expr_new(0);
154 if(v) {
155 v->Identifier = strdup("...");
156 v->expr_type = A1TC_EXTENSIBLE;
157 v->meta_type = AMT_TYPE;
vlm4f4f56d2004-07-01 00:51:31 +0000158 v->_lineno = expr->_lineno; /* The best we can do */
vlmfa67ddc2004-06-03 03:38:44 +0000159 if(v->Identifier == NULL) {
160 asn1p_expr_free(v);
161 r_value = -1;
162 } else {
vlm6a02a8a2004-09-08 00:28:11 +0000163 asn1p_expr_add(expr, v);
vlmfa67ddc2004-06-03 03:38:44 +0000164 }
165 } else {
166 r_value = -1;
167 }
168 }
169
170 return r_value;
171}
172
173
174int
vlm76142452004-09-05 10:36:22 +0000175asn1f_fix_constr_tag(arg_t *arg, int fix_top_level) {
vlmfa67ddc2004-06-03 03:38:44 +0000176 asn1p_expr_t *expr = arg->expr;
177 asn1p_expr_t *v;
vlmfa67ddc2004-06-03 03:38:44 +0000178 int root_tagged = 0; /* The root component is manually tagged */
179 int ext_tagged = 0; /* The extensions are manually tagged */
180 int component_number = 0;
181 int r_value = 0;
182
vlmfd245932005-03-10 10:02:50 +0000183 DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno);
vlm76142452004-09-05 10:36:22 +0000184
185 /*
186 * Fix the top-level type itself first.
187 */
188 if(fix_top_level) {
189 if(expr->tag.tag_class == TC_NOCLASS)
190 return r_value;
191
192 if(_asn1f_fix_type_tag(arg, expr))
193 r_value = -1;
194
195 return r_value;
196 }
197
vlmfa67ddc2004-06-03 03:38:44 +0000198 switch(expr->expr_type) {
199 case ASN_CONSTR_SEQUENCE:
200 case ASN_CONSTR_SET:
201 case ASN_CONSTR_CHOICE:
202 break;
203 default:
204 return 0;
205 }
206
vlmfa67ddc2004-06-03 03:38:44 +0000207 TQ_FOR(v, &(expr->members), next) {
vlmfa67ddc2004-06-03 03:38:44 +0000208
209 if(v->expr_type == A1TC_EXTENSIBLE) {
210 component_number++;
211 continue;
212 }
213
214 if(v->tag.tag_class == TC_NOCLASS) {
215 continue;
vlmfa67ddc2004-06-03 03:38:44 +0000216 }
217
vlm76142452004-09-05 10:36:22 +0000218 switch(component_number) {
219 case 0: case 2:
220 root_tagged = 1; break;
221 default:
222 ext_tagged = 1; break;
vlmfa67ddc2004-06-03 03:38:44 +0000223 }
224
vlm76142452004-09-05 10:36:22 +0000225 if(_asn1f_fix_type_tag(arg, v))
226 r_value = -1;
227
vlmfa67ddc2004-06-03 03:38:44 +0000228 }
229
vlm387a0882004-09-15 11:48:34 +0000230 if((arg->mod->module_flags & MSF_AUTOMATIC_TAGS)
231 && !root_tagged) {
232 if(ext_tagged) {
233 /* X.690: 28.4 */
234 FATAL("In %s at line %d: "
235 "extensions are tagged "
236 "but root components are not",
237 expr->Identifier, expr->_lineno);
238 r_value = -1;
239 } else {
240 /* Make a decision on automatic tagging */
241 expr->auto_tags_OK = 1;
242 }
vlmfa67ddc2004-06-03 03:38:44 +0000243 }
244
245 return r_value;
246}
247
vlm76142452004-09-05 10:36:22 +0000248static int
249_asn1f_fix_type_tag(arg_t *arg, asn1p_expr_t *expr) {
250 int must_explicit = _asn1f_check_if_tag_must_be_explicit(arg, expr);
vlm267b7fd2004-10-11 11:43:08 +0000251 int module_impl_tags = (arg->mod->module_flags & MSF_IMPLICIT_TAGS);
vlm76142452004-09-05 10:36:22 +0000252 int r_value = 0;
253
vlm267b7fd2004-10-11 11:43:08 +0000254 if(expr->tag.tag_mode == TM_DEFAULT) {
255 if(must_explicit || module_impl_tags == 0)
vlm76142452004-09-05 10:36:22 +0000256 expr->tag.tag_mode = TM_EXPLICIT;
vlm267b7fd2004-10-11 11:43:08 +0000257 else
258 expr->tag.tag_mode = TM_IMPLICIT;
vlm76142452004-09-05 10:36:22 +0000259 }
260
261 /*
262 * Perform a final sanity check.
263 */
264 if(must_explicit) {
265 if(expr->tag.tag_mode == TM_IMPLICIT) {
266 FATAL("%s tagged in IMPLICIT mode "
267 "but must be EXPLICIT at line %d",
268 expr->Identifier, expr->_lineno);
269 r_value = -1;
270 } else {
271 expr->tag.tag_mode = TM_EXPLICIT;
272 }
273 }
274
275 return r_value;
276}
277
vlmfa67ddc2004-06-03 03:38:44 +0000278int
279asn1f_fix_constr_autotag(arg_t *arg) {
280 asn1p_expr_t *expr = arg->expr;
281 asn1p_expr_t *v;
vlmd5b3bf32004-09-29 13:17:17 +0000282 asn1c_integer_t tag_value = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000283 int r_value = 0;
284
285 switch(expr->expr_type) {
286 case ASN_CONSTR_SEQUENCE:
287 case ASN_CONSTR_SET:
288 case ASN_CONSTR_CHOICE:
289 if(expr->auto_tags_OK)
290 break;
291 /* Automatic tagging is not applicable */
292 /* Fall through */
293 default:
294 return 0;
295 }
296
vlmfd245932005-03-10 10:02:50 +0000297 DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno);
vlmfa67ddc2004-06-03 03:38:44 +0000298
299 TQ_FOR(v, &(expr->members), next) {
300 int must_explicit;
301
vlm0aa86902004-10-12 23:26:53 +0000302 if(v->expr_type == A1TC_EXTENSIBLE) {
303 /* 28.5, d) */
304 continue;
305 }
vlmfa67ddc2004-06-03 03:38:44 +0000306
vlmf645aff2004-08-22 03:09:24 +0000307 if(0) {
308 /* This may be not true in case COMPONENTS OF */
309 assert(v->tag.tag_class == TC_NOCLASS);
310 }
vlmfa67ddc2004-06-03 03:38:44 +0000311
312 must_explicit = _asn1f_check_if_tag_must_be_explicit(arg, v);
313
314 v->tag.tag_class = TC_CONTEXT_SPECIFIC;
315 v->tag.tag_mode = must_explicit ? TM_EXPLICIT : TM_IMPLICIT;
316 v->tag.tag_value = tag_value++;
317 }
318
319 return r_value;
320}
321
322/*
323 * Check that tags are distinct.
324 */
325int
326asn1f_check_constr_tags_distinct(arg_t *arg) {
327 asn1p_expr_t *expr = arg->expr;
328 asn1p_expr_t *v;
329 int r_value = 0;
330
331 switch(expr->expr_type) {
332 case ASN_CONSTR_SEQUENCE:
333 case ASN_CONSTR_SET:
334 case ASN_CONSTR_CHOICE:
335 break;
336 default:
337 return 0;
338 }
339
340 TQ_FOR(v, &(expr->members), next) {
341 /*
342 * In every series of non-mandatory components,
343 * the tags must be distinct from each other AND the
344 * tag of the following mandatory component.
345 * For SET and CHOICE treat everything as a big set of
346 * non-mandatory components.
347 */
vlm387a0882004-09-15 11:48:34 +0000348 if(expr->expr_type != ASN_CONSTR_SEQUENCE || v->marker.flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000349 asn1p_expr_t *nv;
350 for(nv = v; (nv = TQ_NEXT(nv, next));) {
351 if(_asn1f_compare_tags(arg, v, nv))
352 r_value = -1;
353 if(expr->expr_type == ASN_CONSTR_SEQUENCE
vlm387a0882004-09-15 11:48:34 +0000354 && !nv->marker.flags) break;
vlmfa67ddc2004-06-03 03:38:44 +0000355 }
356 }
357 }
358
359 return r_value;
360}
361
362static int
363_asn1f_check_if_tag_must_be_explicit(arg_t *arg, asn1p_expr_t *v) {
vlm76142452004-09-05 10:36:22 +0000364 struct asn1p_type_tag_s tag;
365 struct asn1p_type_tag_s save_tag;
vlmfa67ddc2004-06-03 03:38:44 +0000366 asn1p_expr_t *reft;
vlm76142452004-09-05 10:36:22 +0000367 int ret;
368
369 /*
370 * Fetch the _next_ tag for this type.
371 */
372 save_tag = v->tag; /* Save existing tag */
373 memset(&v->tag, 0, sizeof(v->tag)); /* Remove it temporarily */
vlm8fbd5622004-09-10 06:07:04 +0000374 ret = asn1f_fetch_outmost_tag(arg->asn, arg->mod, v, &tag, 0);
vlm76142452004-09-05 10:36:22 +0000375 v->tag = save_tag; /* Restore the tag back */
376
377 if(ret == 0) return 0; /* If found tag, it's okay */
vlmfa67ddc2004-06-03 03:38:44 +0000378
vlmf645aff2004-08-22 03:09:24 +0000379 reft = asn1f_find_terminal_type(arg, v);
vlmfa67ddc2004-06-03 03:38:44 +0000380 if(reft) {
381 switch(reft->expr_type) {
vlm76142452004-09-05 10:36:22 +0000382 case ASN_TYPE_ANY:
vlmfa67ddc2004-06-03 03:38:44 +0000383 case ASN_CONSTR_CHOICE:
384 return 1;
385 default:
386 return 0;
387 }
388 }
389
390 return 0;
391}
392
393/*
394 * Check that the tags are distinct.
395 */
396static int
397_asn1f_compare_tags(arg_t *arg, asn1p_expr_t *a, asn1p_expr_t *b) {
398 struct asn1p_type_tag_s ta, tb;
399 int ra, rb;
400 int ret;
401
vlm8fbd5622004-09-10 06:07:04 +0000402 ra = asn1f_fetch_outmost_tag(arg->asn, arg->mod, a, &ta, AFT_MAGIC_ANY);
403 rb = asn1f_fetch_outmost_tag(arg->asn, arg->mod, b, &tb, AFT_MAGIC_ANY);
vlmfa67ddc2004-06-03 03:38:44 +0000404
405 /*
406 * If both tags are explicitly or implicitly given, use them.
407 */
408 if(ra == 0 && rb == 0) {
409 /*
410 * Simple case: fetched both tags.
411 */
vlm76142452004-09-05 10:36:22 +0000412
413 if((ta.tag_value == tb.tag_value
414 && ta.tag_class == tb.tag_class)
415 || ta.tag_value == -1 /* Spread IMAGINARY ANY tag... */
416 || tb.tag_value == -1 /* ...it is an evil virus, fear it! */
417 ) {
vlmfa67ddc2004-06-03 03:38:44 +0000418 char *p = (a->expr_type == A1TC_EXTENSIBLE)
419 ?"potentially ":"";
vlm387a0882004-09-15 11:48:34 +0000420 FATAL("Processing %s at line %d: component \"%s\" at line %d %shas the same tag "
vlmfa67ddc2004-06-03 03:38:44 +0000421 "with component \"%s\" at line %d",
vlm387a0882004-09-15 11:48:34 +0000422 arg->expr->Identifier,
423 arg->expr->_lineno,
vlmfa67ddc2004-06-03 03:38:44 +0000424 a->Identifier,
425 a->_lineno,
426 p,
427 b->Identifier,
428 b->_lineno
429 );
vlm4f4f56d2004-07-01 00:51:31 +0000430 if((arg->mod->module_flags & MSF_EXTENSIBILITY_IMPLIED)
431 && (a->expr_type == A1TC_EXTENSIBLE)
432 && (b->expr_type == A1TC_EXTENSIBLE)) {
433 FATAL("The previous error is due to "
434 "improper use of "
435 "EXTENSIBILITY IMPLIED flag "
436 "of module %s",
437 arg->mod->Identifier);
438 }
vlmfa67ddc2004-06-03 03:38:44 +0000439 return -1;
440 } else {
441 /* Tags are distinct */
442 return 0;
443 }
444 }
445
446 /**********************************************************
447 * Now we must perform some very funny recursion to check
448 * multiple components of CHOICE type, etc.
449 */
450
451 DEBUG("Comparing tags %s:%x <-> %s:%x",
452 a->Identifier, a->expr_type,
453 b->Identifier, b->expr_type);
454
vlm387a0882004-09-15 11:48:34 +0000455 if(ra && a->meta_type == AMT_TYPEREF) {
vlmfa67ddc2004-06-03 03:38:44 +0000456
457 DEBUG(" %s is a type reference", a->Identifier);
458
vlmf645aff2004-08-22 03:09:24 +0000459 a = asn1f_lookup_symbol(arg, a->module, a->reference);
vlmfa67ddc2004-06-03 03:38:44 +0000460 if(!a) return 0; /* Already FATAL()'ed somewhere else */
vlmf645aff2004-08-22 03:09:24 +0000461 WITH_MODULE(a->module, ret = _asn1f_compare_tags(arg, a, b));
vlmfa67ddc2004-06-03 03:38:44 +0000462 return ret;
463 }
464
vlm387a0882004-09-15 11:48:34 +0000465 if(ra && a->expr_type == ASN_CONSTR_CHOICE) {
vlmfa67ddc2004-06-03 03:38:44 +0000466 asn1p_expr_t *v;
467
468 DEBUG(" %s is a choice type (%d)", a->Identifier, a->_mark);
469
470 /*
471 * Iterate over members of CHOICE.
472 */
473 //if(a->_mark & TM_RECURSION) return 0;
474 TQ_FOR(v, &(a->members), next) {
475 //a->_mark |= TM_RECURSION;
476 ret = _asn1f_compare_tags(arg, v, b);
477 //a->_mark &= ~TM_RECURSION;
478 if(ret) return ret;
479 }
480 return 0;
481 }
482
vlm387a0882004-09-15 11:48:34 +0000483 if(rb && b->expr_type == ASN_CONSTR_CHOICE) {
vlmfa67ddc2004-06-03 03:38:44 +0000484 return _asn1f_compare_tags(arg, b, a);
485 }
486
487 if(a->_mark & TM_RECURSION) return 0;
488 if(b->_mark & TM_RECURSION) return 0;
489 a->_mark |= TM_RECURSION;
490 b->_mark |= TM_RECURSION;
491 ret = _asn1f_compare_tags(arg, b, a);
492 a->_mark &= ~TM_RECURSION;
493 b->_mark &= ~TM_RECURSION;
494
495 return ret;
496}
497