blob: 43a3a0d66787287c17b0ecab6bb16e9cab85247b [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
vlmc45eab72005-03-09 20:52:15 +000052 if(flags & APF_PRINT_XML_DTD) {
53 /* Values for BOOLEAN */
54 printf("<!ELEMENT true EMPTY>\n");
55 printf("<!ELEMENT false EMPTY>\n");
56 }
57
vlmfa67ddc2004-06-03 03:38:44 +000058 return 0;
59}
60
61static int
vlm4808c702004-08-18 04:50:37 +000062asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +000063 asn1p_expr_t *tc;
64
vlm60e7ef02004-10-13 09:13:56 +000065 if(flags & APF_PRINT_XML_DTD)
66 printf("<!-- ASN.1 module\n");
67
vlm281d82a2004-09-30 06:38:21 +000068 printf("%s ", mod->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +000069 if(mod->module_oid) {
70 asn1print_oid(mod->module_oid, flags);
71 printf("\n");
72 }
73
vlm60e7ef02004-10-13 09:13:56 +000074 if(flags & APF_PRINT_XML_DTD) {
75 if(mod->source_file_name
76 && strcmp(mod->source_file_name, "-"))
77 printf("found in %s", mod->source_file_name);
78 printf(" -->\n\n");
79
80 TQ_FOR(tc, &(mod->members), next) {
81 asn1print_expr_dtd(asn, mod, tc, flags, 0);
82 }
83
84 return 0;
85 }
86
vlmfa67ddc2004-06-03 03:38:44 +000087 printf("DEFINITIONS");
88
vlm4808c702004-08-18 04:50:37 +000089 if(mod->module_flags & MSF_TAG_INSTRUCTIONS)
90 printf(" TAG INSTRUCTIONS");
91 if(mod->module_flags & MSF_XER_INSTRUCTIONS)
92 printf(" XER INSTRUCTIONS");
vlmfa67ddc2004-06-03 03:38:44 +000093 if(mod->module_flags & MSF_EXPLICIT_TAGS)
94 printf(" EXPLICIT TAGS");
95 if(mod->module_flags & MSF_IMPLICIT_TAGS)
96 printf(" IMPLICIT TAGS");
97 if(mod->module_flags & MSF_AUTOMATIC_TAGS)
98 printf(" AUTOMATIC TAGS");
99 if(mod->module_flags & MSF_EXTENSIBILITY_IMPLIED)
100 printf(" EXTENSIBILITY IMPLIED");
101
102 printf(" ::=\n");
103 printf("BEGIN\n\n");
104
105 TQ_FOR(tc, &(mod->members), next) {
vlm4808c702004-08-18 04:50:37 +0000106 asn1print_expr(asn, mod, tc, flags, 0);
vlm00ad2822004-08-20 13:24:28 +0000107 if(flags & APF_DEBUG_CONSTRAINTS)
108 printf("\n");
109 else
110 printf("\n\n");
vlmfa67ddc2004-06-03 03:38:44 +0000111 }
112
113 printf("END\n");
114
115 return 0;
116}
117
118static int
vlm4808c702004-08-18 04:50:37 +0000119asn1print_oid(asn1p_oid_t *oid, enum asn1print_flags flags) {
vlm8a09e0f2005-02-25 14:20:30 +0000120 size_t accum = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000121 int ac;
vlmfa67ddc2004-06-03 03:38:44 +0000122
vlmb42843a2004-06-05 08:17:50 +0000123 (void)flags; /* Unused argument */
124
vlmfa67ddc2004-06-03 03:38:44 +0000125 printf("{");
126 for(ac = 0; ac < oid->arcs_count; ac++) {
vlm8a09e0f2005-02-25 14:20:30 +0000127 const char *arcname = oid->arcs[ac].name;
128
129 if(accum + strlen(arcname ? arcname : "") > 50) {
vlmfa67ddc2004-06-03 03:38:44 +0000130 printf("\n\t");
131 accum = 0;
vlm8a09e0f2005-02-25 14:20:30 +0000132 } else if(ac) {
133 printf(" ");
134 }
vlmfa67ddc2004-06-03 03:38:44 +0000135
vlm8a09e0f2005-02-25 14:20:30 +0000136 if(arcname) {
137 printf("%s", arcname);
vlmdc4d95a2004-09-05 10:38:50 +0000138 if(oid->arcs[ac].number >= 0) {
vlm47ae1582004-09-24 21:01:43 +0000139 printf("(%" PRIdASN ")", oid->arcs[ac].number);
vlmdc4d95a2004-09-05 10:38:50 +0000140 }
vlmfa67ddc2004-06-03 03:38:44 +0000141 accum += strlen(oid->arcs[ac].name);
142 } else {
vlm8a09e0f2005-02-25 14:20:30 +0000143 printf("%" PRIdASN, oid->arcs[ac].number);
vlmfa67ddc2004-06-03 03:38:44 +0000144 }
145 accum += 4;
146 }
147 printf(" }");
148
149 return 0;
150}
151
152static int
vlm4808c702004-08-18 04:50:37 +0000153asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000154 int cc;
155
vlmb42843a2004-06-05 08:17:50 +0000156 (void)flags; /* Unused argument */
157
vlmfa67ddc2004-06-03 03:38:44 +0000158 for(cc = 0; cc < ref->comp_count; cc++) {
159 if(cc) printf(".");
160 printf("%s", ref->components[cc].name);
161 }
162
163 return 0;
164}
165
166static int
vlm4808c702004-08-18 04:50:37 +0000167asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000168 struct asn1p_type_tag_s *tag = &tc->tag;
169
vlmb42843a2004-06-05 08:17:50 +0000170 (void)flags; /* Unused argument */
171
vlmfa67ddc2004-06-03 03:38:44 +0000172 if(tag->tag_class == TC_NOCLASS)
173 return 0;
174
175 printf("[");
176 switch(tag->tag_class) {
177 case TC_NOCLASS:
178 assert(tag->tag_class != TC_NOCLASS);
179 break;
180 case TC_UNIVERSAL: printf("UNIVERSAL "); break;
181 case TC_PRIVATE: printf("PRIVATE "); break;
182 case TC_APPLICATION: printf("APPLICATION "); break;
183 case TC_CONTEXT_SPECIFIC:
184 break;
185 }
vlm47ae1582004-09-24 21:01:43 +0000186 printf("%" PRIdASN "]", tag->tag_value);
vlmfa67ddc2004-06-03 03:38:44 +0000187
188 switch(tag->tag_mode) {
189 case TM_DEFAULT: break;
190 case TM_IMPLICIT: printf(" IMPLICIT"); break;
191 case TM_EXPLICIT: printf(" EXPLICIT"); break;
192 }
193
194 return 0;
195}
196
197static int
vlm4808c702004-08-18 04:50:37 +0000198asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000199
200 if(val == NULL)
201 return 0;
202
203 switch(val->type) {
204 case ATV_NOVALUE:
205 break;
vlm093f6752004-09-15 11:44:55 +0000206 case ATV_NULL:
207 printf("NULL");
208 return 0;
209 case ATV_REAL:
210 printf("%f", val->value.v_double);
211 return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000212 case ATV_INTEGER:
vlm47ae1582004-09-24 21:01:43 +0000213 printf("%" PRIdASN, val->value.v_integer);
vlmfa67ddc2004-06-03 03:38:44 +0000214 return 0;
215 case ATV_MIN: printf("MIN"); return 0;
216 case ATV_MAX: printf("MAX"); return 0;
217 case ATV_FALSE: printf("FALSE"); return 0;
218 case ATV_TRUE: printf("TRUE"); return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000219 case ATV_STRING:
220 {
221 char *p = val->value.string.buf;
222 putchar('"');
223 if(strchr(p, '"')) {
224 /* Mask quotes */
225 for(; *p; p++) {
226 if(*p == '"')
227 putchar(*p);
228 putchar(*p);
229 }
230 } else {
231 fputs(p, stdout);
232 }
233 putchar('"');
234 }
235 return 0;
236 case ATV_UNPARSED:
237 fputs(val->value.string.buf, stdout);
238 return 0;
239 case ATV_BITVECTOR:
240 {
241 uint8_t *bitvector;
242 int bits;
243 int i;
244
245 bitvector = val->value.binary_vector.bits;
246 bits = val->value.binary_vector.size_in_bits;
247
248 printf("'");
249 if(bits%8) {
250 for(i = 0; i < bits; i++) {
251 uint8_t uc;
252 uc = bitvector[i>>3];
253 putchar(((uc >> (7-(i%8)))&1)?'1':'0');
254 }
255 printf("'B");
256 } else {
257 char hextable[16] = "0123456789ABCDEF";
258 for(i = 0; i < (bits>>3); i++) {
259 putchar(hextable[bitvector[i] >> 4]);
260 putchar(hextable[bitvector[i] & 0x0f]);
261 }
262 printf("'H");
263 }
vlma9e55432005-02-24 21:07:35 +0000264 return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000265 }
vlm093f6752004-09-15 11:44:55 +0000266 case ATV_REFERENCED:
267 return asn1print_ref(val->value.reference, flags);
268 case ATV_CHOICE_IDENTIFIER:
269 printf("%s: ", val->value.choice_identifier.identifier);
270 return asn1print_value(val->value.choice_identifier.value, flags);
vlmfa67ddc2004-06-03 03:38:44 +0000271 }
272
273 assert(val->type || !"Unknown");
274
275 return 0;
276}
277
278static int
vlm4808c702004-08-18 04:50:37 +0000279asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000280 int symno = 0;
281
282 if(ct == 0) return 0;
283
284 if(ct->type == ACT_CA_SET)
285 printf("(");
286
287 switch(ct->type) {
288 case ACT_EL_VALUE:
289 asn1print_value(ct->value, flags);
290 break;
291 case ACT_EL_RANGE:
292 case ACT_EL_LLRANGE:
293 case ACT_EL_RLRANGE:
294 case ACT_EL_ULRANGE:
295 asn1print_value(ct->range_start, flags);
296 switch(ct->type) {
297 case ACT_EL_RANGE: printf(".."); break;
298 case ACT_EL_LLRANGE: printf("<.."); break;
299 case ACT_EL_RLRANGE: printf("..<"); break;
300 case ACT_EL_ULRANGE: printf("<..<"); break;
301 default: printf("?..?"); break;
302 }
303 asn1print_value(ct->range_stop, flags);
304 break;
305 case ACT_EL_EXT:
306 printf("...");
307 break;
308 case ACT_CT_SIZE:
309 case ACT_CT_FROM:
310 switch(ct->type) {
vlm4808c702004-08-18 04:50:37 +0000311 case ACT_CT_SIZE: printf("SIZE("); break;
312 case ACT_CT_FROM: printf("FROM("); break;
vlmfa67ddc2004-06-03 03:38:44 +0000313 default: printf("??? ("); break;
314 }
315 assert(ct->el_count != 0);
316 assert(ct->el_count == 1);
317 asn1print_constraint(ct->elements[0], flags);
318 printf(")");
319 break;
320 case ACT_CT_WCOMP:
321 case ACT_CT_WCOMPS:
322 printf("???");
323 break;
324 case ACT_CA_SET: symno++;
325 case ACT_CA_CRC: symno++;
326 case ACT_CA_CSV: symno++;
327 case ACT_CA_UNI: symno++;
328 case ACT_CA_INT: symno++;
329 case ACT_CA_EXC:
330 {
vlm4808c702004-08-18 04:50:37 +0000331 char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",",
vlmfa67ddc2004-06-03 03:38:44 +0000332 "", "(" };
vlmdc4d95a2004-09-05 10:38:50 +0000333 unsigned int i;
vlmfa67ddc2004-06-03 03:38:44 +0000334 for(i = 0; i < ct->el_count; i++) {
vlm4808c702004-08-18 04:50:37 +0000335 enum asn1print_flags nflags = flags;
vlmfa67ddc2004-06-03 03:38:44 +0000336 if(i) fputs(symtable[symno], stdout);
337 if(ct->type == ACT_CA_CRC) fputs("{", stdout);
338 asn1print_constraint(ct->elements[i], nflags);
339 if(ct->type == ACT_CA_CRC) fputs("}", stdout);
340 if(i+1 < ct->el_count
341 && ct->type == ACT_CA_SET)
342 fputs(")", stdout);
343 }
344 }
345 break;
346 case ACT_INVALID:
347 assert(ct->type != ACT_INVALID);
348 break;
349 }
350
351 if(ct->type == ACT_CA_SET)
352 printf(")");
353
354 return 0;
355}
356
357static int
vlm4808c702004-08-18 04:50:37 +0000358asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000359 if(pl) {
360 int i;
361 printf("{");
362 for(i = 0; i < pl->params_count; i++) {
363 if(i) printf(", ");
364 if(pl->params[i].governor) {
365 asn1print_ref(pl->params[i].governor, flags);
366 printf(":");
367 }
368 printf("%s", pl->params[i].argument);
369 }
370 printf("}");
371 }
372
373 return 0;
374}
375
376static int
vlm4808c702004-08-18 04:50:37 +0000377asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000378 if(wx) {
379 asn1p_wsyntx_chunk_t *wc;
380 printf(" WITH SYNTAX {");
381 TQ_FOR(wc, &(wx->chunks), next) {
382 if(wc->ref) {
383 asn1print_ref(wc->ref, flags);
384 } else {
385 fwrite(wc->buf, 1, wc->len, stdout);
386 }
387 }
388 printf("}\n");
389 }
390
391 return 0;
392}
393
394static int
vlm4808c702004-08-18 04:50:37 +0000395asn1print_crange_value(asn1cnst_edge_t *edge, int as_char) {
396 switch(edge->type) {
397 case ARE_MIN: printf("MIN"); break;
398 case ARE_MAX: printf("MAX"); break;
399 case ARE_VALUE:
400 if(as_char) {
401 printf("\"%c\"", (unsigned char)edge->value);
402 } else {
vlm47ae1582004-09-24 21:01:43 +0000403 printf("%" PRIdASN, edge->value);
vlm4808c702004-08-18 04:50:37 +0000404 }
405 }
406 return 0;
407}
408
409static int
vlmeeca98f2004-08-25 02:00:03 +0000410asn1print_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 +0000411 asn1cnst_range_t *range;
412 int as_char = (type==ACT_CT_FROM);
413 int i;
414
vlmeeca98f2004-08-25 02:00:03 +0000415 range = asn1constraint_compute_PER_range(expr_type, ct, type,
416 0, 0, strict_PER_visible);
vlm4808c702004-08-18 04:50:37 +0000417 if(!range) return -1;
418
vlmeeca98f2004-08-25 02:00:03 +0000419 if(range->incompatible
420 || (strict_PER_visible && range->not_PER_visible)) {
421 asn1constraint_range_free(range);
422 return 0;
423 }
424
vlm4808c702004-08-18 04:50:37 +0000425 switch(type) {
426 case ACT_CT_FROM: printf("(FROM("); break;
427 case ACT_CT_SIZE: printf("(SIZE("); break;
428 default: printf("("); break;
429 }
430 for(i = -1; i < range->el_count; i++) {
431 asn1cnst_range_t *r;
432 if(i == -1) {
433 if(range->el_count) continue;
434 r = range;
435 } else {
436 r = range->elements[i];
437 }
438 if(i > 0) {
439 printf(" | ");
440 }
441 asn1print_crange_value(&r->left, as_char);
442 if(r->left.type != r->right.type
443 || r->left.value != r->right.value) {
444 printf("..");
445 asn1print_crange_value(&r->right, as_char);
446 }
447 }
448 if(range->extensible)
449 printf(",...");
450 printf(type==ACT_EL_RANGE?")":"))");
451
452 if(range->empty_constraint)
453 printf(":Empty!");
454
455 asn1constraint_range_free(range);
456 return 0;
457}
458
459static int
460asn1print_constraint_explain(asn1p_expr_type_e expr_type,
vlmeeca98f2004-08-25 02:00:03 +0000461 asn1p_constraint_t *ct, int s_PV) {
vlm4808c702004-08-18 04:50:37 +0000462
vlmeeca98f2004-08-25 02:00:03 +0000463 asn1print_constraint_explain_type(expr_type, ct, ACT_EL_RANGE, s_PV);
vlm4808c702004-08-18 04:50:37 +0000464 printf(" ");
vlmeeca98f2004-08-25 02:00:03 +0000465 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_SIZE, s_PV);
vlm4808c702004-08-18 04:50:37 +0000466 printf(" ");
vlmeeca98f2004-08-25 02:00:03 +0000467 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_FROM, s_PV);
vlm4808c702004-08-18 04:50:37 +0000468
469 return 0;
470}
471
472static int
473asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level) {
vlm00ad2822004-08-20 13:24:28 +0000474 int SEQ_OF = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000475
vlmdb4ee042005-02-15 07:06:05 +0000476 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
vlm60e7ef02004-10-13 09:13:56 +0000477 INDENT("-- #line %d\n", tc->_lineno);
vlmfa67ddc2004-06-03 03:38:44 +0000478 if(tc->Identifier)
479 INDENT("%s", tc->Identifier);
480
481 if(tc->params) {
482 asn1print_params(tc->params, flags);
483 }
484
485 if(tc->meta_type != AMT_VALUE
vlmda4df3f2004-08-25 02:27:47 +0000486 && tc->meta_type != AMT_VALUESET
vlmfa67ddc2004-06-03 03:38:44 +0000487 && tc->expr_type != A1TC_EXTENSIBLE) {
488 if(level) {
vlmdb4ee042005-02-15 07:06:05 +0000489 if(tc->Identifier && !(flags & APF_NOINDENT))
vlmfa67ddc2004-06-03 03:38:44 +0000490 printf("\t");
491 } else {
492 printf(" ::=");
493 }
494 }
495
496 if(tc->tag.tag_class) {
497 printf(" ");
498 asn1print_tag(tc, flags);
499 }
500
501 switch(tc->expr_type) {
502 case A1TC_EXTENSIBLE:
503 if(tc->value) {
504 printf("!");
505 asn1print_value(tc->value, flags);
506 }
507 break;
vlmddd6fd12004-08-22 03:08:51 +0000508 case A1TC_COMPONENTS_OF:
509 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
510 printf(" COMPONENTS OF");
511 break;
vlmdb4ee042005-02-15 07:06:05 +0000512 case A1TC_PARAMETRIZED:
513 flags |= APF_NOINDENT;
vlmfa67ddc2004-06-03 03:38:44 +0000514 case A1TC_REFERENCE:
515 case A1TC_UNIVERVAL:
vlmfa67ddc2004-06-03 03:38:44 +0000516 break;
517 case A1TC_CLASSDEF:
518 printf(" CLASS");
519 break;
520 case A1TC_CLASSFIELD:
521 /* Nothing to print here */
522 break;
vlm00ad2822004-08-20 13:24:28 +0000523 case ASN_CONSTR_SET_OF:
524 case ASN_CONSTR_SEQUENCE_OF:
525 SEQ_OF = 1;
526 if(tc->expr_type == ASN_CONSTR_SET_OF)
527 printf(" SET");
528 else
529 printf(" SEQUENCE");
530 if(tc->constraints) {
531 printf(" ");
532 asn1print_constraint(tc->constraints, flags);
533 }
534 printf(" OF");
535 break;
vlmfa67ddc2004-06-03 03:38:44 +0000536 default:
537 {
538 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
539 printf(" %s", p?p:"<unknown type!>");
540 }
541 break;
542 }
543
544 if(tc->reference) {
vlmdb4ee042005-02-15 07:06:05 +0000545 if(!(flags & APF_NOINDENT))
546 printf(" ");
vlmfa67ddc2004-06-03 03:38:44 +0000547 asn1print_ref(tc->reference, flags);
548 }
549
vlmda4df3f2004-08-25 02:27:47 +0000550 if(tc->meta_type == AMT_VALUESET)
551 printf(" ::=");
552
vlmfa67ddc2004-06-03 03:38:44 +0000553 /*
554 * Display the descendants (children) of the current type.
555 */
vlmda4df3f2004-08-25 02:27:47 +0000556 if(TQ_FIRST(&(tc->members))
557 || (tc->expr_type & ASN_CONSTR_MASK)
558 || tc->meta_type == AMT_VALUESET
559 || tc->meta_type == AMT_OBJECT
560 || tc->meta_type == AMT_OBJECTSET
561 ) {
vlmfa67ddc2004-06-03 03:38:44 +0000562 asn1p_expr_t *se; /* SubExpression */
vlmddd6fd12004-08-22 03:08:51 +0000563 int put_braces = !SEQ_OF; /* Don't need 'em, if SET OF... */
vlmfa67ddc2004-06-03 03:38:44 +0000564
vlmda4df3f2004-08-25 02:27:47 +0000565 if(put_braces) {
vlmdb4ee042005-02-15 07:06:05 +0000566 if(flags & APF_NOINDENT) {
567 printf("{");
568 if(!TQ_FIRST(&tc->members))
569 printf("}");
570 } else {
571 printf(" {");
572 if(TQ_FIRST(&tc->members))
573 printf("\n");
574 else
575 printf(" }");
576 }
vlmda4df3f2004-08-25 02:27:47 +0000577 }
vlmfa67ddc2004-06-03 03:38:44 +0000578
579 TQ_FOR(se, &(tc->members), next) {
580 /*
vlmddd6fd12004-08-22 03:08:51 +0000581 * Print the expression as it were a stand-alone type.
vlmfa67ddc2004-06-03 03:38:44 +0000582 */
vlm60e7ef02004-10-13 09:13:56 +0000583 asn1print_expr(asn, mod, se, flags, level + 1);
vlm093f6752004-09-15 11:44:55 +0000584 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
585 printf(" DEFAULT ");
586 asn1print_value(se->marker.default_value, flags);
587 } else if((se->marker.flags & EM_OPTIONAL)
588 == EM_OPTIONAL) {
vlmd82eb012004-09-10 08:21:27 +0000589 printf(" OPTIONAL");
vlm093f6752004-09-15 11:44:55 +0000590 }
vlmfa67ddc2004-06-03 03:38:44 +0000591 if(TQ_NEXT(se, next)) {
592 printf(",");
vlmdb4ee042005-02-15 07:06:05 +0000593 if(!(flags & APF_NOINDENT))
594 INDENT("\n");
vlmfa67ddc2004-06-03 03:38:44 +0000595 }
596 }
597
vlmda4df3f2004-08-25 02:27:47 +0000598 if(put_braces && TQ_FIRST(&tc->members)) {
vlmdb4ee042005-02-15 07:06:05 +0000599 if(!(flags & APF_NOINDENT))
600 printf("\n");
vlmfa67ddc2004-06-03 03:38:44 +0000601 INDENT("}");
602 }
603 }
604
605 if(tc->with_syntax)
606 asn1print_with_syntax(tc->with_syntax, flags);
607
vlm00ad2822004-08-20 13:24:28 +0000608 if(!SEQ_OF && tc->constraints) {
vlmfa67ddc2004-06-03 03:38:44 +0000609 printf(" ");
610 asn1print_constraint(tc->constraints, flags);
611 }
vlm4808c702004-08-18 04:50:37 +0000612
vlmfa67ddc2004-06-03 03:38:44 +0000613 if(tc->unique) {
614 printf(" UNIQUE");
615 }
616
617 if(tc->meta_type == AMT_VALUE
618 && tc->expr_type != A1TC_EXTENSIBLE) {
vlmdc4d95a2004-09-05 10:38:50 +0000619 if(tc->expr_type == A1TC_UNIVERVAL) {
vlm093f6752004-09-15 11:44:55 +0000620 if(tc->value) {
621 printf("(");
622 asn1print_value(tc->value, flags);
623 printf(")");
624 }
vlmdc4d95a2004-09-05 10:38:50 +0000625 } else {
626 printf(" ::= ");
627 asn1print_value(tc->value, flags);
628 }
vlmfa67ddc2004-06-03 03:38:44 +0000629 }
630
vlmddd6fd12004-08-22 03:08:51 +0000631 /*
vlmdb4ee042005-02-15 07:06:05 +0000632 * The following section exists entirely for debugging.
vlmddd6fd12004-08-22 03:08:51 +0000633 */
vlm00ad2822004-08-20 13:24:28 +0000634 if(flags & APF_DEBUG_CONSTRAINTS
635 && tc->expr_type != A1TC_EXTENSIBLE) {
vlm4808c702004-08-18 04:50:37 +0000636 asn1p_expr_t *top_parent;
637
638 if(tc->combined_constraints) {
639 printf("\n-- Combined constraints: ");
640 asn1print_constraint(tc->combined_constraints, flags);
641 }
642
vlmcbebb812004-09-22 16:05:13 +0000643 top_parent = asn1f_find_terminal_type_ex(asn, tc);
vlm4808c702004-08-18 04:50:37 +0000644 if(top_parent) {
vlmeeca98f2004-08-25 02:00:03 +0000645 printf("\n-- Practical constraints (%s): ",
646 top_parent->Identifier);
647 asn1print_constraint_explain(top_parent->expr_type,
648 tc->combined_constraints, 0);
vlm00ad2822004-08-20 13:24:28 +0000649 printf("\n-- PER-visible constraints (%s): ",
650 top_parent->Identifier);
vlm4808c702004-08-18 04:50:37 +0000651 asn1print_constraint_explain(top_parent->expr_type,
vlmeeca98f2004-08-25 02:00:03 +0000652 tc->combined_constraints, 1);
vlm4808c702004-08-18 04:50:37 +0000653 }
vlm00ad2822004-08-20 13:24:28 +0000654 printf("\n");
vlm4808c702004-08-18 04:50:37 +0000655 }
656
vlmfa67ddc2004-06-03 03:38:44 +0000657 return 0;
658}
vlm4808c702004-08-18 04:50:37 +0000659
vlm60e7ef02004-10-13 09:13:56 +0000660
661static int
662asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
663 asn1p_expr_t *se;
664 int expr_unordered = 0;
vlmceb93192004-10-14 05:11:25 +0000665 int dont_involve_children = 0;
vlm60e7ef02004-10-13 09:13:56 +0000666
667 switch(expr->meta_type) {
668 case AMT_TYPE:
669 case AMT_TYPEREF:
670 break;
671 default:
672 if(expr->expr_type == A1TC_UNIVERVAL)
673 break;
674 return 0;
675 }
676
677 if(!expr->Identifier) return 0;
678
vlm60e7ef02004-10-13 09:13:56 +0000679 if(flags & APF_LINE_COMMENTS)
680 INDENT("<!-- #line %d -->\n", expr->_lineno);
681 INDENT("<!ELEMENT %s", expr->Identifier);
682
683 if(expr->expr_type == A1TC_REFERENCE) {
684 se = asn1f_find_terminal_type_ex(asn, expr);
685 if(!se) {
vlmceb93192004-10-14 05:11:25 +0000686 printf(" (ANY)");
vlm60e7ef02004-10-13 09:13:56 +0000687 return 0;
688 }
689 expr = se;
vlmceb93192004-10-14 05:11:25 +0000690 dont_involve_children = 1;
vlm60e7ef02004-10-13 09:13:56 +0000691 }
692
vlm4bba1f82005-03-10 15:16:56 +0000693 if(expr->expr_type == ASN_CONSTR_CHOICE
694 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
695 || expr->expr_type == ASN_CONSTR_SET_OF
696 || expr->expr_type == ASN_CONSTR_SET
vlmc45eab72005-03-09 20:52:15 +0000697 || expr->expr_type == ASN_BASIC_INTEGER
698 || expr->expr_type == ASN_BASIC_ENUMERATED) {
699 expr_unordered = 1;
700 }
701
vlm60e7ef02004-10-13 09:13:56 +0000702 if(TQ_FIRST(&expr->members)) {
703 int extensible = 0;
704 printf(" (");
705 TQ_FOR(se, &(expr->members), next) {
706 if(se->expr_type == A1TC_EXTENSIBLE) {
707 extensible = 1;
708 continue;
709 } else if(!se->Identifier
710 && se->expr_type == A1TC_REFERENCE) {
711 asn1print_ref(se->reference, flags);
712 } else if(se->Identifier) {
713 printf("%s", se->Identifier);
714 } else {
715 printf("ANY");
716 }
717 if(expr->expr_type != ASN_CONSTR_SET
718 && expr->expr_type != ASN_CONSTR_CHOICE
719 && expr->expr_type != ASN_BASIC_INTEGER
720 && expr->expr_type != ASN_BASIC_ENUMERATED) {
721 if(expr_unordered)
722 printf("*");
723 else if(se->marker.flags)
724 printf("?");
725 }
726 if(TQ_NEXT(se, next)
727 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
728 printf(expr_unordered?"|":", ");
729 }
730 }
731 if(extensible) {
732 printf(expr_unordered?"|":", ");
733 printf("ANY");
734 if(expr->expr_type != ASN_CONSTR_SET
735 && expr->expr_type != ASN_CONSTR_CHOICE
736 && expr->expr_type != ASN_BASIC_INTEGER
737 && expr->expr_type != ASN_BASIC_ENUMERATED)
738 printf("*");
739 }
740
741 printf(")");
742 if(expr->expr_type == ASN_CONSTR_SET)
743 printf("*");
744
vlmceb93192004-10-14 05:11:25 +0000745 } else switch(expr->expr_type) {
746 case ASN_BASIC_BOOLEAN:
vlmc45eab72005-03-09 20:52:15 +0000747 printf(" (true|false)");
vlmceb93192004-10-14 05:11:25 +0000748 break;
749 case ASN_CONSTR_CHOICE:
750 case ASN_CONSTR_SET:
751 case ASN_CONSTR_SET_OF:
752 case ASN_CONSTR_SEQUENCE:
753 case ASN_CONSTR_SEQUENCE_OF:
754 case ASN_BASIC_NULL:
755 case A1TC_UNIVERVAL:
vlm60e7ef02004-10-13 09:13:56 +0000756 printf(" EMPTY");
vlmceb93192004-10-14 05:11:25 +0000757 break;
758 case ASN_TYPE_ANY:
759 printf(" ANY");
760 break;
761 case ASN_BASIC_BIT_STRING:
762 case ASN_BASIC_OCTET_STRING:
763 case ASN_BASIC_OBJECT_IDENTIFIER:
764 case ASN_BASIC_RELATIVE_OID:
vlm40a69bf2004-10-15 06:01:54 +0000765 case ASN_BASIC_INTEGER:
vlmceb93192004-10-14 05:11:25 +0000766 case ASN_BASIC_UTCTime:
767 case ASN_BASIC_GeneralizedTime:
vlmceb93192004-10-14 05:11:25 +0000768 case ASN_STRING_NumericString:
769 case ASN_STRING_PrintableString:
vlm40a69bf2004-10-15 06:01:54 +0000770 printf(" (#PCDATA)");
vlmceb93192004-10-14 05:11:25 +0000771 break;
772 case ASN_STRING_VisibleString:
773 case ASN_STRING_ISO646String:
774 /* Entity references, but not XML elements may be present */
vlm60e7ef02004-10-13 09:13:56 +0000775 printf(" (#PCDATA)");
vlmceb93192004-10-14 05:11:25 +0000776 break;
777 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
778 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
779 default:
780 /*
781 * XML elements are allowed.
782 * For example, a UTF8String may contain "<bel/>".
783 */
784 printf(" ANY");
vlm60e7ef02004-10-13 09:13:56 +0000785 }
786 printf(">\n");
787
788 /*
789 * Display the descendants (children) of the current type.
790 */
vlmceb93192004-10-14 05:11:25 +0000791 if(!dont_involve_children) {
792 TQ_FOR(se, &(expr->members), next) {
793 if(se->expr_type == A1TC_EXTENSIBLE) continue;
794 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
795 }
vlm60e7ef02004-10-13 09:13:56 +0000796 }
797
798 return 0;
799}