blob: 137848602f31b81d675e921c96fcc3aee7bfc32f [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#include <stdio.h>
2#include <string.h>
3#include <errno.h>
4#include <assert.h>
5
6#include <asn1parser.h>
vlm4808c702004-08-18 04:50:37 +00007#include <asn1fix_export.h>
vlm04a79e62004-08-18 05:41:05 +00008#include <asn1fix_crange.h>
vlmfa67ddc2004-06-03 03:38:44 +00009
10#include "asn1print.h"
11
12#define INDENT(fmt, args...) do { \
vlmdb4ee042005-02-15 07:06:05 +000013 if(!(flags & APF_NOINDENT)) { \
14 int __i = level; \
15 while(__i--) printf(" "); \
16 } \
vlmfa67ddc2004-06-03 03:38:44 +000017 printf(fmt, ##args); \
18 } while(0)
19
vlm4808c702004-08-18 04:50:37 +000020static int asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags);
vlm51c3de92005-03-18 03:53:05 +000021static int asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags);
vlm4808c702004-08-18 04:50:37 +000022static int asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags);
23static int asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags);
24static int asn1print_params(asn1p_paramlist_t *pl,enum asn1print_flags flags);
25static int asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags);
26static int asn1print_constraint(asn1p_constraint_t *, enum asn1print_flags);
27static int asn1print_value(asn1p_value_t *val, enum asn1print_flags flags);
vlm60e7ef02004-10-13 09:13:56 +000028static int asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level);
29static int asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level);
vlmfa67ddc2004-06-03 03:38:44 +000030
31/*
32 * Print the contents of the parsed ASN tree.
33 */
34int
vlm4808c702004-08-18 04:50:37 +000035asn1print(asn1p_t *asn, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +000036 asn1p_module_t *mod;
vlm281d82a2004-09-30 06:38:21 +000037 int modno = 0;
vlmfa67ddc2004-06-03 03:38:44 +000038
39 if(asn == NULL) {
40 errno = EINVAL;
41 return -1;
42 }
43
vlm60e7ef02004-10-13 09:13:56 +000044 if(flags & APF_PRINT_XML_DTD)
45 printf("<!-- XML DTD generated by asn1c-" VERSION " -->\n\n");
46
vlmfa67ddc2004-06-03 03:38:44 +000047 TQ_FOR(mod, &(asn->modules), mod_next) {
vlm79d4e632006-03-06 14:51:00 +000048 if(mod->_tags & MT_STANDARD_MODULE)
49 return 0; /* Ignore modules imported from skeletons */
vlm281d82a2004-09-30 06:38:21 +000050 if(modno++) printf("\n");
vlm4808c702004-08-18 04:50:37 +000051 asn1print_module(asn, mod, flags);
vlmfa67ddc2004-06-03 03:38:44 +000052 }
53
vlmc45eab72005-03-09 20:52:15 +000054 if(flags & APF_PRINT_XML_DTD) {
55 /* Values for BOOLEAN */
56 printf("<!ELEMENT true EMPTY>\n");
57 printf("<!ELEMENT false EMPTY>\n");
58 }
59
vlmfa67ddc2004-06-03 03:38:44 +000060 return 0;
61}
62
63static int
vlm4808c702004-08-18 04:50:37 +000064asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +000065 asn1p_expr_t *tc;
66
vlm60e7ef02004-10-13 09:13:56 +000067 if(flags & APF_PRINT_XML_DTD)
68 printf("<!-- ASN.1 module\n");
69
vlm931aeed2005-08-12 10:09:10 +000070 printf("%s ", mod->ModuleName);
vlmfa67ddc2004-06-03 03:38:44 +000071 if(mod->module_oid) {
vlm931aeed2005-08-12 10:09:10 +000072 asn1print_oid(strlen(mod->ModuleName), mod->module_oid, flags);
vlmfa67ddc2004-06-03 03:38:44 +000073 printf("\n");
74 }
75
vlm60e7ef02004-10-13 09:13:56 +000076 if(flags & APF_PRINT_XML_DTD) {
77 if(mod->source_file_name
78 && strcmp(mod->source_file_name, "-"))
79 printf("found in %s", mod->source_file_name);
80 printf(" -->\n\n");
81
82 TQ_FOR(tc, &(mod->members), next) {
83 asn1print_expr_dtd(asn, mod, tc, flags, 0);
84 }
85
86 return 0;
87 }
88
vlmfa67ddc2004-06-03 03:38:44 +000089 printf("DEFINITIONS");
90
vlm4808c702004-08-18 04:50:37 +000091 if(mod->module_flags & MSF_TAG_INSTRUCTIONS)
92 printf(" TAG INSTRUCTIONS");
93 if(mod->module_flags & MSF_XER_INSTRUCTIONS)
94 printf(" XER INSTRUCTIONS");
vlmfa67ddc2004-06-03 03:38:44 +000095 if(mod->module_flags & MSF_EXPLICIT_TAGS)
96 printf(" EXPLICIT TAGS");
97 if(mod->module_flags & MSF_IMPLICIT_TAGS)
98 printf(" IMPLICIT TAGS");
99 if(mod->module_flags & MSF_AUTOMATIC_TAGS)
100 printf(" AUTOMATIC TAGS");
101 if(mod->module_flags & MSF_EXTENSIBILITY_IMPLIED)
102 printf(" EXTENSIBILITY IMPLIED");
103
104 printf(" ::=\n");
105 printf("BEGIN\n\n");
106
107 TQ_FOR(tc, &(mod->members), next) {
vlm4808c702004-08-18 04:50:37 +0000108 asn1print_expr(asn, mod, tc, flags, 0);
vlma6a84d72006-03-16 10:03:35 +0000109 if(flags & APF_PRINT_CONSTRAINTS)
vlm00ad2822004-08-20 13:24:28 +0000110 printf("\n");
111 else
112 printf("\n\n");
vlmfa67ddc2004-06-03 03:38:44 +0000113 }
114
115 printf("END\n");
116
117 return 0;
118}
119
120static int
vlm51c3de92005-03-18 03:53:05 +0000121asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags) {
122 size_t accum = prior_len;
vlmfa67ddc2004-06-03 03:38:44 +0000123 int ac;
vlmfa67ddc2004-06-03 03:38:44 +0000124
vlmb42843a2004-06-05 08:17:50 +0000125 (void)flags; /* Unused argument */
126
vlmfa67ddc2004-06-03 03:38:44 +0000127 printf("{");
128 for(ac = 0; ac < oid->arcs_count; ac++) {
vlm8a09e0f2005-02-25 14:20:30 +0000129 const char *arcname = oid->arcs[ac].name;
130
vlm398105f2005-03-28 17:52:43 +0000131 if(accum + strlen(arcname ? arcname : "") > 72) {
vlmfa67ddc2004-06-03 03:38:44 +0000132 printf("\n\t");
vlm398105f2005-03-28 17:52:43 +0000133 accum = 8;
vlm51c3de92005-03-18 03:53:05 +0000134 } else {
135 accum += printf(" ");
vlm8a09e0f2005-02-25 14:20:30 +0000136 }
vlmfa67ddc2004-06-03 03:38:44 +0000137
vlm8a09e0f2005-02-25 14:20:30 +0000138 if(arcname) {
vlm51c3de92005-03-18 03:53:05 +0000139 accum += printf("%s", arcname);
vlmdc4d95a2004-09-05 10:38:50 +0000140 if(oid->arcs[ac].number >= 0) {
vlm51c3de92005-03-18 03:53:05 +0000141 accum += printf("(%" PRIdASN ")",
142 oid->arcs[ac].number);
vlmdc4d95a2004-09-05 10:38:50 +0000143 }
vlmfa67ddc2004-06-03 03:38:44 +0000144 } else {
vlm51c3de92005-03-18 03:53:05 +0000145 accum += printf("%" PRIdASN, oid->arcs[ac].number);
vlmfa67ddc2004-06-03 03:38:44 +0000146 }
vlmfa67ddc2004-06-03 03:38:44 +0000147 }
148 printf(" }");
149
150 return 0;
151}
152
153static int
vlm4808c702004-08-18 04:50:37 +0000154asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000155 int cc;
156
vlmb42843a2004-06-05 08:17:50 +0000157 (void)flags; /* Unused argument */
158
vlmfa67ddc2004-06-03 03:38:44 +0000159 for(cc = 0; cc < ref->comp_count; cc++) {
160 if(cc) printf(".");
161 printf("%s", ref->components[cc].name);
162 }
163
164 return 0;
165}
166
167static int
vlm4808c702004-08-18 04:50:37 +0000168asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000169 struct asn1p_type_tag_s *tag = &tc->tag;
170
vlmb42843a2004-06-05 08:17:50 +0000171 (void)flags; /* Unused argument */
172
vlm5b103032005-06-02 05:21:53 +0000173 printf("%s", asn1p_tag2string(tag, 0));
vlmfa67ddc2004-06-03 03:38:44 +0000174
175 return 0;
176}
177
178static int
vlm4808c702004-08-18 04:50:37 +0000179asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000180
181 if(val == NULL)
182 return 0;
183
184 switch(val->type) {
185 case ATV_NOVALUE:
186 break;
vlm093f6752004-09-15 11:44:55 +0000187 case ATV_NULL:
188 printf("NULL");
189 return 0;
190 case ATV_REAL:
191 printf("%f", val->value.v_double);
192 return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000193 case ATV_INTEGER:
vlm47ae1582004-09-24 21:01:43 +0000194 printf("%" PRIdASN, val->value.v_integer);
vlmfa67ddc2004-06-03 03:38:44 +0000195 return 0;
196 case ATV_MIN: printf("MIN"); return 0;
197 case ATV_MAX: printf("MAX"); return 0;
198 case ATV_FALSE: printf("FALSE"); return 0;
199 case ATV_TRUE: printf("TRUE"); return 0;
vlme1e6ed82005-03-24 14:26:38 +0000200 case ATV_TUPLE:
201 printf("{%d, %d}",
202 (int)(val->value.v_integer >> 4),
203 (int)(val->value.v_integer & 0x0f));
204 return 0;
205 case ATV_QUADRUPLE:
206 printf("{%d, %d, %d, %d}",
207 (int)((val->value.v_integer >> 24) & 0xff),
208 (int)((val->value.v_integer >> 16) & 0xff),
209 (int)((val->value.v_integer >> 8) & 0xff),
210 (int)((val->value.v_integer >> 0) & 0xff)
211 );
212 return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000213 case ATV_STRING:
214 {
vlm55289c12005-06-15 18:41:50 +0000215 char *p = (char *)val->value.string.buf;
vlmfa67ddc2004-06-03 03:38:44 +0000216 putchar('"');
217 if(strchr(p, '"')) {
218 /* Mask quotes */
219 for(; *p; p++) {
220 if(*p == '"')
221 putchar(*p);
222 putchar(*p);
223 }
224 } else {
225 fputs(p, stdout);
226 }
227 putchar('"');
228 }
229 return 0;
230 case ATV_UNPARSED:
vlm55289c12005-06-15 18:41:50 +0000231 fputs((char *)val->value.string.buf, stdout);
vlmfa67ddc2004-06-03 03:38:44 +0000232 return 0;
233 case ATV_BITVECTOR:
234 {
235 uint8_t *bitvector;
236 int bits;
237 int i;
238
239 bitvector = val->value.binary_vector.bits;
240 bits = val->value.binary_vector.size_in_bits;
241
242 printf("'");
243 if(bits%8) {
244 for(i = 0; i < bits; i++) {
245 uint8_t uc;
246 uc = bitvector[i>>3];
247 putchar(((uc >> (7-(i%8)))&1)?'1':'0');
248 }
249 printf("'B");
250 } else {
251 char hextable[16] = "0123456789ABCDEF";
252 for(i = 0; i < (bits>>3); i++) {
253 putchar(hextable[bitvector[i] >> 4]);
254 putchar(hextable[bitvector[i] & 0x0f]);
255 }
256 printf("'H");
257 }
vlma9e55432005-02-24 21:07:35 +0000258 return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000259 }
vlm093f6752004-09-15 11:44:55 +0000260 case ATV_REFERENCED:
261 return asn1print_ref(val->value.reference, flags);
262 case ATV_CHOICE_IDENTIFIER:
263 printf("%s: ", val->value.choice_identifier.identifier);
264 return asn1print_value(val->value.choice_identifier.value, flags);
vlmfa67ddc2004-06-03 03:38:44 +0000265 }
266
267 assert(val->type || !"Unknown");
268
269 return 0;
270}
271
272static int
vlm4808c702004-08-18 04:50:37 +0000273asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000274 int symno = 0;
275
276 if(ct == 0) return 0;
277
278 if(ct->type == ACT_CA_SET)
279 printf("(");
280
281 switch(ct->type) {
vlma6a12e32005-03-20 12:58:00 +0000282 case ACT_EL_TYPE:
283 asn1print_value(ct->value, flags);
284 break;
vlmfa67ddc2004-06-03 03:38:44 +0000285 case ACT_EL_VALUE:
286 asn1print_value(ct->value, flags);
287 break;
288 case ACT_EL_RANGE:
289 case ACT_EL_LLRANGE:
290 case ACT_EL_RLRANGE:
291 case ACT_EL_ULRANGE:
292 asn1print_value(ct->range_start, flags);
293 switch(ct->type) {
294 case ACT_EL_RANGE: printf(".."); break;
295 case ACT_EL_LLRANGE: printf("<.."); break;
296 case ACT_EL_RLRANGE: printf("..<"); break;
297 case ACT_EL_ULRANGE: printf("<..<"); break;
298 default: printf("?..?"); break;
299 }
300 asn1print_value(ct->range_stop, flags);
301 break;
302 case ACT_EL_EXT:
303 printf("...");
304 break;
305 case ACT_CT_SIZE:
306 case ACT_CT_FROM:
307 switch(ct->type) {
vlm4808c702004-08-18 04:50:37 +0000308 case ACT_CT_SIZE: printf("SIZE("); break;
309 case ACT_CT_FROM: printf("FROM("); break;
vlmfa67ddc2004-06-03 03:38:44 +0000310 default: printf("??? ("); break;
311 }
312 assert(ct->el_count != 0);
313 assert(ct->el_count == 1);
314 asn1print_constraint(ct->elements[0], flags);
315 printf(")");
316 break;
317 case ACT_CT_WCOMP:
vlm7bbdc9f2005-03-28 15:01:27 +0000318 assert(ct->el_count != 0);
319 assert(ct->el_count == 1);
320 printf("WITH COMPONENT (");
321 asn1print_constraint(ct->elements[0], flags);
322 printf(")");
vlm3b948532005-03-28 07:50:06 +0000323 break;
vlm7bbdc9f2005-03-28 15:01:27 +0000324 case ACT_CT_WCOMPS: {
325 unsigned int i;
326 printf("WITH COMPONENTS { ");
327 for(i = 0; i < ct->el_count; i++) {
328 asn1p_constraint_t *cel = ct->elements[i];
329 if(i) printf(", ");
330 fwrite(cel->value->value.string.buf,
331 1, cel->value->value.string.size,
332 stdout);
333 if(cel->el_count) {
334 assert(cel->el_count == 1);
335 printf(" ");
336 asn1print_constraint(cel->elements[0],
337 flags);
338 }
339 switch(cel->presence) {
340 case ACPRES_DEFAULT: break;
341 case ACPRES_PRESENT: printf(" PRESENT"); break;
342 case ACPRES_ABSENT: printf(" ABSENT"); break;
343 case ACPRES_OPTIONAL: printf(" OPTIONAL");break;
344 }
345 }
346 printf(" }");
347 }
vlm3b948532005-03-28 07:50:06 +0000348 break;
vlm6611add2005-03-20 14:28:32 +0000349 case ACT_CT_CTDBY:
350 printf("CONSTRAINED BY ");
351 assert(ct->value->type == ATV_UNPARSED);
352 fwrite(ct->value->value.string.buf,
353 1, ct->value->value.string.size, stdout);
vlmfa67ddc2004-06-03 03:38:44 +0000354 break;
355 case ACT_CA_SET: symno++;
356 case ACT_CA_CRC: symno++;
357 case ACT_CA_CSV: symno++;
358 case ACT_CA_UNI: symno++;
359 case ACT_CA_INT: symno++;
360 case ACT_CA_EXC:
361 {
vlm4808c702004-08-18 04:50:37 +0000362 char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",",
vlmfa67ddc2004-06-03 03:38:44 +0000363 "", "(" };
vlmdc4d95a2004-09-05 10:38:50 +0000364 unsigned int i;
vlmfa67ddc2004-06-03 03:38:44 +0000365 for(i = 0; i < ct->el_count; i++) {
vlmfa67ddc2004-06-03 03:38:44 +0000366 if(i) fputs(symtable[symno], stdout);
367 if(ct->type == ACT_CA_CRC) fputs("{", stdout);
vlme1e6ed82005-03-24 14:26:38 +0000368 asn1print_constraint(ct->elements[i], flags);
vlmfa67ddc2004-06-03 03:38:44 +0000369 if(ct->type == ACT_CA_CRC) fputs("}", stdout);
370 if(i+1 < ct->el_count
371 && ct->type == ACT_CA_SET)
372 fputs(")", stdout);
373 }
374 }
375 break;
vlme1e6ed82005-03-24 14:26:38 +0000376 case ACT_CA_AEX:
377 assert(ct->el_count == 1);
378 printf("ALL EXCEPT ");
379 asn1print_constraint(ct->elements[0], flags);
380 break;
vlmfa67ddc2004-06-03 03:38:44 +0000381 case ACT_INVALID:
382 assert(ct->type != ACT_INVALID);
383 break;
384 }
385
386 if(ct->type == ACT_CA_SET)
387 printf(")");
388
389 return 0;
390}
391
392static int
vlm4808c702004-08-18 04:50:37 +0000393asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000394 if(pl) {
395 int i;
396 printf("{");
397 for(i = 0; i < pl->params_count; i++) {
398 if(i) printf(", ");
399 if(pl->params[i].governor) {
400 asn1print_ref(pl->params[i].governor, flags);
401 printf(":");
402 }
403 printf("%s", pl->params[i].argument);
404 }
405 printf("}");
406 }
407
408 return 0;
409}
410
411static int
vlm4808c702004-08-18 04:50:37 +0000412asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000413 if(wx) {
414 asn1p_wsyntx_chunk_t *wc;
vlmfa67ddc2004-06-03 03:38:44 +0000415 TQ_FOR(wc, &(wx->chunks), next) {
vlm808411d2006-03-14 16:31:37 +0000416 switch(wc->type) {
417 case WC_LITERAL:
vlmeeb3c512006-03-16 05:11:14 +0000418 case WC_WHITESPACE:
vlma6a84d72006-03-16 10:03:35 +0000419 case WC_FIELD:
vlm808411d2006-03-14 16:31:37 +0000420 printf("%s", wc->content.token);
421 break;
vlm808411d2006-03-14 16:31:37 +0000422 case WC_OPTIONALGROUP:
423 printf("[");
424 asn1print_with_syntax(wc->content.syntax,flags);
425 printf("]");
426 break;
427 }
vlmfa67ddc2004-06-03 03:38:44 +0000428 }
vlmfa67ddc2004-06-03 03:38:44 +0000429 }
430
431 return 0;
432}
433
434static int
vlm4808c702004-08-18 04:50:37 +0000435asn1print_crange_value(asn1cnst_edge_t *edge, int as_char) {
436 switch(edge->type) {
437 case ARE_MIN: printf("MIN"); break;
438 case ARE_MAX: printf("MAX"); break;
439 case ARE_VALUE:
440 if(as_char) {
441 printf("\"%c\"", (unsigned char)edge->value);
442 } else {
vlm47ae1582004-09-24 21:01:43 +0000443 printf("%" PRIdASN, edge->value);
vlm4808c702004-08-18 04:50:37 +0000444 }
445 }
446 return 0;
447}
448
449static int
vlmeeca98f2004-08-25 02:00:03 +0000450asn1print_constraint_explain_type(asn1p_expr_type_e expr_type, asn1p_constraint_t *ct, enum asn1p_constraint_type_e type, int strict_PER_visible) {
vlm4808c702004-08-18 04:50:37 +0000451 asn1cnst_range_t *range;
452 int as_char = (type==ACT_CT_FROM);
453 int i;
454
vlm27028582005-08-14 14:45:44 +0000455 range = asn1constraint_compute_PER_range(expr_type, ct, type, 0, 0,
456 strict_PER_visible ? CPR_strict_PER_visibility : 0);
vlm4808c702004-08-18 04:50:37 +0000457 if(!range) return -1;
458
vlmeeca98f2004-08-25 02:00:03 +0000459 if(range->incompatible
460 || (strict_PER_visible && range->not_PER_visible)) {
461 asn1constraint_range_free(range);
462 return 0;
463 }
464
vlm4808c702004-08-18 04:50:37 +0000465 switch(type) {
466 case ACT_CT_FROM: printf("(FROM("); break;
467 case ACT_CT_SIZE: printf("(SIZE("); break;
468 default: printf("("); break;
469 }
470 for(i = -1; i < range->el_count; i++) {
471 asn1cnst_range_t *r;
472 if(i == -1) {
473 if(range->el_count) continue;
474 r = range;
475 } else {
476 r = range->elements[i];
477 }
478 if(i > 0) {
479 printf(" | ");
480 }
481 asn1print_crange_value(&r->left, as_char);
482 if(r->left.type != r->right.type
483 || r->left.value != r->right.value) {
484 printf("..");
485 asn1print_crange_value(&r->right, as_char);
486 }
487 }
488 if(range->extensible)
489 printf(",...");
490 printf(type==ACT_EL_RANGE?")":"))");
491
492 if(range->empty_constraint)
493 printf(":Empty!");
494
495 asn1constraint_range_free(range);
496 return 0;
497}
498
499static int
500asn1print_constraint_explain(asn1p_expr_type_e expr_type,
vlmeeca98f2004-08-25 02:00:03 +0000501 asn1p_constraint_t *ct, int s_PV) {
vlm4808c702004-08-18 04:50:37 +0000502
vlmeeca98f2004-08-25 02:00:03 +0000503 asn1print_constraint_explain_type(expr_type, ct, ACT_EL_RANGE, s_PV);
vlm4808c702004-08-18 04:50:37 +0000504 printf(" ");
vlmeeca98f2004-08-25 02:00:03 +0000505 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_SIZE, s_PV);
vlm4808c702004-08-18 04:50:37 +0000506 printf(" ");
vlmeeca98f2004-08-25 02:00:03 +0000507 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_FROM, s_PV);
vlm4808c702004-08-18 04:50:37 +0000508
509 return 0;
510}
511
512static int
513asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level) {
vlm00ad2822004-08-20 13:24:28 +0000514 int SEQ_OF = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000515
vlmdb4ee042005-02-15 07:06:05 +0000516 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
vlm60e7ef02004-10-13 09:13:56 +0000517 INDENT("-- #line %d\n", tc->_lineno);
vlm177a5b62005-09-05 05:17:57 +0000518
519 /* Reconstruct compiler directive information */
520 if((tc->marker.flags & EM_INDIRECT)
521 && (tc->marker.flags & EM_OMITABLE) != EM_OMITABLE) {
522 if((flags & APF_NOINDENT))
523 printf(" --<ASN1C.RepresentAsPointer>-- ");
524 else
525 INDENT("--<ASN1C.RepresentAsPointer>--\n");
526 }
527
vlm0c6d3812006-03-21 03:40:38 +0000528 if(tc->Identifier
529 && (!(tc->meta_type == AMT_VALUE && tc->expr_type == A1TC_REFERENCE)
530 || level == 0))
vlmfa67ddc2004-06-03 03:38:44 +0000531 INDENT("%s", tc->Identifier);
532
vlm0c6d3812006-03-21 03:40:38 +0000533 if(tc->lhs_params) {
534 asn1print_params(tc->lhs_params, flags);
vlmfa67ddc2004-06-03 03:38:44 +0000535 }
536
537 if(tc->meta_type != AMT_VALUE
vlmda4df3f2004-08-25 02:27:47 +0000538 && tc->meta_type != AMT_VALUESET
vlmfa67ddc2004-06-03 03:38:44 +0000539 && tc->expr_type != A1TC_EXTENSIBLE) {
540 if(level) {
vlmdb4ee042005-02-15 07:06:05 +0000541 if(tc->Identifier && !(flags & APF_NOINDENT))
vlmfa67ddc2004-06-03 03:38:44 +0000542 printf("\t");
543 } else {
544 printf(" ::=");
545 }
546 }
547
548 if(tc->tag.tag_class) {
549 printf(" ");
550 asn1print_tag(tc, flags);
551 }
552
553 switch(tc->expr_type) {
554 case A1TC_EXTENSIBLE:
555 if(tc->value) {
556 printf("!");
557 asn1print_value(tc->value, flags);
558 }
559 break;
vlmddd6fd12004-08-22 03:08:51 +0000560 case A1TC_COMPONENTS_OF:
561 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
562 printf(" COMPONENTS OF");
563 break;
vlmfa67ddc2004-06-03 03:38:44 +0000564 case A1TC_REFERENCE:
565 case A1TC_UNIVERVAL:
vlmfa67ddc2004-06-03 03:38:44 +0000566 break;
567 case A1TC_CLASSDEF:
568 printf(" CLASS");
569 break;
vlmdc7cf042006-03-09 08:49:26 +0000570 case A1TC_CLASSFIELD_TFS ... A1TC_CLASSFIELD_OSFS:
vlmfa67ddc2004-06-03 03:38:44 +0000571 /* Nothing to print here */
572 break;
vlm00ad2822004-08-20 13:24:28 +0000573 case ASN_CONSTR_SET_OF:
574 case ASN_CONSTR_SEQUENCE_OF:
575 SEQ_OF = 1;
576 if(tc->expr_type == ASN_CONSTR_SET_OF)
577 printf(" SET");
578 else
579 printf(" SEQUENCE");
580 if(tc->constraints) {
581 printf(" ");
582 asn1print_constraint(tc->constraints, flags);
583 }
584 printf(" OF");
585 break;
vlmfa67ddc2004-06-03 03:38:44 +0000586 default:
587 {
588 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
589 printf(" %s", p?p:"<unknown type!>");
590 }
591 break;
592 }
593
vlma750ca42005-06-02 04:06:24 +0000594 /*
595 * Put the name of the referred type.
596 */
vlmfa67ddc2004-06-03 03:38:44 +0000597 if(tc->reference) {
vlma750ca42005-06-02 04:06:24 +0000598 printf(" ");
vlmfa67ddc2004-06-03 03:38:44 +0000599 asn1print_ref(tc->reference, flags);
600 }
601
vlmda4df3f2004-08-25 02:27:47 +0000602 if(tc->meta_type == AMT_VALUESET)
603 printf(" ::=");
604
vlmfa67ddc2004-06-03 03:38:44 +0000605 /*
606 * Display the descendants (children) of the current type.
607 */
vlmda4df3f2004-08-25 02:27:47 +0000608 if(TQ_FIRST(&(tc->members))
609 || (tc->expr_type & ASN_CONSTR_MASK)
610 || tc->meta_type == AMT_VALUESET
611 || tc->meta_type == AMT_OBJECT
vlmdc7cf042006-03-09 08:49:26 +0000612 || tc->meta_type == AMT_OBJECTCLASS
613 || tc->meta_type == AMT_OBJECTFIELD
vlmda4df3f2004-08-25 02:27:47 +0000614 ) {
vlmfa67ddc2004-06-03 03:38:44 +0000615 asn1p_expr_t *se; /* SubExpression */
vlmdc7cf042006-03-09 08:49:26 +0000616 int put_braces = (!SEQ_OF) /* Don't need 'em, if SET OF... */
617 && (tc->meta_type != AMT_OBJECTFIELD);
vlmfa67ddc2004-06-03 03:38:44 +0000618
vlmda4df3f2004-08-25 02:27:47 +0000619 if(put_braces) {
vlmdb4ee042005-02-15 07:06:05 +0000620 if(flags & APF_NOINDENT) {
621 printf("{");
622 if(!TQ_FIRST(&tc->members))
623 printf("}");
624 } else {
625 printf(" {");
626 if(TQ_FIRST(&tc->members))
627 printf("\n");
628 else
629 printf(" }");
630 }
vlmda4df3f2004-08-25 02:27:47 +0000631 }
vlmfa67ddc2004-06-03 03:38:44 +0000632
633 TQ_FOR(se, &(tc->members), next) {
634 /*
vlmddd6fd12004-08-22 03:08:51 +0000635 * Print the expression as it were a stand-alone type.
vlmfa67ddc2004-06-03 03:38:44 +0000636 */
vlm60e7ef02004-10-13 09:13:56 +0000637 asn1print_expr(asn, mod, se, flags, level + 1);
vlm093f6752004-09-15 11:44:55 +0000638 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
639 printf(" DEFAULT ");
640 asn1print_value(se->marker.default_value, flags);
641 } else if((se->marker.flags & EM_OPTIONAL)
642 == EM_OPTIONAL) {
vlmd82eb012004-09-10 08:21:27 +0000643 printf(" OPTIONAL");
vlm093f6752004-09-15 11:44:55 +0000644 }
vlm177a5b62005-09-05 05:17:57 +0000645 if(TQ_NEXT(se, next)) {
vlmfa67ddc2004-06-03 03:38:44 +0000646 printf(",");
vlm177a5b62005-09-05 05:17:57 +0000647 if(!(flags & APF_NOINDENT))
648 INDENT("\n");
649 }
vlmfa67ddc2004-06-03 03:38:44 +0000650 }
651
vlmda4df3f2004-08-25 02:27:47 +0000652 if(put_braces && TQ_FIRST(&tc->members)) {
vlmdb4ee042005-02-15 07:06:05 +0000653 if(!(flags & APF_NOINDENT))
654 printf("\n");
vlmfa67ddc2004-06-03 03:38:44 +0000655 INDENT("}");
656 }
657 }
658
vlm808411d2006-03-14 16:31:37 +0000659 if(tc->with_syntax) {
660 printf(" WITH SYNTAX {");
vlmfa67ddc2004-06-03 03:38:44 +0000661 asn1print_with_syntax(tc->with_syntax, flags);
vlm808411d2006-03-14 16:31:37 +0000662 printf("}\n");
663 }
vlmfa67ddc2004-06-03 03:38:44 +0000664
vlm0c6d3812006-03-21 03:40:38 +0000665 /* Right hand specialization */
666 if(tc->rhs_pspecs) {
667 asn1p_expr_t *se;
668 printf("{");
669 TQ_FOR(se, &(tc->rhs_pspecs->members), next) {
670 asn1print_expr(asn, mod, se, flags, level + 1);
vlm17e65d02006-03-21 04:48:15 +0000671 if(TQ_NEXT(se, next)) printf(", ");
vlm0c6d3812006-03-21 03:40:38 +0000672 }
673 printf("}");
674 }
675
vlm00ad2822004-08-20 13:24:28 +0000676 if(!SEQ_OF && tc->constraints) {
vlmfa67ddc2004-06-03 03:38:44 +0000677 printf(" ");
678 asn1print_constraint(tc->constraints, flags);
679 }
vlm4808c702004-08-18 04:50:37 +0000680
vlmfa67ddc2004-06-03 03:38:44 +0000681 if(tc->unique) {
682 printf(" UNIQUE");
683 }
684
685 if(tc->meta_type == AMT_VALUE
686 && tc->expr_type != A1TC_EXTENSIBLE) {
vlmdc4d95a2004-09-05 10:38:50 +0000687 if(tc->expr_type == A1TC_UNIVERVAL) {
vlm093f6752004-09-15 11:44:55 +0000688 if(tc->value) {
689 printf("(");
690 asn1print_value(tc->value, flags);
691 printf(")");
692 }
vlmdc4d95a2004-09-05 10:38:50 +0000693 } else {
vlm0c6d3812006-03-21 03:40:38 +0000694 if(level == 0) printf(" ::= ");
vlmdc4d95a2004-09-05 10:38:50 +0000695 asn1print_value(tc->value, flags);
696 }
vlmfa67ddc2004-06-03 03:38:44 +0000697 }
698
vlmddd6fd12004-08-22 03:08:51 +0000699 /*
vlmdb4ee042005-02-15 07:06:05 +0000700 * The following section exists entirely for debugging.
vlmddd6fd12004-08-22 03:08:51 +0000701 */
vlma6a84d72006-03-16 10:03:35 +0000702 if(flags & APF_PRINT_CONSTRAINTS
vlm00ad2822004-08-20 13:24:28 +0000703 && tc->expr_type != A1TC_EXTENSIBLE) {
vlm4808c702004-08-18 04:50:37 +0000704 asn1p_expr_t *top_parent;
705
706 if(tc->combined_constraints) {
707 printf("\n-- Combined constraints: ");
708 asn1print_constraint(tc->combined_constraints, flags);
709 }
710
vlmcbebb812004-09-22 16:05:13 +0000711 top_parent = asn1f_find_terminal_type_ex(asn, tc);
vlm4808c702004-08-18 04:50:37 +0000712 if(top_parent) {
vlmeeca98f2004-08-25 02:00:03 +0000713 printf("\n-- Practical constraints (%s): ",
714 top_parent->Identifier);
715 asn1print_constraint_explain(top_parent->expr_type,
716 tc->combined_constraints, 0);
vlm00ad2822004-08-20 13:24:28 +0000717 printf("\n-- PER-visible constraints (%s): ",
718 top_parent->Identifier);
vlm4808c702004-08-18 04:50:37 +0000719 asn1print_constraint_explain(top_parent->expr_type,
vlmeeca98f2004-08-25 02:00:03 +0000720 tc->combined_constraints, 1);
vlm4808c702004-08-18 04:50:37 +0000721 }
vlm00ad2822004-08-20 13:24:28 +0000722 printf("\n");
vlm4808c702004-08-18 04:50:37 +0000723 }
724
vlma6a84d72006-03-16 10:03:35 +0000725 if(flags & APF_PRINT_CLASS_MATRIX
726 && tc->expr_type == A1TC_CLASSDEF) do {
727 int r, col, maxidlen;
728 if(tc->object_class_matrix.rows == 0) {
729 printf("\n-- Class matrix is empty");
730 break;
731 }
732 printf("\n-- Class matrix has %d entr%s:\n",
733 tc->object_class_matrix.rows,
734 tc->object_class_matrix.rows==1 ? "y" : "ies");
735 maxidlen = tc->object_class_matrix.max_identifier_length;
736 for(r = -1; r < tc->object_class_matrix.rows; r++) {
737 struct asn1p_ioc_row_s *row;
738 row = tc->object_class_matrix.row[r<0?0:r];
739 if(r < 0) printf("-- %s", r > 9 ? " " : "");
740 else printf("-- [%*d]", r > 9 ? 2 : 1, r+1);
741 for(col = 0; col < row->columns; col++) {
742 struct asn1p_ioc_cell_s *cell;
743 cell = &row->column[col];
744 if(r < 0) {
745 printf("[%*s]", maxidlen,
746 cell->field->Identifier);
747 continue;
748 }
749 if(!cell->value) {
750 printf(" %*s ", maxidlen, "<no entry>");
751 continue;
752 }
753 printf(" %*s ", maxidlen,
754 cell->value->Identifier);
755 }
756 printf("\n");
757 }
758 } while(0);
759
vlm0c6d3812006-03-21 03:40:38 +0000760 if(flags & APF_PRINT_CLASS_MATRIX
761 && tc->lhs_params) do {
762 int i;
763 if(tc->specializations.pspecs_count == 0) {
764 printf("\n-- No specializations found\n");
765 break;
766 }
767 printf("\n-- Specializations list has %d entr%s:\n",
768 tc->specializations.pspecs_count,
769 tc->specializations.pspecs_count == 1 ? "y" : "ies");
770 for(i = 0; i < tc->specializations.pspecs_count; i++) {
771 asn1p_expr_t *se;
772 struct asn1p_pspec_s *pspec;
773 pspec = &tc->specializations.pspec[i];
774 printf("-- ");
775 TQ_FOR(se, &(pspec->rhs_pspecs->members), next) {
776 asn1print_expr(asn, mod, se, flags, level+1);
777 }
778 printf("\n");
779 }
780 } while(0);
781
vlmfa67ddc2004-06-03 03:38:44 +0000782 return 0;
783}
vlm4808c702004-08-18 04:50:37 +0000784
vlm60e7ef02004-10-13 09:13:56 +0000785
786static int
787asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
788 asn1p_expr_t *se;
789 int expr_unordered = 0;
vlmceb93192004-10-14 05:11:25 +0000790 int dont_involve_children = 0;
vlm60e7ef02004-10-13 09:13:56 +0000791
792 switch(expr->meta_type) {
793 case AMT_TYPE:
794 case AMT_TYPEREF:
795 break;
796 default:
797 if(expr->expr_type == A1TC_UNIVERVAL)
798 break;
799 return 0;
800 }
801
802 if(!expr->Identifier) return 0;
803
vlm60e7ef02004-10-13 09:13:56 +0000804 if(flags & APF_LINE_COMMENTS)
805 INDENT("<!-- #line %d -->\n", expr->_lineno);
806 INDENT("<!ELEMENT %s", expr->Identifier);
807
808 if(expr->expr_type == A1TC_REFERENCE) {
809 se = asn1f_find_terminal_type_ex(asn, expr);
810 if(!se) {
vlmceb93192004-10-14 05:11:25 +0000811 printf(" (ANY)");
vlm60e7ef02004-10-13 09:13:56 +0000812 return 0;
813 }
814 expr = se;
vlmceb93192004-10-14 05:11:25 +0000815 dont_involve_children = 1;
vlm60e7ef02004-10-13 09:13:56 +0000816 }
817
vlm4bba1f82005-03-10 15:16:56 +0000818 if(expr->expr_type == ASN_CONSTR_CHOICE
819 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
820 || expr->expr_type == ASN_CONSTR_SET_OF
821 || expr->expr_type == ASN_CONSTR_SET
vlmc45eab72005-03-09 20:52:15 +0000822 || expr->expr_type == ASN_BASIC_INTEGER
823 || expr->expr_type == ASN_BASIC_ENUMERATED) {
824 expr_unordered = 1;
825 }
826
vlm60e7ef02004-10-13 09:13:56 +0000827 if(TQ_FIRST(&expr->members)) {
828 int extensible = 0;
829 printf(" (");
830 TQ_FOR(se, &(expr->members), next) {
831 if(se->expr_type == A1TC_EXTENSIBLE) {
832 extensible = 1;
833 continue;
834 } else if(!se->Identifier
835 && se->expr_type == A1TC_REFERENCE) {
836 asn1print_ref(se->reference, flags);
837 } else if(se->Identifier) {
838 printf("%s", se->Identifier);
839 } else {
840 printf("ANY");
841 }
842 if(expr->expr_type != ASN_CONSTR_SET
843 && expr->expr_type != ASN_CONSTR_CHOICE
844 && expr->expr_type != ASN_BASIC_INTEGER
845 && expr->expr_type != ASN_BASIC_ENUMERATED) {
846 if(expr_unordered)
847 printf("*");
848 else if(se->marker.flags)
849 printf("?");
850 }
851 if(TQ_NEXT(se, next)
852 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
853 printf(expr_unordered?"|":", ");
854 }
855 }
856 if(extensible) {
857 printf(expr_unordered?"|":", ");
858 printf("ANY");
859 if(expr->expr_type != ASN_CONSTR_SET
860 && expr->expr_type != ASN_CONSTR_CHOICE
861 && expr->expr_type != ASN_BASIC_INTEGER
862 && expr->expr_type != ASN_BASIC_ENUMERATED)
863 printf("*");
864 }
865
866 printf(")");
867 if(expr->expr_type == ASN_CONSTR_SET)
868 printf("*");
869
vlmceb93192004-10-14 05:11:25 +0000870 } else switch(expr->expr_type) {
871 case ASN_BASIC_BOOLEAN:
vlmc45eab72005-03-09 20:52:15 +0000872 printf(" (true|false)");
vlmceb93192004-10-14 05:11:25 +0000873 break;
874 case ASN_CONSTR_CHOICE:
875 case ASN_CONSTR_SET:
876 case ASN_CONSTR_SET_OF:
877 case ASN_CONSTR_SEQUENCE:
878 case ASN_CONSTR_SEQUENCE_OF:
879 case ASN_BASIC_NULL:
880 case A1TC_UNIVERVAL:
vlm60e7ef02004-10-13 09:13:56 +0000881 printf(" EMPTY");
vlmceb93192004-10-14 05:11:25 +0000882 break;
883 case ASN_TYPE_ANY:
884 printf(" ANY");
885 break;
886 case ASN_BASIC_BIT_STRING:
887 case ASN_BASIC_OCTET_STRING:
888 case ASN_BASIC_OBJECT_IDENTIFIER:
889 case ASN_BASIC_RELATIVE_OID:
vlm40a69bf2004-10-15 06:01:54 +0000890 case ASN_BASIC_INTEGER:
vlmceb93192004-10-14 05:11:25 +0000891 case ASN_BASIC_UTCTime:
892 case ASN_BASIC_GeneralizedTime:
vlmceb93192004-10-14 05:11:25 +0000893 case ASN_STRING_NumericString:
894 case ASN_STRING_PrintableString:
vlm40a69bf2004-10-15 06:01:54 +0000895 printf(" (#PCDATA)");
vlmceb93192004-10-14 05:11:25 +0000896 break;
897 case ASN_STRING_VisibleString:
898 case ASN_STRING_ISO646String:
899 /* Entity references, but not XML elements may be present */
vlm60e7ef02004-10-13 09:13:56 +0000900 printf(" (#PCDATA)");
vlmceb93192004-10-14 05:11:25 +0000901 break;
902 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
903 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
904 default:
905 /*
906 * XML elements are allowed.
907 * For example, a UTF8String may contain "<bel/>".
908 */
909 printf(" ANY");
vlm60e7ef02004-10-13 09:13:56 +0000910 }
911 printf(">\n");
912
913 /*
914 * Display the descendants (children) of the current type.
915 */
vlmceb93192004-10-14 05:11:25 +0000916 if(!dont_involve_children) {
917 TQ_FOR(se, &(expr->members), next) {
918 if(se->expr_type == A1TC_EXTENSIBLE) continue;
919 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
920 }
vlm60e7ef02004-10-13 09:13:56 +0000921 }
922
923 return 0;
924}