Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>. |
| 3 | * All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 4 | * Redistribution and modifications are permitted subject to BSD license. |
| 5 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 6 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | #include <constr_CHOICE.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | |
| 9 | /* |
| 10 | * Number of bytes left for this structure. |
| 11 | * (ctx->left) indicates the number of bytes _transferred_ for the structure. |
| 12 | * (size) contains the number of bytes in the buffer passed. |
| 13 | */ |
Lev Walkin | ec1ffd4 | 2004-08-18 04:53:32 +0000 | [diff] [blame] | 14 | #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * If the subprocessor function returns with an indication that it wants |
| 18 | * more data, it may well be a fatal decoding problem, because the |
| 19 | * size is constrained by the <TLV>'s L, even if the buffer size allows |
| 20 | * reading more data. |
| 21 | * For example, consider the buffer containing the following TLVs: |
| 22 | * <T:5><L:1><V> <T:6>... |
| 23 | * The TLV length clearly indicates that one byte is expected in V, but |
| 24 | * if the V processor returns with "want more data" even if the buffer |
| 25 | * contains way more data than the V processor have seen. |
| 26 | */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 27 | #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * This macro "eats" the part of the buffer which is definitely "consumed", |
| 31 | * i.e. was correctly converted into local representation or rightfully skipped. |
| 32 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 33 | #undef ADVANCE |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | #define ADVANCE(num_bytes) do { \ |
| 35 | size_t num = num_bytes; \ |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 36 | ptr = ((const char *)ptr) + num;\ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | size -= num; \ |
| 38 | if(ctx->left >= 0) \ |
| 39 | ctx->left -= num; \ |
| 40 | consumed_myself += num; \ |
| 41 | } while(0) |
| 42 | |
| 43 | /* |
| 44 | * Switch to the next phase of parsing. |
| 45 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 46 | #undef NEXT_PHASE |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | #define NEXT_PHASE(ctx) do { \ |
| 48 | ctx->phase++; \ |
| 49 | ctx->step = 0; \ |
| 50 | } while(0) |
| 51 | |
| 52 | /* |
| 53 | * Return a standardized complex structure. |
| 54 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 55 | #undef RETURN |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 56 | #define RETURN(_code) do { \ |
| 57 | rval.code = _code; \ |
| 58 | rval.consumed = consumed_myself;\ |
| 59 | return rval; \ |
| 60 | } while(0) |
| 61 | |
| 62 | /* |
| 63 | * See the definitions. |
| 64 | */ |
Lev Walkin | 91f5cd0 | 2004-08-11 09:10:59 +0000 | [diff] [blame] | 65 | static int _fetch_present_idx(const void *struct_ptr, int off, int size); |
| 66 | static void _set_present_idx(void *sptr, int offset, int size, int pres); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * Tags are canonically sorted in the tag to member table. |
| 70 | */ |
| 71 | static int |
| 72 | _search4tag(const void *ap, const void *bp) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 73 | const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap; |
| 74 | const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 75 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 76 | int a_class = BER_TAG_CLASS(a->el_tag); |
| 77 | int b_class = BER_TAG_CLASS(b->el_tag); |
| 78 | |
| 79 | if(a_class == b_class) { |
| 80 | ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag); |
| 81 | ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag); |
| 82 | |
| 83 | if(a_value == b_value) |
| 84 | return 0; |
| 85 | else if(a_value < b_value) |
| 86 | return -1; |
| 87 | else |
| 88 | return 1; |
| 89 | } else if(a_class < b_class) { |
| 90 | return -1; |
| 91 | } else { |
| 92 | return 1; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * The decoder of the CHOICE type. |
| 98 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 99 | asn_dec_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 100 | CHOICE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 101 | void **struct_ptr, const void *ptr, size_t size, int tag_mode) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | /* |
| 103 | * Bring closer parts of structure description. |
| 104 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 105 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
| 106 | asn_TYPE_member_t *elements = td->elements; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * Parts of the structure being constructed. |
| 110 | */ |
| 111 | void *st = *struct_ptr; /* Target structure. */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 112 | asn_struct_ctx_t *ctx; /* Decoder context */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 113 | |
| 114 | ber_tlv_tag_t tlv_tag; /* T from TLV */ |
| 115 | ssize_t tag_len; /* Length of TLV's T */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 116 | asn_dec_rval_t rval; /* Return code from subparsers */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 117 | |
| 118 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
| 119 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 120 | ASN_DEBUG("Decoding %s as CHOICE", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * Create the target structure if it is not present already. |
| 124 | */ |
| 125 | if(st == 0) { |
| 126 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 127 | if(st == 0) { |
| 128 | RETURN(RC_FAIL); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Restore parsing context. |
| 134 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 135 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * Start to parse where left previously |
| 139 | */ |
| 140 | switch(ctx->phase) { |
| 141 | case 0: |
| 142 | /* |
| 143 | * PHASE 0. |
| 144 | * Check that the set of tags associated with given structure |
| 145 | * perfectly fits our expectations. |
| 146 | */ |
| 147 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 148 | if(tag_mode || td->tags_count) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 149 | rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 150 | tag_mode, -1, &ctx->left, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 151 | if(rval.code != RC_OK) { |
| 152 | ASN_DEBUG("%s tagging check failed: %d", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 153 | td->name, rval.code); |
Lev Walkin | f0f04d1 | 2004-10-05 06:36:02 +0000 | [diff] [blame] | 154 | return rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if(ctx->left >= 0) { |
| 158 | /* ?Substracted below! */ |
| 159 | ctx->left += rval.consumed; |
| 160 | } |
| 161 | ADVANCE(rval.consumed); |
| 162 | } else { |
| 163 | ctx->left = -1; |
| 164 | } |
| 165 | |
| 166 | NEXT_PHASE(ctx); |
| 167 | |
| 168 | ASN_DEBUG("Structure consumes %ld bytes, buffer %ld", |
| 169 | (long)ctx->left, (long)size); |
| 170 | |
| 171 | /* Fall through */ |
| 172 | case 1: |
| 173 | /* |
| 174 | * Fetch the T from TLV. |
| 175 | */ |
| 176 | tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 177 | ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 178 | switch(tag_len) { |
| 179 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 180 | /* Fall through */ |
| 181 | case -1: RETURN(RC_FAIL); |
| 182 | } |
| 183 | |
| 184 | do { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 185 | asn_TYPE_tag2member_t *t2m; |
| 186 | asn_TYPE_tag2member_t key; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 187 | |
| 188 | key.el_tag = tlv_tag; |
Lev Walkin | c17d90f | 2005-01-17 14:32:45 +0000 | [diff] [blame] | 189 | t2m = (asn_TYPE_tag2member_t *)bsearch(&key, |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 190 | specs->tag2el, specs->tag2el_count, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 191 | sizeof(specs->tag2el[0]), _search4tag); |
| 192 | if(t2m) { |
| 193 | /* |
| 194 | * Found the element corresponding to the tag. |
| 195 | */ |
| 196 | NEXT_PHASE(ctx); |
| 197 | ctx->step = t2m->el_no; |
| 198 | break; |
| 199 | } else if(specs->extensible == 0) { |
| 200 | ASN_DEBUG("Unexpected tag %s " |
| 201 | "in non-extensible CHOICE %s", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 202 | ber_tlv_tag_string(tlv_tag), td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 203 | RETURN(RC_FAIL); |
| 204 | } else { |
| 205 | /* Skip this tag */ |
| 206 | ssize_t skip; |
| 207 | |
| 208 | ASN_DEBUG("Skipping unknown tag %s", |
| 209 | ber_tlv_tag_string(tlv_tag)); |
| 210 | |
Lev Walkin | baaa24f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 211 | skip = ber_skip_length(opt_codec_ctx, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 212 | BER_TLV_CONSTRUCTED(ptr), |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 213 | (const char *)ptr + tag_len, |
| 214 | LEFT - tag_len); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 215 | |
| 216 | switch(skip) { |
| 217 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 218 | /* Fall through */ |
| 219 | case -1: RETURN(RC_FAIL); |
| 220 | } |
| 221 | |
| 222 | ADVANCE(skip + tag_len); |
| 223 | RETURN(RC_OK); |
| 224 | } |
| 225 | } while(0); |
| 226 | |
| 227 | case 2: |
| 228 | /* |
| 229 | * PHASE 2. |
| 230 | * Read in the element. |
| 231 | */ |
| 232 | do { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 233 | asn_TYPE_member_t *elm;/* CHOICE's element */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 234 | void *memb_ptr; /* Pointer to the member */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 235 | void **memb_ptr2; /* Pointer to that pointer */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 236 | |
| 237 | elm = &elements[ctx->step]; |
| 238 | |
| 239 | /* |
| 240 | * Compute the position of the member inside a structure, |
| 241 | * and also a type of containment (it may be contained |
| 242 | * as pointer or using inline inclusion). |
| 243 | */ |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 244 | if(elm->flags & ATF_POINTER) { |
| 245 | /* Member is a pointer to another structure */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 246 | memb_ptr2 = (void **)((char *)st + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 247 | } else { |
| 248 | /* |
| 249 | * A pointer to a pointer |
| 250 | * holding the start of the structure |
| 251 | */ |
| 252 | memb_ptr = (char *)st + elm->memb_offset; |
| 253 | memb_ptr2 = &memb_ptr; |
| 254 | } |
| 255 | /* |
| 256 | * Invoke the member fetch routine according to member's type |
| 257 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 258 | rval = elm->type->ber_decoder(opt_codec_ctx, elm->type, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 259 | memb_ptr2, ptr, LEFT, |
| 260 | elm->tag_mode); |
| 261 | switch(rval.code) { |
| 262 | case RC_OK: |
| 263 | _set_present_idx(st, specs->pres_offset, |
| 264 | specs->pres_size, ctx->step + 1); |
| 265 | break; |
| 266 | case RC_WMORE: /* More data expected */ |
| 267 | if(!SIZE_VIOLATION) { |
| 268 | ADVANCE(rval.consumed); |
| 269 | RETURN(RC_WMORE); |
| 270 | } |
| 271 | RETURN(RC_FAIL); |
| 272 | case RC_FAIL: /* Fatal error */ |
| 273 | RETURN(rval.code); |
| 274 | } /* switch(rval) */ |
| 275 | |
| 276 | ADVANCE(rval.consumed); |
| 277 | } while(0); |
| 278 | |
| 279 | NEXT_PHASE(ctx); |
| 280 | |
| 281 | /* Fall through */ |
| 282 | case 3: |
| 283 | ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 284 | td->name, (long)ctx->left, (long)size, |
| 285 | tag_mode, td->tags_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 286 | |
| 287 | if(ctx->left > 0) { |
| 288 | /* |
| 289 | * The type must be fully decoded |
| 290 | * by the CHOICE member-specific decoder. |
| 291 | */ |
| 292 | RETURN(RC_FAIL); |
| 293 | } |
| 294 | |
| 295 | if(ctx->left == -1 |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 296 | && !(tag_mode || td->tags_count)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 297 | /* |
| 298 | * This is an untagged CHOICE. |
| 299 | * It doesn't contain nothing |
| 300 | * except for the member itself, including all its tags. |
| 301 | * The decoding is completed. |
| 302 | */ |
| 303 | NEXT_PHASE(ctx); |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Read in the "end of data chunks"'s. |
| 309 | */ |
| 310 | while(ctx->left < 0) { |
| 311 | ssize_t tl; |
| 312 | |
| 313 | tl = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 314 | switch(tl) { |
| 315 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 316 | /* Fall through */ |
| 317 | case -1: RETURN(RC_FAIL); |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Expected <0><0>... |
| 322 | */ |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 323 | if(((const uint8_t *)ptr)[0] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 324 | if(LEFT < 2) { |
| 325 | if(SIZE_VIOLATION) |
| 326 | RETURN(RC_FAIL); |
| 327 | else |
| 328 | RETURN(RC_WMORE); |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 329 | } else if(((const uint8_t *)ptr)[1] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 330 | /* |
| 331 | * Correctly finished with <0><0>. |
| 332 | */ |
Lev Walkin | f0f04d1 | 2004-10-05 06:36:02 +0000 | [diff] [blame] | 333 | ADVANCE(2); |
| 334 | ctx->left++; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 335 | continue; |
| 336 | } |
| 337 | } else { |
| 338 | ASN_DEBUG("Unexpected continuation in %s", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 339 | td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 340 | RETURN(RC_FAIL); |
| 341 | } |
| 342 | |
Lev Walkin | f0f04d1 | 2004-10-05 06:36:02 +0000 | [diff] [blame] | 343 | /* UNREACHABLE */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | NEXT_PHASE(ctx); |
| 347 | case 4: |
| 348 | /* No meaningful work here */ |
| 349 | break; |
| 350 | } |
| 351 | |
| 352 | RETURN(RC_OK); |
| 353 | } |
| 354 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 355 | asn_enc_rval_t |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 356 | CHOICE_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 357 | int tag_mode, ber_tlv_tag_t tag, |
| 358 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 359 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
| 360 | asn_TYPE_member_t *elm; /* CHOICE element */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 361 | asn_enc_rval_t erval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 362 | void *memb_ptr; |
| 363 | size_t computed_size = 0; |
| 364 | int present; |
| 365 | |
| 366 | ASN_DEBUG("%s %s as CHOICE", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 367 | cb?"Encoding":"Estimating", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 368 | |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 369 | present = _fetch_present_idx(sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 370 | specs->pres_offset, specs->pres_size); |
| 371 | |
| 372 | /* |
| 373 | * If the structure was not initialized, it cannot be encoded: |
| 374 | * can't deduce what to encode in the choice type. |
| 375 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 376 | if(present <= 0 || present > td->elements_count) { |
| 377 | if(present == 0 && td->elements_count == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 378 | /* The CHOICE is empty?! */ |
| 379 | erval.encoded = 0; |
| 380 | return erval; |
| 381 | } |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 382 | _ASN_ENCODE_FAILED; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Seek over the present member of the structure. |
| 387 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 388 | elm = &td->elements[present-1]; |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 389 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 390 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 391 | if(memb_ptr == 0) { |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 392 | if(elm->optional) { |
| 393 | erval.encoded = 0; |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 394 | return erval; |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 395 | } |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 396 | /* Mandatory element absent */ |
| 397 | _ASN_ENCODE_FAILED; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 398 | } |
| 399 | } else { |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 400 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* |
| 404 | * If the CHOICE itself is tagged EXPLICIT: |
| 405 | * T ::= [2] EXPLICIT CHOICE { ... } |
| 406 | * Then emit the appropriate tags. |
| 407 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 408 | if(tag_mode == 1 || td->tags_count) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 409 | /* |
| 410 | * For this, we need to pre-compute the member. |
| 411 | */ |
| 412 | ssize_t ret; |
| 413 | |
| 414 | /* Encode member with its tag */ |
| 415 | erval = elm->type->der_encoder(elm->type, memb_ptr, |
| 416 | elm->tag_mode, elm->tag, 0, 0); |
| 417 | if(erval.encoded == -1) |
| 418 | return erval; |
| 419 | |
| 420 | /* Encode CHOICE with parent or my own tag */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 421 | ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 422 | cb, app_key); |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 423 | if(ret == -1) |
| 424 | _ASN_ENCODE_FAILED; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 425 | computed_size += ret; |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * Encode the single underlying member. |
| 430 | */ |
| 431 | erval = elm->type->der_encoder(elm->type, memb_ptr, |
| 432 | elm->tag_mode, elm->tag, cb, app_key); |
| 433 | if(erval.encoded == -1) |
| 434 | return erval; |
| 435 | |
| 436 | ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)", |
| 437 | (long)erval.encoded, (long)computed_size); |
| 438 | |
| 439 | erval.encoded += computed_size; |
| 440 | |
| 441 | return erval; |
| 442 | } |
| 443 | |
| 444 | ber_tlv_tag_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 445 | CHOICE_outmost_tag(asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber_tlv_tag_t tag) { |
| 446 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 447 | int present; |
| 448 | |
Lev Walkin | 3278873 | 2005-11-13 09:12:23 +0000 | [diff] [blame] | 449 | assert(tag_mode == 0); (void)tag_mode; |
| 450 | assert(tag == 0); (void)tag; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 451 | |
| 452 | /* |
| 453 | * Figure out which CHOICE element is encoded. |
| 454 | */ |
| 455 | present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size); |
| 456 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 457 | if(present > 0 || present <= td->elements_count) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 458 | asn_TYPE_member_t *elm = &td->elements[present-1]; |
Lev Walkin | 70e7028 | 2004-06-05 01:45:48 +0000 | [diff] [blame] | 459 | const void *memb_ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 460 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 461 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | 70e7028 | 2004-06-05 01:45:48 +0000 | [diff] [blame] | 462 | memb_ptr = *(const void * const *) |
| 463 | ((const char *)ptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 464 | } else { |
Lev Walkin | 70e7028 | 2004-06-05 01:45:48 +0000 | [diff] [blame] | 465 | memb_ptr = (const void *) |
| 466 | ((const char *)ptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 469 | return asn_TYPE_outmost_tag(elm->type, memb_ptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 470 | elm->tag_mode, elm->tag); |
| 471 | } else { |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 472 | return (ber_tlv_tag_t)-1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 477 | CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 478 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 479 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 480 | int present; |
| 481 | |
| 482 | if(!sptr) { |
Lev Walkin | ba4e518 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 483 | _ASN_ERRLOG(app_errlog, app_key, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 484 | "%s: value not given (%s:%d)", |
| 485 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 486 | return -1; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Figure out which CHOICE element is encoded. |
| 491 | */ |
| 492 | present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 493 | if(present > 0 && present <= td->elements_count) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 494 | asn_TYPE_member_t *elm = &td->elements[present-1]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 495 | const void *memb_ptr; |
| 496 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 497 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 498 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 499 | if(!memb_ptr) { |
| 500 | if(elm->optional) |
| 501 | return 0; |
| 502 | _ASN_ERRLOG(app_errlog, app_key, |
| 503 | "%s: mandatory CHOICE element %s absent (%s:%d)", |
| 504 | td->name, elm->name, __FILE__, __LINE__); |
| 505 | return -1; |
| 506 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 507 | } else { |
| 508 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 509 | } |
| 510 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 511 | if(elm->memb_constraints) { |
| 512 | return elm->memb_constraints(elm->type, memb_ptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 513 | app_errlog, app_key); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 514 | } else { |
| 515 | int ret = elm->type->check_constraints(elm->type, |
| 516 | memb_ptr, app_errlog, app_key); |
| 517 | /* |
| 518 | * Cannot inherit it eralier: |
| 519 | * need to make sure we get the updated version. |
| 520 | */ |
| 521 | elm->memb_constraints = elm->type->check_constraints; |
| 522 | return ret; |
| 523 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 524 | } else { |
Lev Walkin | ba4e518 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 525 | _ASN_ERRLOG(app_errlog, app_key, |
Lev Walkin | d34a851 | 2004-08-22 13:55:49 +0000 | [diff] [blame] | 526 | "%s: no CHOICE element given (%s:%d)", |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 527 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 528 | return -1; |
| 529 | } |
| 530 | } |
| 531 | |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 532 | #undef XER_ADVANCE |
| 533 | #define XER_ADVANCE(num_bytes) do { \ |
| 534 | size_t num = num_bytes; \ |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 535 | buf_ptr = ((const char *)buf_ptr) + num;\ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 536 | size -= num; \ |
| 537 | consumed_myself += num; \ |
| 538 | } while(0) |
| 539 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 540 | /* |
| 541 | * Decode the XER (XML) data. |
| 542 | */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 543 | asn_dec_rval_t |
| 544 | CHOICE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 545 | void **struct_ptr, const char *opt_mname, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 546 | const void *buf_ptr, size_t size) { |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 547 | /* |
| 548 | * Bring closer parts of structure description. |
| 549 | */ |
| 550 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
| 551 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 552 | |
| 553 | /* |
| 554 | * Parts of the structure being constructed. |
| 555 | */ |
| 556 | void *st = *struct_ptr; /* Target structure. */ |
| 557 | asn_struct_ctx_t *ctx; /* Decoder context */ |
| 558 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 559 | asn_dec_rval_t rval; /* Return value of a decoder */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 560 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 561 | int edx; /* Element index */ |
| 562 | |
| 563 | /* |
| 564 | * Create the target structure if it is not present already. |
| 565 | */ |
| 566 | if(st == 0) { |
| 567 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 568 | if(st == 0) RETURN(RC_FAIL); |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Restore parsing context. |
| 573 | */ |
| 574 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 575 | if(ctx->phase == 0 && !*xml_tag) |
| 576 | ctx->phase = 1; /* Skip the outer tag checking phase */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 577 | |
| 578 | /* |
| 579 | * Phases of XER/XML processing: |
| 580 | * Phase 0: Check that the opening tag matches our expectations. |
| 581 | * Phase 1: Processing body and reacting on closing tag. |
| 582 | * Phase 2: Processing inner type. |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 583 | * Phase 3: Only waiting for closing tag. |
| 584 | * Phase 4: Skipping unknown extensions. |
| 585 | * Phase 5: PHASED OUT |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 586 | */ |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 587 | for(edx = ctx->step; ctx->phase <= 4;) { |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 588 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 589 | ssize_t ch_size; /* Chunk size */ |
| 590 | xer_check_tag_e tcv; /* Tag check value */ |
| 591 | asn_TYPE_member_t *elm; |
| 592 | |
| 593 | /* |
| 594 | * Go inside the member. |
| 595 | */ |
| 596 | if(ctx->phase == 2) { |
| 597 | asn_dec_rval_t tmprval; |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 598 | void *memb_ptr; /* Pointer to the member */ |
| 599 | void **memb_ptr2; /* Pointer to that pointer */ |
| 600 | |
Lev Walkin | abf6889 | 2004-10-26 10:12:14 +0000 | [diff] [blame] | 601 | elm = &td->elements[edx]; |
| 602 | |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 603 | if(elm->flags & ATF_POINTER) { |
| 604 | /* Member is a pointer to another structure */ |
| 605 | memb_ptr2 = (void **)((char *)st |
| 606 | + elm->memb_offset); |
| 607 | } else { |
| 608 | memb_ptr = (char *)st + elm->memb_offset; |
| 609 | memb_ptr2 = &memb_ptr; |
| 610 | } |
| 611 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 612 | /* Start/Continue decoding the inner member */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 613 | tmprval = elm->type->xer_decoder(opt_codec_ctx, |
| 614 | elm->type, memb_ptr2, elm->name, |
| 615 | buf_ptr, size); |
| 616 | XER_ADVANCE(tmprval.consumed); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 617 | ASN_DEBUG("XER/CHOICE: itdf: code=%d", tmprval.code); |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 618 | if(tmprval.code != RC_OK) |
| 619 | RETURN(tmprval.code); |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 620 | assert(_fetch_present_idx(st, |
| 621 | specs->pres_offset, specs->pres_size) == 0); |
| 622 | /* Record what we've got */ |
| 623 | _set_present_idx(st, |
| 624 | specs->pres_offset, specs->pres_size, edx + 1); |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 625 | ctx->phase = 3; |
| 626 | /* Fall through */ |
| 627 | } |
| 628 | |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 629 | /* No need to wait for closing tag; special mode. */ |
| 630 | if(ctx->phase == 3 && !*xml_tag) { |
| 631 | ctx->phase = 5; /* Phase out */ |
| 632 | RETURN(RC_OK); |
| 633 | } |
| 634 | |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 635 | /* |
| 636 | * Get the next part of the XML stream. |
| 637 | */ |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 638 | ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type); |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 639 | switch(ch_size) { |
| 640 | case -1: RETURN(RC_FAIL); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 641 | case 0: RETURN(RC_WMORE); |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 642 | default: |
| 643 | switch(ch_type) { |
| 644 | case PXER_COMMENT: /* Got XML comment */ |
| 645 | case PXER_TEXT: /* Ignore free-standing text */ |
| 646 | XER_ADVANCE(ch_size); /* Skip silently */ |
| 647 | continue; |
| 648 | case PXER_TAG: |
| 649 | break; /* Check the rest down there */ |
| 650 | } |
| 651 | } |
| 652 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 653 | tcv = xer_check_tag(buf_ptr, ch_size, xml_tag); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 654 | ASN_DEBUG("XER/CHOICE checked [%c%c%c%c] vs [%s], tcv=%d", |
Lev Walkin | 3278873 | 2005-11-13 09:12:23 +0000 | [diff] [blame] | 655 | ch_size>0?((const uint8_t *)buf_ptr)[0]:'?', |
| 656 | ch_size>1?((const uint8_t *)buf_ptr)[1]:'?', |
| 657 | ch_size>2?((const uint8_t *)buf_ptr)[2]:'?', |
| 658 | ch_size>3?((const uint8_t *)buf_ptr)[3]:'?', |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 659 | xml_tag, tcv); |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 660 | |
| 661 | /* Skip the extensions section */ |
| 662 | if(ctx->phase == 4) { |
| 663 | ASN_DEBUG("skip_unknown(%d, %ld)", |
| 664 | tcv, (long)ctx->left); |
| 665 | switch(xer_skip_unknown(tcv, &ctx->left)) { |
| 666 | case -1: |
| 667 | ctx->phase = 5; |
| 668 | RETURN(RC_FAIL); |
| 669 | continue; |
| 670 | case 1: |
| 671 | ctx->phase = 3; |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 672 | /* Fall through */ |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 673 | case 0: |
| 674 | XER_ADVANCE(ch_size); |
| 675 | continue; |
| 676 | case 2: |
| 677 | ctx->phase = 3; |
| 678 | break; |
| 679 | } |
| 680 | } |
| 681 | |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 682 | switch(tcv) { |
| 683 | case XCT_BOTH: |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 684 | break; /* No CHOICE? */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 685 | case XCT_CLOSING: |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 686 | if(ctx->phase != 3) |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 687 | break; |
| 688 | XER_ADVANCE(ch_size); |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 689 | ctx->phase = 5; /* Phase out */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 690 | RETURN(RC_OK); |
| 691 | case XCT_OPENING: |
| 692 | if(ctx->phase == 0) { |
| 693 | XER_ADVANCE(ch_size); |
| 694 | ctx->phase = 1; /* Processing body phase */ |
| 695 | continue; |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 696 | } |
| 697 | /* Fall through */ |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 698 | case XCT_UNKNOWN_OP: |
| 699 | case XCT_UNKNOWN_BO: |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 700 | |
| 701 | if(ctx->phase != 1) |
| 702 | break; /* Really unexpected */ |
| 703 | |
| 704 | /* |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 705 | * Search which inner member corresponds to this tag. |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 706 | */ |
| 707 | for(edx = 0; edx < td->elements_count; edx++) { |
| 708 | elm = &td->elements[edx]; |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 709 | tcv = xer_check_tag(buf_ptr,ch_size,elm->name); |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 710 | switch(tcv) { |
| 711 | case XCT_BOTH: |
| 712 | case XCT_OPENING: |
| 713 | /* |
| 714 | * Process this member. |
| 715 | */ |
| 716 | ctx->step = edx; |
| 717 | ctx->phase = 2; |
| 718 | break; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 719 | case XCT_UNKNOWN_OP: |
| 720 | case XCT_UNKNOWN_BO: |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 721 | continue; |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 722 | default: |
| 723 | edx = td->elements_count; |
| 724 | break; /* Phase out */ |
| 725 | } |
| 726 | break; |
| 727 | } |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 728 | if(edx != td->elements_count) |
| 729 | continue; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 730 | |
| 731 | /* It is expected extension */ |
| 732 | if(specs->extensible) { |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 733 | ASN_DEBUG("Got anticipated extension"); |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 734 | /* |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 735 | * Check for (XCT_BOTH or XCT_UNKNOWN_BO) |
| 736 | * By using a mask. Only record a pure |
| 737 | * <opening> tags. |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 738 | */ |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 739 | if(tcv & XCT_CLOSING) { |
| 740 | /* Found </extension> without body */ |
| 741 | ctx->phase = 3; /* Terminating */ |
| 742 | } else { |
| 743 | ctx->left = 1; |
| 744 | ctx->phase = 4; /* Skip ...'s */ |
| 745 | } |
| 746 | XER_ADVANCE(ch_size); |
| 747 | continue; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 750 | /* Fall through */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 751 | default: |
| 752 | break; |
| 753 | } |
| 754 | |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 755 | ASN_DEBUG("Unexpected XML tag in CHOICE (ph=%d, tag=%s)", |
| 756 | ctx->phase, xml_tag); |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 757 | break; |
| 758 | } |
| 759 | |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 760 | ctx->phase = 5; /* Phase out, just in case */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 761 | RETURN(RC_FAIL); |
| 762 | } |
| 763 | |
| 764 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 765 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 766 | CHOICE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 767 | int ilevel, enum xer_encoder_flags_e flags, |
| 768 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 769 | asn_CHOICE_specifics_t *specs=(asn_CHOICE_specifics_t *)td->specifics; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 770 | asn_enc_rval_t er; |
| 771 | int present; |
| 772 | |
| 773 | if(!sptr) |
| 774 | _ASN_ENCODE_FAILED; |
| 775 | |
| 776 | /* |
| 777 | * Figure out which CHOICE element is encoded. |
| 778 | */ |
| 779 | present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size); |
| 780 | |
| 781 | if(present <= 0 || present > td->elements_count) { |
| 782 | _ASN_ENCODE_FAILED; |
| 783 | } else { |
| 784 | asn_enc_rval_t tmper; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 785 | asn_TYPE_member_t *elm = &td->elements[present-1]; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 786 | void *memb_ptr; |
| 787 | const char *mname = elm->name; |
| 788 | unsigned int mlen = strlen(mname); |
| 789 | |
| 790 | if(elm->flags & ATF_POINTER) { |
| 791 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
| 792 | if(!memb_ptr) _ASN_ENCODE_FAILED; |
| 793 | } else { |
| 794 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
| 795 | } |
| 796 | |
| 797 | er.encoded = 0; |
| 798 | |
| 799 | if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel); |
| 800 | _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1); |
| 801 | |
| 802 | tmper = elm->type->xer_encoder(elm->type, memb_ptr, |
| 803 | ilevel + 1, flags, cb, app_key); |
| 804 | if(tmper.encoded == -1) return tmper; |
| 805 | |
| 806 | _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1); |
| 807 | |
| 808 | er.encoded += 5 + (2 * mlen) + tmper.encoded; |
| 809 | } |
| 810 | |
| 811 | if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel - 1); |
| 812 | |
| 813 | return er; |
Lev Walkin | 942fd08 | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 814 | cb_failed: |
| 815 | _ASN_ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 818 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 819 | CHOICE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 820 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 821 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 822 | int present; |
| 823 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 824 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 825 | |
| 826 | /* |
| 827 | * Figure out which CHOICE element is encoded. |
| 828 | */ |
| 829 | present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size); |
| 830 | |
| 831 | /* |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 832 | * Print that element. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 833 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 834 | if(present > 0 && present <= td->elements_count) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 835 | asn_TYPE_member_t *elm = &td->elements[present-1]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 836 | const void *memb_ptr; |
| 837 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 838 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 839 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 840 | if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 841 | } else { |
| 842 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 843 | } |
| 844 | |
| 845 | /* Print member's name and stuff */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 846 | if(0) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 847 | if(cb(elm->name, strlen(elm->name), app_key) < 0 |
| 848 | || cb(": ", 2, app_key) < 0) |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 849 | return -1; |
| 850 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 851 | |
| 852 | return elm->type->print_struct(elm->type, memb_ptr, ilevel, |
| 853 | cb, app_key); |
| 854 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 855 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
| 859 | void |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 860 | CHOICE_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
| 861 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 862 | int present; |
| 863 | |
| 864 | if(!td || !ptr) |
| 865 | return; |
| 866 | |
| 867 | ASN_DEBUG("Freeing %s as CHOICE", td->name); |
| 868 | |
| 869 | /* |
| 870 | * Figure out which CHOICE element is encoded. |
| 871 | */ |
| 872 | present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size); |
| 873 | |
| 874 | /* |
| 875 | * Free that element. |
| 876 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 877 | if(present > 0 && present <= td->elements_count) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 878 | asn_TYPE_member_t *elm = &td->elements[present-1]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 879 | void *memb_ptr; |
| 880 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 881 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 882 | memb_ptr = *(void **)((char *)ptr + elm->memb_offset); |
| 883 | if(memb_ptr) |
| 884 | elm->type->free_struct(elm->type, memb_ptr, 0); |
| 885 | } else { |
| 886 | memb_ptr = (void *)((char *)ptr + elm->memb_offset); |
| 887 | elm->type->free_struct(elm->type, memb_ptr, 1); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | if(!contents_only) { |
| 892 | FREEMEM(ptr); |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | |
| 897 | /* |
| 898 | * The following functions functions offer protection against -fshort-enums, |
| 899 | * compatible with little- and big-endian machines. |
| 900 | * If assertion is triggered, either disable -fshort-enums, or add an entry |
| 901 | * here with the ->pres_size of your target stracture. |
| 902 | * Unless the target structure is packed, the ".present" member |
| 903 | * is guaranteed to be aligned properly. ASN.1 compiler itself does not |
| 904 | * produce packed code. |
| 905 | */ |
Lev Walkin | 91f5cd0 | 2004-08-11 09:10:59 +0000 | [diff] [blame] | 906 | static int |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 907 | _fetch_present_idx(const void *struct_ptr, int pres_offset, int pres_size) { |
| 908 | const void *present_ptr; |
| 909 | int present; |
| 910 | |
| 911 | present_ptr = ((const char *)struct_ptr) + pres_offset; |
| 912 | |
| 913 | switch(pres_size) { |
| 914 | case sizeof(int): present = *(const int *)present_ptr; break; |
| 915 | case sizeof(short): present = *(const short *)present_ptr; break; |
| 916 | case sizeof(char): present = *(const char *)present_ptr; break; |
| 917 | default: |
| 918 | /* ANSI C mandates enum to be equivalent to integer */ |
| 919 | assert(pres_size != sizeof(int)); |
| 920 | return 0; /* If not aborted, pass back safe value */ |
| 921 | } |
| 922 | |
| 923 | return present; |
| 924 | } |
| 925 | |
Lev Walkin | 91f5cd0 | 2004-08-11 09:10:59 +0000 | [diff] [blame] | 926 | static void |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 927 | _set_present_idx(void *struct_ptr, int pres_offset, int pres_size, int present) { |
| 928 | void *present_ptr; |
| 929 | present_ptr = ((char *)struct_ptr) + pres_offset; |
| 930 | |
| 931 | switch(pres_size) { |
| 932 | case sizeof(int): *(int *)present_ptr = present; break; |
| 933 | case sizeof(short): *(short *)present_ptr = present; break; |
| 934 | case sizeof(char): *(char *)present_ptr = present; break; |
| 935 | default: |
| 936 | /* ANSI C mandates enum to be equivalent to integer */ |
| 937 | assert(pres_size != sizeof(int)); |
| 938 | } |
| 939 | } |