blob: d2be70d9a98bf74eae1ab2a173c11271163f9828 [file] [log] [blame]
Lev Walkincb4cd5b2006-03-14 15:23:06 +00001#include "asn1fix_internal.h"
Lev Walkinaa7f5302006-03-14 15:53:59 +00002#include "asn1fix_cws.h"
Lev Walkin9d542d22006-03-14 16:31:37 +00003
Lev Walkind370e9f2006-03-16 10:03:35 +00004static int _asn1f_parse_class_object_data(arg_t *, asn1p_expr_t *eclass,
5 struct asn1p_ioc_row_s *row, asn1p_wsyntx_t *syntax,
Lev Walkinea6635b2017-08-06 23:23:04 -07006 const uint8_t *buf, const uint8_t *bend,
Lev Walkind357f3d2017-08-10 17:40:37 -07007 int optional_mode, const uint8_t **newpos, int counter);
8static int _asn1f_assign_cell_value(arg_t *arg, struct asn1p_ioc_cell_s *cell, const uint8_t *buf, const uint8_t *bend, int counter);
Lev Walkinea6635b2017-08-06 23:23:04 -07009static asn1p_wsyntx_chunk_t *asn1f_next_literal_chunk(asn1p_wsyntx_t *syntax, asn1p_wsyntx_chunk_t *chunk, const uint8_t *buf);
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +080010
11int
12asn1f_check_class_object(arg_t *arg) {
13 asn1p_expr_t *expr = arg->expr;
14 asn1p_expr_t *eclass;
15 asn1p_ioc_row_t *row;
16 int ret;
17
Lev Walkinea6635b2017-08-06 23:23:04 -070018 if(expr->meta_type != AMT_VALUESET
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +080019 || expr->expr_type != A1TC_REFERENCE
20 || !expr->value
Lev Walkinea6635b2017-08-06 23:23:04 -070021 || expr->value->type != ATV_UNPARSED) {
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +080022 return 0;
Lev Walkinea6635b2017-08-06 23:23:04 -070023 }
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +080024
25 eclass = asn1f_find_terminal_type(arg, expr);
26 if(!eclass
27 || eclass->meta_type != AMT_OBJECTCLASS
28 || eclass->expr_type != A1TC_CLASSDEF) {
29 return 0;
30 }
31
32 if(!eclass->with_syntax) {
33 DEBUG("Can't process classes without %s just yet",
34 "WITH SYNTAX");
35 return 0;
36 }
37
38 row = asn1p_ioc_row_new(eclass);
39 assert(row);
40
41 ret = _asn1f_parse_class_object_data(arg, eclass, row,
42 eclass->with_syntax,
43 expr->value->value.string.buf + 1,
44 expr->value->value.string.buf
45 + expr->value->value.string.size - 1,
Lev Walkind357f3d2017-08-10 17:40:37 -070046 0, 0, 0);
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +080047
48 asn1p_ioc_row_delete(row);
49
50 return ret;
51}
Lev Walkin9d542d22006-03-14 16:31:37 +000052
Lev Walkinea6635b2017-08-06 23:23:04 -070053static int
Lev Walkin4dcf8362017-08-07 20:10:05 -070054_asn1f_is_ioc_row_duplicate(asn1p_ioc_table_t *it, asn1p_ioc_row_t *row) {
55 if(!it) {
56 return 0;
57 }
58
59 for(size_t i = 0; i < it->rows; i++) {
60 switch(asn1p_ioc_row_match(it->row[i], row)) {
Lev Walkinea6635b2017-08-06 23:23:04 -070061 default:
62 case -1:
63 return -1;
64 case 1:
65 continue;
66 case 0:
67 return 1; /* Duplicate! */
68 }
69 }
70 return 0;
71}
72
73struct parse_object_key {
74 arg_t *arg;
75 asn1p_expr_t *expr; /* InformationObjectSet */
76 asn1p_expr_t *eclass; /* CLASS */
Lev Walkind357f3d2017-08-10 17:40:37 -070077 int sequence; /* Sequence counter */
Lev Walkinea6635b2017-08-06 23:23:04 -070078};
79
Lev Walkin4dcf8362017-08-07 20:10:05 -070080/*
81 * Add to the IoC table if the row is unique.
82 */
Lev Walkinea6635b2017-08-06 23:23:04 -070083static int
Lev Walkin4dcf8362017-08-07 20:10:05 -070084_asn1f_add_unique_row(arg_t *arg, asn1p_expr_t *expr, asn1p_ioc_row_t *row) {
Lev Walkin53a28a22017-08-23 09:56:53 -070085 assert(expr->ioc_table);
Lev Walkinea6635b2017-08-06 23:23:04 -070086
Lev Walkin53a28a22017-08-23 09:56:53 -070087 /* Look for duplicates */
88 switch(_asn1f_is_ioc_row_duplicate(expr->ioc_table, row)) {
89 case -1:
90 DEBUG("Found Information Object Duplicate in %s", expr->Identifier,
91 expr->_lineno);
92 return -1;
93 case 0:
94 /* Not a duplicate */
95 break;
96 case 1:
97 /* Proper duplicate detected; ignore */
98 asn1p_ioc_row_delete(row);
99 return 0;
Lev Walkin4dcf8362017-08-07 20:10:05 -0700100 }
Lev Walkinea6635b2017-08-06 23:23:04 -0700101
Lev Walkin4dcf8362017-08-07 20:10:05 -0700102 asn1p_ioc_table_add(expr->ioc_table, row);
Lev Walkinea6635b2017-08-06 23:23:04 -0700103
104 return 0;
105}
106
107/*
108 * Given a single blob of unparsed Information Object specification, parse it
109 * into a given InformationObjectSet.
110 */
111static int
112_asn1f_parse_object_cb(const uint8_t *buf, size_t size, void *keyp) {
113 struct parse_object_key *key = keyp;
114 arg_t *arg = key->arg;
115 asn1p_expr_t *expr = key->expr;
116 asn1p_expr_t *eclass = key->eclass;
117 asn1p_ioc_row_t *row;
118 int ret;
119
Lev Walkind357f3d2017-08-10 17:40:37 -0700120 key->sequence++;
121
Lev Walkinea6635b2017-08-06 23:23:04 -0700122 row = asn1p_ioc_row_new(eclass);
123 assert(row);
124
125 ret = _asn1f_parse_class_object_data(arg, eclass, row, eclass->with_syntax,
Lev Walkind357f3d2017-08-10 17:40:37 -0700126 buf, buf + size, 0, 0, key->sequence);
Lev Walkinea6635b2017-08-06 23:23:04 -0700127 if(ret) {
Lev Walkin48e82d12017-10-19 03:06:35 -0700128 LOG(ret, "Cannot parse %s of CLASS %s found at line %d",
129 expr->Identifier, eclass->Identifier, expr->_lineno);
130 asn1p_ioc_row_delete(row);
Lev Walkinea6635b2017-08-06 23:23:04 -0700131 return ret;
132 }
133
134 /* Add object to a CLASS. */
Lev Walkin4dcf8362017-08-07 20:10:05 -0700135 if(_asn1f_add_unique_row(arg, eclass, row) != 0)
Lev Walkinea6635b2017-08-06 23:23:04 -0700136 return -1;
137
138 /*
139 * Add a copy of the object to the Information Object Set.
140 */
141 row = asn1p_ioc_row_new(eclass);
142 assert(row);
143 ret = _asn1f_parse_class_object_data(arg, eclass, row, eclass->with_syntax,
Lev Walkind357f3d2017-08-10 17:40:37 -0700144 buf, buf + size, 0, 0, key->sequence);
Lev Walkinea6635b2017-08-06 23:23:04 -0700145 assert(ret == 0);
146
Lev Walkin4dcf8362017-08-07 20:10:05 -0700147 if(_asn1f_add_unique_row(arg, expr, row) != 0)
Lev Walkinea6635b2017-08-06 23:23:04 -0700148 return -1;
149
150 return 0;
151}
152
153static int
154_asn1f_foreach_unparsed_union(const asn1p_constraint_t *ct_union,
155 int (*process)(const uint8_t *buf, size_t size,
156 void *key),
157 void *key) {
158 assert(ct_union->type == ACT_CA_UNI);
159
160 for(size_t j = 0; j < ct_union->el_count; j++) {
161 const asn1p_constraint_t *ct2 = ct_union->elements[j];
162 if(ct2->type == ACT_EL_VALUE && ct2->value->type == ATV_UNPARSED) {
163 if(process
164 && process(ct2->value->value.string.buf + 1,
165 ct2->value->value.string.size - 2, key)
166 != 0) {
167 return -1;
168 }
169 continue;
170 }
171 return -1;
172 }
173
174 return 0;
175}
176
177static int
Lev Walkin53a28a22017-08-23 09:56:53 -0700178_asn1f_foreach_unparsed(arg_t *arg, const asn1p_constraint_t *ct,
Lev Walkinea6635b2017-08-06 23:23:04 -0700179 int (*process)(const uint8_t *buf, size_t size,
180 void *key),
Lev Walkinade508c2017-08-23 10:19:30 -0700181 void *keyp) {
182 struct parse_object_key *key = keyp;
Lev Walkinea6635b2017-08-06 23:23:04 -0700183 if(!ct) return -1;
Lev Walkin53a28a22017-08-23 09:56:53 -0700184
185 switch(ct->type) {
186 default:
Lev Walkinade508c2017-08-23 10:19:30 -0700187 DEBUG("Constraint is of unknown type %d for CWS", ct->type);
Lev Walkin53a28a22017-08-23 09:56:53 -0700188 return -1;
189 case ACT_EL_EXT: /* ... */
Lev Walkinade508c2017-08-23 10:19:30 -0700190 if(key) {
191 key->expr->ioc_table->extensible = 1;
192 }
Lev Walkin53a28a22017-08-23 09:56:53 -0700193 return 0;
194 case ACT_CA_UNI: /* | */
Lev Walkinade508c2017-08-23 10:19:30 -0700195 return _asn1f_foreach_unparsed_union(ct, process, keyp);
Lev Walkin53a28a22017-08-23 09:56:53 -0700196 case ACT_CA_CSV: /* , */
197 break;
Lev Walkinea6635b2017-08-06 23:23:04 -0700198 }
Lev Walkinea6635b2017-08-06 23:23:04 -0700199
200 for(size_t i = 0; i < ct->el_count; i++) {
201 const asn1p_constraint_t *ct1 = ct->elements[i];
Lev Walkinade508c2017-08-23 10:19:30 -0700202 if(_asn1f_foreach_unparsed(arg, ct1, process, keyp) != 0) {
Lev Walkinea6635b2017-08-06 23:23:04 -0700203 return -1;
204 }
205 }
206
207 return 0;
208}
209
210static int
Lev Walkin53a28a22017-08-23 09:56:53 -0700211_asn1f_constraint_looks_like_object_set(arg_t *arg,
212 const asn1p_constraint_t *ct) {
213 return 0 == _asn1f_foreach_unparsed(arg, ct, NULL, NULL);
Lev Walkinea6635b2017-08-06 23:23:04 -0700214}
215
Lev Walkind370e9f2006-03-16 10:03:35 +0000216int
217asn1f_parse_class_object(arg_t *arg) {
218 asn1p_expr_t *expr = arg->expr;
219 asn1p_expr_t *eclass;
Lev Walkinea6635b2017-08-06 23:23:04 -0700220 enum {
221 FROM_VALUE,
222 FROM_CONSTRAINT,
223 } source = FROM_VALUE;
Lev Walkind370e9f2006-03-16 10:03:35 +0000224
Lev Walkinea6635b2017-08-06 23:23:04 -0700225 if(expr->meta_type == AMT_VALUE
226 && expr->expr_type == A1TC_REFERENCE
227 && expr->value && expr->value->type == ATV_UNPARSED) {
228 source = FROM_VALUE;
229 } else if(expr->meta_type != AMT_VALUESET
230 || expr->expr_type != A1TC_REFERENCE) {
231 return 0;
232 } else if(expr->value && expr->value->type == ATV_UNPARSED) {
233 source = FROM_VALUE;
234 } else if(!expr->value) {
Lev Walkin53a28a22017-08-23 09:56:53 -0700235 if(_asn1f_constraint_looks_like_object_set(arg, expr->constraints)) {
Lev Walkinea6635b2017-08-06 23:23:04 -0700236 source = FROM_CONSTRAINT;
237 } else {
238 return 0;
239 }
240 } else {
241 return 0;
242 }
Lev Walkin9d542d22006-03-14 16:31:37 +0000243
Lev Walkind370e9f2006-03-16 10:03:35 +0000244 /*
245 * Find the governing class.
246 */
247 eclass = asn1f_find_terminal_type(arg, expr);
248 if(!eclass
249 || eclass->meta_type != AMT_OBJECTCLASS
250 || eclass->expr_type != A1TC_CLASSDEF) {
251 return 0;
252 }
253
254 DEBUG("Value %s of CLASS %s found at line %d",
255 expr->Identifier, eclass->Identifier, expr->_lineno);
256
257 if(!eclass->with_syntax) {
Lev Walkinfaf35a82006-03-16 13:37:03 +0000258 DEBUG("Can't process classes without %s just yet",
259 "WITH SYNTAX");
Lev Walkind370e9f2006-03-16 10:03:35 +0000260 return 0;
261 }
262
Lev Walkinea6635b2017-08-06 23:23:04 -0700263 struct parse_object_key key = {
264 .arg = arg,
265 .expr = expr,
266 .eclass = eclass,
Lev Walkind357f3d2017-08-10 17:40:37 -0700267 .sequence = 0
Lev Walkinea6635b2017-08-06 23:23:04 -0700268 };
Lev Walkind370e9f2006-03-16 10:03:35 +0000269
Lev Walkin53a28a22017-08-23 09:56:53 -0700270 if(!expr->ioc_table) {
271 expr->ioc_table = asn1p_ioc_table_new();
272 }
273
274 if(!eclass->ioc_table) {
275 eclass->ioc_table = asn1p_ioc_table_new();
276 }
277
Lev Walkinea6635b2017-08-06 23:23:04 -0700278 switch(source) {
279 case FROM_VALUE:
280 if(_asn1f_parse_object_cb(expr->value->value.string.buf + 1,
281 expr->value->value.string.size - 2, &key)
282 != 0) {
283 return -1;
284 }
285 break;
286 case FROM_CONSTRAINT:
Lev Walkin53a28a22017-08-23 09:56:53 -0700287 if(_asn1f_foreach_unparsed(arg, expr->constraints,
288 _asn1f_parse_object_cb, &key)
Lev Walkinea6635b2017-08-06 23:23:04 -0700289 != 0) {
290 return -1;
291 }
292 }
Lev Walkin9d542d22006-03-14 16:31:37 +0000293
294 return 0;
295}
Lev Walkind370e9f2006-03-16 10:03:35 +0000296
297#define SKIPSPACES for(; buf < bend && isspace(*buf); buf++)
298
299static int
300_asn1f_parse_class_object_data(arg_t *arg, asn1p_expr_t *eclass,
301 struct asn1p_ioc_row_s *row, asn1p_wsyntx_t *syntax,
Lev Walkinea6635b2017-08-06 23:23:04 -0700302 const uint8_t *buf, const uint8_t *bend,
Lev Walkind357f3d2017-08-10 17:40:37 -0700303 int optional_mode, const uint8_t **newpos, int counter) {
Lev Walkind370e9f2006-03-16 10:03:35 +0000304 struct asn1p_wsyntx_chunk_s *chunk;
305 int ret;
306
307 TQ_FOR(chunk, (&syntax->chunks), next) {
308 switch(chunk->type) {
309 case WC_LITERAL: {
310 int token_len = strlen(chunk->content.token);
311 SKIPSPACES;
312 if(bend - buf < token_len
313 || memcmp(buf, chunk->content.token, token_len)) {
314 if(!optional_mode) {
315 FATAL("While parsing object class value %s at line %d: Expected: \"%s\", found: \"%s\"",
316 arg->expr->Identifier, arg->expr->_lineno, chunk->content.token, buf);
317 }
318 if(newpos) *newpos = buf;
319 return -1;
320 }
321 buf += token_len;
322 } break;
323 case WC_WHITESPACE: break; /* Ignore whitespace */
324 case WC_FIELD: {
325 struct asn1p_ioc_cell_s *cell;
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800326 asn1p_wsyntx_chunk_t *next_literal;
Lev Walkinea6635b2017-08-06 23:23:04 -0700327 const uint8_t *buf_old = buf;
328 const uint8_t *p = 0;
Lev Walkinfaf35a82006-03-16 13:37:03 +0000329
330 SKIPSPACES;
331
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800332 next_literal = asn1f_next_literal_chunk(syntax, chunk, buf);
333 if(!next_literal) {
334 p += (bend - p);
335 } else {
Lev Walkinea6635b2017-08-06 23:23:04 -0700336 p = (uint8_t *)strstr((const char *)buf, (const char *)next_literal->content.token);
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800337 if(!p) {
338 if (!optional_mode)
339 FATAL("Next literal \"%s\" not found !", next_literal->content.token);
340
341 if(newpos) *newpos = buf_old;
342 return -1;
Lev Walkind370e9f2006-03-16 10:03:35 +0000343 }
344 }
Lev Walkind370e9f2006-03-16 10:03:35 +0000345 cell = asn1p_ioc_row_cell_fetch(row,
346 chunk->content.token);
347 if(cell == NULL) {
348 FATAL("Field reference %s found in WITH SYNAX {} clause does not match actual field in Object Class %s",
349 chunk->content.token,
350 eclass->Identifier, eclass->_lineno);
351 if(newpos) *newpos = buf;
352 return -1;
353 }
354 DEBUG("Reference %s satisfied by %s (%d)",
355 chunk->content.token,
356 buf, p - buf);
Lev Walkind357f3d2017-08-10 17:40:37 -0700357 ret = _asn1f_assign_cell_value(arg, cell, buf, p, counter);
Lev Walkindc4376d2006-03-16 11:04:55 +0000358 if(ret) return ret;
Lev Walkind370e9f2006-03-16 10:03:35 +0000359 buf = p;
Lev Walkindc4376d2006-03-16 11:04:55 +0000360 if(newpos) *newpos = buf;
Lev Walkind370e9f2006-03-16 10:03:35 +0000361 } break;
362 case WC_OPTIONALGROUP: {
Lev Walkinea6635b2017-08-06 23:23:04 -0700363 const uint8_t *np = 0;
Lev Walkind370e9f2006-03-16 10:03:35 +0000364 SKIPSPACES;
365 ret = _asn1f_parse_class_object_data(arg, eclass, row,
Lev Walkind357f3d2017-08-10 17:40:37 -0700366 chunk->content.syntax, buf, bend, 1, &np, counter);
Lev Walkind370e9f2006-03-16 10:03:35 +0000367 if(newpos) *newpos = np;
368 if(ret && np != buf)
369 return ret;
Lev Walkindc4376d2006-03-16 11:04:55 +0000370 buf = np;
Lev Walkind370e9f2006-03-16 10:03:35 +0000371 } break;
372 }
373 }
374
375
376 if(newpos) *newpos = buf;
377 return 0;
378}
379
380
381static int
Lev Walkin4dcf8362017-08-07 20:10:05 -0700382_asn1f_assign_cell_value(arg_t *arg, struct asn1p_ioc_cell_s *cell,
Lev Walkind357f3d2017-08-10 17:40:37 -0700383 const uint8_t *buf, const uint8_t *bend, int counter) {
Lev Walkinea6635b2017-08-06 23:23:04 -0700384 asn1p_expr_t *expr = (asn1p_expr_t *)NULL;
Lev Walkind357f3d2017-08-10 17:40:37 -0700385 char *mivr; /* Most Immediate Value Representation */
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800386 int new_ref = 1;
387 asn1p_t *asn;
388 asn1p_module_t *mod;
389 asn1p_expr_t *type_expr = (asn1p_expr_t *)NULL;
390 int i, ret = 0, psize;
391 char *pp;
Lev Walkind370e9f2006-03-16 10:03:35 +0000392
393 if((bend - buf) <= 0) {
394 FATAL("Assignment warning: empty string is being assigned into %s for %s at line %d",
395 cell->field->Identifier,
396 arg->expr->Identifier, arg->expr->_lineno);
397 return -1;
398 }
399
Lev Walkind357f3d2017-08-10 17:40:37 -0700400 mivr = malloc(bend - buf + 1);
401 assert(mivr);
402 memcpy(mivr, buf, bend - buf);
403 mivr[bend - buf] = '\0';
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800404 /* remove trailing space */
Lev Walkind357f3d2017-08-10 17:40:37 -0700405 for (i = bend - buf - 1; (i > 0) && isspace(mivr[i]); i--)
406 mivr[i] = '\0';
Lev Walkind370e9f2006-03-16 10:03:35 +0000407
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800408 /* This value 100 should be larger than following formatting string */
409 psize = bend - buf + 100;
410 pp = malloc(psize);
411 if(pp == NULL) {
Lev Walkind357f3d2017-08-10 17:40:37 -0700412 free(mivr);
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800413 return -1;
414 }
Lev Walkind370e9f2006-03-16 10:03:35 +0000415
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800416 if(cell->field->expr_type == A1TC_CLASSFIELD_TFS) {
417 ret = snprintf(pp, psize,
418 "M DEFINITIONS ::=\nBEGIN\n"
419 "V ::= %s\n"
420 "END\n",
Lev Walkind357f3d2017-08-10 17:40:37 -0700421 mivr
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800422 );
423 } else if(cell->field->expr_type == A1TC_CLASSFIELD_FTVFS) {
424 type_expr = TQ_FIRST(&(cell->field->members));
425 ret = snprintf(pp, psize,
426 "M DEFINITIONS ::=\nBEGIN\n"
427 "v %s ::= %s\n"
428 "END\n",
429 type_expr->reference ?
430 type_expr->reference->components[0].name :
431 _asn1p_expr_type2string(type_expr->expr_type),
Lev Walkind357f3d2017-08-10 17:40:37 -0700432 mivr
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800433 );
Lev Walkind370e9f2006-03-16 10:03:35 +0000434 } else {
Lev Walkind357f3d2017-08-10 17:40:37 -0700435 WARNING("asn1c only be able to parse TypeFieldSpec and FixedTypeValueFieldSpec. Failed when parsing %s at line %d\n", mivr, arg->expr->_lineno);
436 free(mivr);
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800437 free(pp);
438 return -1;
439 }
Lev Walkinea6635b2017-08-06 23:23:04 -0700440 DEBUG("ASN.1:\n\n%s\n", pp);
Bi-Ruei, Chiu3dcf05b2017-05-04 21:45:05 +0800441
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800442 assert(ret < psize);
443 psize = ret;
444
Lev Walkinbe518fa2017-09-07 02:05:28 -0700445 asn = asn1p_parse_buffer(pp, psize,
446 arg->expr->module->source_file_name, arg->expr->_lineno, A1P_NOFLAGS);
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800447 free(pp);
448 if(asn == NULL) {
449 FATAL("Cannot parse Setting token %s "
450 "at line %d",
Lev Walkind357f3d2017-08-10 17:40:37 -0700451 mivr,
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800452 arg->expr->_lineno
453 );
Lev Walkind357f3d2017-08-10 17:40:37 -0700454 free(mivr);
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800455 return -1;
456 } else {
457 mod = TQ_FIRST(&(asn->modules));
458 assert(mod);
459 expr = TQ_REMOVE(&(mod->members), next);
460 assert(expr);
461
Lev Walkinea6635b2017-08-06 23:23:04 -0700462 expr->parent_expr = NULL;
463 asn1p_expr_set_source(expr, arg->expr->module, arg->expr->_lineno);
Lev Walkind357f3d2017-08-10 17:40:37 -0700464 expr->_type_unique_index = counter;
465 DEBUG("Parsed identifier %s, mivr [%s], reference [%s] value [%s]",
466 expr->Identifier, mivr, asn1p_ref_string(expr->reference),
467 asn1f_printable_value(expr->value));
468 free(expr->Identifier);
469 if(expr->value) {
470 expr->Identifier = strdup(asn1f_printable_value(expr->value));
471 } else if (expr->reference) {
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800472 expr->Identifier = strdup(expr->reference->components[expr->reference->comp_count - 1].name);
473 } else {
Lev Walkind357f3d2017-08-10 17:40:37 -0700474 expr->Identifier = mivr;
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800475 }
476 asn1p_delete(asn);
477 }
478
479 if(expr->reference &&
Lev Walkinc0e03b92017-08-22 01:48:23 -0700480 !asn1f_lookup_symbol(arg, expr->rhs_pspecs, expr->reference)) {
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800481
482 asn1p_ref_free(expr->reference);
483 new_ref = 0;
484 expr->reference = type_expr->reference;
485 if (asn1f_value_resolve(arg, expr, 0)) {
486 expr->reference = 0;
487 asn1p_expr_free(expr);
Lev Walkind370e9f2006-03-16 10:03:35 +0000488 FATAL("Cannot find %s referenced by %s at line %d",
Lev Walkind357f3d2017-08-10 17:40:37 -0700489 mivr, arg->expr->Identifier,
Lev Walkind370e9f2006-03-16 10:03:35 +0000490 arg->expr->_lineno);
Lev Walkind357f3d2017-08-10 17:40:37 -0700491 free(mivr);
Lev Walkind370e9f2006-03-16 10:03:35 +0000492 return -1;
493 }
494 }
495
496 DEBUG("Field %s assignment of %s got %s",
Lev Walkind357f3d2017-08-10 17:40:37 -0700497 cell->field->Identifier, mivr, expr->Identifier);
Lev Walkind370e9f2006-03-16 10:03:35 +0000498
499 cell->value = expr;
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800500 cell->new_ref = new_ref;
Lev Walkind370e9f2006-03-16 10:03:35 +0000501
Lev Walkind357f3d2017-08-10 17:40:37 -0700502 if(expr->Identifier != mivr) {
503 free(mivr);
Lev Walkin4dcf8362017-08-07 20:10:05 -0700504 }
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800505
Lev Walkind370e9f2006-03-16 10:03:35 +0000506 return 0;
507}
508
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800509static asn1p_wsyntx_chunk_t *
Lev Walkinea6635b2017-08-06 23:23:04 -0700510asn1f_next_literal_chunk(asn1p_wsyntx_t *syntax, asn1p_wsyntx_chunk_t *chunk, const uint8_t *buf)
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800511{
512 asn1p_wsyntx_chunk_t *next_chunk;
513
514 next_chunk = TQ_NEXT(chunk, next);
515 do {
516 if(next_chunk == (struct asn1p_wsyntx_chunk_s *)0) {
517 if(!syntax->parent)
518 break;
519 next_chunk = TQ_NEXT(syntax->parent, next);
520 } else if(next_chunk->type == WC_LITERAL) {
Lev Walkinea6635b2017-08-06 23:23:04 -0700521 if(strstr((const char *)buf, (char *)next_chunk->content.token))
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800522 break;
523 if(!syntax->parent)
524 break;
525 next_chunk = TQ_NEXT(syntax->parent, next);
526 } else if(next_chunk->type == WC_WHITESPACE) {
527 next_chunk = TQ_NEXT(next_chunk, next);
528 } else if(next_chunk->type == WC_OPTIONALGROUP) {
529 syntax = next_chunk->content.syntax;
530 next_chunk = TQ_FIRST(((&next_chunk->content.syntax->chunks)));
531 } else
532 break;
533 } while (next_chunk);
534
535 return next_chunk;
536}