blob: 486f02588d26f62e14ac93235fd21bdeb5a23746 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include <stdio.h>
Lev Walkinf2b2f372016-03-14 02:23:48 -07002#include <stdarg.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00003#include <string.h>
4#include <errno.h>
5#include <assert.h>
6
Lev Walkinc0e03b92017-08-22 01:48:23 -07007#include <asn1_namespace.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00008#include <asn1parser.h>
Lev Walkin3140e0e2004-08-18 04:50:37 +00009#include <asn1fix_export.h>
Lev Walkin9095ada2004-08-18 05:41:05 +000010#include <asn1fix_crange.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000011
12#include "asn1print.h"
13
Lev Walkin1ec76052016-03-13 17:13:20 -070014#define INDENT(fmt, args...) do { \
15 if(!(flags & APF_NOINDENT)) { \
16 int tmp_i = level; \
Lev Walkinf2b2f372016-03-14 02:23:48 -070017 while(tmp_i--) safe_printf(" "); \
Lev Walkin1ec76052016-03-13 17:13:20 -070018 } \
Lev Walkinf2b2f372016-03-14 02:23:48 -070019 safe_printf(fmt, ##args); \
Lev Walkin1ec76052016-03-13 17:13:20 -070020 } while(0)
Lev Walkinf15320b2004-06-03 03:38:44 +000021
Lev Walkin3140e0e2004-08-18 04:50:37 +000022static int asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags);
Lev Walkind6db8022005-03-18 03:53:05 +000023static int asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags);
Lev Walkin3140e0e2004-08-18 04:50:37 +000024static int asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags);
25static int asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags);
26static int asn1print_params(asn1p_paramlist_t *pl,enum asn1print_flags flags);
27static int asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags);
28static int asn1print_constraint(asn1p_constraint_t *, enum asn1print_flags);
29static int asn1print_value(asn1p_value_t *val, enum asn1print_flags flags);
Lev Walkinf7484512004-10-13 09:13:56 +000030static int asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level);
31static 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 +000032
Lev Walkinf2b2f372016-03-14 02:23:48 -070033/* Check printf's error code, to be pedantic. */
34static int safe_printf(const char *fmt, ...) {
35 va_list ap;
36 va_start(ap, fmt);
37 int ret = vprintf(fmt, ap);
38 assert(ret >= 0);
39 va_end(ap);
40 return ret;
41}
42
43/* Pedantically check fwrite's return value. */
44static size_t safe_fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream) {
45 size_t ret = fwrite(ptr, 1, size * nitems, stream);
46 assert(ret == size * nitems);
47 return ret;
48}
49
Lev Walkinf15320b2004-06-03 03:38:44 +000050/*
51 * Print the contents of the parsed ASN tree.
52 */
53int
Lev Walkin3140e0e2004-08-18 04:50:37 +000054asn1print(asn1p_t *asn, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +000055 asn1p_module_t *mod;
Lev Walkinc70c2ef2004-09-30 06:38:21 +000056 int modno = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000057
58 if(asn == NULL) {
59 errno = EINVAL;
60 return -1;
61 }
62
Lev Walkinf7484512004-10-13 09:13:56 +000063 if(flags & APF_PRINT_XML_DTD)
Lev Walkinf2b2f372016-03-14 02:23:48 -070064 safe_printf("<!-- XML DTD generated by asn1c-" VERSION " -->\n\n");
Lev Walkinf7484512004-10-13 09:13:56 +000065
Lev Walkinf15320b2004-06-03 03:38:44 +000066 TQ_FOR(mod, &(asn->modules), mod_next) {
Lev Walkin6b3ff542006-03-06 14:51:00 +000067 if(mod->_tags & MT_STANDARD_MODULE)
68 return 0; /* Ignore modules imported from skeletons */
Lev Walkinf2b2f372016-03-14 02:23:48 -070069 if(modno++) safe_printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +000070 asn1print_module(asn, mod, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +000071 }
72
Lev Walkin112d69e2005-03-09 20:52:15 +000073 if(flags & APF_PRINT_XML_DTD) {
74 /* Values for BOOLEAN */
Lev Walkinf2b2f372016-03-14 02:23:48 -070075 safe_printf("<!ELEMENT true EMPTY>\n");
76 safe_printf("<!ELEMENT false EMPTY>\n");
Lev Walkin112d69e2005-03-09 20:52:15 +000077 }
78
Lev Walkinf15320b2004-06-03 03:38:44 +000079 return 0;
80}
81
82static int
Lev Walkin3140e0e2004-08-18 04:50:37 +000083asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +000084 asn1p_expr_t *tc;
85
Lev Walkinf7484512004-10-13 09:13:56 +000086 if(flags & APF_PRINT_XML_DTD)
Lev Walkinf2b2f372016-03-14 02:23:48 -070087 safe_printf("<!-- ASN.1 module\n");
Lev Walkinf7484512004-10-13 09:13:56 +000088
Lev Walkinf2b2f372016-03-14 02:23:48 -070089 safe_printf("%s ", mod->ModuleName);
Lev Walkinf15320b2004-06-03 03:38:44 +000090 if(mod->module_oid) {
Lev Walkinb36317c2005-08-12 10:09:10 +000091 asn1print_oid(strlen(mod->ModuleName), mod->module_oid, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -070092 safe_printf("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000093 }
94
Lev Walkinf7484512004-10-13 09:13:56 +000095 if(flags & APF_PRINT_XML_DTD) {
96 if(mod->source_file_name
97 && strcmp(mod->source_file_name, "-"))
Lev Walkinf2b2f372016-03-14 02:23:48 -070098 safe_printf("found in %s", mod->source_file_name);
99 safe_printf(" -->\n\n");
Lev Walkinf7484512004-10-13 09:13:56 +0000100
101 TQ_FOR(tc, &(mod->members), next) {
102 asn1print_expr_dtd(asn, mod, tc, flags, 0);
103 }
104
105 return 0;
106 }
107
Lev Walkinf2b2f372016-03-14 02:23:48 -0700108 safe_printf("DEFINITIONS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000109
Lev Walkin3140e0e2004-08-18 04:50:37 +0000110 if(mod->module_flags & MSF_TAG_INSTRUCTIONS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700111 safe_printf(" TAG INSTRUCTIONS");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000112 if(mod->module_flags & MSF_XER_INSTRUCTIONS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700113 safe_printf(" XER INSTRUCTIONS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000114 if(mod->module_flags & MSF_EXPLICIT_TAGS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700115 safe_printf(" EXPLICIT TAGS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 if(mod->module_flags & MSF_IMPLICIT_TAGS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700117 safe_printf(" IMPLICIT TAGS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000118 if(mod->module_flags & MSF_AUTOMATIC_TAGS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700119 safe_printf(" AUTOMATIC TAGS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 if(mod->module_flags & MSF_EXTENSIBILITY_IMPLIED)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700121 safe_printf(" EXTENSIBILITY IMPLIED");
Lev Walkinf15320b2004-06-03 03:38:44 +0000122
Lev Walkinf2b2f372016-03-14 02:23:48 -0700123 safe_printf(" ::=\n");
124 safe_printf("BEGIN\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000125
126 TQ_FOR(tc, &(mod->members), next) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000127 asn1print_expr(asn, mod, tc, flags, 0);
Lev Walkind370e9f2006-03-16 10:03:35 +0000128 if(flags & APF_PRINT_CONSTRAINTS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700129 safe_printf("\n");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000130 else
Lev Walkinf2b2f372016-03-14 02:23:48 -0700131 safe_printf("\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000132 }
133
Lev Walkinf2b2f372016-03-14 02:23:48 -0700134 safe_printf("END\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000135
136 return 0;
137}
138
139static int
Lev Walkind6db8022005-03-18 03:53:05 +0000140asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags) {
141 size_t accum = prior_len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 int ac;
Lev Walkinf15320b2004-06-03 03:38:44 +0000143
Lev Walkind9bd7752004-06-05 08:17:50 +0000144 (void)flags; /* Unused argument */
145
Lev Walkinf2b2f372016-03-14 02:23:48 -0700146 safe_printf("{");
Lev Walkinf15320b2004-06-03 03:38:44 +0000147 for(ac = 0; ac < oid->arcs_count; ac++) {
Lev Walkin4efbfb72005-02-25 14:20:30 +0000148 const char *arcname = oid->arcs[ac].name;
149
Lev Walkin5b7eeaf2005-03-28 17:52:43 +0000150 if(accum + strlen(arcname ? arcname : "") > 72) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700151 safe_printf("\n\t");
Lev Walkin5b7eeaf2005-03-28 17:52:43 +0000152 accum = 8;
Lev Walkind6db8022005-03-18 03:53:05 +0000153 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700154 accum += safe_printf(" ");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000155 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000156
Lev Walkin4efbfb72005-02-25 14:20:30 +0000157 if(arcname) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700158 accum += safe_printf("%s", arcname);
Lev Walkin0056aec2004-09-05 10:38:50 +0000159 if(oid->arcs[ac].number >= 0) {
Lev Walkinda997b12017-08-04 01:38:41 -0700160 accum += safe_printf("(%s)",
161 asn1p_itoa(oid->arcs[ac].number));
Lev Walkin0056aec2004-09-05 10:38:50 +0000162 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000163 } else {
Lev Walkinda997b12017-08-04 01:38:41 -0700164 accum += safe_printf("%s", asn1p_itoa(oid->arcs[ac].number));
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000166 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700167 safe_printf(" }");
Lev Walkinf15320b2004-06-03 03:38:44 +0000168
169 return 0;
170}
171
172static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000173asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000174
Lev Walkind9bd7752004-06-05 08:17:50 +0000175 (void)flags; /* Unused argument */
176
Lev Walkin62d95d22017-08-06 23:41:11 -0700177 for(size_t cc = 0; cc < ref->comp_count; cc++) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700178 if(cc) safe_printf(".");
179 safe_printf("%s", ref->components[cc].name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 }
181
182 return 0;
183}
184
185static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000186asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 struct asn1p_type_tag_s *tag = &tc->tag;
188
Lev Walkind9bd7752004-06-05 08:17:50 +0000189 (void)flags; /* Unused argument */
190
Lev Walkinf2b2f372016-03-14 02:23:48 -0700191 safe_printf("%s", asn1p_tag2string(tag, 0));
Lev Walkinf15320b2004-06-03 03:38:44 +0000192
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:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700206 safe_printf("NULL");
Lev Walkinb1e07082004-09-15 11:44:55 +0000207 return 0;
208 case ATV_REAL:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700209 safe_printf("%f", val->value.v_double);
Lev Walkinb1e07082004-09-15 11:44:55 +0000210 return 0;
Lev Walkina9532f42006-09-17 04:52:50 +0000211 case ATV_TYPE:
212 asn1print_expr(val->value.v_type->module->asn1p,
213 val->value.v_type->module,
214 val->value.v_type, flags, 0);
215 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000216 case ATV_INTEGER:
Lev Walkinda997b12017-08-04 01:38:41 -0700217 safe_printf("%s", asn1p_itoa(val->value.v_integer));
Lev Walkinf15320b2004-06-03 03:38:44 +0000218 return 0;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700219 case ATV_MIN: safe_printf("MIN"); return 0;
220 case ATV_MAX: safe_printf("MAX"); return 0;
221 case ATV_FALSE: safe_printf("FALSE"); return 0;
222 case ATV_TRUE: safe_printf("TRUE"); return 0;
Lev Walkin1e448d32005-03-24 14:26:38 +0000223 case ATV_TUPLE:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700224 safe_printf("{%d, %d}",
Lev Walkin1e448d32005-03-24 14:26:38 +0000225 (int)(val->value.v_integer >> 4),
226 (int)(val->value.v_integer & 0x0f));
227 return 0;
228 case ATV_QUADRUPLE:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700229 safe_printf("{%d, %d, %d, %d}",
Lev Walkin1e448d32005-03-24 14:26:38 +0000230 (int)((val->value.v_integer >> 24) & 0xff),
231 (int)((val->value.v_integer >> 16) & 0xff),
232 (int)((val->value.v_integer >> 8) & 0xff),
233 (int)((val->value.v_integer >> 0) & 0xff)
234 );
235 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000236 case ATV_STRING:
237 {
Lev Walkin84fbd722005-06-15 18:41:50 +0000238 char *p = (char *)val->value.string.buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000239 putchar('"');
240 if(strchr(p, '"')) {
241 /* Mask quotes */
242 for(; *p; p++) {
243 if(*p == '"')
244 putchar(*p);
245 putchar(*p);
246 }
247 } else {
248 fputs(p, stdout);
249 }
250 putchar('"');
251 }
252 return 0;
253 case ATV_UNPARSED:
Lev Walkin84fbd722005-06-15 18:41:50 +0000254 fputs((char *)val->value.string.buf, stdout);
Lev Walkinf15320b2004-06-03 03:38:44 +0000255 return 0;
256 case ATV_BITVECTOR:
257 {
258 uint8_t *bitvector;
259 int bits;
260 int i;
261
262 bitvector = val->value.binary_vector.bits;
263 bits = val->value.binary_vector.size_in_bits;
264
Lev Walkinf2b2f372016-03-14 02:23:48 -0700265 safe_printf("'");
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 if(bits%8) {
267 for(i = 0; i < bits; i++) {
268 uint8_t uc;
269 uc = bitvector[i>>3];
270 putchar(((uc >> (7-(i%8)))&1)?'1':'0');
271 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700272 safe_printf("'B");
Lev Walkinf15320b2004-06-03 03:38:44 +0000273 } else {
274 char hextable[16] = "0123456789ABCDEF";
275 for(i = 0; i < (bits>>3); i++) {
276 putchar(hextable[bitvector[i] >> 4]);
277 putchar(hextable[bitvector[i] & 0x0f]);
278 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700279 safe_printf("'H");
Lev Walkinf15320b2004-06-03 03:38:44 +0000280 }
Lev Walkin043af0d2005-02-24 21:07:35 +0000281 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000282 }
Lev Walkinb1e07082004-09-15 11:44:55 +0000283 case ATV_REFERENCED:
284 return asn1print_ref(val->value.reference, flags);
Lev Walkin5045dfa2006-03-21 09:41:28 +0000285 case ATV_VALUESET:
286 return asn1print_constraint(val->value.constraint, flags);
Lev Walkinb1e07082004-09-15 11:44:55 +0000287 case ATV_CHOICE_IDENTIFIER:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700288 safe_printf("%s: ", val->value.choice_identifier.identifier);
Lev Walkinb1e07082004-09-15 11:44:55 +0000289 return asn1print_value(val->value.choice_identifier.value, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000290 }
291
292 assert(val->type || !"Unknown");
293
294 return 0;
295}
296
297static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000298asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000299 int symno = 0;
300
301 if(ct == 0) return 0;
302
303 if(ct->type == ACT_CA_SET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700304 safe_printf("(");
Lev Walkinf15320b2004-06-03 03:38:44 +0000305
306 switch(ct->type) {
Lev Walkinff7dd142005-03-20 12:58:00 +0000307 case ACT_EL_TYPE:
Lev Walkin5045dfa2006-03-21 09:41:28 +0000308 asn1print_value(ct->containedSubtype, flags);
Lev Walkinff7dd142005-03-20 12:58:00 +0000309 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000310 case ACT_EL_VALUE:
311 asn1print_value(ct->value, flags);
312 break;
313 case ACT_EL_RANGE:
314 case ACT_EL_LLRANGE:
315 case ACT_EL_RLRANGE:
316 case ACT_EL_ULRANGE:
317 asn1print_value(ct->range_start, flags);
318 switch(ct->type) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700319 case ACT_EL_RANGE: safe_printf(".."); break;
320 case ACT_EL_LLRANGE: safe_printf("<.."); break;
321 case ACT_EL_RLRANGE: safe_printf("..<"); break;
322 case ACT_EL_ULRANGE: safe_printf("<..<"); break;
323 default: safe_printf("?..?"); break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000324 }
325 asn1print_value(ct->range_stop, flags);
326 break;
327 case ACT_EL_EXT:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700328 safe_printf("...");
Lev Walkinf15320b2004-06-03 03:38:44 +0000329 break;
330 case ACT_CT_SIZE:
331 case ACT_CT_FROM:
332 switch(ct->type) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700333 case ACT_CT_SIZE: safe_printf("SIZE("); break;
334 case ACT_CT_FROM: safe_printf("FROM("); break;
335 default: safe_printf("??? ("); break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000336 }
337 assert(ct->el_count != 0);
338 assert(ct->el_count == 1);
339 asn1print_constraint(ct->elements[0], flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700340 safe_printf(")");
Lev Walkinf15320b2004-06-03 03:38:44 +0000341 break;
342 case ACT_CT_WCOMP:
Lev Walkine596bf02005-03-28 15:01:27 +0000343 assert(ct->el_count != 0);
344 assert(ct->el_count == 1);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700345 safe_printf("WITH COMPONENT (");
Lev Walkine596bf02005-03-28 15:01:27 +0000346 asn1print_constraint(ct->elements[0], flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700347 safe_printf(")");
Lev Walkin8638f0d2005-03-28 07:50:06 +0000348 break;
Lev Walkine596bf02005-03-28 15:01:27 +0000349 case ACT_CT_WCOMPS: {
350 unsigned int i;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700351 safe_printf("WITH COMPONENTS { ");
Lev Walkine596bf02005-03-28 15:01:27 +0000352 for(i = 0; i < ct->el_count; i++) {
353 asn1p_constraint_t *cel = ct->elements[i];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700354 if(i) safe_printf(", ");
355 safe_fwrite(cel->value->value.string.buf,
Lev Walkine596bf02005-03-28 15:01:27 +0000356 1, cel->value->value.string.size,
357 stdout);
358 if(cel->el_count) {
359 assert(cel->el_count == 1);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700360 safe_printf(" ");
Lev Walkine596bf02005-03-28 15:01:27 +0000361 asn1print_constraint(cel->elements[0],
362 flags);
363 }
364 switch(cel->presence) {
365 case ACPRES_DEFAULT: break;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700366 case ACPRES_PRESENT: safe_printf(" PRESENT"); break;
367 case ACPRES_ABSENT: safe_printf(" ABSENT"); break;
368 case ACPRES_OPTIONAL: safe_printf(" OPTIONAL");break;
Lev Walkine596bf02005-03-28 15:01:27 +0000369 }
370 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700371 safe_printf(" }");
Lev Walkine596bf02005-03-28 15:01:27 +0000372 }
Lev Walkin8638f0d2005-03-28 07:50:06 +0000373 break;
Lev Walkin1893ddf2005-03-20 14:28:32 +0000374 case ACT_CT_CTDBY:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700375 safe_printf("CONSTRAINED BY ");
Lev Walkin1893ddf2005-03-20 14:28:32 +0000376 assert(ct->value->type == ATV_UNPARSED);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700377 safe_fwrite(ct->value->value.string.buf,
Lev Walkin1893ddf2005-03-20 14:28:32 +0000378 1, ct->value->value.string.size, stdout);
Lev Walkina9532f42006-09-17 04:52:50 +0000379 break;
380 case ACT_CT_CTNG:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700381 safe_printf("CONTAINING ");
Lev Walkina9532f42006-09-17 04:52:50 +0000382 asn1print_expr(ct->value->value.v_type->module->asn1p,
383 ct->value->value.v_type->module,
384 ct->value->value.v_type,
385 flags, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000386 break;
Lev Walkin5c541f12006-10-18 18:40:14 +0000387 case ACT_CT_PATTERN:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700388 safe_printf("PATTERN ");
Lev Walkin5c541f12006-10-18 18:40:14 +0000389 asn1print_value(ct->value, flags);
Lev Walkin5c541f12006-10-18 18:40:14 +0000390 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000391 case ACT_CA_SET: symno++;
392 case ACT_CA_CRC: symno++;
393 case ACT_CA_CSV: symno++;
394 case ACT_CA_UNI: symno++;
395 case ACT_CA_INT: symno++;
396 case ACT_CA_EXC:
397 {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000398 char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",",
Lev Walkinf15320b2004-06-03 03:38:44 +0000399 "", "(" };
Lev Walkin0056aec2004-09-05 10:38:50 +0000400 unsigned int i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000401 for(i = 0; i < ct->el_count; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000402 if(i) fputs(symtable[symno], stdout);
403 if(ct->type == ACT_CA_CRC) fputs("{", stdout);
Lev Walkin1e448d32005-03-24 14:26:38 +0000404 asn1print_constraint(ct->elements[i], flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000405 if(ct->type == ACT_CA_CRC) fputs("}", stdout);
406 if(i+1 < ct->el_count
407 && ct->type == ACT_CA_SET)
408 fputs(")", stdout);
409 }
410 }
411 break;
Lev Walkin1e448d32005-03-24 14:26:38 +0000412 case ACT_CA_AEX:
413 assert(ct->el_count == 1);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700414 safe_printf("ALL EXCEPT ");
Lev Walkin1e448d32005-03-24 14:26:38 +0000415 asn1print_constraint(ct->elements[0], flags);
416 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000417 case ACT_INVALID:
418 assert(ct->type != ACT_INVALID);
419 break;
420 }
421
422 if(ct->type == ACT_CA_SET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700423 safe_printf(")");
Lev Walkinf15320b2004-06-03 03:38:44 +0000424
425 return 0;
426}
427
428static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000429asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 if(pl) {
431 int i;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700432 safe_printf("{");
Lev Walkinf15320b2004-06-03 03:38:44 +0000433 for(i = 0; i < pl->params_count; i++) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700434 if(i) safe_printf(", ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000435 if(pl->params[i].governor) {
436 asn1print_ref(pl->params[i].governor, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700437 safe_printf(":");
Lev Walkinf15320b2004-06-03 03:38:44 +0000438 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700439 safe_printf("%s", pl->params[i].argument);
Lev Walkinf15320b2004-06-03 03:38:44 +0000440 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700441 safe_printf("}");
Lev Walkinf15320b2004-06-03 03:38:44 +0000442 }
443
444 return 0;
445}
446
447static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000448asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000449 if(wx) {
450 asn1p_wsyntx_chunk_t *wc;
Lev Walkinf15320b2004-06-03 03:38:44 +0000451 TQ_FOR(wc, &(wx->chunks), next) {
Lev Walkin9d542d22006-03-14 16:31:37 +0000452 switch(wc->type) {
453 case WC_LITERAL:
Lev Walkin57074f12006-03-16 05:11:14 +0000454 case WC_WHITESPACE:
Lev Walkind370e9f2006-03-16 10:03:35 +0000455 case WC_FIELD:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700456 safe_printf("%s", wc->content.token);
Lev Walkin9d542d22006-03-14 16:31:37 +0000457 break;
Lev Walkin9d542d22006-03-14 16:31:37 +0000458 case WC_OPTIONALGROUP:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700459 safe_printf("[");
Lev Walkin9d542d22006-03-14 16:31:37 +0000460 asn1print_with_syntax(wc->content.syntax,flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700461 safe_printf("]");
Lev Walkin9d542d22006-03-14 16:31:37 +0000462 break;
463 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000464 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000465 }
466
467 return 0;
468}
469
470static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000471asn1print_crange_value(asn1cnst_edge_t *edge, int as_char) {
472 switch(edge->type) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700473 case ARE_MIN: safe_printf("MIN"); break;
474 case ARE_MAX: safe_printf("MAX"); break;
Lev Walkin3140e0e2004-08-18 04:50:37 +0000475 case ARE_VALUE:
476 if(as_char) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700477 safe_printf("\"%c\"", (unsigned char)edge->value);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000478 } else {
Lev Walkinda997b12017-08-04 01:38:41 -0700479 safe_printf("%s", asn1p_itoa(edge->value));
Lev Walkin3140e0e2004-08-18 04:50:37 +0000480 }
481 }
482 return 0;
483}
484
485static int
Lev Walkina28cbb92017-07-31 20:20:17 -0700486asn1print_constraint_explain_type(const char *dbg_name, asn1p_expr_type_e expr_type, asn1p_constraint_t *ct, enum asn1p_constraint_type_e type, enum cpr_flags cpr) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000487 asn1cnst_range_t *range;
488 int as_char = (type==ACT_CT_FROM);
489 int i;
490
Lev Walkina28cbb92017-07-31 20:20:17 -0700491 range = asn1constraint_compute_constraint_range(dbg_name, expr_type, ct, type, 0, 0, cpr);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000492 if(!range) return -1;
493
Lev Walkina2abcaa2017-07-23 21:08:49 +0400494 if(range->incompatible) return 0;
495
496 if((cpr & CPR_strict_OER_visibility) && range->not_OER_visible) {
497 asn1constraint_range_free(range);
498 return 0;
499 }
500
501 if((cpr & CPR_strict_PER_visibility) && range->not_PER_visible) {
Lev Walkine8e87f12004-08-25 02:00:03 +0000502 asn1constraint_range_free(range);
503 return 0;
504 }
505
Lev Walkin3140e0e2004-08-18 04:50:37 +0000506 switch(type) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700507 case ACT_CT_FROM: safe_printf("(FROM("); break;
508 case ACT_CT_SIZE: safe_printf("(SIZE("); break;
509 default: safe_printf("("); break;
Lev Walkin3140e0e2004-08-18 04:50:37 +0000510 }
511 for(i = -1; i < range->el_count; i++) {
512 asn1cnst_range_t *r;
513 if(i == -1) {
514 if(range->el_count) continue;
515 r = range;
516 } else {
517 r = range->elements[i];
518 }
519 if(i > 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700520 safe_printf(" | ");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000521 }
522 asn1print_crange_value(&r->left, as_char);
523 if(r->left.type != r->right.type
524 || r->left.value != r->right.value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700525 safe_printf("..");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000526 asn1print_crange_value(&r->right, as_char);
527 }
528 }
529 if(range->extensible)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700530 safe_printf(",...");
531 safe_printf(type==ACT_EL_RANGE?")":"))");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000532
533 if(range->empty_constraint)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700534 safe_printf(":Empty!");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000535
536 asn1constraint_range_free(range);
537 return 0;
538}
539
540static int
Lev Walkina28cbb92017-07-31 20:20:17 -0700541asn1print_constraint_explain(const char *dbg_name, asn1p_expr_type_e expr_type,
Lev Walkina2abcaa2017-07-23 21:08:49 +0400542 asn1p_constraint_t *ct, enum cpr_flags cpr) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000543
Lev Walkina28cbb92017-07-31 20:20:17 -0700544 asn1print_constraint_explain_type(dbg_name, expr_type, ct, ACT_EL_RANGE, cpr);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700545 safe_printf(" ");
Lev Walkina28cbb92017-07-31 20:20:17 -0700546 asn1print_constraint_explain_type(dbg_name, expr_type, ct, ACT_CT_SIZE, cpr);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700547 safe_printf(" ");
Lev Walkina28cbb92017-07-31 20:20:17 -0700548 asn1print_constraint_explain_type(dbg_name, expr_type, ct, ACT_CT_FROM, cpr);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000549
550 return 0;
551}
552
553static int
554asn1print_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 +0000555 int SEQ_OF = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000556
Lev Walkinf4069d22005-02-15 07:06:05 +0000557 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
Lev Walkinf7484512004-10-13 09:13:56 +0000558 INDENT("-- #line %d\n", tc->_lineno);
Lev Walkinef625402005-09-05 05:17:57 +0000559
560 /* Reconstruct compiler directive information */
561 if((tc->marker.flags & EM_INDIRECT)
562 && (tc->marker.flags & EM_OMITABLE) != EM_OMITABLE) {
563 if((flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700564 safe_printf(" --<ASN1C.RepresentAsPointer>-- ");
Lev Walkinef625402005-09-05 05:17:57 +0000565 else
566 INDENT("--<ASN1C.RepresentAsPointer>--\n");
567 }
568
Lev Walkina00d6b32006-03-21 03:40:38 +0000569 if(tc->Identifier
570 && (!(tc->meta_type == AMT_VALUE && tc->expr_type == A1TC_REFERENCE)
571 || level == 0))
Lev Walkinf15320b2004-06-03 03:38:44 +0000572 INDENT("%s", tc->Identifier);
573
Lev Walkina00d6b32006-03-21 03:40:38 +0000574 if(tc->lhs_params) {
575 asn1print_params(tc->lhs_params, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000576 }
577
578 if(tc->meta_type != AMT_VALUE
Lev Walkinc74ea222004-08-25 02:27:47 +0000579 && tc->meta_type != AMT_VALUESET
Lev Walkinf15320b2004-06-03 03:38:44 +0000580 && tc->expr_type != A1TC_EXTENSIBLE) {
581 if(level) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000582 if(tc->Identifier && !(flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700583 safe_printf("\t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000584 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700585 safe_printf(" ::=");
Lev Walkinf15320b2004-06-03 03:38:44 +0000586 }
587 }
588
589 if(tc->tag.tag_class) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700590 safe_printf(" ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000591 asn1print_tag(tc, flags);
592 }
593
594 switch(tc->expr_type) {
595 case A1TC_EXTENSIBLE:
596 if(tc->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700597 safe_printf("!");
Lev Walkinf15320b2004-06-03 03:38:44 +0000598 asn1print_value(tc->value, flags);
599 }
600 break;
Lev Walkinfd151ce2004-08-22 03:08:51 +0000601 case A1TC_COMPONENTS_OF:
602 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700603 safe_printf(" COMPONENTS OF");
Lev Walkinfd151ce2004-08-22 03:08:51 +0000604 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000605 case A1TC_REFERENCE:
606 case A1TC_UNIVERVAL:
Lev Walkinf15320b2004-06-03 03:38:44 +0000607 break;
608 case A1TC_CLASSDEF:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700609 safe_printf(" CLASS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000610 break;
Lev Walkin9c2285a2006-03-09 08:49:26 +0000611 case A1TC_CLASSFIELD_TFS ... A1TC_CLASSFIELD_OSFS:
Lev Walkinf15320b2004-06-03 03:38:44 +0000612 /* Nothing to print here */
613 break;
Lev Walkinb1ef3962004-08-20 13:24:28 +0000614 case ASN_CONSTR_SET_OF:
615 case ASN_CONSTR_SEQUENCE_OF:
616 SEQ_OF = 1;
617 if(tc->expr_type == ASN_CONSTR_SET_OF)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700618 safe_printf(" SET");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000619 else
Lev Walkinf2b2f372016-03-14 02:23:48 -0700620 safe_printf(" SEQUENCE");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000621 if(tc->constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700622 safe_printf(" ");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000623 asn1print_constraint(tc->constraints, flags);
624 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700625 safe_printf(" OF");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000626 break;
Lev Walkin5045dfa2006-03-21 09:41:28 +0000627 case A1TC_VALUESET:
628 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000629 default:
630 {
631 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700632 safe_printf(" %s", p?p:"<unknown type!>");
Lev Walkinf15320b2004-06-03 03:38:44 +0000633 }
634 break;
635 }
636
Lev Walkinb9d0ae32005-06-02 04:06:24 +0000637 /*
638 * Put the name of the referred type.
639 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000640 if(tc->reference) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700641 safe_printf(" ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000642 asn1print_ref(tc->reference, flags);
643 }
644
Lev Walkin5045dfa2006-03-21 09:41:28 +0000645 if(tc->meta_type == AMT_VALUESET && level == 0)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700646 safe_printf(" ::=");
Lev Walkinc74ea222004-08-25 02:27:47 +0000647
Lev Walkinf15320b2004-06-03 03:38:44 +0000648 /*
649 * Display the descendants (children) of the current type.
650 */
Lev Walkinc74ea222004-08-25 02:27:47 +0000651 if(TQ_FIRST(&(tc->members))
652 || (tc->expr_type & ASN_CONSTR_MASK)
Lev Walkinc74ea222004-08-25 02:27:47 +0000653 || tc->meta_type == AMT_OBJECT
Lev Walkin9c2285a2006-03-09 08:49:26 +0000654 || tc->meta_type == AMT_OBJECTCLASS
655 || tc->meta_type == AMT_OBJECTFIELD
Lev Walkinc74ea222004-08-25 02:27:47 +0000656 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000657 asn1p_expr_t *se; /* SubExpression */
Lev Walkin9c2285a2006-03-09 08:49:26 +0000658 int put_braces = (!SEQ_OF) /* Don't need 'em, if SET OF... */
659 && (tc->meta_type != AMT_OBJECTFIELD);
Lev Walkinf15320b2004-06-03 03:38:44 +0000660
Lev Walkinc74ea222004-08-25 02:27:47 +0000661 if(put_braces) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000662 if(flags & APF_NOINDENT) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700663 safe_printf("{");
Lev Walkinf4069d22005-02-15 07:06:05 +0000664 if(!TQ_FIRST(&tc->members))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700665 safe_printf("}");
Lev Walkinf4069d22005-02-15 07:06:05 +0000666 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700667 safe_printf(" {");
Lev Walkinf4069d22005-02-15 07:06:05 +0000668 if(TQ_FIRST(&tc->members))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700669 safe_printf("\n");
Lev Walkinf4069d22005-02-15 07:06:05 +0000670 else
Lev Walkinf2b2f372016-03-14 02:23:48 -0700671 safe_printf(" }");
Lev Walkinf4069d22005-02-15 07:06:05 +0000672 }
Lev Walkinc74ea222004-08-25 02:27:47 +0000673 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000674
675 TQ_FOR(se, &(tc->members), next) {
676 /*
Lev Walkinfd151ce2004-08-22 03:08:51 +0000677 * Print the expression as it were a stand-alone type.
Lev Walkinf15320b2004-06-03 03:38:44 +0000678 */
Lev Walkinf7484512004-10-13 09:13:56 +0000679 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinb1e07082004-09-15 11:44:55 +0000680 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700681 safe_printf(" DEFAULT ");
Lev Walkinb1e07082004-09-15 11:44:55 +0000682 asn1print_value(se->marker.default_value, flags);
683 } else if((se->marker.flags & EM_OPTIONAL)
684 == EM_OPTIONAL) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700685 safe_printf(" OPTIONAL");
Lev Walkinb1e07082004-09-15 11:44:55 +0000686 }
Lev Walkinef625402005-09-05 05:17:57 +0000687 if(TQ_NEXT(se, next)) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700688 safe_printf(",");
Lev Walkinef625402005-09-05 05:17:57 +0000689 if(!(flags & APF_NOINDENT))
690 INDENT("\n");
691 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000692 }
693
Lev Walkinc74ea222004-08-25 02:27:47 +0000694 if(put_braces && TQ_FIRST(&tc->members)) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000695 if(!(flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700696 safe_printf("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000697 INDENT("}");
698 }
699 }
700
Lev Walkin9d542d22006-03-14 16:31:37 +0000701 if(tc->with_syntax) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700702 safe_printf(" WITH SYNTAX {");
Lev Walkinf15320b2004-06-03 03:38:44 +0000703 asn1print_with_syntax(tc->with_syntax, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700704 safe_printf("}\n");
Lev Walkin9d542d22006-03-14 16:31:37 +0000705 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000706
Lev Walkina00d6b32006-03-21 03:40:38 +0000707 /* Right hand specialization */
708 if(tc->rhs_pspecs) {
709 asn1p_expr_t *se;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700710 safe_printf("{");
Lev Walkina00d6b32006-03-21 03:40:38 +0000711 TQ_FOR(se, &(tc->rhs_pspecs->members), next) {
712 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700713 if(TQ_NEXT(se, next)) safe_printf(", ");
Lev Walkina00d6b32006-03-21 03:40:38 +0000714 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700715 safe_printf("}");
Lev Walkina00d6b32006-03-21 03:40:38 +0000716 }
717
Lev Walkinb1ef3962004-08-20 13:24:28 +0000718 if(!SEQ_OF && tc->constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700719 safe_printf(" ");
Lev Walkin557f27d2006-03-21 07:46:48 +0000720 if(tc->meta_type == AMT_VALUESET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700721 safe_printf("{");
Lev Walkinf15320b2004-06-03 03:38:44 +0000722 asn1print_constraint(tc->constraints, flags);
Lev Walkin557f27d2006-03-21 07:46:48 +0000723 if(tc->meta_type == AMT_VALUESET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700724 safe_printf("}");
Lev Walkinf15320b2004-06-03 03:38:44 +0000725 }
Lev Walkin3140e0e2004-08-18 04:50:37 +0000726
Lev Walkinf15320b2004-06-03 03:38:44 +0000727 if(tc->unique) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700728 safe_printf(" UNIQUE");
Lev Walkinf15320b2004-06-03 03:38:44 +0000729 }
730
731 if(tc->meta_type == AMT_VALUE
732 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin0056aec2004-09-05 10:38:50 +0000733 if(tc->expr_type == A1TC_UNIVERVAL) {
Lev Walkinb1e07082004-09-15 11:44:55 +0000734 if(tc->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700735 safe_printf("(");
Lev Walkinb1e07082004-09-15 11:44:55 +0000736 asn1print_value(tc->value, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700737 safe_printf(")");
Lev Walkinb1e07082004-09-15 11:44:55 +0000738 }
Lev Walkin0056aec2004-09-05 10:38:50 +0000739 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700740 if(level == 0) safe_printf(" ::= ");
Lev Walkin0056aec2004-09-05 10:38:50 +0000741 asn1print_value(tc->value, flags);
742 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000743 }
744
Lev Walkinfd151ce2004-08-22 03:08:51 +0000745 /*
Lev Walkinf4069d22005-02-15 07:06:05 +0000746 * The following section exists entirely for debugging.
Lev Walkinfd151ce2004-08-22 03:08:51 +0000747 */
Lev Walkind370e9f2006-03-16 10:03:35 +0000748 if(flags & APF_PRINT_CONSTRAINTS
Lev Walkinb1ef3962004-08-20 13:24:28 +0000749 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000750 asn1p_expr_t *top_parent;
751
752 if(tc->combined_constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700753 safe_printf("\n-- Combined constraints: ");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000754 asn1print_constraint(tc->combined_constraints, flags);
755 }
756
Lev Walkinc0e03b92017-08-22 01:48:23 -0700757 top_parent = WITH_MODULE_NAMESPACE(
758 tc->module, tc_ns, asn1f_find_terminal_type_ex(asn, tc_ns, tc));
759 if(top_parent) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700760 safe_printf("\n-- Practical constraints (%s): ",
Lev Walkine8e87f12004-08-25 02:00:03 +0000761 top_parent->Identifier);
Lev Walkina28cbb92017-07-31 20:20:17 -0700762 asn1print_constraint_explain(top_parent->Identifier,
763 top_parent->expr_type,
Lev Walkine8e87f12004-08-25 02:00:03 +0000764 tc->combined_constraints, 0);
Lev Walkina2abcaa2017-07-23 21:08:49 +0400765 safe_printf("\n-- OER-visible constraints (%s): ",
766 top_parent->Identifier);
Lev Walkina28cbb92017-07-31 20:20:17 -0700767 asn1print_constraint_explain(top_parent->Identifier,
768 top_parent->expr_type,
Lev Walkina2abcaa2017-07-23 21:08:49 +0400769 tc->combined_constraints, CPR_strict_OER_visibility);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700770 safe_printf("\n-- PER-visible constraints (%s): ",
Lev Walkinb1ef3962004-08-20 13:24:28 +0000771 top_parent->Identifier);
Lev Walkina28cbb92017-07-31 20:20:17 -0700772 asn1print_constraint_explain(top_parent->Identifier,
773 top_parent->expr_type,
Lev Walkina2abcaa2017-07-23 21:08:49 +0400774 tc->combined_constraints, CPR_strict_PER_visibility);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000775 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700776 safe_printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000777 }
778
Lev Walkind0f7b912017-08-07 18:13:04 -0700779 if(flags & APF_PRINT_CLASS_MATRIX) do {
Lev Walkin62d95d22017-08-06 23:41:11 -0700780 size_t col, maxidlen;
Lev Walkin4dcf8362017-08-07 20:10:05 -0700781 if(tc->ioc_table == NULL) {
Lev Walkind0f7b912017-08-07 18:13:04 -0700782 if(tc->expr_type == A1TC_CLASSDEF) {
783 safe_printf("\n-- Information Object Class table is empty");
784 }
Lev Walkind370e9f2006-03-16 10:03:35 +0000785 break;
786 }
Lev Walkind0f7b912017-08-07 18:13:04 -0700787 safe_printf("\n-- Information Object Set has %d entr%s:\n",
Lev Walkin4dcf8362017-08-07 20:10:05 -0700788 tc->ioc_table->rows,
789 tc->ioc_table->rows==1 ? "y" : "ies");
790 maxidlen = asn1p_ioc_table_max_identifier_length(tc->ioc_table);
791 for(ssize_t r = -1; r < (ssize_t)tc->ioc_table->rows; r++) {
792 asn1p_ioc_row_t *row;
793 row = tc->ioc_table->row[r<0?0:r];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700794 if(r < 0) safe_printf("-- %s", r > 9 ? " " : "");
795 else safe_printf("-- [%*d]", r > 9 ? 2 : 1, r+1);
Lev Walkind370e9f2006-03-16 10:03:35 +0000796 for(col = 0; col < row->columns; col++) {
797 struct asn1p_ioc_cell_s *cell;
798 cell = &row->column[col];
799 if(r < 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700800 safe_printf("[%*s]", maxidlen,
Lev Walkind370e9f2006-03-16 10:03:35 +0000801 cell->field->Identifier);
802 continue;
803 }
804 if(!cell->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700805 safe_printf(" %*s ", maxidlen, "<no entry>");
Lev Walkind370e9f2006-03-16 10:03:35 +0000806 continue;
807 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700808 safe_printf(" %*s ", maxidlen,
Lev Walkind370e9f2006-03-16 10:03:35 +0000809 cell->value->Identifier);
810 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700811 safe_printf("\n");
Lev Walkind370e9f2006-03-16 10:03:35 +0000812 }
813 } while(0);
814
Lev Walkina00d6b32006-03-21 03:40:38 +0000815 if(flags & APF_PRINT_CLASS_MATRIX
816 && tc->lhs_params) do {
817 int i;
818 if(tc->specializations.pspecs_count == 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700819 safe_printf("\n-- No specializations found\n");
Lev Walkina00d6b32006-03-21 03:40:38 +0000820 break;
821 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700822 safe_printf("\n-- Specializations list has %d entr%s:\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000823 tc->specializations.pspecs_count,
824 tc->specializations.pspecs_count == 1 ? "y" : "ies");
825 for(i = 0; i < tc->specializations.pspecs_count; i++) {
826 asn1p_expr_t *se;
827 struct asn1p_pspec_s *pspec;
828 pspec = &tc->specializations.pspec[i];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700829 safe_printf("-- ");
Lev Walkina00d6b32006-03-21 03:40:38 +0000830 TQ_FOR(se, &(pspec->rhs_pspecs->members), next) {
831 asn1print_expr(asn, mod, se, flags, level+1);
832 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700833 safe_printf("\n");
Lev Walkina00d6b32006-03-21 03:40:38 +0000834 }
835 } while(0);
836
Lev Walkinf15320b2004-06-03 03:38:44 +0000837 return 0;
838}
Lev Walkin3140e0e2004-08-18 04:50:37 +0000839
Lev Walkinf7484512004-10-13 09:13:56 +0000840
841static int
842asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
843 asn1p_expr_t *se;
844 int expr_unordered = 0;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000845 int dont_involve_children = 0;
Lev Walkinf7484512004-10-13 09:13:56 +0000846
847 switch(expr->meta_type) {
848 case AMT_TYPE:
849 case AMT_TYPEREF:
850 break;
851 default:
852 if(expr->expr_type == A1TC_UNIVERVAL)
853 break;
854 return 0;
855 }
856
857 if(!expr->Identifier) return 0;
858
Lev Walkinf7484512004-10-13 09:13:56 +0000859 if(flags & APF_LINE_COMMENTS)
860 INDENT("<!-- #line %d -->\n", expr->_lineno);
861 INDENT("<!ELEMENT %s", expr->Identifier);
862
863 if(expr->expr_type == A1TC_REFERENCE) {
Lev Walkinc0e03b92017-08-22 01:48:23 -0700864 se = WITH_MODULE_NAMESPACE(expr->module, expr_ns, asn1f_find_terminal_type_ex(asn, expr_ns, expr));
Lev Walkinf7484512004-10-13 09:13:56 +0000865 if(!se) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700866 safe_printf(" (ANY)");
Lev Walkinf7484512004-10-13 09:13:56 +0000867 return 0;
868 }
869 expr = se;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000870 dont_involve_children = 1;
Lev Walkinf7484512004-10-13 09:13:56 +0000871 }
872
Lev Walkind7963aa2005-03-10 15:16:56 +0000873 if(expr->expr_type == ASN_CONSTR_CHOICE
874 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
875 || expr->expr_type == ASN_CONSTR_SET_OF
876 || expr->expr_type == ASN_CONSTR_SET
Lev Walkin112d69e2005-03-09 20:52:15 +0000877 || expr->expr_type == ASN_BASIC_INTEGER
878 || expr->expr_type == ASN_BASIC_ENUMERATED) {
879 expr_unordered = 1;
880 }
881
Lev Walkinf7484512004-10-13 09:13:56 +0000882 if(TQ_FIRST(&expr->members)) {
883 int extensible = 0;
Lev Walkinfbaeb852006-11-21 10:39:52 +0000884 if(expr->expr_type == ASN_BASIC_BIT_STRING)
885 dont_involve_children = 1;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700886 safe_printf(" (");
Lev Walkinf7484512004-10-13 09:13:56 +0000887 TQ_FOR(se, &(expr->members), next) {
888 if(se->expr_type == A1TC_EXTENSIBLE) {
889 extensible = 1;
890 continue;
891 } else if(!se->Identifier
892 && se->expr_type == A1TC_REFERENCE) {
893 asn1print_ref(se->reference, flags);
894 } else if(se->Identifier) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700895 safe_printf("%s", se->Identifier);
Lev Walkinf7484512004-10-13 09:13:56 +0000896 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700897 safe_printf("ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000898 }
899 if(expr->expr_type != ASN_CONSTR_SET
900 && expr->expr_type != ASN_CONSTR_CHOICE
901 && expr->expr_type != ASN_BASIC_INTEGER
902 && expr->expr_type != ASN_BASIC_ENUMERATED) {
903 if(expr_unordered)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700904 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000905 else if(se->marker.flags)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700906 safe_printf("?");
Lev Walkinfbaeb852006-11-21 10:39:52 +0000907 else if(expr->expr_type == ASN_BASIC_BIT_STRING)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700908 safe_printf("?");
Lev Walkinf7484512004-10-13 09:13:56 +0000909 }
910 if(TQ_NEXT(se, next)
911 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700912 safe_printf(expr_unordered?"|":", ");
Lev Walkinf7484512004-10-13 09:13:56 +0000913 }
914 }
915 if(extensible) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700916 safe_printf(expr_unordered?"|":", ");
917 safe_printf("ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000918 if(expr->expr_type != ASN_CONSTR_SET
919 && expr->expr_type != ASN_CONSTR_CHOICE
920 && expr->expr_type != ASN_BASIC_INTEGER
921 && expr->expr_type != ASN_BASIC_ENUMERATED)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700922 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000923 }
924
Lev Walkinf2b2f372016-03-14 02:23:48 -0700925 safe_printf(")");
Lev Walkinf7484512004-10-13 09:13:56 +0000926 if(expr->expr_type == ASN_CONSTR_SET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700927 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000928
Lev Walkinef39f7e2004-10-14 05:11:25 +0000929 } else switch(expr->expr_type) {
930 case ASN_BASIC_BOOLEAN:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700931 safe_printf(" (true|false)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000932 break;
933 case ASN_CONSTR_CHOICE:
934 case ASN_CONSTR_SET:
935 case ASN_CONSTR_SET_OF:
936 case ASN_CONSTR_SEQUENCE:
937 case ASN_CONSTR_SEQUENCE_OF:
938 case ASN_BASIC_NULL:
939 case A1TC_UNIVERVAL:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700940 safe_printf(" EMPTY");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000941 break;
942 case ASN_TYPE_ANY:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700943 safe_printf(" ANY");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000944 break;
945 case ASN_BASIC_BIT_STRING:
946 case ASN_BASIC_OCTET_STRING:
947 case ASN_BASIC_OBJECT_IDENTIFIER:
948 case ASN_BASIC_RELATIVE_OID:
Lev Walkincc116532004-10-15 06:01:54 +0000949 case ASN_BASIC_INTEGER:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000950 case ASN_BASIC_UTCTime:
951 case ASN_BASIC_GeneralizedTime:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000952 case ASN_STRING_NumericString:
953 case ASN_STRING_PrintableString:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700954 safe_printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000955 break;
956 case ASN_STRING_VisibleString:
957 case ASN_STRING_ISO646String:
958 /* Entity references, but not XML elements may be present */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700959 safe_printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000960 break;
961 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
962 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
963 default:
964 /*
965 * XML elements are allowed.
966 * For example, a UTF8String may contain "<bel/>".
967 */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700968 safe_printf(" ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000969 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700970 safe_printf(">\n");
Lev Walkinf7484512004-10-13 09:13:56 +0000971
972 /*
973 * Display the descendants (children) of the current type.
974 */
Lev Walkinef39f7e2004-10-14 05:11:25 +0000975 if(!dont_involve_children) {
976 TQ_FOR(se, &(expr->members), next) {
977 if(se->expr_type == A1TC_EXTENSIBLE) continue;
978 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
979 }
Lev Walkinf7484512004-10-13 09:13:56 +0000980 }
981
982 return 0;
983}