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