blob: 86175f1d3765783d5956c012ebf82912977f8759 [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
vlm931aeed2005-08-12 10:09:10 +000068 printf("%s ", mod->ModuleName);
vlmfa67ddc2004-06-03 03:38:44 +000069 if(mod->module_oid) {
vlm931aeed2005-08-12 10:09:10 +000070 asn1print_oid(strlen(mod->ModuleName), 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
vlm398105f2005-03-28 17:52:43 +0000129 if(accum + strlen(arcname ? arcname : "") > 72) {
vlmfa67ddc2004-06-03 03:38:44 +0000130 printf("\n\t");
vlm398105f2005-03-28 17:52:43 +0000131 accum = 8;
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
vlm5b103032005-06-02 05:21:53 +0000171 printf("%s", asn1p_tag2string(tag, 0));
vlmfa67ddc2004-06-03 03:38:44 +0000172
173 return 0;
174}
175
176static int
vlm4808c702004-08-18 04:50:37 +0000177asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000178
179 if(val == NULL)
180 return 0;
181
182 switch(val->type) {
183 case ATV_NOVALUE:
184 break;
vlm093f6752004-09-15 11:44:55 +0000185 case ATV_NULL:
186 printf("NULL");
187 return 0;
188 case ATV_REAL:
189 printf("%f", val->value.v_double);
190 return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000191 case ATV_INTEGER:
vlm47ae1582004-09-24 21:01:43 +0000192 printf("%" PRIdASN, val->value.v_integer);
vlmfa67ddc2004-06-03 03:38:44 +0000193 return 0;
194 case ATV_MIN: printf("MIN"); return 0;
195 case ATV_MAX: printf("MAX"); return 0;
196 case ATV_FALSE: printf("FALSE"); return 0;
197 case ATV_TRUE: printf("TRUE"); return 0;
vlme1e6ed82005-03-24 14:26:38 +0000198 case ATV_TUPLE:
199 printf("{%d, %d}",
200 (int)(val->value.v_integer >> 4),
201 (int)(val->value.v_integer & 0x0f));
202 return 0;
203 case ATV_QUADRUPLE:
204 printf("{%d, %d, %d, %d}",
205 (int)((val->value.v_integer >> 24) & 0xff),
206 (int)((val->value.v_integer >> 16) & 0xff),
207 (int)((val->value.v_integer >> 8) & 0xff),
208 (int)((val->value.v_integer >> 0) & 0xff)
209 );
210 return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000211 case ATV_STRING:
212 {
vlm55289c12005-06-15 18:41:50 +0000213 char *p = (char *)val->value.string.buf;
vlmfa67ddc2004-06-03 03:38:44 +0000214 putchar('"');
215 if(strchr(p, '"')) {
216 /* Mask quotes */
217 for(; *p; p++) {
218 if(*p == '"')
219 putchar(*p);
220 putchar(*p);
221 }
222 } else {
223 fputs(p, stdout);
224 }
225 putchar('"');
226 }
227 return 0;
228 case ATV_UNPARSED:
vlm55289c12005-06-15 18:41:50 +0000229 fputs((char *)val->value.string.buf, stdout);
vlmfa67ddc2004-06-03 03:38:44 +0000230 return 0;
231 case ATV_BITVECTOR:
232 {
233 uint8_t *bitvector;
234 int bits;
235 int i;
236
237 bitvector = val->value.binary_vector.bits;
238 bits = val->value.binary_vector.size_in_bits;
239
240 printf("'");
241 if(bits%8) {
242 for(i = 0; i < bits; i++) {
243 uint8_t uc;
244 uc = bitvector[i>>3];
245 putchar(((uc >> (7-(i%8)))&1)?'1':'0');
246 }
247 printf("'B");
248 } else {
249 char hextable[16] = "0123456789ABCDEF";
250 for(i = 0; i < (bits>>3); i++) {
251 putchar(hextable[bitvector[i] >> 4]);
252 putchar(hextable[bitvector[i] & 0x0f]);
253 }
254 printf("'H");
255 }
vlma9e55432005-02-24 21:07:35 +0000256 return 0;
vlmfa67ddc2004-06-03 03:38:44 +0000257 }
vlm093f6752004-09-15 11:44:55 +0000258 case ATV_REFERENCED:
259 return asn1print_ref(val->value.reference, flags);
260 case ATV_CHOICE_IDENTIFIER:
261 printf("%s: ", val->value.choice_identifier.identifier);
262 return asn1print_value(val->value.choice_identifier.value, flags);
vlmfa67ddc2004-06-03 03:38:44 +0000263 }
264
265 assert(val->type || !"Unknown");
266
267 return 0;
268}
269
270static int
vlm4808c702004-08-18 04:50:37 +0000271asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000272 int symno = 0;
273
274 if(ct == 0) return 0;
275
276 if(ct->type == ACT_CA_SET)
277 printf("(");
278
279 switch(ct->type) {
vlma6a12e32005-03-20 12:58:00 +0000280 case ACT_EL_TYPE:
281 asn1print_value(ct->value, flags);
282 break;
vlmfa67ddc2004-06-03 03:38:44 +0000283 case ACT_EL_VALUE:
284 asn1print_value(ct->value, flags);
285 break;
286 case ACT_EL_RANGE:
287 case ACT_EL_LLRANGE:
288 case ACT_EL_RLRANGE:
289 case ACT_EL_ULRANGE:
290 asn1print_value(ct->range_start, flags);
291 switch(ct->type) {
292 case ACT_EL_RANGE: printf(".."); break;
293 case ACT_EL_LLRANGE: printf("<.."); break;
294 case ACT_EL_RLRANGE: printf("..<"); break;
295 case ACT_EL_ULRANGE: printf("<..<"); break;
296 default: printf("?..?"); break;
297 }
298 asn1print_value(ct->range_stop, flags);
299 break;
300 case ACT_EL_EXT:
301 printf("...");
302 break;
303 case ACT_CT_SIZE:
304 case ACT_CT_FROM:
305 switch(ct->type) {
vlm4808c702004-08-18 04:50:37 +0000306 case ACT_CT_SIZE: printf("SIZE("); break;
307 case ACT_CT_FROM: printf("FROM("); break;
vlmfa67ddc2004-06-03 03:38:44 +0000308 default: printf("??? ("); break;
309 }
310 assert(ct->el_count != 0);
311 assert(ct->el_count == 1);
312 asn1print_constraint(ct->elements[0], flags);
313 printf(")");
314 break;
315 case ACT_CT_WCOMP:
vlm7bbdc9f2005-03-28 15:01:27 +0000316 assert(ct->el_count != 0);
317 assert(ct->el_count == 1);
318 printf("WITH COMPONENT (");
319 asn1print_constraint(ct->elements[0], flags);
320 printf(")");
vlm3b948532005-03-28 07:50:06 +0000321 break;
vlm7bbdc9f2005-03-28 15:01:27 +0000322 case ACT_CT_WCOMPS: {
323 unsigned int i;
324 printf("WITH COMPONENTS { ");
325 for(i = 0; i < ct->el_count; i++) {
326 asn1p_constraint_t *cel = ct->elements[i];
327 if(i) printf(", ");
328 fwrite(cel->value->value.string.buf,
329 1, cel->value->value.string.size,
330 stdout);
331 if(cel->el_count) {
332 assert(cel->el_count == 1);
333 printf(" ");
334 asn1print_constraint(cel->elements[0],
335 flags);
336 }
337 switch(cel->presence) {
338 case ACPRES_DEFAULT: break;
339 case ACPRES_PRESENT: printf(" PRESENT"); break;
340 case ACPRES_ABSENT: printf(" ABSENT"); break;
341 case ACPRES_OPTIONAL: printf(" OPTIONAL");break;
342 }
343 }
344 printf(" }");
345 }
vlm3b948532005-03-28 07:50:06 +0000346 break;
vlm6611add2005-03-20 14:28:32 +0000347 case ACT_CT_CTDBY:
348 printf("CONSTRAINED BY ");
349 assert(ct->value->type == ATV_UNPARSED);
350 fwrite(ct->value->value.string.buf,
351 1, ct->value->value.string.size, stdout);
vlmfa67ddc2004-06-03 03:38:44 +0000352 break;
353 case ACT_CA_SET: symno++;
354 case ACT_CA_CRC: symno++;
355 case ACT_CA_CSV: symno++;
356 case ACT_CA_UNI: symno++;
357 case ACT_CA_INT: symno++;
358 case ACT_CA_EXC:
359 {
vlm4808c702004-08-18 04:50:37 +0000360 char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",",
vlmfa67ddc2004-06-03 03:38:44 +0000361 "", "(" };
vlmdc4d95a2004-09-05 10:38:50 +0000362 unsigned int i;
vlmfa67ddc2004-06-03 03:38:44 +0000363 for(i = 0; i < ct->el_count; i++) {
vlmfa67ddc2004-06-03 03:38:44 +0000364 if(i) fputs(symtable[symno], stdout);
365 if(ct->type == ACT_CA_CRC) fputs("{", stdout);
vlme1e6ed82005-03-24 14:26:38 +0000366 asn1print_constraint(ct->elements[i], flags);
vlmfa67ddc2004-06-03 03:38:44 +0000367 if(ct->type == ACT_CA_CRC) fputs("}", stdout);
368 if(i+1 < ct->el_count
369 && ct->type == ACT_CA_SET)
370 fputs(")", stdout);
371 }
372 }
373 break;
vlme1e6ed82005-03-24 14:26:38 +0000374 case ACT_CA_AEX:
375 assert(ct->el_count == 1);
376 printf("ALL EXCEPT ");
377 asn1print_constraint(ct->elements[0], flags);
378 break;
vlmfa67ddc2004-06-03 03:38:44 +0000379 case ACT_INVALID:
380 assert(ct->type != ACT_INVALID);
381 break;
382 }
383
384 if(ct->type == ACT_CA_SET)
385 printf(")");
386
387 return 0;
388}
389
390static int
vlm4808c702004-08-18 04:50:37 +0000391asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000392 if(pl) {
393 int i;
394 printf("{");
395 for(i = 0; i < pl->params_count; i++) {
396 if(i) printf(", ");
397 if(pl->params[i].governor) {
398 asn1print_ref(pl->params[i].governor, flags);
399 printf(":");
400 }
401 printf("%s", pl->params[i].argument);
402 }
403 printf("}");
404 }
405
406 return 0;
407}
408
409static int
vlm4808c702004-08-18 04:50:37 +0000410asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
vlmfa67ddc2004-06-03 03:38:44 +0000411 if(wx) {
412 asn1p_wsyntx_chunk_t *wc;
413 printf(" WITH SYNTAX {");
414 TQ_FOR(wc, &(wx->chunks), next) {
415 if(wc->ref) {
416 asn1print_ref(wc->ref, flags);
417 } else {
418 fwrite(wc->buf, 1, wc->len, stdout);
419 }
420 }
421 printf("}\n");
422 }
423
424 return 0;
425}
426
427static int
vlm4808c702004-08-18 04:50:37 +0000428asn1print_crange_value(asn1cnst_edge_t *edge, int as_char) {
429 switch(edge->type) {
430 case ARE_MIN: printf("MIN"); break;
431 case ARE_MAX: printf("MAX"); break;
432 case ARE_VALUE:
433 if(as_char) {
434 printf("\"%c\"", (unsigned char)edge->value);
435 } else {
vlm47ae1582004-09-24 21:01:43 +0000436 printf("%" PRIdASN, edge->value);
vlm4808c702004-08-18 04:50:37 +0000437 }
438 }
439 return 0;
440}
441
442static int
vlmeeca98f2004-08-25 02:00:03 +0000443asn1print_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 +0000444 asn1cnst_range_t *range;
445 int as_char = (type==ACT_CT_FROM);
446 int i;
447
vlm27028582005-08-14 14:45:44 +0000448 range = asn1constraint_compute_PER_range(expr_type, ct, type, 0, 0,
449 strict_PER_visible ? CPR_strict_PER_visibility : 0);
vlm4808c702004-08-18 04:50:37 +0000450 if(!range) return -1;
451
vlmeeca98f2004-08-25 02:00:03 +0000452 if(range->incompatible
453 || (strict_PER_visible && range->not_PER_visible)) {
454 asn1constraint_range_free(range);
455 return 0;
456 }
457
vlm4808c702004-08-18 04:50:37 +0000458 switch(type) {
459 case ACT_CT_FROM: printf("(FROM("); break;
460 case ACT_CT_SIZE: printf("(SIZE("); break;
461 default: printf("("); break;
462 }
463 for(i = -1; i < range->el_count; i++) {
464 asn1cnst_range_t *r;
465 if(i == -1) {
466 if(range->el_count) continue;
467 r = range;
468 } else {
469 r = range->elements[i];
470 }
471 if(i > 0) {
472 printf(" | ");
473 }
474 asn1print_crange_value(&r->left, as_char);
475 if(r->left.type != r->right.type
476 || r->left.value != r->right.value) {
477 printf("..");
478 asn1print_crange_value(&r->right, as_char);
479 }
480 }
481 if(range->extensible)
482 printf(",...");
483 printf(type==ACT_EL_RANGE?")":"))");
484
485 if(range->empty_constraint)
486 printf(":Empty!");
487
488 asn1constraint_range_free(range);
489 return 0;
490}
491
492static int
493asn1print_constraint_explain(asn1p_expr_type_e expr_type,
vlmeeca98f2004-08-25 02:00:03 +0000494 asn1p_constraint_t *ct, int s_PV) {
vlm4808c702004-08-18 04:50:37 +0000495
vlmeeca98f2004-08-25 02:00:03 +0000496 asn1print_constraint_explain_type(expr_type, ct, ACT_EL_RANGE, s_PV);
vlm4808c702004-08-18 04:50:37 +0000497 printf(" ");
vlmeeca98f2004-08-25 02:00:03 +0000498 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_SIZE, s_PV);
vlm4808c702004-08-18 04:50:37 +0000499 printf(" ");
vlmeeca98f2004-08-25 02:00:03 +0000500 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_FROM, s_PV);
vlm4808c702004-08-18 04:50:37 +0000501
502 return 0;
503}
504
505static int
506asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level) {
vlm00ad2822004-08-20 13:24:28 +0000507 int SEQ_OF = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000508
vlmdb4ee042005-02-15 07:06:05 +0000509 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
vlm60e7ef02004-10-13 09:13:56 +0000510 INDENT("-- #line %d\n", tc->_lineno);
vlm177a5b62005-09-05 05:17:57 +0000511
512 /* Reconstruct compiler directive information */
513 if((tc->marker.flags & EM_INDIRECT)
514 && (tc->marker.flags & EM_OMITABLE) != EM_OMITABLE) {
515 if((flags & APF_NOINDENT))
516 printf(" --<ASN1C.RepresentAsPointer>-- ");
517 else
518 INDENT("--<ASN1C.RepresentAsPointer>--\n");
519 }
520
vlmfa67ddc2004-06-03 03:38:44 +0000521 if(tc->Identifier)
522 INDENT("%s", tc->Identifier);
523
524 if(tc->params) {
525 asn1print_params(tc->params, flags);
526 }
527
528 if(tc->meta_type != AMT_VALUE
vlmda4df3f2004-08-25 02:27:47 +0000529 && tc->meta_type != AMT_VALUESET
vlmfa67ddc2004-06-03 03:38:44 +0000530 && tc->expr_type != A1TC_EXTENSIBLE) {
531 if(level) {
vlmdb4ee042005-02-15 07:06:05 +0000532 if(tc->Identifier && !(flags & APF_NOINDENT))
vlmfa67ddc2004-06-03 03:38:44 +0000533 printf("\t");
534 } else {
535 printf(" ::=");
536 }
537 }
538
539 if(tc->tag.tag_class) {
540 printf(" ");
541 asn1print_tag(tc, flags);
542 }
543
544 switch(tc->expr_type) {
545 case A1TC_EXTENSIBLE:
546 if(tc->value) {
547 printf("!");
548 asn1print_value(tc->value, flags);
549 }
550 break;
vlmddd6fd12004-08-22 03:08:51 +0000551 case A1TC_COMPONENTS_OF:
552 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
553 printf(" COMPONENTS OF");
554 break;
vlmdb4ee042005-02-15 07:06:05 +0000555 case A1TC_PARAMETRIZED:
556 flags |= APF_NOINDENT;
vlmfa67ddc2004-06-03 03:38:44 +0000557 case A1TC_REFERENCE:
558 case A1TC_UNIVERVAL:
vlmfa67ddc2004-06-03 03:38:44 +0000559 break;
560 case A1TC_CLASSDEF:
561 printf(" CLASS");
562 break;
563 case A1TC_CLASSFIELD:
564 /* Nothing to print here */
565 break;
vlm00ad2822004-08-20 13:24:28 +0000566 case ASN_CONSTR_SET_OF:
567 case ASN_CONSTR_SEQUENCE_OF:
568 SEQ_OF = 1;
569 if(tc->expr_type == ASN_CONSTR_SET_OF)
570 printf(" SET");
571 else
572 printf(" SEQUENCE");
573 if(tc->constraints) {
574 printf(" ");
575 asn1print_constraint(tc->constraints, flags);
576 }
577 printf(" OF");
578 break;
vlmfa67ddc2004-06-03 03:38:44 +0000579 default:
580 {
581 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
582 printf(" %s", p?p:"<unknown type!>");
583 }
584 break;
585 }
586
vlma750ca42005-06-02 04:06:24 +0000587 /*
588 * Put the name of the referred type.
589 */
vlmfa67ddc2004-06-03 03:38:44 +0000590 if(tc->reference) {
vlma750ca42005-06-02 04:06:24 +0000591 printf(" ");
vlmfa67ddc2004-06-03 03:38:44 +0000592 asn1print_ref(tc->reference, flags);
593 }
594
vlmda4df3f2004-08-25 02:27:47 +0000595 if(tc->meta_type == AMT_VALUESET)
596 printf(" ::=");
597
vlmfa67ddc2004-06-03 03:38:44 +0000598 /*
599 * Display the descendants (children) of the current type.
600 */
vlmda4df3f2004-08-25 02:27:47 +0000601 if(TQ_FIRST(&(tc->members))
602 || (tc->expr_type & ASN_CONSTR_MASK)
603 || tc->meta_type == AMT_VALUESET
604 || tc->meta_type == AMT_OBJECT
605 || tc->meta_type == AMT_OBJECTSET
606 ) {
vlmfa67ddc2004-06-03 03:38:44 +0000607 asn1p_expr_t *se; /* SubExpression */
vlmddd6fd12004-08-22 03:08:51 +0000608 int put_braces = !SEQ_OF; /* Don't need 'em, if SET OF... */
vlmfa67ddc2004-06-03 03:38:44 +0000609
vlmda4df3f2004-08-25 02:27:47 +0000610 if(put_braces) {
vlmdb4ee042005-02-15 07:06:05 +0000611 if(flags & APF_NOINDENT) {
612 printf("{");
613 if(!TQ_FIRST(&tc->members))
614 printf("}");
615 } else {
616 printf(" {");
617 if(TQ_FIRST(&tc->members))
618 printf("\n");
619 else
620 printf(" }");
621 }
vlmda4df3f2004-08-25 02:27:47 +0000622 }
vlmfa67ddc2004-06-03 03:38:44 +0000623
624 TQ_FOR(se, &(tc->members), next) {
625 /*
vlmddd6fd12004-08-22 03:08:51 +0000626 * Print the expression as it were a stand-alone type.
vlmfa67ddc2004-06-03 03:38:44 +0000627 */
vlm60e7ef02004-10-13 09:13:56 +0000628 asn1print_expr(asn, mod, se, flags, level + 1);
vlm093f6752004-09-15 11:44:55 +0000629 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
630 printf(" DEFAULT ");
631 asn1print_value(se->marker.default_value, flags);
632 } else if((se->marker.flags & EM_OPTIONAL)
633 == EM_OPTIONAL) {
vlmd82eb012004-09-10 08:21:27 +0000634 printf(" OPTIONAL");
vlm093f6752004-09-15 11:44:55 +0000635 }
vlm177a5b62005-09-05 05:17:57 +0000636 if(TQ_NEXT(se, next)) {
vlmfa67ddc2004-06-03 03:38:44 +0000637 printf(",");
vlm177a5b62005-09-05 05:17:57 +0000638 if(!(flags & APF_NOINDENT))
639 INDENT("\n");
640 }
vlmfa67ddc2004-06-03 03:38:44 +0000641 }
642
vlmda4df3f2004-08-25 02:27:47 +0000643 if(put_braces && TQ_FIRST(&tc->members)) {
vlmdb4ee042005-02-15 07:06:05 +0000644 if(!(flags & APF_NOINDENT))
645 printf("\n");
vlmfa67ddc2004-06-03 03:38:44 +0000646 INDENT("}");
647 }
648 }
649
650 if(tc->with_syntax)
651 asn1print_with_syntax(tc->with_syntax, flags);
652
vlm00ad2822004-08-20 13:24:28 +0000653 if(!SEQ_OF && tc->constraints) {
vlmfa67ddc2004-06-03 03:38:44 +0000654 printf(" ");
655 asn1print_constraint(tc->constraints, flags);
656 }
vlm4808c702004-08-18 04:50:37 +0000657
vlmfa67ddc2004-06-03 03:38:44 +0000658 if(tc->unique) {
659 printf(" UNIQUE");
660 }
661
662 if(tc->meta_type == AMT_VALUE
663 && tc->expr_type != A1TC_EXTENSIBLE) {
vlmdc4d95a2004-09-05 10:38:50 +0000664 if(tc->expr_type == A1TC_UNIVERVAL) {
vlm093f6752004-09-15 11:44:55 +0000665 if(tc->value) {
666 printf("(");
667 asn1print_value(tc->value, flags);
668 printf(")");
669 }
vlmdc4d95a2004-09-05 10:38:50 +0000670 } else {
671 printf(" ::= ");
672 asn1print_value(tc->value, flags);
673 }
vlmfa67ddc2004-06-03 03:38:44 +0000674 }
675
vlmddd6fd12004-08-22 03:08:51 +0000676 /*
vlmdb4ee042005-02-15 07:06:05 +0000677 * The following section exists entirely for debugging.
vlmddd6fd12004-08-22 03:08:51 +0000678 */
vlm00ad2822004-08-20 13:24:28 +0000679 if(flags & APF_DEBUG_CONSTRAINTS
680 && tc->expr_type != A1TC_EXTENSIBLE) {
vlm4808c702004-08-18 04:50:37 +0000681 asn1p_expr_t *top_parent;
682
683 if(tc->combined_constraints) {
684 printf("\n-- Combined constraints: ");
685 asn1print_constraint(tc->combined_constraints, flags);
686 }
687
vlmcbebb812004-09-22 16:05:13 +0000688 top_parent = asn1f_find_terminal_type_ex(asn, tc);
vlm4808c702004-08-18 04:50:37 +0000689 if(top_parent) {
vlmeeca98f2004-08-25 02:00:03 +0000690 printf("\n-- Practical constraints (%s): ",
691 top_parent->Identifier);
692 asn1print_constraint_explain(top_parent->expr_type,
693 tc->combined_constraints, 0);
vlm00ad2822004-08-20 13:24:28 +0000694 printf("\n-- PER-visible constraints (%s): ",
695 top_parent->Identifier);
vlm4808c702004-08-18 04:50:37 +0000696 asn1print_constraint_explain(top_parent->expr_type,
vlmeeca98f2004-08-25 02:00:03 +0000697 tc->combined_constraints, 1);
vlm4808c702004-08-18 04:50:37 +0000698 }
vlm00ad2822004-08-20 13:24:28 +0000699 printf("\n");
vlm4808c702004-08-18 04:50:37 +0000700 }
701
vlmfa67ddc2004-06-03 03:38:44 +0000702 return 0;
703}
vlm4808c702004-08-18 04:50:37 +0000704
vlm60e7ef02004-10-13 09:13:56 +0000705
706static int
707asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
708 asn1p_expr_t *se;
709 int expr_unordered = 0;
vlmceb93192004-10-14 05:11:25 +0000710 int dont_involve_children = 0;
vlm60e7ef02004-10-13 09:13:56 +0000711
712 switch(expr->meta_type) {
713 case AMT_TYPE:
714 case AMT_TYPEREF:
715 break;
716 default:
717 if(expr->expr_type == A1TC_UNIVERVAL)
718 break;
719 return 0;
720 }
721
722 if(!expr->Identifier) return 0;
723
vlm60e7ef02004-10-13 09:13:56 +0000724 if(flags & APF_LINE_COMMENTS)
725 INDENT("<!-- #line %d -->\n", expr->_lineno);
726 INDENT("<!ELEMENT %s", expr->Identifier);
727
728 if(expr->expr_type == A1TC_REFERENCE) {
729 se = asn1f_find_terminal_type_ex(asn, expr);
730 if(!se) {
vlmceb93192004-10-14 05:11:25 +0000731 printf(" (ANY)");
vlm60e7ef02004-10-13 09:13:56 +0000732 return 0;
733 }
734 expr = se;
vlmceb93192004-10-14 05:11:25 +0000735 dont_involve_children = 1;
vlm60e7ef02004-10-13 09:13:56 +0000736 }
737
vlm4bba1f82005-03-10 15:16:56 +0000738 if(expr->expr_type == ASN_CONSTR_CHOICE
739 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
740 || expr->expr_type == ASN_CONSTR_SET_OF
741 || expr->expr_type == ASN_CONSTR_SET
vlmc45eab72005-03-09 20:52:15 +0000742 || expr->expr_type == ASN_BASIC_INTEGER
743 || expr->expr_type == ASN_BASIC_ENUMERATED) {
744 expr_unordered = 1;
745 }
746
vlm60e7ef02004-10-13 09:13:56 +0000747 if(TQ_FIRST(&expr->members)) {
748 int extensible = 0;
749 printf(" (");
750 TQ_FOR(se, &(expr->members), next) {
751 if(se->expr_type == A1TC_EXTENSIBLE) {
752 extensible = 1;
753 continue;
754 } else if(!se->Identifier
755 && se->expr_type == A1TC_REFERENCE) {
756 asn1print_ref(se->reference, flags);
757 } else if(se->Identifier) {
758 printf("%s", se->Identifier);
759 } else {
760 printf("ANY");
761 }
762 if(expr->expr_type != ASN_CONSTR_SET
763 && expr->expr_type != ASN_CONSTR_CHOICE
764 && expr->expr_type != ASN_BASIC_INTEGER
765 && expr->expr_type != ASN_BASIC_ENUMERATED) {
766 if(expr_unordered)
767 printf("*");
768 else if(se->marker.flags)
769 printf("?");
770 }
771 if(TQ_NEXT(se, next)
772 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
773 printf(expr_unordered?"|":", ");
774 }
775 }
776 if(extensible) {
777 printf(expr_unordered?"|":", ");
778 printf("ANY");
779 if(expr->expr_type != ASN_CONSTR_SET
780 && expr->expr_type != ASN_CONSTR_CHOICE
781 && expr->expr_type != ASN_BASIC_INTEGER
782 && expr->expr_type != ASN_BASIC_ENUMERATED)
783 printf("*");
784 }
785
786 printf(")");
787 if(expr->expr_type == ASN_CONSTR_SET)
788 printf("*");
789
vlmceb93192004-10-14 05:11:25 +0000790 } else switch(expr->expr_type) {
791 case ASN_BASIC_BOOLEAN:
vlmc45eab72005-03-09 20:52:15 +0000792 printf(" (true|false)");
vlmceb93192004-10-14 05:11:25 +0000793 break;
794 case ASN_CONSTR_CHOICE:
795 case ASN_CONSTR_SET:
796 case ASN_CONSTR_SET_OF:
797 case ASN_CONSTR_SEQUENCE:
798 case ASN_CONSTR_SEQUENCE_OF:
799 case ASN_BASIC_NULL:
800 case A1TC_UNIVERVAL:
vlm60e7ef02004-10-13 09:13:56 +0000801 printf(" EMPTY");
vlmceb93192004-10-14 05:11:25 +0000802 break;
803 case ASN_TYPE_ANY:
804 printf(" ANY");
805 break;
806 case ASN_BASIC_BIT_STRING:
807 case ASN_BASIC_OCTET_STRING:
808 case ASN_BASIC_OBJECT_IDENTIFIER:
809 case ASN_BASIC_RELATIVE_OID:
vlm40a69bf2004-10-15 06:01:54 +0000810 case ASN_BASIC_INTEGER:
vlmceb93192004-10-14 05:11:25 +0000811 case ASN_BASIC_UTCTime:
812 case ASN_BASIC_GeneralizedTime:
vlmceb93192004-10-14 05:11:25 +0000813 case ASN_STRING_NumericString:
814 case ASN_STRING_PrintableString:
vlm40a69bf2004-10-15 06:01:54 +0000815 printf(" (#PCDATA)");
vlmceb93192004-10-14 05:11:25 +0000816 break;
817 case ASN_STRING_VisibleString:
818 case ASN_STRING_ISO646String:
819 /* Entity references, but not XML elements may be present */
vlm60e7ef02004-10-13 09:13:56 +0000820 printf(" (#PCDATA)");
vlmceb93192004-10-14 05:11:25 +0000821 break;
822 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
823 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
824 default:
825 /*
826 * XML elements are allowed.
827 * For example, a UTF8String may contain "<bel/>".
828 */
829 printf(" ANY");
vlm60e7ef02004-10-13 09:13:56 +0000830 }
831 printf(">\n");
832
833 /*
834 * Display the descendants (children) of the current type.
835 */
vlmceb93192004-10-14 05:11:25 +0000836 if(!dont_involve_children) {
837 TQ_FOR(se, &(expr->members), next) {
838 if(se->expr_type == A1TC_EXTENSIBLE) continue;
839 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
840 }
vlm60e7ef02004-10-13 09:13:56 +0000841 }
842
843 return 0;
844}