Lev Walkin | f15320b | 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> |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 7 | #include <asn1fix_export.h> |
Lev Walkin | 9095ada | 2004-08-18 05:41:05 +0000 | [diff] [blame] | 8 | #include <asn1fix_crange.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 9 | |
| 10 | #include "asn1print.h" |
| 11 | |
| 12 | #define INDENT(fmt, args...) do { \ |
Lev Walkin | f4069d2 | 2005-02-15 07:06:05 +0000 | [diff] [blame] | 13 | if(!(flags & APF_NOINDENT)) { \ |
| 14 | int __i = level; \ |
| 15 | while(__i--) printf(" "); \ |
| 16 | } \ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 17 | printf(fmt, ##args); \ |
| 18 | } while(0) |
| 19 | |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 20 | static int asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags); |
Lev Walkin | d6db802 | 2005-03-18 03:53:05 +0000 | [diff] [blame] | 21 | static int asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags); |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 22 | static int asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags); |
| 23 | static int asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags); |
| 24 | static int asn1print_params(asn1p_paramlist_t *pl,enum asn1print_flags flags); |
| 25 | static int asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags); |
| 26 | static int asn1print_constraint(asn1p_constraint_t *, enum asn1print_flags); |
| 27 | static int asn1print_value(asn1p_value_t *val, enum asn1print_flags flags); |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 28 | static int asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level); |
| 29 | static int asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * Print the contents of the parsed ASN tree. |
| 33 | */ |
| 34 | int |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 35 | asn1print(asn1p_t *asn, enum asn1print_flags flags) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 36 | asn1p_module_t *mod; |
Lev Walkin | c70c2ef | 2004-09-30 06:38:21 +0000 | [diff] [blame] | 37 | int modno = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | |
| 39 | if(asn == NULL) { |
| 40 | errno = EINVAL; |
| 41 | return -1; |
| 42 | } |
| 43 | |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 44 | if(flags & APF_PRINT_XML_DTD) |
| 45 | printf("<!-- XML DTD generated by asn1c-" VERSION " -->\n\n"); |
| 46 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | TQ_FOR(mod, &(asn->modules), mod_next) { |
Lev Walkin | 6b3ff54 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 48 | if(mod->_tags & MT_STANDARD_MODULE) |
| 49 | return 0; /* Ignore modules imported from skeletons */ |
Lev Walkin | c70c2ef | 2004-09-30 06:38:21 +0000 | [diff] [blame] | 50 | if(modno++) printf("\n"); |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 51 | asn1print_module(asn, mod, flags); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Lev Walkin | 112d69e | 2005-03-09 20:52:15 +0000 | [diff] [blame] | 54 | if(flags & APF_PRINT_XML_DTD) { |
| 55 | /* Values for BOOLEAN */ |
| 56 | printf("<!ELEMENT true EMPTY>\n"); |
| 57 | printf("<!ELEMENT false EMPTY>\n"); |
| 58 | } |
| 59 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 64 | asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 65 | asn1p_expr_t *tc; |
| 66 | |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 67 | if(flags & APF_PRINT_XML_DTD) |
| 68 | printf("<!-- ASN.1 module\n"); |
| 69 | |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 70 | printf("%s ", mod->ModuleName); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 71 | if(mod->module_oid) { |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 72 | asn1print_oid(strlen(mod->ModuleName), mod->module_oid, flags); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 73 | printf("\n"); |
| 74 | } |
| 75 | |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 76 | if(flags & APF_PRINT_XML_DTD) { |
| 77 | if(mod->source_file_name |
| 78 | && strcmp(mod->source_file_name, "-")) |
| 79 | printf("found in %s", mod->source_file_name); |
| 80 | printf(" -->\n\n"); |
| 81 | |
| 82 | TQ_FOR(tc, &(mod->members), next) { |
| 83 | asn1print_expr_dtd(asn, mod, tc, flags, 0); |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 89 | printf("DEFINITIONS"); |
| 90 | |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 91 | if(mod->module_flags & MSF_TAG_INSTRUCTIONS) |
| 92 | printf(" TAG INSTRUCTIONS"); |
| 93 | if(mod->module_flags & MSF_XER_INSTRUCTIONS) |
| 94 | printf(" XER INSTRUCTIONS"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 95 | if(mod->module_flags & MSF_EXPLICIT_TAGS) |
| 96 | printf(" EXPLICIT TAGS"); |
| 97 | if(mod->module_flags & MSF_IMPLICIT_TAGS) |
| 98 | printf(" IMPLICIT TAGS"); |
| 99 | if(mod->module_flags & MSF_AUTOMATIC_TAGS) |
| 100 | printf(" AUTOMATIC TAGS"); |
| 101 | if(mod->module_flags & MSF_EXTENSIBILITY_IMPLIED) |
| 102 | printf(" EXTENSIBILITY IMPLIED"); |
| 103 | |
| 104 | printf(" ::=\n"); |
| 105 | printf("BEGIN\n\n"); |
| 106 | |
| 107 | TQ_FOR(tc, &(mod->members), next) { |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 108 | asn1print_expr(asn, mod, tc, flags, 0); |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 109 | if(flags & APF_PRINT_CONSTRAINTS) |
Lev Walkin | b1ef396 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 110 | printf("\n"); |
| 111 | else |
| 112 | printf("\n\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | printf("END\n"); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static int |
Lev Walkin | d6db802 | 2005-03-18 03:53:05 +0000 | [diff] [blame] | 121 | asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags) { |
| 122 | size_t accum = prior_len; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 123 | int ac; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 124 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 125 | (void)flags; /* Unused argument */ |
| 126 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | printf("{"); |
| 128 | for(ac = 0; ac < oid->arcs_count; ac++) { |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 129 | const char *arcname = oid->arcs[ac].name; |
| 130 | |
Lev Walkin | 5b7eeaf | 2005-03-28 17:52:43 +0000 | [diff] [blame] | 131 | if(accum + strlen(arcname ? arcname : "") > 72) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 132 | printf("\n\t"); |
Lev Walkin | 5b7eeaf | 2005-03-28 17:52:43 +0000 | [diff] [blame] | 133 | accum = 8; |
Lev Walkin | d6db802 | 2005-03-18 03:53:05 +0000 | [diff] [blame] | 134 | } else { |
| 135 | accum += printf(" "); |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 136 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 137 | |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 138 | if(arcname) { |
Lev Walkin | d6db802 | 2005-03-18 03:53:05 +0000 | [diff] [blame] | 139 | accum += printf("%s", arcname); |
Lev Walkin | 0056aec | 2004-09-05 10:38:50 +0000 | [diff] [blame] | 140 | if(oid->arcs[ac].number >= 0) { |
Lev Walkin | d6db802 | 2005-03-18 03:53:05 +0000 | [diff] [blame] | 141 | accum += printf("(%" PRIdASN ")", |
| 142 | oid->arcs[ac].number); |
Lev Walkin | 0056aec | 2004-09-05 10:38:50 +0000 | [diff] [blame] | 143 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 144 | } else { |
Lev Walkin | d6db802 | 2005-03-18 03:53:05 +0000 | [diff] [blame] | 145 | accum += printf("%" PRIdASN, oid->arcs[ac].number); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 146 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 147 | } |
| 148 | printf(" }"); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 154 | asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 155 | int cc; |
| 156 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 157 | (void)flags; /* Unused argument */ |
| 158 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 159 | for(cc = 0; cc < ref->comp_count; cc++) { |
| 160 | if(cc) printf("."); |
| 161 | printf("%s", ref->components[cc].name); |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 168 | asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 169 | struct asn1p_type_tag_s *tag = &tc->tag; |
| 170 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 171 | (void)flags; /* Unused argument */ |
| 172 | |
Lev Walkin | 7116096 | 2005-06-02 05:21:53 +0000 | [diff] [blame] | 173 | printf("%s", asn1p_tag2string(tag, 0)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 179 | asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 180 | |
| 181 | if(val == NULL) |
| 182 | return 0; |
| 183 | |
| 184 | switch(val->type) { |
| 185 | case ATV_NOVALUE: |
| 186 | break; |
Lev Walkin | b1e0708 | 2004-09-15 11:44:55 +0000 | [diff] [blame] | 187 | case ATV_NULL: |
| 188 | printf("NULL"); |
| 189 | return 0; |
| 190 | case ATV_REAL: |
| 191 | printf("%f", val->value.v_double); |
| 192 | return 0; |
Lev Walkin | a9532f4 | 2006-09-17 04:52:50 +0000 | [diff] [blame] | 193 | case ATV_TYPE: |
| 194 | asn1print_expr(val->value.v_type->module->asn1p, |
| 195 | val->value.v_type->module, |
| 196 | val->value.v_type, flags, 0); |
| 197 | return 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 198 | case ATV_INTEGER: |
Lev Walkin | 33c16ba | 2004-09-24 21:01:43 +0000 | [diff] [blame] | 199 | printf("%" PRIdASN, val->value.v_integer); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 200 | return 0; |
| 201 | case ATV_MIN: printf("MIN"); return 0; |
| 202 | case ATV_MAX: printf("MAX"); return 0; |
| 203 | case ATV_FALSE: printf("FALSE"); return 0; |
| 204 | case ATV_TRUE: printf("TRUE"); return 0; |
Lev Walkin | 1e448d3 | 2005-03-24 14:26:38 +0000 | [diff] [blame] | 205 | case ATV_TUPLE: |
| 206 | printf("{%d, %d}", |
| 207 | (int)(val->value.v_integer >> 4), |
| 208 | (int)(val->value.v_integer & 0x0f)); |
| 209 | return 0; |
| 210 | case ATV_QUADRUPLE: |
| 211 | printf("{%d, %d, %d, %d}", |
| 212 | (int)((val->value.v_integer >> 24) & 0xff), |
| 213 | (int)((val->value.v_integer >> 16) & 0xff), |
| 214 | (int)((val->value.v_integer >> 8) & 0xff), |
| 215 | (int)((val->value.v_integer >> 0) & 0xff) |
| 216 | ); |
| 217 | return 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 218 | case ATV_STRING: |
| 219 | { |
Lev Walkin | 84fbd72 | 2005-06-15 18:41:50 +0000 | [diff] [blame] | 220 | char *p = (char *)val->value.string.buf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 221 | putchar('"'); |
| 222 | if(strchr(p, '"')) { |
| 223 | /* Mask quotes */ |
| 224 | for(; *p; p++) { |
| 225 | if(*p == '"') |
| 226 | putchar(*p); |
| 227 | putchar(*p); |
| 228 | } |
| 229 | } else { |
| 230 | fputs(p, stdout); |
| 231 | } |
| 232 | putchar('"'); |
| 233 | } |
| 234 | return 0; |
| 235 | case ATV_UNPARSED: |
Lev Walkin | 84fbd72 | 2005-06-15 18:41:50 +0000 | [diff] [blame] | 236 | fputs((char *)val->value.string.buf, stdout); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 237 | return 0; |
| 238 | case ATV_BITVECTOR: |
| 239 | { |
| 240 | uint8_t *bitvector; |
| 241 | int bits; |
| 242 | int i; |
| 243 | |
| 244 | bitvector = val->value.binary_vector.bits; |
| 245 | bits = val->value.binary_vector.size_in_bits; |
| 246 | |
| 247 | printf("'"); |
| 248 | if(bits%8) { |
| 249 | for(i = 0; i < bits; i++) { |
| 250 | uint8_t uc; |
| 251 | uc = bitvector[i>>3]; |
| 252 | putchar(((uc >> (7-(i%8)))&1)?'1':'0'); |
| 253 | } |
| 254 | printf("'B"); |
| 255 | } else { |
| 256 | char hextable[16] = "0123456789ABCDEF"; |
| 257 | for(i = 0; i < (bits>>3); i++) { |
| 258 | putchar(hextable[bitvector[i] >> 4]); |
| 259 | putchar(hextable[bitvector[i] & 0x0f]); |
| 260 | } |
| 261 | printf("'H"); |
| 262 | } |
Lev Walkin | 043af0d | 2005-02-24 21:07:35 +0000 | [diff] [blame] | 263 | return 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 264 | } |
Lev Walkin | b1e0708 | 2004-09-15 11:44:55 +0000 | [diff] [blame] | 265 | case ATV_REFERENCED: |
| 266 | return asn1print_ref(val->value.reference, flags); |
Lev Walkin | 5045dfa | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 267 | case ATV_VALUESET: |
| 268 | return asn1print_constraint(val->value.constraint, flags); |
Lev Walkin | b1e0708 | 2004-09-15 11:44:55 +0000 | [diff] [blame] | 269 | case ATV_CHOICE_IDENTIFIER: |
| 270 | printf("%s: ", val->value.choice_identifier.identifier); |
| 271 | return asn1print_value(val->value.choice_identifier.value, flags); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | assert(val->type || !"Unknown"); |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static int |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 280 | asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 281 | int symno = 0; |
| 282 | |
| 283 | if(ct == 0) return 0; |
| 284 | |
| 285 | if(ct->type == ACT_CA_SET) |
| 286 | printf("("); |
| 287 | |
| 288 | switch(ct->type) { |
Lev Walkin | ff7dd14 | 2005-03-20 12:58:00 +0000 | [diff] [blame] | 289 | case ACT_EL_TYPE: |
Lev Walkin | 5045dfa | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 290 | asn1print_value(ct->containedSubtype, flags); |
Lev Walkin | ff7dd14 | 2005-03-20 12:58:00 +0000 | [diff] [blame] | 291 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 292 | case ACT_EL_VALUE: |
| 293 | asn1print_value(ct->value, flags); |
| 294 | break; |
| 295 | case ACT_EL_RANGE: |
| 296 | case ACT_EL_LLRANGE: |
| 297 | case ACT_EL_RLRANGE: |
| 298 | case ACT_EL_ULRANGE: |
| 299 | asn1print_value(ct->range_start, flags); |
| 300 | switch(ct->type) { |
| 301 | case ACT_EL_RANGE: printf(".."); break; |
| 302 | case ACT_EL_LLRANGE: printf("<.."); break; |
| 303 | case ACT_EL_RLRANGE: printf("..<"); break; |
| 304 | case ACT_EL_ULRANGE: printf("<..<"); break; |
| 305 | default: printf("?..?"); break; |
| 306 | } |
| 307 | asn1print_value(ct->range_stop, flags); |
| 308 | break; |
| 309 | case ACT_EL_EXT: |
| 310 | printf("..."); |
| 311 | break; |
| 312 | case ACT_CT_SIZE: |
| 313 | case ACT_CT_FROM: |
| 314 | switch(ct->type) { |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 315 | case ACT_CT_SIZE: printf("SIZE("); break; |
| 316 | case ACT_CT_FROM: printf("FROM("); break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 317 | default: printf("??? ("); break; |
| 318 | } |
| 319 | assert(ct->el_count != 0); |
| 320 | assert(ct->el_count == 1); |
| 321 | asn1print_constraint(ct->elements[0], flags); |
| 322 | printf(")"); |
| 323 | break; |
| 324 | case ACT_CT_WCOMP: |
Lev Walkin | e596bf0 | 2005-03-28 15:01:27 +0000 | [diff] [blame] | 325 | assert(ct->el_count != 0); |
| 326 | assert(ct->el_count == 1); |
| 327 | printf("WITH COMPONENT ("); |
| 328 | asn1print_constraint(ct->elements[0], flags); |
| 329 | printf(")"); |
Lev Walkin | 8638f0d | 2005-03-28 07:50:06 +0000 | [diff] [blame] | 330 | break; |
Lev Walkin | e596bf0 | 2005-03-28 15:01:27 +0000 | [diff] [blame] | 331 | case ACT_CT_WCOMPS: { |
| 332 | unsigned int i; |
| 333 | printf("WITH COMPONENTS { "); |
| 334 | for(i = 0; i < ct->el_count; i++) { |
| 335 | asn1p_constraint_t *cel = ct->elements[i]; |
| 336 | if(i) printf(", "); |
| 337 | fwrite(cel->value->value.string.buf, |
| 338 | 1, cel->value->value.string.size, |
| 339 | stdout); |
| 340 | if(cel->el_count) { |
| 341 | assert(cel->el_count == 1); |
| 342 | printf(" "); |
| 343 | asn1print_constraint(cel->elements[0], |
| 344 | flags); |
| 345 | } |
| 346 | switch(cel->presence) { |
| 347 | case ACPRES_DEFAULT: break; |
| 348 | case ACPRES_PRESENT: printf(" PRESENT"); break; |
| 349 | case ACPRES_ABSENT: printf(" ABSENT"); break; |
| 350 | case ACPRES_OPTIONAL: printf(" OPTIONAL");break; |
| 351 | } |
| 352 | } |
| 353 | printf(" }"); |
| 354 | } |
Lev Walkin | 8638f0d | 2005-03-28 07:50:06 +0000 | [diff] [blame] | 355 | break; |
Lev Walkin | 1893ddf | 2005-03-20 14:28:32 +0000 | [diff] [blame] | 356 | case ACT_CT_CTDBY: |
Lev Walkin | c6ab03c | 2006-10-21 05:54:49 +0000 | [diff] [blame] | 357 | printf("CONSTRAINED BY "); |
Lev Walkin | 1893ddf | 2005-03-20 14:28:32 +0000 | [diff] [blame] | 358 | assert(ct->value->type == ATV_UNPARSED); |
| 359 | fwrite(ct->value->value.string.buf, |
| 360 | 1, ct->value->value.string.size, stdout); |
Lev Walkin | a9532f4 | 2006-09-17 04:52:50 +0000 | [diff] [blame] | 361 | break; |
| 362 | case ACT_CT_CTNG: |
Lev Walkin | c6ab03c | 2006-10-21 05:54:49 +0000 | [diff] [blame] | 363 | printf("CONTAINING "); |
Lev Walkin | a9532f4 | 2006-09-17 04:52:50 +0000 | [diff] [blame] | 364 | asn1print_expr(ct->value->value.v_type->module->asn1p, |
| 365 | ct->value->value.v_type->module, |
| 366 | ct->value->value.v_type, |
| 367 | flags, 1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 368 | break; |
Lev Walkin | 5c541f1 | 2006-10-18 18:40:14 +0000 | [diff] [blame] | 369 | case ACT_CT_PATTERN: |
Lev Walkin | c6ab03c | 2006-10-21 05:54:49 +0000 | [diff] [blame] | 370 | printf("PATTERN "); |
Lev Walkin | 5c541f1 | 2006-10-18 18:40:14 +0000 | [diff] [blame] | 371 | asn1print_value(ct->value, flags); |
Lev Walkin | 5c541f1 | 2006-10-18 18:40:14 +0000 | [diff] [blame] | 372 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 373 | case ACT_CA_SET: symno++; |
| 374 | case ACT_CA_CRC: symno++; |
| 375 | case ACT_CA_CSV: symno++; |
| 376 | case ACT_CA_UNI: symno++; |
| 377 | case ACT_CA_INT: symno++; |
| 378 | case ACT_CA_EXC: |
| 379 | { |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 380 | char *symtable[] = { " EXCEPT ", " ^ ", " | ", ",", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 381 | "", "(" }; |
Lev Walkin | 0056aec | 2004-09-05 10:38:50 +0000 | [diff] [blame] | 382 | unsigned int i; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 383 | for(i = 0; i < ct->el_count; i++) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 384 | if(i) fputs(symtable[symno], stdout); |
| 385 | if(ct->type == ACT_CA_CRC) fputs("{", stdout); |
Lev Walkin | 1e448d3 | 2005-03-24 14:26:38 +0000 | [diff] [blame] | 386 | asn1print_constraint(ct->elements[i], flags); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 387 | if(ct->type == ACT_CA_CRC) fputs("}", stdout); |
| 388 | if(i+1 < ct->el_count |
| 389 | && ct->type == ACT_CA_SET) |
| 390 | fputs(")", stdout); |
| 391 | } |
| 392 | } |
| 393 | break; |
Lev Walkin | 1e448d3 | 2005-03-24 14:26:38 +0000 | [diff] [blame] | 394 | case ACT_CA_AEX: |
| 395 | assert(ct->el_count == 1); |
| 396 | printf("ALL EXCEPT "); |
| 397 | asn1print_constraint(ct->elements[0], flags); |
| 398 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 399 | case ACT_INVALID: |
| 400 | assert(ct->type != ACT_INVALID); |
| 401 | break; |
| 402 | } |
| 403 | |
| 404 | if(ct->type == ACT_CA_SET) |
| 405 | printf(")"); |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static int |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 411 | asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags flags) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 412 | if(pl) { |
| 413 | int i; |
| 414 | printf("{"); |
| 415 | for(i = 0; i < pl->params_count; i++) { |
| 416 | if(i) printf(", "); |
| 417 | if(pl->params[i].governor) { |
| 418 | asn1print_ref(pl->params[i].governor, flags); |
| 419 | printf(":"); |
| 420 | } |
| 421 | printf("%s", pl->params[i].argument); |
| 422 | } |
| 423 | printf("}"); |
| 424 | } |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static int |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 430 | asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 431 | if(wx) { |
| 432 | asn1p_wsyntx_chunk_t *wc; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 433 | TQ_FOR(wc, &(wx->chunks), next) { |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 434 | switch(wc->type) { |
| 435 | case WC_LITERAL: |
Lev Walkin | 57074f1 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 436 | case WC_WHITESPACE: |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 437 | case WC_FIELD: |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 438 | printf("%s", wc->content.token); |
| 439 | break; |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 440 | case WC_OPTIONALGROUP: |
| 441 | printf("["); |
| 442 | asn1print_with_syntax(wc->content.syntax,flags); |
| 443 | printf("]"); |
| 444 | break; |
| 445 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 446 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | static int |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 453 | asn1print_crange_value(asn1cnst_edge_t *edge, int as_char) { |
| 454 | switch(edge->type) { |
| 455 | case ARE_MIN: printf("MIN"); break; |
| 456 | case ARE_MAX: printf("MAX"); break; |
| 457 | case ARE_VALUE: |
| 458 | if(as_char) { |
| 459 | printf("\"%c\"", (unsigned char)edge->value); |
| 460 | } else { |
Lev Walkin | 33c16ba | 2004-09-24 21:01:43 +0000 | [diff] [blame] | 461 | printf("%" PRIdASN, edge->value); |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int |
Lev Walkin | e8e87f1 | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 468 | asn1print_constraint_explain_type(asn1p_expr_type_e expr_type, asn1p_constraint_t *ct, enum asn1p_constraint_type_e type, int strict_PER_visible) { |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 469 | asn1cnst_range_t *range; |
| 470 | int as_char = (type==ACT_CT_FROM); |
| 471 | int i; |
| 472 | |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 473 | range = asn1constraint_compute_PER_range(expr_type, ct, type, 0, 0, |
| 474 | strict_PER_visible ? CPR_strict_PER_visibility : 0); |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 475 | if(!range) return -1; |
| 476 | |
Lev Walkin | e8e87f1 | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 477 | if(range->incompatible |
| 478 | || (strict_PER_visible && range->not_PER_visible)) { |
| 479 | asn1constraint_range_free(range); |
| 480 | return 0; |
| 481 | } |
| 482 | |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 483 | switch(type) { |
| 484 | case ACT_CT_FROM: printf("(FROM("); break; |
| 485 | case ACT_CT_SIZE: printf("(SIZE("); break; |
| 486 | default: printf("("); break; |
| 487 | } |
| 488 | for(i = -1; i < range->el_count; i++) { |
| 489 | asn1cnst_range_t *r; |
| 490 | if(i == -1) { |
| 491 | if(range->el_count) continue; |
| 492 | r = range; |
| 493 | } else { |
| 494 | r = range->elements[i]; |
| 495 | } |
| 496 | if(i > 0) { |
| 497 | printf(" | "); |
| 498 | } |
| 499 | asn1print_crange_value(&r->left, as_char); |
| 500 | if(r->left.type != r->right.type |
| 501 | || r->left.value != r->right.value) { |
| 502 | printf(".."); |
| 503 | asn1print_crange_value(&r->right, as_char); |
| 504 | } |
| 505 | } |
| 506 | if(range->extensible) |
| 507 | printf(",..."); |
| 508 | printf(type==ACT_EL_RANGE?")":"))"); |
| 509 | |
| 510 | if(range->empty_constraint) |
| 511 | printf(":Empty!"); |
| 512 | |
| 513 | asn1constraint_range_free(range); |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static int |
| 518 | asn1print_constraint_explain(asn1p_expr_type_e expr_type, |
Lev Walkin | e8e87f1 | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 519 | asn1p_constraint_t *ct, int s_PV) { |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 520 | |
Lev Walkin | e8e87f1 | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 521 | asn1print_constraint_explain_type(expr_type, ct, ACT_EL_RANGE, s_PV); |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 522 | printf(" "); |
Lev Walkin | e8e87f1 | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 523 | asn1print_constraint_explain_type(expr_type, ct, ACT_CT_SIZE, s_PV); |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 524 | printf(" "); |
Lev Walkin | e8e87f1 | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 525 | asn1print_constraint_explain_type(expr_type, ct, ACT_CT_FROM, s_PV); |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static int |
| 531 | asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level) { |
Lev Walkin | b1ef396 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 532 | int SEQ_OF = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 533 | |
Lev Walkin | f4069d2 | 2005-02-15 07:06:05 +0000 | [diff] [blame] | 534 | if(flags & APF_LINE_COMMENTS && !(flags & APF_NOINDENT)) |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 535 | INDENT("-- #line %d\n", tc->_lineno); |
Lev Walkin | ef62540 | 2005-09-05 05:17:57 +0000 | [diff] [blame] | 536 | |
| 537 | /* Reconstruct compiler directive information */ |
| 538 | if((tc->marker.flags & EM_INDIRECT) |
| 539 | && (tc->marker.flags & EM_OMITABLE) != EM_OMITABLE) { |
| 540 | if((flags & APF_NOINDENT)) |
| 541 | printf(" --<ASN1C.RepresentAsPointer>-- "); |
| 542 | else |
| 543 | INDENT("--<ASN1C.RepresentAsPointer>--\n"); |
| 544 | } |
| 545 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 546 | if(tc->Identifier |
| 547 | && (!(tc->meta_type == AMT_VALUE && tc->expr_type == A1TC_REFERENCE) |
| 548 | || level == 0)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 549 | INDENT("%s", tc->Identifier); |
| 550 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 551 | if(tc->lhs_params) { |
| 552 | asn1print_params(tc->lhs_params, flags); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | if(tc->meta_type != AMT_VALUE |
Lev Walkin | c74ea22 | 2004-08-25 02:27:47 +0000 | [diff] [blame] | 556 | && tc->meta_type != AMT_VALUESET |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 557 | && tc->expr_type != A1TC_EXTENSIBLE) { |
| 558 | if(level) { |
Lev Walkin | f4069d2 | 2005-02-15 07:06:05 +0000 | [diff] [blame] | 559 | if(tc->Identifier && !(flags & APF_NOINDENT)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 560 | printf("\t"); |
| 561 | } else { |
| 562 | printf(" ::="); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | if(tc->tag.tag_class) { |
| 567 | printf(" "); |
| 568 | asn1print_tag(tc, flags); |
| 569 | } |
| 570 | |
| 571 | switch(tc->expr_type) { |
| 572 | case A1TC_EXTENSIBLE: |
| 573 | if(tc->value) { |
| 574 | printf("!"); |
| 575 | asn1print_value(tc->value, flags); |
| 576 | } |
| 577 | break; |
Lev Walkin | fd151ce | 2004-08-22 03:08:51 +0000 | [diff] [blame] | 578 | case A1TC_COMPONENTS_OF: |
| 579 | SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */ |
| 580 | printf(" COMPONENTS OF"); |
| 581 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 582 | case A1TC_REFERENCE: |
| 583 | case A1TC_UNIVERVAL: |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 584 | break; |
| 585 | case A1TC_CLASSDEF: |
| 586 | printf(" CLASS"); |
| 587 | break; |
Lev Walkin | 9c2285a | 2006-03-09 08:49:26 +0000 | [diff] [blame] | 588 | case A1TC_CLASSFIELD_TFS ... A1TC_CLASSFIELD_OSFS: |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 589 | /* Nothing to print here */ |
| 590 | break; |
Lev Walkin | b1ef396 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 591 | case ASN_CONSTR_SET_OF: |
| 592 | case ASN_CONSTR_SEQUENCE_OF: |
| 593 | SEQ_OF = 1; |
| 594 | if(tc->expr_type == ASN_CONSTR_SET_OF) |
| 595 | printf(" SET"); |
| 596 | else |
| 597 | printf(" SEQUENCE"); |
| 598 | if(tc->constraints) { |
| 599 | printf(" "); |
| 600 | asn1print_constraint(tc->constraints, flags); |
| 601 | } |
| 602 | printf(" OF"); |
| 603 | break; |
Lev Walkin | 5045dfa | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 604 | case A1TC_VALUESET: |
| 605 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 606 | default: |
| 607 | { |
| 608 | char *p = ASN_EXPR_TYPE2STR(tc->expr_type); |
| 609 | printf(" %s", p?p:"<unknown type!>"); |
| 610 | } |
| 611 | break; |
| 612 | } |
| 613 | |
Lev Walkin | b9d0ae3 | 2005-06-02 04:06:24 +0000 | [diff] [blame] | 614 | /* |
| 615 | * Put the name of the referred type. |
| 616 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 617 | if(tc->reference) { |
Lev Walkin | b9d0ae3 | 2005-06-02 04:06:24 +0000 | [diff] [blame] | 618 | printf(" "); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 619 | asn1print_ref(tc->reference, flags); |
| 620 | } |
| 621 | |
Lev Walkin | 5045dfa | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 622 | if(tc->meta_type == AMT_VALUESET && level == 0) |
Lev Walkin | c74ea22 | 2004-08-25 02:27:47 +0000 | [diff] [blame] | 623 | printf(" ::="); |
| 624 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 625 | /* |
| 626 | * Display the descendants (children) of the current type. |
| 627 | */ |
Lev Walkin | c74ea22 | 2004-08-25 02:27:47 +0000 | [diff] [blame] | 628 | if(TQ_FIRST(&(tc->members)) |
| 629 | || (tc->expr_type & ASN_CONSTR_MASK) |
Lev Walkin | c74ea22 | 2004-08-25 02:27:47 +0000 | [diff] [blame] | 630 | || tc->meta_type == AMT_OBJECT |
Lev Walkin | 9c2285a | 2006-03-09 08:49:26 +0000 | [diff] [blame] | 631 | || tc->meta_type == AMT_OBJECTCLASS |
| 632 | || tc->meta_type == AMT_OBJECTFIELD |
Lev Walkin | c74ea22 | 2004-08-25 02:27:47 +0000 | [diff] [blame] | 633 | ) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 634 | asn1p_expr_t *se; /* SubExpression */ |
Lev Walkin | 9c2285a | 2006-03-09 08:49:26 +0000 | [diff] [blame] | 635 | int put_braces = (!SEQ_OF) /* Don't need 'em, if SET OF... */ |
| 636 | && (tc->meta_type != AMT_OBJECTFIELD); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 637 | |
Lev Walkin | c74ea22 | 2004-08-25 02:27:47 +0000 | [diff] [blame] | 638 | if(put_braces) { |
Lev Walkin | f4069d2 | 2005-02-15 07:06:05 +0000 | [diff] [blame] | 639 | if(flags & APF_NOINDENT) { |
| 640 | printf("{"); |
| 641 | if(!TQ_FIRST(&tc->members)) |
| 642 | printf("}"); |
| 643 | } else { |
| 644 | printf(" {"); |
| 645 | if(TQ_FIRST(&tc->members)) |
| 646 | printf("\n"); |
| 647 | else |
| 648 | printf(" }"); |
| 649 | } |
Lev Walkin | c74ea22 | 2004-08-25 02:27:47 +0000 | [diff] [blame] | 650 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 651 | |
| 652 | TQ_FOR(se, &(tc->members), next) { |
| 653 | /* |
Lev Walkin | fd151ce | 2004-08-22 03:08:51 +0000 | [diff] [blame] | 654 | * Print the expression as it were a stand-alone type. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 655 | */ |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 656 | asn1print_expr(asn, mod, se, flags, level + 1); |
Lev Walkin | b1e0708 | 2004-09-15 11:44:55 +0000 | [diff] [blame] | 657 | if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) { |
| 658 | printf(" DEFAULT "); |
| 659 | asn1print_value(se->marker.default_value, flags); |
| 660 | } else if((se->marker.flags & EM_OPTIONAL) |
| 661 | == EM_OPTIONAL) { |
Lev Walkin | 6e8da2b | 2004-09-10 08:21:27 +0000 | [diff] [blame] | 662 | printf(" OPTIONAL"); |
Lev Walkin | b1e0708 | 2004-09-15 11:44:55 +0000 | [diff] [blame] | 663 | } |
Lev Walkin | ef62540 | 2005-09-05 05:17:57 +0000 | [diff] [blame] | 664 | if(TQ_NEXT(se, next)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 665 | printf(","); |
Lev Walkin | ef62540 | 2005-09-05 05:17:57 +0000 | [diff] [blame] | 666 | if(!(flags & APF_NOINDENT)) |
| 667 | INDENT("\n"); |
| 668 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Lev Walkin | c74ea22 | 2004-08-25 02:27:47 +0000 | [diff] [blame] | 671 | if(put_braces && TQ_FIRST(&tc->members)) { |
Lev Walkin | f4069d2 | 2005-02-15 07:06:05 +0000 | [diff] [blame] | 672 | if(!(flags & APF_NOINDENT)) |
| 673 | printf("\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 674 | INDENT("}"); |
| 675 | } |
| 676 | } |
| 677 | |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 678 | if(tc->with_syntax) { |
| 679 | printf(" WITH SYNTAX {"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 680 | asn1print_with_syntax(tc->with_syntax, flags); |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 681 | printf("}\n"); |
| 682 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 683 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 684 | /* Right hand specialization */ |
| 685 | if(tc->rhs_pspecs) { |
| 686 | asn1p_expr_t *se; |
| 687 | printf("{"); |
| 688 | TQ_FOR(se, &(tc->rhs_pspecs->members), next) { |
| 689 | asn1print_expr(asn, mod, se, flags, level + 1); |
Lev Walkin | 0c0bca6 | 2006-03-21 04:48:15 +0000 | [diff] [blame] | 690 | if(TQ_NEXT(se, next)) printf(", "); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 691 | } |
| 692 | printf("}"); |
| 693 | } |
| 694 | |
Lev Walkin | b1ef396 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 695 | if(!SEQ_OF && tc->constraints) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 696 | printf(" "); |
Lev Walkin | 557f27d | 2006-03-21 07:46:48 +0000 | [diff] [blame] | 697 | if(tc->meta_type == AMT_VALUESET) |
| 698 | printf("{"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 699 | asn1print_constraint(tc->constraints, flags); |
Lev Walkin | 557f27d | 2006-03-21 07:46:48 +0000 | [diff] [blame] | 700 | if(tc->meta_type == AMT_VALUESET) |
| 701 | printf("}"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 702 | } |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 703 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 704 | if(tc->unique) { |
| 705 | printf(" UNIQUE"); |
| 706 | } |
| 707 | |
| 708 | if(tc->meta_type == AMT_VALUE |
| 709 | && tc->expr_type != A1TC_EXTENSIBLE) { |
Lev Walkin | 0056aec | 2004-09-05 10:38:50 +0000 | [diff] [blame] | 710 | if(tc->expr_type == A1TC_UNIVERVAL) { |
Lev Walkin | b1e0708 | 2004-09-15 11:44:55 +0000 | [diff] [blame] | 711 | if(tc->value) { |
| 712 | printf("("); |
| 713 | asn1print_value(tc->value, flags); |
| 714 | printf(")"); |
| 715 | } |
Lev Walkin | 0056aec | 2004-09-05 10:38:50 +0000 | [diff] [blame] | 716 | } else { |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 717 | if(level == 0) printf(" ::= "); |
Lev Walkin | 0056aec | 2004-09-05 10:38:50 +0000 | [diff] [blame] | 718 | asn1print_value(tc->value, flags); |
| 719 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Lev Walkin | fd151ce | 2004-08-22 03:08:51 +0000 | [diff] [blame] | 722 | /* |
Lev Walkin | f4069d2 | 2005-02-15 07:06:05 +0000 | [diff] [blame] | 723 | * The following section exists entirely for debugging. |
Lev Walkin | fd151ce | 2004-08-22 03:08:51 +0000 | [diff] [blame] | 724 | */ |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 725 | if(flags & APF_PRINT_CONSTRAINTS |
Lev Walkin | b1ef396 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 726 | && tc->expr_type != A1TC_EXTENSIBLE) { |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 727 | asn1p_expr_t *top_parent; |
| 728 | |
| 729 | if(tc->combined_constraints) { |
| 730 | printf("\n-- Combined constraints: "); |
| 731 | asn1print_constraint(tc->combined_constraints, flags); |
| 732 | } |
| 733 | |
Lev Walkin | e4d6ab8 | 2004-09-22 16:05:13 +0000 | [diff] [blame] | 734 | top_parent = asn1f_find_terminal_type_ex(asn, tc); |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 735 | if(top_parent) { |
Lev Walkin | e8e87f1 | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 736 | printf("\n-- Practical constraints (%s): ", |
| 737 | top_parent->Identifier); |
| 738 | asn1print_constraint_explain(top_parent->expr_type, |
| 739 | tc->combined_constraints, 0); |
Lev Walkin | b1ef396 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 740 | printf("\n-- PER-visible constraints (%s): ", |
| 741 | top_parent->Identifier); |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 742 | asn1print_constraint_explain(top_parent->expr_type, |
Lev Walkin | e8e87f1 | 2004-08-25 02:00:03 +0000 | [diff] [blame] | 743 | tc->combined_constraints, 1); |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 744 | } |
Lev Walkin | b1ef396 | 2004-08-20 13:24:28 +0000 | [diff] [blame] | 745 | printf("\n"); |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 748 | if(flags & APF_PRINT_CLASS_MATRIX |
| 749 | && tc->expr_type == A1TC_CLASSDEF) do { |
| 750 | int r, col, maxidlen; |
| 751 | if(tc->object_class_matrix.rows == 0) { |
| 752 | printf("\n-- Class matrix is empty"); |
| 753 | break; |
| 754 | } |
| 755 | printf("\n-- Class matrix has %d entr%s:\n", |
| 756 | tc->object_class_matrix.rows, |
| 757 | tc->object_class_matrix.rows==1 ? "y" : "ies"); |
| 758 | maxidlen = tc->object_class_matrix.max_identifier_length; |
| 759 | for(r = -1; r < tc->object_class_matrix.rows; r++) { |
| 760 | struct asn1p_ioc_row_s *row; |
| 761 | row = tc->object_class_matrix.row[r<0?0:r]; |
| 762 | if(r < 0) printf("-- %s", r > 9 ? " " : ""); |
| 763 | else printf("-- [%*d]", r > 9 ? 2 : 1, r+1); |
| 764 | for(col = 0; col < row->columns; col++) { |
| 765 | struct asn1p_ioc_cell_s *cell; |
| 766 | cell = &row->column[col]; |
| 767 | if(r < 0) { |
| 768 | printf("[%*s]", maxidlen, |
| 769 | cell->field->Identifier); |
| 770 | continue; |
| 771 | } |
| 772 | if(!cell->value) { |
| 773 | printf(" %*s ", maxidlen, "<no entry>"); |
| 774 | continue; |
| 775 | } |
| 776 | printf(" %*s ", maxidlen, |
| 777 | cell->value->Identifier); |
| 778 | } |
| 779 | printf("\n"); |
| 780 | } |
| 781 | } while(0); |
| 782 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 783 | if(flags & APF_PRINT_CLASS_MATRIX |
| 784 | && tc->lhs_params) do { |
| 785 | int i; |
| 786 | if(tc->specializations.pspecs_count == 0) { |
| 787 | printf("\n-- No specializations found\n"); |
| 788 | break; |
| 789 | } |
| 790 | printf("\n-- Specializations list has %d entr%s:\n", |
| 791 | tc->specializations.pspecs_count, |
| 792 | tc->specializations.pspecs_count == 1 ? "y" : "ies"); |
| 793 | for(i = 0; i < tc->specializations.pspecs_count; i++) { |
| 794 | asn1p_expr_t *se; |
| 795 | struct asn1p_pspec_s *pspec; |
| 796 | pspec = &tc->specializations.pspec[i]; |
| 797 | printf("-- "); |
| 798 | TQ_FOR(se, &(pspec->rhs_pspecs->members), next) { |
| 799 | asn1print_expr(asn, mod, se, flags, level+1); |
| 800 | } |
| 801 | printf("\n"); |
| 802 | } |
| 803 | } while(0); |
| 804 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 805 | return 0; |
| 806 | } |
Lev Walkin | 3140e0e | 2004-08-18 04:50:37 +0000 | [diff] [blame] | 807 | |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 808 | |
| 809 | static int |
| 810 | asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum asn1print_flags flags, int level) { |
| 811 | asn1p_expr_t *se; |
| 812 | int expr_unordered = 0; |
Lev Walkin | ef39f7e | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 813 | int dont_involve_children = 0; |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 814 | |
| 815 | switch(expr->meta_type) { |
| 816 | case AMT_TYPE: |
| 817 | case AMT_TYPEREF: |
| 818 | break; |
| 819 | default: |
| 820 | if(expr->expr_type == A1TC_UNIVERVAL) |
| 821 | break; |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | if(!expr->Identifier) return 0; |
| 826 | |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 827 | if(flags & APF_LINE_COMMENTS) |
| 828 | INDENT("<!-- #line %d -->\n", expr->_lineno); |
| 829 | INDENT("<!ELEMENT %s", expr->Identifier); |
| 830 | |
| 831 | if(expr->expr_type == A1TC_REFERENCE) { |
| 832 | se = asn1f_find_terminal_type_ex(asn, expr); |
| 833 | if(!se) { |
Lev Walkin | ef39f7e | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 834 | printf(" (ANY)"); |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 835 | return 0; |
| 836 | } |
| 837 | expr = se; |
Lev Walkin | ef39f7e | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 838 | dont_involve_children = 1; |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Lev Walkin | d7963aa | 2005-03-10 15:16:56 +0000 | [diff] [blame] | 841 | if(expr->expr_type == ASN_CONSTR_CHOICE |
| 842 | || expr->expr_type == ASN_CONSTR_SEQUENCE_OF |
| 843 | || expr->expr_type == ASN_CONSTR_SET_OF |
| 844 | || expr->expr_type == ASN_CONSTR_SET |
Lev Walkin | 112d69e | 2005-03-09 20:52:15 +0000 | [diff] [blame] | 845 | || expr->expr_type == ASN_BASIC_INTEGER |
| 846 | || expr->expr_type == ASN_BASIC_ENUMERATED) { |
| 847 | expr_unordered = 1; |
| 848 | } |
| 849 | |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 850 | if(TQ_FIRST(&expr->members)) { |
| 851 | int extensible = 0; |
Lev Walkin | fbaeb85 | 2006-11-21 10:39:52 +0000 | [diff] [blame] | 852 | if(expr->expr_type == ASN_BASIC_BIT_STRING) |
| 853 | dont_involve_children = 1; |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 854 | printf(" ("); |
| 855 | TQ_FOR(se, &(expr->members), next) { |
| 856 | if(se->expr_type == A1TC_EXTENSIBLE) { |
| 857 | extensible = 1; |
| 858 | continue; |
| 859 | } else if(!se->Identifier |
| 860 | && se->expr_type == A1TC_REFERENCE) { |
| 861 | asn1print_ref(se->reference, flags); |
| 862 | } else if(se->Identifier) { |
| 863 | printf("%s", se->Identifier); |
| 864 | } else { |
| 865 | printf("ANY"); |
| 866 | } |
| 867 | if(expr->expr_type != ASN_CONSTR_SET |
| 868 | && expr->expr_type != ASN_CONSTR_CHOICE |
| 869 | && expr->expr_type != ASN_BASIC_INTEGER |
| 870 | && expr->expr_type != ASN_BASIC_ENUMERATED) { |
| 871 | if(expr_unordered) |
| 872 | printf("*"); |
| 873 | else if(se->marker.flags) |
| 874 | printf("?"); |
Lev Walkin | fbaeb85 | 2006-11-21 10:39:52 +0000 | [diff] [blame] | 875 | else if(expr->expr_type == ASN_BASIC_BIT_STRING) |
| 876 | printf("?"); |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 877 | } |
| 878 | if(TQ_NEXT(se, next) |
| 879 | && TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) { |
| 880 | printf(expr_unordered?"|":", "); |
| 881 | } |
| 882 | } |
| 883 | if(extensible) { |
| 884 | printf(expr_unordered?"|":", "); |
| 885 | printf("ANY"); |
| 886 | if(expr->expr_type != ASN_CONSTR_SET |
| 887 | && expr->expr_type != ASN_CONSTR_CHOICE |
| 888 | && expr->expr_type != ASN_BASIC_INTEGER |
| 889 | && expr->expr_type != ASN_BASIC_ENUMERATED) |
| 890 | printf("*"); |
| 891 | } |
| 892 | |
| 893 | printf(")"); |
| 894 | if(expr->expr_type == ASN_CONSTR_SET) |
| 895 | printf("*"); |
| 896 | |
Lev Walkin | ef39f7e | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 897 | } else switch(expr->expr_type) { |
| 898 | case ASN_BASIC_BOOLEAN: |
Lev Walkin | 112d69e | 2005-03-09 20:52:15 +0000 | [diff] [blame] | 899 | printf(" (true|false)"); |
Lev Walkin | ef39f7e | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 900 | break; |
| 901 | case ASN_CONSTR_CHOICE: |
| 902 | case ASN_CONSTR_SET: |
| 903 | case ASN_CONSTR_SET_OF: |
| 904 | case ASN_CONSTR_SEQUENCE: |
| 905 | case ASN_CONSTR_SEQUENCE_OF: |
| 906 | case ASN_BASIC_NULL: |
| 907 | case A1TC_UNIVERVAL: |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 908 | printf(" EMPTY"); |
Lev Walkin | ef39f7e | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 909 | break; |
| 910 | case ASN_TYPE_ANY: |
| 911 | printf(" ANY"); |
| 912 | break; |
| 913 | case ASN_BASIC_BIT_STRING: |
| 914 | case ASN_BASIC_OCTET_STRING: |
| 915 | case ASN_BASIC_OBJECT_IDENTIFIER: |
| 916 | case ASN_BASIC_RELATIVE_OID: |
Lev Walkin | cc11653 | 2004-10-15 06:01:54 +0000 | [diff] [blame] | 917 | case ASN_BASIC_INTEGER: |
Lev Walkin | ef39f7e | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 918 | case ASN_BASIC_UTCTime: |
| 919 | case ASN_BASIC_GeneralizedTime: |
Lev Walkin | ef39f7e | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 920 | case ASN_STRING_NumericString: |
| 921 | case ASN_STRING_PrintableString: |
Lev Walkin | cc11653 | 2004-10-15 06:01:54 +0000 | [diff] [blame] | 922 | printf(" (#PCDATA)"); |
Lev Walkin | ef39f7e | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 923 | break; |
| 924 | case ASN_STRING_VisibleString: |
| 925 | case ASN_STRING_ISO646String: |
| 926 | /* Entity references, but not XML elements may be present */ |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 927 | printf(" (#PCDATA)"); |
Lev Walkin | ef39f7e | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 928 | break; |
| 929 | case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */ |
| 930 | case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */ |
| 931 | default: |
| 932 | /* |
| 933 | * XML elements are allowed. |
| 934 | * For example, a UTF8String may contain "<bel/>". |
| 935 | */ |
| 936 | printf(" ANY"); |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 937 | } |
| 938 | printf(">\n"); |
| 939 | |
| 940 | /* |
| 941 | * Display the descendants (children) of the current type. |
| 942 | */ |
Lev Walkin | ef39f7e | 2004-10-14 05:11:25 +0000 | [diff] [blame] | 943 | if(!dont_involve_children) { |
| 944 | TQ_FOR(se, &(expr->members), next) { |
| 945 | if(se->expr_type == A1TC_EXTENSIBLE) continue; |
| 946 | asn1print_expr_dtd(asn, mod, se, flags, level + 1); |
| 947 | } |
Lev Walkin | f748451 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | return 0; |
| 951 | } |