blob: 8e8049dc80febbc959b99e7502e1206c87d8bfe5 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#include <stdio.h>
2#include <string.h>
3#include <errno.h>
4#include <assert.h>
5
6#include <asn1parser.h>
7
8#include "asn1print.h"
9
10#define INDENT(fmt, args...) do { \
11 int __i = level; while(__i--) putchar(' '); \
12 printf(fmt, ##args); \
13 } while(0)
14
15static int asn1print_module(asn1p_module_t *mod, enum asn1print_flags_e flags);
16static int asn1print_oid(asn1p_oid_t *oid, enum asn1print_flags_e flags);
17static int asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags_e flags);
18static int asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags_e flags);
19static int asn1print_params(asn1p_paramlist_t *pl,enum asn1print_flags_e flags);
20static int asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags_e flags);
21static int asn1print_constraint(asn1p_constraint_t *, enum asn1print_flags_e);
22static int asn1print_value(asn1p_value_t *val, enum asn1print_flags_e flags);
23static int asn1print_expr(asn1p_expr_t *tc, enum asn1print_flags_e flags,
24 int level);
25
26/*
27 * Print the contents of the parsed ASN tree.
28 */
29int
30asn1print(asn1p_t *asn, enum asn1print_flags_e flags) {
31 asn1p_module_t *mod;
32
33 if(asn == NULL) {
34 errno = EINVAL;
35 return -1;
36 }
37
38 TQ_FOR(mod, &(asn->modules), mod_next) {
39 asn1print_module(mod, flags);
40 }
41
42 return 0;
43}
44
45static int
46asn1print_module(asn1p_module_t *mod, enum asn1print_flags_e flags) {
47 asn1p_expr_t *tc;
48
49 if(!(flags & APF_NO_SOURCE_COMMENTS))
50 printf("\n-- Contents of \"%s\"\n", mod->source_file_name);
51
52 printf("\n%s ", mod->Identifier);
53 if(mod->module_oid) {
54 asn1print_oid(mod->module_oid, flags);
55 printf("\n");
56 }
57
58 printf("DEFINITIONS");
59
60 if(mod->module_flags & MSF_EXPLICIT_TAGS)
61 printf(" EXPLICIT TAGS");
62 if(mod->module_flags & MSF_IMPLICIT_TAGS)
63 printf(" IMPLICIT TAGS");
64 if(mod->module_flags & MSF_AUTOMATIC_TAGS)
65 printf(" AUTOMATIC TAGS");
66 if(mod->module_flags & MSF_EXTENSIBILITY_IMPLIED)
67 printf(" EXTENSIBILITY IMPLIED");
68
69 printf(" ::=\n");
70 printf("BEGIN\n\n");
71
72 TQ_FOR(tc, &(mod->members), next) {
73 asn1print_expr(tc, flags, 0);
74 printf("\n\n");
75 }
76
77 printf("END\n");
78
79 return 0;
80}
81
82static int
83asn1print_oid(asn1p_oid_t *oid, enum asn1print_flags_e flags) {
84 int ac;
85 int accum = 0;
86
vlmb42843a2004-06-05 08:17:50 +000087 (void)flags; /* Unused argument */
88
vlmfa67ddc2004-06-03 03:38:44 +000089 printf("{");
90 for(ac = 0; ac < oid->arcs_count; ac++) {
91 if(accum + strlen(oid->arcs[ac].name?:"") > 50) {
92 printf("\n\t");
93 accum = 0;
94 } else if(ac) printf(" ");
95
96 if(oid->arcs[ac].name) {
97 printf("%s(%d)",
98 oid->arcs[ac].name,
99 (int)oid->arcs[ac].number);
100 accum += strlen(oid->arcs[ac].name);
101 } else {
102 printf("%d",
103 (int)oid->arcs[ac].number);
104 }
105 accum += 4;
106 }
107 printf(" }");
108
109 return 0;
110}
111
112static int
113asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags_e flags) {
114 int cc;
115
vlmb42843a2004-06-05 08:17:50 +0000116 (void)flags; /* Unused argument */
117
vlmfa67ddc2004-06-03 03:38:44 +0000118 for(cc = 0; cc < ref->comp_count; cc++) {
119 if(cc) printf(".");
120 printf("%s", ref->components[cc].name);
121 }
122
123 return 0;
124}
125
126static int
127asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags_e flags) {
128 struct asn1p_type_tag_s *tag = &tc->tag;
129
vlmb42843a2004-06-05 08:17:50 +0000130 (void)flags; /* Unused argument */
131
vlmfa67ddc2004-06-03 03:38:44 +0000132 if(tag->tag_class == TC_NOCLASS)
133 return 0;
134
135 printf("[");
136 switch(tag->tag_class) {
137 case TC_NOCLASS:
138 assert(tag->tag_class != TC_NOCLASS);
139 break;
140 case TC_UNIVERSAL: printf("UNIVERSAL "); break;
141 case TC_PRIVATE: printf("PRIVATE "); break;
142 case TC_APPLICATION: printf("APPLICATION "); break;
143 case TC_CONTEXT_SPECIFIC:
144 break;
145 }
146 printf("%lld]", (long long)tag->tag_value);
147
148 switch(tag->tag_mode) {
149 case TM_DEFAULT: break;
150 case TM_IMPLICIT: printf(" IMPLICIT"); break;
151 case TM_EXPLICIT: printf(" EXPLICIT"); break;
152 }
153
154 return 0;
155}
156
157static int
158asn1print_value(asn1p_value_t *val, enum asn1print_flags_e flags) {
159
160 if(val == NULL)
161 return 0;
162
163 switch(val->type) {
164 case ATV_NOVALUE:
165 break;
166 case ATV_REFERENCED:
167 return asn1print_ref(val->value.reference, flags);
168 case ATV_INTEGER:
169 printf("%lld", (long long)val->value.v_integer);
170 return 0;
171 case ATV_MIN: printf("MIN"); return 0;
172 case ATV_MAX: printf("MAX"); return 0;
173 case ATV_FALSE: printf("FALSE"); return 0;
174 case ATV_TRUE: printf("TRUE"); return 0;
175 case ATV_REAL:
176 printf("%f", val->value.v_double);
177 return 0;
178 case ATV_STRING:
179 {
180 char *p = val->value.string.buf;
181 putchar('"');
182 if(strchr(p, '"')) {
183 /* Mask quotes */
184 for(; *p; p++) {
185 if(*p == '"')
186 putchar(*p);
187 putchar(*p);
188 }
189 } else {
190 fputs(p, stdout);
191 }
192 putchar('"');
193 }
194 return 0;
195 case ATV_UNPARSED:
196 fputs(val->value.string.buf, stdout);
197 return 0;
198 case ATV_BITVECTOR:
199 {
200 uint8_t *bitvector;
201 int bits;
202 int i;
203
204 bitvector = val->value.binary_vector.bits;
205 bits = val->value.binary_vector.size_in_bits;
206
207 printf("'");
208 if(bits%8) {
209 for(i = 0; i < bits; i++) {
210 uint8_t uc;
211 uc = bitvector[i>>3];
212 putchar(((uc >> (7-(i%8)))&1)?'1':'0');
213 }
214 printf("'B");
215 } else {
216 char hextable[16] = "0123456789ABCDEF";
217 for(i = 0; i < (bits>>3); i++) {
218 putchar(hextable[bitvector[i] >> 4]);
219 putchar(hextable[bitvector[i] & 0x0f]);
220 }
221 printf("'H");
222 }
223 }
224 }
225
226 assert(val->type || !"Unknown");
227
228 return 0;
229}
230
231static int
232asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags_e flags) {
233 int symno = 0;
234
235 if(ct == 0) return 0;
236
237 if(ct->type == ACT_CA_SET)
238 printf("(");
239
240 switch(ct->type) {
241 case ACT_EL_VALUE:
242 asn1print_value(ct->value, flags);
243 break;
244 case ACT_EL_RANGE:
245 case ACT_EL_LLRANGE:
246 case ACT_EL_RLRANGE:
247 case ACT_EL_ULRANGE:
248 asn1print_value(ct->range_start, flags);
249 switch(ct->type) {
250 case ACT_EL_RANGE: printf(".."); break;
251 case ACT_EL_LLRANGE: printf("<.."); break;
252 case ACT_EL_RLRANGE: printf("..<"); break;
253 case ACT_EL_ULRANGE: printf("<..<"); break;
254 default: printf("?..?"); break;
255 }
256 asn1print_value(ct->range_stop, flags);
257 break;
258 case ACT_EL_EXT:
259 printf("...");
260 break;
261 case ACT_CT_SIZE:
262 case ACT_CT_FROM:
263 switch(ct->type) {
264 case ACT_CT_SIZE: printf("SIZE ("); break;
265 case ACT_CT_FROM: printf("FROM ("); break;
266 default: printf("??? ("); break;
267 }
268 assert(ct->el_count != 0);
269 assert(ct->el_count == 1);
270 asn1print_constraint(ct->elements[0], flags);
271 printf(")");
272 break;
273 case ACT_CT_WCOMP:
274 case ACT_CT_WCOMPS:
275 printf("???");
276 break;
277 case ACT_CA_SET: symno++;
278 case ACT_CA_CRC: symno++;
279 case ACT_CA_CSV: symno++;
280 case ACT_CA_UNI: symno++;
281 case ACT_CA_INT: symno++;
282 case ACT_CA_EXC:
283 {
284 char *symtable[] = { " EXCEPT ", "^", "|", ",",
285 "", "(" };
286 int i;
287 for(i = 0; i < ct->el_count; i++) {
288 enum asn1print_flags_e nflags = flags;
289 if(i) fputs(symtable[symno], stdout);
290 if(ct->type == ACT_CA_CRC) fputs("{", stdout);
291 asn1print_constraint(ct->elements[i], nflags);
292 if(ct->type == ACT_CA_CRC) fputs("}", stdout);
293 if(i+1 < ct->el_count
294 && ct->type == ACT_CA_SET)
295 fputs(")", stdout);
296 }
297 }
298 break;
299 case ACT_INVALID:
300 assert(ct->type != ACT_INVALID);
301 break;
302 }
303
304 if(ct->type == ACT_CA_SET)
305 printf(")");
306
307 return 0;
308}
309
310static int
311asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags_e flags) {
312 if(pl) {
313 int i;
314 printf("{");
315 for(i = 0; i < pl->params_count; i++) {
316 if(i) printf(", ");
317 if(pl->params[i].governor) {
318 asn1print_ref(pl->params[i].governor, flags);
319 printf(":");
320 }
321 printf("%s", pl->params[i].argument);
322 }
323 printf("}");
324 }
325
326 return 0;
327}
328
329static int
330asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags_e flags) {
331 if(wx) {
332 asn1p_wsyntx_chunk_t *wc;
333 printf(" WITH SYNTAX {");
334 TQ_FOR(wc, &(wx->chunks), next) {
335 if(wc->ref) {
336 asn1print_ref(wc->ref, flags);
337 } else {
338 fwrite(wc->buf, 1, wc->len, stdout);
339 }
340 }
341 printf("}\n");
342 }
343
344 return 0;
345}
346
347static int
348asn1print_expr(asn1p_expr_t *tc, enum asn1print_flags_e flags, int level) {
349
350 if(flags & APF_LINE_COMMENTS)
351 INDENT("-- #line %d\n", tc->_lineno);
352 if(tc->Identifier)
353 INDENT("%s", tc->Identifier);
354
355 if(tc->params) {
356 asn1print_params(tc->params, flags);
357 }
358
359 if(tc->meta_type != AMT_VALUE
360 && tc->expr_type != A1TC_EXTENSIBLE) {
361 if(level) {
362 if(tc->Identifier)
363 printf("\t");
364 } else {
365 printf(" ::=");
366 }
367 }
368
369 if(tc->tag.tag_class) {
370 printf(" ");
371 asn1print_tag(tc, flags);
372 }
373
374 switch(tc->expr_type) {
375 case A1TC_EXTENSIBLE:
376 if(tc->value) {
377 printf("!");
378 asn1print_value(tc->value, flags);
379 }
380 break;
381 case A1TC_REFERENCE:
382 case A1TC_UNIVERVAL:
383 case A1TC_PARAMETRIZED:
384 break;
385 case A1TC_CLASSDEF:
386 printf(" CLASS");
387 break;
388 case A1TC_CLASSFIELD:
389 /* Nothing to print here */
390 break;
391 default:
392 {
393 char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
394 printf(" %s", p?p:"<unknown type!>");
395 }
396 break;
397 }
398
399 if(tc->reference) {
400 printf(" ");
401 asn1print_ref(tc->reference, flags);
402 }
403
404 /*
405 * Display the descendants (children) of the current type.
406 */
407 if(TQ_FIRST(&(tc->members))) {
408 asn1p_expr_t *se; /* SubExpression */
409
410 if((tc->expr_type != ASN_CONSTR_SEQUENCE_OF
411 && tc->expr_type != ASN_CONSTR_SET_OF)
412 || TQ_FIRST(&(tc->members))->expr_type & ASN_CONSTR_MASK)
413 printf(" {\n");
414
415 TQ_FOR(se, &(tc->members), next) {
416 /*
417 * Print the expression as it were stand-alone type.
418 */
419 asn1print_expr(se, flags, level + 4);
420 switch(se->marker) {
421 case EM_NOMARK: break;
422 case EM_OPTIONAL: printf(" OPTIONAL"); break;
423 case EM_DEFAULT: printf(" DEFAULT <?>"); break;
424 }
425 if(TQ_NEXT(se, next)) {
426 printf(",");
427 INDENT("\n");
428 }
429 }
430
431 if((tc->expr_type != ASN_CONSTR_SEQUENCE_OF
432 && tc->expr_type != ASN_CONSTR_SET_OF)
433 || TQ_FIRST(&(tc->members))->expr_type & ASN_CONSTR_MASK) {
434 printf("\n");
435 INDENT("}");
436 }
437 }
438
439 if(tc->with_syntax)
440 asn1print_with_syntax(tc->with_syntax, flags);
441
442 if(tc->constraints) {
443 printf(" ");
444 asn1print_constraint(tc->constraints, flags);
445 }
446 if(tc->unique) {
447 printf(" UNIQUE");
448 }
449
450 if(tc->meta_type == AMT_VALUE
451 && tc->expr_type != A1TC_EXTENSIBLE) {
452 if(tc->expr_type == A1TC_UNIVERVAL)
453 printf("(");
454 else
455 printf(" ::= ");
456 asn1print_value(tc->value, flags);
457 if(tc->expr_type == A1TC_UNIVERVAL)
458 printf(")");
459 }
460
461 return 0;
462}