blob: 47cfb1f888ea63fed11eb2b690cc0e38bc45a97c [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 Walkin6b3ff542006-03-06 14:51:00 +000048 if(mod->_tags & MT_STANDARD_MODULE)
49 return 0; /* Ignore modules imported from skeletons */
Lev Walkinc70c2ef2004-09-30 06:38:21 +000050 if(modno++) printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +000051 asn1print_module(asn, mod, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +000052 }
53
Lev Walkin112d69e2005-03-09 20:52:15 +000054 if(flags & APF_PRINT_XML_DTD) {
55 /* Values for BOOLEAN */
56 printf("<!ELEMENT true EMPTY>\n");
57 printf("<!ELEMENT false EMPTY>\n");
58 }
59
Lev Walkinf15320b2004-06-03 03:38:44 +000060 return 0;
61}
62
63static int
Lev Walkin3140e0e2004-08-18 04:50:37 +000064asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +000065 asn1p_expr_t *tc;
66
Lev Walkinf7484512004-10-13 09:13:56 +000067 if(flags & APF_PRINT_XML_DTD)
68 printf("<!-- ASN.1 module\n");
69
Lev Walkinb36317c2005-08-12 10:09:10 +000070 printf("%s ", mod->ModuleName);
Lev Walkinf15320b2004-06-03 03:38:44 +000071 if(mod->module_oid) {
Lev Walkinb36317c2005-08-12 10:09:10 +000072 asn1print_oid(strlen(mod->ModuleName), mod->module_oid, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +000073 printf("\n");
74 }
75
Lev Walkinf7484512004-10-13 09:13:56 +000076 if(flags & APF_PRINT_XML_DTD) {
77 if(mod->source_file_name
78 && strcmp(mod->source_file_name, "-"))
79 printf("found in %s", mod->source_file_name);
80 printf(" -->\n\n");
81
82 TQ_FOR(tc, &(mod->members), next) {
83 asn1print_expr_dtd(asn, mod, tc, flags, 0);
84 }
85
86 return 0;
87 }
88
Lev Walkinf15320b2004-06-03 03:38:44 +000089 printf("DEFINITIONS");
90
Lev Walkin3140e0e2004-08-18 04:50:37 +000091 if(mod->module_flags & MSF_TAG_INSTRUCTIONS)
92 printf(" TAG INSTRUCTIONS");
93 if(mod->module_flags & MSF_XER_INSTRUCTIONS)
94 printf(" XER INSTRUCTIONS");
Lev Walkinf15320b2004-06-03 03:38:44 +000095 if(mod->module_flags & MSF_EXPLICIT_TAGS)
96 printf(" EXPLICIT TAGS");
97 if(mod->module_flags & MSF_IMPLICIT_TAGS)
98 printf(" IMPLICIT TAGS");
99 if(mod->module_flags & MSF_AUTOMATIC_TAGS)
100 printf(" AUTOMATIC TAGS");
101 if(mod->module_flags & MSF_EXTENSIBILITY_IMPLIED)
102 printf(" EXTENSIBILITY IMPLIED");
103
104 printf(" ::=\n");
105 printf("BEGIN\n\n");
106
107 TQ_FOR(tc, &(mod->members), next) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000108 asn1print_expr(asn, mod, tc, flags, 0);
Lev Walkind370e9f2006-03-16 10:03:35 +0000109 if(flags & APF_PRINT_CONSTRAINTS)
Lev Walkinb1ef3962004-08-20 13:24:28 +0000110 printf("\n");
111 else
112 printf("\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000113 }
114
115 printf("END\n");
116
117 return 0;
118}
119
120static int
Lev Walkind6db8022005-03-18 03:53:05 +0000121asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags) {
122 size_t accum = prior_len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000123 int ac;
Lev Walkinf15320b2004-06-03 03:38:44 +0000124
Lev Walkind9bd7752004-06-05 08:17:50 +0000125 (void)flags; /* Unused argument */
126
Lev Walkinf15320b2004-06-03 03:38:44 +0000127 printf("{");
128 for(ac = 0; ac < oid->arcs_count; ac++) {
Lev Walkin4efbfb72005-02-25 14:20:30 +0000129 const char *arcname = oid->arcs[ac].name;
130
Lev Walkin5b7eeaf2005-03-28 17:52:43 +0000131 if(accum + strlen(arcname ? arcname : "") > 72) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000132 printf("\n\t");
Lev Walkin5b7eeaf2005-03-28 17:52:43 +0000133 accum = 8;
Lev Walkind6db8022005-03-18 03:53:05 +0000134 } else {
135 accum += printf(" ");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000136 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000137
Lev Walkin4efbfb72005-02-25 14:20:30 +0000138 if(arcname) {
Lev Walkind6db8022005-03-18 03:53:05 +0000139 accum += printf("%s", arcname);
Lev Walkin0056aec2004-09-05 10:38:50 +0000140 if(oid->arcs[ac].number >= 0) {
Lev Walkind6db8022005-03-18 03:53:05 +0000141 accum += printf("(%" PRIdASN ")",
142 oid->arcs[ac].number);
Lev Walkin0056aec2004-09-05 10:38:50 +0000143 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 } else {
Lev Walkind6db8022005-03-18 03:53:05 +0000145 accum += printf("%" PRIdASN, oid->arcs[ac].number);
Lev Walkinf15320b2004-06-03 03:38:44 +0000146 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000147 }
148 printf(" }");
149
150 return 0;
151}
152
153static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000154asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000155 int cc;
156
Lev Walkind9bd7752004-06-05 08:17:50 +0000157 (void)flags; /* Unused argument */
158
Lev Walkinf15320b2004-06-03 03:38:44 +0000159 for(cc = 0; cc < ref->comp_count; cc++) {
160 if(cc) printf(".");
161 printf("%s", ref->components[cc].name);
162 }
163
164 return 0;
165}
166
167static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000168asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000169 struct asn1p_type_tag_s *tag = &tc->tag;
170
Lev Walkind9bd7752004-06-05 08:17:50 +0000171 (void)flags; /* Unused argument */
172
Lev Walkin71160962005-06-02 05:21:53 +0000173 printf("%s", asn1p_tag2string(tag, 0));
Lev Walkinf15320b2004-06-03 03:38:44 +0000174
175 return 0;
176}
177
178static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000179asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000180
181 if(val == NULL)
182 return 0;
183
184 switch(val->type) {
185 case ATV_NOVALUE:
186 break;
Lev Walkinb1e07082004-09-15 11:44:55 +0000187 case ATV_NULL:
188 printf("NULL");
189 return 0;
190 case ATV_REAL:
191 printf("%f", val->value.v_double);
192 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 case ATV_INTEGER:
Lev Walkin33c16ba2004-09-24 21:01:43 +0000194 printf("%" PRIdASN, val->value.v_integer);
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 return 0;
196 case ATV_MIN: printf("MIN"); return 0;
197 case ATV_MAX: printf("MAX"); return 0;
198 case ATV_FALSE: printf("FALSE"); return 0;
199 case ATV_TRUE: printf("TRUE"); return 0;
Lev Walkin1e448d32005-03-24 14:26:38 +0000200 case ATV_TUPLE:
201 printf("{%d, %d}",
202 (int)(val->value.v_integer >> 4),
203 (int)(val->value.v_integer & 0x0f));
204 return 0;
205 case ATV_QUADRUPLE:
206 printf("{%d, %d, %d, %d}",
207 (int)((val->value.v_integer >> 24) & 0xff),
208 (int)((val->value.v_integer >> 16) & 0xff),
209 (int)((val->value.v_integer >> 8) & 0xff),
210 (int)((val->value.v_integer >> 0) & 0xff)
211 );
212 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000213 case ATV_STRING:
214 {
Lev Walkin84fbd722005-06-15 18:41:50 +0000215 char *p = (char *)val->value.string.buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000216 putchar('"');
217 if(strchr(p, '"')) {
218 /* Mask quotes */
219 for(; *p; p++) {
220 if(*p == '"')
221 putchar(*p);
222 putchar(*p);
223 }
224 } else {
225 fputs(p, stdout);
226 }
227 putchar('"');
228 }
229 return 0;
230 case ATV_UNPARSED:
Lev Walkin84fbd722005-06-15 18:41:50 +0000231 fputs((char *)val->value.string.buf, stdout);
Lev Walkinf15320b2004-06-03 03:38:44 +0000232 return 0;
233 case ATV_BITVECTOR:
234 {
235 uint8_t *bitvector;
236 int bits;
237 int i;
238
239 bitvector = val->value.binary_vector.bits;
240 bits = val->value.binary_vector.size_in_bits;
241
242 printf("'");
243 if(bits%8) {
244 for(i = 0; i < bits; i++) {
245 uint8_t uc;
246 uc = bitvector[i>>3];
247 putchar(((uc >> (7-(i%8)))&1)?'1':'0');
248 }
249 printf("'B");
250 } else {
251 char hextable[16] = "0123456789ABCDEF";
252 for(i = 0; i < (bits>>3); i++) {
253 putchar(hextable[bitvector[i] >> 4]);
254 putchar(hextable[bitvector[i] & 0x0f]);
255 }
256 printf("'H");
257 }
Lev Walkin043af0d2005-02-24 21:07:35 +0000258 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000259 }
Lev Walkinb1e07082004-09-15 11:44:55 +0000260 case ATV_REFERENCED:
261 return asn1print_ref(val->value.reference, flags);
Lev Walkin5045dfa2006-03-21 09:41:28 +0000262 case ATV_VALUESET:
263 return asn1print_constraint(val->value.constraint, flags);
Lev Walkinb1e07082004-09-15 11:44:55 +0000264 case ATV_CHOICE_IDENTIFIER:
265 printf("%s: ", val->value.choice_identifier.identifier);
266 return asn1print_value(val->value.choice_identifier.value, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000267 }
268
269 assert(val->type || !"Unknown");
270
271 return 0;
272}
273
274static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000275asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000276 int symno = 0;
277
278 if(ct == 0) return 0;
279
280 if(ct->type == ACT_CA_SET)
281 printf("(");
282
283 switch(ct->type) {
Lev Walkinff7dd142005-03-20 12:58:00 +0000284 case ACT_EL_TYPE:
Lev Walkin5045dfa2006-03-21 09:41:28 +0000285 asn1print_value(ct->containedSubtype, flags);
Lev Walkinff7dd142005-03-20 12:58:00 +0000286 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000287 case ACT_EL_VALUE:
288 asn1print_value(ct->value, flags);
289 break;
290 case ACT_EL_RANGE:
291 case ACT_EL_LLRANGE:
292 case ACT_EL_RLRANGE:
293 case ACT_EL_ULRANGE:
294 asn1print_value(ct->range_start, flags);
295 switch(ct->type) {
296 case ACT_EL_RANGE: printf(".."); break;
297 case ACT_EL_LLRANGE: printf("<.."); break;
298 case ACT_EL_RLRANGE: printf("..<"); break;
299 case ACT_EL_ULRANGE: printf("<..<"); break;
300 default: printf("?..?"); break;
301 }
302 asn1print_value(ct->range_stop, flags);
303 break;
304 case ACT_EL_EXT:
305 printf("...");
306 break;
307 case ACT_CT_SIZE:
308 case ACT_CT_FROM:
309 switch(ct->type) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000310 case ACT_CT_SIZE: printf("SIZE("); break;
311 case ACT_CT_FROM: printf("FROM("); break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000312 default: printf("??? ("); break;
313 }
314 assert(ct->el_count != 0);
315 assert(ct->el_count == 1);
316 asn1print_constraint(ct->elements[0], flags);
317 printf(")");
318 break;
319 case ACT_CT_WCOMP:
Lev Walkine596bf02005-03-28 15:01:27 +0000320 assert(ct->el_count != 0);
321 assert(ct->el_count == 1);
322 printf("WITH COMPONENT (");
323 asn1print_constraint(ct->elements[0], flags);
324 printf(")");
Lev Walkin8638f0d2005-03-28 07:50:06 +0000325 break;
Lev Walkine596bf02005-03-28 15:01:27 +0000326 case ACT_CT_WCOMPS: {
327 unsigned int i;
328 printf("WITH COMPONENTS { ");
329 for(i = 0; i < ct->el_count; i++) {
330 asn1p_constraint_t *cel = ct->elements[i];
331 if(i) printf(", ");
332 fwrite(cel->value->value.string.buf,
333 1, cel->value->value.string.size,
334 stdout);
335 if(cel->el_count) {
336 assert(cel->el_count == 1);
337 printf(" ");
338 asn1print_constraint(cel->elements[0],
339 flags);
340 }
341 switch(cel->presence) {
342 case ACPRES_DEFAULT: break;
343 case ACPRES_PRESENT: printf(" PRESENT"); break;
344 case ACPRES_ABSENT: printf(" ABSENT"); break;
345 case ACPRES_OPTIONAL: printf(" OPTIONAL");break;
346 }
347 }
348 printf(" }");
349 }
Lev Walkin8638f0d2005-03-28 07:50:06 +0000350 break;
Lev Walkin1893ddf2005-03-20 14:28:32 +0000351 case ACT_CT_CTDBY:
352 printf("CONSTRAINED BY ");
353 assert(ct->value->type == ATV_UNPARSED);
354 fwrite(ct->value->value.string.buf,
355 1, ct->value->value.string.size, stdout);
Lev Walkinf15320b2004-06-03 03:38:44 +0000356 break;
357 case ACT_CA_SET: symno++;
358 case ACT_CA_CRC: symno++;
359 case ACT_CA_CSV: symno++;
360 case ACT_CA_UNI: symno++;
361 case ACT_CA_INT: symno++;
362 case ACT_CA_EXC:
363 {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000364 char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",",
Lev Walkinf15320b2004-06-03 03:38:44 +0000365 "", "(" };
Lev Walkin0056aec2004-09-05 10:38:50 +0000366 unsigned int i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000367 for(i = 0; i < ct->el_count; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000368 if(i) fputs(symtable[symno], stdout);
369 if(ct->type == ACT_CA_CRC) fputs("{", stdout);
Lev Walkin1e448d32005-03-24 14:26:38 +0000370 asn1print_constraint(ct->elements[i], flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000371 if(ct->type == ACT_CA_CRC) fputs("}", stdout);
372 if(i+1 < ct->el_count
373 && ct->type == ACT_CA_SET)
374 fputs(")", stdout);
375 }
376 }
377 break;
Lev Walkin1e448d32005-03-24 14:26:38 +0000378 case ACT_CA_AEX:
379 assert(ct->el_count == 1);
380 printf("ALL EXCEPT ");
381 asn1print_constraint(ct->elements[0], flags);
382 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000383 case ACT_INVALID:
384 assert(ct->type != ACT_INVALID);
385 break;
386 }
387
388 if(ct->type == ACT_CA_SET)
389 printf(")");
390
391 return 0;
392}
393
394static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000395asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000396 if(pl) {
397 int i;
398 printf("{");
399 for(i = 0; i < pl->params_count; i++) {
400 if(i) printf(", ");
401 if(pl->params[i].governor) {
402 asn1print_ref(pl->params[i].governor, flags);
403 printf(":");
404 }
405 printf("%s", pl->params[i].argument);
406 }
407 printf("}");
408 }
409
410 return 0;
411}
412
413static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000414asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000415 if(wx) {
416 asn1p_wsyntx_chunk_t *wc;
Lev Walkinf15320b2004-06-03 03:38:44 +0000417 TQ_FOR(wc, &(wx->chunks), next) {
Lev Walkin9d542d22006-03-14 16:31:37 +0000418 switch(wc->type) {
419 case WC_LITERAL:
Lev Walkin57074f12006-03-16 05:11:14 +0000420 case WC_WHITESPACE:
Lev Walkind370e9f2006-03-16 10:03:35 +0000421 case WC_FIELD:
Lev Walkin9d542d22006-03-14 16:31:37 +0000422 printf("%s", wc->content.token);
423 break;
Lev Walkin9d542d22006-03-14 16:31:37 +0000424 case WC_OPTIONALGROUP:
425 printf("[");
426 asn1print_with_syntax(wc->content.syntax,flags);
427 printf("]");
428 break;
429 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000431 }
432
433 return 0;
434}
435
436static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000437asn1print_crange_value(asn1cnst_edge_t *edge, int as_char) {
438 switch(edge->type) {
439 case ARE_MIN: printf("MIN"); break;
440 case ARE_MAX: printf("MAX"); break;
441 case ARE_VALUE:
442 if(as_char) {
443 printf("\"%c\"", (unsigned char)edge->value);
444 } else {
Lev Walkin33c16ba2004-09-24 21:01:43 +0000445 printf("%" PRIdASN, edge->value);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000446 }
447 }
448 return 0;
449}
450
451static int
Lev Walkine8e87f12004-08-25 02:00:03 +0000452asn1print_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 +0000453 asn1cnst_range_t *range;
454 int as_char = (type==ACT_CT_FROM);
455 int i;
456
Lev Walkin4b553412005-08-14 14:45:44 +0000457 range = asn1constraint_compute_PER_range(expr_type, ct, type, 0, 0,
458 strict_PER_visible ? CPR_strict_PER_visibility : 0);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000459 if(!range) return -1;
460
Lev Walkine8e87f12004-08-25 02:00:03 +0000461 if(range->incompatible
462 || (strict_PER_visible && range->not_PER_visible)) {
463 asn1constraint_range_free(range);
464 return 0;
465 }
466
Lev Walkin3140e0e2004-08-18 04:50:37 +0000467 switch(type) {
468 case ACT_CT_FROM: printf("(FROM("); break;
469 case ACT_CT_SIZE: printf("(SIZE("); break;
470 default: printf("("); break;
471 }
472 for(i = -1; i < range->el_count; i++) {
473 asn1cnst_range_t *r;
474 if(i == -1) {
475 if(range->el_count) continue;
476 r = range;
477 } else {
478 r = range->elements[i];
479 }
480 if(i > 0) {
481 printf(" | ");
482 }
483 asn1print_crange_value(&r->left, as_char);
484 if(r->left.type != r->right.type
485 || r->left.value != r->right.value) {
486 printf("..");
487 asn1print_crange_value(&r->right, as_char);
488 }
489 }
490 if(range->extensible)
491 printf(",...");
492 printf(type==ACT_EL_RANGE?")":"))");
493
494 if(range->empty_constraint)
495 printf(":Empty!");
496
497 asn1constraint_range_free(range);
498 return 0;
499}
500
501static int
502asn1print_constraint_explain(asn1p_expr_type_e expr_type,
Lev Walkine8e87f12004-08-25 02:00:03 +0000503 asn1p_constraint_t *ct, int s_PV) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000504
Lev Walkine8e87f12004-08-25 02:00:03 +0000505 asn1print_constraint_explain_type(expr_type, ct, ACT_EL_RANGE, s_PV);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000506 printf(" ");
Lev Walkine8e87f12004-08-25 02:00:03 +0000507 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_SIZE, s_PV);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000508 printf(" ");
Lev Walkine8e87f12004-08-25 02:00:03 +0000509 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_FROM, s_PV);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000510
511 return 0;
512}
513
514static int
515asn1print_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 +0000516 int SEQ_OF = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000517
Lev Walkinf4069d22005-02-15 07:06:05 +0000518 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
Lev Walkinf7484512004-10-13 09:13:56 +0000519 INDENT("-- #line %d\n", tc->_lineno);
Lev Walkinef625402005-09-05 05:17:57 +0000520
521 /* Reconstruct compiler directive information */
522 if((tc->marker.flags & EM_INDIRECT)
523 && (tc->marker.flags & EM_OMITABLE) != EM_OMITABLE) {
524 if((flags & APF_NOINDENT))
525 printf(" --<ASN1C.RepresentAsPointer>-- ");
526 else
527 INDENT("--<ASN1C.RepresentAsPointer>--\n");
528 }
529
Lev Walkina00d6b32006-03-21 03:40:38 +0000530 if(tc->Identifier
531 && (!(tc->meta_type == AMT_VALUE && tc->expr_type == A1TC_REFERENCE)
532 || level == 0))
Lev Walkinf15320b2004-06-03 03:38:44 +0000533 INDENT("%s", tc->Identifier);
534
Lev Walkina00d6b32006-03-21 03:40:38 +0000535 if(tc->lhs_params) {
536 asn1print_params(tc->lhs_params, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000537 }
538
539 if(tc->meta_type != AMT_VALUE
Lev Walkinc74ea222004-08-25 02:27:47 +0000540 && tc->meta_type != AMT_VALUESET
Lev Walkinf15320b2004-06-03 03:38:44 +0000541 && tc->expr_type != A1TC_EXTENSIBLE) {
542 if(level) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000543 if(tc->Identifier && !(flags & APF_NOINDENT))
Lev Walkinf15320b2004-06-03 03:38:44 +0000544 printf("\t");
545 } else {
546 printf(" ::=");
547 }
548 }
549
550 if(tc->tag.tag_class) {
551 printf(" ");
552 asn1print_tag(tc, flags);
553 }
554
555 switch(tc->expr_type) {
556 case A1TC_EXTENSIBLE:
557 if(tc->value) {
558 printf("!");
559 asn1print_value(tc->value, flags);
560 }
561 break;
Lev Walkinfd151ce2004-08-22 03:08:51 +0000562 case A1TC_COMPONENTS_OF:
563 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
564 printf(" COMPONENTS OF");
565 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000566 case A1TC_REFERENCE:
567 case A1TC_UNIVERVAL:
Lev Walkinf15320b2004-06-03 03:38:44 +0000568 break;
569 case A1TC_CLASSDEF:
570 printf(" CLASS");
571 break;
Lev Walkin9c2285a2006-03-09 08:49:26 +0000572 case A1TC_CLASSFIELD_TFS ... A1TC_CLASSFIELD_OSFS:
Lev Walkinf15320b2004-06-03 03:38:44 +0000573 /* Nothing to print here */
574 break;
Lev Walkinb1ef3962004-08-20 13:24:28 +0000575 case ASN_CONSTR_SET_OF:
576 case ASN_CONSTR_SEQUENCE_OF:
577 SEQ_OF = 1;
578 if(tc->expr_type == ASN_CONSTR_SET_OF)
579 printf(" SET");
580 else
581 printf(" SEQUENCE");
582 if(tc->constraints) {
583 printf(" ");
584 asn1print_constraint(tc->constraints, flags);
585 }
586 printf(" OF");
587 break;
Lev Walkin5045dfa2006-03-21 09:41:28 +0000588 case A1TC_VALUESET:
589 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000590 default:
591 {
592 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
593 printf(" %s", p?p:"<unknown type!>");
594 }
595 break;
596 }
597
Lev Walkinb9d0ae32005-06-02 04:06:24 +0000598 /*
599 * Put the name of the referred type.
600 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000601 if(tc->reference) {
Lev Walkinb9d0ae32005-06-02 04:06:24 +0000602 printf(" ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000603 asn1print_ref(tc->reference, flags);
604 }
605
Lev Walkin5045dfa2006-03-21 09:41:28 +0000606 if(tc->meta_type == AMT_VALUESET && level == 0)
Lev Walkinc74ea222004-08-25 02:27:47 +0000607 printf(" ::=");
608
Lev Walkinf15320b2004-06-03 03:38:44 +0000609 /*
610 * Display the descendants (children) of the current type.
611 */
Lev Walkinc74ea222004-08-25 02:27:47 +0000612 if(TQ_FIRST(&(tc->members))
613 || (tc->expr_type & ASN_CONSTR_MASK)
Lev Walkinc74ea222004-08-25 02:27:47 +0000614 || tc->meta_type == AMT_OBJECT
Lev Walkin9c2285a2006-03-09 08:49:26 +0000615 || tc->meta_type == AMT_OBJECTCLASS
616 || tc->meta_type == AMT_OBJECTFIELD
Lev Walkinc74ea222004-08-25 02:27:47 +0000617 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000618 asn1p_expr_t *se; /* SubExpression */
Lev Walkin9c2285a2006-03-09 08:49:26 +0000619 int put_braces = (!SEQ_OF) /* Don't need 'em, if SET OF... */
620 && (tc->meta_type != AMT_OBJECTFIELD);
Lev Walkinf15320b2004-06-03 03:38:44 +0000621
Lev Walkinc74ea222004-08-25 02:27:47 +0000622 if(put_braces) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000623 if(flags & APF_NOINDENT) {
624 printf("{");
625 if(!TQ_FIRST(&tc->members))
626 printf("}");
627 } else {
628 printf(" {");
629 if(TQ_FIRST(&tc->members))
630 printf("\n");
631 else
632 printf(" }");
633 }
Lev Walkinc74ea222004-08-25 02:27:47 +0000634 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000635
636 TQ_FOR(se, &(tc->members), next) {
637 /*
Lev Walkinfd151ce2004-08-22 03:08:51 +0000638 * Print the expression as it were a stand-alone type.
Lev Walkinf15320b2004-06-03 03:38:44 +0000639 */
Lev Walkinf7484512004-10-13 09:13:56 +0000640 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinb1e07082004-09-15 11:44:55 +0000641 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
642 printf(" DEFAULT ");
643 asn1print_value(se->marker.default_value, flags);
644 } else if((se->marker.flags & EM_OPTIONAL)
645 == EM_OPTIONAL) {
Lev Walkin6e8da2b2004-09-10 08:21:27 +0000646 printf(" OPTIONAL");
Lev Walkinb1e07082004-09-15 11:44:55 +0000647 }
Lev Walkinef625402005-09-05 05:17:57 +0000648 if(TQ_NEXT(se, next)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000649 printf(",");
Lev Walkinef625402005-09-05 05:17:57 +0000650 if(!(flags & APF_NOINDENT))
651 INDENT("\n");
652 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000653 }
654
Lev Walkinc74ea222004-08-25 02:27:47 +0000655 if(put_braces && TQ_FIRST(&tc->members)) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000656 if(!(flags & APF_NOINDENT))
657 printf("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000658 INDENT("}");
659 }
660 }
661
Lev Walkin9d542d22006-03-14 16:31:37 +0000662 if(tc->with_syntax) {
663 printf(" WITH SYNTAX {");
Lev Walkinf15320b2004-06-03 03:38:44 +0000664 asn1print_with_syntax(tc->with_syntax, flags);
Lev Walkin9d542d22006-03-14 16:31:37 +0000665 printf("}\n");
666 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000667
Lev Walkina00d6b32006-03-21 03:40:38 +0000668 /* Right hand specialization */
669 if(tc->rhs_pspecs) {
670 asn1p_expr_t *se;
671 printf("{");
672 TQ_FOR(se, &(tc->rhs_pspecs->members), next) {
673 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkin0c0bca62006-03-21 04:48:15 +0000674 if(TQ_NEXT(se, next)) printf(", ");
Lev Walkina00d6b32006-03-21 03:40:38 +0000675 }
676 printf("}");
677 }
678
Lev Walkinb1ef3962004-08-20 13:24:28 +0000679 if(!SEQ_OF && tc->constraints) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000680 printf(" ");
Lev Walkin557f27d2006-03-21 07:46:48 +0000681 if(tc->meta_type == AMT_VALUESET)
682 printf("{");
Lev Walkinf15320b2004-06-03 03:38:44 +0000683 asn1print_constraint(tc->constraints, flags);
Lev Walkin557f27d2006-03-21 07:46:48 +0000684 if(tc->meta_type == AMT_VALUESET)
685 printf("}");
Lev Walkinf15320b2004-06-03 03:38:44 +0000686 }
Lev Walkin3140e0e2004-08-18 04:50:37 +0000687
Lev Walkinf15320b2004-06-03 03:38:44 +0000688 if(tc->unique) {
689 printf(" UNIQUE");
690 }
691
692 if(tc->meta_type == AMT_VALUE
693 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin0056aec2004-09-05 10:38:50 +0000694 if(tc->expr_type == A1TC_UNIVERVAL) {
Lev Walkinb1e07082004-09-15 11:44:55 +0000695 if(tc->value) {
696 printf("(");
697 asn1print_value(tc->value, flags);
698 printf(")");
699 }
Lev Walkin0056aec2004-09-05 10:38:50 +0000700 } else {
Lev Walkina00d6b32006-03-21 03:40:38 +0000701 if(level == 0) printf(" ::= ");
Lev Walkin0056aec2004-09-05 10:38:50 +0000702 asn1print_value(tc->value, flags);
703 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000704 }
705
Lev Walkinfd151ce2004-08-22 03:08:51 +0000706 /*
Lev Walkinf4069d22005-02-15 07:06:05 +0000707 * The following section exists entirely for debugging.
Lev Walkinfd151ce2004-08-22 03:08:51 +0000708 */
Lev Walkind370e9f2006-03-16 10:03:35 +0000709 if(flags & APF_PRINT_CONSTRAINTS
Lev Walkinb1ef3962004-08-20 13:24:28 +0000710 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000711 asn1p_expr_t *top_parent;
712
713 if(tc->combined_constraints) {
714 printf("\n-- Combined constraints: ");
715 asn1print_constraint(tc->combined_constraints, flags);
716 }
717
Lev Walkine4d6ab82004-09-22 16:05:13 +0000718 top_parent = asn1f_find_terminal_type_ex(asn, tc);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000719 if(top_parent) {
Lev Walkine8e87f12004-08-25 02:00:03 +0000720 printf("\n-- Practical constraints (%s): ",
721 top_parent->Identifier);
722 asn1print_constraint_explain(top_parent->expr_type,
723 tc->combined_constraints, 0);
Lev Walkinb1ef3962004-08-20 13:24:28 +0000724 printf("\n-- PER-visible constraints (%s): ",
725 top_parent->Identifier);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000726 asn1print_constraint_explain(top_parent->expr_type,
Lev Walkine8e87f12004-08-25 02:00:03 +0000727 tc->combined_constraints, 1);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000728 }
Lev Walkinb1ef3962004-08-20 13:24:28 +0000729 printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000730 }
731
Lev Walkind370e9f2006-03-16 10:03:35 +0000732 if(flags & APF_PRINT_CLASS_MATRIX
733 && tc->expr_type == A1TC_CLASSDEF) do {
734 int r, col, maxidlen;
735 if(tc->object_class_matrix.rows == 0) {
736 printf("\n-- Class matrix is empty");
737 break;
738 }
739 printf("\n-- Class matrix has %d entr%s:\n",
740 tc->object_class_matrix.rows,
741 tc->object_class_matrix.rows==1 ? "y" : "ies");
742 maxidlen = tc->object_class_matrix.max_identifier_length;
743 for(r = -1; r < tc->object_class_matrix.rows; r++) {
744 struct asn1p_ioc_row_s *row;
745 row = tc->object_class_matrix.row[r<0?0:r];
746 if(r < 0) printf("-- %s", r > 9 ? " " : "");
747 else printf("-- [%*d]", r > 9 ? 2 : 1, r+1);
748 for(col = 0; col < row->columns; col++) {
749 struct asn1p_ioc_cell_s *cell;
750 cell = &row->column[col];
751 if(r < 0) {
752 printf("[%*s]", maxidlen,
753 cell->field->Identifier);
754 continue;
755 }
756 if(!cell->value) {
757 printf(" %*s ", maxidlen, "<no entry>");
758 continue;
759 }
760 printf(" %*s ", maxidlen,
761 cell->value->Identifier);
762 }
763 printf("\n");
764 }
765 } while(0);
766
Lev Walkina00d6b32006-03-21 03:40:38 +0000767 if(flags & APF_PRINT_CLASS_MATRIX
768 && tc->lhs_params) do {
769 int i;
770 if(tc->specializations.pspecs_count == 0) {
771 printf("\n-- No specializations found\n");
772 break;
773 }
774 printf("\n-- Specializations list has %d entr%s:\n",
775 tc->specializations.pspecs_count,
776 tc->specializations.pspecs_count == 1 ? "y" : "ies");
777 for(i = 0; i < tc->specializations.pspecs_count; i++) {
778 asn1p_expr_t *se;
779 struct asn1p_pspec_s *pspec;
780 pspec = &tc->specializations.pspec[i];
781 printf("-- ");
782 TQ_FOR(se, &(pspec->rhs_pspecs->members), next) {
783 asn1print_expr(asn, mod, se, flags, level+1);
784 }
785 printf("\n");
786 }
787 } while(0);
788
Lev Walkinf15320b2004-06-03 03:38:44 +0000789 return 0;
790}
Lev Walkin3140e0e2004-08-18 04:50:37 +0000791
Lev Walkinf7484512004-10-13 09:13:56 +0000792
793static int
794asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
795 asn1p_expr_t *se;
796 int expr_unordered = 0;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000797 int dont_involve_children = 0;
Lev Walkinf7484512004-10-13 09:13:56 +0000798
799 switch(expr->meta_type) {
800 case AMT_TYPE:
801 case AMT_TYPEREF:
802 break;
803 default:
804 if(expr->expr_type == A1TC_UNIVERVAL)
805 break;
806 return 0;
807 }
808
809 if(!expr->Identifier) return 0;
810
Lev Walkinf7484512004-10-13 09:13:56 +0000811 if(flags & APF_LINE_COMMENTS)
812 INDENT("<!-- #line %d -->\n", expr->_lineno);
813 INDENT("<!ELEMENT %s", expr->Identifier);
814
815 if(expr->expr_type == A1TC_REFERENCE) {
816 se = asn1f_find_terminal_type_ex(asn, expr);
817 if(!se) {
Lev Walkinef39f7e2004-10-14 05:11:25 +0000818 printf(" (ANY)");
Lev Walkinf7484512004-10-13 09:13:56 +0000819 return 0;
820 }
821 expr = se;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000822 dont_involve_children = 1;
Lev Walkinf7484512004-10-13 09:13:56 +0000823 }
824
Lev Walkind7963aa2005-03-10 15:16:56 +0000825 if(expr->expr_type == ASN_CONSTR_CHOICE
826 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
827 || expr->expr_type == ASN_CONSTR_SET_OF
828 || expr->expr_type == ASN_CONSTR_SET
Lev Walkin112d69e2005-03-09 20:52:15 +0000829 || expr->expr_type == ASN_BASIC_INTEGER
830 || expr->expr_type == ASN_BASIC_ENUMERATED) {
831 expr_unordered = 1;
832 }
833
Lev Walkinf7484512004-10-13 09:13:56 +0000834 if(TQ_FIRST(&expr->members)) {
835 int extensible = 0;
836 printf(" (");
837 TQ_FOR(se, &(expr->members), next) {
838 if(se->expr_type == A1TC_EXTENSIBLE) {
839 extensible = 1;
840 continue;
841 } else if(!se->Identifier
842 && se->expr_type == A1TC_REFERENCE) {
843 asn1print_ref(se->reference, flags);
844 } else if(se->Identifier) {
845 printf("%s", se->Identifier);
846 } else {
847 printf("ANY");
848 }
849 if(expr->expr_type != ASN_CONSTR_SET
850 && expr->expr_type != ASN_CONSTR_CHOICE
851 && expr->expr_type != ASN_BASIC_INTEGER
852 && expr->expr_type != ASN_BASIC_ENUMERATED) {
853 if(expr_unordered)
854 printf("*");
855 else if(se->marker.flags)
856 printf("?");
857 }
858 if(TQ_NEXT(se, next)
859 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
860 printf(expr_unordered?"|":", ");
861 }
862 }
863 if(extensible) {
864 printf(expr_unordered?"|":", ");
865 printf("ANY");
866 if(expr->expr_type != ASN_CONSTR_SET
867 && expr->expr_type != ASN_CONSTR_CHOICE
868 && expr->expr_type != ASN_BASIC_INTEGER
869 && expr->expr_type != ASN_BASIC_ENUMERATED)
870 printf("*");
871 }
872
873 printf(")");
874 if(expr->expr_type == ASN_CONSTR_SET)
875 printf("*");
876
Lev Walkinef39f7e2004-10-14 05:11:25 +0000877 } else switch(expr->expr_type) {
878 case ASN_BASIC_BOOLEAN:
Lev Walkin112d69e2005-03-09 20:52:15 +0000879 printf(" (true|false)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000880 break;
881 case ASN_CONSTR_CHOICE:
882 case ASN_CONSTR_SET:
883 case ASN_CONSTR_SET_OF:
884 case ASN_CONSTR_SEQUENCE:
885 case ASN_CONSTR_SEQUENCE_OF:
886 case ASN_BASIC_NULL:
887 case A1TC_UNIVERVAL:
Lev Walkinf7484512004-10-13 09:13:56 +0000888 printf(" EMPTY");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000889 break;
890 case ASN_TYPE_ANY:
891 printf(" ANY");
892 break;
893 case ASN_BASIC_BIT_STRING:
894 case ASN_BASIC_OCTET_STRING:
895 case ASN_BASIC_OBJECT_IDENTIFIER:
896 case ASN_BASIC_RELATIVE_OID:
Lev Walkincc116532004-10-15 06:01:54 +0000897 case ASN_BASIC_INTEGER:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000898 case ASN_BASIC_UTCTime:
899 case ASN_BASIC_GeneralizedTime:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000900 case ASN_STRING_NumericString:
901 case ASN_STRING_PrintableString:
Lev Walkincc116532004-10-15 06:01:54 +0000902 printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000903 break;
904 case ASN_STRING_VisibleString:
905 case ASN_STRING_ISO646String:
906 /* Entity references, but not XML elements may be present */
Lev Walkinf7484512004-10-13 09:13:56 +0000907 printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000908 break;
909 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
910 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
911 default:
912 /*
913 * XML elements are allowed.
914 * For example, a UTF8String may contain "<bel/>".
915 */
916 printf(" ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000917 }
918 printf(">\n");
919
920 /*
921 * Display the descendants (children) of the current type.
922 */
Lev Walkinef39f7e2004-10-14 05:11:25 +0000923 if(!dont_involve_children) {
924 TQ_FOR(se, &(expr->members), next) {
925 if(se->expr_type == A1TC_EXTENSIBLE) continue;
926 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
927 }
Lev Walkinf7484512004-10-13 09:13:56 +0000928 }
929
930 return 0;
931}