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 | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 106 | 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] | 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 | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 519 | if(elm->memb_constraints) { |
| 520 | return elm->memb_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 { |
| 523 | int ret = elm->type->check_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 | /* |
| 526 | * Cannot inherit it eralier: |
| 527 | * need to make sure we get the updated version. |
| 528 | */ |
| 529 | elm->memb_constraints = elm->type->check_constraints; |
| 530 | return ret; |
| 531 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 532 | } else { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 533 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | d34a851 | 2004-08-22 13:55:49 +0000 | [diff] [blame] | 534 | "%s: no CHOICE element given (%s:%d)", |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 535 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 536 | return -1; |
| 537 | } |
| 538 | } |
| 539 | |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 540 | #undef XER_ADVANCE |
| 541 | #define XER_ADVANCE(num_bytes) do { \ |
| 542 | size_t num = num_bytes; \ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 543 | buf_ptr = (const void *)(((const char *)buf_ptr) + num); \ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 544 | size -= num; \ |
| 545 | consumed_myself += num; \ |
| 546 | } while(0) |
| 547 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 548 | /* |
| 549 | * Decode the XER (XML) data. |
| 550 | */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 551 | asn_dec_rval_t |
| 552 | CHOICE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 553 | void **struct_ptr, const char *opt_mname, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 554 | const void *buf_ptr, size_t size) { |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 555 | /* |
| 556 | * Bring closer parts of structure description. |
| 557 | */ |
| 558 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
| 559 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 560 | |
| 561 | /* |
| 562 | * Parts of the structure being constructed. |
| 563 | */ |
| 564 | void *st = *struct_ptr; /* Target structure. */ |
| 565 | asn_struct_ctx_t *ctx; /* Decoder context */ |
| 566 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 567 | asn_dec_rval_t rval; /* Return value of a decoder */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 568 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 569 | size_t edx; /* Element index */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 570 | |
| 571 | /* |
| 572 | * Create the target structure if it is not present already. |
| 573 | */ |
| 574 | if(st == 0) { |
| 575 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 576 | if(st == 0) RETURN(RC_FAIL); |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * Restore parsing context. |
| 581 | */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 582 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 583 | if(ctx->phase == 0 && !*xml_tag) |
| 584 | ctx->phase = 1; /* Skip the outer tag checking phase */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 585 | |
| 586 | /* |
| 587 | * Phases of XER/XML processing: |
| 588 | * Phase 0: Check that the opening tag matches our expectations. |
| 589 | * Phase 1: Processing body and reacting on closing tag. |
| 590 | * Phase 2: Processing inner type. |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 591 | * Phase 3: Only waiting for closing tag. |
| 592 | * Phase 4: Skipping unknown extensions. |
| 593 | * Phase 5: PHASED OUT |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 594 | */ |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 595 | for(edx = ctx->step; ctx->phase <= 4;) { |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 596 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 597 | ssize_t ch_size; /* Chunk size */ |
| 598 | xer_check_tag_e tcv; /* Tag check value */ |
| 599 | asn_TYPE_member_t *elm; |
| 600 | |
| 601 | /* |
| 602 | * Go inside the member. |
| 603 | */ |
| 604 | if(ctx->phase == 2) { |
| 605 | asn_dec_rval_t tmprval; |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 606 | void *memb_ptr; /* Pointer to the member */ |
| 607 | void **memb_ptr2; /* Pointer to that pointer */ |
| 608 | |
Lev Walkin | abf6889 | 2004-10-26 10:12:14 +0000 | [diff] [blame] | 609 | elm = &td->elements[edx]; |
| 610 | |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 611 | if(elm->flags & ATF_POINTER) { |
| 612 | /* Member is a pointer to another structure */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 613 | memb_ptr2 = (void **)((char *)st |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 614 | + elm->memb_offset); |
| 615 | } else { |
| 616 | memb_ptr = (char *)st + elm->memb_offset; |
| 617 | memb_ptr2 = &memb_ptr; |
| 618 | } |
| 619 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 620 | /* Start/Continue decoding the inner member */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 621 | tmprval = elm->type->op->xer_decoder(opt_codec_ctx, |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 622 | elm->type, memb_ptr2, elm->name, |
| 623 | buf_ptr, size); |
| 624 | XER_ADVANCE(tmprval.consumed); |
Lev Walkin | 866c880 | 2006-09-17 08:23:35 +0000 | [diff] [blame] | 625 | ASN_DEBUG("XER/CHOICE: itdf: [%s] code=%d", |
| 626 | elm->type->name, tmprval.code); |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 627 | if(tmprval.code != RC_OK) |
| 628 | RETURN(tmprval.code); |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 629 | assert(_fetch_present_idx(st, |
| 630 | specs->pres_offset, specs->pres_size) == 0); |
| 631 | /* Record what we've got */ |
| 632 | _set_present_idx(st, |
| 633 | specs->pres_offset, specs->pres_size, edx + 1); |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 634 | ctx->phase = 3; |
| 635 | /* Fall through */ |
| 636 | } |
| 637 | |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 638 | /* No need to wait for closing tag; special mode. */ |
| 639 | if(ctx->phase == 3 && !*xml_tag) { |
| 640 | ctx->phase = 5; /* Phase out */ |
| 641 | RETURN(RC_OK); |
| 642 | } |
| 643 | |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 644 | /* |
| 645 | * Get the next part of the XML stream. |
| 646 | */ |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 647 | ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type); |
Lev Walkin | 9736348 | 2016-01-24 19:23:02 -0800 | [diff] [blame] | 648 | if(ch_size == -1) { |
| 649 | RETURN(RC_FAIL); |
| 650 | } else { |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 651 | switch(ch_type) { |
Lev Walkin | 9736348 | 2016-01-24 19:23:02 -0800 | [diff] [blame] | 652 | case PXER_WMORE: |
| 653 | RETURN(RC_WMORE); |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 654 | case PXER_COMMENT: /* Got XML comment */ |
| 655 | case PXER_TEXT: /* Ignore free-standing text */ |
| 656 | XER_ADVANCE(ch_size); /* Skip silently */ |
| 657 | continue; |
| 658 | case PXER_TAG: |
| 659 | break; /* Check the rest down there */ |
| 660 | } |
| 661 | } |
| 662 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 663 | tcv = xer_check_tag(buf_ptr, ch_size, xml_tag); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 664 | 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] | 665 | ch_size>0?((const uint8_t *)buf_ptr)[0]:'?', |
| 666 | ch_size>1?((const uint8_t *)buf_ptr)[1]:'?', |
| 667 | ch_size>2?((const uint8_t *)buf_ptr)[2]:'?', |
| 668 | ch_size>3?((const uint8_t *)buf_ptr)[3]:'?', |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 669 | xml_tag, tcv); |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 670 | |
| 671 | /* Skip the extensions section */ |
| 672 | if(ctx->phase == 4) { |
| 673 | ASN_DEBUG("skip_unknown(%d, %ld)", |
| 674 | tcv, (long)ctx->left); |
| 675 | switch(xer_skip_unknown(tcv, &ctx->left)) { |
| 676 | case -1: |
| 677 | ctx->phase = 5; |
| 678 | RETURN(RC_FAIL); |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 679 | case 1: |
| 680 | ctx->phase = 3; |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 681 | /* Fall through */ |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 682 | case 0: |
| 683 | XER_ADVANCE(ch_size); |
| 684 | continue; |
| 685 | case 2: |
| 686 | ctx->phase = 3; |
| 687 | break; |
| 688 | } |
| 689 | } |
| 690 | |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 691 | switch(tcv) { |
| 692 | case XCT_BOTH: |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 693 | break; /* No CHOICE? */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 694 | case XCT_CLOSING: |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 695 | if(ctx->phase != 3) |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 696 | break; |
| 697 | XER_ADVANCE(ch_size); |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 698 | ctx->phase = 5; /* Phase out */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 699 | RETURN(RC_OK); |
| 700 | case XCT_OPENING: |
| 701 | if(ctx->phase == 0) { |
| 702 | XER_ADVANCE(ch_size); |
| 703 | ctx->phase = 1; /* Processing body phase */ |
| 704 | continue; |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 705 | } |
| 706 | /* Fall through */ |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 707 | case XCT_UNKNOWN_OP: |
| 708 | case XCT_UNKNOWN_BO: |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 709 | |
| 710 | if(ctx->phase != 1) |
| 711 | break; /* Really unexpected */ |
| 712 | |
| 713 | /* |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 714 | * Search which inner member corresponds to this tag. |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 715 | */ |
| 716 | for(edx = 0; edx < td->elements_count; edx++) { |
| 717 | elm = &td->elements[edx]; |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 718 | tcv = xer_check_tag(buf_ptr,ch_size,elm->name); |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 719 | switch(tcv) { |
| 720 | case XCT_BOTH: |
| 721 | case XCT_OPENING: |
| 722 | /* |
| 723 | * Process this member. |
| 724 | */ |
| 725 | ctx->step = edx; |
| 726 | ctx->phase = 2; |
| 727 | break; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 728 | case XCT_UNKNOWN_OP: |
| 729 | case XCT_UNKNOWN_BO: |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 730 | continue; |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 731 | default: |
| 732 | edx = td->elements_count; |
| 733 | break; /* Phase out */ |
| 734 | } |
| 735 | break; |
| 736 | } |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 737 | if(edx != td->elements_count) |
| 738 | continue; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 739 | |
| 740 | /* It is expected extension */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 741 | if(specs->ext_start != -1) { |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 742 | ASN_DEBUG("Got anticipated extension"); |
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 | * Check for (XCT_BOTH or XCT_UNKNOWN_BO) |
| 745 | * By using a mask. Only record a pure |
| 746 | * <opening> tags. |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 747 | */ |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 748 | if(tcv & XCT_CLOSING) { |
| 749 | /* Found </extension> without body */ |
| 750 | ctx->phase = 3; /* Terminating */ |
| 751 | } else { |
| 752 | ctx->left = 1; |
| 753 | ctx->phase = 4; /* Skip ...'s */ |
| 754 | } |
| 755 | XER_ADVANCE(ch_size); |
| 756 | continue; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 759 | /* Fall through */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 760 | default: |
| 761 | break; |
| 762 | } |
| 763 | |
Lev Walkin | 866c880 | 2006-09-17 08:23:35 +0000 | [diff] [blame] | 764 | ASN_DEBUG("Unexpected XML tag [%c%c%c%c] in CHOICE [%s]" |
| 765 | " (ph=%d, tag=%s)", |
| 766 | ch_size>0?((const uint8_t *)buf_ptr)[0]:'?', |
| 767 | ch_size>1?((const uint8_t *)buf_ptr)[1]:'?', |
| 768 | ch_size>2?((const uint8_t *)buf_ptr)[2]:'?', |
| 769 | ch_size>3?((const uint8_t *)buf_ptr)[3]:'?', |
| 770 | td->name, ctx->phase, xml_tag); |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 771 | break; |
| 772 | } |
| 773 | |
Lev Walkin | c34dc46 | 2005-02-18 16:23:48 +0000 | [diff] [blame] | 774 | ctx->phase = 5; /* Phase out, just in case */ |
Lev Walkin | 8d01b4c | 2004-10-23 14:57:50 +0000 | [diff] [blame] | 775 | RETURN(RC_FAIL); |
| 776 | } |
| 777 | |
| 778 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 779 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 780 | CHOICE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 781 | int ilevel, enum xer_encoder_flags_e flags, |
| 782 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 783 | asn_CHOICE_specifics_t *specs=(asn_CHOICE_specifics_t *)td->specifics; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 784 | asn_enc_rval_t er; |
Lev Walkin | 63a3523 | 2017-08-10 19:59:47 -0700 | [diff] [blame] | 785 | unsigned present; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 786 | |
| 787 | if(!sptr) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 788 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 789 | |
| 790 | /* |
| 791 | * Figure out which CHOICE element is encoded. |
| 792 | */ |
| 793 | present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size); |
| 794 | |
Lev Walkin | 63a3523 | 2017-08-10 19:59:47 -0700 | [diff] [blame] | 795 | if(present == 0 || present > td->elements_count) { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 796 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 797 | } else { |
| 798 | asn_enc_rval_t tmper; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 799 | asn_TYPE_member_t *elm = &td->elements[present-1]; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 800 | void *memb_ptr; |
| 801 | const char *mname = elm->name; |
| 802 | unsigned int mlen = strlen(mname); |
| 803 | |
| 804 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 805 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 806 | if(!memb_ptr) ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 807 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 808 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | er.encoded = 0; |
| 812 | |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 813 | if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 814 | ASN__CALLBACK3("<", 1, mname, mlen, ">", 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 815 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 816 | tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 817 | ilevel + 1, flags, cb, app_key); |
| 818 | if(tmper.encoded == -1) return tmper; |
| 819 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 820 | ASN__CALLBACK3("</", 2, mname, mlen, ">", 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 821 | |
| 822 | er.encoded += 5 + (2 * mlen) + tmper.encoded; |
| 823 | } |
| 824 | |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 825 | if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel - 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 826 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 827 | ASN__ENCODED_OK(er); |
Lev Walkin | 942fd08 | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 828 | cb_failed: |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 829 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 832 | asn_dec_rval_t |
| 833 | 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] | 834 | const asn_per_constraints_t *constraints, void **sptr, |
| 835 | asn_per_data_t *pd) { |
| 836 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 837 | asn_dec_rval_t rv; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 838 | const asn_per_constraint_t *ct; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 839 | asn_TYPE_member_t *elm; /* CHOICE's element */ |
| 840 | void *memb_ptr; |
| 841 | void **memb_ptr2; |
| 842 | void *st = *sptr; |
| 843 | int value; |
| 844 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 845 | if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx)) |
| 846 | ASN__DECODE_FAILED; |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 847 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 848 | /* |
| 849 | * Create the target structure if it is not present already. |
| 850 | */ |
| 851 | if(!st) { |
| 852 | st = *sptr = CALLOC(1, specs->struct_size); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 853 | if(!st) ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | if(constraints) ct = &constraints->value; |
| 857 | else if(td->per_constraints) ct = &td->per_constraints->value; |
| 858 | else ct = 0; |
| 859 | |
| 860 | if(ct && ct->flags & APC_EXTENSIBLE) { |
| 861 | value = per_get_few_bits(pd, 1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 862 | if(value < 0) ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 863 | if(value) ct = 0; /* Not restricted */ |
| 864 | } |
| 865 | |
| 866 | if(ct && ct->range_bits >= 0) { |
| 867 | value = per_get_few_bits(pd, ct->range_bits); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 868 | if(value < 0) ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 869 | ASN_DEBUG("CHOICE %s got index %d in range %d", |
| 870 | td->name, value, ct->range_bits); |
Lev Walkin | 0a8aa60 | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 871 | if(value > ct->upper_bound) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 872 | ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 873 | } else { |
| 874 | if(specs->ext_start == -1) |
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 | value = uper_get_nsnnwn(pd); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 877 | if(value < 0) ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 878 | value += specs->ext_start; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 879 | if((unsigned)value >= td->elements_count) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 880 | ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /* Adjust if canonical order is different from natural order */ |
| 884 | if(specs->canonical_order) |
| 885 | value = specs->canonical_order[value]; |
| 886 | |
| 887 | /* Set presence to be able to free it later */ |
| 888 | _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1); |
| 889 | |
| 890 | elm = &td->elements[value]; |
| 891 | if(elm->flags & ATF_POINTER) { |
| 892 | /* Member is a pointer to another structure */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 893 | memb_ptr2 = (void **)((char *)st + elm->memb_offset); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 894 | } else { |
| 895 | memb_ptr = (char *)st + elm->memb_offset; |
| 896 | memb_ptr2 = &memb_ptr; |
| 897 | } |
| 898 | ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name); |
| 899 | |
Lev Walkin | 6f9060f | 2007-06-29 01:45:03 +0000 | [diff] [blame] | 900 | if(ct && ct->range_bits >= 0) { |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 901 | rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type, |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 902 | elm->per_constraints, memb_ptr2, pd); |
Lev Walkin | 6f9060f | 2007-06-29 01:45:03 +0000 | [diff] [blame] | 903 | } else { |
| 904 | rv = uper_open_type_get(opt_codec_ctx, elm->type, |
| 905 | elm->per_constraints, memb_ptr2, pd); |
| 906 | } |
| 907 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 908 | if(rv.code != RC_OK) |
Lev Walkin | 6f9060f | 2007-06-29 01:45:03 +0000 | [diff] [blame] | 909 | ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d", |
| 910 | elm->name, td->name, rv.code); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 911 | return rv; |
| 912 | } |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 913 | |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 914 | asn_enc_rval_t |
| 915 | CHOICE_encode_uper(asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 916 | const asn_per_constraints_t *constraints, void *sptr, |
| 917 | asn_per_outp_t *po) { |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 918 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 919 | asn_TYPE_member_t *elm; /* CHOICE's element */ |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 920 | const asn_per_constraint_t *ct; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 921 | void *memb_ptr; |
Lev Walkin | 63a3523 | 2017-08-10 19:59:47 -0700 | [diff] [blame] | 922 | unsigned present; |
Lev Walkin | e14480f | 2013-03-24 03:28:00 -0700 | [diff] [blame] | 923 | int present_enc; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 924 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 925 | if(!sptr) ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 926 | |
| 927 | ASN_DEBUG("Encoding %s as CHOICE", td->name); |
| 928 | |
| 929 | if(constraints) ct = &constraints->value; |
| 930 | else if(td->per_constraints) ct = &td->per_constraints->value; |
| 931 | else ct = 0; |
| 932 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 933 | present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size); |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 934 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 935 | /* |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 936 | * If the structure was not initialized properly, it cannot be encoded: |
| 937 | * can't deduce what to encode in the choice type. |
| 938 | */ |
Lev Walkin | 63a3523 | 2017-08-10 19:59:47 -0700 | [diff] [blame] | 939 | if(present == 0 || present > td->elements_count) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 940 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 941 | else |
| 942 | present--; |
| 943 | |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 944 | ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present); |
| 945 | |
Lev Walkin | e14480f | 2013-03-24 03:28:00 -0700 | [diff] [blame] | 946 | /* Adjust if canonical order is different from natural order */ |
| 947 | if(specs->canonical_order) |
| 948 | present_enc = specs->canonical_order[present]; |
| 949 | else |
| 950 | present_enc = present; |
| 951 | |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 952 | if(ct && ct->range_bits >= 0) { |
Lev Walkin | e14480f | 2013-03-24 03:28:00 -0700 | [diff] [blame] | 953 | if(present_enc < ct->lower_bound |
| 954 | || present_enc > ct->upper_bound) { |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 955 | if(ct->flags & APC_EXTENSIBLE) { |
| 956 | if(per_put_few_bits(po, 1, 1)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 957 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 958 | } else { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 959 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 960 | } |
| 961 | ct = 0; |
| 962 | } |
| 963 | } |
| 964 | if(ct && ct->flags & APC_EXTENSIBLE) |
| 965 | if(per_put_few_bits(po, 0, 1)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 966 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 967 | |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 968 | elm = &td->elements[present]; |
| 969 | if(elm->flags & ATF_POINTER) { |
| 970 | /* Member is a pointer to another structure */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 971 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 972 | if(!memb_ptr) ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 973 | } else { |
| 974 | memb_ptr = (char *)sptr + elm->memb_offset; |
| 975 | } |
| 976 | |
Lev Walkin | 6f9060f | 2007-06-29 01:45:03 +0000 | [diff] [blame] | 977 | if(ct && ct->range_bits >= 0) { |
Lev Walkin | e14480f | 2013-03-24 03:28:00 -0700 | [diff] [blame] | 978 | if(per_put_few_bits(po, present_enc, ct->range_bits)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 979 | ASN__ENCODE_FAILED; |
Lev Walkin | 6f9060f | 2007-06-29 01:45:03 +0000 | [diff] [blame] | 980 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 981 | return elm->type->op->uper_encoder(elm->type, elm->per_constraints, |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 982 | memb_ptr, po); |
Lev Walkin | 6f9060f | 2007-06-29 01:45:03 +0000 | [diff] [blame] | 983 | } else { |
| 984 | asn_enc_rval_t rval; |
| 985 | if(specs->ext_start == -1) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 986 | ASN__ENCODE_FAILED; |
Lev Walkin | e14480f | 2013-03-24 03:28:00 -0700 | [diff] [blame] | 987 | if(uper_put_nsnnwn(po, present_enc - specs->ext_start)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 988 | ASN__ENCODE_FAILED; |
Lev Walkin | 6f9060f | 2007-06-29 01:45:03 +0000 | [diff] [blame] | 989 | if(uper_open_type_put(elm->type, elm->per_constraints, |
| 990 | memb_ptr, po)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 991 | ASN__ENCODE_FAILED; |
Lev Walkin | 6f9060f | 2007-06-29 01:45:03 +0000 | [diff] [blame] | 992 | rval.encoded = 0; |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 993 | ASN__ENCODED_OK(rval); |
Lev Walkin | 6f9060f | 2007-06-29 01:45:03 +0000 | [diff] [blame] | 994 | } |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 997 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 998 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 999 | CHOICE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1000 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 1001 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
Lev Walkin | 63a3523 | 2017-08-10 19:59:47 -0700 | [diff] [blame] | 1002 | unsigned present; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1003 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 1004 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1005 | |
| 1006 | /* |
| 1007 | * Figure out which CHOICE element is encoded. |
| 1008 | */ |
| 1009 | present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size); |
| 1010 | |
| 1011 | /* |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1012 | * Print that element. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1013 | */ |
Lev Walkin | 63a3523 | 2017-08-10 19:59:47 -0700 | [diff] [blame] | 1014 | if(present > 0 && present <= td->elements_count) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 1015 | asn_TYPE_member_t *elm = &td->elements[present-1]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1016 | const void *memb_ptr; |
| 1017 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 1018 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1019 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 1020 | if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1021 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1022 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /* Print member's name and stuff */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 1026 | if(0) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 1027 | if(cb(elm->name, strlen(elm->name), app_key) < 0 |
| 1028 | || cb(": ", 2, app_key) < 0) |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 1029 | return -1; |
| 1030 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1031 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1032 | return elm->type->op->print_struct(elm->type, memb_ptr, ilevel, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1033 | cb, app_key); |
| 1034 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 1035 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | void |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 1040 | CHOICE_free(const asn_TYPE_descriptor_t *td, void *ptr, |
| 1041 | enum asn_struct_free_method method) { |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 1042 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
Lev Walkin | 63a3523 | 2017-08-10 19:59:47 -0700 | [diff] [blame] | 1043 | unsigned present; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1044 | |
| 1045 | if(!td || !ptr) |
| 1046 | return; |
| 1047 | |
| 1048 | ASN_DEBUG("Freeing %s as CHOICE", td->name); |
| 1049 | |
| 1050 | /* |
| 1051 | * Figure out which CHOICE element is encoded. |
| 1052 | */ |
| 1053 | present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size); |
| 1054 | |
| 1055 | /* |
| 1056 | * Free that element. |
| 1057 | */ |
Lev Walkin | 63a3523 | 2017-08-10 19:59:47 -0700 | [diff] [blame] | 1058 | if(present > 0 && present <= td->elements_count) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 1059 | asn_TYPE_member_t *elm = &td->elements[present-1]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1060 | void *memb_ptr; |
| 1061 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 1062 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1063 | memb_ptr = *(void **)((char *)ptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1064 | if(memb_ptr) |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 1065 | ASN_STRUCT_FREE(*elm->type, memb_ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1066 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1067 | memb_ptr = (void *)((char *)ptr + elm->memb_offset); |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 1068 | ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 1072 | switch(method) { |
| 1073 | case ASFM_FREE_EVERYTHING: |
| 1074 | FREEMEM(ptr); |
| 1075 | break; |
| 1076 | case ASFM_FREE_UNDERLYING: |
| 1077 | break; |
| 1078 | case ASFM_FREE_UNDERLYING_AND_RESET: |
| 1079 | memset(ptr, 0, specs->struct_size); |
| 1080 | break; |
| 1081 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | /* |
| 1086 | * The following functions functions offer protection against -fshort-enums, |
| 1087 | * compatible with little- and big-endian machines. |
| 1088 | * If assertion is triggered, either disable -fshort-enums, or add an entry |
| 1089 | * here with the ->pres_size of your target stracture. |
| 1090 | * Unless the target structure is packed, the ".present" member |
| 1091 | * is guaranteed to be aligned properly. ASN.1 compiler itself does not |
| 1092 | * produce packed code. |
| 1093 | */ |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 1094 | static unsigned |
| 1095 | _fetch_present_idx(const void *struct_ptr, unsigned pres_offset, |
| 1096 | unsigned pres_size) { |
| 1097 | const void *present_ptr; |
Lev Walkin | 63a3523 | 2017-08-10 19:59:47 -0700 | [diff] [blame] | 1098 | unsigned present; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1099 | |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1100 | present_ptr = ((const char *)struct_ptr) + pres_offset; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1101 | |
| 1102 | switch(pres_size) { |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 1103 | case sizeof(int): present = *(const unsigned int *)present_ptr; break; |
| 1104 | case sizeof(short): present = *(const unsigned short *)present_ptr; break; |
| 1105 | case sizeof(char): present = *(const unsigned char *)present_ptr; break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1106 | default: |
| 1107 | /* ANSI C mandates enum to be equivalent to integer */ |
| 1108 | assert(pres_size != sizeof(int)); |
| 1109 | return 0; /* If not aborted, pass back safe value */ |
| 1110 | } |
| 1111 | |
| 1112 | return present; |
| 1113 | } |
| 1114 | |
Lev Walkin | 91f5cd0 | 2004-08-11 09:10:59 +0000 | [diff] [blame] | 1115 | static void |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 1116 | _set_present_idx(void *struct_ptr, unsigned pres_offset, unsigned pres_size, |
| 1117 | unsigned present) { |
| 1118 | void *present_ptr; |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1119 | present_ptr = ((char *)struct_ptr) + pres_offset; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1120 | |
| 1121 | switch(pres_size) { |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 1122 | case sizeof(int): *(unsigned int *)present_ptr = present; break; |
| 1123 | case sizeof(short): *(unsigned short *)present_ptr = present; break; |
| 1124 | case sizeof(char): *(unsigned char *)present_ptr = present; break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1125 | default: |
| 1126 | /* ANSI C mandates enum to be equivalent to integer */ |
| 1127 | assert(pres_size != sizeof(int)); |
| 1128 | } |
| 1129 | } |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 1130 | |
| 1131 | static const void * |
| 1132 | _get_member_ptr(const asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | 63a3523 | 2017-08-10 19:59:47 -0700 | [diff] [blame] | 1133 | asn_TYPE_member_t **elm_ptr, unsigned *present_out) { |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 1134 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
Lev Walkin | 63a3523 | 2017-08-10 19:59:47 -0700 | [diff] [blame] | 1135 | unsigned present; |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 1136 | |
| 1137 | if(!sptr) { |
| 1138 | *elm_ptr = NULL; |
| 1139 | *present_out = 0; |
| 1140 | return NULL; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * Figure out which CHOICE element is encoded. |
| 1145 | */ |
| 1146 | present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size); |
| 1147 | *present_out = present; |
| 1148 | |
| 1149 | /* |
| 1150 | * The presence index is intentionally 1-based to avoid |
| 1151 | * treating zeroed structure as a valid one. |
| 1152 | */ |
Lev Walkin | 63a3523 | 2017-08-10 19:59:47 -0700 | [diff] [blame] | 1153 | if(present > 0 && present <= td->elements_count) { |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 1154 | asn_TYPE_member_t *const elm = &td->elements[present - 1]; |
| 1155 | const void *memb_ptr; |
| 1156 | |
| 1157 | if(elm->flags & ATF_POINTER) { |
| 1158 | memb_ptr = |
| 1159 | *(const void *const *)((const char *)sptr + elm->memb_offset); |
| 1160 | } else { |
| 1161 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 1162 | } |
| 1163 | *elm_ptr = elm; |
| 1164 | return memb_ptr; |
| 1165 | } else { |
| 1166 | *elm_ptr = NULL; |
| 1167 | return NULL; |
| 1168 | } |
| 1169 | |
| 1170 | } |
| 1171 | |
| 1172 | int |
| 1173 | CHOICE_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) { |
| 1174 | asn_TYPE_member_t *aelm; |
| 1175 | asn_TYPE_member_t *belm; |
Lev Walkin | 63a3523 | 2017-08-10 19:59:47 -0700 | [diff] [blame] | 1176 | unsigned apresent = 0; |
| 1177 | unsigned bpresent = 0; |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 1178 | const void *amember = _get_member_ptr(td, aptr, &aelm, &apresent); |
| 1179 | const void *bmember = _get_member_ptr(td, bptr, &belm, &apresent); |
| 1180 | |
| 1181 | if(amember && bmember) { |
| 1182 | if(apresent == bpresent) { |
| 1183 | assert(aelm == belm); |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1184 | return aelm->type->op->compare_struct(aelm->type, amember, bmember); |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 1185 | } else if(apresent < bpresent) { |
| 1186 | return -1; |
| 1187 | } else { |
| 1188 | return 1; |
| 1189 | } |
| 1190 | } else if(!amember) { |
| 1191 | return -1; |
| 1192 | } else { |
| 1193 | return 1; |
| 1194 | } |
| 1195 | } |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 1196 | |
| 1197 | /* |
| 1198 | * Return the 1-based choice variant presence index. |
| 1199 | * Returns 0 in case of error. |
| 1200 | */ |
| 1201 | unsigned |
| 1202 | CHOICE_variant_get_presence(const asn_TYPE_descriptor_t *td, const void *sptr) { |
| 1203 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
| 1204 | return _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size); |
| 1205 | } |
| 1206 | |
| 1207 | /* |
| 1208 | * Sets or resets the 1-based choice variant presence index. |
| 1209 | * In case a previous index is not zero, the currently selected structure |
| 1210 | * member is freed and zeroed-out first. |
| 1211 | * Returns 0 on success and -1 on error. |
| 1212 | */ |
| 1213 | int |
| 1214 | CHOICE_variant_set_presence(const asn_TYPE_descriptor_t *td, void *sptr, |
| 1215 | unsigned present) { |
| 1216 | extern asn_CHOICE_specifics_t asn_SPC_value_specs_3; |
| 1217 | asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; |
| 1218 | unsigned old_present; |
| 1219 | |
| 1220 | if(!sptr) { |
| 1221 | return -1; |
| 1222 | } |
| 1223 | |
| 1224 | if(present > td->elements_count) |
| 1225 | return -1; |
| 1226 | |
| 1227 | old_present = |
| 1228 | _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size); |
| 1229 | if(present == old_present) |
| 1230 | return 0; |
| 1231 | |
Lev Walkin | 91ca273 | 2017-08-11 02:32:42 -0700 | [diff] [blame] | 1232 | if(old_present != 0) { |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 1233 | assert(old_present <= td->elements_count); |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 1234 | ASN_STRUCT_RESET(*td, sptr); |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | _set_present_idx(sptr, specs->pres_offset, specs->pres_size, present); |
| 1238 | |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1242 | asn_TYPE_operation_t asn_OP_CHOICE = { |
| 1243 | CHOICE_free, |
| 1244 | CHOICE_print, |
| 1245 | CHOICE_compare, |
| 1246 | CHOICE_constraint, |
| 1247 | CHOICE_decode_ber, |
| 1248 | CHOICE_encode_der, |
| 1249 | CHOICE_decode_xer, |
| 1250 | CHOICE_encode_xer, |
| 1251 | #ifdef ASN_DISABLE_OER_SUPPORT |
| 1252 | 0, |
| 1253 | 0, |
| 1254 | #else |
Lev Walkin | 9a1736d | 2017-08-27 23:46:34 -0700 | [diff] [blame] | 1255 | CHOICE_decode_oer, |
| 1256 | CHOICE_encode_oer, |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1257 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
| 1258 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 1259 | 0, |
| 1260 | 0, |
| 1261 | #else |
| 1262 | CHOICE_decode_uper, |
| 1263 | CHOICE_encode_uper, |
| 1264 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
| 1265 | CHOICE_outmost_tag |
| 1266 | }; |