vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <errno.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include <asn1parser.h> |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 7 | #include <asn1fix_export.h> |
vlm | 04a79e6 | 2004-08-18 05:41:05 +0000 | [diff] [blame] | 8 | #include <asn1fix_crange.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 9 | |
| 10 | #include "asn1print.h" |
| 11 | |
| 12 | #define INDENT(fmt, args...) do { \ |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 13 | int __i = level; while(__i--) printf(" "); \ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | printf(fmt, ##args); \ |
| 15 | } while(0) |
| 16 | |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 17 | static int asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags); |
| 18 | static int asn1print_oid(asn1p_oid_t *oid, enum asn1print_flags flags); |
| 19 | static int asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags); |
| 20 | static int asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags); |
| 21 | static int asn1print_params(asn1p_paramlist_t *pl,enum asn1print_flags flags); |
| 22 | static int asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags); |
| 23 | static int asn1print_constraint(asn1p_constraint_t *, enum asn1print_flags); |
| 24 | static int asn1print_value(asn1p_value_t *val, enum asn1print_flags flags); |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 25 | static int asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level); |
| 26 | static int asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Print the contents of the parsed ASN tree. |
| 30 | */ |
| 31 | int |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 32 | asn1print(asn1p_t *asn, enum asn1print_flags flags) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 33 | asn1p_module_t *mod; |
vlm | 281d82a | 2004-09-30 06:38:21 +0000 | [diff] [blame] | 34 | int modno = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 35 | |
| 36 | if(asn == NULL) { |
| 37 | errno = EINVAL; |
| 38 | return -1; |
| 39 | } |
| 40 | |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 41 | if(flags & APF_PRINT_XML_DTD) |
| 42 | printf("<!-- XML DTD generated by asn1c-" VERSION " -->\n\n"); |
| 43 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 44 | TQ_FOR(mod, &(asn->modules), mod_next) { |
vlm | 281d82a | 2004-09-30 06:38:21 +0000 | [diff] [blame] | 45 | if(modno++) printf("\n"); |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 46 | asn1print_module(asn, mod, flags); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 53 | asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 54 | asn1p_expr_t *tc; |
| 55 | |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 56 | if(flags & APF_PRINT_XML_DTD) |
| 57 | printf("<!-- ASN.1 module\n"); |
| 58 | |
vlm | 281d82a | 2004-09-30 06:38:21 +0000 | [diff] [blame] | 59 | printf("%s ", mod->Identifier); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | if(mod->module_oid) { |
| 61 | asn1print_oid(mod->module_oid, flags); |
| 62 | printf("\n"); |
| 63 | } |
| 64 | |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 65 | if(flags & APF_PRINT_XML_DTD) { |
| 66 | if(mod->source_file_name |
| 67 | && strcmp(mod->source_file_name, "-")) |
| 68 | printf("found in %s", mod->source_file_name); |
| 69 | printf(" -->\n\n"); |
| 70 | |
| 71 | TQ_FOR(tc, &(mod->members), next) { |
| 72 | asn1print_expr_dtd(asn, mod, tc, flags, 0); |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 78 | printf("DEFINITIONS"); |
| 79 | |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 80 | if(mod->module_flags & MSF_TAG_INSTRUCTIONS) |
| 81 | printf(" TAG INSTRUCTIONS"); |
| 82 | if(mod->module_flags & MSF_XER_INSTRUCTIONS) |
| 83 | printf(" XER INSTRUCTIONS"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | if(mod->module_flags & MSF_EXPLICIT_TAGS) |
| 85 | printf(" EXPLICIT TAGS"); |
| 86 | if(mod->module_flags & MSF_IMPLICIT_TAGS) |
| 87 | printf(" IMPLICIT TAGS"); |
| 88 | if(mod->module_flags & MSF_AUTOMATIC_TAGS) |
| 89 | printf(" AUTOMATIC TAGS"); |
| 90 | if(mod->module_flags & MSF_EXTENSIBILITY_IMPLIED) |
| 91 | printf(" EXTENSIBILITY IMPLIED"); |
| 92 | |
| 93 | printf(" ::=\n"); |
| 94 | printf("BEGIN\n\n"); |
| 95 | |
| 96 | TQ_FOR(tc, &(mod->members), next) { |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 97 | asn1print_expr(asn, mod, tc, flags, 0); |
vlm | 00ad282 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 98 | if(flags & APF_DEBUG_CONSTRAINTS) |
| 99 | printf("\n"); |
| 100 | else |
| 101 | printf("\n\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | printf("END\n"); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 110 | asn1print_oid(asn1p_oid_t *oid, enum asn1print_flags flags) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | int ac; |
| 112 | int accum = 0; |
| 113 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 114 | (void)flags; /* Unused argument */ |
| 115 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 116 | printf("{"); |
| 117 | for(ac = 0; ac < oid->arcs_count; ac++) { |
| 118 | if(accum + strlen(oid->arcs[ac].name?:"") > 50) { |
| 119 | printf("\n\t"); |
| 120 | accum = 0; |
| 121 | } else if(ac) printf(" "); |
| 122 | |
| 123 | if(oid->arcs[ac].name) { |
vlm | dc4d95a | 2004-09-05 10:38:50 +0000 | [diff] [blame] | 124 | printf("%s", oid->arcs[ac].name); |
| 125 | if(oid->arcs[ac].number >= 0) { |
vlm | 47ae158 | 2004-09-24 21:01:43 +0000 | [diff] [blame] | 126 | printf("(%" PRIdASN ")", oid->arcs[ac].number); |
vlm | dc4d95a | 2004-09-05 10:38:50 +0000 | [diff] [blame] | 127 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 128 | accum += strlen(oid->arcs[ac].name); |
| 129 | } else { |
| 130 | printf("%d", |
| 131 | (int)oid->arcs[ac].number); |
| 132 | } |
| 133 | accum += 4; |
| 134 | } |
| 135 | printf(" }"); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 141 | asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 142 | int cc; |
| 143 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 144 | (void)flags; /* Unused argument */ |
| 145 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 146 | for(cc = 0; cc < ref->comp_count; cc++) { |
| 147 | if(cc) printf("."); |
| 148 | printf("%s", ref->components[cc].name); |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static int |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 155 | asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 156 | struct asn1p_type_tag_s *tag = &tc->tag; |
| 157 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 158 | (void)flags; /* Unused argument */ |
| 159 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 160 | if(tag->tag_class == TC_NOCLASS) |
| 161 | return 0; |
| 162 | |
| 163 | printf("["); |
| 164 | switch(tag->tag_class) { |
| 165 | case TC_NOCLASS: |
| 166 | assert(tag->tag_class != TC_NOCLASS); |
| 167 | break; |
| 168 | case TC_UNIVERSAL: printf("UNIVERSAL "); break; |
| 169 | case TC_PRIVATE: printf("PRIVATE "); break; |
| 170 | case TC_APPLICATION: printf("APPLICATION "); break; |
| 171 | case TC_CONTEXT_SPECIFIC: |
| 172 | break; |
| 173 | } |
vlm | 47ae158 | 2004-09-24 21:01:43 +0000 | [diff] [blame] | 174 | printf("%" PRIdASN "]", tag->tag_value); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 175 | |
| 176 | switch(tag->tag_mode) { |
| 177 | case TM_DEFAULT: break; |
| 178 | case TM_IMPLICIT: printf(" IMPLICIT"); break; |
| 179 | case TM_EXPLICIT: printf(" EXPLICIT"); break; |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 186 | asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 187 | |
| 188 | if(val == NULL) |
| 189 | return 0; |
| 190 | |
| 191 | switch(val->type) { |
| 192 | case ATV_NOVALUE: |
| 193 | break; |
vlm | 093f675 | 2004-09-15 11:44:55 +0000 | [diff] [blame] | 194 | case ATV_NULL: |
| 195 | printf("NULL"); |
| 196 | return 0; |
| 197 | case ATV_REAL: |
| 198 | printf("%f", val->value.v_double); |
| 199 | return 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 200 | case ATV_INTEGER: |
vlm | 47ae158 | 2004-09-24 21:01:43 +0000 | [diff] [blame] | 201 | printf("%" PRIdASN, val->value.v_integer); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 202 | return 0; |
| 203 | case ATV_MIN: printf("MIN"); return 0; |
| 204 | case ATV_MAX: printf("MAX"); return 0; |
| 205 | case ATV_FALSE: printf("FALSE"); return 0; |
| 206 | case ATV_TRUE: printf("TRUE"); return 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 207 | case ATV_STRING: |
| 208 | { |
| 209 | char *p = val->value.string.buf; |
| 210 | putchar('"'); |
| 211 | if(strchr(p, '"')) { |
| 212 | /* Mask quotes */ |
| 213 | for(; *p; p++) { |
| 214 | if(*p == '"') |
| 215 | putchar(*p); |
| 216 | putchar(*p); |
| 217 | } |
| 218 | } else { |
| 219 | fputs(p, stdout); |
| 220 | } |
| 221 | putchar('"'); |
| 222 | } |
| 223 | return 0; |
| 224 | case ATV_UNPARSED: |
| 225 | fputs(val->value.string.buf, stdout); |
| 226 | return 0; |
| 227 | case ATV_BITVECTOR: |
| 228 | { |
| 229 | uint8_t *bitvector; |
| 230 | int bits; |
| 231 | int i; |
| 232 | |
| 233 | bitvector = val->value.binary_vector.bits; |
| 234 | bits = val->value.binary_vector.size_in_bits; |
| 235 | |
| 236 | printf("'"); |
| 237 | if(bits%8) { |
| 238 | for(i = 0; i < bits; i++) { |
| 239 | uint8_t uc; |
| 240 | uc = bitvector[i>>3]; |
| 241 | putchar(((uc >> (7-(i%8)))&1)?'1':'0'); |
| 242 | } |
| 243 | printf("'B"); |
| 244 | } else { |
| 245 | char hextable[16] = "0123456789ABCDEF"; |
| 246 | for(i = 0; i < (bits>>3); i++) { |
| 247 | putchar(hextable[bitvector[i] >> 4]); |
| 248 | putchar(hextable[bitvector[i] & 0x0f]); |
| 249 | } |
| 250 | printf("'H"); |
| 251 | } |
| 252 | } |
vlm | 093f675 | 2004-09-15 11:44:55 +0000 | [diff] [blame] | 253 | case ATV_REFERENCED: |
| 254 | return asn1print_ref(val->value.reference, flags); |
| 255 | case ATV_CHOICE_IDENTIFIER: |
| 256 | printf("%s: ", val->value.choice_identifier.identifier); |
| 257 | return asn1print_value(val->value.choice_identifier.value, flags); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | assert(val->type || !"Unknown"); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 266 | asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 267 | int symno = 0; |
| 268 | |
| 269 | if(ct == 0) return 0; |
| 270 | |
| 271 | if(ct->type == ACT_CA_SET) |
| 272 | printf("("); |
| 273 | |
| 274 | switch(ct->type) { |
| 275 | case ACT_EL_VALUE: |
| 276 | asn1print_value(ct->value, flags); |
| 277 | break; |
| 278 | case ACT_EL_RANGE: |
| 279 | case ACT_EL_LLRANGE: |
| 280 | case ACT_EL_RLRANGE: |
| 281 | case ACT_EL_ULRANGE: |
| 282 | asn1print_value(ct->range_start, flags); |
| 283 | switch(ct->type) { |
| 284 | case ACT_EL_RANGE: printf(".."); break; |
| 285 | case ACT_EL_LLRANGE: printf("<.."); break; |
| 286 | case ACT_EL_RLRANGE: printf("..<"); break; |
| 287 | case ACT_EL_ULRANGE: printf("<..<"); break; |
| 288 | default: printf("?..?"); break; |
| 289 | } |
| 290 | asn1print_value(ct->range_stop, flags); |
| 291 | break; |
| 292 | case ACT_EL_EXT: |
| 293 | printf("..."); |
| 294 | break; |
| 295 | case ACT_CT_SIZE: |
| 296 | case ACT_CT_FROM: |
| 297 | switch(ct->type) { |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 298 | case ACT_CT_SIZE: printf("SIZE("); break; |
| 299 | case ACT_CT_FROM: printf("FROM("); break; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 300 | default: printf("??? ("); break; |
| 301 | } |
| 302 | assert(ct->el_count != 0); |
| 303 | assert(ct->el_count == 1); |
| 304 | asn1print_constraint(ct->elements[0], flags); |
| 305 | printf(")"); |
| 306 | break; |
| 307 | case ACT_CT_WCOMP: |
| 308 | case ACT_CT_WCOMPS: |
| 309 | printf("???"); |
| 310 | break; |
| 311 | case ACT_CA_SET: symno++; |
| 312 | case ACT_CA_CRC: symno++; |
| 313 | case ACT_CA_CSV: symno++; |
| 314 | case ACT_CA_UNI: symno++; |
| 315 | case ACT_CA_INT: symno++; |
| 316 | case ACT_CA_EXC: |
| 317 | { |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 318 | char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 319 | "", "(" }; |
vlm | dc4d95a | 2004-09-05 10:38:50 +0000 | [diff] [blame] | 320 | unsigned int i; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 321 | for(i = 0; i < ct->el_count; i++) { |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 322 | enum asn1print_flags nflags = flags; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 323 | if(i) fputs(symtable[symno], stdout); |
| 324 | if(ct->type == ACT_CA_CRC) fputs("{", stdout); |
| 325 | asn1print_constraint(ct->elements[i], nflags); |
| 326 | if(ct->type == ACT_CA_CRC) fputs("}", stdout); |
| 327 | if(i+1 < ct->el_count |
| 328 | && ct->type == ACT_CA_SET) |
| 329 | fputs(")", stdout); |
| 330 | } |
| 331 | } |
| 332 | break; |
| 333 | case ACT_INVALID: |
| 334 | assert(ct->type != ACT_INVALID); |
| 335 | break; |
| 336 | } |
| 337 | |
| 338 | if(ct->type == ACT_CA_SET) |
| 339 | printf(")"); |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 345 | asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags flags) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 346 | if(pl) { |
| 347 | int i; |
| 348 | printf("{"); |
| 349 | for(i = 0; i < pl->params_count; i++) { |
| 350 | if(i) printf(", "); |
| 351 | if(pl->params[i].governor) { |
| 352 | asn1print_ref(pl->params[i].governor, flags); |
| 353 | printf(":"); |
| 354 | } |
| 355 | printf("%s", pl->params[i].argument); |
| 356 | } |
| 357 | printf("}"); |
| 358 | } |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static int |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 364 | asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 365 | if(wx) { |
| 366 | asn1p_wsyntx_chunk_t *wc; |
| 367 | printf(" WITH SYNTAX {"); |
| 368 | TQ_FOR(wc, &(wx->chunks), next) { |
| 369 | if(wc->ref) { |
| 370 | asn1print_ref(wc->ref, flags); |
| 371 | } else { |
| 372 | fwrite(wc->buf, 1, wc->len, stdout); |
| 373 | } |
| 374 | } |
| 375 | printf("}\n"); |
| 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static int |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 382 | asn1print_crange_value(asn1cnst_edge_t *edge, int as_char) { |
| 383 | switch(edge->type) { |
| 384 | case ARE_MIN: printf("MIN"); break; |
| 385 | case ARE_MAX: printf("MAX"); break; |
| 386 | case ARE_VALUE: |
| 387 | if(as_char) { |
| 388 | printf("\"%c\"", (unsigned char)edge->value); |
| 389 | } else { |
vlm | 47ae158 | 2004-09-24 21:01:43 +0000 | [diff] [blame] | 390 | printf("%" PRIdASN, edge->value); |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int |
vlm | eeca98f | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 397 | asn1print_constraint_explain_type(asn1p_expr_type_e expr_type, asn1p_constraint_t *ct, enum asn1p_constraint_type_e type, int strict_PER_visible) { |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 398 | asn1cnst_range_t *range; |
| 399 | int as_char = (type==ACT_CT_FROM); |
| 400 | int i; |
| 401 | |
vlm | eeca98f | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 402 | range = asn1constraint_compute_PER_range(expr_type, ct, type, |
| 403 | 0, 0, strict_PER_visible); |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 404 | if(!range) return -1; |
| 405 | |
vlm | eeca98f | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 406 | if(range->incompatible |
| 407 | || (strict_PER_visible && range->not_PER_visible)) { |
| 408 | asn1constraint_range_free(range); |
| 409 | return 0; |
| 410 | } |
| 411 | |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 412 | switch(type) { |
| 413 | case ACT_CT_FROM: printf("(FROM("); break; |
| 414 | case ACT_CT_SIZE: printf("(SIZE("); break; |
| 415 | default: printf("("); break; |
| 416 | } |
| 417 | for(i = -1; i < range->el_count; i++) { |
| 418 | asn1cnst_range_t *r; |
| 419 | if(i == -1) { |
| 420 | if(range->el_count) continue; |
| 421 | r = range; |
| 422 | } else { |
| 423 | r = range->elements[i]; |
| 424 | } |
| 425 | if(i > 0) { |
| 426 | printf(" | "); |
| 427 | } |
| 428 | asn1print_crange_value(&r->left, as_char); |
| 429 | if(r->left.type != r->right.type |
| 430 | || r->left.value != r->right.value) { |
| 431 | printf(".."); |
| 432 | asn1print_crange_value(&r->right, as_char); |
| 433 | } |
| 434 | } |
| 435 | if(range->extensible) |
| 436 | printf(",..."); |
| 437 | printf(type==ACT_EL_RANGE?")":"))"); |
| 438 | |
| 439 | if(range->empty_constraint) |
| 440 | printf(":Empty!"); |
| 441 | |
| 442 | asn1constraint_range_free(range); |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static int |
| 447 | asn1print_constraint_explain(asn1p_expr_type_e expr_type, |
vlm | eeca98f | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 448 | asn1p_constraint_t *ct, int s_PV) { |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 449 | |
vlm | eeca98f | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 450 | asn1print_constraint_explain_type(expr_type, ct, ACT_EL_RANGE, s_PV); |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 451 | printf(" "); |
vlm | eeca98f | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 452 | asn1print_constraint_explain_type(expr_type, ct, ACT_CT_SIZE, s_PV); |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 453 | printf(" "); |
vlm | eeca98f | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 454 | asn1print_constraint_explain_type(expr_type, ct, ACT_CT_FROM, s_PV); |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static int |
| 460 | asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level) { |
vlm | 00ad282 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 461 | int SEQ_OF = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 462 | |
| 463 | if(flags & APF_LINE_COMMENTS) |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 464 | INDENT("-- #line %d\n", tc->_lineno); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 465 | if(tc->Identifier) |
| 466 | INDENT("%s", tc->Identifier); |
| 467 | |
| 468 | if(tc->params) { |
| 469 | asn1print_params(tc->params, flags); |
| 470 | } |
| 471 | |
| 472 | if(tc->meta_type != AMT_VALUE |
vlm | da4df3f | 2004-08-25 02:27:47 +0000 | [diff] [blame] | 473 | && tc->meta_type != AMT_VALUESET |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 474 | && tc->expr_type != A1TC_EXTENSIBLE) { |
| 475 | if(level) { |
| 476 | if(tc->Identifier) |
| 477 | printf("\t"); |
| 478 | } else { |
| 479 | printf(" ::="); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | if(tc->tag.tag_class) { |
| 484 | printf(" "); |
| 485 | asn1print_tag(tc, flags); |
| 486 | } |
| 487 | |
| 488 | switch(tc->expr_type) { |
| 489 | case A1TC_EXTENSIBLE: |
| 490 | if(tc->value) { |
| 491 | printf("!"); |
| 492 | asn1print_value(tc->value, flags); |
| 493 | } |
| 494 | break; |
vlm | ddd6fd1 | 2004-08-22 03:08:51 +0000 | [diff] [blame] | 495 | case A1TC_COMPONENTS_OF: |
| 496 | SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */ |
| 497 | printf(" COMPONENTS OF"); |
| 498 | break; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 499 | case A1TC_REFERENCE: |
| 500 | case A1TC_UNIVERVAL: |
| 501 | case A1TC_PARAMETRIZED: |
| 502 | break; |
| 503 | case A1TC_CLASSDEF: |
| 504 | printf(" CLASS"); |
| 505 | break; |
| 506 | case A1TC_CLASSFIELD: |
| 507 | /* Nothing to print here */ |
| 508 | break; |
vlm | 00ad282 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 509 | case ASN_CONSTR_SET_OF: |
| 510 | case ASN_CONSTR_SEQUENCE_OF: |
| 511 | SEQ_OF = 1; |
| 512 | if(tc->expr_type == ASN_CONSTR_SET_OF) |
| 513 | printf(" SET"); |
| 514 | else |
| 515 | printf(" SEQUENCE"); |
| 516 | if(tc->constraints) { |
| 517 | printf(" "); |
| 518 | asn1print_constraint(tc->constraints, flags); |
| 519 | } |
| 520 | printf(" OF"); |
| 521 | break; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 522 | default: |
| 523 | { |
| 524 | char *p = ASN_EXPR_TYPE2STR(tc->expr_type); |
| 525 | printf(" %s", p?p:"<unknown type!>"); |
| 526 | } |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | if(tc->reference) { |
| 531 | printf(" "); |
| 532 | asn1print_ref(tc->reference, flags); |
| 533 | } |
| 534 | |
vlm | da4df3f | 2004-08-25 02:27:47 +0000 | [diff] [blame] | 535 | if(tc->meta_type == AMT_VALUESET) |
| 536 | printf(" ::="); |
| 537 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 538 | /* |
| 539 | * Display the descendants (children) of the current type. |
| 540 | */ |
vlm | da4df3f | 2004-08-25 02:27:47 +0000 | [diff] [blame] | 541 | if(TQ_FIRST(&(tc->members)) |
| 542 | || (tc->expr_type & ASN_CONSTR_MASK) |
| 543 | || tc->meta_type == AMT_VALUESET |
| 544 | || tc->meta_type == AMT_OBJECT |
| 545 | || tc->meta_type == AMT_OBJECTSET |
| 546 | ) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 547 | asn1p_expr_t *se; /* SubExpression */ |
vlm | ddd6fd1 | 2004-08-22 03:08:51 +0000 | [diff] [blame] | 548 | int put_braces = !SEQ_OF; /* Don't need 'em, if SET OF... */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 549 | |
vlm | da4df3f | 2004-08-25 02:27:47 +0000 | [diff] [blame] | 550 | if(put_braces) { |
| 551 | printf(" {"); |
| 552 | if(TQ_FIRST(&tc->members)) |
| 553 | printf("\n"); |
| 554 | else printf(" }"); |
| 555 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 556 | |
| 557 | TQ_FOR(se, &(tc->members), next) { |
| 558 | /* |
vlm | ddd6fd1 | 2004-08-22 03:08:51 +0000 | [diff] [blame] | 559 | * Print the expression as it were a stand-alone type. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 560 | */ |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 561 | asn1print_expr(asn, mod, se, flags, level + 1); |
vlm | 093f675 | 2004-09-15 11:44:55 +0000 | [diff] [blame] | 562 | if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) { |
| 563 | printf(" DEFAULT "); |
| 564 | asn1print_value(se->marker.default_value, flags); |
| 565 | } else if((se->marker.flags & EM_OPTIONAL) |
| 566 | == EM_OPTIONAL) { |
vlm | d82eb01 | 2004-09-10 08:21:27 +0000 | [diff] [blame] | 567 | printf(" OPTIONAL"); |
vlm | 093f675 | 2004-09-15 11:44:55 +0000 | [diff] [blame] | 568 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 569 | if(TQ_NEXT(se, next)) { |
| 570 | printf(","); |
| 571 | INDENT("\n"); |
| 572 | } |
| 573 | } |
| 574 | |
vlm | da4df3f | 2004-08-25 02:27:47 +0000 | [diff] [blame] | 575 | if(put_braces && TQ_FIRST(&tc->members)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 576 | printf("\n"); |
| 577 | INDENT("}"); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | if(tc->with_syntax) |
| 582 | asn1print_with_syntax(tc->with_syntax, flags); |
| 583 | |
vlm | 00ad282 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 584 | if(!SEQ_OF && tc->constraints) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 585 | printf(" "); |
| 586 | asn1print_constraint(tc->constraints, flags); |
| 587 | } |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 588 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 589 | if(tc->unique) { |
| 590 | printf(" UNIQUE"); |
| 591 | } |
| 592 | |
| 593 | if(tc->meta_type == AMT_VALUE |
| 594 | && tc->expr_type != A1TC_EXTENSIBLE) { |
vlm | dc4d95a | 2004-09-05 10:38:50 +0000 | [diff] [blame] | 595 | if(tc->expr_type == A1TC_UNIVERVAL) { |
vlm | 093f675 | 2004-09-15 11:44:55 +0000 | [diff] [blame] | 596 | if(tc->value) { |
| 597 | printf("("); |
| 598 | asn1print_value(tc->value, flags); |
| 599 | printf(")"); |
| 600 | } |
vlm | dc4d95a | 2004-09-05 10:38:50 +0000 | [diff] [blame] | 601 | } else { |
| 602 | printf(" ::= "); |
| 603 | asn1print_value(tc->value, flags); |
| 604 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 605 | } |
| 606 | |
vlm | ddd6fd1 | 2004-08-22 03:08:51 +0000 | [diff] [blame] | 607 | /* |
| 608 | * The following section exists entirely for debugging only. |
| 609 | */ |
vlm | 00ad282 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 610 | if(flags & APF_DEBUG_CONSTRAINTS |
| 611 | && tc->expr_type != A1TC_EXTENSIBLE) { |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 612 | asn1p_expr_t *top_parent; |
| 613 | |
| 614 | if(tc->combined_constraints) { |
| 615 | printf("\n-- Combined constraints: "); |
| 616 | asn1print_constraint(tc->combined_constraints, flags); |
| 617 | } |
| 618 | |
vlm | cbebb81 | 2004-09-22 16:05:13 +0000 | [diff] [blame] | 619 | top_parent = asn1f_find_terminal_type_ex(asn, tc); |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 620 | if(top_parent) { |
vlm | eeca98f | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 621 | printf("\n-- Practical constraints (%s): ", |
| 622 | top_parent->Identifier); |
| 623 | asn1print_constraint_explain(top_parent->expr_type, |
| 624 | tc->combined_constraints, 0); |
vlm | 00ad282 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 625 | printf("\n-- PER-visible constraints (%s): ", |
| 626 | top_parent->Identifier); |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 627 | asn1print_constraint_explain(top_parent->expr_type, |
vlm | eeca98f | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 628 | tc->combined_constraints, 1); |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 629 | } |
vlm | 00ad282 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 630 | printf("\n"); |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 631 | } |
| 632 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 633 | return 0; |
| 634 | } |
vlm | 4808c70 | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 635 | |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 636 | |
| 637 | static int |
| 638 | asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) { |
| 639 | asn1p_expr_t *se; |
| 640 | int expr_unordered = 0; |
vlm | ceb9319 | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 641 | int dont_involve_children = 0; |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 642 | |
| 643 | switch(expr->meta_type) { |
| 644 | case AMT_TYPE: |
| 645 | case AMT_TYPEREF: |
| 646 | break; |
| 647 | default: |
| 648 | if(expr->expr_type == A1TC_UNIVERVAL) |
| 649 | break; |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | if(!expr->Identifier) return 0; |
| 654 | |
| 655 | if(expr->expr_type == ASN_CONSTR_CHOICE |
| 656 | || expr->expr_type == ASN_CONSTR_SEQUENCE_OF |
| 657 | || expr->expr_type == ASN_CONSTR_SET_OF |
| 658 | || expr->expr_type == ASN_CONSTR_SET |
| 659 | || expr->expr_type == ASN_BASIC_INTEGER |
| 660 | || expr->expr_type == ASN_BASIC_ENUMERATED) { |
| 661 | expr_unordered = 1; |
| 662 | } |
| 663 | |
| 664 | if(flags & APF_LINE_COMMENTS) |
| 665 | INDENT("<!-- #line %d -->\n", expr->_lineno); |
| 666 | INDENT("<!ELEMENT %s", expr->Identifier); |
| 667 | |
| 668 | if(expr->expr_type == A1TC_REFERENCE) { |
| 669 | se = asn1f_find_terminal_type_ex(asn, expr); |
| 670 | if(!se) { |
vlm | ceb9319 | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 671 | printf(" (ANY)"); |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 672 | return 0; |
| 673 | } |
| 674 | expr = se; |
vlm | ceb9319 | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 675 | dont_involve_children = 1; |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | if(TQ_FIRST(&expr->members)) { |
| 679 | int extensible = 0; |
| 680 | printf(" ("); |
| 681 | TQ_FOR(se, &(expr->members), next) { |
| 682 | if(se->expr_type == A1TC_EXTENSIBLE) { |
| 683 | extensible = 1; |
| 684 | continue; |
| 685 | } else if(!se->Identifier |
| 686 | && se->expr_type == A1TC_REFERENCE) { |
| 687 | asn1print_ref(se->reference, flags); |
| 688 | } else if(se->Identifier) { |
| 689 | printf("%s", se->Identifier); |
| 690 | } else { |
| 691 | printf("ANY"); |
| 692 | } |
| 693 | if(expr->expr_type != ASN_CONSTR_SET |
| 694 | && expr->expr_type != ASN_CONSTR_CHOICE |
| 695 | && expr->expr_type != ASN_BASIC_INTEGER |
| 696 | && expr->expr_type != ASN_BASIC_ENUMERATED) { |
| 697 | if(expr_unordered) |
| 698 | printf("*"); |
| 699 | else if(se->marker.flags) |
| 700 | printf("?"); |
| 701 | } |
| 702 | if(TQ_NEXT(se, next) |
| 703 | && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) { |
| 704 | printf(expr_unordered?"|":", "); |
| 705 | } |
| 706 | } |
| 707 | if(extensible) { |
| 708 | printf(expr_unordered?"|":", "); |
| 709 | printf("ANY"); |
| 710 | if(expr->expr_type != ASN_CONSTR_SET |
| 711 | && expr->expr_type != ASN_CONSTR_CHOICE |
| 712 | && expr->expr_type != ASN_BASIC_INTEGER |
| 713 | && expr->expr_type != ASN_BASIC_ENUMERATED) |
| 714 | printf("*"); |
| 715 | } |
| 716 | |
| 717 | printf(")"); |
| 718 | if(expr->expr_type == ASN_CONSTR_SET) |
| 719 | printf("*"); |
| 720 | |
vlm | ceb9319 | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 721 | } else switch(expr->expr_type) { |
| 722 | case ASN_BASIC_BOOLEAN: |
| 723 | printf("(true|false)"); |
| 724 | break; |
| 725 | case ASN_CONSTR_CHOICE: |
| 726 | case ASN_CONSTR_SET: |
| 727 | case ASN_CONSTR_SET_OF: |
| 728 | case ASN_CONSTR_SEQUENCE: |
| 729 | case ASN_CONSTR_SEQUENCE_OF: |
| 730 | case ASN_BASIC_NULL: |
| 731 | case A1TC_UNIVERVAL: |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 732 | printf(" EMPTY"); |
vlm | ceb9319 | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 733 | break; |
| 734 | case ASN_TYPE_ANY: |
| 735 | printf(" ANY"); |
| 736 | break; |
| 737 | case ASN_BASIC_BIT_STRING: |
| 738 | case ASN_BASIC_OCTET_STRING: |
| 739 | case ASN_BASIC_OBJECT_IDENTIFIER: |
| 740 | case ASN_BASIC_RELATIVE_OID: |
vlm | 40a69bf | 2004-10-15 06:01:54 +0000 | [diff] [blame] | 741 | case ASN_BASIC_INTEGER: |
vlm | ceb9319 | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 742 | case ASN_BASIC_UTCTime: |
| 743 | case ASN_BASIC_GeneralizedTime: |
vlm | ceb9319 | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 744 | case ASN_STRING_NumericString: |
| 745 | case ASN_STRING_PrintableString: |
vlm | 40a69bf | 2004-10-15 06:01:54 +0000 | [diff] [blame] | 746 | printf(" (#PCDATA)"); |
vlm | ceb9319 | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 747 | break; |
| 748 | case ASN_STRING_VisibleString: |
| 749 | case ASN_STRING_ISO646String: |
| 750 | /* Entity references, but not XML elements may be present */ |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 751 | printf(" (#PCDATA)"); |
vlm | ceb9319 | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 752 | break; |
| 753 | case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */ |
| 754 | case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */ |
| 755 | default: |
| 756 | /* |
| 757 | * XML elements are allowed. |
| 758 | * For example, a UTF8String may contain "<bel/>". |
| 759 | */ |
| 760 | printf(" ANY"); |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 761 | } |
| 762 | printf(">\n"); |
| 763 | |
| 764 | /* |
| 765 | * Display the descendants (children) of the current type. |
| 766 | */ |
vlm | ceb9319 | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 767 | if(!dont_involve_children) { |
| 768 | TQ_FOR(se, &(expr->members), next) { |
| 769 | if(se->expr_type == A1TC_EXTENSIBLE) continue; |
| 770 | asn1print_expr_dtd(asn, mod, se, flags, level + 1); |
| 771 | } |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | return 0; |
| 775 | } |