blob: 1d06dbab7f7295c61ac16a2b7aa9b58c5be7b1d1 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include "asn1fix_internal.h"
Lev Walkin4efbfb72005-02-25 14:20:30 +00002#include "asn1fix.h"
Lev Walkinf15320b2004-06-03 03:38:44 +00003
4/* Print everything to stderr */
5static void _default_error_logger(int _severity, const char *fmt, ...);
6
7/*
8 * Internal check functions.
9 */
Lev Walkind24c62d2004-09-15 11:47:23 +000010static int asn1f_fix_module__phase_1(arg_t *arg);
11static int asn1f_fix_module__phase_2(arg_t *arg);
Lev Walkinf15320b2004-06-03 03:38:44 +000012static int asn1f_fix_simple(arg_t *arg); /* For INTEGER/ENUMERATED */
13static int asn1f_fix_constructed(arg_t *arg); /* For SEQUENCE/SET/CHOICE */
Lev Walkin1ef05162004-08-25 00:42:25 +000014static int asn1f_resolve_constraints(arg_t *arg); /* For subtype constraints */
15static int asn1f_check_constraints(arg_t *arg); /* For subtype constraints */
Lev Walkinf593f102005-02-22 07:59:59 +000016static int asn1f_check_duplicate(arg_t *arg);
Lev Walkin1dec5802005-03-04 09:02:27 +000017static int asn1f_apply_unique_index(arg_t *arg);
Lev Walkinf15320b2004-06-03 03:38:44 +000018
Lev Walkinb45e0672004-08-18 05:42:05 +000019arg_t a1f_replace_me_with_proper_interface_arg;
Lev Walkinf15320b2004-06-03 03:38:44 +000020
21/*
22 * Scan every module defined here in search for inconsistences.
23 */
24int
25asn1f_process(asn1p_t *asn, enum asn1f_flags flags,
26 error_logger_f error_logger) {
27 arg_t arg;
28 int fatals = 0;
29 int warnings = 0;
Lev Walkin4533a5f2005-03-18 04:22:41 +000030 int ret;
Lev Walkinf15320b2004-06-03 03:38:44 +000031
32 /*
33 * Check validity of arguments.
34 */
35 if(asn == NULL) {
36 errno = EINVAL;
37 return -1;
38 }
39
40 /*
41 * If errors handler is not specified, default to internal one.
42 */
43 if(error_logger == 0) {
44 error_logger = _default_error_logger;
45 }
46
47 memset(&arg, 0, sizeof(arg));
48 arg.asn = asn;
49 arg.eh = error_logger;
50
51 if(flags & A1F_DEBUG) {
52 arg.debug = arg.eh;
53 arg.debug(-1, "Called %s() with flags %d", __func__, flags);
54 flags &= ~A1F_DEBUG;
55 }
56
Lev Walkin5253da42004-08-20 13:25:56 +000057 /* Allow SIZE() constraint for INTEGER and other types */
58 if(flags & A1F_EXTENDED_SizeConstraint) {
59 arg.flags |= A1F_EXTENDED_SizeConstraint;
60 flags &= ~A1F_EXTENDED_SizeConstraint;
61 if(arg.debug) {
62 arg.debug(-1,
63 "Extended SizeConstraint support enabled");
64 }
65 }
66
Lev Walkinb45e0672004-08-18 05:42:05 +000067 a1f_replace_me_with_proper_interface_arg = arg;
68
Lev Walkinf15320b2004-06-03 03:38:44 +000069 /*
70 * Check that we haven't missed an unknown flag.
71 */
72 if(flags) {
73 errno = EINVAL;
74 return -1;
75 }
76
77 /*
78 * Process each module in the list.
Lev Walkinf593f102005-02-22 07:59:59 +000079 * PHASE I.
Lev Walkinf15320b2004-06-03 03:38:44 +000080 */
81 TQ_FOR(arg.mod, &(asn->modules), mod_next) {
Lev Walkin4533a5f2005-03-18 04:22:41 +000082 ret = asn1f_fix_module__phase_1(&arg);
Lev Walkinf15320b2004-06-03 03:38:44 +000083 /*
84 * These lines are used for illustration purposes.
85 * RET2RVAL() is used everywhere else.
86 */
87 if(ret == -1) fatals++;
88 if(ret == 1) warnings++;
89 }
Lev Walkinf593f102005-02-22 07:59:59 +000090 /* PHASE II. */
Lev Walkind24c62d2004-09-15 11:47:23 +000091 TQ_FOR(arg.mod, &(asn->modules), mod_next) {
Lev Walkin4533a5f2005-03-18 04:22:41 +000092 ret = asn1f_fix_module__phase_2(&arg);
Lev Walkind24c62d2004-09-15 11:47:23 +000093 if(ret == -1) fatals++;
94 if(ret == 1) warnings++;
95 }
96
97 memset(&a1f_replace_me_with_proper_interface_arg, 0, sizeof(arg_t));
Lev Walkinf15320b2004-06-03 03:38:44 +000098
99 /*
100 * Compute a return value.
101 */
102 return fatals?-1:warnings?1:0;
103}
104
105/*
106 * Check the internals of a single module.
107 */
108static int
Lev Walkind24c62d2004-09-15 11:47:23 +0000109asn1f_fix_module__phase_1(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000110 asn1p_expr_t *expr;
111 int rvalue = 0;
Lev Walkin1ef05162004-08-25 00:42:25 +0000112 int ret;
Lev Walkin4533a5f2005-03-18 04:22:41 +0000113 asn1p_module_t *omod;
114
115 /*
116 * Check that we don't have a similarly named module.
117 */
118 TQ_FOR(omod, &arg->asn->modules, mod_next) {
119 int sameNames;
120 if(omod == arg->mod) break;
Lev Walkinb36317c2005-08-12 10:09:10 +0000121 sameNames = strcmp(omod->ModuleName, arg->mod->ModuleName)?0:1;
Lev Walkin4533a5f2005-03-18 04:22:41 +0000122 if(omod->module_oid && arg->mod->module_oid) {
123 /* Compare only the OID. */
124 if(asn1p_oid_compare(omod->module_oid,
125 arg->mod->module_oid) == 0) {
Lev Walkin1a447952005-03-18 04:26:12 +0000126 FATAL("ASN.1 module %s in %s "
Lev Walkin4533a5f2005-03-18 04:22:41 +0000127 "has the same OBJECT IDENTIFIER"
Lev Walkin1a447952005-03-18 04:26:12 +0000128 " as module %s",
Lev Walkinb36317c2005-08-12 10:09:10 +0000129 omod->ModuleName,
Lev Walkin1a447952005-03-18 04:26:12 +0000130 omod->source_file_name,
Lev Walkinb36317c2005-08-12 10:09:10 +0000131 arg->mod->ModuleName
Lev Walkin4533a5f2005-03-18 04:22:41 +0000132 );
133 RET2RVAL(-1, rvalue);
134 } else if(sameNames) {
Lev Walkinb36317c2005-08-12 10:09:10 +0000135 WARNING("ASN.1 module %s is defined more than once, with different OIDs", omod->ModuleName);
Lev Walkin4533a5f2005-03-18 04:22:41 +0000136 RET2RVAL(1, rvalue);
137 }
138 } else if(sameNames) {
139 FATAL("ASN.1 module %s is defined more than once",
Lev Walkinb36317c2005-08-12 10:09:10 +0000140 omod->ModuleName);
Lev Walkin4533a5f2005-03-18 04:22:41 +0000141 RET2RVAL(-1, rvalue);
142 }
143 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000144
Lev Walkinb45e0672004-08-18 05:42:05 +0000145 switch((arg->mod->module_flags & MSF_MASK_TAGS)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000146 case MSF_NOFLAGS:
147 case MSF_EXPLICIT_TAGS:
148 case MSF_IMPLICIT_TAGS:
149 case MSF_AUTOMATIC_TAGS:
150 break;
151 default:
152 FATAL("Module %s defined with ambiguous global tagging mode",
Lev Walkinb36317c2005-08-12 10:09:10 +0000153 arg->mod->ModuleName);
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 RET2RVAL(-1, rvalue);
155 }
156
Lev Walkinb45e0672004-08-18 05:42:05 +0000157 switch((arg->mod->module_flags & MSF_MASK_INSTRUCTIONS)) {
158 case MSF_NOFLAGS:
Lev Walkin4ec3b4c2004-08-22 03:09:24 +0000159 /*
160 * arg->mod->module_flags |= MSF_TAG_INSTRUCTIONS;
161 */
Lev Walkinb45e0672004-08-18 05:42:05 +0000162 break;
163 case MSF_unk_INSTRUCTIONS:
164 WARNING("Module %s defined with unrecognized "
Lev Walkinb36317c2005-08-12 10:09:10 +0000165 "encoding reference", arg->mod->ModuleName);
Lev Walkinb45e0672004-08-18 05:42:05 +0000166 RET2RVAL(1, rvalue);
167 /* Fall through */
168 case MSF_TAG_INSTRUCTIONS:
169 case MSF_XER_INSTRUCTIONS:
170 break;
171 default:
172 FATAL("Module %s defined with ambiguous encoding reference",
Lev Walkinb36317c2005-08-12 10:09:10 +0000173 arg->mod->ModuleName);
Lev Walkinb45e0672004-08-18 05:42:05 +0000174 RET2RVAL(-1, rvalue);
175 }
176
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 /*
178 * Do various non-recursive transformations.
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 */
180 TQ_FOR(expr, &(arg->mod->members), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 arg->expr = expr;
182
Lev Walkinf593f102005-02-22 07:59:59 +0000183 /* Check whether this type is a duplicate */
184 ret = asn1f_check_duplicate(arg);
185 RET2RVAL(ret, rvalue);
186
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 if(expr->meta_type == AMT_PARAMTYPE)
188 /* Do not process the parametrized type just yet */
189 continue;
190
191 DEBUG("=== Now processing \"%s\" at line %d ===",
192 expr->Identifier, expr->_lineno);
193 assert(expr->meta_type != AMT_INVALID);
194
195 /*
196 * 2.1 Pre-process simple types (ENUMERATED, INTEGER, etc).
197 */
198 ret = asn1f_recurse_expr(arg, asn1f_fix_simple);
199 RET2RVAL(ret, rvalue);
200
201 /*
Lev Walkinf15320b2004-06-03 03:38:44 +0000202 * 2.5.4
203 */
204 ret = asn1f_recurse_expr(arg, asn1f_fix_dereference_types);
205 RET2RVAL(ret, rvalue);
206
207 /*
Lev Walkind541c252004-09-05 10:36:22 +0000208 * Fix tagging of top-level types.
209 */
210 ret = asn1f_fix_constr_tag(arg, 1);
211 RET2RVAL(ret, rvalue);
212
213 /*
214 * 2.[234] Process SEQUENCE/SET/CHOICE types.
215 */
216 ret = asn1f_recurse_expr(arg, asn1f_fix_constructed);
217 RET2RVAL(ret, rvalue);
218
219 /*
Lev Walkinf15320b2004-06-03 03:38:44 +0000220 * 2.5.5
221 */
222 ret = asn1f_recurse_expr(arg, asn1f_fix_dereference_values);
223 RET2RVAL(ret, rvalue);
224
225 /*
226 * Resolve references in constraints.
227 */
Lev Walkin1ef05162004-08-25 00:42:25 +0000228 ret = asn1f_recurse_expr(arg, asn1f_resolve_constraints);
Lev Walkinf15320b2004-06-03 03:38:44 +0000229 RET2RVAL(ret, rvalue);
230
231 /*
232 * 6. INTEGER value processed at 2.5.4.
233 */
234
235 /*
236 * Make sure everybody's behaving well.
237 */
238 assert(arg->expr == expr);
239 }
240
241 /*
242 * 5. Automatic tagging
243 */
244 TQ_FOR(expr, &(arg->mod->members), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000245
246 arg->expr = expr;
247
248 ret = asn1f_recurse_expr(arg, asn1f_fix_constr_autotag);
249 RET2RVAL(ret, rvalue);
250
251 assert(arg->expr == expr);
252 }
253
254 /*
255 * 8. fix BIT STRING
256 * 9. fix spaces in cstrings
257 */
258 TQ_FOR(expr, &(arg->mod->members), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000259 arg->expr = expr;
260
261 ret = asn1f_recurse_expr(arg, asn1f_fix_bit_string);
262 RET2RVAL(ret, rvalue);
263
264 ret = asn1f_recurse_expr(arg, asn1f_fix_cstring);
265 RET2RVAL(ret, rvalue);
266
267 assert(arg->expr == expr);
268 }
269
270 /*
271 * ... Check for tags distinctness.
272 */
273 TQ_FOR(expr, &(arg->mod->members), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000274 arg->expr = expr;
275
276 ret = asn1f_recurse_expr(arg, asn1f_check_constr_tags_distinct);
277 RET2RVAL(ret, rvalue);
278
279 assert(arg->expr == expr);
280 }
281
Lev Walkind24c62d2004-09-15 11:47:23 +0000282 return rvalue;
283}
284
285static int
286asn1f_fix_module__phase_2(arg_t *arg) {
287 asn1p_expr_t *expr;
288 int rvalue = 0;
289 int ret;
290
Lev Walkin1ef05162004-08-25 00:42:25 +0000291 TQ_FOR(expr, &(arg->mod->members), next) {
292 arg->expr = expr;
293
Lev Walkinf593f102005-02-22 07:59:59 +0000294 if(expr->meta_type == AMT_PARAMTYPE)
Lev Walkind541c252004-09-05 10:36:22 +0000295 /* Do not process the parametrized types here */
296 continue;
297
Lev Walkinb25fa062004-09-29 13:17:06 +0000298 /*
Lev Walkin3645c1c2004-10-31 00:11:50 +0000299 * Dereference DEFAULT values.
300 */
301 ret = asn1f_recurse_expr(arg, asn1f_fix_dereference_defaults);
302 RET2RVAL(ret, rvalue);
303
304 /*
Lev Walkinb25fa062004-09-29 13:17:06 +0000305 * Check semantic validity of constraints.
306 */
Lev Walkin1ef05162004-08-25 00:42:25 +0000307 ret = asn1f_recurse_expr(arg, asn1f_check_constraints);
308 RET2RVAL(ret, rvalue);
309
Lev Walkin1dec5802005-03-04 09:02:27 +0000310 /*
311 * Uniquely tag each inner type.
312 */
313 asn1f_apply_unique_index(0);
314 ret = asn1f_recurse_expr(arg, asn1f_apply_unique_index);
315 RET2RVAL(ret, rvalue);
316
Lev Walkin1ef05162004-08-25 00:42:25 +0000317 assert(arg->expr == expr);
318 }
319
Lev Walkinf15320b2004-06-03 03:38:44 +0000320 return rvalue;
321}
322
Lev Walkinf15320b2004-06-03 03:38:44 +0000323static int
324asn1f_fix_simple(arg_t *arg) {
325 int rvalue = 0;
326 int ret;
327
328 ret = asn1f_fix_enum(arg);
329 RET2RVAL(ret, rvalue);
330
331 ret = asn1f_fix_integer(arg);
332 RET2RVAL(ret, rvalue);
333
334 return rvalue;
335}
336
337static int
338asn1f_fix_constructed(arg_t *arg) {
339 int rvalue = 0;
340 int ret;
341
342 switch(arg->expr->expr_type) {
343 case ASN_CONSTR_SEQUENCE:
344 case ASN_CONSTR_SET:
345 case ASN_CONSTR_CHOICE:
346 break;
347 default:
348 return 0;
349 }
350
351 /* Check identifier distinctness */
352 ret = asn1f_check_unique_expr(arg, NULL);
353 RET2RVAL(ret, rvalue);
354
355 /* Fix extensibility */
356 ret = asn1f_fix_constr_ext(arg);
357 RET2RVAL(ret, rvalue);
358
359 /* Fix tagging */
Lev Walkind541c252004-09-05 10:36:22 +0000360 ret = asn1f_fix_constr_tag(arg, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000361 RET2RVAL(ret, rvalue);
362
Lev Walkin4ec3b4c2004-08-22 03:09:24 +0000363 /* Import COMPONENTS OF stuff */
364 ret = asn1f_pull_components_of(arg);
365 RET2RVAL(ret, rvalue);
366
Lev Walkinf15320b2004-06-03 03:38:44 +0000367 return rvalue;
368}
369
370static int
Lev Walkin1ef05162004-08-25 00:42:25 +0000371asn1f_resolve_constraints(arg_t *arg) {
Lev Walkinb45e0672004-08-18 05:42:05 +0000372 asn1p_expr_t *top_parent;
Lev Walkin5253da42004-08-20 13:25:56 +0000373 asn1p_expr_type_e etype;
Lev Walkinf15320b2004-06-03 03:38:44 +0000374 int rvalue = 0;
375 int ret;
376
Lev Walkin4ec3b4c2004-08-22 03:09:24 +0000377 top_parent = asn1f_find_terminal_type(arg, arg->expr);
Lev Walkin5253da42004-08-20 13:25:56 +0000378 if(top_parent)
379 etype = top_parent->expr_type;
380 else etype = A1TC_INVALID;
381
Lev Walkin03850182005-03-10 10:02:50 +0000382 DEBUG("(%s)", arg->expr->Identifier);
Lev Walkind541c252004-09-05 10:36:22 +0000383
Lev Walkin7ec9b4c2005-03-20 12:57:21 +0000384 ret = asn1constraint_resolve(arg, arg->expr->constraints, etype, 0);
Lev Walkinb45e0672004-08-18 05:42:05 +0000385 RET2RVAL(ret, rvalue);
Lev Walkinf15320b2004-06-03 03:38:44 +0000386
Lev Walkin1ef05162004-08-25 00:42:25 +0000387 return rvalue;
388}
389
390static int
391asn1f_check_constraints(arg_t *arg) {
392 static enum asn1p_constraint_type_e test_types[] = {
393 ACT_EL_RANGE, ACT_CT_SIZE, ACT_CT_FROM };
394 asn1p_expr_t *top_parent;
395 asn1cnst_range_t *range;
396 asn1p_expr_type_e etype;
397 unsigned int i;
398 int rvalue = 0;
399 int ret;
400
Lev Walkin03850182005-03-10 10:02:50 +0000401 DEBUG("(%s{%d/%d})",
Lev Walkind541c252004-09-05 10:36:22 +0000402 arg->expr->Identifier,
403 arg->expr->meta_type, arg->expr->expr_type);
404
Lev Walkin1ef05162004-08-25 00:42:25 +0000405 top_parent = asn1f_find_terminal_type(arg, arg->expr);
406 if(!top_parent)
407 return 0;
408 etype = top_parent->expr_type;
409
Lev Walkinb45e0672004-08-18 05:42:05 +0000410 ret = asn1constraint_pullup(arg);
411 RET2RVAL(ret, rvalue);
412
Lev Walkin1ef05162004-08-25 00:42:25 +0000413 for(i = 0; i < sizeof(test_types)/sizeof(test_types[0]); i++) {
414 range = asn1constraint_compute_PER_range(
Lev Walkinb9fa7802004-08-25 02:04:39 +0000415 etype,
Lev Walkin1ef05162004-08-25 00:42:25 +0000416 arg->expr->combined_constraints,
Lev Walkinb9fa7802004-08-25 02:04:39 +0000417 test_types[i], 0, 0, 0);
Lev Walkind541c252004-09-05 10:36:22 +0000418 if(!range && errno == EPERM) {
Lev Walkin9288d1c2005-03-10 11:27:13 +0000419 FATAL("This error happened for \"%s\" (meta %d) "
420 "at line %d",
Lev Walkind541c252004-09-05 10:36:22 +0000421 arg->expr->Identifier,
422 arg->expr->meta_type,
423 arg->expr->_lineno);
Lev Walkin1ef05162004-08-25 00:42:25 +0000424 return -1;
Lev Walkind541c252004-09-05 10:36:22 +0000425 }
Lev Walkin1ef05162004-08-25 00:42:25 +0000426 asn1constraint_range_free(range);
Lev Walkinb45e0672004-08-18 05:42:05 +0000427 }
Lev Walkin1ef05162004-08-25 00:42:25 +0000428
Lev Walkinf15320b2004-06-03 03:38:44 +0000429 return rvalue;
430}
431
Lev Walkinf593f102005-02-22 07:59:59 +0000432static int
433asn1f_check_duplicate(arg_t *arg) {
434 arg_t tmparg = *arg;
435
436 /*
437 * This is a linear scan in search of a similar type.
438 * The linear scan is just fine for the task, no need to over-optimize.
439 */
440 TQ_FOR(tmparg.mod, &arg->asn->modules, mod_next) {
441 TQ_FOR(tmparg.expr, &(tmparg.mod->members), next) {
442 assert(tmparg.expr->Identifier);
443 assert(arg->expr->Identifier);
444 if(tmparg.expr == arg->expr) break;
445
446 if(strcmp(tmparg.expr->Identifier,
447 arg->expr->Identifier) == 0) {
448 int diff_files = strcmp(arg->mod->source_file_name, tmparg.mod->source_file_name) ? 1 : 0;
449 FATAL("ASN.1 expression \"%s\" at line %d of module %s\n"
450 "clashes with expression \"%s\" at line %d of module %s"
451 "%s%s%s.\n"
452 "Please rename either instance to resolve the conflict",
453 arg->expr->Identifier,
454 arg->expr->_lineno,
Lev Walkinb36317c2005-08-12 10:09:10 +0000455 arg->mod->ModuleName,
Lev Walkinf593f102005-02-22 07:59:59 +0000456 tmparg.expr->Identifier,
457 tmparg.expr->_lineno,
Lev Walkinb36317c2005-08-12 10:09:10 +0000458 tmparg.mod->ModuleName,
Lev Walkinf593f102005-02-22 07:59:59 +0000459 diff_files ? " (" : "",
460 diff_files ? tmparg.mod->source_file_name : "",
461 diff_files ? ")" : ""
462 );
463 return -1;
464 }
465 }
466 if(tmparg.mod == arg->mod) break;
467 }
468
469 return 0;
470}
471
Lev Walkin1dec5802005-03-04 09:02:27 +0000472static int
473asn1f_apply_unique_index(arg_t *arg) {
474 static int unique_index;
475 if(!arg) { unique_index = 0; return 0; }
476
Lev Walkin1dec5802005-03-04 09:02:27 +0000477 arg->expr->_type_unique_index = ++unique_index;
478
479 return 0;
480}
481
Lev Walkinf15320b2004-06-03 03:38:44 +0000482/*
483 * Print everything to stderr
484 */
485static void
486_default_error_logger(int _severity, const char *fmt, ...) {
487 va_list ap;
488 char *pfx = "";
489
490 switch(_severity) {
491 case -1: pfx = "DEBUG: "; break;
492 case 0: pfx = "WARNING: "; break;
493 case 1: pfx = "FATAL: "; break;
494 }
495
496 fprintf(stderr, "%s", pfx);
497 va_start(ap, fmt);
498 vfprintf(stderr, fmt, ap);
499 va_end(ap);
500 fprintf(stderr, "\n");
501}