blob: 92c8d0e3ebf61f4d74e5a994833e0e6f16e61d3d [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;
Lev Walkind523ea42017-09-06 22:15:08 -0700340 int perhaps_subconstraints = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000341
342 if(ct == 0) return 0;
343
344 if(ct->type == ACT_CA_SET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700345 safe_printf("(");
Lev Walkinf15320b2004-06-03 03:38:44 +0000346
347 switch(ct->type) {
Lev Walkinff7dd142005-03-20 12:58:00 +0000348 case ACT_EL_TYPE:
Lev Walkin5045dfa2006-03-21 09:41:28 +0000349 asn1print_value(ct->containedSubtype, flags);
Lev Walkind523ea42017-09-06 22:15:08 -0700350 perhaps_subconstraints = 1;
Lev Walkinff7dd142005-03-20 12:58:00 +0000351 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000352 case ACT_EL_VALUE:
353 asn1print_value(ct->value, flags);
Lev Walkind523ea42017-09-06 22:15:08 -0700354 perhaps_subconstraints = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000355 break;
356 case ACT_EL_RANGE:
357 case ACT_EL_LLRANGE:
358 case ACT_EL_RLRANGE:
359 case ACT_EL_ULRANGE:
360 asn1print_value(ct->range_start, flags);
361 switch(ct->type) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700362 case ACT_EL_RANGE: safe_printf(".."); break;
363 case ACT_EL_LLRANGE: safe_printf("<.."); break;
364 case ACT_EL_RLRANGE: safe_printf("..<"); break;
365 case ACT_EL_ULRANGE: safe_printf("<..<"); break;
366 default: safe_printf("?..?"); break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000367 }
368 asn1print_value(ct->range_stop, flags);
369 break;
370 case ACT_EL_EXT:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700371 safe_printf("...");
Lev Walkinf15320b2004-06-03 03:38:44 +0000372 break;
373 case ACT_CT_SIZE:
374 case ACT_CT_FROM:
375 switch(ct->type) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700376 case ACT_CT_SIZE: safe_printf("SIZE("); break;
377 case ACT_CT_FROM: safe_printf("FROM("); break;
378 default: safe_printf("??? ("); break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000379 }
380 assert(ct->el_count != 0);
381 assert(ct->el_count == 1);
382 asn1print_constraint(ct->elements[0], flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700383 safe_printf(")");
Lev Walkinf15320b2004-06-03 03:38:44 +0000384 break;
385 case ACT_CT_WCOMP:
Lev Walkine596bf02005-03-28 15:01:27 +0000386 assert(ct->el_count != 0);
387 assert(ct->el_count == 1);
Lev Walkind523ea42017-09-06 22:15:08 -0700388 safe_printf("WITH COMPONENT");
389 perhaps_subconstraints = 1;
Lev Walkin8638f0d2005-03-28 07:50:06 +0000390 break;
Lev Walkine596bf02005-03-28 15:01:27 +0000391 case ACT_CT_WCOMPS: {
392 unsigned int i;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700393 safe_printf("WITH COMPONENTS { ");
Lev Walkine596bf02005-03-28 15:01:27 +0000394 for(i = 0; i < ct->el_count; i++) {
395 asn1p_constraint_t *cel = ct->elements[i];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700396 if(i) safe_printf(", ");
Lev Walkind523ea42017-09-06 22:15:08 -0700397 asn1print_constraint(cel, flags);
Lev Walkine596bf02005-03-28 15:01:27 +0000398 switch(cel->presence) {
399 case ACPRES_DEFAULT: break;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700400 case ACPRES_PRESENT: safe_printf(" PRESENT"); break;
401 case ACPRES_ABSENT: safe_printf(" ABSENT"); break;
402 case ACPRES_OPTIONAL: safe_printf(" OPTIONAL");break;
Lev Walkine596bf02005-03-28 15:01:27 +0000403 }
404 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700405 safe_printf(" }");
Lev Walkine596bf02005-03-28 15:01:27 +0000406 }
Lev Walkin8638f0d2005-03-28 07:50:06 +0000407 break;
Lev Walkin1893ddf2005-03-20 14:28:32 +0000408 case ACT_CT_CTDBY:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700409 safe_printf("CONSTRAINED BY ");
Lev Walkin1893ddf2005-03-20 14:28:32 +0000410 assert(ct->value->type == ATV_UNPARSED);
Lev Walkinf4b711a2017-08-23 06:55:27 -0700411 safe_fwrite(ct->value->value.string.buf, ct->value->value.string.size);
Lev Walkina9532f42006-09-17 04:52:50 +0000412 break;
413 case ACT_CT_CTNG:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700414 safe_printf("CONTAINING ");
Lev Walkina9532f42006-09-17 04:52:50 +0000415 asn1print_expr(ct->value->value.v_type->module->asn1p,
416 ct->value->value.v_type->module,
417 ct->value->value.v_type,
418 flags, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000419 break;
Lev Walkin5c541f12006-10-18 18:40:14 +0000420 case ACT_CT_PATTERN:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700421 safe_printf("PATTERN ");
Lev Walkin5c541f12006-10-18 18:40:14 +0000422 asn1print_value(ct->value, flags);
Lev Walkin5c541f12006-10-18 18:40:14 +0000423 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000424 case ACT_CA_SET: symno++;
425 case ACT_CA_CRC: symno++;
426 case ACT_CA_CSV: symno++;
427 case ACT_CA_UNI: symno++;
428 case ACT_CA_INT: symno++;
429 case ACT_CA_EXC:
430 {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000431 char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",",
Lev Walkinf15320b2004-06-03 03:38:44 +0000432 "", "(" };
Lev Walkin0056aec2004-09-05 10:38:50 +0000433 unsigned int i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000434 for(i = 0; i < ct->el_count; i++) {
Lev Walkinf4b711a2017-08-23 06:55:27 -0700435 if(i) safe_printf("%s", symtable[symno]);
436 if(ct->type == ACT_CA_CRC) safe_printf("{");
Lev Walkin1e448d32005-03-24 14:26:38 +0000437 asn1print_constraint(ct->elements[i], flags);
Lev Walkinf4b711a2017-08-23 06:55:27 -0700438 if(ct->type == ACT_CA_CRC) safe_printf("}");
Lev Walkind523ea42017-09-06 22:15:08 -0700439 if(ct->type == ACT_CA_SET && i+1 < ct->el_count)
440 safe_printf(") ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000441 }
442 }
443 break;
Lev Walkin1e448d32005-03-24 14:26:38 +0000444 case ACT_CA_AEX:
445 assert(ct->el_count == 1);
Lev Walkind523ea42017-09-06 22:15:08 -0700446 safe_printf("ALL EXCEPT");
447 perhaps_subconstraints = 1;
Lev Walkin1e448d32005-03-24 14:26:38 +0000448 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000449 case ACT_INVALID:
450 assert(ct->type != ACT_INVALID);
451 break;
452 }
453
Lev Walkind523ea42017-09-06 22:15:08 -0700454 if(perhaps_subconstraints && ct->el_count) {
455 safe_printf(" ");
456 assert(ct->el_count == 1);
457 asn1print_constraint(ct->elements[0], flags);
458 }
459
Lev Walkinf15320b2004-06-03 03:38:44 +0000460 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 Walkind523ea42017-09-06 22:15:08 -0700594 int has_space = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000595
Lev Walkind523ea42017-09-06 22:15:08 -0700596#define HAS_SPACE() \
597 do { \
598 has_space = 1; \
599 } while(0)
600#define ENSURE_SPACE() \
601 do { \
602 if(!has_space) { \
603 has_space = 1; \
604 safe_printf(" "); \
605 } \
606 } while(0)
607#define WANT_SPACE() \
608 do { \
609 has_space = 0; \
610 } while(0)
611
612 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
Lev Walkinf7484512004-10-13 09:13:56 +0000613 INDENT("-- #line %d\n", tc->_lineno);
Lev Walkinef625402005-09-05 05:17:57 +0000614
615 /* Reconstruct compiler directive information */
616 if((tc->marker.flags & EM_INDIRECT)
617 && (tc->marker.flags & EM_OMITABLE) != EM_OMITABLE) {
618 if((flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700619 safe_printf(" --<ASN1C.RepresentAsPointer>-- ");
Lev Walkinef625402005-09-05 05:17:57 +0000620 else
621 INDENT("--<ASN1C.RepresentAsPointer>--\n");
622 }
623
Lev Walkina00d6b32006-03-21 03:40:38 +0000624 if(tc->Identifier
625 && (!(tc->meta_type == AMT_VALUE && tc->expr_type == A1TC_REFERENCE)
Lev Walkind523ea42017-09-06 22:15:08 -0700626 || level == 0)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000627 INDENT("%s", tc->Identifier);
Lev Walkind523ea42017-09-06 22:15:08 -0700628 WANT_SPACE();
629 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000630
Lev Walkina00d6b32006-03-21 03:40:38 +0000631 if(tc->lhs_params) {
632 asn1print_params(tc->lhs_params, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000633 }
634
635 if(tc->meta_type != AMT_VALUE
Lev Walkinc74ea222004-08-25 02:27:47 +0000636 && tc->meta_type != AMT_VALUESET
Lev Walkinf15320b2004-06-03 03:38:44 +0000637 && tc->expr_type != A1TC_EXTENSIBLE) {
638 if(level) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000639 if(tc->Identifier && !(flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700640 safe_printf("\t");
Lev Walkind523ea42017-09-06 22:15:08 -0700641 } else if(tc->Identifier) {
642 ENSURE_SPACE();
643 safe_printf("::=");
644 WANT_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000645 }
646 }
647
648 if(tc->tag.tag_class) {
Lev Walkind523ea42017-09-06 22:15:08 -0700649 ENSURE_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000650 asn1print_tag(tc, flags);
Lev Walkind523ea42017-09-06 22:15:08 -0700651 WANT_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000652 }
653
654 switch(tc->expr_type) {
655 case A1TC_EXTENSIBLE:
656 if(tc->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700657 safe_printf("!");
Lev Walkinf15320b2004-06-03 03:38:44 +0000658 asn1print_value(tc->value, flags);
659 }
660 break;
Lev Walkinfd151ce2004-08-22 03:08:51 +0000661 case A1TC_COMPONENTS_OF:
662 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700663 safe_printf(" COMPONENTS OF");
Lev Walkind523ea42017-09-06 22:15:08 -0700664 WANT_SPACE();
Lev Walkinfd151ce2004-08-22 03:08:51 +0000665 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000666 case A1TC_REFERENCE:
667 case A1TC_UNIVERVAL:
Lev Walkinf15320b2004-06-03 03:38:44 +0000668 break;
669 case A1TC_CLASSDEF:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700670 safe_printf(" CLASS");
Lev Walkind523ea42017-09-06 22:15:08 -0700671 WANT_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000672 break;
Lev Walkin9c2285a2006-03-09 08:49:26 +0000673 case A1TC_CLASSFIELD_TFS ... A1TC_CLASSFIELD_OSFS:
Lev Walkinf15320b2004-06-03 03:38:44 +0000674 /* Nothing to print here */
675 break;
Lev Walkinb1ef3962004-08-20 13:24:28 +0000676 case ASN_CONSTR_SET_OF:
677 case ASN_CONSTR_SEQUENCE_OF:
678 SEQ_OF = 1;
Lev Walkind523ea42017-09-06 22:15:08 -0700679 ENSURE_SPACE();
Lev Walkinb1ef3962004-08-20 13:24:28 +0000680 if(tc->expr_type == ASN_CONSTR_SET_OF)
Lev Walkind523ea42017-09-06 22:15:08 -0700681 safe_printf("SET");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000682 else
Lev Walkind523ea42017-09-06 22:15:08 -0700683 safe_printf("SEQUENCE");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000684 if(tc->constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700685 safe_printf(" ");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000686 asn1print_constraint(tc->constraints, flags);
687 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700688 safe_printf(" OF");
Lev Walkind523ea42017-09-06 22:15:08 -0700689 WANT_SPACE();
Lev Walkinb1ef3962004-08-20 13:24:28 +0000690 break;
Lev Walkin5045dfa2006-03-21 09:41:28 +0000691 case A1TC_VALUESET:
692 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000693 default:
694 {
695 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
Lev Walkind523ea42017-09-06 22:15:08 -0700696 ENSURE_SPACE();
697 safe_printf("%s", p?p:"<unknown type!>");
698 WANT_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000699 }
700 break;
701 }
702
Lev Walkinb9d0ae32005-06-02 04:06:24 +0000703 /*
704 * Put the name of the referred type.
705 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000706 if(tc->reference) {
Lev Walkind523ea42017-09-06 22:15:08 -0700707 ENSURE_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000708 asn1print_ref(tc->reference, flags);
Lev Walkind523ea42017-09-06 22:15:08 -0700709 WANT_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000710 }
711
Lev Walkind523ea42017-09-06 22:15:08 -0700712 if(tc->meta_type == AMT_VALUESET && level == 0) {
713 ENSURE_SPACE();
714 safe_printf("::=");
715 WANT_SPACE();
716 }
Lev Walkinc74ea222004-08-25 02:27:47 +0000717
Lev Walkinf15320b2004-06-03 03:38:44 +0000718 /*
719 * Display the descendants (children) of the current type.
720 */
Lev Walkinc74ea222004-08-25 02:27:47 +0000721 if(TQ_FIRST(&(tc->members))
722 || (tc->expr_type & ASN_CONSTR_MASK)
Lev Walkinc74ea222004-08-25 02:27:47 +0000723 || tc->meta_type == AMT_OBJECT
Lev Walkin9c2285a2006-03-09 08:49:26 +0000724 || tc->meta_type == AMT_OBJECTCLASS
725 || tc->meta_type == AMT_OBJECTFIELD
Lev Walkinc74ea222004-08-25 02:27:47 +0000726 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000727 asn1p_expr_t *se; /* SubExpression */
Lev Walkin9c2285a2006-03-09 08:49:26 +0000728 int put_braces = (!SEQ_OF) /* Don't need 'em, if SET OF... */
729 && (tc->meta_type != AMT_OBJECTFIELD);
Lev Walkinf15320b2004-06-03 03:38:44 +0000730
Lev Walkinc74ea222004-08-25 02:27:47 +0000731 if(put_braces) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000732 if(flags & APF_NOINDENT) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700733 safe_printf("{");
Lev Walkinf4069d22005-02-15 07:06:05 +0000734 if(!TQ_FIRST(&tc->members))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700735 safe_printf("}");
Lev Walkinf4069d22005-02-15 07:06:05 +0000736 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700737 safe_printf(" {");
Lev Walkinf4069d22005-02-15 07:06:05 +0000738 if(TQ_FIRST(&tc->members))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700739 safe_printf("\n");
Lev Walkinf4069d22005-02-15 07:06:05 +0000740 else
Lev Walkinf2b2f372016-03-14 02:23:48 -0700741 safe_printf(" }");
Lev Walkinf4069d22005-02-15 07:06:05 +0000742 }
Lev Walkinc74ea222004-08-25 02:27:47 +0000743 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000744
745 TQ_FOR(se, &(tc->members), next) {
746 /*
Lev Walkinfd151ce2004-08-22 03:08:51 +0000747 * Print the expression as it were a stand-alone type.
Lev Walkinf15320b2004-06-03 03:38:44 +0000748 */
Lev Walkinf7484512004-10-13 09:13:56 +0000749 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinb1e07082004-09-15 11:44:55 +0000750 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700751 safe_printf(" DEFAULT ");
Lev Walkinb1e07082004-09-15 11:44:55 +0000752 asn1print_value(se->marker.default_value, flags);
753 } else if((se->marker.flags & EM_OPTIONAL)
754 == EM_OPTIONAL) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700755 safe_printf(" OPTIONAL");
Lev Walkinb1e07082004-09-15 11:44:55 +0000756 }
Lev Walkinef625402005-09-05 05:17:57 +0000757 if(TQ_NEXT(se, next)) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700758 safe_printf(",");
Lev Walkinef625402005-09-05 05:17:57 +0000759 if(!(flags & APF_NOINDENT))
760 INDENT("\n");
761 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000762 }
763
Lev Walkinc74ea222004-08-25 02:27:47 +0000764 if(put_braces && TQ_FIRST(&tc->members)) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000765 if(!(flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700766 safe_printf("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000767 INDENT("}");
768 }
769 }
770
Lev Walkin9d542d22006-03-14 16:31:37 +0000771 if(tc->with_syntax) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700772 safe_printf(" WITH SYNTAX {");
Lev Walkinf15320b2004-06-03 03:38:44 +0000773 asn1print_with_syntax(tc->with_syntax, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700774 safe_printf("}\n");
Lev Walkin9d542d22006-03-14 16:31:37 +0000775 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000776
Lev Walkina00d6b32006-03-21 03:40:38 +0000777 /* Right hand specialization */
778 if(tc->rhs_pspecs) {
779 asn1p_expr_t *se;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700780 safe_printf("{");
Lev Walkina00d6b32006-03-21 03:40:38 +0000781 TQ_FOR(se, &(tc->rhs_pspecs->members), next) {
782 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700783 if(TQ_NEXT(se, next)) safe_printf(", ");
Lev Walkina00d6b32006-03-21 03:40:38 +0000784 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700785 safe_printf("}");
Lev Walkina00d6b32006-03-21 03:40:38 +0000786 }
787
Lev Walkinb1ef3962004-08-20 13:24:28 +0000788 if(!SEQ_OF && tc->constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700789 safe_printf(" ");
Lev Walkin557f27d2006-03-21 07:46:48 +0000790 if(tc->meta_type == AMT_VALUESET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700791 safe_printf("{");
Lev Walkinf15320b2004-06-03 03:38:44 +0000792 asn1print_constraint(tc->constraints, flags);
Lev Walkin557f27d2006-03-21 07:46:48 +0000793 if(tc->meta_type == AMT_VALUESET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700794 safe_printf("}");
Lev Walkinf15320b2004-06-03 03:38:44 +0000795 }
Lev Walkin3140e0e2004-08-18 04:50:37 +0000796
Lev Walkinf15320b2004-06-03 03:38:44 +0000797 if(tc->unique) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700798 safe_printf(" UNIQUE");
Lev Walkinf15320b2004-06-03 03:38:44 +0000799 }
800
801 if(tc->meta_type == AMT_VALUE
802 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin0056aec2004-09-05 10:38:50 +0000803 if(tc->expr_type == A1TC_UNIVERVAL) {
Lev Walkinb1e07082004-09-15 11:44:55 +0000804 if(tc->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700805 safe_printf("(");
Lev Walkinb1e07082004-09-15 11:44:55 +0000806 asn1print_value(tc->value, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700807 safe_printf(")");
Lev Walkinb1e07082004-09-15 11:44:55 +0000808 }
Lev Walkin0056aec2004-09-05 10:38:50 +0000809 } else {
Lev Walkind523ea42017-09-06 22:15:08 -0700810 if(level == 0 && tc->Identifier) safe_printf(" ::= ");
Lev Walkin0056aec2004-09-05 10:38:50 +0000811 asn1print_value(tc->value, flags);
812 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000813 }
814
Lev Walkinfd151ce2004-08-22 03:08:51 +0000815 /*
Lev Walkinf4069d22005-02-15 07:06:05 +0000816 * The following section exists entirely for debugging.
Lev Walkinfd151ce2004-08-22 03:08:51 +0000817 */
Lev Walkind370e9f2006-03-16 10:03:35 +0000818 if(flags & APF_PRINT_CONSTRAINTS
Lev Walkinb1ef3962004-08-20 13:24:28 +0000819 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000820 asn1p_expr_t *top_parent;
821
822 if(tc->combined_constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700823 safe_printf("\n-- Combined constraints: ");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000824 asn1print_constraint(tc->combined_constraints, flags);
825 }
826
Lev Walkinc0e03b92017-08-22 01:48:23 -0700827 top_parent = WITH_MODULE_NAMESPACE(
828 tc->module, tc_ns, asn1f_find_terminal_type_ex(asn, tc_ns, tc));
829 if(top_parent) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700830 safe_printf("\n-- Practical constraints (%s): ",
Lev Walkine8e87f12004-08-25 02:00:03 +0000831 top_parent->Identifier);
Lev Walkina28cbb92017-07-31 20:20:17 -0700832 asn1print_constraint_explain(top_parent->Identifier,
833 top_parent->expr_type,
Lev Walkine8e87f12004-08-25 02:00:03 +0000834 tc->combined_constraints, 0);
Lev Walkina2abcaa2017-07-23 21:08:49 +0400835 safe_printf("\n-- OER-visible constraints (%s): ",
836 top_parent->Identifier);
Lev Walkina28cbb92017-07-31 20:20:17 -0700837 asn1print_constraint_explain(top_parent->Identifier,
838 top_parent->expr_type,
Lev Walkina2abcaa2017-07-23 21:08:49 +0400839 tc->combined_constraints, CPR_strict_OER_visibility);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700840 safe_printf("\n-- PER-visible constraints (%s): ",
Lev Walkinb1ef3962004-08-20 13:24:28 +0000841 top_parent->Identifier);
Lev Walkina28cbb92017-07-31 20:20:17 -0700842 asn1print_constraint_explain(top_parent->Identifier,
843 top_parent->expr_type,
Lev Walkina2abcaa2017-07-23 21:08:49 +0400844 tc->combined_constraints, CPR_strict_PER_visibility);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000845 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700846 safe_printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000847 }
848
Lev Walkind0f7b912017-08-07 18:13:04 -0700849 if(flags & APF_PRINT_CLASS_MATRIX) do {
Lev Walkin62d95d22017-08-06 23:41:11 -0700850 size_t col, maxidlen;
Lev Walkin4dcf8362017-08-07 20:10:05 -0700851 if(tc->ioc_table == NULL) {
Lev Walkind0f7b912017-08-07 18:13:04 -0700852 if(tc->expr_type == A1TC_CLASSDEF) {
853 safe_printf("\n-- Information Object Class table is empty");
854 }
Lev Walkind370e9f2006-03-16 10:03:35 +0000855 break;
856 }
Lev Walkind0f7b912017-08-07 18:13:04 -0700857 safe_printf("\n-- Information Object Set has %d entr%s:\n",
Lev Walkin4dcf8362017-08-07 20:10:05 -0700858 tc->ioc_table->rows,
859 tc->ioc_table->rows==1 ? "y" : "ies");
860 maxidlen = asn1p_ioc_table_max_identifier_length(tc->ioc_table);
861 for(ssize_t r = -1; r < (ssize_t)tc->ioc_table->rows; r++) {
862 asn1p_ioc_row_t *row;
863 row = tc->ioc_table->row[r<0?0:r];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700864 if(r < 0) safe_printf("-- %s", r > 9 ? " " : "");
Lev Walkinbc407262017-08-23 10:03:22 -0700865 else
866 safe_printf("-- [%*d]", (tc->ioc_table->rows > 9) + 1, r + 1);
867 for(col = 0; col < row->columns; col++) {
Lev Walkind370e9f2006-03-16 10:03:35 +0000868 struct asn1p_ioc_cell_s *cell;
869 cell = &row->column[col];
870 if(r < 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700871 safe_printf("[%*s]", maxidlen,
Lev Walkind370e9f2006-03-16 10:03:35 +0000872 cell->field->Identifier);
873 continue;
874 }
875 if(!cell->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700876 safe_printf(" %*s ", maxidlen, "<no entry>");
Lev Walkind370e9f2006-03-16 10:03:35 +0000877 continue;
878 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700879 safe_printf(" %*s ", maxidlen,
Lev Walkind370e9f2006-03-16 10:03:35 +0000880 cell->value->Identifier);
881 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700882 safe_printf("\n");
Lev Walkind370e9f2006-03-16 10:03:35 +0000883 }
Lev Walkinbc407262017-08-23 10:03:22 -0700884 if(tc->ioc_table->extensible) {
885 safe_printf("-- [%*s] ...\n", (tc->ioc_table->rows>9)+1, "");
886 }
887 } while(0);
Lev Walkind370e9f2006-03-16 10:03:35 +0000888
Lev Walkina00d6b32006-03-21 03:40:38 +0000889 if(flags & APF_PRINT_CLASS_MATRIX
890 && tc->lhs_params) do {
891 int i;
892 if(tc->specializations.pspecs_count == 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700893 safe_printf("\n-- No specializations found\n");
Lev Walkina00d6b32006-03-21 03:40:38 +0000894 break;
895 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700896 safe_printf("\n-- Specializations list has %d entr%s:\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000897 tc->specializations.pspecs_count,
898 tc->specializations.pspecs_count == 1 ? "y" : "ies");
899 for(i = 0; i < tc->specializations.pspecs_count; i++) {
900 asn1p_expr_t *se;
901 struct asn1p_pspec_s *pspec;
902 pspec = &tc->specializations.pspec[i];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700903 safe_printf("-- ");
Lev Walkina00d6b32006-03-21 03:40:38 +0000904 TQ_FOR(se, &(pspec->rhs_pspecs->members), next) {
905 asn1print_expr(asn, mod, se, flags, level+1);
906 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700907 safe_printf("\n");
Lev Walkina00d6b32006-03-21 03:40:38 +0000908 }
909 } while(0);
910
Lev Walkinf15320b2004-06-03 03:38:44 +0000911 return 0;
912}
Lev Walkin3140e0e2004-08-18 04:50:37 +0000913
Lev Walkinf7484512004-10-13 09:13:56 +0000914
915static int
916asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
917 asn1p_expr_t *se;
918 int expr_unordered = 0;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000919 int dont_involve_children = 0;
Lev Walkinf7484512004-10-13 09:13:56 +0000920
921 switch(expr->meta_type) {
922 case AMT_TYPE:
923 case AMT_TYPEREF:
924 break;
925 default:
926 if(expr->expr_type == A1TC_UNIVERVAL)
927 break;
928 return 0;
929 }
930
931 if(!expr->Identifier) return 0;
932
Lev Walkinf7484512004-10-13 09:13:56 +0000933 if(flags & APF_LINE_COMMENTS)
934 INDENT("<!-- #line %d -->\n", expr->_lineno);
935 INDENT("<!ELEMENT %s", expr->Identifier);
936
937 if(expr->expr_type == A1TC_REFERENCE) {
Lev Walkinc0e03b92017-08-22 01:48:23 -0700938 se = WITH_MODULE_NAMESPACE(expr->module, expr_ns, asn1f_find_terminal_type_ex(asn, expr_ns, expr));
Lev Walkinf7484512004-10-13 09:13:56 +0000939 if(!se) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700940 safe_printf(" (ANY)");
Lev Walkinf7484512004-10-13 09:13:56 +0000941 return 0;
942 }
943 expr = se;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000944 dont_involve_children = 1;
Lev Walkinf7484512004-10-13 09:13:56 +0000945 }
946
Lev Walkind7963aa2005-03-10 15:16:56 +0000947 if(expr->expr_type == ASN_CONSTR_CHOICE
948 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
949 || expr->expr_type == ASN_CONSTR_SET_OF
950 || expr->expr_type == ASN_CONSTR_SET
Lev Walkin112d69e2005-03-09 20:52:15 +0000951 || expr->expr_type == ASN_BASIC_INTEGER
952 || expr->expr_type == ASN_BASIC_ENUMERATED) {
953 expr_unordered = 1;
954 }
955
Lev Walkinf7484512004-10-13 09:13:56 +0000956 if(TQ_FIRST(&expr->members)) {
957 int extensible = 0;
Lev Walkinfbaeb852006-11-21 10:39:52 +0000958 if(expr->expr_type == ASN_BASIC_BIT_STRING)
959 dont_involve_children = 1;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700960 safe_printf(" (");
Lev Walkinf7484512004-10-13 09:13:56 +0000961 TQ_FOR(se, &(expr->members), next) {
962 if(se->expr_type == A1TC_EXTENSIBLE) {
963 extensible = 1;
964 continue;
965 } else if(!se->Identifier
966 && se->expr_type == A1TC_REFERENCE) {
967 asn1print_ref(se->reference, flags);
968 } else if(se->Identifier) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700969 safe_printf("%s", se->Identifier);
Lev Walkinf7484512004-10-13 09:13:56 +0000970 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700971 safe_printf("ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000972 }
973 if(expr->expr_type != ASN_CONSTR_SET
974 && expr->expr_type != ASN_CONSTR_CHOICE
975 && expr->expr_type != ASN_BASIC_INTEGER
976 && expr->expr_type != ASN_BASIC_ENUMERATED) {
977 if(expr_unordered)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700978 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000979 else if(se->marker.flags)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700980 safe_printf("?");
Lev Walkinfbaeb852006-11-21 10:39:52 +0000981 else if(expr->expr_type == ASN_BASIC_BIT_STRING)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700982 safe_printf("?");
Lev Walkinf7484512004-10-13 09:13:56 +0000983 }
984 if(TQ_NEXT(se, next)
985 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700986 safe_printf(expr_unordered?"|":", ");
Lev Walkinf7484512004-10-13 09:13:56 +0000987 }
988 }
989 if(extensible) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700990 safe_printf(expr_unordered?"|":", ");
991 safe_printf("ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000992 if(expr->expr_type != ASN_CONSTR_SET
993 && expr->expr_type != ASN_CONSTR_CHOICE
994 && expr->expr_type != ASN_BASIC_INTEGER
995 && expr->expr_type != ASN_BASIC_ENUMERATED)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700996 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000997 }
998
Lev Walkinf2b2f372016-03-14 02:23:48 -0700999 safe_printf(")");
Lev Walkinf7484512004-10-13 09:13:56 +00001000 if(expr->expr_type == ASN_CONSTR_SET)
Lev Walkinf2b2f372016-03-14 02:23:48 -07001001 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +00001002
Lev Walkinef39f7e2004-10-14 05:11:25 +00001003 } else switch(expr->expr_type) {
1004 case ASN_BASIC_BOOLEAN:
Lev Walkinf2b2f372016-03-14 02:23:48 -07001005 safe_printf(" (true|false)");
Lev Walkinef39f7e2004-10-14 05:11:25 +00001006 break;
1007 case ASN_CONSTR_CHOICE:
1008 case ASN_CONSTR_SET:
1009 case ASN_CONSTR_SET_OF:
1010 case ASN_CONSTR_SEQUENCE:
1011 case ASN_CONSTR_SEQUENCE_OF:
1012 case ASN_BASIC_NULL:
1013 case A1TC_UNIVERVAL:
Lev Walkinf2b2f372016-03-14 02:23:48 -07001014 safe_printf(" EMPTY");
Lev Walkinef39f7e2004-10-14 05:11:25 +00001015 break;
1016 case ASN_TYPE_ANY:
Lev Walkinf2b2f372016-03-14 02:23:48 -07001017 safe_printf(" ANY");
Lev Walkinef39f7e2004-10-14 05:11:25 +00001018 break;
1019 case ASN_BASIC_BIT_STRING:
1020 case ASN_BASIC_OCTET_STRING:
1021 case ASN_BASIC_OBJECT_IDENTIFIER:
1022 case ASN_BASIC_RELATIVE_OID:
Lev Walkincc116532004-10-15 06:01:54 +00001023 case ASN_BASIC_INTEGER:
Lev Walkinef39f7e2004-10-14 05:11:25 +00001024 case ASN_BASIC_UTCTime:
1025 case ASN_BASIC_GeneralizedTime:
Lev Walkinef39f7e2004-10-14 05:11:25 +00001026 case ASN_STRING_NumericString:
1027 case ASN_STRING_PrintableString:
Lev Walkinf2b2f372016-03-14 02:23:48 -07001028 safe_printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +00001029 break;
1030 case ASN_STRING_VisibleString:
1031 case ASN_STRING_ISO646String:
1032 /* Entity references, but not XML elements may be present */
Lev Walkinf2b2f372016-03-14 02:23:48 -07001033 safe_printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +00001034 break;
1035 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
1036 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
1037 default:
1038 /*
1039 * XML elements are allowed.
1040 * For example, a UTF8String may contain "<bel/>".
1041 */
Lev Walkinf2b2f372016-03-14 02:23:48 -07001042 safe_printf(" ANY");
Lev Walkinf7484512004-10-13 09:13:56 +00001043 }
Lev Walkinf2b2f372016-03-14 02:23:48 -07001044 safe_printf(">\n");
Lev Walkinf7484512004-10-13 09:13:56 +00001045
1046 /*
1047 * Display the descendants (children) of the current type.
1048 */
Lev Walkinef39f7e2004-10-14 05:11:25 +00001049 if(!dont_involve_children) {
1050 TQ_FOR(se, &(expr->members), next) {
1051 if(se->expr_type == A1TC_EXTENSIBLE) continue;
1052 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
1053 }
Lev Walkinf7484512004-10-13 09:13:56 +00001054 }
1055
1056 return 0;
1057}