blob: 01e21ead652b29f57c8edd294222a7e8c6012b6c [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 Walkin5b7eeaf2005-03-28 17:52:43 +0000129 if(accum + strlen(arcname ? arcname : "") > 72) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 printf("\n\t");
Lev Walkin5b7eeaf2005-03-28 17:52:43 +0000131 accum = 8;
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 Walkin71160962005-06-02 05:21:53 +0000171 printf("%s", asn1p_tag2string(tag, 0));
Lev Walkinf15320b2004-06-03 03:38:44 +0000172
173 return 0;
174}
175
176static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000177asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000178
179 if(val == NULL)
180 return 0;
181
182 switch(val->type) {
183 case ATV_NOVALUE:
184 break;
Lev Walkinb1e07082004-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;
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 case ATV_INTEGER:
Lev Walkin33c16ba2004-09-24 21:01:43 +0000192 printf("%" PRIdASN, val->value.v_integer);
Lev Walkinf15320b2004-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;
Lev Walkin1e448d32005-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;
Lev Walkinf15320b2004-06-03 03:38:44 +0000211 case ATV_STRING:
212 {
Lev Walkin84fbd722005-06-15 18:41:50 +0000213 char *p = (char *)val->value.string.buf;
Lev Walkinf15320b2004-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:
Lev Walkin84fbd722005-06-15 18:41:50 +0000229 fputs((char *)val->value.string.buf, stdout);
Lev Walkinf15320b2004-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 }
Lev Walkin043af0d2005-02-24 21:07:35 +0000256 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000257 }
Lev Walkinb1e07082004-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);
Lev Walkinf15320b2004-06-03 03:38:44 +0000263 }
264
265 assert(val->type || !"Unknown");
266
267 return 0;
268}
269
270static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000271asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
Lev Walkinf15320b2004-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) {
Lev Walkinff7dd142005-03-20 12:58:00 +0000280 case ACT_EL_TYPE:
281 asn1print_value(ct->value, flags);
282 break;
Lev Walkinf15320b2004-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) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000306 case ACT_CT_SIZE: printf("SIZE("); break;
307 case ACT_CT_FROM: printf("FROM("); break;
Lev Walkinf15320b2004-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:
Lev Walkine596bf02005-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(")");
Lev Walkin8638f0d2005-03-28 07:50:06 +0000321 break;
Lev Walkine596bf02005-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 }
Lev Walkin8638f0d2005-03-28 07:50:06 +0000346 break;
Lev Walkin1893ddf2005-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);
Lev Walkinf15320b2004-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 {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000360 char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",",
Lev Walkinf15320b2004-06-03 03:38:44 +0000361 "", "(" };
Lev Walkin0056aec2004-09-05 10:38:50 +0000362 unsigned int i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000363 for(i = 0; i < ct->el_count; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000364 if(i) fputs(symtable[symno], stdout);
365 if(ct->type == ACT_CA_CRC) fputs("{", stdout);
Lev Walkin1e448d32005-03-24 14:26:38 +0000366 asn1print_constraint(ct->elements[i], flags);
Lev Walkinf15320b2004-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;
Lev Walkin1e448d32005-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;
Lev Walkinf15320b2004-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
Lev Walkin3140e0e2004-08-18 04:50:37 +0000391asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags flags) {
Lev Walkinf15320b2004-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
Lev Walkin3140e0e2004-08-18 04:50:37 +0000410asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
Lev Walkinf15320b2004-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
Lev Walkin3140e0e2004-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 {
Lev Walkin33c16ba2004-09-24 21:01:43 +0000436 printf("%" PRIdASN, edge->value);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000437 }
438 }
439 return 0;
440}
441
442static int
Lev Walkine8e87f12004-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) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000444 asn1cnst_range_t *range;
445 int as_char = (type==ACT_CT_FROM);
446 int i;
447
Lev Walkine8e87f12004-08-25 02:00:03 +0000448 range = asn1constraint_compute_PER_range(expr_type, ct, type,
449 0, 0, strict_PER_visible);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000450 if(!range) return -1;
451
Lev Walkine8e87f12004-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
Lev Walkin3140e0e2004-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,
Lev Walkine8e87f12004-08-25 02:00:03 +0000494 asn1p_constraint_t *ct, int s_PV) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000495
Lev Walkine8e87f12004-08-25 02:00:03 +0000496 asn1print_constraint_explain_type(expr_type, ct, ACT_EL_RANGE, s_PV);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000497 printf(" ");
Lev Walkine8e87f12004-08-25 02:00:03 +0000498 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_SIZE, s_PV);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000499 printf(" ");
Lev Walkine8e87f12004-08-25 02:00:03 +0000500 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_FROM, s_PV);
Lev Walkin3140e0e2004-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) {
Lev Walkinb1ef3962004-08-20 13:24:28 +0000507 int SEQ_OF = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000508
Lev Walkinf4069d22005-02-15 07:06:05 +0000509 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
Lev Walkinf7484512004-10-13 09:13:56 +0000510 INDENT("-- #line %d\n", tc->_lineno);
Lev Walkinf15320b2004-06-03 03:38:44 +0000511 if(tc->Identifier)
512 INDENT("%s", tc->Identifier);
513
514 if(tc->params) {
515 asn1print_params(tc->params, flags);
516 }
517
518 if(tc->meta_type != AMT_VALUE
Lev Walkinc74ea222004-08-25 02:27:47 +0000519 && tc->meta_type != AMT_VALUESET
Lev Walkinf15320b2004-06-03 03:38:44 +0000520 && tc->expr_type != A1TC_EXTENSIBLE) {
521 if(level) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000522 if(tc->Identifier && !(flags & APF_NOINDENT))
Lev Walkinf15320b2004-06-03 03:38:44 +0000523 printf("\t");
524 } else {
525 printf(" ::=");
526 }
527 }
528
529 if(tc->tag.tag_class) {
530 printf(" ");
531 asn1print_tag(tc, flags);
532 }
533
534 switch(tc->expr_type) {
535 case A1TC_EXTENSIBLE:
536 if(tc->value) {
537 printf("!");
538 asn1print_value(tc->value, flags);
539 }
540 break;
Lev Walkinfd151ce2004-08-22 03:08:51 +0000541 case A1TC_COMPONENTS_OF:
542 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
543 printf(" COMPONENTS OF");
544 break;
Lev Walkinf4069d22005-02-15 07:06:05 +0000545 case A1TC_PARAMETRIZED:
546 flags |= APF_NOINDENT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000547 case A1TC_REFERENCE:
548 case A1TC_UNIVERVAL:
Lev Walkinf15320b2004-06-03 03:38:44 +0000549 break;
550 case A1TC_CLASSDEF:
551 printf(" CLASS");
552 break;
553 case A1TC_CLASSFIELD:
554 /* Nothing to print here */
555 break;
Lev Walkinb1ef3962004-08-20 13:24:28 +0000556 case ASN_CONSTR_SET_OF:
557 case ASN_CONSTR_SEQUENCE_OF:
558 SEQ_OF = 1;
559 if(tc->expr_type == ASN_CONSTR_SET_OF)
560 printf(" SET");
561 else
562 printf(" SEQUENCE");
563 if(tc->constraints) {
564 printf(" ");
565 asn1print_constraint(tc->constraints, flags);
566 }
567 printf(" OF");
568 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000569 default:
570 {
571 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
572 printf(" %s", p?p:"<unknown type!>");
573 }
574 break;
575 }
576
Lev Walkinb9d0ae32005-06-02 04:06:24 +0000577 /*
578 * Put the name of the referred type.
579 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000580 if(tc->reference) {
Lev Walkinb9d0ae32005-06-02 04:06:24 +0000581 printf(" ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000582 asn1print_ref(tc->reference, flags);
583 }
584
Lev Walkinc74ea222004-08-25 02:27:47 +0000585 if(tc->meta_type == AMT_VALUESET)
586 printf(" ::=");
587
Lev Walkinf15320b2004-06-03 03:38:44 +0000588 /*
589 * Display the descendants (children) of the current type.
590 */
Lev Walkinc74ea222004-08-25 02:27:47 +0000591 if(TQ_FIRST(&(tc->members))
592 || (tc->expr_type & ASN_CONSTR_MASK)
593 || tc->meta_type == AMT_VALUESET
594 || tc->meta_type == AMT_OBJECT
595 || tc->meta_type == AMT_OBJECTSET
596 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000597 asn1p_expr_t *se; /* SubExpression */
Lev Walkinfd151ce2004-08-22 03:08:51 +0000598 int put_braces = !SEQ_OF; /* Don't need 'em, if SET OF... */
Lev Walkinf15320b2004-06-03 03:38:44 +0000599
Lev Walkinc74ea222004-08-25 02:27:47 +0000600 if(put_braces) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000601 if(flags & APF_NOINDENT) {
602 printf("{");
603 if(!TQ_FIRST(&tc->members))
604 printf("}");
605 } else {
606 printf(" {");
607 if(TQ_FIRST(&tc->members))
608 printf("\n");
609 else
610 printf(" }");
611 }
Lev Walkinc74ea222004-08-25 02:27:47 +0000612 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000613
614 TQ_FOR(se, &(tc->members), next) {
615 /*
Lev Walkinfd151ce2004-08-22 03:08:51 +0000616 * Print the expression as it were a stand-alone type.
Lev Walkinf15320b2004-06-03 03:38:44 +0000617 */
Lev Walkinf7484512004-10-13 09:13:56 +0000618 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinb1e07082004-09-15 11:44:55 +0000619 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
620 printf(" DEFAULT ");
621 asn1print_value(se->marker.default_value, flags);
622 } else if((se->marker.flags & EM_OPTIONAL)
623 == EM_OPTIONAL) {
Lev Walkin6e8da2b2004-09-10 08:21:27 +0000624 printf(" OPTIONAL");
Lev Walkinb1e07082004-09-15 11:44:55 +0000625 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000626 if(TQ_NEXT(se, next)) {
627 printf(",");
Lev Walkinf4069d22005-02-15 07:06:05 +0000628 if(!(flags & APF_NOINDENT))
629 INDENT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000630 }
631 }
632
Lev Walkinc74ea222004-08-25 02:27:47 +0000633 if(put_braces && TQ_FIRST(&tc->members)) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000634 if(!(flags & APF_NOINDENT))
635 printf("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000636 INDENT("}");
637 }
638 }
639
640 if(tc->with_syntax)
641 asn1print_with_syntax(tc->with_syntax, flags);
642
Lev Walkinb1ef3962004-08-20 13:24:28 +0000643 if(!SEQ_OF && tc->constraints) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000644 printf(" ");
645 asn1print_constraint(tc->constraints, flags);
646 }
Lev Walkin3140e0e2004-08-18 04:50:37 +0000647
Lev Walkinf15320b2004-06-03 03:38:44 +0000648 if(tc->unique) {
649 printf(" UNIQUE");
650 }
651
652 if(tc->meta_type == AMT_VALUE
653 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin0056aec2004-09-05 10:38:50 +0000654 if(tc->expr_type == A1TC_UNIVERVAL) {
Lev Walkinb1e07082004-09-15 11:44:55 +0000655 if(tc->value) {
656 printf("(");
657 asn1print_value(tc->value, flags);
658 printf(")");
659 }
Lev Walkin0056aec2004-09-05 10:38:50 +0000660 } else {
661 printf(" ::= ");
662 asn1print_value(tc->value, flags);
663 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000664 }
665
Lev Walkinfd151ce2004-08-22 03:08:51 +0000666 /*
Lev Walkinf4069d22005-02-15 07:06:05 +0000667 * The following section exists entirely for debugging.
Lev Walkinfd151ce2004-08-22 03:08:51 +0000668 */
Lev Walkinb1ef3962004-08-20 13:24:28 +0000669 if(flags & APF_DEBUG_CONSTRAINTS
670 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000671 asn1p_expr_t *top_parent;
672
673 if(tc->combined_constraints) {
674 printf("\n-- Combined constraints: ");
675 asn1print_constraint(tc->combined_constraints, flags);
676 }
677
Lev Walkine4d6ab82004-09-22 16:05:13 +0000678 top_parent = asn1f_find_terminal_type_ex(asn, tc);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000679 if(top_parent) {
Lev Walkine8e87f12004-08-25 02:00:03 +0000680 printf("\n-- Practical constraints (%s): ",
681 top_parent->Identifier);
682 asn1print_constraint_explain(top_parent->expr_type,
683 tc->combined_constraints, 0);
Lev Walkinb1ef3962004-08-20 13:24:28 +0000684 printf("\n-- PER-visible constraints (%s): ",
685 top_parent->Identifier);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000686 asn1print_constraint_explain(top_parent->expr_type,
Lev Walkine8e87f12004-08-25 02:00:03 +0000687 tc->combined_constraints, 1);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000688 }
Lev Walkinb1ef3962004-08-20 13:24:28 +0000689 printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000690 }
691
Lev Walkinf15320b2004-06-03 03:38:44 +0000692 return 0;
693}
Lev Walkin3140e0e2004-08-18 04:50:37 +0000694
Lev Walkinf7484512004-10-13 09:13:56 +0000695
696static int
697asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
698 asn1p_expr_t *se;
699 int expr_unordered = 0;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000700 int dont_involve_children = 0;
Lev Walkinf7484512004-10-13 09:13:56 +0000701
702 switch(expr->meta_type) {
703 case AMT_TYPE:
704 case AMT_TYPEREF:
705 break;
706 default:
707 if(expr->expr_type == A1TC_UNIVERVAL)
708 break;
709 return 0;
710 }
711
712 if(!expr->Identifier) return 0;
713
Lev Walkinf7484512004-10-13 09:13:56 +0000714 if(flags & APF_LINE_COMMENTS)
715 INDENT("<!-- #line %d -->\n", expr->_lineno);
716 INDENT("<!ELEMENT %s", expr->Identifier);
717
718 if(expr->expr_type == A1TC_REFERENCE) {
719 se = asn1f_find_terminal_type_ex(asn, expr);
720 if(!se) {
Lev Walkinef39f7e2004-10-14 05:11:25 +0000721 printf(" (ANY)");
Lev Walkinf7484512004-10-13 09:13:56 +0000722 return 0;
723 }
724 expr = se;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000725 dont_involve_children = 1;
Lev Walkinf7484512004-10-13 09:13:56 +0000726 }
727
Lev Walkind7963aa2005-03-10 15:16:56 +0000728 if(expr->expr_type == ASN_CONSTR_CHOICE
729 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
730 || expr->expr_type == ASN_CONSTR_SET_OF
731 || expr->expr_type == ASN_CONSTR_SET
Lev Walkin112d69e2005-03-09 20:52:15 +0000732 || expr->expr_type == ASN_BASIC_INTEGER
733 || expr->expr_type == ASN_BASIC_ENUMERATED) {
734 expr_unordered = 1;
735 }
736
Lev Walkinf7484512004-10-13 09:13:56 +0000737 if(TQ_FIRST(&expr->members)) {
738 int extensible = 0;
739 printf(" (");
740 TQ_FOR(se, &(expr->members), next) {
741 if(se->expr_type == A1TC_EXTENSIBLE) {
742 extensible = 1;
743 continue;
744 } else if(!se->Identifier
745 && se->expr_type == A1TC_REFERENCE) {
746 asn1print_ref(se->reference, flags);
747 } else if(se->Identifier) {
748 printf("%s", se->Identifier);
749 } else {
750 printf("ANY");
751 }
752 if(expr->expr_type != ASN_CONSTR_SET
753 && expr->expr_type != ASN_CONSTR_CHOICE
754 && expr->expr_type != ASN_BASIC_INTEGER
755 && expr->expr_type != ASN_BASIC_ENUMERATED) {
756 if(expr_unordered)
757 printf("*");
758 else if(se->marker.flags)
759 printf("?");
760 }
761 if(TQ_NEXT(se, next)
762 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
763 printf(expr_unordered?"|":", ");
764 }
765 }
766 if(extensible) {
767 printf(expr_unordered?"|":", ");
768 printf("ANY");
769 if(expr->expr_type != ASN_CONSTR_SET
770 && expr->expr_type != ASN_CONSTR_CHOICE
771 && expr->expr_type != ASN_BASIC_INTEGER
772 && expr->expr_type != ASN_BASIC_ENUMERATED)
773 printf("*");
774 }
775
776 printf(")");
777 if(expr->expr_type == ASN_CONSTR_SET)
778 printf("*");
779
Lev Walkinef39f7e2004-10-14 05:11:25 +0000780 } else switch(expr->expr_type) {
781 case ASN_BASIC_BOOLEAN:
Lev Walkin112d69e2005-03-09 20:52:15 +0000782 printf(" (true|false)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000783 break;
784 case ASN_CONSTR_CHOICE:
785 case ASN_CONSTR_SET:
786 case ASN_CONSTR_SET_OF:
787 case ASN_CONSTR_SEQUENCE:
788 case ASN_CONSTR_SEQUENCE_OF:
789 case ASN_BASIC_NULL:
790 case A1TC_UNIVERVAL:
Lev Walkinf7484512004-10-13 09:13:56 +0000791 printf(" EMPTY");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000792 break;
793 case ASN_TYPE_ANY:
794 printf(" ANY");
795 break;
796 case ASN_BASIC_BIT_STRING:
797 case ASN_BASIC_OCTET_STRING:
798 case ASN_BASIC_OBJECT_IDENTIFIER:
799 case ASN_BASIC_RELATIVE_OID:
Lev Walkincc116532004-10-15 06:01:54 +0000800 case ASN_BASIC_INTEGER:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000801 case ASN_BASIC_UTCTime:
802 case ASN_BASIC_GeneralizedTime:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000803 case ASN_STRING_NumericString:
804 case ASN_STRING_PrintableString:
Lev Walkincc116532004-10-15 06:01:54 +0000805 printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000806 break;
807 case ASN_STRING_VisibleString:
808 case ASN_STRING_ISO646String:
809 /* Entity references, but not XML elements may be present */
Lev Walkinf7484512004-10-13 09:13:56 +0000810 printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000811 break;
812 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
813 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
814 default:
815 /*
816 * XML elements are allowed.
817 * For example, a UTF8String may contain "<bel/>".
818 */
819 printf(" ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000820 }
821 printf(">\n");
822
823 /*
824 * Display the descendants (children) of the current type.
825 */
Lev Walkinef39f7e2004-10-14 05:11:25 +0000826 if(!dont_involve_children) {
827 TQ_FOR(se, &(expr->members), next) {
828 if(se->expr_type == A1TC_EXTENSIBLE) continue;
829 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
830 }
Lev Walkinf7484512004-10-13 09:13:56 +0000831 }
832
833 return 0;
834}