blob: ae3042d98100ba531db983caf1d750d78cdd199e [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#include "asn1fix_internal.h"
2
vlm93226382005-03-20 11:27:19 +00003enum ftt_what {
4 FTT_TYPE, /* Find the type of the given expression */
5 FTT_VALUE, /* Find the value of the given expression */
6};
7
8static asn1p_expr_t *asn1f_find_terminal_thing(arg_t *arg, asn1p_expr_t *expr, enum ftt_what);
vlmfa67ddc2004-06-03 03:38:44 +00009static int asn1f_compatible_with_exports(arg_t *arg, asn1p_module_t *mod, const char *name);
10
11
12/*
13 * Lookup a child by its name.
14 */
15asn1p_expr_t *
16asn1f_lookup_child(asn1p_expr_t *tc, const char *name) {
17 asn1p_expr_t *child_tc;
18
19 TQ_FOR(child_tc, &(tc->members), next) {
20 if(child_tc->Identifier
21 && strcmp(child_tc->Identifier, name) == 0) {
22 return child_tc;
23 }
24 }
25
26 errno = ESRCH;
27 return NULL;
28}
29
30asn1p_module_t *
vlmf98e07a2004-09-15 11:46:47 +000031asn1f_lookup_in_imports(arg_t *arg, asn1p_module_t *mod, const char *name) {
vlmfa67ddc2004-06-03 03:38:44 +000032 asn1p_xports_t *xp;
33 asn1p_expr_t *tc;
34
35 /*
36 * Search in which exactly module this name is defined.
37 */
vlmf98e07a2004-09-15 11:46:47 +000038 TQ_FOR(xp, &(mod->imports), xp_next) {
vlmfa67ddc2004-06-03 03:38:44 +000039 TQ_FOR(tc, &(xp->members), next) {
40 if(strcmp(name, tc->Identifier) == 0)
41 break;
42 }
43 if(tc) break;
44 }
45 if(xp == NULL) {
46 errno = ESRCH;
47 return NULL;
48 }
49
50 /*
51 * Okay, right now we have a module name and, hopefully, an OID.
52 * Search the arg->asn for the specified module.
53 */
vlm931aeed2005-08-12 10:09:10 +000054 mod = asn1f_lookup_module(arg, xp->fromModuleName, xp->identifier.oid);
vlmfa67ddc2004-06-03 03:38:44 +000055 if(mod == NULL) {
vlmf1d86c92005-03-18 05:20:39 +000056 /* Conditional debug */
57 if(!(arg->expr->_mark & TM_BROKEN)) {
58 arg->expr->_mark |= TM_BROKEN;
vlmcfd23ac2005-04-12 17:46:30 +000059 FATAL("Cannot find external module \"%s\" "
60 "mentioned for "
61 "\"%s\" at line %d",
vlm931aeed2005-08-12 10:09:10 +000062 xp->fromModuleName, name, arg->expr->_lineno);
vlmf1d86c92005-03-18 05:20:39 +000063 }
vlmfa67ddc2004-06-03 03:38:44 +000064 /* ENOENT/ETOOMANYREFS */
65 return NULL;
66 }
67
68 /*
69 * Check that the EXPORTS section of this module contains
70 * the symbol we care about, or it is EXPORTS ALL.
71 */
72 if(asn1f_compatible_with_exports(arg, mod, name)) {
73 errno = EPERM;
74 return NULL;
75 }
76
77 return mod;
78}
79
80asn1p_module_t *
81asn1f_lookup_module(arg_t *arg, const char *module_name, asn1p_oid_t *oid) {
82 asn1p_module_t *mod;
83
84 assert(module_name);
85
86 /*
87 * If OID is given, the module_name is unused.
88 * If OID is not given, the module_name may mean
89 * either the real module's name, or the symbol which is the
90 * result of renaming. Check this first.
91 */
92 if(oid == 0) {
93 asn1p_xports_t *xp;
94 /*
95 * Check inside the IMPORTS section for possible renaming.
96 * Renaming practically means that a module_name is mentioned
97 * somewhere in the IMPORTS section AND OID is given.
98 */
99 TQ_FOR(xp, &(arg->mod->imports), xp_next) {
vlm931aeed2005-08-12 10:09:10 +0000100 if(strcmp(module_name, xp->fromModuleName))
vlmfa67ddc2004-06-03 03:38:44 +0000101 continue;
102 if(oid) {
103 FATAL("Ambiguous reference: "
104 "%s "
105 "matches several modules",
106 module_name);
107 errno = ETOOMANYREFS;
108 return NULL;
109 }
110 /*
111 * Yes, there is a renaming.
112 * Make lookup use OID instead.
113 */
vlm931aeed2005-08-12 10:09:10 +0000114 oid = xp->identifier.oid;
vlmfa67ddc2004-06-03 03:38:44 +0000115 }
116 }
117
118 /*
119 * Perform lookup using OID or module_name.
120 */
121 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
122 if(oid) {
123 if(mod->module_oid) {
124 if(asn1p_oid_compare(oid,
125 mod->module_oid)) {
126 continue;
127 } else {
128 /* Match! Even if name doesn't. */
129 return mod;
130 }
131 } else {
132 /* Not match, even if name is the same. */
133 continue;
134 }
135 }
136
vlm931aeed2005-08-12 10:09:10 +0000137 if(strcmp(module_name, mod->ModuleName) == 0)
vlmfa67ddc2004-06-03 03:38:44 +0000138 return mod;
139 }
140
141 DEBUG("\tModule \"%s\" not found", module_name);
142
143 errno = ENOENT;
144 return NULL;
145}
146
147
148
149asn1p_expr_t *
vlm2e0c1942004-08-22 03:10:23 +0000150asn1f_lookup_symbol(arg_t *arg, asn1p_module_t *mod, asn1p_ref_t *ref) {
vlmfa67ddc2004-06-03 03:38:44 +0000151 asn1p_expr_t *ref_tc; /* Referenced tc */
vlmf98e07a2004-09-15 11:46:47 +0000152 asn1p_module_t *imports_from;
vlmfa67ddc2004-06-03 03:38:44 +0000153 char *modulename;
154 char *identifier;
155
156 /*
157 * First of all, a reference to a symbol may be specified
158 * using several possible forms:
159 * a) simple identifier
160 * v INTEGER ::= value
161 * b) external reference
162 * v INTEGER ::= Module1.value
163 * c) class-related stuff (the most complex stuff)
164 * v ::= <[A-Z][A-Z0-9a-z-]*>.&<[A-Z0-9a-z-]+>.
165 * All other forms are not implemented at this moment.
166 */
167
vlmfd245932005-03-10 10:02:50 +0000168 DEBUG("(%s) in %s for line %d",
vlmfa67ddc2004-06-03 03:38:44 +0000169 asn1f_printable_reference(ref),
vlm931aeed2005-08-12 10:09:10 +0000170 mod->ModuleName,
vlmfa67ddc2004-06-03 03:38:44 +0000171 ref->_lineno);
172
173 if(ref->comp_count == 1) {
174 modulename = NULL;
175 identifier = ref->components[0].name;
176 } else if(ref->comp_count == 2
177 && ref->components[1].name[0] != '&') {
178 modulename = ref->components[0].name;
179 identifier = ref->components[1].name;
180 } else if(ref->comp_count > 1
181 && isupper(ref->components[0].name[0])
182 && ref->components[1].name[0] == '&') {
183 asn1p_expr_t *extract;
184 /*
185 * This is a reference to a CLASS-related stuff.
186 * Employ a separate function for that.
187 */
vlm2e0c1942004-08-22 03:10:23 +0000188 extract = asn1f_class_access(arg, mod, ref);
vlmfa67ddc2004-06-03 03:38:44 +0000189
190 return extract;
191 } else {
192 DEBUG("\tToo many components: %d", ref->comp_count);
193 errno = EINVAL;
194 return NULL;
195 }
196
197 /*
198 * If module name is specified explicitly
199 * OR the current module's IMPORTS clause contains the identifier,
200 * fetch that module.
201 */
202 if(modulename) {
vlmf98e07a2004-09-15 11:46:47 +0000203 imports_from = asn1f_lookup_module(arg, modulename, 0);
204 if(imports_from == NULL) {
vlmfa67ddc2004-06-03 03:38:44 +0000205 FATAL("Module \"%s\" "
206 "mentioned at line %d is not found",
207 modulename, ref->_lineno);
208 return NULL;
209 }
210
211 /*
212 * Check that the EXPORTS section of this module contains
213 * the symbol we care about, or it is EXPORTS ALL.
214 */
vlmf98e07a2004-09-15 11:46:47 +0000215 if(asn1f_compatible_with_exports(arg,imports_from,identifier)) {
vlmfa67ddc2004-06-03 03:38:44 +0000216 errno = EPERM;
217 return NULL;
218 }
219 } else {
vlm79d4e632006-03-06 14:51:00 +0000220 /* Search inside the IMPORTS section of the current module */
vlmf98e07a2004-09-15 11:46:47 +0000221 imports_from = asn1f_lookup_in_imports(arg, mod, identifier);
222 if(imports_from == NULL && errno != ESRCH) {
vlmfa67ddc2004-06-03 03:38:44 +0000223 /*
224 * Return only of the name was not found.
225 * If module was not found or more serious error
226 * encountered, just return preserving the errno.
227 */
228 return NULL;
229 }
230 }
231
vlmf98e07a2004-09-15 11:46:47 +0000232 /*
233 * The symbol is being imported from another module.
234 */
vlm79d4e632006-03-06 14:51:00 +0000235 importing:
vlmf98e07a2004-09-15 11:46:47 +0000236 if(imports_from) {
237 asn1p_ref_t tmpref = *ref;
vlmf1d86c92005-03-18 05:20:39 +0000238 asn1p_expr_t *expr;
vlmf98e07a2004-09-15 11:46:47 +0000239 if(modulename) {
240 /*
241 * The modulename is specified inside this reference.
242 * To avoid recursion, reformat the reference
243 * as it were local to that module.
244 */
245 tmpref.components++; /* Hide the first element */
246 tmpref.comp_count--;
247 assert(tmpref.comp_count > 0);
248 }
vlmf1d86c92005-03-18 05:20:39 +0000249
250 expr = asn1f_lookup_symbol(arg, imports_from, &tmpref);
vlmd938e0b2006-03-16 11:04:55 +0000251 if(!expr && !(arg->expr->_mark & TM_BROKEN)
252 && !(imports_from->_tags & MT_STANDARD_MODULE)) {
vlmf1d86c92005-03-18 05:20:39 +0000253 arg->expr->_mark |= TM_BROKEN;
254 if(modulename) {
255 FATAL("Module %s referred by %s in module %s "
256 "does not contain the requested symbol",
vlm931aeed2005-08-12 10:09:10 +0000257 imports_from->ModuleName,
vlmf1d86c92005-03-18 05:20:39 +0000258 asn1f_printable_reference(ref),
vlm931aeed2005-08-12 10:09:10 +0000259 mod->ModuleName);
vlmf1d86c92005-03-18 05:20:39 +0000260 } else {
261 FATAL("Module %s referred in IMPORTS section "
262 "for %s of module %s does not contain "
263 "the requested symbol",
vlm931aeed2005-08-12 10:09:10 +0000264 imports_from->ModuleName,
vlmf1d86c92005-03-18 05:20:39 +0000265 asn1f_printable_reference(ref),
vlm931aeed2005-08-12 10:09:10 +0000266 mod->ModuleName);
vlmf1d86c92005-03-18 05:20:39 +0000267 }
268 }
269 return expr;
vlmf98e07a2004-09-15 11:46:47 +0000270 }
vlmfa67ddc2004-06-03 03:38:44 +0000271
272 /*
vlmf98e07a2004-09-15 11:46:47 +0000273 * Now we know where to search for a value: in the current module.
vlmfa67ddc2004-06-03 03:38:44 +0000274 */
vlmf98e07a2004-09-15 11:46:47 +0000275 TQ_FOR(ref_tc, &(mod->members), next) {
vlmfa67ddc2004-06-03 03:38:44 +0000276 if(ref_tc->Identifier)
277 if(strcmp(ref_tc->Identifier, identifier) == 0)
278 break;
279 }
vlm79d4e632006-03-06 14:51:00 +0000280 if(ref_tc)
281 return ref_tc;
282
283 {
284 /* Search inside standard module */
285 static asn1p_oid_t *uioc_oid;
286 if(!uioc_oid) {
287 asn1p_oid_arc_t arcs[] = {
288 { 1, "iso" },
289 { 3, "org" },
290 { 6, "dod" },
291 { 1, "internet" },
292 { 4, "private" },
293 { 1, "enterprise" },
294 { 9363, "spelio" },
295 { 1, "software" },
296 { 5, "asn1c" },
297 { 3, "standard-modules" },
298 { 0, "auto-imported" },
299 { 1, 0 }
300 };
301 uioc_oid = asn1p_oid_construct(arcs,
302 sizeof(arcs)/sizeof(arcs[0]));
vlmd9cd3f92004-06-28 21:21:24 +0000303 }
vlm79d4e632006-03-06 14:51:00 +0000304 if(!imports_from && mod->module_oid
305 && asn1p_oid_compare(mod->module_oid, uioc_oid)) {
306 imports_from = asn1f_lookup_module(arg,
307 "ASN1C-UsefulInformationObjectClasses",
308 uioc_oid);
309 if(imports_from) goto importing;
310 }
vlmfa67ddc2004-06-03 03:38:44 +0000311 }
312
vlm79d4e632006-03-06 14:51:00 +0000313 DEBUG("Module \"%s\" does not contain \"%s\" "
314 "mentioned at line %d: %s",
315 mod->ModuleName,
316 identifier,
317 ref->_lineno,
318 strerror(errno));
319
320 if(asn1f_check_known_external_type(identifier) == 0) {
321 errno = EEXIST; /* Exists somewhere */
322 } else {
323 errno = ESRCH;
324 }
325 return NULL;
vlmfa67ddc2004-06-03 03:38:44 +0000326}
327
328
329asn1p_expr_t *
vlm2e0c1942004-08-22 03:10:23 +0000330asn1f_find_terminal_type(arg_t *arg, asn1p_expr_t *expr) {
vlm93226382005-03-20 11:27:19 +0000331 return asn1f_find_terminal_thing(arg, expr, FTT_TYPE);
vlmfa67ddc2004-06-03 03:38:44 +0000332}
333
334asn1p_expr_t *
vlm2e0c1942004-08-22 03:10:23 +0000335asn1f_find_terminal_value(arg_t *arg, asn1p_expr_t *expr) {
vlm93226382005-03-20 11:27:19 +0000336 return asn1f_find_terminal_thing(arg, expr, FTT_VALUE);
vlmfa67ddc2004-06-03 03:38:44 +0000337}
338
339static asn1p_expr_t *
vlm93226382005-03-20 11:27:19 +0000340asn1f_find_terminal_thing(arg_t *arg, asn1p_expr_t *expr, enum ftt_what what) {
vlm2fb3f752005-03-20 13:17:48 +0000341 asn1p_ref_t *ref = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000342 asn1p_expr_t *tc;
343
vlm93226382005-03-20 11:27:19 +0000344 switch(what) {
345 case FTT_TYPE:
346 /* Expression may be a terminal type itself */
347 if(expr->expr_type != A1TC_REFERENCE)
348 return expr;
349 ref = expr->reference;
350 break;
351 case FTT_VALUE:
vlmfa67ddc2004-06-03 03:38:44 +0000352 assert(expr->meta_type == AMT_VALUE);
353 assert(expr->value);
vlm2e0c1942004-08-22 03:10:23 +0000354 /* Expression may be a terminal type itself */
355 if(expr->value->type != ATV_REFERENCED)
vlmfa67ddc2004-06-03 03:38:44 +0000356 return expr;
vlmfa67ddc2004-06-03 03:38:44 +0000357 ref = expr->value->value.reference;
vlm93226382005-03-20 11:27:19 +0000358 break;
vlmfa67ddc2004-06-03 03:38:44 +0000359 }
360
vlmfd245932005-03-10 10:02:50 +0000361 DEBUG("%s(%s->%s) for line %d",
vlm93226382005-03-20 11:27:19 +0000362 (what == FTT_VALUE)?"VALUE":"TYPE",
vlmfa67ddc2004-06-03 03:38:44 +0000363 expr->Identifier, asn1f_printable_reference(ref),
364 expr->_lineno);
365
366 assert(ref);
367
368 /*
369 * Lookup inside the type itself (ENUMERATED, INTEGER, etc).
370 */
vlm93226382005-03-20 11:27:19 +0000371 if(what == FTT_VALUE) {
vlmfa67ddc2004-06-03 03:38:44 +0000372 asn1p_expr_t *val_type_tc;
vlm2e0c1942004-08-22 03:10:23 +0000373 val_type_tc = asn1f_find_terminal_type(arg, expr);
vlmfa67ddc2004-06-03 03:38:44 +0000374 if(val_type_tc
375 && asn1f_look_value_in_type(arg, val_type_tc, expr))
376 return NULL;
377 if(expr->value->type != ATV_REFERENCED) {
vlmfa67ddc2004-06-03 03:38:44 +0000378 return expr;
379 }
380 assert(ref == expr->value->value.reference);
381 ref = expr->value->value.reference;
382 }
383
384 /*
vlmf1d86c92005-03-18 05:20:39 +0000385 * Lookup inside the default module and its IMPORTS section.
vlmfa67ddc2004-06-03 03:38:44 +0000386 */
vlm2e0c1942004-08-22 03:10:23 +0000387 tc = asn1f_lookup_symbol(arg, expr->module, ref);
vlmfa67ddc2004-06-03 03:38:44 +0000388 if(tc == NULL) {
vlmd9cd3f92004-06-28 21:21:24 +0000389 DEBUG("\tSymbol \"%s\" not found: %s",
390 asn1f_printable_reference(ref),
391 strerror(errno));
vlmfa67ddc2004-06-03 03:38:44 +0000392 return NULL;
393 }
394
395 /*
396 * Recursive loops detection.
397 */
398 if(tc->_mark & TM_RECURSION) {
399 DEBUG("Recursion loop detected for %s at line %d",
400 asn1f_printable_reference(ref), ref->_lineno);
401 errno = EPERM;
402 return NULL;
403 }
404
vlm337167e2005-11-26 11:25:14 +0000405 tc->_type_referenced = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000406 tc->_mark |= TM_RECURSION;
vlm2e0c1942004-08-22 03:10:23 +0000407 WITH_MODULE(tc->module,
vlm93226382005-03-20 11:27:19 +0000408 expr = asn1f_find_terminal_thing(arg, tc, what));
vlmfa67ddc2004-06-03 03:38:44 +0000409 tc->_mark &= ~TM_RECURSION;
410
411 return expr;
412}
413
414/*
415 * Make sure that the specified name is present or otherwise does
416 * not contradict with the EXPORTS clause of the specified module.
417 */
418static int
419asn1f_compatible_with_exports(arg_t *arg, asn1p_module_t *mod, const char *name) {
420 asn1p_xports_t *exports;
421 asn1p_expr_t *item;
422
423 assert(mod);
424 assert(name);
425
426 exports = TQ_FIRST(&(mod->exports));
427 if(exports == NULL) {
428 /* No EXPORTS section or EXPORTS ALL; */
429 return 0;
430 }
431
432 TQ_FOR(item, &(exports->members), next) {
433 if(strcmp(item->Identifier, name) == 0)
434 return 0;
435 }
436
vlmf1d86c92005-03-18 05:20:39 +0000437 /* Conditional debug */
438 if(!(arg->expr->_mark & TM_BROKEN)) {
439 arg->expr->_mark |= TM_BROKEN;
440 FATAL("EXPORTS section of module %s in %s "
441 "does not mention %s at line %d",
vlm931aeed2005-08-12 10:09:10 +0000442 mod->ModuleName, mod->source_file_name, name,
vlmf1d86c92005-03-18 05:20:39 +0000443 arg->expr->_lineno);
444 }
vlmfa67ddc2004-06-03 03:38:44 +0000445
446 errno = ESRCH;
447 return -1;
448}