blob: 0cb730416e6df34b825089b9255cd2a206d06d12 [file] [log] [blame]
Lev Walkinf15320b2004-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>
Lev Walkin3140e0e2004-08-18 04:50:37 +00007#include <asn1fix_export.h>
Lev Walkin9095ada2004-08-18 05:41:05 +00008#include <asn1fix_crange.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00009
10#include "asn1print.h"
11
12#define INDENT(fmt, args...) do { \
Lev Walkinf4069d22005-02-15 07:06:05 +000013 if(!(flags & APF_NOINDENT)) { \
14 int __i = level; \
15 while(__i--) printf(" "); \
16 } \
Lev Walkinf15320b2004-06-03 03:38:44 +000017 printf(fmt, ##args); \
18 } while(0)
19
Lev Walkin3140e0e2004-08-18 04:50:37 +000020static int asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags);
Lev Walkind6db8022005-03-18 03:53:05 +000021static int asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags);
Lev Walkin3140e0e2004-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);
Lev Walkinf7484512004-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);
Lev Walkinf15320b2004-06-03 03:38:44 +000030
31/*
32 * Print the contents of the parsed ASN tree.
33 */
34int
Lev Walkin3140e0e2004-08-18 04:50:37 +000035asn1print(asn1p_t *asn, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +000036 asn1p_module_t *mod;
Lev Walkinc70c2ef2004-09-30 06:38:21 +000037 int modno = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000038
39 if(asn == NULL) {
40 errno = EINVAL;
41 return -1;
42 }
43
Lev Walkinf7484512004-10-13 09:13:56 +000044 if(flags & APF_PRINT_XML_DTD)
45 printf("<!-- XML DTD generated by asn1c-" VERSION " -->\n\n");
46
Lev Walkinf15320b2004-06-03 03:38:44 +000047 TQ_FOR(mod, &(asn->modules), mod_next) {
Lev Walkinc70c2ef2004-09-30 06:38:21 +000048 if(modno++) printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +000049 asn1print_module(asn, mod, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +000050 }
51
Lev Walkin112d69e2005-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
Lev Walkinf15320b2004-06-03 03:38:44 +000058 return 0;
59}
60
61static int
Lev Walkin3140e0e2004-08-18 04:50:37 +000062asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +000063 asn1p_expr_t *tc;
64
Lev Walkinf7484512004-10-13 09:13:56 +000065 if(flags & APF_PRINT_XML_DTD)
66 printf("<!-- ASN.1 module\n");
67
Lev Walkinc70c2ef2004-09-30 06:38:21 +000068 printf("%s ", mod->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +000069 if(mod->module_oid) {
Lev Walkind6db8022005-03-18 03:53:05 +000070 asn1print_oid(strlen(mod->Identifier), mod->module_oid, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +000071 printf("\n");
72 }
73
Lev Walkinf7484512004-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
Lev Walkinf15320b2004-06-03 03:38:44 +000087 printf("DEFINITIONS");
88
Lev Walkin3140e0e2004-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");
Lev Walkinf15320b2004-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) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000106 asn1print_expr(asn, mod, tc, flags, 0);
Lev Walkinb1ef3962004-08-20 13:24:28 +0000107 if(flags & APF_DEBUG_CONSTRAINTS)
108 printf("\n");
109 else
110 printf("\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000111 }
112
113 printf("END\n");
114
115 return 0;
116}
117
118static int
Lev Walkind6db8022005-03-18 03:53:05 +0000119asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags) {
120 size_t accum = prior_len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000121 int ac;
Lev Walkinf15320b2004-06-03 03:38:44 +0000122
Lev Walkind9bd7752004-06-05 08:17:50 +0000123 (void)flags; /* Unused argument */
124
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 printf("{");
126 for(ac = 0; ac < oid->arcs_count; ac++) {
Lev Walkin4efbfb72005-02-25 14:20:30 +0000127 const char *arcname = oid->arcs[ac].name;
128
Lev Walkind6db8022005-03-18 03:53:05 +0000129 if(accum + strlen(arcname ? arcname : "") > 75) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 printf("\n\t");
131 accum = 0;
Lev Walkind6db8022005-03-18 03:53:05 +0000132 } else {
133 accum += printf(" ");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000134 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000135
Lev Walkin4efbfb72005-02-25 14:20:30 +0000136 if(arcname) {
Lev Walkind6db8022005-03-18 03:53:05 +0000137 accum += printf("%s", arcname);
Lev Walkin0056aec2004-09-05 10:38:50 +0000138 if(oid->arcs[ac].number >= 0) {
Lev Walkind6db8022005-03-18 03:53:05 +0000139 accum += printf("(%" PRIdASN ")",
140 oid->arcs[ac].number);
Lev Walkin0056aec2004-09-05 10:38:50 +0000141 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 } else {
Lev Walkind6db8022005-03-18 03:53:05 +0000143 accum += printf("%" PRIdASN, oid->arcs[ac].number);
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000145 }
146 printf(" }");
147
148 return 0;
149}
150
151static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000152asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000153 int cc;
154
Lev Walkind9bd7752004-06-05 08:17:50 +0000155 (void)flags; /* Unused argument */
156
Lev Walkinf15320b2004-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
Lev Walkin3140e0e2004-08-18 04:50:37 +0000166asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000167 struct asn1p_type_tag_s *tag = &tc->tag;
168
Lev Walkind9bd7752004-06-05 08:17:50 +0000169 (void)flags; /* Unused argument */
170
Lev Walkinf15320b2004-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 }
Lev Walkin33c16ba2004-09-24 21:01:43 +0000185 printf("%" PRIdASN "]", tag->tag_value);
Lev Walkinf15320b2004-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
Lev Walkin3140e0e2004-08-18 04:50:37 +0000197asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000198
199 if(val == NULL)
200 return 0;
201
202 switch(val->type) {
203 case ATV_NOVALUE:
204 break;
Lev Walkinb1e07082004-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;
Lev Walkinf15320b2004-06-03 03:38:44 +0000211 case ATV_INTEGER:
Lev Walkin33c16ba2004-09-24 21:01:43 +0000212 printf("%" PRIdASN, val->value.v_integer);
Lev Walkinf15320b2004-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;
Lev Walkinf15320b2004-06-03 03:38:44 +0000218 case ATV_STRING:
219 {
220 char *p = val->value.string.buf;
221 putchar('"');
222 if(strchr(p, '"')) {
223 /* Mask quotes */
224 for(; *p; p++) {
225 if(*p == '"')
226 putchar(*p);
227 putchar(*p);
228 }
229 } else {
230 fputs(p, stdout);
231 }
232 putchar('"');
233 }
234 return 0;
235 case ATV_UNPARSED:
236 fputs(val->value.string.buf, stdout);
237 return 0;
238 case ATV_BITVECTOR:
239 {
240 uint8_t *bitvector;
241 int bits;
242 int i;
243
244 bitvector = val->value.binary_vector.bits;
245 bits = val->value.binary_vector.size_in_bits;
246
247 printf("'");
248 if(bits%8) {
249 for(i = 0; i < bits; i++) {
250 uint8_t uc;
251 uc = bitvector[i>>3];
252 putchar(((uc >> (7-(i%8)))&1)?'1':'0');
253 }
254 printf("'B");
255 } else {
256 char hextable[16] = "0123456789ABCDEF";
257 for(i = 0; i < (bits>>3); i++) {
258 putchar(hextable[bitvector[i] >> 4]);
259 putchar(hextable[bitvector[i] & 0x0f]);
260 }
261 printf("'H");
262 }
Lev Walkin043af0d2005-02-24 21:07:35 +0000263 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000264 }
Lev Walkinb1e07082004-09-15 11:44:55 +0000265 case ATV_REFERENCED:
266 return asn1print_ref(val->value.reference, flags);
267 case ATV_CHOICE_IDENTIFIER:
268 printf("%s: ", val->value.choice_identifier.identifier);
269 return asn1print_value(val->value.choice_identifier.value, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000270 }
271
272 assert(val->type || !"Unknown");
273
274 return 0;
275}
276
277static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000278asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000279 int symno = 0;
280
281 if(ct == 0) return 0;
282
283 if(ct->type == ACT_CA_SET)
284 printf("(");
285
286 switch(ct->type) {
Lev Walkinff7dd142005-03-20 12:58:00 +0000287 case ACT_EL_TYPE:
288 asn1print_value(ct->value, flags);
289 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000290 case ACT_EL_VALUE:
291 asn1print_value(ct->value, flags);
292 break;
293 case ACT_EL_RANGE:
294 case ACT_EL_LLRANGE:
295 case ACT_EL_RLRANGE:
296 case ACT_EL_ULRANGE:
297 asn1print_value(ct->range_start, flags);
298 switch(ct->type) {
299 case ACT_EL_RANGE: printf(".."); break;
300 case ACT_EL_LLRANGE: printf("<.."); break;
301 case ACT_EL_RLRANGE: printf("..<"); break;
302 case ACT_EL_ULRANGE: printf("<..<"); break;
303 default: printf("?..?"); break;
304 }
305 asn1print_value(ct->range_stop, flags);
306 break;
307 case ACT_EL_EXT:
308 printf("...");
309 break;
310 case ACT_CT_SIZE:
311 case ACT_CT_FROM:
312 switch(ct->type) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000313 case ACT_CT_SIZE: printf("SIZE("); break;
314 case ACT_CT_FROM: printf("FROM("); break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000315 default: printf("??? ("); break;
316 }
317 assert(ct->el_count != 0);
318 assert(ct->el_count == 1);
319 asn1print_constraint(ct->elements[0], flags);
320 printf(")");
321 break;
322 case ACT_CT_WCOMP:
323 case ACT_CT_WCOMPS:
324 printf("???");
325 break;
326 case ACT_CA_SET: symno++;
327 case ACT_CA_CRC: symno++;
328 case ACT_CA_CSV: symno++;
329 case ACT_CA_UNI: symno++;
330 case ACT_CA_INT: symno++;
331 case ACT_CA_EXC:
332 {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000333 char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",",
Lev Walkinf15320b2004-06-03 03:38:44 +0000334 "", "(" };
Lev Walkin0056aec2004-09-05 10:38:50 +0000335 unsigned int i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000336 for(i = 0; i < ct->el_count; i++) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000337 enum asn1print_flags nflags = flags;
Lev Walkinf15320b2004-06-03 03:38:44 +0000338 if(i) fputs(symtable[symno], stdout);
339 if(ct->type == ACT_CA_CRC) fputs("{", stdout);
340 asn1print_constraint(ct->elements[i], nflags);
341 if(ct->type == ACT_CA_CRC) fputs("}", stdout);
342 if(i+1 < ct->el_count
343 && ct->type == ACT_CA_SET)
344 fputs(")", stdout);
345 }
346 }
347 break;
348 case ACT_INVALID:
349 assert(ct->type != ACT_INVALID);
350 break;
351 }
352
353 if(ct->type == ACT_CA_SET)
354 printf(")");
355
356 return 0;
357}
358
359static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000360asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000361 if(pl) {
362 int i;
363 printf("{");
364 for(i = 0; i < pl->params_count; i++) {
365 if(i) printf(", ");
366 if(pl->params[i].governor) {
367 asn1print_ref(pl->params[i].governor, flags);
368 printf(":");
369 }
370 printf("%s", pl->params[i].argument);
371 }
372 printf("}");
373 }
374
375 return 0;
376}
377
378static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000379asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000380 if(wx) {
381 asn1p_wsyntx_chunk_t *wc;
382 printf(" WITH SYNTAX {");
383 TQ_FOR(wc, &(wx->chunks), next) {
384 if(wc->ref) {
385 asn1print_ref(wc->ref, flags);
386 } else {
387 fwrite(wc->buf, 1, wc->len, stdout);
388 }
389 }
390 printf("}\n");
391 }
392
393 return 0;
394}
395
396static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000397asn1print_crange_value(asn1cnst_edge_t *edge, int as_char) {
398 switch(edge->type) {
399 case ARE_MIN: printf("MIN"); break;
400 case ARE_MAX: printf("MAX"); break;
401 case ARE_VALUE:
402 if(as_char) {
403 printf("\"%c\"", (unsigned char)edge->value);
404 } else {
Lev Walkin33c16ba2004-09-24 21:01:43 +0000405 printf("%" PRIdASN, edge->value);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000406 }
407 }
408 return 0;
409}
410
411static int
Lev Walkine8e87f12004-08-25 02:00:03 +0000412asn1print_constraint_explain_type(asn1p_expr_type_e expr_type, asn1p_constraint_t *ct, enum asn1p_constraint_type_e type, int strict_PER_visible) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000413 asn1cnst_range_t *range;
414 int as_char = (type==ACT_CT_FROM);
415 int i;
416
Lev Walkine8e87f12004-08-25 02:00:03 +0000417 range = asn1constraint_compute_PER_range(expr_type, ct, type,
418 0, 0, strict_PER_visible);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000419 if(!range) return -1;
420
Lev Walkine8e87f12004-08-25 02:00:03 +0000421 if(range->incompatible
422 || (strict_PER_visible && range->not_PER_visible)) {
423 asn1constraint_range_free(range);
424 return 0;
425 }
426
Lev Walkin3140e0e2004-08-18 04:50:37 +0000427 switch(type) {
428 case ACT_CT_FROM: printf("(FROM("); break;
429 case ACT_CT_SIZE: printf("(SIZE("); break;
430 default: printf("("); break;
431 }
432 for(i = -1; i < range->el_count; i++) {
433 asn1cnst_range_t *r;
434 if(i == -1) {
435 if(range->el_count) continue;
436 r = range;
437 } else {
438 r = range->elements[i];
439 }
440 if(i > 0) {
441 printf(" | ");
442 }
443 asn1print_crange_value(&r->left, as_char);
444 if(r->left.type != r->right.type
445 || r->left.value != r->right.value) {
446 printf("..");
447 asn1print_crange_value(&r->right, as_char);
448 }
449 }
450 if(range->extensible)
451 printf(",...");
452 printf(type==ACT_EL_RANGE?")":"))");
453
454 if(range->empty_constraint)
455 printf(":Empty!");
456
457 asn1constraint_range_free(range);
458 return 0;
459}
460
461static int
462asn1print_constraint_explain(asn1p_expr_type_e expr_type,
Lev Walkine8e87f12004-08-25 02:00:03 +0000463 asn1p_constraint_t *ct, int s_PV) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000464
Lev Walkine8e87f12004-08-25 02:00:03 +0000465 asn1print_constraint_explain_type(expr_type, ct, ACT_EL_RANGE, s_PV);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000466 printf(" ");
Lev Walkine8e87f12004-08-25 02:00:03 +0000467 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_SIZE, s_PV);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000468 printf(" ");
Lev Walkine8e87f12004-08-25 02:00:03 +0000469 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_FROM, s_PV);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000470
471 return 0;
472}
473
474static int
475asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level) {
Lev Walkinb1ef3962004-08-20 13:24:28 +0000476 int SEQ_OF = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000477
Lev Walkinf4069d22005-02-15 07:06:05 +0000478 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
Lev Walkinf7484512004-10-13 09:13:56 +0000479 INDENT("-- #line %d\n", tc->_lineno);
Lev Walkinf15320b2004-06-03 03:38:44 +0000480 if(tc->Identifier)
481 INDENT("%s", tc->Identifier);
482
483 if(tc->params) {
484 asn1print_params(tc->params, flags);
485 }
486
487 if(tc->meta_type != AMT_VALUE
Lev Walkinc74ea222004-08-25 02:27:47 +0000488 && tc->meta_type != AMT_VALUESET
Lev Walkinf15320b2004-06-03 03:38:44 +0000489 && tc->expr_type != A1TC_EXTENSIBLE) {
490 if(level) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000491 if(tc->Identifier && !(flags & APF_NOINDENT))
Lev Walkinf15320b2004-06-03 03:38:44 +0000492 printf("\t");
493 } else {
494 printf(" ::=");
495 }
496 }
497
498 if(tc->tag.tag_class) {
499 printf(" ");
500 asn1print_tag(tc, flags);
501 }
502
503 switch(tc->expr_type) {
504 case A1TC_EXTENSIBLE:
505 if(tc->value) {
506 printf("!");
507 asn1print_value(tc->value, flags);
508 }
509 break;
Lev Walkinfd151ce2004-08-22 03:08:51 +0000510 case A1TC_COMPONENTS_OF:
511 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
512 printf(" COMPONENTS OF");
513 break;
Lev Walkinf4069d22005-02-15 07:06:05 +0000514 case A1TC_PARAMETRIZED:
515 flags |= APF_NOINDENT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000516 case A1TC_REFERENCE:
517 case A1TC_UNIVERVAL:
Lev Walkinf15320b2004-06-03 03:38:44 +0000518 break;
519 case A1TC_CLASSDEF:
520 printf(" CLASS");
521 break;
522 case A1TC_CLASSFIELD:
523 /* Nothing to print here */
524 break;
Lev Walkinb1ef3962004-08-20 13:24:28 +0000525 case ASN_CONSTR_SET_OF:
526 case ASN_CONSTR_SEQUENCE_OF:
527 SEQ_OF = 1;
528 if(tc->expr_type == ASN_CONSTR_SET_OF)
529 printf(" SET");
530 else
531 printf(" SEQUENCE");
532 if(tc->constraints) {
533 printf(" ");
534 asn1print_constraint(tc->constraints, flags);
535 }
536 printf(" OF");
537 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000538 default:
539 {
540 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
541 printf(" %s", p?p:"<unknown type!>");
542 }
543 break;
544 }
545
546 if(tc->reference) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000547 if(!(flags & APF_NOINDENT))
548 printf(" ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000549 asn1print_ref(tc->reference, flags);
550 }
551
Lev Walkinc74ea222004-08-25 02:27:47 +0000552 if(tc->meta_type == AMT_VALUESET)
553 printf(" ::=");
554
Lev Walkinf15320b2004-06-03 03:38:44 +0000555 /*
556 * Display the descendants (children) of the current type.
557 */
Lev Walkinc74ea222004-08-25 02:27:47 +0000558 if(TQ_FIRST(&(tc->members))
559 || (tc->expr_type & ASN_CONSTR_MASK)
560 || tc->meta_type == AMT_VALUESET
561 || tc->meta_type == AMT_OBJECT
562 || tc->meta_type == AMT_OBJECTSET
563 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000564 asn1p_expr_t *se; /* SubExpression */
Lev Walkinfd151ce2004-08-22 03:08:51 +0000565 int put_braces = !SEQ_OF; /* Don't need 'em, if SET OF... */
Lev Walkinf15320b2004-06-03 03:38:44 +0000566
Lev Walkinc74ea222004-08-25 02:27:47 +0000567 if(put_braces) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000568 if(flags & APF_NOINDENT) {
569 printf("{");
570 if(!TQ_FIRST(&tc->members))
571 printf("}");
572 } else {
573 printf(" {");
574 if(TQ_FIRST(&tc->members))
575 printf("\n");
576 else
577 printf(" }");
578 }
Lev Walkinc74ea222004-08-25 02:27:47 +0000579 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000580
581 TQ_FOR(se, &(tc->members), next) {
582 /*
Lev Walkinfd151ce2004-08-22 03:08:51 +0000583 * Print the expression as it were a stand-alone type.
Lev Walkinf15320b2004-06-03 03:38:44 +0000584 */
Lev Walkinf7484512004-10-13 09:13:56 +0000585 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinb1e07082004-09-15 11:44:55 +0000586 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
587 printf(" DEFAULT ");
588 asn1print_value(se->marker.default_value, flags);
589 } else if((se->marker.flags & EM_OPTIONAL)
590 == EM_OPTIONAL) {
Lev Walkin6e8da2b2004-09-10 08:21:27 +0000591 printf(" OPTIONAL");
Lev Walkinb1e07082004-09-15 11:44:55 +0000592 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000593 if(TQ_NEXT(se, next)) {
594 printf(",");
Lev Walkinf4069d22005-02-15 07:06:05 +0000595 if(!(flags & APF_NOINDENT))
596 INDENT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000597 }
598 }
599
Lev Walkinc74ea222004-08-25 02:27:47 +0000600 if(put_braces && TQ_FIRST(&tc->members)) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000601 if(!(flags & APF_NOINDENT))
602 printf("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000603 INDENT("}");
604 }
605 }
606
607 if(tc->with_syntax)
608 asn1print_with_syntax(tc->with_syntax, flags);
609
Lev Walkinb1ef3962004-08-20 13:24:28 +0000610 if(!SEQ_OF && tc->constraints) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000611 printf(" ");
612 asn1print_constraint(tc->constraints, flags);
613 }
Lev Walkin3140e0e2004-08-18 04:50:37 +0000614
Lev Walkinf15320b2004-06-03 03:38:44 +0000615 if(tc->unique) {
616 printf(" UNIQUE");
617 }
618
619 if(tc->meta_type == AMT_VALUE
620 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin0056aec2004-09-05 10:38:50 +0000621 if(tc->expr_type == A1TC_UNIVERVAL) {
Lev Walkinb1e07082004-09-15 11:44:55 +0000622 if(tc->value) {
623 printf("(");
624 asn1print_value(tc->value, flags);
625 printf(")");
626 }
Lev Walkin0056aec2004-09-05 10:38:50 +0000627 } else {
628 printf(" ::= ");
629 asn1print_value(tc->value, flags);
630 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000631 }
632
Lev Walkinfd151ce2004-08-22 03:08:51 +0000633 /*
Lev Walkinf4069d22005-02-15 07:06:05 +0000634 * The following section exists entirely for debugging.
Lev Walkinfd151ce2004-08-22 03:08:51 +0000635 */
Lev Walkinb1ef3962004-08-20 13:24:28 +0000636 if(flags & APF_DEBUG_CONSTRAINTS
637 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000638 asn1p_expr_t *top_parent;
639
640 if(tc->combined_constraints) {
641 printf("\n-- Combined constraints: ");
642 asn1print_constraint(tc->combined_constraints, flags);
643 }
644
Lev Walkine4d6ab82004-09-22 16:05:13 +0000645 top_parent = asn1f_find_terminal_type_ex(asn, tc);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000646 if(top_parent) {
Lev Walkine8e87f12004-08-25 02:00:03 +0000647 printf("\n-- Practical constraints (%s): ",
648 top_parent->Identifier);
649 asn1print_constraint_explain(top_parent->expr_type,
650 tc->combined_constraints, 0);
Lev Walkinb1ef3962004-08-20 13:24:28 +0000651 printf("\n-- PER-visible constraints (%s): ",
652 top_parent->Identifier);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000653 asn1print_constraint_explain(top_parent->expr_type,
Lev Walkine8e87f12004-08-25 02:00:03 +0000654 tc->combined_constraints, 1);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000655 }
Lev Walkinb1ef3962004-08-20 13:24:28 +0000656 printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000657 }
658
Lev Walkinf15320b2004-06-03 03:38:44 +0000659 return 0;
660}
Lev Walkin3140e0e2004-08-18 04:50:37 +0000661
Lev Walkinf7484512004-10-13 09:13:56 +0000662
663static int
664asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
665 asn1p_expr_t *se;
666 int expr_unordered = 0;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000667 int dont_involve_children = 0;
Lev Walkinf7484512004-10-13 09:13:56 +0000668
669 switch(expr->meta_type) {
670 case AMT_TYPE:
671 case AMT_TYPEREF:
672 break;
673 default:
674 if(expr->expr_type == A1TC_UNIVERVAL)
675 break;
676 return 0;
677 }
678
679 if(!expr->Identifier) return 0;
680
Lev Walkinf7484512004-10-13 09:13:56 +0000681 if(flags & APF_LINE_COMMENTS)
682 INDENT("<!-- #line %d -->\n", expr->_lineno);
683 INDENT("<!ELEMENT %s", expr->Identifier);
684
685 if(expr->expr_type == A1TC_REFERENCE) {
686 se = asn1f_find_terminal_type_ex(asn, expr);
687 if(!se) {
Lev Walkinef39f7e2004-10-14 05:11:25 +0000688 printf(" (ANY)");
Lev Walkinf7484512004-10-13 09:13:56 +0000689 return 0;
690 }
691 expr = se;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000692 dont_involve_children = 1;
Lev Walkinf7484512004-10-13 09:13:56 +0000693 }
694
Lev Walkind7963aa2005-03-10 15:16:56 +0000695 if(expr->expr_type == ASN_CONSTR_CHOICE
696 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
697 || expr->expr_type == ASN_CONSTR_SET_OF
698 || expr->expr_type == ASN_CONSTR_SET
Lev Walkin112d69e2005-03-09 20:52:15 +0000699 || expr->expr_type == ASN_BASIC_INTEGER
700 || expr->expr_type == ASN_BASIC_ENUMERATED) {
701 expr_unordered = 1;
702 }
703
Lev Walkinf7484512004-10-13 09:13:56 +0000704 if(TQ_FIRST(&expr->members)) {
705 int extensible = 0;
706 printf(" (");
707 TQ_FOR(se, &(expr->members), next) {
708 if(se->expr_type == A1TC_EXTENSIBLE) {
709 extensible = 1;
710 continue;
711 } else if(!se->Identifier
712 && se->expr_type == A1TC_REFERENCE) {
713 asn1print_ref(se->reference, flags);
714 } else if(se->Identifier) {
715 printf("%s", se->Identifier);
716 } else {
717 printf("ANY");
718 }
719 if(expr->expr_type != ASN_CONSTR_SET
720 && expr->expr_type != ASN_CONSTR_CHOICE
721 && expr->expr_type != ASN_BASIC_INTEGER
722 && expr->expr_type != ASN_BASIC_ENUMERATED) {
723 if(expr_unordered)
724 printf("*");
725 else if(se->marker.flags)
726 printf("?");
727 }
728 if(TQ_NEXT(se, next)
729 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
730 printf(expr_unordered?"|":", ");
731 }
732 }
733 if(extensible) {
734 printf(expr_unordered?"|":", ");
735 printf("ANY");
736 if(expr->expr_type != ASN_CONSTR_SET
737 && expr->expr_type != ASN_CONSTR_CHOICE
738 && expr->expr_type != ASN_BASIC_INTEGER
739 && expr->expr_type != ASN_BASIC_ENUMERATED)
740 printf("*");
741 }
742
743 printf(")");
744 if(expr->expr_type == ASN_CONSTR_SET)
745 printf("*");
746
Lev Walkinef39f7e2004-10-14 05:11:25 +0000747 } else switch(expr->expr_type) {
748 case ASN_BASIC_BOOLEAN:
Lev Walkin112d69e2005-03-09 20:52:15 +0000749 printf(" (true|false)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000750 break;
751 case ASN_CONSTR_CHOICE:
752 case ASN_CONSTR_SET:
753 case ASN_CONSTR_SET_OF:
754 case ASN_CONSTR_SEQUENCE:
755 case ASN_CONSTR_SEQUENCE_OF:
756 case ASN_BASIC_NULL:
757 case A1TC_UNIVERVAL:
Lev Walkinf7484512004-10-13 09:13:56 +0000758 printf(" EMPTY");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000759 break;
760 case ASN_TYPE_ANY:
761 printf(" ANY");
762 break;
763 case ASN_BASIC_BIT_STRING:
764 case ASN_BASIC_OCTET_STRING:
765 case ASN_BASIC_OBJECT_IDENTIFIER:
766 case ASN_BASIC_RELATIVE_OID:
Lev Walkincc116532004-10-15 06:01:54 +0000767 case ASN_BASIC_INTEGER:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000768 case ASN_BASIC_UTCTime:
769 case ASN_BASIC_GeneralizedTime:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000770 case ASN_STRING_NumericString:
771 case ASN_STRING_PrintableString:
Lev Walkincc116532004-10-15 06:01:54 +0000772 printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000773 break;
774 case ASN_STRING_VisibleString:
775 case ASN_STRING_ISO646String:
776 /* Entity references, but not XML elements may be present */
Lev Walkinf7484512004-10-13 09:13:56 +0000777 printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000778 break;
779 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
780 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
781 default:
782 /*
783 * XML elements are allowed.
784 * For example, a UTF8String may contain "<bel/>".
785 */
786 printf(" ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000787 }
788 printf(">\n");
789
790 /*
791 * Display the descendants (children) of the current type.
792 */
Lev Walkinef39f7e2004-10-14 05:11:25 +0000793 if(!dont_involve_children) {
794 TQ_FOR(se, &(expr->members), next) {
795 if(se->expr_type == A1TC_EXTENSIBLE) continue;
796 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
797 }
Lev Walkinf7484512004-10-13 09:13:56 +0000798 }
799
800 return 0;
801}