blob: 4a0379c150fbc7bfcf270981af1307b60dc63d8a [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);
21static int asn1print_oid(asn1p_oid_t *oid, enum asn1print_flags flags);
22static 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) {
vlm281d82a2004-09-30 06:38:21 +000048 if(modno++) printf("\n");
vlm4808c702004-08-18 04:50:37 +000049 asn1print_module(asn, mod, flags);
vlmfa67ddc2004-06-03 03:38:44 +000050 }
51
52 return 0;
53}
54
55static int
vlm4808c702004-08-18 04:50:37 +000056asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +000057 asn1p_expr_t *tc;
58
vlm60e7ef02004-10-13 09:13:56 +000059 if(flags & APF_PRINT_XML_DTD)
60 printf("<!-- ASN.1 module\n");
61
vlm281d82a2004-09-30 06:38:21 +000062 printf("%s ", mod->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +000063 if(mod->module_oid) {
64 asn1print_oid(mod->module_oid, flags);
65 printf("\n");
66 }
67
vlm60e7ef02004-10-13 09:13:56 +000068 if(flags & APF_PRINT_XML_DTD) {
69 if(mod->source_file_name
70 && strcmp(mod->source_file_name, "-"))
71 printf("found in %s", mod->source_file_name);
72 printf(" -->\n\n");
73
74 TQ_FOR(tc, &(mod->members), next) {
75 asn1print_expr_dtd(asn, mod, tc, flags, 0);
76 }
77
78 return 0;
79 }
80
vlmfa67ddc2004-06-03 03:38:44 +000081 printf("DEFINITIONS");
82
vlm4808c702004-08-18 04:50:37 +000083 if(mod->module_flags & MSF_TAG_INSTRUCTIONS)
84 printf(" TAG INSTRUCTIONS");
85 if(mod->module_flags & MSF_XER_INSTRUCTIONS)
86 printf(" XER INSTRUCTIONS");
vlmfa67ddc2004-06-03 03:38:44 +000087 if(mod->module_flags & MSF_EXPLICIT_TAGS)
88 printf(" EXPLICIT TAGS");
89 if(mod->module_flags & MSF_IMPLICIT_TAGS)
90 printf(" IMPLICIT TAGS");
91 if(mod->module_flags & MSF_AUTOMATIC_TAGS)
92 printf(" AUTOMATIC TAGS");
93 if(mod->module_flags & MSF_EXTENSIBILITY_IMPLIED)
94 printf(" EXTENSIBILITY IMPLIED");
95
96 printf(" ::=\n");
97 printf("BEGIN\n\n");
98
99 TQ_FOR(tc, &(mod->members), next) {
vlm4808c702004-08-18 04:50:37 +0000100 asn1print_expr(asn, mod, tc, flags, 0);
vlm00ad2822004-08-20 13:24:28 +0000101 if(flags & APF_DEBUG_CONSTRAINTS)
102 printf("\n");
103 else
104 printf("\n\n");
vlmfa67ddc2004-06-03 03:38:44 +0000105 }
106
107 printf("END\n");
108
109 return 0;
110}
111
112static int
vlm4808c702004-08-18 04:50:37 +0000113asn1print_oid(asn1p_oid_t *oid, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000114 int ac;
115 int accum = 0;
116
vlmb42843a2004-06-05 08:17:50 +0000117 (void)flags; /* Unused argument */
118
vlmfa67ddc2004-06-03 03:38:44 +0000119 printf("{");
120 for(ac = 0; ac < oid->arcs_count; ac++) {
121 if(accum + strlen(oid->arcs[ac].name?:"") > 50) {
122 printf("\n\t");
123 accum = 0;
124 } else if(ac) printf(" ");
125
126 if(oid->arcs[ac].name) {
vlmdc4d95a2004-09-05 10:38:50 +0000127 printf("%s", oid->arcs[ac].name);
128 if(oid->arcs[ac].number >= 0) {
vlm47ae1582004-09-24 21:01:43 +0000129 printf("(%" PRIdASN ")", oid->arcs[ac].number);
vlmdc4d95a2004-09-05 10:38:50 +0000130 }
vlmfa67ddc2004-06-03 03:38:44 +0000131 accum += strlen(oid->arcs[ac].name);
132 } else {
133 printf("%d",
134 (int)oid->arcs[ac].number);
135 }
136 accum += 4;
137 }
138 printf(" }");
139
140 return 0;
141}
142
143static int
vlm4808c702004-08-18 04:50:37 +0000144asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000145 int cc;
146
vlmb42843a2004-06-05 08:17:50 +0000147 (void)flags; /* Unused argument */
148
vlmfa67ddc2004-06-03 03:38:44 +0000149 for(cc = 0; cc < ref->comp_count; cc++) {
150 if(cc) printf(".");
151 printf("%s", ref->components[cc].name);
152 }
153
154 return 0;
155}
156
157static int
vlm4808c702004-08-18 04:50:37 +0000158asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000159 struct asn1p_type_tag_s *tag = &tc->tag;
160
vlmb42843a2004-06-05 08:17:50 +0000161 (void)flags; /* Unused argument */
162
vlmfa67ddc2004-06-03 03:38:44 +0000163 if(tag->tag_class == TC_NOCLASS)
164 return 0;
165
166 printf("[");
167 switch(tag->tag_class) {
168 case TC_NOCLASS:
169 assert(tag->tag_class != TC_NOCLASS);
170 break;
171 case TC_UNIVERSAL: printf("UNIVERSAL "); break;
172 case TC_PRIVATE: printf("PRIVATE "); break;
173 case TC_APPLICATION: printf("APPLICATION "); break;
174 case TC_CONTEXT_SPECIFIC:
175 break;
176 }
vlm47ae1582004-09-24 21:01:43 +0000177 printf("%" PRIdASN "]", tag->tag_value);
vlmfa67ddc2004-06-03 03:38:44 +0000178
179 switch(tag->tag_mode) {
180 case TM_DEFAULT: break;
181 case TM_IMPLICIT: printf(" IMPLICIT"); break;
182 case TM_EXPLICIT: printf(" EXPLICIT"); break;
183 }
184
185 return 0;
186}
187
188static int
vlm4808c702004-08-18 04:50:37 +0000189asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000190
191 if(val == NULL)
192 return 0;
193
194 switch(val->type) {
195 case ATV_NOVALUE:
196 break;
vlm093f6752004-09-15 11:44:55 +0000197 case ATV_NULL:
198 printf("NULL");
199 return 0;
200 case ATV_REAL:
201 printf("%f", val->value.v_double);
202 return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000203 case ATV_INTEGER:
vlm47ae1582004-09-24 21:01:43 +0000204 printf("%" PRIdASN, val->value.v_integer);
vlmfa67ddc2004-06-03 03:38:44 +0000205 return 0;
206 case ATV_MIN: printf("MIN"); return 0;
207 case ATV_MAX: printf("MAX"); return 0;
208 case ATV_FALSE: printf("FALSE"); return 0;
209 case ATV_TRUE: printf("TRUE"); return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000210 case ATV_STRING:
211 {
212 char *p = val->value.string.buf;
213 putchar('"');
214 if(strchr(p, '"')) {
215 /* Mask quotes */
216 for(; *p; p++) {
217 if(*p == '"')
218 putchar(*p);
219 putchar(*p);
220 }
221 } else {
222 fputs(p, stdout);
223 }
224 putchar('"');
225 }
226 return 0;
227 case ATV_UNPARSED:
228 fputs(val->value.string.buf, stdout);
229 return 0;
230 case ATV_BITVECTOR:
231 {
232 uint8_t *bitvector;
233 int bits;
234 int i;
235
236 bitvector = val->value.binary_vector.bits;
237 bits = val->value.binary_vector.size_in_bits;
238
239 printf("'");
240 if(bits%8) {
241 for(i = 0; i < bits; i++) {
242 uint8_t uc;
243 uc = bitvector[i>>3];
244 putchar(((uc >> (7-(i%8)))&1)?'1':'0');
245 }
246 printf("'B");
247 } else {
248 char hextable[16] = "0123456789ABCDEF";
249 for(i = 0; i < (bits>>3); i++) {
250 putchar(hextable[bitvector[i] >> 4]);
251 putchar(hextable[bitvector[i] & 0x0f]);
252 }
253 printf("'H");
254 }
255 }
vlm093f6752004-09-15 11:44:55 +0000256 case ATV_REFERENCED:
257 return asn1print_ref(val->value.reference, flags);
258 case ATV_CHOICE_IDENTIFIER:
259 printf("%s: ", val->value.choice_identifier.identifier);
260 return asn1print_value(val->value.choice_identifier.value, flags);
vlmfa67ddc2004-06-03 03:38:44 +0000261 }
262
263 assert(val->type || !"Unknown");
264
265 return 0;
266}
267
268static int
vlm4808c702004-08-18 04:50:37 +0000269asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000270 int symno = 0;
271
272 if(ct == 0) return 0;
273
274 if(ct->type == ACT_CA_SET)
275 printf("(");
276
277 switch(ct->type) {
278 case ACT_EL_VALUE:
279 asn1print_value(ct->value, flags);
280 break;
281 case ACT_EL_RANGE:
282 case ACT_EL_LLRANGE:
283 case ACT_EL_RLRANGE:
284 case ACT_EL_ULRANGE:
285 asn1print_value(ct->range_start, flags);
286 switch(ct->type) {
287 case ACT_EL_RANGE: printf(".."); break;
288 case ACT_EL_LLRANGE: printf("<.."); break;
289 case ACT_EL_RLRANGE: printf("..<"); break;
290 case ACT_EL_ULRANGE: printf("<..<"); break;
291 default: printf("?..?"); break;
292 }
293 asn1print_value(ct->range_stop, flags);
294 break;
295 case ACT_EL_EXT:
296 printf("...");
297 break;
298 case ACT_CT_SIZE:
299 case ACT_CT_FROM:
300 switch(ct->type) {
vlm4808c702004-08-18 04:50:37 +0000301 case ACT_CT_SIZE: printf("SIZE("); break;
302 case ACT_CT_FROM: printf("FROM("); break;
vlmfa67ddc2004-06-03 03:38:44 +0000303 default: printf("??? ("); break;
304 }
305 assert(ct->el_count != 0);
306 assert(ct->el_count == 1);
307 asn1print_constraint(ct->elements[0], flags);
308 printf(")");
309 break;
310 case ACT_CT_WCOMP:
311 case ACT_CT_WCOMPS:
312 printf("???");
313 break;
314 case ACT_CA_SET: symno++;
315 case ACT_CA_CRC: symno++;
316 case ACT_CA_CSV: symno++;
317 case ACT_CA_UNI: symno++;
318 case ACT_CA_INT: symno++;
319 case ACT_CA_EXC:
320 {
vlm4808c702004-08-18 04:50:37 +0000321 char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",",
vlmfa67ddc2004-06-03 03:38:44 +0000322 "", "(" };
vlmdc4d95a2004-09-05 10:38:50 +0000323 unsigned int i;
vlmfa67ddc2004-06-03 03:38:44 +0000324 for(i = 0; i < ct->el_count; i++) {
vlm4808c702004-08-18 04:50:37 +0000325 enum asn1print_flags nflags = flags;
vlmfa67ddc2004-06-03 03:38:44 +0000326 if(i) fputs(symtable[symno], stdout);
327 if(ct->type == ACT_CA_CRC) fputs("{", stdout);
328 asn1print_constraint(ct->elements[i], nflags);
329 if(ct->type == ACT_CA_CRC) fputs("}", stdout);
330 if(i+1 < ct->el_count
331 && ct->type == ACT_CA_SET)
332 fputs(")", stdout);
333 }
334 }
335 break;
336 case ACT_INVALID:
337 assert(ct->type != ACT_INVALID);
338 break;
339 }
340
341 if(ct->type == ACT_CA_SET)
342 printf(")");
343
344 return 0;
345}
346
347static int
vlm4808c702004-08-18 04:50:37 +0000348asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000349 if(pl) {
350 int i;
351 printf("{");
352 for(i = 0; i < pl->params_count; i++) {
353 if(i) printf(", ");
354 if(pl->params[i].governor) {
355 asn1print_ref(pl->params[i].governor, flags);
356 printf(":");
357 }
358 printf("%s", pl->params[i].argument);
359 }
360 printf("}");
361 }
362
363 return 0;
364}
365
366static int
vlm4808c702004-08-18 04:50:37 +0000367asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000368 if(wx) {
369 asn1p_wsyntx_chunk_t *wc;
370 printf(" WITH SYNTAX {");
371 TQ_FOR(wc, &(wx->chunks), next) {
372 if(wc->ref) {
373 asn1print_ref(wc->ref, flags);
374 } else {
375 fwrite(wc->buf, 1, wc->len, stdout);
376 }
377 }
378 printf("}\n");
379 }
380
381 return 0;
382}
383
384static int
vlm4808c702004-08-18 04:50:37 +0000385asn1print_crange_value(asn1cnst_edge_t *edge, int as_char) {
386 switch(edge->type) {
387 case ARE_MIN: printf("MIN"); break;
388 case ARE_MAX: printf("MAX"); break;
389 case ARE_VALUE:
390 if(as_char) {
391 printf("\"%c\"", (unsigned char)edge->value);
392 } else {
vlm47ae1582004-09-24 21:01:43 +0000393 printf("%" PRIdASN, edge->value);
vlm4808c702004-08-18 04:50:37 +0000394 }
395 }
396 return 0;
397}
398
399static int
vlmeeca98f2004-08-25 02:00:03 +0000400asn1print_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 +0000401 asn1cnst_range_t *range;
402 int as_char = (type==ACT_CT_FROM);
403 int i;
404
vlmeeca98f2004-08-25 02:00:03 +0000405 range = asn1constraint_compute_PER_range(expr_type, ct, type,
406 0, 0, strict_PER_visible);
vlm4808c702004-08-18 04:50:37 +0000407 if(!range) return -1;
408
vlmeeca98f2004-08-25 02:00:03 +0000409 if(range->incompatible
410 || (strict_PER_visible && range->not_PER_visible)) {
411 asn1constraint_range_free(range);
412 return 0;
413 }
414
vlm4808c702004-08-18 04:50:37 +0000415 switch(type) {
416 case ACT_CT_FROM: printf("(FROM("); break;
417 case ACT_CT_SIZE: printf("(SIZE("); break;
418 default: printf("("); break;
419 }
420 for(i = -1; i < range->el_count; i++) {
421 asn1cnst_range_t *r;
422 if(i == -1) {
423 if(range->el_count) continue;
424 r = range;
425 } else {
426 r = range->elements[i];
427 }
428 if(i > 0) {
429 printf(" | ");
430 }
431 asn1print_crange_value(&r->left, as_char);
432 if(r->left.type != r->right.type
433 || r->left.value != r->right.value) {
434 printf("..");
435 asn1print_crange_value(&r->right, as_char);
436 }
437 }
438 if(range->extensible)
439 printf(",...");
440 printf(type==ACT_EL_RANGE?")":"))");
441
442 if(range->empty_constraint)
443 printf(":Empty!");
444
445 asn1constraint_range_free(range);
446 return 0;
447}
448
449static int
450asn1print_constraint_explain(asn1p_expr_type_e expr_type,
vlmeeca98f2004-08-25 02:00:03 +0000451 asn1p_constraint_t *ct, int s_PV) {
vlm4808c702004-08-18 04:50:37 +0000452
vlmeeca98f2004-08-25 02:00:03 +0000453 asn1print_constraint_explain_type(expr_type, ct, ACT_EL_RANGE, s_PV);
vlm4808c702004-08-18 04:50:37 +0000454 printf(" ");
vlmeeca98f2004-08-25 02:00:03 +0000455 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_SIZE, s_PV);
vlm4808c702004-08-18 04:50:37 +0000456 printf(" ");
vlmeeca98f2004-08-25 02:00:03 +0000457 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_FROM, s_PV);
vlm4808c702004-08-18 04:50:37 +0000458
459 return 0;
460}
461
462static int
463asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level) {
vlm00ad2822004-08-20 13:24:28 +0000464 int SEQ_OF = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000465
vlmdb4ee042005-02-15 07:06:05 +0000466 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
vlm60e7ef02004-10-13 09:13:56 +0000467 INDENT("-- #line %d\n", tc->_lineno);
vlmfa67ddc2004-06-03 03:38:44 +0000468 if(tc->Identifier)
469 INDENT("%s", tc->Identifier);
470
471 if(tc->params) {
472 asn1print_params(tc->params, flags);
473 }
474
475 if(tc->meta_type != AMT_VALUE
vlmda4df3f2004-08-25 02:27:47 +0000476 && tc->meta_type != AMT_VALUESET
vlmfa67ddc2004-06-03 03:38:44 +0000477 && tc->expr_type != A1TC_EXTENSIBLE) {
478 if(level) {
vlmdb4ee042005-02-15 07:06:05 +0000479 if(tc->Identifier && !(flags & APF_NOINDENT))
vlmfa67ddc2004-06-03 03:38:44 +0000480 printf("\t");
481 } else {
482 printf(" ::=");
483 }
484 }
485
486 if(tc->tag.tag_class) {
487 printf(" ");
488 asn1print_tag(tc, flags);
489 }
490
491 switch(tc->expr_type) {
492 case A1TC_EXTENSIBLE:
493 if(tc->value) {
494 printf("!");
495 asn1print_value(tc->value, flags);
496 }
497 break;
vlmddd6fd12004-08-22 03:08:51 +0000498 case A1TC_COMPONENTS_OF:
499 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
500 printf(" COMPONENTS OF");
501 break;
vlmdb4ee042005-02-15 07:06:05 +0000502 case A1TC_PARAMETRIZED:
503 flags |= APF_NOINDENT;
vlmfa67ddc2004-06-03 03:38:44 +0000504 case A1TC_REFERENCE:
505 case A1TC_UNIVERVAL:
vlmfa67ddc2004-06-03 03:38:44 +0000506 break;
507 case A1TC_CLASSDEF:
508 printf(" CLASS");
509 break;
510 case A1TC_CLASSFIELD:
511 /* Nothing to print here */
512 break;
vlm00ad2822004-08-20 13:24:28 +0000513 case ASN_CONSTR_SET_OF:
514 case ASN_CONSTR_SEQUENCE_OF:
515 SEQ_OF = 1;
516 if(tc->expr_type == ASN_CONSTR_SET_OF)
517 printf(" SET");
518 else
519 printf(" SEQUENCE");
520 if(tc->constraints) {
521 printf(" ");
522 asn1print_constraint(tc->constraints, flags);
523 }
524 printf(" OF");
525 break;
vlmfa67ddc2004-06-03 03:38:44 +0000526 default:
527 {
528 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
529 printf(" %s", p?p:"<unknown type!>");
530 }
531 break;
532 }
533
534 if(tc->reference) {
vlmdb4ee042005-02-15 07:06:05 +0000535 if(!(flags & APF_NOINDENT))
536 printf(" ");
vlmfa67ddc2004-06-03 03:38:44 +0000537 asn1print_ref(tc->reference, flags);
538 }
539
vlmda4df3f2004-08-25 02:27:47 +0000540 if(tc->meta_type == AMT_VALUESET)
541 printf(" ::=");
542
vlmfa67ddc2004-06-03 03:38:44 +0000543 /*
544 * Display the descendants (children) of the current type.
545 */
vlmda4df3f2004-08-25 02:27:47 +0000546 if(TQ_FIRST(&(tc->members))
547 || (tc->expr_type & ASN_CONSTR_MASK)
548 || tc->meta_type == AMT_VALUESET
549 || tc->meta_type == AMT_OBJECT
550 || tc->meta_type == AMT_OBJECTSET
551 ) {
vlmfa67ddc2004-06-03 03:38:44 +0000552 asn1p_expr_t *se; /* SubExpression */
vlmddd6fd12004-08-22 03:08:51 +0000553 int put_braces = !SEQ_OF; /* Don't need 'em, if SET OF... */
vlmfa67ddc2004-06-03 03:38:44 +0000554
vlmda4df3f2004-08-25 02:27:47 +0000555 if(put_braces) {
vlmdb4ee042005-02-15 07:06:05 +0000556 if(flags & APF_NOINDENT) {
557 printf("{");
558 if(!TQ_FIRST(&tc->members))
559 printf("}");
560 } else {
561 printf(" {");
562 if(TQ_FIRST(&tc->members))
563 printf("\n");
564 else
565 printf(" }");
566 }
vlmda4df3f2004-08-25 02:27:47 +0000567 }
vlmfa67ddc2004-06-03 03:38:44 +0000568
569 TQ_FOR(se, &(tc->members), next) {
570 /*
vlmddd6fd12004-08-22 03:08:51 +0000571 * Print the expression as it were a stand-alone type.
vlmfa67ddc2004-06-03 03:38:44 +0000572 */
vlm60e7ef02004-10-13 09:13:56 +0000573 asn1print_expr(asn, mod, se, flags, level + 1);
vlm093f6752004-09-15 11:44:55 +0000574 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
575 printf(" DEFAULT ");
576 asn1print_value(se->marker.default_value, flags);
577 } else if((se->marker.flags & EM_OPTIONAL)
578 == EM_OPTIONAL) {
vlmd82eb012004-09-10 08:21:27 +0000579 printf(" OPTIONAL");
vlm093f6752004-09-15 11:44:55 +0000580 }
vlmfa67ddc2004-06-03 03:38:44 +0000581 if(TQ_NEXT(se, next)) {
582 printf(",");
vlmdb4ee042005-02-15 07:06:05 +0000583 if(!(flags & APF_NOINDENT))
584 INDENT("\n");
vlmfa67ddc2004-06-03 03:38:44 +0000585 }
586 }
587
vlmda4df3f2004-08-25 02:27:47 +0000588 if(put_braces && TQ_FIRST(&tc->members)) {
vlmdb4ee042005-02-15 07:06:05 +0000589 if(!(flags & APF_NOINDENT))
590 printf("\n");
vlmfa67ddc2004-06-03 03:38:44 +0000591 INDENT("}");
592 }
593 }
594
595 if(tc->with_syntax)
596 asn1print_with_syntax(tc->with_syntax, flags);
597
vlm00ad2822004-08-20 13:24:28 +0000598 if(!SEQ_OF && tc->constraints) {
vlmfa67ddc2004-06-03 03:38:44 +0000599 printf(" ");
600 asn1print_constraint(tc->constraints, flags);
601 }
vlm4808c702004-08-18 04:50:37 +0000602
vlmfa67ddc2004-06-03 03:38:44 +0000603 if(tc->unique) {
604 printf(" UNIQUE");
605 }
606
607 if(tc->meta_type == AMT_VALUE
608 && tc->expr_type != A1TC_EXTENSIBLE) {
vlmdc4d95a2004-09-05 10:38:50 +0000609 if(tc->expr_type == A1TC_UNIVERVAL) {
vlm093f6752004-09-15 11:44:55 +0000610 if(tc->value) {
611 printf("(");
612 asn1print_value(tc->value, flags);
613 printf(")");
614 }
vlmdc4d95a2004-09-05 10:38:50 +0000615 } else {
616 printf(" ::= ");
617 asn1print_value(tc->value, flags);
618 }
vlmfa67ddc2004-06-03 03:38:44 +0000619 }
620
vlmddd6fd12004-08-22 03:08:51 +0000621 /*
vlmdb4ee042005-02-15 07:06:05 +0000622 * The following section exists entirely for debugging.
vlmddd6fd12004-08-22 03:08:51 +0000623 */
vlm00ad2822004-08-20 13:24:28 +0000624 if(flags & APF_DEBUG_CONSTRAINTS
625 && tc->expr_type != A1TC_EXTENSIBLE) {
vlm4808c702004-08-18 04:50:37 +0000626 asn1p_expr_t *top_parent;
627
628 if(tc->combined_constraints) {
629 printf("\n-- Combined constraints: ");
630 asn1print_constraint(tc->combined_constraints, flags);
631 }
632
vlmcbebb812004-09-22 16:05:13 +0000633 top_parent = asn1f_find_terminal_type_ex(asn, tc);
vlm4808c702004-08-18 04:50:37 +0000634 if(top_parent) {
vlmeeca98f2004-08-25 02:00:03 +0000635 printf("\n-- Practical constraints (%s): ",
636 top_parent->Identifier);
637 asn1print_constraint_explain(top_parent->expr_type,
638 tc->combined_constraints, 0);
vlm00ad2822004-08-20 13:24:28 +0000639 printf("\n-- PER-visible constraints (%s): ",
640 top_parent->Identifier);
vlm4808c702004-08-18 04:50:37 +0000641 asn1print_constraint_explain(top_parent->expr_type,
vlmeeca98f2004-08-25 02:00:03 +0000642 tc->combined_constraints, 1);
vlm4808c702004-08-18 04:50:37 +0000643 }
vlm00ad2822004-08-20 13:24:28 +0000644 printf("\n");
vlm4808c702004-08-18 04:50:37 +0000645 }
646
vlmfa67ddc2004-06-03 03:38:44 +0000647 return 0;
648}
vlm4808c702004-08-18 04:50:37 +0000649
vlm60e7ef02004-10-13 09:13:56 +0000650
651static int
652asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
653 asn1p_expr_t *se;
654 int expr_unordered = 0;
vlmceb93192004-10-14 05:11:25 +0000655 int dont_involve_children = 0;
vlm60e7ef02004-10-13 09:13:56 +0000656
657 switch(expr->meta_type) {
658 case AMT_TYPE:
659 case AMT_TYPEREF:
660 break;
661 default:
662 if(expr->expr_type == A1TC_UNIVERVAL)
663 break;
664 return 0;
665 }
666
667 if(!expr->Identifier) return 0;
668
669 if(expr->expr_type == ASN_CONSTR_CHOICE
670 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
671 || expr->expr_type == ASN_CONSTR_SET_OF
672 || expr->expr_type == ASN_CONSTR_SET
673 || expr->expr_type == ASN_BASIC_INTEGER
674 || expr->expr_type == ASN_BASIC_ENUMERATED) {
675 expr_unordered = 1;
676 }
677
678 if(flags & APF_LINE_COMMENTS)
679 INDENT("<!-- #line %d -->\n", expr->_lineno);
680 INDENT("<!ELEMENT %s", expr->Identifier);
681
682 if(expr->expr_type == A1TC_REFERENCE) {
683 se = asn1f_find_terminal_type_ex(asn, expr);
684 if(!se) {
vlmceb93192004-10-14 05:11:25 +0000685 printf(" (ANY)");
vlm60e7ef02004-10-13 09:13:56 +0000686 return 0;
687 }
688 expr = se;
vlmceb93192004-10-14 05:11:25 +0000689 dont_involve_children = 1;
vlm60e7ef02004-10-13 09:13:56 +0000690 }
691
692 if(TQ_FIRST(&expr->members)) {
693 int extensible = 0;
694 printf(" (");
695 TQ_FOR(se, &(expr->members), next) {
696 if(se->expr_type == A1TC_EXTENSIBLE) {
697 extensible = 1;
698 continue;
699 } else if(!se->Identifier
700 && se->expr_type == A1TC_REFERENCE) {
701 asn1print_ref(se->reference, flags);
702 } else if(se->Identifier) {
703 printf("%s", se->Identifier);
704 } else {
705 printf("ANY");
706 }
707 if(expr->expr_type != ASN_CONSTR_SET
708 && expr->expr_type != ASN_CONSTR_CHOICE
709 && expr->expr_type != ASN_BASIC_INTEGER
710 && expr->expr_type != ASN_BASIC_ENUMERATED) {
711 if(expr_unordered)
712 printf("*");
713 else if(se->marker.flags)
714 printf("?");
715 }
716 if(TQ_NEXT(se, next)
717 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
718 printf(expr_unordered?"|":", ");
719 }
720 }
721 if(extensible) {
722 printf(expr_unordered?"|":", ");
723 printf("ANY");
724 if(expr->expr_type != ASN_CONSTR_SET
725 && expr->expr_type != ASN_CONSTR_CHOICE
726 && expr->expr_type != ASN_BASIC_INTEGER
727 && expr->expr_type != ASN_BASIC_ENUMERATED)
728 printf("*");
729 }
730
731 printf(")");
732 if(expr->expr_type == ASN_CONSTR_SET)
733 printf("*");
734
vlmceb93192004-10-14 05:11:25 +0000735 } else switch(expr->expr_type) {
736 case ASN_BASIC_BOOLEAN:
737 printf("(true|false)");
738 break;
739 case ASN_CONSTR_CHOICE:
740 case ASN_CONSTR_SET:
741 case ASN_CONSTR_SET_OF:
742 case ASN_CONSTR_SEQUENCE:
743 case ASN_CONSTR_SEQUENCE_OF:
744 case ASN_BASIC_NULL:
745 case A1TC_UNIVERVAL:
vlm60e7ef02004-10-13 09:13:56 +0000746 printf(" EMPTY");
vlmceb93192004-10-14 05:11:25 +0000747 break;
748 case ASN_TYPE_ANY:
749 printf(" ANY");
750 break;
751 case ASN_BASIC_BIT_STRING:
752 case ASN_BASIC_OCTET_STRING:
753 case ASN_BASIC_OBJECT_IDENTIFIER:
754 case ASN_BASIC_RELATIVE_OID:
vlm40a69bf2004-10-15 06:01:54 +0000755 case ASN_BASIC_INTEGER:
vlmceb93192004-10-14 05:11:25 +0000756 case ASN_BASIC_UTCTime:
757 case ASN_BASIC_GeneralizedTime:
vlmceb93192004-10-14 05:11:25 +0000758 case ASN_STRING_NumericString:
759 case ASN_STRING_PrintableString:
vlm40a69bf2004-10-15 06:01:54 +0000760 printf(" (#PCDATA)");
vlmceb93192004-10-14 05:11:25 +0000761 break;
762 case ASN_STRING_VisibleString:
763 case ASN_STRING_ISO646String:
764 /* Entity references, but not XML elements may be present */
vlm60e7ef02004-10-13 09:13:56 +0000765 printf(" (#PCDATA)");
vlmceb93192004-10-14 05:11:25 +0000766 break;
767 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
768 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
769 default:
770 /*
771 * XML elements are allowed.
772 * For example, a UTF8String may contain "<bel/>".
773 */
774 printf(" ANY");
vlm60e7ef02004-10-13 09:13:56 +0000775 }
776 printf(">\n");
777
778 /*
779 * Display the descendants (children) of the current type.
780 */
vlmceb93192004-10-14 05:11:25 +0000781 if(!dont_involve_children) {
782 TQ_FOR(se, &(expr->members), next) {
783 if(se->expr_type == A1TC_EXTENSIBLE) continue;
784 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
785 }
vlm60e7ef02004-10-13 09:13:56 +0000786 }
787
788 return 0;
789}