blob: d4e24bc638f886254e1fa9049bce06489f8edf57 [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 Walkinf4b711a2017-08-23 06:55:27 -07007#include <asn1_buffer.h>
Lev Walkinc0e03b92017-08-22 01:48:23 -07008#include <asn1_namespace.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00009#include <asn1parser.h>
Lev Walkin3140e0e2004-08-18 04:50:37 +000010#include <asn1fix_export.h>
Lev Walkin9095ada2004-08-18 05:41:05 +000011#include <asn1fix_crange.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000012
13#include "asn1print.h"
14
Lev Walkinf4b711a2017-08-23 06:55:27 -070015static abuf all_output_;
16
17typedef enum {
18 PRINT_STDOUT,
19 GLOBAL_BUFFER,
20} print_method_e;
21static print_method_e print_method_;
22
Lev Walkin1ec76052016-03-13 17:13:20 -070023#define INDENT(fmt, args...) do { \
24 if(!(flags & APF_NOINDENT)) { \
25 int tmp_i = level; \
Lev Walkinf2b2f372016-03-14 02:23:48 -070026 while(tmp_i--) safe_printf(" "); \
Lev Walkin1ec76052016-03-13 17:13:20 -070027 } \
Lev Walkinf2b2f372016-03-14 02:23:48 -070028 safe_printf(fmt, ##args); \
Lev Walkin1ec76052016-03-13 17:13:20 -070029 } while(0)
Lev Walkinf15320b2004-06-03 03:38:44 +000030
Lev Walkin3140e0e2004-08-18 04:50:37 +000031static int asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags);
Lev Walkind6db8022005-03-18 03:53:05 +000032static int asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags);
Lev Walkinf4b711a2017-08-23 06:55:27 -070033static int asn1print_ref(const asn1p_ref_t *ref, enum asn1print_flags flags);
34static int asn1print_tag(const asn1p_expr_t *tc, enum asn1print_flags flags);
35static int asn1print_params(const asn1p_paramlist_t *pl,enum asn1print_flags flags);
36static int asn1print_with_syntax(const asn1p_wsyntx_t *wx, enum asn1print_flags flags);
37static int asn1print_constraint(const asn1p_constraint_t *, enum asn1print_flags);
38static int asn1print_value(const asn1p_value_t *val, enum asn1print_flags flags);
Lev Walkinf7484512004-10-13 09:13:56 +000039static int asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level);
40static 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 +000041
Lev Walkinf2b2f372016-03-14 02:23:48 -070042/* Check printf's error code, to be pedantic. */
43static int safe_printf(const char *fmt, ...) {
Lev Walkinf4b711a2017-08-23 06:55:27 -070044 int ret;
Lev Walkinf2b2f372016-03-14 02:23:48 -070045 va_list ap;
46 va_start(ap, fmt);
Lev Walkinf4b711a2017-08-23 06:55:27 -070047
48 switch(print_method_) {
49 case PRINT_STDOUT:
50 ret = vprintf(fmt, ap);
51 break;
52 case GLOBAL_BUFFER:
53 ret = abuf_vprintf(&all_output_, fmt, ap);
54 break;
55 }
Lev Walkinf2b2f372016-03-14 02:23:48 -070056 assert(ret >= 0);
57 va_end(ap);
Lev Walkinf4b711a2017-08-23 06:55:27 -070058
Lev Walkinf2b2f372016-03-14 02:23:48 -070059 return ret;
60}
61
62/* Pedantically check fwrite's return value. */
Lev Walkinf4b711a2017-08-23 06:55:27 -070063static size_t safe_fwrite(const void *ptr, size_t size) {
64 size_t ret;
65
66 switch(print_method_) {
67 case PRINT_STDOUT:
68 ret = fwrite(ptr, 1, size, stdout);
69 assert(ret == size);
70 break;
71 case GLOBAL_BUFFER:
72 abuf_add_bytes(&all_output_, ptr, size);
73 ret = size;
74 break;
75 }
76
Lev Walkinf2b2f372016-03-14 02:23:48 -070077 return ret;
78}
79
Lev Walkinf15320b2004-06-03 03:38:44 +000080/*
81 * Print the contents of the parsed ASN tree.
82 */
83int
Lev Walkin3140e0e2004-08-18 04:50:37 +000084asn1print(asn1p_t *asn, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +000085 asn1p_module_t *mod;
Lev Walkinc70c2ef2004-09-30 06:38:21 +000086 int modno = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000087
88 if(asn == NULL) {
89 errno = EINVAL;
90 return -1;
91 }
92
Lev Walkinf7484512004-10-13 09:13:56 +000093 if(flags & APF_PRINT_XML_DTD)
Lev Walkinf2b2f372016-03-14 02:23:48 -070094 safe_printf("<!-- XML DTD generated by asn1c-" VERSION " -->\n\n");
Lev Walkinf7484512004-10-13 09:13:56 +000095
Lev Walkinf15320b2004-06-03 03:38:44 +000096 TQ_FOR(mod, &(asn->modules), mod_next) {
Lev Walkin6b3ff542006-03-06 14:51:00 +000097 if(mod->_tags & MT_STANDARD_MODULE)
98 return 0; /* Ignore modules imported from skeletons */
Lev Walkinf2b2f372016-03-14 02:23:48 -070099 if(modno++) safe_printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000100 asn1print_module(asn, mod, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000101 }
102
Lev Walkin112d69e2005-03-09 20:52:15 +0000103 if(flags & APF_PRINT_XML_DTD) {
104 /* Values for BOOLEAN */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700105 safe_printf("<!ELEMENT true EMPTY>\n");
106 safe_printf("<!ELEMENT false EMPTY>\n");
Lev Walkin112d69e2005-03-09 20:52:15 +0000107 }
108
Lev Walkinf15320b2004-06-03 03:38:44 +0000109 return 0;
110}
111
112static int
Lev Walkin3140e0e2004-08-18 04:50:37 +0000113asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000114 asn1p_expr_t *tc;
115
Lev Walkinf7484512004-10-13 09:13:56 +0000116 if(flags & APF_PRINT_XML_DTD)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700117 safe_printf("<!-- ASN.1 module\n");
Lev Walkinf7484512004-10-13 09:13:56 +0000118
Lev Walkinf2b2f372016-03-14 02:23:48 -0700119 safe_printf("%s ", mod->ModuleName);
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 if(mod->module_oid) {
Lev Walkinb36317c2005-08-12 10:09:10 +0000121 asn1print_oid(strlen(mod->ModuleName), mod->module_oid, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700122 safe_printf("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000123 }
124
Lev Walkinf7484512004-10-13 09:13:56 +0000125 if(flags & APF_PRINT_XML_DTD) {
126 if(mod->source_file_name
127 && strcmp(mod->source_file_name, "-"))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700128 safe_printf("found in %s", mod->source_file_name);
129 safe_printf(" -->\n\n");
Lev Walkinf7484512004-10-13 09:13:56 +0000130
131 TQ_FOR(tc, &(mod->members), next) {
132 asn1print_expr_dtd(asn, mod, tc, flags, 0);
133 }
134
135 return 0;
136 }
137
Lev Walkinf2b2f372016-03-14 02:23:48 -0700138 safe_printf("DEFINITIONS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000139
Lev Walkin3140e0e2004-08-18 04:50:37 +0000140 if(mod->module_flags & MSF_TAG_INSTRUCTIONS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700141 safe_printf(" TAG INSTRUCTIONS");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000142 if(mod->module_flags & MSF_XER_INSTRUCTIONS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700143 safe_printf(" XER INSTRUCTIONS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 if(mod->module_flags & MSF_EXPLICIT_TAGS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700145 safe_printf(" EXPLICIT TAGS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000146 if(mod->module_flags & MSF_IMPLICIT_TAGS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700147 safe_printf(" IMPLICIT TAGS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 if(mod->module_flags & MSF_AUTOMATIC_TAGS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700149 safe_printf(" AUTOMATIC TAGS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000150 if(mod->module_flags & MSF_EXTENSIBILITY_IMPLIED)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700151 safe_printf(" EXTENSIBILITY IMPLIED");
Lev Walkinf15320b2004-06-03 03:38:44 +0000152
Lev Walkinf2b2f372016-03-14 02:23:48 -0700153 safe_printf(" ::=\n");
154 safe_printf("BEGIN\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000155
156 TQ_FOR(tc, &(mod->members), next) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000157 asn1print_expr(asn, mod, tc, flags, 0);
Lev Walkind370e9f2006-03-16 10:03:35 +0000158 if(flags & APF_PRINT_CONSTRAINTS)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700159 safe_printf("\n");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000160 else
Lev Walkinf2b2f372016-03-14 02:23:48 -0700161 safe_printf("\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 }
163
Lev Walkinf2b2f372016-03-14 02:23:48 -0700164 safe_printf("END\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000165
166 return 0;
167}
168
169static int
Lev Walkind6db8022005-03-18 03:53:05 +0000170asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags) {
171 size_t accum = prior_len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 int ac;
Lev Walkinf15320b2004-06-03 03:38:44 +0000173
Lev Walkind9bd7752004-06-05 08:17:50 +0000174 (void)flags; /* Unused argument */
175
Lev Walkinf2b2f372016-03-14 02:23:48 -0700176 safe_printf("{");
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 for(ac = 0; ac < oid->arcs_count; ac++) {
Lev Walkin4efbfb72005-02-25 14:20:30 +0000178 const char *arcname = oid->arcs[ac].name;
179
Lev Walkin5b7eeaf2005-03-28 17:52:43 +0000180 if(accum + strlen(arcname ? arcname : "") > 72) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700181 safe_printf("\n\t");
Lev Walkin5b7eeaf2005-03-28 17:52:43 +0000182 accum = 8;
Lev Walkind6db8022005-03-18 03:53:05 +0000183 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700184 accum += safe_printf(" ");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000185 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000186
Lev Walkin4efbfb72005-02-25 14:20:30 +0000187 if(arcname) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700188 accum += safe_printf("%s", arcname);
Lev Walkin0056aec2004-09-05 10:38:50 +0000189 if(oid->arcs[ac].number >= 0) {
Lev Walkinda997b12017-08-04 01:38:41 -0700190 accum += safe_printf("(%s)",
191 asn1p_itoa(oid->arcs[ac].number));
Lev Walkin0056aec2004-09-05 10:38:50 +0000192 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 } else {
Lev Walkinda997b12017-08-04 01:38:41 -0700194 accum += safe_printf("%s", asn1p_itoa(oid->arcs[ac].number));
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000196 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700197 safe_printf(" }");
Lev Walkinf15320b2004-06-03 03:38:44 +0000198
199 return 0;
200}
201
202static int
Lev Walkinf4b711a2017-08-23 06:55:27 -0700203asn1print_ref(const asn1p_ref_t *ref, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000204
Lev Walkind9bd7752004-06-05 08:17:50 +0000205 (void)flags; /* Unused argument */
206
Lev Walkin62d95d22017-08-06 23:41:11 -0700207 for(size_t cc = 0; cc < ref->comp_count; cc++) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700208 if(cc) safe_printf(".");
209 safe_printf("%s", ref->components[cc].name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000210 }
211
212 return 0;
213}
214
215static int
Lev Walkinf4b711a2017-08-23 06:55:27 -0700216asn1print_tag(const asn1p_expr_t *tc, enum asn1print_flags flags) {
217 const struct asn1p_type_tag_s *tag = &tc->tag;
Lev Walkinf15320b2004-06-03 03:38:44 +0000218
Lev Walkind9bd7752004-06-05 08:17:50 +0000219 (void)flags; /* Unused argument */
220
Lev Walkinf2b2f372016-03-14 02:23:48 -0700221 safe_printf("%s", asn1p_tag2string(tag, 0));
Lev Walkinf15320b2004-06-03 03:38:44 +0000222
223 return 0;
224}
225
226static int
Lev Walkinf4b711a2017-08-23 06:55:27 -0700227asn1print_value(const asn1p_value_t *val, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000228
229 if(val == NULL)
230 return 0;
231
232 switch(val->type) {
233 case ATV_NOVALUE:
234 break;
Lev Walkinb1e07082004-09-15 11:44:55 +0000235 case ATV_NULL:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700236 safe_printf("NULL");
Lev Walkinb1e07082004-09-15 11:44:55 +0000237 return 0;
238 case ATV_REAL:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700239 safe_printf("%f", val->value.v_double);
Lev Walkinb1e07082004-09-15 11:44:55 +0000240 return 0;
Lev Walkina9532f42006-09-17 04:52:50 +0000241 case ATV_TYPE:
242 asn1print_expr(val->value.v_type->module->asn1p,
243 val->value.v_type->module,
244 val->value.v_type, flags, 0);
245 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000246 case ATV_INTEGER:
Lev Walkinda997b12017-08-04 01:38:41 -0700247 safe_printf("%s", asn1p_itoa(val->value.v_integer));
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 return 0;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700249 case ATV_MIN: safe_printf("MIN"); return 0;
250 case ATV_MAX: safe_printf("MAX"); return 0;
251 case ATV_FALSE: safe_printf("FALSE"); return 0;
252 case ATV_TRUE: safe_printf("TRUE"); return 0;
Lev Walkin1e448d32005-03-24 14:26:38 +0000253 case ATV_TUPLE:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700254 safe_printf("{%d, %d}",
Lev Walkin1e448d32005-03-24 14:26:38 +0000255 (int)(val->value.v_integer >> 4),
256 (int)(val->value.v_integer & 0x0f));
257 return 0;
258 case ATV_QUADRUPLE:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700259 safe_printf("{%d, %d, %d, %d}",
Lev Walkin1e448d32005-03-24 14:26:38 +0000260 (int)((val->value.v_integer >> 24) & 0xff),
261 (int)((val->value.v_integer >> 16) & 0xff),
262 (int)((val->value.v_integer >> 8) & 0xff),
263 (int)((val->value.v_integer >> 0) & 0xff)
264 );
265 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 case ATV_STRING:
267 {
Lev Walkin84fbd722005-06-15 18:41:50 +0000268 char *p = (char *)val->value.string.buf;
Lev Walkinf4b711a2017-08-23 06:55:27 -0700269 safe_printf("\"");
Lev Walkinf15320b2004-06-03 03:38:44 +0000270 if(strchr(p, '"')) {
271 /* Mask quotes */
272 for(; *p; p++) {
273 if(*p == '"')
Lev Walkinf4b711a2017-08-23 06:55:27 -0700274 safe_printf("%c", *p);
275 safe_printf("%c", *p);
Lev Walkinf15320b2004-06-03 03:38:44 +0000276 }
277 } else {
Lev Walkinf4b711a2017-08-23 06:55:27 -0700278 safe_printf("%s", p);
Lev Walkinf15320b2004-06-03 03:38:44 +0000279 }
Lev Walkinf4b711a2017-08-23 06:55:27 -0700280 safe_printf("\"");
Lev Walkinf15320b2004-06-03 03:38:44 +0000281 }
282 return 0;
283 case ATV_UNPARSED:
Lev Walkinf4b711a2017-08-23 06:55:27 -0700284 safe_printf("%s", (char *)val->value.string.buf);
Lev Walkinf15320b2004-06-03 03:38:44 +0000285 return 0;
286 case ATV_BITVECTOR:
287 {
288 uint8_t *bitvector;
289 int bits;
290 int i;
291
292 bitvector = val->value.binary_vector.bits;
293 bits = val->value.binary_vector.size_in_bits;
294
Lev Walkinf2b2f372016-03-14 02:23:48 -0700295 safe_printf("'");
Lev Walkinf15320b2004-06-03 03:38:44 +0000296 if(bits%8) {
297 for(i = 0; i < bits; i++) {
298 uint8_t uc;
299 uc = bitvector[i>>3];
Lev Walkinf4b711a2017-08-23 06:55:27 -0700300 safe_printf("%c", ((uc >> (7-(i%8)))&1)?'1':'0');
Lev Walkinf15320b2004-06-03 03:38:44 +0000301 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700302 safe_printf("'B");
Lev Walkinf15320b2004-06-03 03:38:44 +0000303 } else {
304 char hextable[16] = "0123456789ABCDEF";
305 for(i = 0; i < (bits>>3); i++) {
Lev Walkinf4b711a2017-08-23 06:55:27 -0700306 safe_printf("%c", hextable[bitvector[i] >> 4]);
307 safe_printf("%c", hextable[bitvector[i] & 0x0f]);
Lev Walkinf15320b2004-06-03 03:38:44 +0000308 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700309 safe_printf("'H");
Lev Walkinf15320b2004-06-03 03:38:44 +0000310 }
Lev Walkin043af0d2005-02-24 21:07:35 +0000311 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000312 }
Lev Walkinb1e07082004-09-15 11:44:55 +0000313 case ATV_REFERENCED:
314 return asn1print_ref(val->value.reference, flags);
Lev Walkin5045dfa2006-03-21 09:41:28 +0000315 case ATV_VALUESET:
316 return asn1print_constraint(val->value.constraint, flags);
Lev Walkinb1e07082004-09-15 11:44:55 +0000317 case ATV_CHOICE_IDENTIFIER:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700318 safe_printf("%s: ", val->value.choice_identifier.identifier);
Lev Walkinb1e07082004-09-15 11:44:55 +0000319 return asn1print_value(val->value.choice_identifier.value, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000320 }
321
322 assert(val->type || !"Unknown");
323
324 return 0;
325}
326
Lev Walkinf4b711a2017-08-23 06:55:27 -0700327const char *
328asn1p_constraint_string(const asn1p_constraint_t *ct) {
329 size_t old_len = all_output_.length;
330 print_method_e old_method = print_method_;
331 print_method_ = GLOBAL_BUFFER;
332 asn1print_constraint(ct, APF_NOINDENT);
333 print_method_ = old_method;
334 return &all_output_.buffer[old_len];
335}
336
Lev Walkinf15320b2004-06-03 03:38:44 +0000337static int
Lev Walkinf4b711a2017-08-23 06:55:27 -0700338asn1print_constraint(const asn1p_constraint_t *ct, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000339 int symno = 0;
340
341 if(ct == 0) return 0;
342
343 if(ct->type == ACT_CA_SET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700344 safe_printf("(");
Lev Walkinf15320b2004-06-03 03:38:44 +0000345
346 switch(ct->type) {
Lev Walkinff7dd142005-03-20 12:58:00 +0000347 case ACT_EL_TYPE:
Lev Walkin5045dfa2006-03-21 09:41:28 +0000348 asn1print_value(ct->containedSubtype, flags);
Lev Walkinff7dd142005-03-20 12:58:00 +0000349 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000350 case ACT_EL_VALUE:
351 asn1print_value(ct->value, flags);
352 break;
353 case ACT_EL_RANGE:
354 case ACT_EL_LLRANGE:
355 case ACT_EL_RLRANGE:
356 case ACT_EL_ULRANGE:
357 asn1print_value(ct->range_start, flags);
358 switch(ct->type) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700359 case ACT_EL_RANGE: safe_printf(".."); break;
360 case ACT_EL_LLRANGE: safe_printf("<.."); break;
361 case ACT_EL_RLRANGE: safe_printf("..<"); break;
362 case ACT_EL_ULRANGE: safe_printf("<..<"); break;
363 default: safe_printf("?..?"); break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000364 }
365 asn1print_value(ct->range_stop, flags);
366 break;
367 case ACT_EL_EXT:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700368 safe_printf("...");
Lev Walkinf15320b2004-06-03 03:38:44 +0000369 break;
370 case ACT_CT_SIZE:
371 case ACT_CT_FROM:
372 switch(ct->type) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700373 case ACT_CT_SIZE: safe_printf("SIZE("); break;
374 case ACT_CT_FROM: safe_printf("FROM("); break;
375 default: safe_printf("??? ("); break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000376 }
377 assert(ct->el_count != 0);
378 assert(ct->el_count == 1);
379 asn1print_constraint(ct->elements[0], flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700380 safe_printf(")");
Lev Walkinf15320b2004-06-03 03:38:44 +0000381 break;
382 case ACT_CT_WCOMP:
Lev Walkine596bf02005-03-28 15:01:27 +0000383 assert(ct->el_count != 0);
384 assert(ct->el_count == 1);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700385 safe_printf("WITH COMPONENT (");
Lev Walkine596bf02005-03-28 15:01:27 +0000386 asn1print_constraint(ct->elements[0], flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700387 safe_printf(")");
Lev Walkin8638f0d2005-03-28 07:50:06 +0000388 break;
Lev Walkine596bf02005-03-28 15:01:27 +0000389 case ACT_CT_WCOMPS: {
390 unsigned int i;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700391 safe_printf("WITH COMPONENTS { ");
Lev Walkine596bf02005-03-28 15:01:27 +0000392 for(i = 0; i < ct->el_count; i++) {
393 asn1p_constraint_t *cel = ct->elements[i];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700394 if(i) safe_printf(", ");
395 safe_fwrite(cel->value->value.string.buf,
Lev Walkinf4b711a2017-08-23 06:55:27 -0700396 cel->value->value.string.size);
Lev Walkine596bf02005-03-28 15:01:27 +0000397 if(cel->el_count) {
398 assert(cel->el_count == 1);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700399 safe_printf(" ");
Lev Walkine596bf02005-03-28 15:01:27 +0000400 asn1print_constraint(cel->elements[0],
401 flags);
402 }
403 switch(cel->presence) {
404 case ACPRES_DEFAULT: break;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700405 case ACPRES_PRESENT: safe_printf(" PRESENT"); break;
406 case ACPRES_ABSENT: safe_printf(" ABSENT"); break;
407 case ACPRES_OPTIONAL: safe_printf(" OPTIONAL");break;
Lev Walkine596bf02005-03-28 15:01:27 +0000408 }
409 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700410 safe_printf(" }");
Lev Walkine596bf02005-03-28 15:01:27 +0000411 }
Lev Walkin8638f0d2005-03-28 07:50:06 +0000412 break;
Lev Walkin1893ddf2005-03-20 14:28:32 +0000413 case ACT_CT_CTDBY:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700414 safe_printf("CONSTRAINED BY ");
Lev Walkin1893ddf2005-03-20 14:28:32 +0000415 assert(ct->value->type == ATV_UNPARSED);
Lev Walkinf4b711a2017-08-23 06:55:27 -0700416 safe_fwrite(ct->value->value.string.buf, ct->value->value.string.size);
Lev Walkina9532f42006-09-17 04:52:50 +0000417 break;
418 case ACT_CT_CTNG:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700419 safe_printf("CONTAINING ");
Lev Walkina9532f42006-09-17 04:52:50 +0000420 asn1print_expr(ct->value->value.v_type->module->asn1p,
421 ct->value->value.v_type->module,
422 ct->value->value.v_type,
423 flags, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000424 break;
Lev Walkin5c541f12006-10-18 18:40:14 +0000425 case ACT_CT_PATTERN:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700426 safe_printf("PATTERN ");
Lev Walkin5c541f12006-10-18 18:40:14 +0000427 asn1print_value(ct->value, flags);
Lev Walkin5c541f12006-10-18 18:40:14 +0000428 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000429 case ACT_CA_SET: symno++;
430 case ACT_CA_CRC: symno++;
431 case ACT_CA_CSV: symno++;
432 case ACT_CA_UNI: symno++;
433 case ACT_CA_INT: symno++;
434 case ACT_CA_EXC:
435 {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000436 char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",",
Lev Walkinf15320b2004-06-03 03:38:44 +0000437 "", "(" };
Lev Walkin0056aec2004-09-05 10:38:50 +0000438 unsigned int i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000439 for(i = 0; i < ct->el_count; i++) {
Lev Walkinf4b711a2017-08-23 06:55:27 -0700440 if(i) safe_printf("%s", symtable[symno]);
441 if(ct->type == ACT_CA_CRC) safe_printf("{");
Lev Walkin1e448d32005-03-24 14:26:38 +0000442 asn1print_constraint(ct->elements[i], flags);
Lev Walkinf4b711a2017-08-23 06:55:27 -0700443 if(ct->type == ACT_CA_CRC) safe_printf("}");
Lev Walkinf15320b2004-06-03 03:38:44 +0000444 if(i+1 < ct->el_count
445 && ct->type == ACT_CA_SET)
Lev Walkinf4b711a2017-08-23 06:55:27 -0700446 safe_printf(")");
Lev Walkinf15320b2004-06-03 03:38:44 +0000447 }
448 }
449 break;
Lev Walkin1e448d32005-03-24 14:26:38 +0000450 case ACT_CA_AEX:
451 assert(ct->el_count == 1);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700452 safe_printf("ALL EXCEPT ");
Lev Walkin1e448d32005-03-24 14:26:38 +0000453 asn1print_constraint(ct->elements[0], flags);
454 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000455 case ACT_INVALID:
456 assert(ct->type != ACT_INVALID);
457 break;
458 }
459
460 if(ct->type == ACT_CA_SET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700461 safe_printf(")");
Lev Walkinf15320b2004-06-03 03:38:44 +0000462
463 return 0;
464}
465
466static int
Lev Walkinf4b711a2017-08-23 06:55:27 -0700467asn1print_params(const asn1p_paramlist_t *pl, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000468 if(pl) {
469 int i;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700470 safe_printf("{");
Lev Walkinf15320b2004-06-03 03:38:44 +0000471 for(i = 0; i < pl->params_count; i++) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700472 if(i) safe_printf(", ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000473 if(pl->params[i].governor) {
474 asn1print_ref(pl->params[i].governor, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700475 safe_printf(":");
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700477 safe_printf("%s", pl->params[i].argument);
Lev Walkinf15320b2004-06-03 03:38:44 +0000478 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700479 safe_printf("}");
Lev Walkinf15320b2004-06-03 03:38:44 +0000480 }
481
482 return 0;
483}
484
485static int
Lev Walkinf4b711a2017-08-23 06:55:27 -0700486asn1print_with_syntax(const asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000487 if(wx) {
Lev Walkinf4b711a2017-08-23 06:55:27 -0700488 const asn1p_wsyntx_chunk_t *wc;
Lev Walkinf15320b2004-06-03 03:38:44 +0000489 TQ_FOR(wc, &(wx->chunks), next) {
Lev Walkin9d542d22006-03-14 16:31:37 +0000490 switch(wc->type) {
491 case WC_LITERAL:
Lev Walkin57074f12006-03-16 05:11:14 +0000492 case WC_WHITESPACE:
Lev Walkind370e9f2006-03-16 10:03:35 +0000493 case WC_FIELD:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700494 safe_printf("%s", wc->content.token);
Lev Walkin9d542d22006-03-14 16:31:37 +0000495 break;
Lev Walkin9d542d22006-03-14 16:31:37 +0000496 case WC_OPTIONALGROUP:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700497 safe_printf("[");
Lev Walkin9d542d22006-03-14 16:31:37 +0000498 asn1print_with_syntax(wc->content.syntax,flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700499 safe_printf("]");
Lev Walkin9d542d22006-03-14 16:31:37 +0000500 break;
501 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000502 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000503 }
504
505 return 0;
506}
507
508static int
Lev Walkinf4b711a2017-08-23 06:55:27 -0700509asn1print_crange_value(const asn1cnst_edge_t *edge, int as_char) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000510 switch(edge->type) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700511 case ARE_MIN: safe_printf("MIN"); break;
512 case ARE_MAX: safe_printf("MAX"); break;
Lev Walkin3140e0e2004-08-18 04:50:37 +0000513 case ARE_VALUE:
514 if(as_char) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700515 safe_printf("\"%c\"", (unsigned char)edge->value);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000516 } else {
Lev Walkinda997b12017-08-04 01:38:41 -0700517 safe_printf("%s", asn1p_itoa(edge->value));
Lev Walkin3140e0e2004-08-18 04:50:37 +0000518 }
519 }
520 return 0;
521}
522
523static int
Lev Walkina28cbb92017-07-31 20:20:17 -0700524asn1print_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 +0000525 asn1cnst_range_t *range;
526 int as_char = (type==ACT_CT_FROM);
527 int i;
528
Lev Walkina28cbb92017-07-31 20:20:17 -0700529 range = asn1constraint_compute_constraint_range(dbg_name, expr_type, ct, type, 0, 0, cpr);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000530 if(!range) return -1;
531
Lev Walkina2abcaa2017-07-23 21:08:49 +0400532 if(range->incompatible) return 0;
533
534 if((cpr & CPR_strict_OER_visibility) && range->not_OER_visible) {
535 asn1constraint_range_free(range);
536 return 0;
537 }
538
539 if((cpr & CPR_strict_PER_visibility) && range->not_PER_visible) {
Lev Walkine8e87f12004-08-25 02:00:03 +0000540 asn1constraint_range_free(range);
541 return 0;
542 }
543
Lev Walkin3140e0e2004-08-18 04:50:37 +0000544 switch(type) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700545 case ACT_CT_FROM: safe_printf("(FROM("); break;
546 case ACT_CT_SIZE: safe_printf("(SIZE("); break;
547 default: safe_printf("("); break;
Lev Walkin3140e0e2004-08-18 04:50:37 +0000548 }
549 for(i = -1; i < range->el_count; i++) {
550 asn1cnst_range_t *r;
551 if(i == -1) {
552 if(range->el_count) continue;
553 r = range;
554 } else {
555 r = range->elements[i];
556 }
557 if(i > 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700558 safe_printf(" | ");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000559 }
560 asn1print_crange_value(&r->left, as_char);
561 if(r->left.type != r->right.type
562 || r->left.value != r->right.value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700563 safe_printf("..");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000564 asn1print_crange_value(&r->right, as_char);
565 }
566 }
567 if(range->extensible)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700568 safe_printf(",...");
569 safe_printf(type==ACT_EL_RANGE?")":"))");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000570
571 if(range->empty_constraint)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700572 safe_printf(":Empty!");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000573
574 asn1constraint_range_free(range);
575 return 0;
576}
577
578static int
Lev Walkina28cbb92017-07-31 20:20:17 -0700579asn1print_constraint_explain(const char *dbg_name, asn1p_expr_type_e expr_type,
Lev Walkina2abcaa2017-07-23 21:08:49 +0400580 asn1p_constraint_t *ct, enum cpr_flags cpr) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000581
Lev Walkina28cbb92017-07-31 20:20:17 -0700582 asn1print_constraint_explain_type(dbg_name, expr_type, ct, ACT_EL_RANGE, cpr);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700583 safe_printf(" ");
Lev Walkina28cbb92017-07-31 20:20:17 -0700584 asn1print_constraint_explain_type(dbg_name, expr_type, ct, ACT_CT_SIZE, cpr);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700585 safe_printf(" ");
Lev Walkina28cbb92017-07-31 20:20:17 -0700586 asn1print_constraint_explain_type(dbg_name, expr_type, ct, ACT_CT_FROM, cpr);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000587
588 return 0;
589}
590
591static int
592asn1print_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 +0000593 int SEQ_OF = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000594
Lev Walkinf4069d22005-02-15 07:06:05 +0000595 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
Lev Walkinf7484512004-10-13 09:13:56 +0000596 INDENT("-- #line %d\n", tc->_lineno);
Lev Walkinef625402005-09-05 05:17:57 +0000597
598 /* Reconstruct compiler directive information */
599 if((tc->marker.flags & EM_INDIRECT)
600 && (tc->marker.flags & EM_OMITABLE) != EM_OMITABLE) {
601 if((flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700602 safe_printf(" --<ASN1C.RepresentAsPointer>-- ");
Lev Walkinef625402005-09-05 05:17:57 +0000603 else
604 INDENT("--<ASN1C.RepresentAsPointer>--\n");
605 }
606
Lev Walkina00d6b32006-03-21 03:40:38 +0000607 if(tc->Identifier
608 && (!(tc->meta_type == AMT_VALUE && tc->expr_type == A1TC_REFERENCE)
609 || level == 0))
Lev Walkinf15320b2004-06-03 03:38:44 +0000610 INDENT("%s", tc->Identifier);
611
Lev Walkina00d6b32006-03-21 03:40:38 +0000612 if(tc->lhs_params) {
613 asn1print_params(tc->lhs_params, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000614 }
615
616 if(tc->meta_type != AMT_VALUE
Lev Walkinc74ea222004-08-25 02:27:47 +0000617 && tc->meta_type != AMT_VALUESET
Lev Walkinf15320b2004-06-03 03:38:44 +0000618 && tc->expr_type != A1TC_EXTENSIBLE) {
619 if(level) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000620 if(tc->Identifier && !(flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700621 safe_printf("\t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000622 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700623 safe_printf(" ::=");
Lev Walkinf15320b2004-06-03 03:38:44 +0000624 }
625 }
626
627 if(tc->tag.tag_class) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700628 safe_printf(" ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000629 asn1print_tag(tc, flags);
630 }
631
632 switch(tc->expr_type) {
633 case A1TC_EXTENSIBLE:
634 if(tc->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700635 safe_printf("!");
Lev Walkinf15320b2004-06-03 03:38:44 +0000636 asn1print_value(tc->value, flags);
637 }
638 break;
Lev Walkinfd151ce2004-08-22 03:08:51 +0000639 case A1TC_COMPONENTS_OF:
640 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700641 safe_printf(" COMPONENTS OF");
Lev Walkinfd151ce2004-08-22 03:08:51 +0000642 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000643 case A1TC_REFERENCE:
644 case A1TC_UNIVERVAL:
Lev Walkinf15320b2004-06-03 03:38:44 +0000645 break;
646 case A1TC_CLASSDEF:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700647 safe_printf(" CLASS");
Lev Walkinf15320b2004-06-03 03:38:44 +0000648 break;
Lev Walkin9c2285a2006-03-09 08:49:26 +0000649 case A1TC_CLASSFIELD_TFS ... A1TC_CLASSFIELD_OSFS:
Lev Walkinf15320b2004-06-03 03:38:44 +0000650 /* Nothing to print here */
651 break;
Lev Walkinb1ef3962004-08-20 13:24:28 +0000652 case ASN_CONSTR_SET_OF:
653 case ASN_CONSTR_SEQUENCE_OF:
654 SEQ_OF = 1;
655 if(tc->expr_type == ASN_CONSTR_SET_OF)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700656 safe_printf(" SET");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000657 else
Lev Walkinf2b2f372016-03-14 02:23:48 -0700658 safe_printf(" SEQUENCE");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000659 if(tc->constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700660 safe_printf(" ");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000661 asn1print_constraint(tc->constraints, flags);
662 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700663 safe_printf(" OF");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000664 break;
Lev Walkin5045dfa2006-03-21 09:41:28 +0000665 case A1TC_VALUESET:
666 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000667 default:
668 {
669 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700670 safe_printf(" %s", p?p:"<unknown type!>");
Lev Walkinf15320b2004-06-03 03:38:44 +0000671 }
672 break;
673 }
674
Lev Walkinb9d0ae32005-06-02 04:06:24 +0000675 /*
676 * Put the name of the referred type.
677 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000678 if(tc->reference) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700679 safe_printf(" ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000680 asn1print_ref(tc->reference, flags);
681 }
682
Lev Walkin5045dfa2006-03-21 09:41:28 +0000683 if(tc->meta_type == AMT_VALUESET && level == 0)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700684 safe_printf(" ::=");
Lev Walkinc74ea222004-08-25 02:27:47 +0000685
Lev Walkinf15320b2004-06-03 03:38:44 +0000686 /*
687 * Display the descendants (children) of the current type.
688 */
Lev Walkinc74ea222004-08-25 02:27:47 +0000689 if(TQ_FIRST(&(tc->members))
690 || (tc->expr_type & ASN_CONSTR_MASK)
Lev Walkinc74ea222004-08-25 02:27:47 +0000691 || tc->meta_type == AMT_OBJECT
Lev Walkin9c2285a2006-03-09 08:49:26 +0000692 || tc->meta_type == AMT_OBJECTCLASS
693 || tc->meta_type == AMT_OBJECTFIELD
Lev Walkinc74ea222004-08-25 02:27:47 +0000694 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000695 asn1p_expr_t *se; /* SubExpression */
Lev Walkin9c2285a2006-03-09 08:49:26 +0000696 int put_braces = (!SEQ_OF) /* Don't need 'em, if SET OF... */
697 && (tc->meta_type != AMT_OBJECTFIELD);
Lev Walkinf15320b2004-06-03 03:38:44 +0000698
Lev Walkinc74ea222004-08-25 02:27:47 +0000699 if(put_braces) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000700 if(flags & APF_NOINDENT) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700701 safe_printf("{");
Lev Walkinf4069d22005-02-15 07:06:05 +0000702 if(!TQ_FIRST(&tc->members))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700703 safe_printf("}");
Lev Walkinf4069d22005-02-15 07:06:05 +0000704 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700705 safe_printf(" {");
Lev Walkinf4069d22005-02-15 07:06:05 +0000706 if(TQ_FIRST(&tc->members))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700707 safe_printf("\n");
Lev Walkinf4069d22005-02-15 07:06:05 +0000708 else
Lev Walkinf2b2f372016-03-14 02:23:48 -0700709 safe_printf(" }");
Lev Walkinf4069d22005-02-15 07:06:05 +0000710 }
Lev Walkinc74ea222004-08-25 02:27:47 +0000711 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000712
713 TQ_FOR(se, &(tc->members), next) {
714 /*
Lev Walkinfd151ce2004-08-22 03:08:51 +0000715 * Print the expression as it were a stand-alone type.
Lev Walkinf15320b2004-06-03 03:38:44 +0000716 */
Lev Walkinf7484512004-10-13 09:13:56 +0000717 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinb1e07082004-09-15 11:44:55 +0000718 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700719 safe_printf(" DEFAULT ");
Lev Walkinb1e07082004-09-15 11:44:55 +0000720 asn1print_value(se->marker.default_value, flags);
721 } else if((se->marker.flags & EM_OPTIONAL)
722 == EM_OPTIONAL) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700723 safe_printf(" OPTIONAL");
Lev Walkinb1e07082004-09-15 11:44:55 +0000724 }
Lev Walkinef625402005-09-05 05:17:57 +0000725 if(TQ_NEXT(se, next)) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700726 safe_printf(",");
Lev Walkinef625402005-09-05 05:17:57 +0000727 if(!(flags & APF_NOINDENT))
728 INDENT("\n");
729 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000730 }
731
Lev Walkinc74ea222004-08-25 02:27:47 +0000732 if(put_braces && TQ_FIRST(&tc->members)) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000733 if(!(flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700734 safe_printf("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000735 INDENT("}");
736 }
737 }
738
Lev Walkin9d542d22006-03-14 16:31:37 +0000739 if(tc->with_syntax) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700740 safe_printf(" WITH SYNTAX {");
Lev Walkinf15320b2004-06-03 03:38:44 +0000741 asn1print_with_syntax(tc->with_syntax, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700742 safe_printf("}\n");
Lev Walkin9d542d22006-03-14 16:31:37 +0000743 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000744
Lev Walkina00d6b32006-03-21 03:40:38 +0000745 /* Right hand specialization */
746 if(tc->rhs_pspecs) {
747 asn1p_expr_t *se;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700748 safe_printf("{");
Lev Walkina00d6b32006-03-21 03:40:38 +0000749 TQ_FOR(se, &(tc->rhs_pspecs->members), next) {
750 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700751 if(TQ_NEXT(se, next)) safe_printf(", ");
Lev Walkina00d6b32006-03-21 03:40:38 +0000752 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700753 safe_printf("}");
Lev Walkina00d6b32006-03-21 03:40:38 +0000754 }
755
Lev Walkinb1ef3962004-08-20 13:24:28 +0000756 if(!SEQ_OF && tc->constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700757 safe_printf(" ");
Lev Walkin557f27d2006-03-21 07:46:48 +0000758 if(tc->meta_type == AMT_VALUESET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700759 safe_printf("{");
Lev Walkinf15320b2004-06-03 03:38:44 +0000760 asn1print_constraint(tc->constraints, flags);
Lev Walkin557f27d2006-03-21 07:46:48 +0000761 if(tc->meta_type == AMT_VALUESET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700762 safe_printf("}");
Lev Walkinf15320b2004-06-03 03:38:44 +0000763 }
Lev Walkin3140e0e2004-08-18 04:50:37 +0000764
Lev Walkinf15320b2004-06-03 03:38:44 +0000765 if(tc->unique) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700766 safe_printf(" UNIQUE");
Lev Walkinf15320b2004-06-03 03:38:44 +0000767 }
768
769 if(tc->meta_type == AMT_VALUE
770 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin0056aec2004-09-05 10:38:50 +0000771 if(tc->expr_type == A1TC_UNIVERVAL) {
Lev Walkinb1e07082004-09-15 11:44:55 +0000772 if(tc->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700773 safe_printf("(");
Lev Walkinb1e07082004-09-15 11:44:55 +0000774 asn1print_value(tc->value, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700775 safe_printf(")");
Lev Walkinb1e07082004-09-15 11:44:55 +0000776 }
Lev Walkin0056aec2004-09-05 10:38:50 +0000777 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700778 if(level == 0) safe_printf(" ::= ");
Lev Walkin0056aec2004-09-05 10:38:50 +0000779 asn1print_value(tc->value, flags);
780 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000781 }
782
Lev Walkinfd151ce2004-08-22 03:08:51 +0000783 /*
Lev Walkinf4069d22005-02-15 07:06:05 +0000784 * The following section exists entirely for debugging.
Lev Walkinfd151ce2004-08-22 03:08:51 +0000785 */
Lev Walkind370e9f2006-03-16 10:03:35 +0000786 if(flags & APF_PRINT_CONSTRAINTS
Lev Walkinb1ef3962004-08-20 13:24:28 +0000787 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000788 asn1p_expr_t *top_parent;
789
790 if(tc->combined_constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700791 safe_printf("\n-- Combined constraints: ");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000792 asn1print_constraint(tc->combined_constraints, flags);
793 }
794
Lev Walkinc0e03b92017-08-22 01:48:23 -0700795 top_parent = WITH_MODULE_NAMESPACE(
796 tc->module, tc_ns, asn1f_find_terminal_type_ex(asn, tc_ns, tc));
797 if(top_parent) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700798 safe_printf("\n-- Practical constraints (%s): ",
Lev Walkine8e87f12004-08-25 02:00:03 +0000799 top_parent->Identifier);
Lev Walkina28cbb92017-07-31 20:20:17 -0700800 asn1print_constraint_explain(top_parent->Identifier,
801 top_parent->expr_type,
Lev Walkine8e87f12004-08-25 02:00:03 +0000802 tc->combined_constraints, 0);
Lev Walkina2abcaa2017-07-23 21:08:49 +0400803 safe_printf("\n-- OER-visible constraints (%s): ",
804 top_parent->Identifier);
Lev Walkina28cbb92017-07-31 20:20:17 -0700805 asn1print_constraint_explain(top_parent->Identifier,
806 top_parent->expr_type,
Lev Walkina2abcaa2017-07-23 21:08:49 +0400807 tc->combined_constraints, CPR_strict_OER_visibility);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700808 safe_printf("\n-- PER-visible constraints (%s): ",
Lev Walkinb1ef3962004-08-20 13:24:28 +0000809 top_parent->Identifier);
Lev Walkina28cbb92017-07-31 20:20:17 -0700810 asn1print_constraint_explain(top_parent->Identifier,
811 top_parent->expr_type,
Lev Walkina2abcaa2017-07-23 21:08:49 +0400812 tc->combined_constraints, CPR_strict_PER_visibility);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000813 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700814 safe_printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000815 }
816
Lev Walkind0f7b912017-08-07 18:13:04 -0700817 if(flags & APF_PRINT_CLASS_MATRIX) do {
Lev Walkin62d95d22017-08-06 23:41:11 -0700818 size_t col, maxidlen;
Lev Walkin4dcf8362017-08-07 20:10:05 -0700819 if(tc->ioc_table == NULL) {
Lev Walkind0f7b912017-08-07 18:13:04 -0700820 if(tc->expr_type == A1TC_CLASSDEF) {
821 safe_printf("\n-- Information Object Class table is empty");
822 }
Lev Walkind370e9f2006-03-16 10:03:35 +0000823 break;
824 }
Lev Walkind0f7b912017-08-07 18:13:04 -0700825 safe_printf("\n-- Information Object Set has %d entr%s:\n",
Lev Walkin4dcf8362017-08-07 20:10:05 -0700826 tc->ioc_table->rows,
827 tc->ioc_table->rows==1 ? "y" : "ies");
828 maxidlen = asn1p_ioc_table_max_identifier_length(tc->ioc_table);
829 for(ssize_t r = -1; r < (ssize_t)tc->ioc_table->rows; r++) {
830 asn1p_ioc_row_t *row;
831 row = tc->ioc_table->row[r<0?0:r];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700832 if(r < 0) safe_printf("-- %s", r > 9 ? " " : "");
833 else safe_printf("-- [%*d]", r > 9 ? 2 : 1, r+1);
Lev Walkind370e9f2006-03-16 10:03:35 +0000834 for(col = 0; col < row->columns; col++) {
835 struct asn1p_ioc_cell_s *cell;
836 cell = &row->column[col];
837 if(r < 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700838 safe_printf("[%*s]", maxidlen,
Lev Walkind370e9f2006-03-16 10:03:35 +0000839 cell->field->Identifier);
840 continue;
841 }
842 if(!cell->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700843 safe_printf(" %*s ", maxidlen, "<no entry>");
Lev Walkind370e9f2006-03-16 10:03:35 +0000844 continue;
845 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700846 safe_printf(" %*s ", maxidlen,
Lev Walkind370e9f2006-03-16 10:03:35 +0000847 cell->value->Identifier);
848 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700849 safe_printf("\n");
Lev Walkind370e9f2006-03-16 10:03:35 +0000850 }
851 } while(0);
852
Lev Walkina00d6b32006-03-21 03:40:38 +0000853 if(flags & APF_PRINT_CLASS_MATRIX
854 && tc->lhs_params) do {
855 int i;
856 if(tc->specializations.pspecs_count == 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700857 safe_printf("\n-- No specializations found\n");
Lev Walkina00d6b32006-03-21 03:40:38 +0000858 break;
859 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700860 safe_printf("\n-- Specializations list has %d entr%s:\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000861 tc->specializations.pspecs_count,
862 tc->specializations.pspecs_count == 1 ? "y" : "ies");
863 for(i = 0; i < tc->specializations.pspecs_count; i++) {
864 asn1p_expr_t *se;
865 struct asn1p_pspec_s *pspec;
866 pspec = &tc->specializations.pspec[i];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700867 safe_printf("-- ");
Lev Walkina00d6b32006-03-21 03:40:38 +0000868 TQ_FOR(se, &(pspec->rhs_pspecs->members), next) {
869 asn1print_expr(asn, mod, se, flags, level+1);
870 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700871 safe_printf("\n");
Lev Walkina00d6b32006-03-21 03:40:38 +0000872 }
873 } while(0);
874
Lev Walkinf15320b2004-06-03 03:38:44 +0000875 return 0;
876}
Lev Walkin3140e0e2004-08-18 04:50:37 +0000877
Lev Walkinf7484512004-10-13 09:13:56 +0000878
879static int
880asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
881 asn1p_expr_t *se;
882 int expr_unordered = 0;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000883 int dont_involve_children = 0;
Lev Walkinf7484512004-10-13 09:13:56 +0000884
885 switch(expr->meta_type) {
886 case AMT_TYPE:
887 case AMT_TYPEREF:
888 break;
889 default:
890 if(expr->expr_type == A1TC_UNIVERVAL)
891 break;
892 return 0;
893 }
894
895 if(!expr->Identifier) return 0;
896
Lev Walkinf7484512004-10-13 09:13:56 +0000897 if(flags & APF_LINE_COMMENTS)
898 INDENT("<!-- #line %d -->\n", expr->_lineno);
899 INDENT("<!ELEMENT %s", expr->Identifier);
900
901 if(expr->expr_type == A1TC_REFERENCE) {
Lev Walkinc0e03b92017-08-22 01:48:23 -0700902 se = WITH_MODULE_NAMESPACE(expr->module, expr_ns, asn1f_find_terminal_type_ex(asn, expr_ns, expr));
Lev Walkinf7484512004-10-13 09:13:56 +0000903 if(!se) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700904 safe_printf(" (ANY)");
Lev Walkinf7484512004-10-13 09:13:56 +0000905 return 0;
906 }
907 expr = se;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000908 dont_involve_children = 1;
Lev Walkinf7484512004-10-13 09:13:56 +0000909 }
910
Lev Walkind7963aa2005-03-10 15:16:56 +0000911 if(expr->expr_type == ASN_CONSTR_CHOICE
912 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
913 || expr->expr_type == ASN_CONSTR_SET_OF
914 || expr->expr_type == ASN_CONSTR_SET
Lev Walkin112d69e2005-03-09 20:52:15 +0000915 || expr->expr_type == ASN_BASIC_INTEGER
916 || expr->expr_type == ASN_BASIC_ENUMERATED) {
917 expr_unordered = 1;
918 }
919
Lev Walkinf7484512004-10-13 09:13:56 +0000920 if(TQ_FIRST(&expr->members)) {
921 int extensible = 0;
Lev Walkinfbaeb852006-11-21 10:39:52 +0000922 if(expr->expr_type == ASN_BASIC_BIT_STRING)
923 dont_involve_children = 1;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700924 safe_printf(" (");
Lev Walkinf7484512004-10-13 09:13:56 +0000925 TQ_FOR(se, &(expr->members), next) {
926 if(se->expr_type == A1TC_EXTENSIBLE) {
927 extensible = 1;
928 continue;
929 } else if(!se->Identifier
930 && se->expr_type == A1TC_REFERENCE) {
931 asn1print_ref(se->reference, flags);
932 } else if(se->Identifier) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700933 safe_printf("%s", se->Identifier);
Lev Walkinf7484512004-10-13 09:13:56 +0000934 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700935 safe_printf("ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000936 }
937 if(expr->expr_type != ASN_CONSTR_SET
938 && expr->expr_type != ASN_CONSTR_CHOICE
939 && expr->expr_type != ASN_BASIC_INTEGER
940 && expr->expr_type != ASN_BASIC_ENUMERATED) {
941 if(expr_unordered)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700942 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000943 else if(se->marker.flags)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700944 safe_printf("?");
Lev Walkinfbaeb852006-11-21 10:39:52 +0000945 else if(expr->expr_type == ASN_BASIC_BIT_STRING)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700946 safe_printf("?");
Lev Walkinf7484512004-10-13 09:13:56 +0000947 }
948 if(TQ_NEXT(se, next)
949 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700950 safe_printf(expr_unordered?"|":", ");
Lev Walkinf7484512004-10-13 09:13:56 +0000951 }
952 }
953 if(extensible) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700954 safe_printf(expr_unordered?"|":", ");
955 safe_printf("ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000956 if(expr->expr_type != ASN_CONSTR_SET
957 && expr->expr_type != ASN_CONSTR_CHOICE
958 && expr->expr_type != ASN_BASIC_INTEGER
959 && expr->expr_type != ASN_BASIC_ENUMERATED)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700960 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000961 }
962
Lev Walkinf2b2f372016-03-14 02:23:48 -0700963 safe_printf(")");
Lev Walkinf7484512004-10-13 09:13:56 +0000964 if(expr->expr_type == ASN_CONSTR_SET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700965 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000966
Lev Walkinef39f7e2004-10-14 05:11:25 +0000967 } else switch(expr->expr_type) {
968 case ASN_BASIC_BOOLEAN:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700969 safe_printf(" (true|false)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000970 break;
971 case ASN_CONSTR_CHOICE:
972 case ASN_CONSTR_SET:
973 case ASN_CONSTR_SET_OF:
974 case ASN_CONSTR_SEQUENCE:
975 case ASN_CONSTR_SEQUENCE_OF:
976 case ASN_BASIC_NULL:
977 case A1TC_UNIVERVAL:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700978 safe_printf(" EMPTY");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000979 break;
980 case ASN_TYPE_ANY:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700981 safe_printf(" ANY");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000982 break;
983 case ASN_BASIC_BIT_STRING:
984 case ASN_BASIC_OCTET_STRING:
985 case ASN_BASIC_OBJECT_IDENTIFIER:
986 case ASN_BASIC_RELATIVE_OID:
Lev Walkincc116532004-10-15 06:01:54 +0000987 case ASN_BASIC_INTEGER:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000988 case ASN_BASIC_UTCTime:
989 case ASN_BASIC_GeneralizedTime:
Lev Walkinef39f7e2004-10-14 05:11:25 +0000990 case ASN_STRING_NumericString:
991 case ASN_STRING_PrintableString:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700992 safe_printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000993 break;
994 case ASN_STRING_VisibleString:
995 case ASN_STRING_ISO646String:
996 /* Entity references, but not XML elements may be present */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700997 safe_printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +0000998 break;
999 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
1000 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
1001 default:
1002 /*
1003 * XML elements are allowed.
1004 * For example, a UTF8String may contain "<bel/>".
1005 */
Lev Walkinf2b2f372016-03-14 02:23:48 -07001006 safe_printf(" ANY");
Lev Walkinf7484512004-10-13 09:13:56 +00001007 }
Lev Walkinf2b2f372016-03-14 02:23:48 -07001008 safe_printf(">\n");
Lev Walkinf7484512004-10-13 09:13:56 +00001009
1010 /*
1011 * Display the descendants (children) of the current type.
1012 */
Lev Walkinef39f7e2004-10-14 05:11:25 +00001013 if(!dont_involve_children) {
1014 TQ_FOR(se, &(expr->members), next) {
1015 if(se->expr_type == A1TC_EXTENSIBLE) continue;
1016 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
1017 }
Lev Walkinf7484512004-10-13 09:13:56 +00001018 }
1019
1020 return 0;
1021}