blob: 0cbf9646f1af266dd33b3e33742411db25460a25 [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
7#include <asn1parser.h>
Lev Walkin3140e0e2004-08-18 04:50:37 +00008#include <asn1fix_export.h>
Lev Walkin9095ada2004-08-18 05:41:05 +00009#include <asn1fix_crange.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000010
11#include "asn1print.h"
12
Lev Walkin1ec76052016-03-13 17:13:20 -070013#define INDENT(fmt, args...) do { \
14 if(!(flags & APF_NOINDENT)) { \
15 int tmp_i = level; \
Lev Walkinf2b2f372016-03-14 02:23:48 -070016 while(tmp_i--) safe_printf(" "); \
Lev Walkin1ec76052016-03-13 17:13:20 -070017 } \
Lev Walkinf2b2f372016-03-14 02:23:48 -070018 safe_printf(fmt, ##args); \
Lev Walkin1ec76052016-03-13 17:13:20 -070019 } while(0)
Lev Walkinf15320b2004-06-03 03:38:44 +000020
Lev Walkin3140e0e2004-08-18 04:50:37 +000021static int asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags);
Lev Walkind6db8022005-03-18 03:53:05 +000022static int asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags);
Lev Walkin3140e0e2004-08-18 04:50:37 +000023static int asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags);
24static int asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags);
25static int asn1print_params(asn1p_paramlist_t *pl,enum asn1print_flags flags);
26static int asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags);
27static int asn1print_constraint(asn1p_constraint_t *, enum asn1print_flags);
28static int asn1print_value(asn1p_value_t *val, enum asn1print_flags flags);
Lev Walkinf7484512004-10-13 09:13:56 +000029static int asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level);
30static 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 +000031
Lev Walkinf2b2f372016-03-14 02:23:48 -070032/* Check printf's error code, to be pedantic. */
33static int safe_printf(const char *fmt, ...) {
34 va_list ap;
35 va_start(ap, fmt);
36 int ret = vprintf(fmt, ap);
37 assert(ret >= 0);
38 va_end(ap);
39 return ret;
40}
41
42/* Pedantically check fwrite's return value. */
43static size_t safe_fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream) {
44 size_t ret = fwrite(ptr, 1, size * nitems, stream);
45 assert(ret == size * nitems);
46 return ret;
47}
48
Lev Walkinf15320b2004-06-03 03:38:44 +000049/*
50 * Print the contents of the parsed ASN tree.
51 */
52int
Lev Walkin3140e0e2004-08-18 04:50:37 +000053asn1print(asn1p_t *asn, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +000054 asn1p_module_t *mod;
Lev Walkinc70c2ef2004-09-30 06:38:21 +000055 int modno = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000056
57 if(asn == NULL) {
58 errno = EINVAL;
59 return -1;
60 }
61
Lev Walkinf7484512004-10-13 09:13:56 +000062 if(flags & APF_PRINT_XML_DTD)
Lev Walkinf2b2f372016-03-14 02:23:48 -070063 safe_printf("<!-- XML DTD generated by asn1c-" VERSION " -->\n\n");
Lev Walkinf7484512004-10-13 09:13:56 +000064
Lev Walkinf15320b2004-06-03 03:38:44 +000065 TQ_FOR(mod, &(asn->modules), mod_next) {
Lev Walkin6b3ff542006-03-06 14:51:00 +000066 if(mod->_tags & MT_STANDARD_MODULE)
67 return 0; /* Ignore modules imported from skeletons */
Lev Walkinf2b2f372016-03-14 02:23:48 -070068 if(modno++) safe_printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +000069 asn1print_module(asn, mod, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +000070 }
71
Lev Walkin112d69e2005-03-09 20:52:15 +000072 if(flags & APF_PRINT_XML_DTD) {
73 /* Values for BOOLEAN */
Lev Walkinf2b2f372016-03-14 02:23:48 -070074 safe_printf("<!ELEMENT true EMPTY>\n");
75 safe_printf("<!ELEMENT false EMPTY>\n");
Lev Walkin112d69e2005-03-09 20:52:15 +000076 }
77
Lev Walkinf15320b2004-06-03 03:38:44 +000078 return 0;
79}
80
81static int
Lev Walkin3140e0e2004-08-18 04:50:37 +000082asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +000083 asn1p_expr_t *tc;
84
Lev Walkinf7484512004-10-13 09:13:56 +000085 if(flags & APF_PRINT_XML_DTD)
Lev Walkinf2b2f372016-03-14 02:23:48 -070086 safe_printf("<!-- ASN.1 module\n");
Lev Walkinf7484512004-10-13 09:13:56 +000087
Lev Walkinf2b2f372016-03-14 02:23:48 -070088 safe_printf("%s ", mod->ModuleName);
Lev Walkinf15320b2004-06-03 03:38:44 +000089 if(mod->module_oid) {
Lev Walkinb36317c2005-08-12 10:09:10 +000090 asn1print_oid(strlen(mod->ModuleName), mod->module_oid, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -070091 safe_printf("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000092 }
93
Lev Walkinf7484512004-10-13 09:13:56 +000094 if(flags & APF_PRINT_XML_DTD) {
95 if(mod->source_file_name
96 && strcmp(mod->source_file_name, "-"))
Lev Walkinf2b2f372016-03-14 02:23:48 -070097 safe_printf("found in %s", mod->source_file_name);
98 safe_printf(" -->\n\n");
Lev Walkinf7484512004-10-13 09:13:56 +000099
100 TQ_FOR(tc, &(mod->members), next) {
101 asn1print_expr_dtd(asn, mod, tc, flags, 0);
102 }
103
104 return 0;
105 }
106
Lev Walkinf2b2f372016-03-14 02:23:48 -0700107 safe_printf("DEFINITIONS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000108
Lev Walkin3140e0e2004-08-18 04:50:37 +0000109 if(mod->module_flags & MSF_TAG_INSTRUCTIONS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700110 safe_printf(" TAG INSTRUCTIONS");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000111 if(mod->module_flags & MSF_XER_INSTRUCTIONS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700112 safe_printf(" XER INSTRUCTIONS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000113 if(mod->module_flags & MSF_EXPLICIT_TAGS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700114 safe_printf(" EXPLICIT TAGS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000115 if(mod->module_flags & MSF_IMPLICIT_TAGS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700116 safe_printf(" IMPLICIT TAGS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000117 if(mod->module_flags & MSF_AUTOMATIC_TAGS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700118 safe_printf(" AUTOMATIC TAGS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000119 if(mod->module_flags & MSF_EXTENSIBILITY_IMPLIED)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700120 safe_printf(" EXTENSIBILITY IMPLIED");
Lev Walkinf15320b2004-06-03 03:38:44 +0000121
Lev Walkinf2b2f372016-03-14 02:23:48 -0700122 safe_printf(" ::=\n");
123 safe_printf("BEGIN\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000124
125 TQ_FOR(tc, &(mod->members), next) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000126 asn1print_expr(asn, mod, tc, flags, 0);
Lev Walkind370e9f2006-03-16 10:03:35 +0000127 if(flags & APF_PRINT_CONSTRAINTS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700128 safe_printf("\n");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000129 else
Lev Walkinf2b2f372016-03-14 02:23:48 -0700130 safe_printf("\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000131 }
132
Lev Walkinf2b2f372016-03-14 02:23:48 -0700133 safe_printf("END\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000134
135 return 0;
136}
137
138static int
Lev Walkind6db8022005-03-18 03:53:05 +0000139asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags) {
140 size_t accum = prior_len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000141 int ac;
Lev Walkinf15320b2004-06-03 03:38:44 +0000142
Lev Walkind9bd7752004-06-05 08:17:50 +0000143 (void)flags; /* Unused argument */
144
Lev Walkinf2b2f372016-03-14 02:23:48 -0700145 safe_printf("{");
Lev Walkinf15320b2004-06-03 03:38:44 +0000146 for(ac = 0; ac < oid->arcs_count; ac++) {
Lev Walkin4efbfb72005-02-25 14:20:30 +0000147 const char *arcname = oid->arcs[ac].name;
148
Lev Walkin5b7eeaf2005-03-28 17:52:43 +0000149 if(accum + strlen(arcname ? arcname : "") > 72) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700150 safe_printf("\n\t");
Lev Walkin5b7eeaf2005-03-28 17:52:43 +0000151 accum = 8;
Lev Walkind6db8022005-03-18 03:53:05 +0000152 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700153 accum += safe_printf(" ");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000154 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000155
Lev Walkin4efbfb72005-02-25 14:20:30 +0000156 if(arcname) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700157 accum += safe_printf("%s", arcname);
Lev Walkin0056aec2004-09-05 10:38:50 +0000158 if(oid->arcs[ac].number >= 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700159 accum += safe_printf("(%" PRIdASN ")",
Lev Walkind6db8022005-03-18 03:53:05 +0000160 oid->arcs[ac].number);
Lev Walkin0056aec2004-09-05 10:38:50 +0000161 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700163 accum += safe_printf("%" PRIdASN, oid->arcs[ac].number);
Lev Walkinf15320b2004-06-03 03:38:44 +0000164 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700166 safe_printf(" }");
Lev Walkinf15320b2004-06-03 03:38:44 +0000167
168 return 0;
169}
170
171static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000172asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 int cc;
174
Lev Walkind9bd7752004-06-05 08:17:50 +0000175 (void)flags; /* Unused argument */
176
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 for(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 Walkinf2b2f372016-03-14 02:23:48 -0700217 safe_printf("%" PRIdASN, 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 Walkinf2b2f372016-03-14 02:23:48 -0700479 safe_printf("%" PRIdASN, edge->value);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000480 }
481 }
482 return 0;
483}
484
485static int
Lev Walkine8e87f12004-08-25 02:00:03 +0000486asn1print_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 +0000487 asn1cnst_range_t *range;
488 int as_char = (type==ACT_CT_FROM);
489 int i;
490
Lev Walkin4b553412005-08-14 14:45:44 +0000491 range = asn1constraint_compute_PER_range(expr_type, ct, type, 0, 0,
492 strict_PER_visible ? CPR_strict_PER_visibility : 0);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000493 if(!range) return -1;
494
Lev Walkine8e87f12004-08-25 02:00:03 +0000495 if(range->incompatible
496 || (strict_PER_visible && range->not_PER_visible)) {
497 asn1constraint_range_free(range);
498 return 0;
499 }
500
Lev Walkin3140e0e2004-08-18 04:50:37 +0000501 switch(type) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700502 case ACT_CT_FROM: safe_printf("(FROM("); break;
503 case ACT_CT_SIZE: safe_printf("(SIZE("); break;
504 default: safe_printf("("); break;
Lev Walkin3140e0e2004-08-18 04:50:37 +0000505 }
506 for(i = -1; i < range->el_count; i++) {
507 asn1cnst_range_t *r;
508 if(i == -1) {
509 if(range->el_count) continue;
510 r = range;
511 } else {
512 r = range->elements[i];
513 }
514 if(i > 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700515 safe_printf(" | ");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000516 }
517 asn1print_crange_value(&r->left, as_char);
518 if(r->left.type != r->right.type
519 || r->left.value != r->right.value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700520 safe_printf("..");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000521 asn1print_crange_value(&r->right, as_char);
522 }
523 }
524 if(range->extensible)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700525 safe_printf(",...");
526 safe_printf(type==ACT_EL_RANGE?")":"))");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000527
528 if(range->empty_constraint)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700529 safe_printf(":Empty!");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000530
531 asn1constraint_range_free(range);
532 return 0;
533}
534
535static int
536asn1print_constraint_explain(asn1p_expr_type_e expr_type,
Lev Walkine8e87f12004-08-25 02:00:03 +0000537 asn1p_constraint_t *ct, int s_PV) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000538
Lev Walkine8e87f12004-08-25 02:00:03 +0000539 asn1print_constraint_explain_type(expr_type, ct, ACT_EL_RANGE, s_PV);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700540 safe_printf(" ");
Lev Walkine8e87f12004-08-25 02:00:03 +0000541 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_SIZE, s_PV);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700542 safe_printf(" ");
Lev Walkine8e87f12004-08-25 02:00:03 +0000543 asn1print_constraint_explain_type(expr_type, ct, ACT_CT_FROM, s_PV);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000544
545 return 0;
546}
547
548static int
549asn1print_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 +0000550 int SEQ_OF = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000551
Lev Walkinf4069d22005-02-15 07:06:05 +0000552 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
Lev Walkinf7484512004-10-13 09:13:56 +0000553 INDENT("-- #line %d\n", tc->_lineno);
Lev Walkinef625402005-09-05 05:17:57 +0000554
555 /* Reconstruct compiler directive information */
556 if((tc->marker.flags & EM_INDIRECT)
557 && (tc->marker.flags & EM_OMITABLE) != EM_OMITABLE) {
558 if((flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700559 safe_printf(" --<ASN1C.RepresentAsPointer>-- ");
Lev Walkinef625402005-09-05 05:17:57 +0000560 else
561 INDENT("--<ASN1C.RepresentAsPointer>--\n");
562 }
563
Lev Walkina00d6b32006-03-21 03:40:38 +0000564 if(tc->Identifier
565 && (!(tc->meta_type == AMT_VALUE && tc->expr_type == A1TC_REFERENCE)
566 || level == 0))
Lev Walkinf15320b2004-06-03 03:38:44 +0000567 INDENT("%s", tc->Identifier);
568
Lev Walkina00d6b32006-03-21 03:40:38 +0000569 if(tc->lhs_params) {
570 asn1print_params(tc->lhs_params, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000571 }
572
573 if(tc->meta_type != AMT_VALUE
Lev Walkinc74ea222004-08-25 02:27:47 +0000574 && tc->meta_type != AMT_VALUESET
Lev Walkinf15320b2004-06-03 03:38:44 +0000575 && tc->expr_type != A1TC_EXTENSIBLE) {
576 if(level) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000577 if(tc->Identifier && !(flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700578 safe_printf("\t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000579 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700580 safe_printf(" ::=");
Lev Walkinf15320b2004-06-03 03:38:44 +0000581 }
582 }
583
584 if(tc->tag.tag_class) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700585 safe_printf(" ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000586 asn1print_tag(tc, flags);
587 }
588
589 switch(tc->expr_type) {
590 case A1TC_EXTENSIBLE:
591 if(tc->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700592 safe_printf("!");
Lev Walkinf15320b2004-06-03 03:38:44 +0000593 asn1print_value(tc->value, flags);
594 }
595 break;
Lev Walkinfd151ce2004-08-22 03:08:51 +0000596 case A1TC_COMPONENTS_OF:
597 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700598 safe_printf(" COMPONENTS OF");
Lev Walkinfd151ce2004-08-22 03:08:51 +0000599 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000600 case A1TC_REFERENCE:
601 case A1TC_UNIVERVAL:
Lev Walkinf15320b2004-06-03 03:38:44 +0000602 break;
603 case A1TC_CLASSDEF:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700604 safe_printf(" CLASS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000605 break;
Lev Walkin9c2285a2006-03-09 08:49:26 +0000606 case A1TC_CLASSFIELD_TFS ... A1TC_CLASSFIELD_OSFS:
Lev Walkinf15320b2004-06-03 03:38:44 +0000607 /* Nothing to print here */
608 break;
Lev Walkinb1ef3962004-08-20 13:24:28 +0000609 case ASN_CONSTR_SET_OF:
610 case ASN_CONSTR_SEQUENCE_OF:
611 SEQ_OF = 1;
612 if(tc->expr_type == ASN_CONSTR_SET_OF)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700613 safe_printf(" SET");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000614 else
Lev Walkinf2b2f372016-03-14 02:23:48 -0700615 safe_printf(" SEQUENCE");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000616 if(tc->constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700617 safe_printf(" ");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000618 asn1print_constraint(tc->constraints, flags);
619 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700620 safe_printf(" OF");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000621 break;
Lev Walkin5045dfa2006-03-21 09:41:28 +0000622 case A1TC_VALUESET:
623 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000624 default:
625 {
626 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700627 safe_printf(" %s", p?p:"<unknown type!>");
Lev Walkinf15320b2004-06-03 03:38:44 +0000628 }
629 break;
630 }
631
Lev Walkinb9d0ae32005-06-02 04:06:24 +0000632 /*
633 * Put the name of the referred type.
634 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000635 if(tc->reference) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700636 safe_printf(" ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000637 asn1print_ref(tc->reference, flags);
638 }
639
Lev Walkin5045dfa2006-03-21 09:41:28 +0000640 if(tc->meta_type == AMT_VALUESET && level == 0)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700641 safe_printf(" ::=");
Lev Walkinc74ea222004-08-25 02:27:47 +0000642
Lev Walkinf15320b2004-06-03 03:38:44 +0000643 /*
644 * Display the descendants (children) of the current type.
645 */
Lev Walkinc74ea222004-08-25 02:27:47 +0000646 if(TQ_FIRST(&(tc->members))
647 || (tc->expr_type & ASN_CONSTR_MASK)
Lev Walkinc74ea222004-08-25 02:27:47 +0000648 || tc->meta_type == AMT_OBJECT
Lev Walkin9c2285a2006-03-09 08:49:26 +0000649 || tc->meta_type == AMT_OBJECTCLASS
650 || tc->meta_type == AMT_OBJECTFIELD
Lev Walkinc74ea222004-08-25 02:27:47 +0000651 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000652 asn1p_expr_t *se; /* SubExpression */
Lev Walkin9c2285a2006-03-09 08:49:26 +0000653 int put_braces = (!SEQ_OF) /* Don't need 'em, if SET OF... */
654 && (tc->meta_type != AMT_OBJECTFIELD);
Lev Walkinf15320b2004-06-03 03:38:44 +0000655
Lev Walkinc74ea222004-08-25 02:27:47 +0000656 if(put_braces) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000657 if(flags & APF_NOINDENT) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700658 safe_printf("{");
Lev Walkinf4069d22005-02-15 07:06:05 +0000659 if(!TQ_FIRST(&tc->members))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700660 safe_printf("}");
Lev Walkinf4069d22005-02-15 07:06:05 +0000661 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700662 safe_printf(" {");
Lev Walkinf4069d22005-02-15 07:06:05 +0000663 if(TQ_FIRST(&tc->members))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700664 safe_printf("\n");
Lev Walkinf4069d22005-02-15 07:06:05 +0000665 else
Lev Walkinf2b2f372016-03-14 02:23:48 -0700666 safe_printf(" }");
Lev Walkinf4069d22005-02-15 07:06:05 +0000667 }
Lev Walkinc74ea222004-08-25 02:27:47 +0000668 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000669
670 TQ_FOR(se, &(tc->members), next) {
671 /*
Lev Walkinfd151ce2004-08-22 03:08:51 +0000672 * Print the expression as it were a stand-alone type.
Lev Walkinf15320b2004-06-03 03:38:44 +0000673 */
Lev Walkinf7484512004-10-13 09:13:56 +0000674 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinb1e07082004-09-15 11:44:55 +0000675 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700676 safe_printf(" DEFAULT ");
Lev Walkinb1e07082004-09-15 11:44:55 +0000677 asn1print_value(se->marker.default_value, flags);
678 } else if((se->marker.flags & EM_OPTIONAL)
679 == EM_OPTIONAL) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700680 safe_printf(" OPTIONAL");
Lev Walkinb1e07082004-09-15 11:44:55 +0000681 }
Lev Walkinef625402005-09-05 05:17:57 +0000682 if(TQ_NEXT(se, next)) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700683 safe_printf(",");
Lev Walkinef625402005-09-05 05:17:57 +0000684 if(!(flags & APF_NOINDENT))
685 INDENT("\n");
686 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000687 }
688
Lev Walkinc74ea222004-08-25 02:27:47 +0000689 if(put_braces && TQ_FIRST(&tc->members)) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000690 if(!(flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700691 safe_printf("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000692 INDENT("}");
693 }
694 }
695
Lev Walkin9d542d22006-03-14 16:31:37 +0000696 if(tc->with_syntax) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700697 safe_printf(" WITH SYNTAX {");
Lev Walkinf15320b2004-06-03 03:38:44 +0000698 asn1print_with_syntax(tc->with_syntax, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700699 safe_printf("}\n");
Lev Walkin9d542d22006-03-14 16:31:37 +0000700 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000701
Lev Walkina00d6b32006-03-21 03:40:38 +0000702 /* Right hand specialization */
703 if(tc->rhs_pspecs) {
704 asn1p_expr_t *se;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700705 safe_printf("{");
Lev Walkina00d6b32006-03-21 03:40:38 +0000706 TQ_FOR(se, &(tc->rhs_pspecs->members), next) {
707 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700708 if(TQ_NEXT(se, next)) safe_printf(", ");
Lev Walkina00d6b32006-03-21 03:40:38 +0000709 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700710 safe_printf("}");
Lev Walkina00d6b32006-03-21 03:40:38 +0000711 }
712
Lev Walkinb1ef3962004-08-20 13:24:28 +0000713 if(!SEQ_OF && tc->constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700714 safe_printf(" ");
Lev Walkin557f27d2006-03-21 07:46:48 +0000715 if(tc->meta_type == AMT_VALUESET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700716 safe_printf("{");
Lev Walkinf15320b2004-06-03 03:38:44 +0000717 asn1print_constraint(tc->constraints, flags);
Lev Walkin557f27d2006-03-21 07:46:48 +0000718 if(tc->meta_type == AMT_VALUESET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700719 safe_printf("}");
Lev Walkinf15320b2004-06-03 03:38:44 +0000720 }
Lev Walkin3140e0e2004-08-18 04:50:37 +0000721
Lev Walkinf15320b2004-06-03 03:38:44 +0000722 if(tc->unique) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700723 safe_printf(" UNIQUE");
Lev Walkinf15320b2004-06-03 03:38:44 +0000724 }
725
726 if(tc->meta_type == AMT_VALUE
727 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin0056aec2004-09-05 10:38:50 +0000728 if(tc->expr_type == A1TC_UNIVERVAL) {
Lev Walkinb1e07082004-09-15 11:44:55 +0000729 if(tc->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700730 safe_printf("(");
Lev Walkinb1e07082004-09-15 11:44:55 +0000731 asn1print_value(tc->value, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700732 safe_printf(")");
Lev Walkinb1e07082004-09-15 11:44:55 +0000733 }
Lev Walkin0056aec2004-09-05 10:38:50 +0000734 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700735 if(level == 0) safe_printf(" ::= ");
Lev Walkin0056aec2004-09-05 10:38:50 +0000736 asn1print_value(tc->value, flags);
737 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000738 }
739
Lev Walkinfd151ce2004-08-22 03:08:51 +0000740 /*
Lev Walkinf4069d22005-02-15 07:06:05 +0000741 * The following section exists entirely for debugging.
Lev Walkinfd151ce2004-08-22 03:08:51 +0000742 */
Lev Walkind370e9f2006-03-16 10:03:35 +0000743 if(flags & APF_PRINT_CONSTRAINTS
Lev Walkinb1ef3962004-08-20 13:24:28 +0000744 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000745 asn1p_expr_t *top_parent;
746
747 if(tc->combined_constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700748 safe_printf("\n-- Combined constraints: ");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000749 asn1print_constraint(tc->combined_constraints, flags);
750 }
751
Lev Walkine4d6ab82004-09-22 16:05:13 +0000752 top_parent = asn1f_find_terminal_type_ex(asn, tc);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000753 if(top_parent) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700754 safe_printf("\n-- Practical constraints (%s): ",
Lev Walkine8e87f12004-08-25 02:00:03 +0000755 top_parent->Identifier);
756 asn1print_constraint_explain(top_parent->expr_type,
757 tc->combined_constraints, 0);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700758 safe_printf("\n-- PER-visible constraints (%s): ",
Lev Walkinb1ef3962004-08-20 13:24:28 +0000759 top_parent->Identifier);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000760 asn1print_constraint_explain(top_parent->expr_type,
Lev Walkine8e87f12004-08-25 02:00:03 +0000761 tc->combined_constraints, 1);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000762 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700763 safe_printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000764 }
765
Lev Walkind370e9f2006-03-16 10:03:35 +0000766 if(flags & APF_PRINT_CLASS_MATRIX
767 && tc->expr_type == A1TC_CLASSDEF) do {
768 int r, col, maxidlen;
769 if(tc->object_class_matrix.rows == 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700770 safe_printf("\n-- Class matrix is empty");
Lev Walkind370e9f2006-03-16 10:03:35 +0000771 break;
772 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700773 safe_printf("\n-- Class matrix has %d entr%s:\n",
Lev Walkind370e9f2006-03-16 10:03:35 +0000774 tc->object_class_matrix.rows,
775 tc->object_class_matrix.rows==1 ? "y" : "ies");
776 maxidlen = tc->object_class_matrix.max_identifier_length;
777 for(r = -1; r < tc->object_class_matrix.rows; r++) {
778 struct asn1p_ioc_row_s *row;
779 row = tc->object_class_matrix.row[r<0?0:r];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700780 if(r < 0) safe_printf("-- %s", r > 9 ? " " : "");
781 else safe_printf("-- [%*d]", r > 9 ? 2 : 1, r+1);
Lev Walkind370e9f2006-03-16 10:03:35 +0000782 for(col = 0; col < row->columns; col++) {
783 struct asn1p_ioc_cell_s *cell;
784 cell = &row->column[col];
785 if(r < 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700786 safe_printf("[%*s]", maxidlen,
Lev Walkind370e9f2006-03-16 10:03:35 +0000787 cell->field->Identifier);
788 continue;
789 }
790 if(!cell->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700791 safe_printf(" %*s ", maxidlen, "<no entry>");
Lev Walkind370e9f2006-03-16 10:03:35 +0000792 continue;
793 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700794 safe_printf(" %*s ", maxidlen,
Lev Walkind370e9f2006-03-16 10:03:35 +0000795 cell->value->Identifier);
796 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700797 safe_printf("\n");
Lev Walkind370e9f2006-03-16 10:03:35 +0000798 }
799 } while(0);
800
Lev Walkina00d6b32006-03-21 03:40:38 +0000801 if(flags & APF_PRINT_CLASS_MATRIX
802 && tc->lhs_params) do {
803 int i;
804 if(tc->specializations.pspecs_count == 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700805 safe_printf("\n-- No specializations found\n");
Lev Walkina00d6b32006-03-21 03:40:38 +0000806 break;
807 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700808 safe_printf("\n-- Specializations list has %d entr%s:\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000809 tc->specializations.pspecs_count,
810 tc->specializations.pspecs_count == 1 ? "y" : "ies");
811 for(i = 0; i < tc->specializations.pspecs_count; i++) {
812 asn1p_expr_t *se;
813 struct asn1p_pspec_s *pspec;
814 pspec = &tc->specializations.pspec[i];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700815 safe_printf("-- ");
Lev Walkina00d6b32006-03-21 03:40:38 +0000816 TQ_FOR(se, &(pspec->rhs_pspecs->members), next) {
817 asn1print_expr(asn, mod, se, flags, level+1);
818 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700819 safe_printf("\n");
Lev Walkina00d6b32006-03-21 03:40:38 +0000820 }
821 } while(0);
822
Lev Walkinf15320b2004-06-03 03:38:44 +0000823 return 0;
824}
Lev Walkin3140e0e2004-08-18 04:50:37 +0000825
Lev Walkinf7484512004-10-13 09:13:56 +0000826
827static int
828asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
829 asn1p_expr_t *se;
830 int expr_unordered = 0;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000831 int dont_involve_children = 0;
Lev Walkinf7484512004-10-13 09:13:56 +0000832
833 switch(expr->meta_type) {
834 case AMT_TYPE:
835 case AMT_TYPEREF:
836 break;
837 default:
838 if(expr->expr_type == A1TC_UNIVERVAL)
839 break;
840 return 0;
841 }
842
843 if(!expr->Identifier) return 0;
844
Lev Walkinf7484512004-10-13 09:13:56 +0000845 if(flags & APF_LINE_COMMENTS)
846 INDENT("<!-- #line %d -->\n", expr->_lineno);
847 INDENT("<!ELEMENT %s", expr->Identifier);
848
849 if(expr->expr_type == A1TC_REFERENCE) {
850 se = asn1f_find_terminal_type_ex(asn, expr);
851 if(!se) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700852 safe_printf(" (ANY)");
Lev Walkinf7484512004-10-13 09:13:56 +0000853 return 0;
854 }
855 expr = se;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000856 dont_involve_children = 1;
Lev Walkinf7484512004-10-13 09:13:56 +0000857 }
858
Lev Walkind7963aa2005-03-10 15:16:56 +0000859 if(expr->expr_type == ASN_CONSTR_CHOICE
860 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
861 || expr->expr_type == ASN_CONSTR_SET_OF
862 || expr->expr_type == ASN_CONSTR_SET
Lev Walkin112d69e2005-03-09 20:52:15 +0000863 || expr->expr_type == ASN_BASIC_INTEGER
864 || expr->expr_type == ASN_BASIC_ENUMERATED) {
865 expr_unordered = 1;
866 }
867
Lev Walkinf7484512004-10-13 09:13:56 +0000868 if(TQ_FIRST(&expr->members)) {
869 int extensible = 0;
Lev Walkinfbaeb852006-11-21 10:39:52 +0000870 if(expr->expr_type == ASN_BASIC_BIT_STRING)
871 dont_involve_children = 1;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700872 safe_printf(" (");
Lev Walkinf7484512004-10-13 09:13:56 +0000873 TQ_FOR(se, &(expr->members), next) {
874 if(se->expr_type == A1TC_EXTENSIBLE) {
875 extensible = 1;
876 continue;
877 } else if(!se->Identifier
878 && se->expr_type == A1TC_REFERENCE) {
879 asn1print_ref(se->reference, flags);
880 } else if(se->Identifier) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700881 safe_printf("%s", se->Identifier);
Lev Walkinf7484512004-10-13 09:13:56 +0000882 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700883 safe_printf("ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000884 }
885 if(expr->expr_type != ASN_CONSTR_SET
886 && expr->expr_type != ASN_CONSTR_CHOICE
887 && expr->expr_type != ASN_BASIC_INTEGER
888 && expr->expr_type != ASN_BASIC_ENUMERATED) {
889 if(expr_unordered)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700890 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000891 else if(se->marker.flags)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700892 safe_printf("?");
Lev Walkinfbaeb852006-11-21 10:39:52 +0000893 else if(expr->expr_type == ASN_BASIC_BIT_STRING)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700894 safe_printf("?");
Lev Walkinf7484512004-10-13 09:13:56 +0000895 }
896 if(TQ_NEXT(se, next)
897 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700898 safe_printf(expr_unordered?"|":", ");
Lev Walkinf7484512004-10-13 09:13:56 +0000899 }
900 }
901 if(extensible) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700902 safe_printf(expr_unordered?"|":", ");
903 safe_printf("ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000904 if(expr->expr_type != ASN_CONSTR_SET
905 && expr->expr_type != ASN_CONSTR_CHOICE
906 && expr->expr_type != ASN_BASIC_INTEGER
907 && expr->expr_type != ASN_BASIC_ENUMERATED)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700908 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000909 }
910
Lev Walkinf2b2f372016-03-14 02:23:48 -0700911 safe_printf(")");
Lev Walkinf7484512004-10-13 09:13:56 +0000912 if(expr->expr_type == ASN_CONSTR_SET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700913 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000914
Lev Walkinef39f7e2004-10-14 05:11:25 +0000915 } else switch(expr->expr_type) {
916 case ASN_BASIC_BOOLEAN:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700917 safe_printf(" (true|false)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000918 break;
919 case ASN_CONSTR_CHOICE:
920 case ASN_CONSTR_SET:
921 case ASN_CONSTR_SET_OF:
922 case ASN_CONSTR_SEQUENCE:
923 case ASN_CONSTR_SEQUENCE_OF:
924 case ASN_BASIC_NULL:
925 case A1TC_UNIVERVAL:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700926 safe_printf(" EMPTY");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000927 break;
928 case ASN_TYPE_ANY:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700929 safe_printf(" ANY");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000930 break;
931 case ASN_BASIC_BIT_STRING:
932 case ASN_BASIC_OCTET_STRING:
933 case ASN_BASIC_OBJECT_IDENTIFIER:
934 case ASN_BASIC_RELATIVE_OID:
Lev Walkincc116532004-10-15 06:01:54 +0000935 case ASN_BASIC_INTEGER:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000936 case ASN_BASIC_UTCTime:
937 case ASN_BASIC_GeneralizedTime:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000938 case ASN_STRING_NumericString:
939 case ASN_STRING_PrintableString:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700940 safe_printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000941 break;
942 case ASN_STRING_VisibleString:
943 case ASN_STRING_ISO646String:
944 /* Entity references, but not XML elements may be present */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700945 safe_printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000946 break;
947 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
948 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
949 default:
950 /*
951 * XML elements are allowed.
952 * For example, a UTF8String may contain "<bel/>".
953 */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700954 safe_printf(" ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000955 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700956 safe_printf(">\n");
Lev Walkinf7484512004-10-13 09:13:56 +0000957
958 /*
959 * Display the descendants (children) of the current type.
960 */
Lev Walkinef39f7e2004-10-14 05:11:25 +0000961 if(!dont_involve_children) {
962 TQ_FOR(se, &(expr->members), next) {
963 if(se->expr_type == A1TC_EXTENSIBLE) continue;
964 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
965 }
Lev Walkinf7484512004-10-13 09:13:56 +0000966 }
967
968 return 0;
969}