blob: ecb91f0951b650a273c701bce4ce8357da0018eb [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) {
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) {
vlm51c3de92005-03-18 03:53:05 +000070 asn1print_oid(strlen(mod->Identifier), mod->module_oid, flags);
vlmfa67ddc2004-06-03 03:38:44 +000071 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
vlm51c3de92005-03-18 03:53:05 +0000119asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags) {
120 size_t accum = prior_len;
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
vlm51c3de92005-03-18 03:53:05 +0000129 if(accum + strlen(arcname ? arcname : "") > 75) {
vlmfa67ddc2004-06-03 03:38:44 +0000130 printf("\n\t");
131 accum = 0;
vlm51c3de92005-03-18 03:53:05 +0000132 } else {
133 accum += printf(" ");
vlm8a09e0f2005-02-25 14:20:30 +0000134 }
vlmfa67ddc2004-06-03 03:38:44 +0000135
vlm8a09e0f2005-02-25 14:20:30 +0000136 if(arcname) {
vlm51c3de92005-03-18 03:53:05 +0000137 accum += printf("%s", arcname);
vlmdc4d95a2004-09-05 10:38:50 +0000138 if(oid->arcs[ac].number >= 0) {
vlm51c3de92005-03-18 03:53:05 +0000139 accum += printf("(%" PRIdASN ")",
140 oid->arcs[ac].number);
vlmdc4d95a2004-09-05 10:38:50 +0000141 }
vlmfa67ddc2004-06-03 03:38:44 +0000142 } else {
vlm51c3de92005-03-18 03:53:05 +0000143 accum += printf("%" PRIdASN, oid->arcs[ac].number);
vlmfa67ddc2004-06-03 03:38:44 +0000144 }
vlmfa67ddc2004-06-03 03:38:44 +0000145 }
146 printf(" }");
147
148 return 0;
149}
150
151static int
vlm4808c702004-08-18 04:50:37 +0000152asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000153 int cc;
154
vlmb42843a2004-06-05 08:17:50 +0000155 (void)flags; /* Unused argument */
156
vlmfa67ddc2004-06-03 03:38:44 +0000157 for(cc = 0; cc < ref->comp_count; cc++) {
158 if(cc) printf(".");
159 printf("%s", ref->components[cc].name);
160 }
161
162 return 0;
163}
164
165static int
vlm4808c702004-08-18 04:50:37 +0000166asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000167 struct asn1p_type_tag_s *tag = &tc->tag;
168
vlmb42843a2004-06-05 08:17:50 +0000169 (void)flags; /* Unused argument */
170
vlmfa67ddc2004-06-03 03:38:44 +0000171 if(tag->tag_class == TC_NOCLASS)
172 return 0;
173
174 printf("[");
175 switch(tag->tag_class) {
176 case TC_NOCLASS:
177 assert(tag->tag_class != TC_NOCLASS);
178 break;
179 case TC_UNIVERSAL: printf("UNIVERSAL "); break;
180 case TC_PRIVATE: printf("PRIVATE "); break;
181 case TC_APPLICATION: printf("APPLICATION "); break;
182 case TC_CONTEXT_SPECIFIC:
183 break;
184 }
vlm47ae1582004-09-24 21:01:43 +0000185 printf("%" PRIdASN "]", tag->tag_value);
vlmfa67ddc2004-06-03 03:38:44 +0000186
187 switch(tag->tag_mode) {
188 case TM_DEFAULT: break;
189 case TM_IMPLICIT: printf(" IMPLICIT"); break;
190 case TM_EXPLICIT: printf(" EXPLICIT"); break;
191 }
192
193 return 0;
194}
195
196static int
vlm4808c702004-08-18 04:50:37 +0000197asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000198
199 if(val == NULL)
200 return 0;
201
202 switch(val->type) {
203 case ATV_NOVALUE:
204 break;
vlm093f6752004-09-15 11:44:55 +0000205 case ATV_NULL:
206 printf("NULL");
207 return 0;
208 case ATV_REAL:
209 printf("%f", val->value.v_double);
210 return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000211 case ATV_INTEGER:
vlm47ae1582004-09-24 21:01:43 +0000212 printf("%" PRIdASN, val->value.v_integer);
vlmfa67ddc2004-06-03 03:38:44 +0000213 return 0;
214 case ATV_MIN: printf("MIN"); return 0;
215 case ATV_MAX: printf("MAX"); return 0;
216 case ATV_FALSE: printf("FALSE"); return 0;
217 case ATV_TRUE: printf("TRUE"); return 0;
vlme1e6ed82005-03-24 14:26:38 +0000218 case ATV_TUPLE:
219 printf("{%d, %d}",
220 (int)(val->value.v_integer >> 4),
221 (int)(val->value.v_integer & 0x0f));
222 return 0;
223 case ATV_QUADRUPLE:
224 printf("{%d, %d, %d, %d}",
225 (int)((val->value.v_integer >> 24) & 0xff),
226 (int)((val->value.v_integer >> 16) & 0xff),
227 (int)((val->value.v_integer >> 8) & 0xff),
228 (int)((val->value.v_integer >> 0) & 0xff)
229 );
230 return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000231 case ATV_STRING:
232 {
233 char *p = val->value.string.buf;
234 putchar('"');
235 if(strchr(p, '"')) {
236 /* Mask quotes */
237 for(; *p; p++) {
238 if(*p == '"')
239 putchar(*p);
240 putchar(*p);
241 }
242 } else {
243 fputs(p, stdout);
244 }
245 putchar('"');
246 }
247 return 0;
248 case ATV_UNPARSED:
249 fputs(val->value.string.buf, stdout);
250 return 0;
251 case ATV_BITVECTOR:
252 {
253 uint8_t *bitvector;
254 int bits;
255 int i;
256
257 bitvector = val->value.binary_vector.bits;
258 bits = val->value.binary_vector.size_in_bits;
259
260 printf("'");
261 if(bits%8) {
262 for(i = 0; i < bits; i++) {
263 uint8_t uc;
264 uc = bitvector[i>>3];
265 putchar(((uc >> (7-(i%8)))&1)?'1':'0');
266 }
267 printf("'B");
268 } else {
269 char hextable[16] = "0123456789ABCDEF";
270 for(i = 0; i < (bits>>3); i++) {
271 putchar(hextable[bitvector[i] >> 4]);
272 putchar(hextable[bitvector[i] & 0x0f]);
273 }
274 printf("'H");
275 }
vlma9e55432005-02-24 21:07:35 +0000276 return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000277 }
vlm093f6752004-09-15 11:44:55 +0000278 case ATV_REFERENCED:
279 return asn1print_ref(val->value.reference, flags);
280 case ATV_CHOICE_IDENTIFIER:
281 printf("%s: ", val->value.choice_identifier.identifier);
282 return asn1print_value(val->value.choice_identifier.value, flags);
vlmfa67ddc2004-06-03 03:38:44 +0000283 }
284
285 assert(val->type || !"Unknown");
286
287 return 0;
288}
289
290static int
vlm4808c702004-08-18 04:50:37 +0000291asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000292 int symno = 0;
293
294 if(ct == 0) return 0;
295
296 if(ct->type == ACT_CA_SET)
297 printf("(");
298
299 switch(ct->type) {
vlma6a12e32005-03-20 12:58:00 +0000300 case ACT_EL_TYPE:
301 asn1print_value(ct->value, flags);
302 break;
vlmfa67ddc2004-06-03 03:38:44 +0000303 case ACT_EL_VALUE:
304 asn1print_value(ct->value, flags);
305 break;
306 case ACT_EL_RANGE:
307 case ACT_EL_LLRANGE:
308 case ACT_EL_RLRANGE:
309 case ACT_EL_ULRANGE:
310 asn1print_value(ct->range_start, flags);
311 switch(ct->type) {
312 case ACT_EL_RANGE: printf(".."); break;
313 case ACT_EL_LLRANGE: printf("<.."); break;
314 case ACT_EL_RLRANGE: printf("..<"); break;
315 case ACT_EL_ULRANGE: printf("<..<"); break;
316 default: printf("?..?"); break;
317 }
318 asn1print_value(ct->range_stop, flags);
319 break;
320 case ACT_EL_EXT:
321 printf("...");
322 break;
323 case ACT_CT_SIZE:
324 case ACT_CT_FROM:
325 switch(ct->type) {
vlm4808c702004-08-18 04:50:37 +0000326 case ACT_CT_SIZE: printf("SIZE("); break;
327 case ACT_CT_FROM: printf("FROM("); break;
vlmfa67ddc2004-06-03 03:38:44 +0000328 default: printf("??? ("); break;
329 }
330 assert(ct->el_count != 0);
331 assert(ct->el_count == 1);
332 asn1print_constraint(ct->elements[0], flags);
333 printf(")");
334 break;
335 case ACT_CT_WCOMP:
336 case ACT_CT_WCOMPS:
337 printf("???");
vlm6611add2005-03-20 14:28:32 +0000338 case ACT_CT_CTDBY:
339 printf("CONSTRAINED BY ");
340 assert(ct->value->type == ATV_UNPARSED);
341 fwrite(ct->value->value.string.buf,
342 1, ct->value->value.string.size, stdout);
vlmfa67ddc2004-06-03 03:38:44 +0000343 break;
344 case ACT_CA_SET: symno++;
345 case ACT_CA_CRC: symno++;
346 case ACT_CA_CSV: symno++;
347 case ACT_CA_UNI: symno++;
348 case ACT_CA_INT: symno++;
349 case ACT_CA_EXC:
350 {
vlm4808c702004-08-18 04:50:37 +0000351 char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",",
vlmfa67ddc2004-06-03 03:38:44 +0000352 "", "(" };
vlmdc4d95a2004-09-05 10:38:50 +0000353 unsigned int i;
vlmfa67ddc2004-06-03 03:38:44 +0000354 for(i = 0; i < ct->el_count; i++) {
vlmfa67ddc2004-06-03 03:38:44 +0000355 if(i) fputs(symtable[symno], stdout);
356 if(ct->type == ACT_CA_CRC) fputs("{", stdout);
vlme1e6ed82005-03-24 14:26:38 +0000357 asn1print_constraint(ct->elements[i], flags);
vlmfa67ddc2004-06-03 03:38:44 +0000358 if(ct->type == ACT_CA_CRC) fputs("}", stdout);
359 if(i+1 < ct->el_count
360 && ct->type == ACT_CA_SET)
361 fputs(")", stdout);
362 }
363 }
364 break;
vlme1e6ed82005-03-24 14:26:38 +0000365 case ACT_CA_AEX:
366 assert(ct->el_count == 1);
367 printf("ALL EXCEPT ");
368 asn1print_constraint(ct->elements[0], flags);
369 break;
vlmfa67ddc2004-06-03 03:38:44 +0000370 case ACT_INVALID:
371 assert(ct->type != ACT_INVALID);
372 break;
373 }
374
375 if(ct->type == ACT_CA_SET)
376 printf(")");
377
378 return 0;
379}
380
381static int
vlm4808c702004-08-18 04:50:37 +0000382asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000383 if(pl) {
384 int i;
385 printf("{");
386 for(i = 0; i < pl->params_count; i++) {
387 if(i) printf(", ");
388 if(pl->params[i].governor) {
389 asn1print_ref(pl->params[i].governor, flags);
390 printf(":");
391 }
392 printf("%s", pl->params[i].argument);
393 }
394 printf("}");
395 }
396
397 return 0;
398}
399
400static int
vlm4808c702004-08-18 04:50:37 +0000401asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000402 if(wx) {
403 asn1p_wsyntx_chunk_t *wc;
404 printf(" WITH SYNTAX {");
405 TQ_FOR(wc, &(wx->chunks), next) {
406 if(wc->ref) {
407 asn1print_ref(wc->ref, flags);
408 } else {
409 fwrite(wc->buf, 1, wc->len, stdout);
410 }
411 }
412 printf("}\n");
413 }
414
415 return 0;
416}
417
418static int
vlm4808c702004-08-18 04:50:37 +0000419asn1print_crange_value(asn1cnst_edge_t *edge, int as_char) {
420 switch(edge->type) {
421 case ARE_MIN: printf("MIN"); break;
422 case ARE_MAX: printf("MAX"); break;
423 case ARE_VALUE:
424 if(as_char) {
425 printf("\"%c\"", (unsigned char)edge->value);
426 } else {
vlm47ae1582004-09-24 21:01:43 +0000427 printf("%" PRIdASN, edge->value);
vlm4808c702004-08-18 04:50:37 +0000428 }
429 }
430 return 0;
431}
432
433static int
vlmeeca98f2004-08-25 02:00:03 +0000434asn1print_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 +0000435 asn1cnst_range_t *range;
436 int as_char = (type==ACT_CT_FROM);
437 int i;
438
vlmeeca98f2004-08-25 02:00:03 +0000439 range = asn1constraint_compute_PER_range(expr_type, ct, type,
440 0, 0, strict_PER_visible);
vlm4808c702004-08-18 04:50:37 +0000441 if(!range) return -1;
442
vlmeeca98f2004-08-25 02:00:03 +0000443 if(range->incompatible
444 || (strict_PER_visible && range->not_PER_visible)) {
445 asn1constraint_range_free(range);
446 return 0;
447 }
448
vlm4808c702004-08-18 04:50:37 +0000449 switch(type) {
450 case ACT_CT_FROM: printf("(FROM("); break;
451 case ACT_CT_SIZE: printf("(SIZE("); break;
452 default: printf("("); break;
453 }
454 for(i = -1; i < range->el_count; i++) {
455 asn1cnst_range_t *r;
456 if(i == -1) {
457 if(range->el_count) continue;
458 r = range;
459 } else {
460 r = range->elements[i];
461 }
462 if(i > 0) {
463 printf(" | ");
464 }
465 asn1print_crange_value(&r->left, as_char);
466 if(r->left.type != r->right.type
467 || r->left.value != r->right.value) {
468 printf("..");
469 asn1print_crange_value(&r->right, as_char);
470 }
471 }
472 if(range->extensible)
473 printf(",...");
474 printf(type==ACT_EL_RANGE?")":"))");
475
476 if(range->empty_constraint)
477 printf(":Empty!");
478
479 asn1constraint_range_free(range);
480 return 0;
481}
482
483static int
484asn1print_constraint_explain(asn1p_expr_type_e expr_type,
vlmeeca98f2004-08-25 02:00:03 +0000485 asn1p_constraint_t *ct, int s_PV) {
vlm4808c702004-08-18 04:50:37 +0000486
vlmeeca98f2004-08-25 02:00:03 +0000487 asn1print_constraint_explain_type(expr_type, ct, ACT_EL_RANGE, s_PV);
vlm4808c702004-08-18 04:50:37 +0000488 printf(" ");
vlmeeca98f2004-08-25 02:00:03 +0000489 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_SIZE, s_PV);
vlm4808c702004-08-18 04:50:37 +0000490 printf(" ");
vlmeeca98f2004-08-25 02:00:03 +0000491 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_FROM, s_PV);
vlm4808c702004-08-18 04:50:37 +0000492
493 return 0;
494}
495
496static int
497asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level) {
vlm00ad2822004-08-20 13:24:28 +0000498 int SEQ_OF = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000499
vlmdb4ee042005-02-15 07:06:05 +0000500 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
vlm60e7ef02004-10-13 09:13:56 +0000501 INDENT("-- #line %d\n", tc->_lineno);
vlmfa67ddc2004-06-03 03:38:44 +0000502 if(tc->Identifier)
503 INDENT("%s", tc->Identifier);
504
505 if(tc->params) {
506 asn1print_params(tc->params, flags);
507 }
508
509 if(tc->meta_type != AMT_VALUE
vlmda4df3f2004-08-25 02:27:47 +0000510 && tc->meta_type != AMT_VALUESET
vlmfa67ddc2004-06-03 03:38:44 +0000511 && tc->expr_type != A1TC_EXTENSIBLE) {
512 if(level) {
vlmdb4ee042005-02-15 07:06:05 +0000513 if(tc->Identifier && !(flags & APF_NOINDENT))
vlmfa67ddc2004-06-03 03:38:44 +0000514 printf("\t");
515 } else {
516 printf(" ::=");
517 }
518 }
519
520 if(tc->tag.tag_class) {
521 printf(" ");
522 asn1print_tag(tc, flags);
523 }
524
525 switch(tc->expr_type) {
526 case A1TC_EXTENSIBLE:
527 if(tc->value) {
528 printf("!");
529 asn1print_value(tc->value, flags);
530 }
531 break;
vlmddd6fd12004-08-22 03:08:51 +0000532 case A1TC_COMPONENTS_OF:
533 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
534 printf(" COMPONENTS OF");
535 break;
vlmdb4ee042005-02-15 07:06:05 +0000536 case A1TC_PARAMETRIZED:
537 flags |= APF_NOINDENT;
vlmfa67ddc2004-06-03 03:38:44 +0000538 case A1TC_REFERENCE:
539 case A1TC_UNIVERVAL:
vlmfa67ddc2004-06-03 03:38:44 +0000540 break;
541 case A1TC_CLASSDEF:
542 printf(" CLASS");
543 break;
544 case A1TC_CLASSFIELD:
545 /* Nothing to print here */
546 break;
vlm00ad2822004-08-20 13:24:28 +0000547 case ASN_CONSTR_SET_OF:
548 case ASN_CONSTR_SEQUENCE_OF:
549 SEQ_OF = 1;
550 if(tc->expr_type == ASN_CONSTR_SET_OF)
551 printf(" SET");
552 else
553 printf(" SEQUENCE");
554 if(tc->constraints) {
555 printf(" ");
556 asn1print_constraint(tc->constraints, flags);
557 }
558 printf(" OF");
559 break;
vlmfa67ddc2004-06-03 03:38:44 +0000560 default:
561 {
562 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
563 printf(" %s", p?p:"<unknown type!>");
564 }
565 break;
566 }
567
568 if(tc->reference) {
vlmdb4ee042005-02-15 07:06:05 +0000569 if(!(flags & APF_NOINDENT))
570 printf(" ");
vlmfa67ddc2004-06-03 03:38:44 +0000571 asn1print_ref(tc->reference, flags);
572 }
573
vlmda4df3f2004-08-25 02:27:47 +0000574 if(tc->meta_type == AMT_VALUESET)
575 printf(" ::=");
576
vlmfa67ddc2004-06-03 03:38:44 +0000577 /*
578 * Display the descendants (children) of the current type.
579 */
vlmda4df3f2004-08-25 02:27:47 +0000580 if(TQ_FIRST(&(tc->members))
581 || (tc->expr_type & ASN_CONSTR_MASK)
582 || tc->meta_type == AMT_VALUESET
583 || tc->meta_type == AMT_OBJECT
584 || tc->meta_type == AMT_OBJECTSET
585 ) {
vlmfa67ddc2004-06-03 03:38:44 +0000586 asn1p_expr_t *se; /* SubExpression */
vlmddd6fd12004-08-22 03:08:51 +0000587 int put_braces = !SEQ_OF; /* Don't need 'em, if SET OF... */
vlmfa67ddc2004-06-03 03:38:44 +0000588
vlmda4df3f2004-08-25 02:27:47 +0000589 if(put_braces) {
vlmdb4ee042005-02-15 07:06:05 +0000590 if(flags & APF_NOINDENT) {
591 printf("{");
592 if(!TQ_FIRST(&tc->members))
593 printf("}");
594 } else {
595 printf(" {");
596 if(TQ_FIRST(&tc->members))
597 printf("\n");
598 else
599 printf(" }");
600 }
vlmda4df3f2004-08-25 02:27:47 +0000601 }
vlmfa67ddc2004-06-03 03:38:44 +0000602
603 TQ_FOR(se, &(tc->members), next) {
604 /*
vlmddd6fd12004-08-22 03:08:51 +0000605 * Print the expression as it were a stand-alone type.
vlmfa67ddc2004-06-03 03:38:44 +0000606 */
vlm60e7ef02004-10-13 09:13:56 +0000607 asn1print_expr(asn, mod, se, flags, level + 1);
vlm093f6752004-09-15 11:44:55 +0000608 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
609 printf(" DEFAULT ");
610 asn1print_value(se->marker.default_value, flags);
611 } else if((se->marker.flags & EM_OPTIONAL)
612 == EM_OPTIONAL) {
vlmd82eb012004-09-10 08:21:27 +0000613 printf(" OPTIONAL");
vlm093f6752004-09-15 11:44:55 +0000614 }
vlmfa67ddc2004-06-03 03:38:44 +0000615 if(TQ_NEXT(se, next)) {
616 printf(",");
vlmdb4ee042005-02-15 07:06:05 +0000617 if(!(flags & APF_NOINDENT))
618 INDENT("\n");
vlmfa67ddc2004-06-03 03:38:44 +0000619 }
620 }
621
vlmda4df3f2004-08-25 02:27:47 +0000622 if(put_braces && TQ_FIRST(&tc->members)) {
vlmdb4ee042005-02-15 07:06:05 +0000623 if(!(flags & APF_NOINDENT))
624 printf("\n");
vlmfa67ddc2004-06-03 03:38:44 +0000625 INDENT("}");
626 }
627 }
628
629 if(tc->with_syntax)
630 asn1print_with_syntax(tc->with_syntax, flags);
631
vlm00ad2822004-08-20 13:24:28 +0000632 if(!SEQ_OF && tc->constraints) {
vlmfa67ddc2004-06-03 03:38:44 +0000633 printf(" ");
634 asn1print_constraint(tc->constraints, flags);
635 }
vlm4808c702004-08-18 04:50:37 +0000636
vlmfa67ddc2004-06-03 03:38:44 +0000637 if(tc->unique) {
638 printf(" UNIQUE");
639 }
640
641 if(tc->meta_type == AMT_VALUE
642 && tc->expr_type != A1TC_EXTENSIBLE) {
vlmdc4d95a2004-09-05 10:38:50 +0000643 if(tc->expr_type == A1TC_UNIVERVAL) {
vlm093f6752004-09-15 11:44:55 +0000644 if(tc->value) {
645 printf("(");
646 asn1print_value(tc->value, flags);
647 printf(")");
648 }
vlmdc4d95a2004-09-05 10:38:50 +0000649 } else {
650 printf(" ::= ");
651 asn1print_value(tc->value, flags);
652 }
vlmfa67ddc2004-06-03 03:38:44 +0000653 }
654
vlmddd6fd12004-08-22 03:08:51 +0000655 /*
vlmdb4ee042005-02-15 07:06:05 +0000656 * The following section exists entirely for debugging.
vlmddd6fd12004-08-22 03:08:51 +0000657 */
vlm00ad2822004-08-20 13:24:28 +0000658 if(flags & APF_DEBUG_CONSTRAINTS
659 && tc->expr_type != A1TC_EXTENSIBLE) {
vlm4808c702004-08-18 04:50:37 +0000660 asn1p_expr_t *top_parent;
661
662 if(tc->combined_constraints) {
663 printf("\n-- Combined constraints: ");
664 asn1print_constraint(tc->combined_constraints, flags);
665 }
666
vlmcbebb812004-09-22 16:05:13 +0000667 top_parent = asn1f_find_terminal_type_ex(asn, tc);
vlm4808c702004-08-18 04:50:37 +0000668 if(top_parent) {
vlmeeca98f2004-08-25 02:00:03 +0000669 printf("\n-- Practical constraints (%s): ",
670 top_parent->Identifier);
671 asn1print_constraint_explain(top_parent->expr_type,
672 tc->combined_constraints, 0);
vlm00ad2822004-08-20 13:24:28 +0000673 printf("\n-- PER-visible constraints (%s): ",
674 top_parent->Identifier);
vlm4808c702004-08-18 04:50:37 +0000675 asn1print_constraint_explain(top_parent->expr_type,
vlmeeca98f2004-08-25 02:00:03 +0000676 tc->combined_constraints, 1);
vlm4808c702004-08-18 04:50:37 +0000677 }
vlm00ad2822004-08-20 13:24:28 +0000678 printf("\n");
vlm4808c702004-08-18 04:50:37 +0000679 }
680
vlmfa67ddc2004-06-03 03:38:44 +0000681 return 0;
682}
vlm4808c702004-08-18 04:50:37 +0000683
vlm60e7ef02004-10-13 09:13:56 +0000684
685static int
686asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
687 asn1p_expr_t *se;
688 int expr_unordered = 0;
vlmceb93192004-10-14 05:11:25 +0000689 int dont_involve_children = 0;
vlm60e7ef02004-10-13 09:13:56 +0000690
691 switch(expr->meta_type) {
692 case AMT_TYPE:
693 case AMT_TYPEREF:
694 break;
695 default:
696 if(expr->expr_type == A1TC_UNIVERVAL)
697 break;
698 return 0;
699 }
700
701 if(!expr->Identifier) return 0;
702
vlm60e7ef02004-10-13 09:13:56 +0000703 if(flags & APF_LINE_COMMENTS)
704 INDENT("<!-- #line %d -->\n", expr->_lineno);
705 INDENT("<!ELEMENT %s", expr->Identifier);
706
707 if(expr->expr_type == A1TC_REFERENCE) {
708 se = asn1f_find_terminal_type_ex(asn, expr);
709 if(!se) {
vlmceb93192004-10-14 05:11:25 +0000710 printf(" (ANY)");
vlm60e7ef02004-10-13 09:13:56 +0000711 return 0;
712 }
713 expr = se;
vlmceb93192004-10-14 05:11:25 +0000714 dont_involve_children = 1;
vlm60e7ef02004-10-13 09:13:56 +0000715 }
716
vlm4bba1f82005-03-10 15:16:56 +0000717 if(expr->expr_type == ASN_CONSTR_CHOICE
718 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
719 || expr->expr_type == ASN_CONSTR_SET_OF
720 || expr->expr_type == ASN_CONSTR_SET
vlmc45eab72005-03-09 20:52:15 +0000721 || expr->expr_type == ASN_BASIC_INTEGER
722 || expr->expr_type == ASN_BASIC_ENUMERATED) {
723 expr_unordered = 1;
724 }
725
vlm60e7ef02004-10-13 09:13:56 +0000726 if(TQ_FIRST(&expr->members)) {
727 int extensible = 0;
728 printf(" (");
729 TQ_FOR(se, &(expr->members), next) {
730 if(se->expr_type == A1TC_EXTENSIBLE) {
731 extensible = 1;
732 continue;
733 } else if(!se->Identifier
734 && se->expr_type == A1TC_REFERENCE) {
735 asn1print_ref(se->reference, flags);
736 } else if(se->Identifier) {
737 printf("%s", se->Identifier);
738 } else {
739 printf("ANY");
740 }
741 if(expr->expr_type != ASN_CONSTR_SET
742 && expr->expr_type != ASN_CONSTR_CHOICE
743 && expr->expr_type != ASN_BASIC_INTEGER
744 && expr->expr_type != ASN_BASIC_ENUMERATED) {
745 if(expr_unordered)
746 printf("*");
747 else if(se->marker.flags)
748 printf("?");
749 }
750 if(TQ_NEXT(se, next)
751 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
752 printf(expr_unordered?"|":", ");
753 }
754 }
755 if(extensible) {
756 printf(expr_unordered?"|":", ");
757 printf("ANY");
758 if(expr->expr_type != ASN_CONSTR_SET
759 && expr->expr_type != ASN_CONSTR_CHOICE
760 && expr->expr_type != ASN_BASIC_INTEGER
761 && expr->expr_type != ASN_BASIC_ENUMERATED)
762 printf("*");
763 }
764
765 printf(")");
766 if(expr->expr_type == ASN_CONSTR_SET)
767 printf("*");
768
vlmceb93192004-10-14 05:11:25 +0000769 } else switch(expr->expr_type) {
770 case ASN_BASIC_BOOLEAN:
vlmc45eab72005-03-09 20:52:15 +0000771 printf(" (true|false)");
vlmceb93192004-10-14 05:11:25 +0000772 break;
773 case ASN_CONSTR_CHOICE:
774 case ASN_CONSTR_SET:
775 case ASN_CONSTR_SET_OF:
776 case ASN_CONSTR_SEQUENCE:
777 case ASN_CONSTR_SEQUENCE_OF:
778 case ASN_BASIC_NULL:
779 case A1TC_UNIVERVAL:
vlm60e7ef02004-10-13 09:13:56 +0000780 printf(" EMPTY");
vlmceb93192004-10-14 05:11:25 +0000781 break;
782 case ASN_TYPE_ANY:
783 printf(" ANY");
784 break;
785 case ASN_BASIC_BIT_STRING:
786 case ASN_BASIC_OCTET_STRING:
787 case ASN_BASIC_OBJECT_IDENTIFIER:
788 case ASN_BASIC_RELATIVE_OID:
vlm40a69bf2004-10-15 06:01:54 +0000789 case ASN_BASIC_INTEGER:
vlmceb93192004-10-14 05:11:25 +0000790 case ASN_BASIC_UTCTime:
791 case ASN_BASIC_GeneralizedTime:
vlmceb93192004-10-14 05:11:25 +0000792 case ASN_STRING_NumericString:
793 case ASN_STRING_PrintableString:
vlm40a69bf2004-10-15 06:01:54 +0000794 printf(" (#PCDATA)");
vlmceb93192004-10-14 05:11:25 +0000795 break;
796 case ASN_STRING_VisibleString:
797 case ASN_STRING_ISO646String:
798 /* Entity references, but not XML elements may be present */
vlm60e7ef02004-10-13 09:13:56 +0000799 printf(" (#PCDATA)");
vlmceb93192004-10-14 05:11:25 +0000800 break;
801 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
802 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
803 default:
804 /*
805 * XML elements are allowed.
806 * For example, a UTF8String may contain "<bel/>".
807 */
808 printf(" ANY");
vlm60e7ef02004-10-13 09:13:56 +0000809 }
810 printf(">\n");
811
812 /*
813 * Display the descendants (children) of the current type.
814 */
vlmceb93192004-10-14 05:11:25 +0000815 if(!dont_involve_children) {
816 TQ_FOR(se, &(expr->members), next) {
817 if(se->expr_type == A1TC_EXTENSIBLE) continue;
818 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
819 }
vlm60e7ef02004-10-13 09:13:56 +0000820 }
821
822 return 0;
823}