blob: 61b952879a9ab27a904b2bdd6c505c74cb080a19 [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, ...) {
Vasil Velichkov97656bd2017-10-19 04:31:29 +030044 int ret = 0;
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
Lev Walkinf15320b2004-06-03 03:38:44 +0000344 switch(ct->type) {
Lev Walkinff7dd142005-03-20 12:58:00 +0000345 case ACT_EL_TYPE:
Lev Walkin5045dfa2006-03-21 09:41:28 +0000346 asn1print_value(ct->containedSubtype, flags);
Lev Walkind523ea42017-09-06 22:15:08 -0700347 perhaps_subconstraints = 1;
Lev Walkinff7dd142005-03-20 12:58:00 +0000348 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000349 case ACT_EL_VALUE:
350 asn1print_value(ct->value, flags);
Lev Walkind523ea42017-09-06 22:15:08 -0700351 perhaps_subconstraints = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000352 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 Walkin0c686452017-09-07 22:59:36 -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 Walkinf15320b2004-06-03 03:38:44 +0000380 break;
381 case ACT_CT_WCOMP:
Lev Walkine596bf02005-03-28 15:01:27 +0000382 assert(ct->el_count != 0);
383 assert(ct->el_count == 1);
Lev Walkind523ea42017-09-06 22:15:08 -0700384 safe_printf("WITH COMPONENT");
385 perhaps_subconstraints = 1;
Lev Walkin8638f0d2005-03-28 07:50:06 +0000386 break;
Lev Walkine596bf02005-03-28 15:01:27 +0000387 case ACT_CT_WCOMPS: {
388 unsigned int i;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700389 safe_printf("WITH COMPONENTS { ");
Lev Walkine596bf02005-03-28 15:01:27 +0000390 for(i = 0; i < ct->el_count; i++) {
391 asn1p_constraint_t *cel = ct->elements[i];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700392 if(i) safe_printf(", ");
Lev Walkind523ea42017-09-06 22:15:08 -0700393 asn1print_constraint(cel, flags);
Lev Walkine596bf02005-03-28 15:01:27 +0000394 switch(cel->presence) {
395 case ACPRES_DEFAULT: break;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700396 case ACPRES_PRESENT: safe_printf(" PRESENT"); break;
397 case ACPRES_ABSENT: safe_printf(" ABSENT"); break;
398 case ACPRES_OPTIONAL: safe_printf(" OPTIONAL");break;
Lev Walkine596bf02005-03-28 15:01:27 +0000399 }
400 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700401 safe_printf(" }");
Lev Walkine596bf02005-03-28 15:01:27 +0000402 }
Lev Walkin8638f0d2005-03-28 07:50:06 +0000403 break;
Lev Walkin1893ddf2005-03-20 14:28:32 +0000404 case ACT_CT_CTDBY:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700405 safe_printf("CONSTRAINED BY ");
Lev Walkin1893ddf2005-03-20 14:28:32 +0000406 assert(ct->value->type == ATV_UNPARSED);
Lev Walkinf4b711a2017-08-23 06:55:27 -0700407 safe_fwrite(ct->value->value.string.buf, ct->value->value.string.size);
Lev Walkina9532f42006-09-17 04:52:50 +0000408 break;
409 case ACT_CT_CTNG:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700410 safe_printf("CONTAINING ");
Lev Walkina9532f42006-09-17 04:52:50 +0000411 asn1print_expr(ct->value->value.v_type->module->asn1p,
412 ct->value->value.v_type->module,
413 ct->value->value.v_type,
414 flags, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000415 break;
Lev Walkin5c541f12006-10-18 18:40:14 +0000416 case ACT_CT_PATTERN:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700417 safe_printf("PATTERN ");
Lev Walkin5c541f12006-10-18 18:40:14 +0000418 asn1print_value(ct->value, flags);
Lev Walkin5c541f12006-10-18 18:40:14 +0000419 break;
Lev Walkin48e82d12017-10-19 03:06:35 -0700420 case ACT_CA_SET: symno++; /* Fall through */
421 case ACT_CA_CRC: symno++; /* Fall through */
422 case ACT_CA_CSV: symno++; /* Fall through */
423 case ACT_CA_UNI: symno++; /* Fall through */
424 case ACT_CA_INT: symno++; /* Fall through */
Lev Walkinf15320b2004-06-03 03:38:44 +0000425 case ACT_CA_EXC:
426 {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000427 char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",",
Lev Walkinf15320b2004-06-03 03:38:44 +0000428 "", "(" };
Lev Walkin0056aec2004-09-05 10:38:50 +0000429 unsigned int i;
Lev Walkin0c686452017-09-07 22:59:36 -0700430 if(ct->type == ACT_CA_SET) safe_printf("(");
Lev Walkinf15320b2004-06-03 03:38:44 +0000431 for(i = 0; i < ct->el_count; i++) {
Lev Walkinf4b711a2017-08-23 06:55:27 -0700432 if(i) safe_printf("%s", symtable[symno]);
433 if(ct->type == ACT_CA_CRC) safe_printf("{");
Lev Walkin1e448d32005-03-24 14:26:38 +0000434 asn1print_constraint(ct->elements[i], flags);
Lev Walkinf4b711a2017-08-23 06:55:27 -0700435 if(ct->type == ACT_CA_CRC) safe_printf("}");
Lev Walkind523ea42017-09-06 22:15:08 -0700436 if(ct->type == ACT_CA_SET && i+1 < ct->el_count)
437 safe_printf(") ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000438 }
Lev Walkin0c686452017-09-07 22:59:36 -0700439 if(ct->type == ACT_CA_SET) safe_printf(")");
Lev Walkinf15320b2004-06-03 03:38:44 +0000440 }
441 break;
Lev Walkin1e448d32005-03-24 14:26:38 +0000442 case ACT_CA_AEX:
443 assert(ct->el_count == 1);
Lev Walkind523ea42017-09-06 22:15:08 -0700444 safe_printf("ALL EXCEPT");
445 perhaps_subconstraints = 1;
Lev Walkin1e448d32005-03-24 14:26:38 +0000446 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000447 case ACT_INVALID:
448 assert(ct->type != ACT_INVALID);
449 break;
450 }
451
Lev Walkind523ea42017-09-06 22:15:08 -0700452 if(perhaps_subconstraints && ct->el_count) {
453 safe_printf(" ");
454 assert(ct->el_count == 1);
455 asn1print_constraint(ct->elements[0], flags);
456 }
457
Lev Walkinf15320b2004-06-03 03:38:44 +0000458 return 0;
459}
460
461static int
Lev Walkinf4b711a2017-08-23 06:55:27 -0700462asn1print_params(const asn1p_paramlist_t *pl, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000463 if(pl) {
464 int i;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700465 safe_printf("{");
Lev Walkinf15320b2004-06-03 03:38:44 +0000466 for(i = 0; i < pl->params_count; i++) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700467 if(i) safe_printf(", ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000468 if(pl->params[i].governor) {
469 asn1print_ref(pl->params[i].governor, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700470 safe_printf(":");
Lev Walkinf15320b2004-06-03 03:38:44 +0000471 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700472 safe_printf("%s", pl->params[i].argument);
Lev Walkinf15320b2004-06-03 03:38:44 +0000473 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700474 safe_printf("}");
Lev Walkinf15320b2004-06-03 03:38:44 +0000475 }
476
477 return 0;
478}
479
480static int
Lev Walkinf4b711a2017-08-23 06:55:27 -0700481asn1print_with_syntax(const asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000482 if(wx) {
Lev Walkinf4b711a2017-08-23 06:55:27 -0700483 const asn1p_wsyntx_chunk_t *wc;
Lev Walkinf15320b2004-06-03 03:38:44 +0000484 TQ_FOR(wc, &(wx->chunks), next) {
Lev Walkin9d542d22006-03-14 16:31:37 +0000485 switch(wc->type) {
486 case WC_LITERAL:
Lev Walkin57074f12006-03-16 05:11:14 +0000487 case WC_WHITESPACE:
Lev Walkind370e9f2006-03-16 10:03:35 +0000488 case WC_FIELD:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700489 safe_printf("%s", wc->content.token);
Lev Walkin9d542d22006-03-14 16:31:37 +0000490 break;
Lev Walkin9d542d22006-03-14 16:31:37 +0000491 case WC_OPTIONALGROUP:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700492 safe_printf("[");
Lev Walkin9d542d22006-03-14 16:31:37 +0000493 asn1print_with_syntax(wc->content.syntax,flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700494 safe_printf("]");
Lev Walkin9d542d22006-03-14 16:31:37 +0000495 break;
496 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000497 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000498 }
499
500 return 0;
501}
502
503static int
Lev Walkinf4b711a2017-08-23 06:55:27 -0700504asn1print_crange_value(const asn1cnst_edge_t *edge, int as_char) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000505 switch(edge->type) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700506 case ARE_MIN: safe_printf("MIN"); break;
507 case ARE_MAX: safe_printf("MAX"); break;
Lev Walkin3140e0e2004-08-18 04:50:37 +0000508 case ARE_VALUE:
509 if(as_char) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700510 safe_printf("\"%c\"", (unsigned char)edge->value);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000511 } else {
Lev Walkinda997b12017-08-04 01:38:41 -0700512 safe_printf("%s", asn1p_itoa(edge->value));
Lev Walkin3140e0e2004-08-18 04:50:37 +0000513 }
514 }
515 return 0;
516}
517
518static int
Lev Walkina28cbb92017-07-31 20:20:17 -0700519asn1print_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 +0000520 asn1cnst_range_t *range;
521 int as_char = (type==ACT_CT_FROM);
522 int i;
523
Lev Walkina28cbb92017-07-31 20:20:17 -0700524 range = asn1constraint_compute_constraint_range(dbg_name, expr_type, ct, type, 0, 0, cpr);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000525 if(!range) return -1;
526
Lev Walkina2abcaa2017-07-23 21:08:49 +0400527 if(range->incompatible) return 0;
528
529 if((cpr & CPR_strict_OER_visibility) && range->not_OER_visible) {
530 asn1constraint_range_free(range);
531 return 0;
532 }
533
534 if((cpr & CPR_strict_PER_visibility) && range->not_PER_visible) {
Lev Walkine8e87f12004-08-25 02:00:03 +0000535 asn1constraint_range_free(range);
536 return 0;
537 }
538
Lev Walkin3140e0e2004-08-18 04:50:37 +0000539 switch(type) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700540 case ACT_CT_FROM: safe_printf("(FROM("); break;
541 case ACT_CT_SIZE: safe_printf("(SIZE("); break;
542 default: safe_printf("("); break;
Lev Walkin3140e0e2004-08-18 04:50:37 +0000543 }
544 for(i = -1; i < range->el_count; i++) {
545 asn1cnst_range_t *r;
546 if(i == -1) {
547 if(range->el_count) continue;
548 r = range;
549 } else {
550 r = range->elements[i];
551 }
552 if(i > 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700553 safe_printf(" | ");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000554 }
555 asn1print_crange_value(&r->left, as_char);
556 if(r->left.type != r->right.type
557 || r->left.value != r->right.value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700558 safe_printf("..");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000559 asn1print_crange_value(&r->right, as_char);
560 }
561 }
562 if(range->extensible)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700563 safe_printf(",...");
564 safe_printf(type==ACT_EL_RANGE?")":"))");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000565
566 if(range->empty_constraint)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700567 safe_printf(":Empty!");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000568
569 asn1constraint_range_free(range);
570 return 0;
571}
572
573static int
Lev Walkina28cbb92017-07-31 20:20:17 -0700574asn1print_constraint_explain(const char *dbg_name, asn1p_expr_type_e expr_type,
Lev Walkina2abcaa2017-07-23 21:08:49 +0400575 asn1p_constraint_t *ct, enum cpr_flags cpr) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000576
Lev Walkina28cbb92017-07-31 20:20:17 -0700577 asn1print_constraint_explain_type(dbg_name, expr_type, ct, ACT_EL_RANGE, cpr);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700578 safe_printf(" ");
Lev Walkina28cbb92017-07-31 20:20:17 -0700579 asn1print_constraint_explain_type(dbg_name, expr_type, ct, ACT_CT_SIZE, cpr);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700580 safe_printf(" ");
Lev Walkina28cbb92017-07-31 20:20:17 -0700581 asn1print_constraint_explain_type(dbg_name, expr_type, ct, ACT_CT_FROM, cpr);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000582
583 return 0;
584}
585
586static int
587asn1print_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 +0000588 int SEQ_OF = 0;
Lev Walkind523ea42017-09-06 22:15:08 -0700589 int has_space = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000590
Lev Walkind523ea42017-09-06 22:15:08 -0700591#define HAS_SPACE() \
592 do { \
593 has_space = 1; \
594 } while(0)
595#define ENSURE_SPACE() \
596 do { \
597 if(!has_space) { \
598 has_space = 1; \
599 safe_printf(" "); \
600 } \
601 } while(0)
602#define WANT_SPACE() \
603 do { \
604 has_space = 0; \
605 } while(0)
606
607 if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT))
Lev Walkinf7484512004-10-13 09:13:56 +0000608 INDENT("-- #line %d\n", tc->_lineno);
Lev Walkinef625402005-09-05 05:17:57 +0000609
610 /* Reconstruct compiler directive information */
611 if((tc->marker.flags & EM_INDIRECT)
612 && (tc->marker.flags & EM_OMITABLE) != EM_OMITABLE) {
613 if((flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700614 safe_printf(" --<ASN1C.RepresentAsPointer>-- ");
Lev Walkinef625402005-09-05 05:17:57 +0000615 else
616 INDENT("--<ASN1C.RepresentAsPointer>--\n");
617 }
618
Lev Walkina00d6b32006-03-21 03:40:38 +0000619 if(tc->Identifier
620 && (!(tc->meta_type == AMT_VALUE && tc->expr_type == A1TC_REFERENCE)
Lev Walkind523ea42017-09-06 22:15:08 -0700621 || level == 0)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000622 INDENT("%s", tc->Identifier);
Lev Walkind523ea42017-09-06 22:15:08 -0700623 WANT_SPACE();
624 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000625
Lev Walkina00d6b32006-03-21 03:40:38 +0000626 if(tc->lhs_params) {
627 asn1print_params(tc->lhs_params, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +0000628 }
629
630 if(tc->meta_type != AMT_VALUE
Lev Walkinc74ea222004-08-25 02:27:47 +0000631 && tc->meta_type != AMT_VALUESET
Lev Walkinf15320b2004-06-03 03:38:44 +0000632 && tc->expr_type != A1TC_EXTENSIBLE) {
633 if(level) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000634 if(tc->Identifier && !(flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700635 safe_printf("\t");
Lev Walkind523ea42017-09-06 22:15:08 -0700636 } else if(tc->Identifier) {
637 ENSURE_SPACE();
638 safe_printf("::=");
639 WANT_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000640 }
641 }
642
643 if(tc->tag.tag_class) {
Lev Walkind523ea42017-09-06 22:15:08 -0700644 ENSURE_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000645 asn1print_tag(tc, flags);
Lev Walkind523ea42017-09-06 22:15:08 -0700646 WANT_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000647 }
648
649 switch(tc->expr_type) {
650 case A1TC_EXTENSIBLE:
651 if(tc->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700652 safe_printf("!");
Lev Walkinf15320b2004-06-03 03:38:44 +0000653 asn1print_value(tc->value, flags);
654 }
655 break;
Lev Walkinfd151ce2004-08-22 03:08:51 +0000656 case A1TC_COMPONENTS_OF:
657 SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700658 safe_printf(" COMPONENTS OF");
Lev Walkind523ea42017-09-06 22:15:08 -0700659 WANT_SPACE();
Lev Walkinfd151ce2004-08-22 03:08:51 +0000660 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000661 case A1TC_REFERENCE:
662 case A1TC_UNIVERVAL:
Lev Walkinf15320b2004-06-03 03:38:44 +0000663 break;
664 case A1TC_CLASSDEF:
Lev Walkinf2b2f372016-03-14 02:23:48 -0700665 safe_printf(" CLASS");
Lev Walkind523ea42017-09-06 22:15:08 -0700666 WANT_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000667 break;
Lev Walkin9c2285a2006-03-09 08:49:26 +0000668 case A1TC_CLASSFIELD_TFS ... A1TC_CLASSFIELD_OSFS:
Lev Walkinf15320b2004-06-03 03:38:44 +0000669 /* Nothing to print here */
670 break;
Lev Walkinb1ef3962004-08-20 13:24:28 +0000671 case ASN_CONSTR_SET_OF:
672 case ASN_CONSTR_SEQUENCE_OF:
673 SEQ_OF = 1;
Lev Walkind523ea42017-09-06 22:15:08 -0700674 ENSURE_SPACE();
Lev Walkinb1ef3962004-08-20 13:24:28 +0000675 if(tc->expr_type == ASN_CONSTR_SET_OF)
Lev Walkind523ea42017-09-06 22:15:08 -0700676 safe_printf("SET");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000677 else
Lev Walkind523ea42017-09-06 22:15:08 -0700678 safe_printf("SEQUENCE");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000679 if(tc->constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700680 safe_printf(" ");
Lev Walkinb1ef3962004-08-20 13:24:28 +0000681 asn1print_constraint(tc->constraints, flags);
682 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700683 safe_printf(" OF");
Lev Walkind523ea42017-09-06 22:15:08 -0700684 WANT_SPACE();
Lev Walkinb1ef3962004-08-20 13:24:28 +0000685 break;
Lev Walkin5045dfa2006-03-21 09:41:28 +0000686 case A1TC_VALUESET:
687 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000688 default:
689 {
690 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
Lev Walkind523ea42017-09-06 22:15:08 -0700691 ENSURE_SPACE();
692 safe_printf("%s", p?p:"<unknown type!>");
693 WANT_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000694 }
695 break;
696 }
697
Lev Walkinb9d0ae32005-06-02 04:06:24 +0000698 /*
699 * Put the name of the referred type.
700 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000701 if(tc->reference) {
Lev Walkind523ea42017-09-06 22:15:08 -0700702 ENSURE_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000703 asn1print_ref(tc->reference, flags);
Lev Walkind523ea42017-09-06 22:15:08 -0700704 WANT_SPACE();
Lev Walkinf15320b2004-06-03 03:38:44 +0000705 }
706
Lev Walkind523ea42017-09-06 22:15:08 -0700707 if(tc->meta_type == AMT_VALUESET && level == 0) {
708 ENSURE_SPACE();
709 safe_printf("::=");
710 WANT_SPACE();
711 }
Lev Walkinc74ea222004-08-25 02:27:47 +0000712
Lev Walkinf15320b2004-06-03 03:38:44 +0000713 /*
714 * Display the descendants (children) of the current type.
715 */
Lev Walkinc74ea222004-08-25 02:27:47 +0000716 if(TQ_FIRST(&(tc->members))
717 || (tc->expr_type & ASN_CONSTR_MASK)
Lev Walkinc74ea222004-08-25 02:27:47 +0000718 || tc->meta_type == AMT_OBJECT
Lev Walkin9c2285a2006-03-09 08:49:26 +0000719 || tc->meta_type == AMT_OBJECTCLASS
720 || tc->meta_type == AMT_OBJECTFIELD
Lev Walkinc74ea222004-08-25 02:27:47 +0000721 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000722 asn1p_expr_t *se; /* SubExpression */
Lev Walkin9c2285a2006-03-09 08:49:26 +0000723 int put_braces = (!SEQ_OF) /* Don't need 'em, if SET OF... */
724 && (tc->meta_type != AMT_OBJECTFIELD);
Lev Walkinf15320b2004-06-03 03:38:44 +0000725
Lev Walkinc74ea222004-08-25 02:27:47 +0000726 if(put_braces) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000727 if(flags & APF_NOINDENT) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700728 safe_printf("{");
Lev Walkinf4069d22005-02-15 07:06:05 +0000729 if(!TQ_FIRST(&tc->members))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700730 safe_printf("}");
Lev Walkinf4069d22005-02-15 07:06:05 +0000731 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700732 safe_printf(" {");
Lev Walkinf4069d22005-02-15 07:06:05 +0000733 if(TQ_FIRST(&tc->members))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700734 safe_printf("\n");
Lev Walkinf4069d22005-02-15 07:06:05 +0000735 else
Lev Walkinf2b2f372016-03-14 02:23:48 -0700736 safe_printf(" }");
Lev Walkinf4069d22005-02-15 07:06:05 +0000737 }
Lev Walkinc74ea222004-08-25 02:27:47 +0000738 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000739
740 TQ_FOR(se, &(tc->members), next) {
741 /*
Lev Walkinfd151ce2004-08-22 03:08:51 +0000742 * Print the expression as it were a stand-alone type.
Lev Walkinf15320b2004-06-03 03:38:44 +0000743 */
Lev Walkinf7484512004-10-13 09:13:56 +0000744 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinb1e07082004-09-15 11:44:55 +0000745 if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700746 safe_printf(" DEFAULT ");
Lev Walkinb1e07082004-09-15 11:44:55 +0000747 asn1print_value(se->marker.default_value, flags);
748 } else if((se->marker.flags & EM_OPTIONAL)
749 == EM_OPTIONAL) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700750 safe_printf(" OPTIONAL");
Lev Walkinb1e07082004-09-15 11:44:55 +0000751 }
Lev Walkinef625402005-09-05 05:17:57 +0000752 if(TQ_NEXT(se, next)) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700753 safe_printf(",");
Lev Walkinef625402005-09-05 05:17:57 +0000754 if(!(flags & APF_NOINDENT))
755 INDENT("\n");
756 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000757 }
758
Lev Walkinc74ea222004-08-25 02:27:47 +0000759 if(put_braces && TQ_FIRST(&tc->members)) {
Lev Walkinf4069d22005-02-15 07:06:05 +0000760 if(!(flags & APF_NOINDENT))
Lev Walkinf2b2f372016-03-14 02:23:48 -0700761 safe_printf("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000762 INDENT("}");
763 }
764 }
765
Lev Walkin9d542d22006-03-14 16:31:37 +0000766 if(tc->with_syntax) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700767 safe_printf(" WITH SYNTAX {");
Lev Walkinf15320b2004-06-03 03:38:44 +0000768 asn1print_with_syntax(tc->with_syntax, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700769 safe_printf("}\n");
Lev Walkin9d542d22006-03-14 16:31:37 +0000770 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000771
Lev Walkina00d6b32006-03-21 03:40:38 +0000772 /* Right hand specialization */
773 if(tc->rhs_pspecs) {
774 asn1p_expr_t *se;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700775 safe_printf("{");
Lev Walkina00d6b32006-03-21 03:40:38 +0000776 TQ_FOR(se, &(tc->rhs_pspecs->members), next) {
777 asn1print_expr(asn, mod, se, flags, level + 1);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700778 if(TQ_NEXT(se, next)) safe_printf(", ");
Lev Walkina00d6b32006-03-21 03:40:38 +0000779 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700780 safe_printf("}");
Lev Walkina00d6b32006-03-21 03:40:38 +0000781 }
782
Lev Walkinb1ef3962004-08-20 13:24:28 +0000783 if(!SEQ_OF && tc->constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700784 safe_printf(" ");
Lev Walkin557f27d2006-03-21 07:46:48 +0000785 if(tc->meta_type == AMT_VALUESET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700786 safe_printf("{");
Lev Walkinf15320b2004-06-03 03:38:44 +0000787 asn1print_constraint(tc->constraints, flags);
Lev Walkin557f27d2006-03-21 07:46:48 +0000788 if(tc->meta_type == AMT_VALUESET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700789 safe_printf("}");
Lev Walkinf15320b2004-06-03 03:38:44 +0000790 }
Lev Walkin3140e0e2004-08-18 04:50:37 +0000791
Lev Walkinf15320b2004-06-03 03:38:44 +0000792 if(tc->unique) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700793 safe_printf(" UNIQUE");
Lev Walkinf15320b2004-06-03 03:38:44 +0000794 }
795
796 if(tc->meta_type == AMT_VALUE
797 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin0056aec2004-09-05 10:38:50 +0000798 if(tc->expr_type == A1TC_UNIVERVAL) {
Lev Walkinb1e07082004-09-15 11:44:55 +0000799 if(tc->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700800 safe_printf("(");
Lev Walkinb1e07082004-09-15 11:44:55 +0000801 asn1print_value(tc->value, flags);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700802 safe_printf(")");
Lev Walkinb1e07082004-09-15 11:44:55 +0000803 }
Lev Walkin0056aec2004-09-05 10:38:50 +0000804 } else {
Lev Walkind523ea42017-09-06 22:15:08 -0700805 if(level == 0 && tc->Identifier) safe_printf(" ::= ");
Lev Walkin0056aec2004-09-05 10:38:50 +0000806 asn1print_value(tc->value, flags);
807 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000808 }
809
Lev Walkinfd151ce2004-08-22 03:08:51 +0000810 /*
Lev Walkinf4069d22005-02-15 07:06:05 +0000811 * The following section exists entirely for debugging.
Lev Walkinfd151ce2004-08-22 03:08:51 +0000812 */
Lev Walkind370e9f2006-03-16 10:03:35 +0000813 if(flags & APF_PRINT_CONSTRAINTS
Lev Walkinb1ef3962004-08-20 13:24:28 +0000814 && tc->expr_type != A1TC_EXTENSIBLE) {
Lev Walkin3140e0e2004-08-18 04:50:37 +0000815 asn1p_expr_t *top_parent;
816
817 if(tc->combined_constraints) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700818 safe_printf("\n-- Combined constraints: ");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000819 asn1print_constraint(tc->combined_constraints, flags);
820 }
821
Lev Walkinc0e03b92017-08-22 01:48:23 -0700822 top_parent = WITH_MODULE_NAMESPACE(
823 tc->module, tc_ns, asn1f_find_terminal_type_ex(asn, tc_ns, tc));
824 if(top_parent) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700825 safe_printf("\n-- Practical constraints (%s): ",
Lev Walkine8e87f12004-08-25 02:00:03 +0000826 top_parent->Identifier);
Lev Walkina28cbb92017-07-31 20:20:17 -0700827 asn1print_constraint_explain(top_parent->Identifier,
828 top_parent->expr_type,
Lev Walkine8e87f12004-08-25 02:00:03 +0000829 tc->combined_constraints, 0);
Lev Walkina2abcaa2017-07-23 21:08:49 +0400830 safe_printf("\n-- OER-visible constraints (%s): ",
831 top_parent->Identifier);
Lev Walkina28cbb92017-07-31 20:20:17 -0700832 asn1print_constraint_explain(top_parent->Identifier,
833 top_parent->expr_type,
Lev Walkina2abcaa2017-07-23 21:08:49 +0400834 tc->combined_constraints, CPR_strict_OER_visibility);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700835 safe_printf("\n-- PER-visible constraints (%s): ",
Lev Walkinb1ef3962004-08-20 13:24:28 +0000836 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_PER_visibility);
Lev Walkin3140e0e2004-08-18 04:50:37 +0000840 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700841 safe_printf("\n");
Lev Walkin3140e0e2004-08-18 04:50:37 +0000842 }
843
Lev Walkind0f7b912017-08-07 18:13:04 -0700844 if(flags & APF_PRINT_CLASS_MATRIX) do {
Lev Walkin62d95d22017-08-06 23:41:11 -0700845 size_t col, maxidlen;
Lev Walkin4dcf8362017-08-07 20:10:05 -0700846 if(tc->ioc_table == NULL) {
Lev Walkind0f7b912017-08-07 18:13:04 -0700847 if(tc->expr_type == A1TC_CLASSDEF) {
848 safe_printf("\n-- Information Object Class table is empty");
849 }
Lev Walkind370e9f2006-03-16 10:03:35 +0000850 break;
851 }
Lev Walkind0f7b912017-08-07 18:13:04 -0700852 safe_printf("\n-- Information Object Set has %d entr%s:\n",
Lev Walkin4dcf8362017-08-07 20:10:05 -0700853 tc->ioc_table->rows,
854 tc->ioc_table->rows==1 ? "y" : "ies");
855 maxidlen = asn1p_ioc_table_max_identifier_length(tc->ioc_table);
856 for(ssize_t r = -1; r < (ssize_t)tc->ioc_table->rows; r++) {
857 asn1p_ioc_row_t *row;
858 row = tc->ioc_table->row[r<0?0:r];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700859 if(r < 0) safe_printf("-- %s", r > 9 ? " " : "");
Lev Walkinbc407262017-08-23 10:03:22 -0700860 else
861 safe_printf("-- [%*d]", (tc->ioc_table->rows > 9) + 1, r + 1);
862 for(col = 0; col < row->columns; col++) {
Lev Walkind370e9f2006-03-16 10:03:35 +0000863 struct asn1p_ioc_cell_s *cell;
864 cell = &row->column[col];
865 if(r < 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700866 safe_printf("[%*s]", maxidlen,
Lev Walkind370e9f2006-03-16 10:03:35 +0000867 cell->field->Identifier);
868 continue;
869 }
870 if(!cell->value) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700871 safe_printf(" %*s ", maxidlen, "<no entry>");
Lev Walkind370e9f2006-03-16 10:03:35 +0000872 continue;
873 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700874 safe_printf(" %*s ", maxidlen,
Lev Walkind370e9f2006-03-16 10:03:35 +0000875 cell->value->Identifier);
876 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700877 safe_printf("\n");
Lev Walkind370e9f2006-03-16 10:03:35 +0000878 }
Lev Walkinbc407262017-08-23 10:03:22 -0700879 if(tc->ioc_table->extensible) {
880 safe_printf("-- [%*s] ...\n", (tc->ioc_table->rows>9)+1, "");
881 }
882 } while(0);
Lev Walkind370e9f2006-03-16 10:03:35 +0000883
Lev Walkina00d6b32006-03-21 03:40:38 +0000884 if(flags & APF_PRINT_CLASS_MATRIX
885 && tc->lhs_params) do {
886 int i;
887 if(tc->specializations.pspecs_count == 0) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700888 safe_printf("\n-- No specializations found\n");
Lev Walkina00d6b32006-03-21 03:40:38 +0000889 break;
890 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700891 safe_printf("\n-- Specializations list has %d entr%s:\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000892 tc->specializations.pspecs_count,
893 tc->specializations.pspecs_count == 1 ? "y" : "ies");
894 for(i = 0; i < tc->specializations.pspecs_count; i++) {
895 asn1p_expr_t *se;
896 struct asn1p_pspec_s *pspec;
897 pspec = &tc->specializations.pspec[i];
Lev Walkinf2b2f372016-03-14 02:23:48 -0700898 safe_printf("-- ");
Lev Walkina00d6b32006-03-21 03:40:38 +0000899 TQ_FOR(se, &(pspec->rhs_pspecs->members), next) {
900 asn1print_expr(asn, mod, se, flags, level+1);
901 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700902 safe_printf("\n");
Lev Walkina00d6b32006-03-21 03:40:38 +0000903 }
904 } while(0);
905
Lev Walkinf15320b2004-06-03 03:38:44 +0000906 return 0;
907}
Lev Walkin3140e0e2004-08-18 04:50:37 +0000908
Lev Walkinf7484512004-10-13 09:13:56 +0000909
910static int
911asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) {
912 asn1p_expr_t *se;
913 int expr_unordered = 0;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000914 int dont_involve_children = 0;
Lev Walkinf7484512004-10-13 09:13:56 +0000915
916 switch(expr->meta_type) {
917 case AMT_TYPE:
918 case AMT_TYPEREF:
919 break;
920 default:
921 if(expr->expr_type == A1TC_UNIVERVAL)
922 break;
923 return 0;
924 }
925
926 if(!expr->Identifier) return 0;
927
Lev Walkinf7484512004-10-13 09:13:56 +0000928 if(flags & APF_LINE_COMMENTS)
929 INDENT("<!-- #line %d -->\n", expr->_lineno);
930 INDENT("<!ELEMENT %s", expr->Identifier);
931
932 if(expr->expr_type == A1TC_REFERENCE) {
Lev Walkinc0e03b92017-08-22 01:48:23 -0700933 se = WITH_MODULE_NAMESPACE(expr->module, expr_ns, asn1f_find_terminal_type_ex(asn, expr_ns, expr));
Lev Walkinf7484512004-10-13 09:13:56 +0000934 if(!se) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700935 safe_printf(" (ANY)");
Lev Walkinf7484512004-10-13 09:13:56 +0000936 return 0;
937 }
938 expr = se;
Lev Walkinef39f7e2004-10-14 05:11:25 +0000939 dont_involve_children = 1;
Lev Walkinf7484512004-10-13 09:13:56 +0000940 }
941
Lev Walkind7963aa2005-03-10 15:16:56 +0000942 if(expr->expr_type == ASN_CONSTR_CHOICE
943 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
944 || expr->expr_type == ASN_CONSTR_SET_OF
945 || expr->expr_type == ASN_CONSTR_SET
Lev Walkin112d69e2005-03-09 20:52:15 +0000946 || expr->expr_type == ASN_BASIC_INTEGER
947 || expr->expr_type == ASN_BASIC_ENUMERATED) {
948 expr_unordered = 1;
949 }
950
Lev Walkinf7484512004-10-13 09:13:56 +0000951 if(TQ_FIRST(&expr->members)) {
952 int extensible = 0;
Lev Walkinfbaeb852006-11-21 10:39:52 +0000953 if(expr->expr_type == ASN_BASIC_BIT_STRING)
954 dont_involve_children = 1;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700955 safe_printf(" (");
Lev Walkinf7484512004-10-13 09:13:56 +0000956 TQ_FOR(se, &(expr->members), next) {
957 if(se->expr_type == A1TC_EXTENSIBLE) {
958 extensible = 1;
959 continue;
960 } else if(!se->Identifier
961 && se->expr_type == A1TC_REFERENCE) {
962 asn1print_ref(se->reference, flags);
963 } else if(se->Identifier) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700964 safe_printf("%s", se->Identifier);
Lev Walkinf7484512004-10-13 09:13:56 +0000965 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700966 safe_printf("ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000967 }
968 if(expr->expr_type != ASN_CONSTR_SET
969 && expr->expr_type != ASN_CONSTR_CHOICE
970 && expr->expr_type != ASN_BASIC_INTEGER
971 && expr->expr_type != ASN_BASIC_ENUMERATED) {
972 if(expr_unordered)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700973 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000974 else if(se->marker.flags)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700975 safe_printf("?");
Lev Walkinfbaeb852006-11-21 10:39:52 +0000976 else if(expr->expr_type == ASN_BASIC_BIT_STRING)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700977 safe_printf("?");
Lev Walkinf7484512004-10-13 09:13:56 +0000978 }
979 if(TQ_NEXT(se, next)
980 && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700981 safe_printf(expr_unordered?"|":", ");
Lev Walkinf7484512004-10-13 09:13:56 +0000982 }
983 }
984 if(extensible) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700985 safe_printf(expr_unordered?"|":", ");
986 safe_printf("ANY");
Lev Walkinf7484512004-10-13 09:13:56 +0000987 if(expr->expr_type != ASN_CONSTR_SET
988 && expr->expr_type != ASN_CONSTR_CHOICE
989 && expr->expr_type != ASN_BASIC_INTEGER
990 && expr->expr_type != ASN_BASIC_ENUMERATED)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700991 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000992 }
993
Lev Walkinf2b2f372016-03-14 02:23:48 -0700994 safe_printf(")");
Lev Walkinf7484512004-10-13 09:13:56 +0000995 if(expr->expr_type == ASN_CONSTR_SET)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700996 safe_printf("*");
Lev Walkinf7484512004-10-13 09:13:56 +0000997
Lev Walkinef39f7e2004-10-14 05:11:25 +0000998 } else switch(expr->expr_type) {
999 case ASN_BASIC_BOOLEAN:
Lev Walkinf2b2f372016-03-14 02:23:48 -07001000 safe_printf(" (true|false)");
Lev Walkinef39f7e2004-10-14 05:11:25 +00001001 break;
1002 case ASN_CONSTR_CHOICE:
1003 case ASN_CONSTR_SET:
1004 case ASN_CONSTR_SET_OF:
1005 case ASN_CONSTR_SEQUENCE:
1006 case ASN_CONSTR_SEQUENCE_OF:
1007 case ASN_BASIC_NULL:
1008 case A1TC_UNIVERVAL:
Lev Walkinf2b2f372016-03-14 02:23:48 -07001009 safe_printf(" EMPTY");
Lev Walkinef39f7e2004-10-14 05:11:25 +00001010 break;
1011 case ASN_TYPE_ANY:
Lev Walkinf2b2f372016-03-14 02:23:48 -07001012 safe_printf(" ANY");
Lev Walkinef39f7e2004-10-14 05:11:25 +00001013 break;
1014 case ASN_BASIC_BIT_STRING:
1015 case ASN_BASIC_OCTET_STRING:
1016 case ASN_BASIC_OBJECT_IDENTIFIER:
1017 case ASN_BASIC_RELATIVE_OID:
Lev Walkincc116532004-10-15 06:01:54 +00001018 case ASN_BASIC_INTEGER:
Lev Walkinef39f7e2004-10-14 05:11:25 +00001019 case ASN_BASIC_UTCTime:
1020 case ASN_BASIC_GeneralizedTime:
Lev Walkinef39f7e2004-10-14 05:11:25 +00001021 case ASN_STRING_NumericString:
1022 case ASN_STRING_PrintableString:
Lev Walkinf2b2f372016-03-14 02:23:48 -07001023 safe_printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +00001024 break;
1025 case ASN_STRING_VisibleString:
1026 case ASN_STRING_ISO646String:
1027 /* Entity references, but not XML elements may be present */
Lev Walkinf2b2f372016-03-14 02:23:48 -07001028 safe_printf(" (#PCDATA)");
Lev Walkinef39f7e2004-10-14 05:11:25 +00001029 break;
1030 case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
1031 case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
1032 default:
1033 /*
1034 * XML elements are allowed.
1035 * For example, a UTF8String may contain "<bel/>".
1036 */
Lev Walkinf2b2f372016-03-14 02:23:48 -07001037 safe_printf(" ANY");
Lev Walkinf7484512004-10-13 09:13:56 +00001038 }
Lev Walkinf2b2f372016-03-14 02:23:48 -07001039 safe_printf(">\n");
Lev Walkinf7484512004-10-13 09:13:56 +00001040
1041 /*
1042 * Display the descendants (children) of the current type.
1043 */
Lev Walkinef39f7e2004-10-14 05:11:25 +00001044 if(!dont_involve_children) {
1045 TQ_FOR(se, &(expr->members), next) {
1046 if(se->expr_type == A1TC_EXTENSIBLE) continue;
1047 asn1print_expr_dtd(asn, mod, se, flags, level + 1);
1048 }
Lev Walkinf7484512004-10-13 09:13:56 +00001049 }
1050
1051 return 0;
1052}