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