blob: fc0e7b35b4586e52313458e3634fd99c006df938 [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 Walkin1e448d32005-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;
Lev Walkinf15320b2004-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 }
Lev Walkin043af0d2005-02-24 21:07:35 +0000276 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000277 }
Lev Walkinb1e07082004-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);
Lev Walkinf15320b2004-06-03 03:38:44 +0000283 }
284
285 assert(val->type || !"Unknown");
286
287 return 0;
288}
289
290static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000291asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
Lev Walkinf15320b2004-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) {
Lev Walkinff7dd142005-03-20 12:58:00 +0000300 case ACT_EL_TYPE:
301 asn1print_value(ct->value, flags);
302 break;
Lev Walkinf15320b2004-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) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000326 case ACT_CT_SIZE: printf("SIZE("); break;
327 case ACT_CT_FROM: printf("FROM("); break;
Lev Walkinf15320b2004-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:
Lev Walkine596bf02005-03-28 15:01:27 +0000336 assert(ct->el_count != 0);
337 assert(ct->el_count == 1);
338 printf("WITH COMPONENT (");
339 asn1print_constraint(ct->elements[0], flags);
340 printf(")");
Lev Walkin8638f0d2005-03-28 07:50:06 +0000341 break;
Lev Walkine596bf02005-03-28 15:01:27 +0000342 case ACT_CT_WCOMPS: {
343 unsigned int i;
344 printf("WITH COMPONENTS { ");
345 for(i = 0; i < ct->el_count; i++) {
346 asn1p_constraint_t *cel = ct->elements[i];
347 if(i) printf(", ");
348 fwrite(cel->value->value.string.buf,
349 1, cel->value->value.string.size,
350 stdout);
351 if(cel->el_count) {
352 assert(cel->el_count == 1);
353 printf(" ");
354 asn1print_constraint(cel->elements[0],
355 flags);
356 }
357 switch(cel->presence) {
358 case ACPRES_DEFAULT: break;
359 case ACPRES_PRESENT: printf(" PRESENT"); break;
360 case ACPRES_ABSENT: printf(" ABSENT"); break;
361 case ACPRES_OPTIONAL: printf(" OPTIONAL");break;
362 }
363 }
364 printf(" }");
365 }
Lev Walkin8638f0d2005-03-28 07:50:06 +0000366 break;
Lev Walkin1893ddf2005-03-20 14:28:32 +0000367 case ACT_CT_CTDBY:
368 printf("CONSTRAINED BY ");
369 assert(ct->value->type == ATV_UNPARSED);
370 fwrite(ct->value->value.string.buf,
371 1, ct->value->value.string.size, stdout);
Lev Walkinf15320b2004-06-03 03:38:44 +0000372 break;
373 case ACT_CA_SET: symno++;
374 case ACT_CA_CRC: symno++;
375 case ACT_CA_CSV: symno++;
376 case ACT_CA_UNI: symno++;
377 case ACT_CA_INT: symno++;
378 case ACT_CA_EXC:
379 {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000380 char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",",
Lev Walkinf15320b2004-06-03 03:38:44 +0000381 "", "(" };
Lev Walkin0056aec2004-09-05 10:38:50 +0000382 unsigned int i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000383 for(i = 0; i < ct->el_count; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000384 if(i) fputs(symtable[symno], stdout);
385 if(ct->type == ACT_CA_CRC) fputs("{", stdout);
Lev Walkin1e448d32005-03-24 14:26:38 +0000386 asn1print_constraint(ct->elements[i], flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000387 if(ct->type == ACT_CA_CRC) fputs("}", stdout);
388 if(i+1 < ct->el_count
389 && ct->type == ACT_CA_SET)
390 fputs(")", stdout);
391 }
392 }
393 break;
Lev Walkin1e448d32005-03-24 14:26:38 +0000394 case ACT_CA_AEX:
395 assert(ct->el_count == 1);
396 printf("ALL EXCEPT ");
397 asn1print_constraint(ct->elements[0], flags);
398 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000399 case ACT_INVALID:
400 assert(ct->type != ACT_INVALID);
401 break;
402 }
403
404 if(ct->type == ACT_CA_SET)
405 printf(")");
406
407 return 0;
408}
409
410static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000411asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000412 if(pl) {
413 int i;
414 printf("{");
415 for(i = 0; i < pl->params_count; i++) {
416 if(i) printf(", ");
417 if(pl->params[i].governor) {
418 asn1print_ref(pl->params[i].governor, flags);
419 printf(":");
420 }
421 printf("%s", pl->params[i].argument);
422 }
423 printf("}");
424 }
425
426 return 0;
427}
428
429static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000430asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000431 if(wx) {
432 asn1p_wsyntx_chunk_t *wc;
433 printf(" WITH SYNTAX {");
434 TQ_FOR(wc, &(wx->chunks), next) {
435 if(wc->ref) {
436 asn1print_ref(wc->ref, flags);
437 } else {
438 fwrite(wc->buf, 1, wc->len, stdout);
439 }
440 }
441 printf("}\n");
442 }
443
444 return 0;
445}
446
447static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000448asn1print_crange_value(asn1cnst_edge_t *edge, int as_char) {
449 switch(edge->type) {
450 case ARE_MIN: printf("MIN"); break;
451 case ARE_MAX: printf("MAX"); break;
452 case ARE_VALUE:
453 if(as_char) {
454 printf("\"%c\"", (unsigned char)edge->value);
455 } else {
Lev Walkin33c16ba2004-09-24 21:01:43 +0000456 printf("%" PRIdASN, edge->value);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000457 }
458 }
459 return 0;
460}
461
462static int
Lev Walkine8e87f12004-08-25 02:00:03 +0000463asn1print_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 +0000464 asn1cnst_range_t *range;
465 int as_char = (type==ACT_CT_FROM);
466 int i;
467
Lev Walkine8e87f12004-08-25 02:00:03 +0000468 range = asn1constraint_compute_PER_range(expr_type, ct, type,
469 0, 0, strict_PER_visible);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000470 if(!range) return -1;
471
Lev Walkine8e87f12004-08-25 02:00:03 +0000472 if(range->incompatible
473 || (strict_PER_visible && range->not_PER_visible)) {
474 asn1constraint_range_free(range);
475 return 0;
476 }
477
Lev Walkin3140e0e2004-08-18 04:50:37 +0000478 switch(type) {
479 case ACT_CT_FROM: printf("(FROM("); break;
480 case ACT_CT_SIZE: printf("(SIZE("); break;
481 default: printf("("); break;
482 }
483 for(i = -1; i < range->el_count; i++) {
484 asn1cnst_range_t *r;
485 if(i == -1) {
486 if(range->el_count) continue;
487 r = range;
488 } else {
489 r = range->elements[i];
490 }
491 if(i > 0) {
492 printf(" | ");
493 }
494 asn1print_crange_value(&r->left, as_char);
495 if(r->left.type != r->right.type
496 || r->left.value != r->right.value) {
497 printf("..");
498 asn1print_crange_value(&r->right, as_char);
499 }
500 }
501 if(range->extensible)
502 printf(",...");
503 printf(type==ACT_EL_RANGE?")":"))");
504
505 if(range->empty_constraint)
506 printf(":Empty!");
507
508 asn1constraint_range_free(range);
509 return 0;
510}
511
512static int
513asn1print_constraint_explain(asn1p_expr_type_e expr_type,
Lev Walkine8e87f12004-08-25 02:00:03 +0000514 asn1p_constraint_t *ct, int s_PV) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000515
Lev Walkine8e87f12004-08-25 02:00:03 +0000516 asn1print_constraint_explain_type(expr_type, ct, ACT_EL_RANGE, s_PV);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000517 printf(" ");
Lev Walkine8e87f12004-08-25 02:00:03 +0000518 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_SIZE, s_PV);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000519 printf(" ");
Lev Walkine8e87f12004-08-25 02:00:03 +0000520 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_FROM, s_PV);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000521
522 return 0;
523}
524
525static int
526asn1print_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 +0000527 int SEQ_OF = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000528
Lev Walkinf4069d22005-02-15 07:06:05 +0000529 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
Lev Walkinf7484512004-10-13 09:13:56 +0000530 INDENT("-- #line %d\n", tc->_lineno);
Lev Walkinf15320b2004-06-03 03:38:44 +0000531 if(tc->Identifier)
532 INDENT("%s", tc->Identifier);
533
534 if(tc->params) {
535 asn1print_params(tc->params, flags);
536 }
537
538 if(tc->meta_type != AMT_VALUE
Lev Walkinc74ea222004-08-25 02:27:47 +0000539 && tc->meta_type != AMT_VALUESET
Lev Walkinf15320b2004-06-03 03:38:44 +0000540 && tc->expr_type != A1TC_EXTENSIBLE) {
541 if(level) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000542 if(tc->Identifier && !(flags & APF_NOINDENT))
Lev Walkinf15320b2004-06-03 03:38:44 +0000543 printf("\t");
544 } else {
545 printf(" ::=");
546 }
547 }
548
549 if(tc->tag.tag_class) {
550 printf(" ");
551 asn1print_tag(tc, flags);
552 }
553
554 switch(tc->expr_type) {
555 case A1TC_EXTENSIBLE:
556 if(tc->value) {
557 printf("!");
558 asn1print_value(tc->value, flags);
559 }
560 break;
Lev Walkinfd151ce2004-08-22 03:08:51 +0000561 case A1TC_COMPONENTS_OF:
562 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
563 printf(" COMPONENTS OF");
564 break;
Lev Walkinf4069d22005-02-15 07:06:05 +0000565 case A1TC_PARAMETRIZED:
566 flags |= APF_NOINDENT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000567 case A1TC_REFERENCE:
568 case A1TC_UNIVERVAL:
Lev Walkinf15320b2004-06-03 03:38:44 +0000569 break;
570 case A1TC_CLASSDEF:
571 printf(" CLASS");
572 break;
573 case A1TC_CLASSFIELD:
574 /* Nothing to print here */
575 break;
Lev Walkinb1ef3962004-08-20 13:24:28 +0000576 case ASN_CONSTR_SET_OF:
577 case ASN_CONSTR_SEQUENCE_OF:
578 SEQ_OF = 1;
579 if(tc->expr_type == ASN_CONSTR_SET_OF)
580 printf(" SET");
581 else
582 printf(" SEQUENCE");
583 if(tc->constraints) {
584 printf(" ");
585 asn1print_constraint(tc->constraints, flags);
586 }
587 printf(" OF");
588 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000589 default:
590 {
591 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
592 printf(" %s", p?p:"<unknown type!>");
593 }
594 break;
595 }
596
597 if(tc->reference) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000598 if(!(flags & APF_NOINDENT))
599 printf(" ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000600 asn1print_ref(tc->reference, flags);
601 }
602
Lev Walkinc74ea222004-08-25 02:27:47 +0000603 if(tc->meta_type == AMT_VALUESET)
604 printf(" ::=");
605
Lev Walkinf15320b2004-06-03 03:38:44 +0000606 /*
607 * Display the descendants (children) of the current type.
608 */
Lev Walkinc74ea222004-08-25 02:27:47 +0000609 if(TQ_FIRST(&(tc->members))
610 || (tc->expr_type & ASN_CONSTR_MASK)
611 || tc->meta_type == AMT_VALUESET
612 || tc->meta_type == AMT_OBJECT
613 || tc->meta_type == AMT_OBJECTSET
614 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000615 asn1p_expr_t *se; /* SubExpression */
Lev Walkinfd151ce2004-08-22 03:08:51 +0000616 int put_braces = !SEQ_OF; /* Don't need 'em, if SET OF... */
Lev Walkinf15320b2004-06-03 03:38:44 +0000617
Lev Walkinc74ea222004-08-25 02:27:47 +0000618 if(put_braces) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000619 if(flags & APF_NOINDENT) {
620 printf("{");
621 if(!TQ_FIRST(&tc->members))
622 printf("}");
623 } else {
624 printf(" {");
625 if(TQ_FIRST(&tc->members))
626 printf("\n");
627 else
628 printf(" }");
629 }
Lev Walkinc74ea222004-08-25 02:27:47 +0000630 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000631
632 TQ_FOR(se, &(tc->members), next) {
633 /*
Lev Walkinfd151ce2004-08-22 03:08:51 +0000634 * Print the expression as it were a stand-alone type.
Lev Walkinf15320b2004-06-03 03:38:44 +0000635 */
Lev Walkinf7484512004-10-13 09:13:56 +0000636 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinb1e07082004-09-15 11:44:55 +0000637 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
638 printf(" DEFAULT ");
639 asn1print_value(se->marker.default_value, flags);
640 } else if((se->marker.flags & EM_OPTIONAL)
641 == EM_OPTIONAL) {
Lev Walkin6e8da2b2004-09-10 08:21:27 +0000642 printf(" OPTIONAL");
Lev Walkinb1e07082004-09-15 11:44:55 +0000643 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000644 if(TQ_NEXT(se, next)) {
645 printf(",");
Lev Walkinf4069d22005-02-15 07:06:05 +0000646 if(!(flags & APF_NOINDENT))
647 INDENT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000648 }
649 }
650
Lev Walkinc74ea222004-08-25 02:27:47 +0000651 if(put_braces && TQ_FIRST(&tc->members)) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000652 if(!(flags & APF_NOINDENT))
653 printf("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000654 INDENT("}");
655 }
656 }
657
658 if(tc->with_syntax)
659 asn1print_with_syntax(tc->with_syntax, flags);
660
Lev Walkinb1ef3962004-08-20 13:24:28 +0000661 if(!SEQ_OF && tc->constraints) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000662 printf(" ");
663 asn1print_constraint(tc->constraints, flags);
664 }
Lev Walkin3140e0e2004-08-18 04:50:37 +0000665
Lev Walkinf15320b2004-06-03 03:38:44 +0000666 if(tc->unique) {
667 printf(" UNIQUE");
668 }
669
670 if(tc->meta_type == AMT_VALUE
671 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin0056aec2004-09-05 10:38:50 +0000672 if(tc->expr_type == A1TC_UNIVERVAL) {
Lev Walkinb1e07082004-09-15 11:44:55 +0000673 if(tc->value) {
674 printf("(");
675 asn1print_value(tc->value, flags);
676 printf(")");
677 }
Lev Walkin0056aec2004-09-05 10:38:50 +0000678 } else {
679 printf(" ::= ");
680 asn1print_value(tc->value, flags);
681 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000682 }
683
Lev Walkinfd151ce2004-08-22 03:08:51 +0000684 /*
Lev Walkinf4069d22005-02-15 07:06:05 +0000685 * The following section exists entirely for debugging.
Lev Walkinfd151ce2004-08-22 03:08:51 +0000686 */
Lev Walkinb1ef3962004-08-20 13:24:28 +0000687 if(flags & APF_DEBUG_CONSTRAINTS
688 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000689 asn1p_expr_t *top_parent;
690
691 if(tc->combined_constraints) {
692 printf("\n-- Combined constraints: ");
693 asn1print_constraint(tc->combined_constraints, flags);
694 }
695
Lev Walkine4d6ab82004-09-22 16:05:13 +0000696 top_parent = asn1f_find_terminal_type_ex(asn, tc);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000697 if(top_parent) {
Lev Walkine8e87f12004-08-25 02:00:03 +0000698 printf("\n-- Practical constraints (%s): ",
699 top_parent->Identifier);
700 asn1print_constraint_explain(top_parent->expr_type,
701 tc->combined_constraints, 0);
Lev Walkinb1ef3962004-08-20 13:24:28 +0000702 printf("\n-- PER-visible constraints (%s): ",
703 top_parent->Identifier);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000704 asn1print_constraint_explain(top_parent->expr_type,
Lev Walkine8e87f12004-08-25 02:00:03 +0000705 tc->combined_constraints, 1);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000706 }
Lev Walkinb1ef3962004-08-20 13:24:28 +0000707 printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000708 }
709
Lev Walkinf15320b2004-06-03 03:38:44 +0000710 return 0;
711}
Lev Walkin3140e0e2004-08-18 04:50:37 +0000712
Lev Walkinf7484512004-10-13 09:13:56 +0000713
714static int
715asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
716 asn1p_expr_t *se;
717 int expr_unordered = 0;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000718 int dont_involve_children = 0;
Lev Walkinf7484512004-10-13 09:13:56 +0000719
720 switch(expr->meta_type) {
721 case AMT_TYPE:
722 case AMT_TYPEREF:
723 break;
724 default:
725 if(expr->expr_type == A1TC_UNIVERVAL)
726 break;
727 return 0;
728 }
729
730 if(!expr->Identifier) return 0;
731
Lev Walkinf7484512004-10-13 09:13:56 +0000732 if(flags & APF_LINE_COMMENTS)
733 INDENT("<!-- #line %d -->\n", expr->_lineno);
734 INDENT("<!ELEMENT %s", expr->Identifier);
735
736 if(expr->expr_type == A1TC_REFERENCE) {
737 se = asn1f_find_terminal_type_ex(asn, expr);
738 if(!se) {
Lev Walkinef39f7e2004-10-14 05:11:25 +0000739 printf(" (ANY)");
Lev Walkinf7484512004-10-13 09:13:56 +0000740 return 0;
741 }
742 expr = se;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000743 dont_involve_children = 1;
Lev Walkinf7484512004-10-13 09:13:56 +0000744 }
745
Lev Walkind7963aa2005-03-10 15:16:56 +0000746 if(expr->expr_type == ASN_CONSTR_CHOICE
747 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
748 || expr->expr_type == ASN_CONSTR_SET_OF
749 || expr->expr_type == ASN_CONSTR_SET
Lev Walkin112d69e2005-03-09 20:52:15 +0000750 || expr->expr_type == ASN_BASIC_INTEGER
751 || expr->expr_type == ASN_BASIC_ENUMERATED) {
752 expr_unordered = 1;
753 }
754
Lev Walkinf7484512004-10-13 09:13:56 +0000755 if(TQ_FIRST(&expr->members)) {
756 int extensible = 0;
757 printf(" (");
758 TQ_FOR(se, &(expr->members), next) {
759 if(se->expr_type == A1TC_EXTENSIBLE) {
760 extensible = 1;
761 continue;
762 } else if(!se->Identifier
763 && se->expr_type == A1TC_REFERENCE) {
764 asn1print_ref(se->reference, flags);
765 } else if(se->Identifier) {
766 printf("%s", se->Identifier);
767 } else {
768 printf("ANY");
769 }
770 if(expr->expr_type != ASN_CONSTR_SET
771 && expr->expr_type != ASN_CONSTR_CHOICE
772 && expr->expr_type != ASN_BASIC_INTEGER
773 && expr->expr_type != ASN_BASIC_ENUMERATED) {
774 if(expr_unordered)
775 printf("*");
776 else if(se->marker.flags)
777 printf("?");
778 }
779 if(TQ_NEXT(se, next)
780 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
781 printf(expr_unordered?"|":", ");
782 }
783 }
784 if(extensible) {
785 printf(expr_unordered?"|":", ");
786 printf("ANY");
787 if(expr->expr_type != ASN_CONSTR_SET
788 && expr->expr_type != ASN_CONSTR_CHOICE
789 && expr->expr_type != ASN_BASIC_INTEGER
790 && expr->expr_type != ASN_BASIC_ENUMERATED)
791 printf("*");
792 }
793
794 printf(")");
795 if(expr->expr_type == ASN_CONSTR_SET)
796 printf("*");
797
Lev Walkinef39f7e2004-10-14 05:11:25 +0000798 } else switch(expr->expr_type) {
799 case ASN_BASIC_BOOLEAN:
Lev Walkin112d69e2005-03-09 20:52:15 +0000800 printf(" (true|false)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000801 break;
802 case ASN_CONSTR_CHOICE:
803 case ASN_CONSTR_SET:
804 case ASN_CONSTR_SET_OF:
805 case ASN_CONSTR_SEQUENCE:
806 case ASN_CONSTR_SEQUENCE_OF:
807 case ASN_BASIC_NULL:
808 case A1TC_UNIVERVAL:
Lev Walkinf7484512004-10-13 09:13:56 +0000809 printf(" EMPTY");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000810 break;
811 case ASN_TYPE_ANY:
812 printf(" ANY");
813 break;
814 case ASN_BASIC_BIT_STRING:
815 case ASN_BASIC_OCTET_STRING:
816 case ASN_BASIC_OBJECT_IDENTIFIER:
817 case ASN_BASIC_RELATIVE_OID:
Lev Walkincc116532004-10-15 06:01:54 +0000818 case ASN_BASIC_INTEGER:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000819 case ASN_BASIC_UTCTime:
820 case ASN_BASIC_GeneralizedTime:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000821 case ASN_STRING_NumericString:
822 case ASN_STRING_PrintableString:
Lev Walkincc116532004-10-15 06:01:54 +0000823 printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000824 break;
825 case ASN_STRING_VisibleString:
826 case ASN_STRING_ISO646String:
827 /* Entity references, but not XML elements may be present */
Lev Walkinf7484512004-10-13 09:13:56 +0000828 printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000829 break;
830 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
831 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
832 default:
833 /*
834 * XML elements are allowed.
835 * For example, a UTF8String may contain "<bel/>".
836 */
837 printf(" ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000838 }
839 printf(">\n");
840
841 /*
842 * Display the descendants (children) of the current type.
843 */
Lev Walkinef39f7e2004-10-14 05:11:25 +0000844 if(!dont_involve_children) {
845 TQ_FOR(se, &(expr->members), next) {
846 if(se->expr_type == A1TC_EXTENSIBLE) continue;
847 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
848 }
Lev Walkinf7484512004-10-13 09:13:56 +0000849 }
850
851 return 0;
852}