Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 2 | * Copyright (c) 2003-2017 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_SEQUENCE.h> |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 8 | #include <OPEN_TYPE.h> |
Lev Walkin | 9218bc1 | 2007-06-27 04:09:37 +0000 | [diff] [blame] | 9 | #include <per_opentype.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 10 | |
| 11 | /* |
| 12 | * Number of bytes left for this structure. |
| 13 | * (ctx->left) indicates the number of bytes _transferred_ for the structure. |
| 14 | * (size) contains the number of bytes in the buffer passed. |
| 15 | */ |
Lev Walkin | ec1ffd4 | 2004-08-18 04:53:32 +0000 | [diff] [blame] | 16 | #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * If the subprocessor function returns with an indication that it wants |
| 20 | * more data, it may well be a fatal decoding problem, because the |
| 21 | * size is constrained by the <TLV>'s L, even if the buffer size allows |
| 22 | * reading more data. |
| 23 | * For example, consider the buffer containing the following TLVs: |
| 24 | * <T:5><L:1><V> <T:6>... |
| 25 | * The TLV length clearly indicates that one byte is expected in V, but |
| 26 | * if the V processor returns with "want more data" even if the buffer |
| 27 | * contains way more data than the V processor have seen. |
| 28 | */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 29 | #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * This macro "eats" the part of the buffer which is definitely "consumed", |
| 33 | * i.e. was correctly converted into local representation or rightfully skipped. |
| 34 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 35 | #undef ADVANCE |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 36 | #define ADVANCE(num_bytes) do { \ |
| 37 | size_t num = num_bytes; \ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 38 | ptr = ((const char *)ptr) + num; \ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 39 | size -= num; \ |
| 40 | if(ctx->left >= 0) \ |
| 41 | ctx->left -= num; \ |
| 42 | consumed_myself += num; \ |
| 43 | } while(0) |
| 44 | |
| 45 | /* |
| 46 | * Switch to the next phase of parsing. |
| 47 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 48 | #undef NEXT_PHASE |
| 49 | #undef PHASE_OUT |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 50 | #define NEXT_PHASE(ctx) do { \ |
| 51 | ctx->phase++; \ |
| 52 | ctx->step = 0; \ |
| 53 | } while(0) |
| 54 | #define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0) |
| 55 | |
| 56 | /* |
| 57 | * Return a standardized complex structure. |
| 58 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 59 | #undef RETURN |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | #define RETURN(_code) do { \ |
| 61 | rval.code = _code; \ |
| 62 | rval.consumed = consumed_myself;\ |
| 63 | return rval; \ |
| 64 | } while(0) |
| 65 | |
| 66 | /* |
| 67 | * Check whether we are inside the extensions group. |
| 68 | */ |
| 69 | #define IN_EXTENSION_GROUP(specs, memb_idx) \ |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 70 | ( (((signed)(memb_idx)) > (specs)->ext_after) \ |
| 71 | &&(((signed)(memb_idx)) < (specs)->ext_before)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 72 | |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * Tags are canonically sorted in the tag2element map. |
| 76 | */ |
| 77 | static int |
| 78 | _t2e_cmp(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 | 4c36e30 | 2004-06-06 07:20:02 +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 | |
Lev Walkin | 924fa03 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 89 | if(a_value == b_value) { |
Lev Walkin | 50299c1 | 2004-06-14 14:11:14 +0000 | [diff] [blame] | 90 | if(a->el_no > b->el_no) |
| 91 | return 1; |
Lev Walkin | 924fa03 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 92 | /* |
| 93 | * Important: we do not check |
Lev Walkin | 50299c1 | 2004-06-14 14:11:14 +0000 | [diff] [blame] | 94 | * for a->el_no <= b->el_no! |
Lev Walkin | 924fa03 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 95 | */ |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 96 | return 0; |
Lev Walkin | 924fa03 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 97 | } else if(a_value < b_value) |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 98 | return -1; |
| 99 | else |
| 100 | return 1; |
| 101 | } else if(a_class < b_class) { |
| 102 | return -1; |
| 103 | } else { |
| 104 | return 1; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 109 | /* |
| 110 | * The decoder of the SEQUENCE type. |
| 111 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 112 | asn_dec_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 113 | SEQUENCE_decode_ber(const asn_codec_ctx_t *opt_codec_ctx, |
| 114 | const asn_TYPE_descriptor_t *td, void **struct_ptr, |
| 115 | const void *ptr, size_t size, int tag_mode) { |
| 116 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 117 | * Bring closer parts of structure description. |
| 118 | */ |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 119 | const asn_SEQUENCE_specifics_t *specs = (const asn_SEQUENCE_specifics_t *)td->specifics; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 120 | const asn_TYPE_member_t *elements = td->elements; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 121 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 122 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 123 | * Parts of the structure being constructed. |
| 124 | */ |
| 125 | void *st = *struct_ptr; /* Target structure. */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 126 | asn_struct_ctx_t *ctx; /* Decoder context */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | |
| 128 | ber_tlv_tag_t tlv_tag; /* T from TLV */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 129 | asn_dec_rval_t rval; /* Return code from subparsers */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 130 | |
| 131 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 132 | size_t edx; /* SEQUENCE element's index */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 133 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 134 | ASN_DEBUG("Decoding %s as SEQUENCE", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * Create the target structure if it is not present already. |
| 138 | */ |
| 139 | if(st == 0) { |
| 140 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 141 | if(st == 0) { |
| 142 | RETURN(RC_FAIL); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Restore parsing context. |
| 148 | */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 149 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * Start to parse where left previously |
| 153 | */ |
| 154 | switch(ctx->phase) { |
| 155 | case 0: |
| 156 | /* |
| 157 | * PHASE 0. |
| 158 | * Check that the set of tags associated with given structure |
| 159 | * perfectly fits our expectations. |
| 160 | */ |
| 161 | |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 162 | rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 163 | tag_mode, 1, &ctx->left, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 164 | if(rval.code != RC_OK) { |
| 165 | ASN_DEBUG("%s tagging check failed: %d", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 166 | td->name, rval.code); |
Lev Walkin | 566a567 | 2004-10-05 06:36:57 +0000 | [diff] [blame] | 167 | return rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | if(ctx->left >= 0) |
| 171 | ctx->left += rval.consumed; /* ?Substracted below! */ |
| 172 | ADVANCE(rval.consumed); |
| 173 | |
| 174 | NEXT_PHASE(ctx); |
| 175 | |
| 176 | ASN_DEBUG("Structure consumes %ld bytes, buffer %ld", |
| 177 | (long)ctx->left, (long)size); |
| 178 | |
| 179 | /* Fall through */ |
| 180 | case 1: |
| 181 | /* |
| 182 | * PHASE 1. |
| 183 | * From the place where we've left it previously, |
| 184 | * try to decode the next member from the list of |
| 185 | * this structure's elements. |
| 186 | * (ctx->step) stores the member being processed |
| 187 | * between invocations and the microphase {0,1} of parsing |
| 188 | * that member: |
| 189 | * step = (<member_number> * 2 + <microphase>). |
| 190 | */ |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 191 | for(edx = ((size_t)ctx->step >> 1); edx < td->elements_count; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 192 | edx++, ctx->step = (ctx->step & ~1) + 2) { |
| 193 | void *memb_ptr; /* Pointer to the member */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 194 | void **memb_ptr2; /* Pointer to that pointer */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 195 | ssize_t tag_len; /* Length of TLV's T */ |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 196 | size_t opt_edx_end; /* Next non-optional element */ |
| 197 | size_t n; |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 198 | int use_bsearch; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 199 | |
| 200 | if(ctx->step & 1) |
| 201 | goto microphase2; |
| 202 | |
| 203 | /* |
| 204 | * MICROPHASE 1: Synchronize decoding. |
| 205 | */ |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 206 | ASN_DEBUG("In %s SEQUENCE left %d, edx=%zu flags=%d" |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 207 | " opt=%d ec=%d", |
| 208 | td->name, (int)ctx->left, edx, |
| 209 | elements[edx].flags, elements[edx].optional, |
| 210 | td->elements_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 211 | |
| 212 | if(ctx->left == 0 /* No more stuff is expected */ |
| 213 | && ( |
| 214 | /* Explicit OPTIONAL specification reaches the end */ |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 215 | (edx + elements[edx].optional |
| 216 | == td->elements_count) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 217 | || |
| 218 | /* All extensions are optional */ |
| 219 | (IN_EXTENSION_GROUP(specs, edx) |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 220 | && specs->ext_before > (signed)td->elements_count) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 221 | ) |
| 222 | ) { |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 223 | ASN_DEBUG("End of SEQUENCE %s", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 224 | /* |
| 225 | * Found the legitimate end of the structure. |
| 226 | */ |
| 227 | PHASE_OUT(ctx); |
| 228 | RETURN(RC_OK); |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Fetch the T from TLV. |
| 233 | */ |
| 234 | tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 235 | ASN_DEBUG("Current tag in %s SEQUENCE for element %zu " |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 236 | "(%s) is %s encoded in %d bytes, of frame %ld", |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 237 | td->name, edx, elements[edx].name, |
| 238 | ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 239 | switch(tag_len) { |
| 240 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 241 | /* Fall through */ |
| 242 | case -1: RETURN(RC_FAIL); |
| 243 | } |
| 244 | |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 245 | if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) { |
Lev Walkin | 566a567 | 2004-10-05 06:36:57 +0000 | [diff] [blame] | 246 | if(LEFT < 2) { |
| 247 | if(SIZE_VIOLATION) |
| 248 | RETURN(RC_FAIL); |
| 249 | else |
| 250 | RETURN(RC_WMORE); |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 251 | } else if(((const uint8_t *)ptr)[1] == 0) { |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 252 | ASN_DEBUG("edx = %zu, opt = %d, ec=%d", |
Lev Walkin | 566a567 | 2004-10-05 06:36:57 +0000 | [diff] [blame] | 253 | edx, elements[edx].optional, |
| 254 | td->elements_count); |
| 255 | if((edx + elements[edx].optional |
| 256 | == td->elements_count) |
| 257 | || (IN_EXTENSION_GROUP(specs, edx) |
| 258 | && specs->ext_before |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 259 | > (signed)td->elements_count)) { |
Lev Walkin | 566a567 | 2004-10-05 06:36:57 +0000 | [diff] [blame] | 260 | /* |
| 261 | * Yeah, baby! Found the terminator |
| 262 | * of the indefinite length structure. |
| 263 | */ |
| 264 | /* |
| 265 | * Proceed to the canonical |
| 266 | * finalization function. |
| 267 | * No advancing is necessary. |
| 268 | */ |
| 269 | goto phase3; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 274 | /* |
| 275 | * Find the next available type with this tag. |
| 276 | */ |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 277 | use_bsearch = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 278 | opt_edx_end = edx + elements[edx].optional + 1; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 279 | if(opt_edx_end > td->elements_count) |
| 280 | opt_edx_end = td->elements_count; /* Cap */ |
Lev Walkin | f9dbd9a | 2004-07-01 00:48:04 +0000 | [diff] [blame] | 281 | else if(opt_edx_end - edx > 8) { |
Lev Walkin | 924fa03 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 282 | /* Limit the scope of linear search... */ |
Lev Walkin | f9dbd9a | 2004-07-01 00:48:04 +0000 | [diff] [blame] | 283 | opt_edx_end = edx + 8; |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 284 | use_bsearch = 1; |
Lev Walkin | 924fa03 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 285 | /* ... and resort to bsearch() */ |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 286 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 287 | for(n = edx; n < opt_edx_end; n++) { |
| 288 | if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) { |
| 289 | /* |
| 290 | * Found element corresponding to the tag |
| 291 | * being looked at. |
| 292 | * Reposition over the right element. |
| 293 | */ |
| 294 | edx = n; |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 295 | ctx->step = 1 + 2 * edx; /* Remember! */ |
| 296 | goto microphase2; |
Lev Walkin | 14fd3e5 | 2017-08-27 01:38:45 -0700 | [diff] [blame] | 297 | } else if(elements[n].flags & ATF_ANY_TYPE) { |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 298 | /* |
| 299 | * This is the ANY type, which may bear |
| 300 | * any flag whatsoever. |
| 301 | */ |
| 302 | edx = n; |
| 303 | ctx->step = 1 + 2 * edx; /* Remember! */ |
| 304 | goto microphase2; |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 305 | } else if(elements[n].tag == (ber_tlv_tag_t)-1) { |
| 306 | use_bsearch = 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 307 | break; |
| 308 | } |
| 309 | } |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 310 | if(use_bsearch) { |
| 311 | /* |
Lev Walkin | 4e0704f | 2004-09-04 06:17:35 +0000 | [diff] [blame] | 312 | * Resort to a binary search over |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 313 | * sorted array of tags. |
| 314 | */ |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 315 | const asn_TYPE_tag2member_t *t2m; |
Lev Walkin | eff98a5 | 2017-09-13 22:24:35 +0000 | [diff] [blame] | 316 | asn_TYPE_tag2member_t key = {0, 0, 0, 0}; |
| 317 | key.el_tag = tlv_tag; |
| 318 | key.el_no = edx; |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 319 | t2m = (const asn_TYPE_tag2member_t *)bsearch(&key, |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 320 | specs->tag2el, specs->tag2el_count, |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 321 | sizeof(specs->tag2el[0]), _t2e_cmp); |
Lev Walkin | 924fa03 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 322 | if(t2m) { |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 323 | const asn_TYPE_tag2member_t *best = 0; |
| 324 | const asn_TYPE_tag2member_t *t2m_f, *t2m_l; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 325 | size_t edx_max = edx + elements[edx].optional; |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 326 | /* |
| 327 | * Rewind to the first element with that tag, |
| 328 | * `cause bsearch() does not guarantee order. |
| 329 | */ |
Lev Walkin | 924fa03 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 330 | t2m_f = t2m + t2m->toff_first; |
| 331 | t2m_l = t2m + t2m->toff_last; |
| 332 | for(t2m = t2m_f; t2m <= t2m_l; t2m++) { |
| 333 | if(t2m->el_no > edx_max) break; |
| 334 | if(t2m->el_no < edx) continue; |
| 335 | best = t2m; |
| 336 | } |
| 337 | if(best) { |
| 338 | edx = best->el_no; |
| 339 | ctx->step = 1 + 2 * edx; |
| 340 | goto microphase2; |
| 341 | } |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 342 | } |
| 343 | n = opt_edx_end; |
| 344 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 345 | if(n == opt_edx_end) { |
| 346 | /* |
| 347 | * If tag is unknown, it may be either |
| 348 | * an unknown (thus, incorrect) tag, |
| 349 | * or an extension (...), |
| 350 | * or an end of the indefinite-length structure. |
| 351 | */ |
Lev Walkin | 7cbbc90 | 2006-10-03 06:39:36 +0000 | [diff] [blame] | 352 | if(!IN_EXTENSION_GROUP(specs, |
| 353 | edx + elements[edx].optional)) { |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 354 | ASN_DEBUG("Unexpected tag %s (at %zu)", |
Lev Walkin | 566a567 | 2004-10-05 06:36:57 +0000 | [diff] [blame] | 355 | ber_tlv_tag_string(tlv_tag), edx); |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 356 | ASN_DEBUG("Expected tag %s (%s)%s", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 357 | ber_tlv_tag_string(elements[edx].tag), |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 358 | elements[edx].name, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 359 | elements[edx].optional |
| 360 | ?" or alternatives":""); |
| 361 | RETURN(RC_FAIL); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 362 | } else { |
| 363 | /* Skip this tag */ |
| 364 | ssize_t skip; |
Lev Walkin | 7cbbc90 | 2006-10-03 06:39:36 +0000 | [diff] [blame] | 365 | edx += elements[edx].optional; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 366 | |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 367 | ASN_DEBUG("Skipping unexpected %s (at %zu)", |
Lev Walkin | 7cbbc90 | 2006-10-03 06:39:36 +0000 | [diff] [blame] | 368 | ber_tlv_tag_string(tlv_tag), edx); |
Lev Walkin | baaa24f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 369 | skip = ber_skip_length(opt_codec_ctx, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 370 | BER_TLV_CONSTRUCTED(ptr), |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 371 | (const char *)ptr + tag_len, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 372 | LEFT - tag_len); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 373 | ASN_DEBUG("Skip length %d in %s", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 374 | (int)skip, td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 375 | switch(skip) { |
| 376 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 377 | /* Fall through */ |
| 378 | case -1: RETURN(RC_FAIL); |
| 379 | } |
| 380 | |
| 381 | ADVANCE(skip + tag_len); |
| 382 | ctx->step -= 2; |
| 383 | edx--; |
| 384 | continue; /* Try again with the next tag */ |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * MICROPHASE 2: Invoke the member-specific decoder. |
| 390 | */ |
| 391 | ctx->step |= 1; /* Confirm entering next microphase */ |
| 392 | microphase2: |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 393 | ASN_DEBUG("Inside SEQUENCE %s MF2", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 394 | |
| 395 | /* |
| 396 | * Compute the position of the member inside a structure, |
| 397 | * and also a type of containment (it may be contained |
| 398 | * as pointer or using inline inclusion). |
| 399 | */ |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 400 | if(elements[edx].flags & ATF_POINTER) { |
| 401 | /* Member is a pointer to another structure */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 402 | memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 403 | } else { |
| 404 | /* |
| 405 | * A pointer to a pointer |
| 406 | * holding the start of the structure |
| 407 | */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 408 | memb_ptr = (char *)st + elements[edx].memb_offset; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 409 | memb_ptr2 = &memb_ptr; |
| 410 | } |
| 411 | /* |
| 412 | * Invoke the member fetch routine according to member's type |
| 413 | */ |
Lev Walkin | 75b6fe4 | 2017-08-27 01:03:22 -0700 | [diff] [blame] | 414 | if(elements[edx].flags & ATF_OPEN_TYPE) { |
Lev Walkin | c10d30a | 2017-08-27 00:38:21 -0700 | [diff] [blame] | 415 | rval = OPEN_TYPE_ber_get(opt_codec_ctx, td, st, &elements[edx], ptr, LEFT); |
| 416 | } else { |
| 417 | rval = elements[edx].type->op->ber_decoder(opt_codec_ctx, |
| 418 | elements[edx].type, |
| 419 | memb_ptr2, ptr, LEFT, |
| 420 | elements[edx].tag_mode); |
| 421 | } |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 422 | ASN_DEBUG("In %s SEQUENCE decoded %zu %s of %d " |
Lev Walkin | 5beb7de | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 423 | "in %d bytes rval.code %d, size=%d", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 424 | td->name, edx, elements[edx].type->name, |
Lev Walkin | 5beb7de | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 425 | (int)LEFT, (int)rval.consumed, rval.code, (int)size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 426 | switch(rval.code) { |
| 427 | case RC_OK: |
| 428 | break; |
| 429 | case RC_WMORE: /* More data expected */ |
| 430 | if(!SIZE_VIOLATION) { |
| 431 | ADVANCE(rval.consumed); |
| 432 | RETURN(RC_WMORE); |
| 433 | } |
Lev Walkin | 5beb7de | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 434 | ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)", |
| 435 | (long)ctx->left, (long)size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 436 | /* Fall through */ |
| 437 | case RC_FAIL: /* Fatal error */ |
| 438 | RETURN(RC_FAIL); |
| 439 | } /* switch(rval) */ |
| 440 | |
| 441 | ADVANCE(rval.consumed); |
| 442 | } /* for(all structure members) */ |
| 443 | |
| 444 | phase3: |
| 445 | ctx->phase = 3; |
Lev Walkin | 48e82d1 | 2017-10-19 03:06:35 -0700 | [diff] [blame^] | 446 | /* Fall through */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 447 | case 3: /* 00 and other tags expected */ |
| 448 | case 4: /* only 00's expected */ |
| 449 | |
| 450 | ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 451 | td->name, (long)ctx->left, (long)size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 452 | |
| 453 | /* |
| 454 | * Skip everything until the end of the SEQUENCE. |
| 455 | */ |
| 456 | while(ctx->left) { |
| 457 | ssize_t tl, ll; |
| 458 | |
| 459 | tl = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 460 | switch(tl) { |
| 461 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 462 | /* Fall through */ |
| 463 | case -1: RETURN(RC_FAIL); |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * If expected <0><0>... |
| 468 | */ |
| 469 | if(ctx->left < 0 |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 470 | && ((const uint8_t *)ptr)[0] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 471 | if(LEFT < 2) { |
| 472 | if(SIZE_VIOLATION) |
| 473 | RETURN(RC_FAIL); |
| 474 | else |
| 475 | RETURN(RC_WMORE); |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 476 | } else if(((const uint8_t *)ptr)[1] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 477 | /* |
| 478 | * Correctly finished with <0><0>. |
| 479 | */ |
| 480 | ADVANCE(2); |
| 481 | ctx->left++; |
| 482 | ctx->phase = 4; |
| 483 | continue; |
| 484 | } |
| 485 | } |
| 486 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 487 | if(!IN_EXTENSION_GROUP(specs, td->elements_count) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 488 | || ctx->phase == 4) { |
| 489 | ASN_DEBUG("Unexpected continuation " |
| 490 | "of a non-extensible type " |
| 491 | "%s (SEQUENCE): %s", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 492 | td->name, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 493 | ber_tlv_tag_string(tlv_tag)); |
| 494 | RETURN(RC_FAIL); |
| 495 | } |
| 496 | |
Lev Walkin | baaa24f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 497 | ll = ber_skip_length(opt_codec_ctx, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 498 | BER_TLV_CONSTRUCTED(ptr), |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 499 | (const char *)ptr + tl, LEFT - tl); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 500 | switch(ll) { |
| 501 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 502 | /* Fall through */ |
| 503 | case -1: RETURN(RC_FAIL); |
| 504 | } |
| 505 | |
| 506 | ADVANCE(tl + ll); |
| 507 | } |
| 508 | |
| 509 | PHASE_OUT(ctx); |
| 510 | } |
| 511 | |
| 512 | RETURN(RC_OK); |
| 513 | } |
| 514 | |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 515 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 516 | /* |
| 517 | * The DER encoder of the SEQUENCE type. |
| 518 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 519 | asn_enc_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 520 | SEQUENCE_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr, |
| 521 | int tag_mode, ber_tlv_tag_t tag, |
| 522 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 523 | size_t computed_size = 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 524 | asn_enc_rval_t erval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 525 | ssize_t ret; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 526 | size_t edx; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 527 | |
| 528 | ASN_DEBUG("%s %s as SEQUENCE", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 529 | cb?"Encoding":"Estimating", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 530 | |
| 531 | /* |
| 532 | * Gather the length of the underlying members sequence. |
| 533 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 534 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 535 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 536 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 537 | const void *memb_ptr; /* Pointer to the member */ |
| 538 | const void *const *memb_ptr2; /* Pointer to that pointer */ |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 539 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 540 | if(elm->flags & ATF_POINTER) { |
| 541 | memb_ptr2 = |
| 542 | (const void *const *)((const char *)sptr + elm->memb_offset); |
| 543 | if(!*memb_ptr2) { |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 544 | ASN_DEBUG("Element %s %zu not present", |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 545 | elm->name, edx); |
| 546 | if(elm->optional) |
| 547 | continue; |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 548 | /* Mandatory element is missing */ |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 549 | ASN__ENCODE_FAILED; |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 550 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 551 | } else { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 552 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 553 | memb_ptr2 = &memb_ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 554 | } |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 555 | |
| 556 | /* Eliminate default values */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 557 | if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0) |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 558 | continue; |
| 559 | |
| 560 | erval = elm->type->op->der_encoder(elm->type, *memb_ptr2, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 561 | elm->tag_mode, elm->tag, |
| 562 | 0, 0); |
| 563 | if(erval.encoded == -1) |
| 564 | return erval; |
| 565 | computed_size += erval.encoded; |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 566 | ASN_DEBUG("Member %zu %s estimated %ld bytes", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 567 | edx, elm->name, (long)erval.encoded); |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * Encode the TLV for the sequence itself. |
| 572 | */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 573 | ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 574 | ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size); |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 575 | if(ret == -1) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 576 | ASN__ENCODE_FAILED; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 577 | erval.encoded = computed_size + ret; |
| 578 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 579 | if(!cb) ASN__ENCODED_OK(erval); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 580 | |
| 581 | /* |
| 582 | * Encode all members. |
| 583 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 584 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 585 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 586 | asn_enc_rval_t tmperval; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 587 | const void *memb_ptr; /* Pointer to the member */ |
| 588 | const void *const *memb_ptr2; /* Pointer to that pointer */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 589 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 590 | if(elm->flags & ATF_POINTER) { |
| 591 | memb_ptr2 = |
| 592 | (const void *const *)((const char *)sptr + elm->memb_offset); |
| 593 | if(!*memb_ptr2) continue; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 594 | } else { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 595 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 596 | memb_ptr2 = &memb_ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 597 | } |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 598 | |
| 599 | /* Eliminate default values */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 600 | if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0) |
| 601 | continue; |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 602 | |
| 603 | tmperval = elm->type->op->der_encoder(elm->type, *memb_ptr2, |
| 604 | elm->tag_mode, elm->tag, cb, app_key); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 605 | if(tmperval.encoded == -1) |
| 606 | return tmperval; |
| 607 | computed_size -= tmperval.encoded; |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 608 | ASN_DEBUG("Member %zu %s of SEQUENCE %s encoded in %ld bytes", |
Lev Walkin | a6a926a | 2005-03-02 01:54:28 +0000 | [diff] [blame] | 609 | edx, elm->name, td->name, (long)tmperval.encoded); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 612 | if(computed_size != 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 613 | /* |
| 614 | * Encoded size is not equal to the computed size. |
| 615 | */ |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 616 | ASN__ENCODE_FAILED; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 617 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 618 | ASN__ENCODED_OK(erval); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 621 | |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 622 | #undef XER_ADVANCE |
Lev Walkin | f59dac9 | 2017-08-26 21:26:51 -0700 | [diff] [blame] | 623 | #define XER_ADVANCE(num_bytes) \ |
| 624 | do { \ |
| 625 | size_t num = (num_bytes); \ |
| 626 | ptr = ((const char *)ptr) + num; \ |
| 627 | size -= num; \ |
| 628 | consumed_myself += num; \ |
| 629 | } while(0) |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 630 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 631 | /* |
| 632 | * Decode the XER (XML) data. |
| 633 | */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 634 | asn_dec_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 635 | SEQUENCE_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, |
| 636 | const asn_TYPE_descriptor_t *td, void **struct_ptr, |
| 637 | const char *opt_mname, const void *ptr, size_t size) { |
| 638 | /* |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 639 | * Bring closer parts of structure description. |
| 640 | */ |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 641 | const asn_SEQUENCE_specifics_t *specs |
| 642 | = (const asn_SEQUENCE_specifics_t *)td->specifics; |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 643 | asn_TYPE_member_t *elements = td->elements; |
| 644 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 645 | |
| 646 | /* |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 647 | * ... and parts of the structure being constructed. |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 648 | */ |
| 649 | void *st = *struct_ptr; /* Target structure. */ |
| 650 | asn_struct_ctx_t *ctx; /* Decoder context */ |
| 651 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 652 | asn_dec_rval_t rval; /* Return value from a decoder */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 653 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 654 | size_t edx; /* Element index */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 655 | |
| 656 | /* |
| 657 | * Create the target structure if it is not present already. |
| 658 | */ |
| 659 | if(st == 0) { |
| 660 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 661 | if(st == 0) RETURN(RC_FAIL); |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Restore parsing context. |
| 666 | */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 667 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 668 | |
| 669 | |
| 670 | /* |
| 671 | * Phases of XER/XML processing: |
| 672 | * Phase 0: Check that the opening tag matches our expectations. |
| 673 | * Phase 1: Processing body and reacting on closing tag. |
| 674 | * Phase 2: Processing inner type. |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 675 | * Phase 3: Skipping unknown extensions. |
| 676 | * Phase 4: PHASED OUT |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 677 | */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 678 | for(edx = ctx->step; ctx->phase <= 3;) { |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 679 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 680 | ssize_t ch_size; /* Chunk size */ |
| 681 | xer_check_tag_e tcv; /* Tag check value */ |
| 682 | asn_TYPE_member_t *elm; |
| 683 | |
| 684 | /* |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 685 | * Go inside the inner member of a sequence. |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 686 | */ |
| 687 | if(ctx->phase == 2) { |
| 688 | asn_dec_rval_t tmprval; |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 689 | void *memb_ptr_dontuse; /* Pointer to the member */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 690 | void **memb_ptr2; /* Pointer to that pointer */ |
| 691 | |
Lev Walkin | abf6889 | 2004-10-26 10:12:14 +0000 | [diff] [blame] | 692 | elm = &td->elements[edx]; |
| 693 | |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 694 | if(elm->flags & ATF_POINTER) { |
| 695 | /* Member is a pointer to another structure */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 696 | memb_ptr2 = (void **)((char *)st + elm->memb_offset); |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 697 | } else { |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 698 | memb_ptr_dontuse = (char *)st + elm->memb_offset; |
| 699 | memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Lev Walkin | 75b6fe4 | 2017-08-27 01:03:22 -0700 | [diff] [blame] | 702 | if(elm->flags & ATF_OPEN_TYPE) { |
Lev Walkin | f59dac9 | 2017-08-26 21:26:51 -0700 | [diff] [blame] | 703 | tmprval = OPEN_TYPE_xer_get(opt_codec_ctx, td, st, elm, ptr, size); |
| 704 | } else { |
| 705 | /* Invoke the inner type decoder, m.b. multiple times */ |
| 706 | tmprval = elm->type->op->xer_decoder(opt_codec_ctx, |
| 707 | elm->type, memb_ptr2, elm->name, |
| 708 | ptr, size); |
| 709 | } |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 710 | XER_ADVANCE(tmprval.consumed); |
| 711 | if(tmprval.code != RC_OK) |
| 712 | RETURN(tmprval.code); |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 713 | ctx->phase = 1; /* Back to body processing */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 714 | ctx->step = ++edx; |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 715 | ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d", |
| 716 | ctx->phase, ctx->step); |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 717 | /* Fall through */ |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Get the next part of the XML stream. |
| 722 | */ |
Lev Walkin | f59dac9 | 2017-08-26 21:26:51 -0700 | [diff] [blame] | 723 | ch_size = xer_next_token(&ctx->context, ptr, size, |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 724 | &ch_type); |
Lev Walkin | 9736348 | 2016-01-24 19:23:02 -0800 | [diff] [blame] | 725 | if(ch_size == -1) { |
| 726 | RETURN(RC_FAIL); |
Lev Walkin | f59dac9 | 2017-08-26 21:26:51 -0700 | [diff] [blame] | 727 | } else { |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 728 | switch(ch_type) { |
Lev Walkin | f59dac9 | 2017-08-26 21:26:51 -0700 | [diff] [blame] | 729 | case PXER_WMORE: |
| 730 | RETURN(RC_WMORE); |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 731 | case PXER_COMMENT: /* Got XML comment */ |
| 732 | case PXER_TEXT: /* Ignore free-standing text */ |
| 733 | XER_ADVANCE(ch_size); /* Skip silently */ |
| 734 | continue; |
| 735 | case PXER_TAG: |
| 736 | break; /* Check the rest down there */ |
| 737 | } |
| 738 | } |
| 739 | |
Lev Walkin | f59dac9 | 2017-08-26 21:26:51 -0700 | [diff] [blame] | 740 | tcv = xer_check_tag(ptr, ch_size, xml_tag); |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 741 | ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]", |
| 742 | tcv, ctx->phase, xml_tag); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 743 | |
| 744 | /* Skip the extensions section */ |
| 745 | if(ctx->phase == 3) { |
| 746 | switch(xer_skip_unknown(tcv, &ctx->left)) { |
| 747 | case -1: |
| 748 | ctx->phase = 4; |
| 749 | RETURN(RC_FAIL); |
| 750 | case 0: |
| 751 | XER_ADVANCE(ch_size); |
| 752 | continue; |
| 753 | case 1: |
| 754 | XER_ADVANCE(ch_size); |
| 755 | ctx->phase = 1; |
| 756 | continue; |
| 757 | case 2: |
| 758 | ctx->phase = 1; |
| 759 | break; |
| 760 | } |
| 761 | } |
| 762 | |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 763 | switch(tcv) { |
| 764 | case XCT_CLOSING: |
| 765 | if(ctx->phase == 0) break; |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 766 | ctx->phase = 0; |
| 767 | /* Fall through */ |
| 768 | case XCT_BOTH: |
| 769 | if(ctx->phase == 0) { |
| 770 | if(edx >= td->elements_count |
| 771 | || |
| 772 | /* Explicit OPTIONAL specs reaches the end */ |
| 773 | (edx + elements[edx].optional |
| 774 | == td->elements_count) |
| 775 | || |
| 776 | /* All extensions are optional */ |
| 777 | (IN_EXTENSION_GROUP(specs, edx) |
| 778 | && specs->ext_before |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 779 | > (signed)td->elements_count) |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 780 | ) { |
| 781 | XER_ADVANCE(ch_size); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 782 | ctx->phase = 4; /* Phase out */ |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 783 | RETURN(RC_OK); |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 784 | } else { |
| 785 | ASN_DEBUG("Premature end of XER SEQUENCE"); |
| 786 | RETURN(RC_FAIL); |
| 787 | } |
| 788 | } |
| 789 | /* Fall through */ |
| 790 | case XCT_OPENING: |
| 791 | if(ctx->phase == 0) { |
| 792 | XER_ADVANCE(ch_size); |
| 793 | ctx->phase = 1; /* Processing body phase */ |
| 794 | continue; |
| 795 | } |
Lev Walkin | da3ca41 | 2004-10-23 14:58:15 +0000 | [diff] [blame] | 796 | /* Fall through */ |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 797 | case XCT_UNKNOWN_OP: |
| 798 | case XCT_UNKNOWN_BO: |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 799 | |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 800 | ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%zu", |
Lev Walkin | 0266619 | 2005-07-03 05:31:02 +0000 | [diff] [blame] | 801 | tcv, ctx->phase, edx); |
| 802 | if(ctx->phase != 1) { |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 803 | break; /* Really unexpected */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 804 | } |
Lev Walkin | 0266619 | 2005-07-03 05:31:02 +0000 | [diff] [blame] | 805 | |
| 806 | if(edx < td->elements_count) { |
| 807 | /* |
| 808 | * Search which member corresponds to this tag. |
| 809 | */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 810 | size_t n; |
| 811 | size_t edx_end = edx + elements[edx].optional + 1; |
Lev Walkin | 0266619 | 2005-07-03 05:31:02 +0000 | [diff] [blame] | 812 | if(edx_end > td->elements_count) |
| 813 | edx_end = td->elements_count; |
| 814 | for(n = edx; n < edx_end; n++) { |
| 815 | elm = &td->elements[n]; |
Lev Walkin | f59dac9 | 2017-08-26 21:26:51 -0700 | [diff] [blame] | 816 | tcv = xer_check_tag(ptr, ch_size, elm->name); |
Lev Walkin | 0266619 | 2005-07-03 05:31:02 +0000 | [diff] [blame] | 817 | switch(tcv) { |
| 818 | case XCT_BOTH: |
| 819 | case XCT_OPENING: |
| 820 | /* |
| 821 | * Process this member. |
| 822 | */ |
| 823 | ctx->step = edx = n; |
| 824 | ctx->phase = 2; |
| 825 | break; |
| 826 | case XCT_UNKNOWN_OP: |
| 827 | case XCT_UNKNOWN_BO: |
| 828 | continue; |
| 829 | default: |
| 830 | n = edx_end; |
| 831 | break; /* Phase out */ |
| 832 | } |
| 833 | break; |
| 834 | } |
| 835 | if(n != edx_end) |
| 836 | continue; |
| 837 | } else { |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 838 | ASN_DEBUG("Out of defined members: %zu/%u", |
Lev Walkin | 0266619 | 2005-07-03 05:31:02 +0000 | [diff] [blame] | 839 | edx, td->elements_count); |
| 840 | } |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 841 | |
| 842 | /* It is expected extension */ |
| 843 | if(IN_EXTENSION_GROUP(specs, |
Lev Walkin | 0266619 | 2005-07-03 05:31:02 +0000 | [diff] [blame] | 844 | edx + (edx < td->elements_count |
| 845 | ? elements[edx].optional : 0))) { |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 846 | ASN_DEBUG("Got anticipated extension at %zu", |
Lev Walkin | 0266619 | 2005-07-03 05:31:02 +0000 | [diff] [blame] | 847 | edx); |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 848 | /* |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 849 | * Check for (XCT_BOTH or XCT_UNKNOWN_BO) |
| 850 | * By using a mask. Only record a pure |
| 851 | * <opening> tags. |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 852 | */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 853 | if(tcv & XCT_CLOSING) { |
| 854 | /* Found </extension> without body */ |
| 855 | } else { |
| 856 | ctx->left = 1; |
| 857 | ctx->phase = 3; /* Skip ...'s */ |
| 858 | } |
| 859 | XER_ADVANCE(ch_size); |
| 860 | continue; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /* Fall through */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 864 | default: |
| 865 | break; |
| 866 | } |
| 867 | |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 868 | ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]", |
Lev Walkin | f59dac9 | 2017-08-26 21:26:51 -0700 | [diff] [blame] | 869 | size>0?((const char *)ptr)[0]:'.', |
| 870 | size>1?((const char *)ptr)[1]:'.', |
| 871 | size>2?((const char *)ptr)[2]:'.', |
| 872 | size>3?((const char *)ptr)[3]:'.', |
| 873 | size>4?((const char *)ptr)[4]:'.', |
| 874 | size>5?((const char *)ptr)[5]:'.'); |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 875 | break; |
| 876 | } |
| 877 | |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 878 | ctx->phase = 4; /* "Phase out" on hard failure */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 879 | RETURN(RC_FAIL); |
| 880 | } |
| 881 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 882 | asn_enc_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 883 | SEQUENCE_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, |
| 884 | int ilevel, enum xer_encoder_flags_e flags, |
| 885 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 886 | asn_enc_rval_t er; |
| 887 | int xcan = (flags & XER_F_CANONICAL); |
| 888 | asn_TYPE_descriptor_t *tmp_def_val_td = 0; |
| 889 | void *tmp_def_val = 0; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 890 | size_t edx; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 891 | |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 892 | if(!sptr) ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 893 | |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 894 | er.encoded = 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 895 | |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 896 | for(edx = 0; edx < td->elements_count; edx++) { |
| 897 | asn_enc_rval_t tmper; |
| 898 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 899 | const void *memb_ptr; |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 900 | const char *mname = elm->name; |
| 901 | unsigned int mlen = strlen(mname); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 902 | |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 903 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 904 | memb_ptr = |
| 905 | *(const void *const *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 906 | if(!memb_ptr) { |
| 907 | assert(tmp_def_val == 0); |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 908 | if(elm->default_value_set) { |
| 909 | if(elm->default_value_set(&tmp_def_val)) { |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 910 | ASN__ENCODE_FAILED; |
| 911 | } else { |
| 912 | memb_ptr = tmp_def_val; |
| 913 | tmp_def_val_td = elm->type; |
| 914 | } |
| 915 | } else if(elm->optional) { |
| 916 | continue; |
| 917 | } else { |
| 918 | /* Mandatory element is missing */ |
| 919 | ASN__ENCODE_FAILED; |
| 920 | } |
| 921 | } |
| 922 | } else { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 923 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 924 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 925 | |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 926 | if(!xcan) ASN__TEXT_INDENT(1, ilevel); |
| 927 | ASN__CALLBACK3("<", 1, mname, mlen, ">", 1); |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 928 | |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 929 | /* Print the member itself */ |
| 930 | tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, ilevel + 1, |
| 931 | flags, cb, app_key); |
| 932 | if(tmp_def_val) { |
| 933 | ASN_STRUCT_FREE(*tmp_def_val_td, tmp_def_val); |
| 934 | tmp_def_val = 0; |
| 935 | } |
| 936 | if(tmper.encoded == -1) return tmper; |
Lev Walkin | 4fe2882 | 2017-09-18 02:57:34 -0700 | [diff] [blame] | 937 | er.encoded += tmper.encoded; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 938 | |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 939 | ASN__CALLBACK3("</", 2, mname, mlen, ">", 1); |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 940 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 941 | |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 942 | if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 943 | |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 944 | ASN__ENCODED_OK(er); |
Lev Walkin | 942fd08 | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 945 | cb_failed: |
Lev Walkin | 28ba9db | 2017-08-31 02:06:58 -0700 | [diff] [blame] | 946 | if(tmp_def_val) ASN_STRUCT_FREE(*tmp_def_val_td, tmp_def_val); |
| 947 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 950 | int |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 951 | SEQUENCE_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 952 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 953 | size_t edx; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 954 | int ret; |
| 955 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 956 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 957 | |
| 958 | /* Dump preamble */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 959 | if(cb(td->name, strlen(td->name), app_key) < 0 |
| 960 | || cb(" ::= {", 6, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 961 | return -1; |
| 962 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 963 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 964 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 965 | const void *memb_ptr; |
| 966 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 967 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 968 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 969 | if(!memb_ptr) { |
| 970 | if(elm->optional) continue; |
| 971 | /* Print <absent> line */ |
| 972 | /* Fall through */ |
| 973 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 974 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 975 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | /* Indentation */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 979 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 980 | |
| 981 | /* Print the member's name and stuff */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 982 | if(cb(elm->name, strlen(elm->name), app_key) < 0 |
| 983 | || cb(": ", 2, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 984 | return -1; |
| 985 | |
| 986 | /* Print the member itself */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 987 | ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 988 | cb, app_key); |
| 989 | if(ret) return ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 992 | ilevel--; |
| 993 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 994 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 995 | return (cb("}", 1, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | void |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 999 | SEQUENCE_free(const asn_TYPE_descriptor_t *td, void *sptr, |
| 1000 | enum asn_struct_free_method method) { |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 1001 | size_t edx; |
Vasil Velichkov | aead8c5 | 2017-10-10 05:34:48 +0300 | [diff] [blame] | 1002 | const asn_SEQUENCE_specifics_t *specs = |
| 1003 | (const asn_SEQUENCE_specifics_t *)td->specifics; |
| 1004 | asn_struct_ctx_t *ctx; /* Decoder context */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1005 | |
| 1006 | if(!td || !sptr) |
| 1007 | return; |
| 1008 | |
| 1009 | ASN_DEBUG("Freeing %s as SEQUENCE", td->name); |
| 1010 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 1011 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 1012 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1013 | void *memb_ptr; |
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 = *(void **)((char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1016 | if(memb_ptr) |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 1017 | ASN_STRUCT_FREE(*elm->type, memb_ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1018 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1019 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 1020 | ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
Vasil Velichkov | aead8c5 | 2017-10-10 05:34:48 +0300 | [diff] [blame] | 1024 | /* Clean parsing context */ |
| 1025 | ctx = (asn_struct_ctx_t *)((char *)sptr + specs->ctx_offset); |
| 1026 | FREEMEM(ctx->ptr); |
| 1027 | |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 1028 | switch(method) { |
| 1029 | case ASFM_FREE_EVERYTHING: |
| 1030 | FREEMEM(sptr); |
| 1031 | break; |
| 1032 | case ASFM_FREE_UNDERLYING: |
| 1033 | break; |
| 1034 | case ASFM_FREE_UNDERLYING_AND_RESET: |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 1035 | memset( |
| 1036 | sptr, 0, |
| 1037 | ((const asn_SEQUENCE_specifics_t *)(td->specifics))->struct_size); |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 1038 | break; |
| 1039 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | int |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1043 | SEQUENCE_constraint(const asn_TYPE_descriptor_t *td, const void *sptr, |
| 1044 | asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
| 1045 | size_t edx; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1046 | |
| 1047 | if(!sptr) { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1048 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 1049 | "%s: value not given (%s:%d)", |
| 1050 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1051 | return -1; |
| 1052 | } |
| 1053 | |
| 1054 | /* |
| 1055 | * Iterate over structure members and check their validity. |
| 1056 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 1057 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 1058 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1059 | const void *memb_ptr; |
| 1060 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 1061 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1062 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 1063 | if(!memb_ptr) { |
| 1064 | if(elm->optional) |
| 1065 | continue; |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1066 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 1067 | "%s: mandatory element %s absent (%s:%d)", |
| 1068 | td->name, elm->name, __FILE__, __LINE__); |
| 1069 | return -1; |
| 1070 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1071 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1072 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1075 | if(elm->encoding_constraints.general_constraints) { |
| 1076 | int ret = elm->encoding_constraints.general_constraints(elm->type, memb_ptr, |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 1077 | ctfailcb, app_key); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 1078 | if(ret) return ret; |
| 1079 | } else { |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1080 | return elm->type->encoding_constraints.general_constraints(elm->type, |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 1081 | memb_ptr, ctfailcb, app_key); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 1082 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | return 0; |
| 1086 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1087 | |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 1088 | #ifndef ASN_DISABLE_PER_SUPPORT |
| 1089 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1090 | asn_dec_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1091 | SEQUENCE_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, |
| 1092 | const asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 1093 | const asn_per_constraints_t *constraints, void **sptr, |
| 1094 | asn_per_data_t *pd) { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1095 | const asn_SEQUENCE_specifics_t *specs = (const asn_SEQUENCE_specifics_t *)td->specifics; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1096 | void *st = *sptr; /* Target structure. */ |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1097 | int extpresent; /* Extension additions are present */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1098 | uint8_t *opres; /* Presence of optional root members */ |
| 1099 | asn_per_data_t opmd; |
| 1100 | asn_dec_rval_t rv; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 1101 | size_t edx; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1102 | |
| 1103 | (void)constraints; |
| 1104 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1105 | if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx)) |
| 1106 | ASN__DECODE_FAILED; |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 1107 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1108 | if(!st) { |
| 1109 | st = *sptr = CALLOC(1, specs->struct_size); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1110 | if(!st) ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name); |
| 1114 | |
| 1115 | /* Handle extensions */ |
| 1116 | if(specs->ext_before >= 0) { |
| 1117 | extpresent = per_get_few_bits(pd, 1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1118 | if(extpresent < 0) ASN__DECODE_STARVED; |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1119 | } else { |
| 1120 | extpresent = 0; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | /* Prepare a place and read-in the presence bitmap */ |
Lev Walkin | 73a5cc6 | 2007-06-24 08:45:31 +0000 | [diff] [blame] | 1124 | memset(&opmd, 0, sizeof(opmd)); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1125 | if(specs->roms_count) { |
| 1126 | opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1127 | if(!opres) ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1128 | /* Get the presence map */ |
| 1129 | if(per_get_many_bits(pd, opres, 0, specs->roms_count)) { |
| 1130 | FREEMEM(opres); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1131 | ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1132 | } |
| 1133 | opmd.buffer = opres; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1134 | opmd.nbits = specs->roms_count; |
| 1135 | ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)", |
| 1136 | td->name, specs->roms_count, *opres); |
| 1137 | } else { |
| 1138 | opres = 0; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * Get the sequence ROOT elements. |
| 1143 | */ |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1144 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1145 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1146 | void *memb_ptr; /* Pointer to the member */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1147 | void **memb_ptr2; /* Pointer to that pointer */ |
| 1148 | |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1149 | if(IN_EXTENSION_GROUP(specs, edx)) |
| 1150 | continue; |
| 1151 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1152 | /* Fetch the pointer to this member */ |
| 1153 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1154 | memb_ptr2 = (void **)((char *)st + elm->memb_offset); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1155 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1156 | memb_ptr = (char *)st + elm->memb_offset; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1157 | memb_ptr2 = &memb_ptr; |
| 1158 | } |
| 1159 | |
| 1160 | /* Deal with optionality */ |
| 1161 | if(elm->optional) { |
| 1162 | int present = per_get_few_bits(&opmd, 1); |
| 1163 | ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)", |
| 1164 | td->name, elm->name, present, |
| 1165 | (int)opmd.nboff, (int)opmd.nbits); |
| 1166 | if(present == 0) { |
| 1167 | /* This element is not present */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1168 | if(elm->default_value_set) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1169 | /* Fill-in DEFAULT */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1170 | if(elm->default_value_set(memb_ptr2)) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1171 | FREEMEM(opres); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1172 | ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1173 | } |
Lev Walkin | 8032f7a | 2007-06-27 01:54:57 +0000 | [diff] [blame] | 1174 | ASN_DEBUG("Filled-in default"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1175 | } |
| 1176 | /* The member is just not present */ |
| 1177 | continue; |
| 1178 | } |
| 1179 | /* Fall through */ |
| 1180 | } |
| 1181 | |
| 1182 | /* Fetch the member from the stream */ |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 1183 | ASN_DEBUG("Decoding member \"%s\" in %s", elm->name, td->name); |
Lev Walkin | 9de6cd8 | 2017-08-10 05:47:46 -0700 | [diff] [blame] | 1184 | |
Lev Walkin | 75b6fe4 | 2017-08-27 01:03:22 -0700 | [diff] [blame] | 1185 | if(elm->flags & ATF_OPEN_TYPE) { |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1186 | rv = OPEN_TYPE_uper_get(opt_codec_ctx, td, st, elm, pd); |
| 1187 | } else { |
Lev Walkin | f59dac9 | 2017-08-26 21:26:51 -0700 | [diff] [blame] | 1188 | rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type, |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1189 | elm->encoding_constraints.per_constraints, memb_ptr2, pd); |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1190 | } |
| 1191 | if(rv.code != RC_OK) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1192 | ASN_DEBUG("Failed decode %s in %s", |
| 1193 | elm->name, td->name); |
| 1194 | FREEMEM(opres); |
| 1195 | return rv; |
| 1196 | } |
| 1197 | } |
| 1198 | |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1199 | /* Optionality map is not needed anymore */ |
| 1200 | FREEMEM(opres); |
| 1201 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1202 | /* |
| 1203 | * Deal with extensions. |
| 1204 | */ |
| 1205 | if(extpresent) { |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1206 | ssize_t bmlength; |
| 1207 | uint8_t *epres; /* Presence of extension members */ |
| 1208 | asn_per_data_t epmd; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1209 | |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1210 | bmlength = uper_get_nslength(pd); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1211 | if(bmlength < 0) ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1212 | |
Lev Walkin | aaae9bf | 2010-10-24 16:56:30 -0700 | [diff] [blame] | 1213 | ASN_DEBUG("Extensions %ld present in %s", (long)bmlength, td->name); |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1214 | |
| 1215 | epres = (uint8_t *)MALLOC((bmlength + 15) >> 3); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1216 | if(!epres) ASN__DECODE_STARVED; |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1217 | |
| 1218 | /* Get the extensions map */ |
Simo Sorce | ce71d91 | 2015-09-03 16:34:20 -0400 | [diff] [blame] | 1219 | if(per_get_many_bits(pd, epres, 0, bmlength)) { |
| 1220 | FREEMEM(epres); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1221 | ASN__DECODE_STARVED; |
Simo Sorce | ce71d91 | 2015-09-03 16:34:20 -0400 | [diff] [blame] | 1222 | } |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1223 | |
Lev Walkin | 73a5cc6 | 2007-06-24 08:45:31 +0000 | [diff] [blame] | 1224 | memset(&epmd, 0, sizeof(epmd)); |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1225 | epmd.buffer = epres; |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1226 | epmd.nbits = bmlength; |
Lev Walkin | aaae9bf | 2010-10-24 16:56:30 -0700 | [diff] [blame] | 1227 | ASN_DEBUG("Read in extensions bitmap for %s of %ld bits (%x..)", |
| 1228 | td->name, (long)bmlength, *epres); |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1229 | |
| 1230 | /* Go over extensions and read them in */ |
| 1231 | for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) { |
| 1232 | asn_TYPE_member_t *elm = &td->elements[edx]; |
| 1233 | void *memb_ptr; /* Pointer to the member */ |
| 1234 | void **memb_ptr2; /* Pointer to that pointer */ |
| 1235 | int present; |
| 1236 | |
| 1237 | if(!IN_EXTENSION_GROUP(specs, edx)) { |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 1238 | ASN_DEBUG("%zu is not an extension", edx); |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1239 | continue; |
| 1240 | } |
| 1241 | |
| 1242 | /* Fetch the pointer to this member */ |
| 1243 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1244 | memb_ptr2 = (void **)((char *)st + elm->memb_offset); |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1245 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1246 | memb_ptr = (void *)((char *)st + elm->memb_offset); |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1247 | memb_ptr2 = &memb_ptr; |
| 1248 | } |
| 1249 | |
| 1250 | present = per_get_few_bits(&epmd, 1); |
| 1251 | if(present <= 0) { |
| 1252 | if(present < 0) break; /* No more extensions */ |
| 1253 | continue; |
| 1254 | } |
| 1255 | |
| 1256 | ASN_DEBUG("Decoding member %s in %s %p", elm->name, td->name, *memb_ptr2); |
Lev Walkin | 9218bc1 | 2007-06-27 04:09:37 +0000 | [diff] [blame] | 1257 | rv = uper_open_type_get(opt_codec_ctx, elm->type, |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1258 | elm->encoding_constraints.per_constraints, memb_ptr2, pd); |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1259 | if(rv.code != RC_OK) { |
| 1260 | FREEMEM(epres); |
| 1261 | return rv; |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | /* Skip over overflow extensions which aren't present |
| 1266 | * in this system's version of the protocol */ |
Lev Walkin | 06230f7 | 2007-06-26 09:52:44 +0000 | [diff] [blame] | 1267 | for(;;) { |
Lev Walkin | a2987ea | 2007-06-26 23:56:54 +0000 | [diff] [blame] | 1268 | ASN_DEBUG("Getting overflow extensions"); |
Lev Walkin | 06230f7 | 2007-06-26 09:52:44 +0000 | [diff] [blame] | 1269 | switch(per_get_few_bits(&epmd, 1)) { |
| 1270 | case -1: break; |
| 1271 | case 0: continue; |
| 1272 | default: |
Lev Walkin | 9218bc1 | 2007-06-27 04:09:37 +0000 | [diff] [blame] | 1273 | if(uper_open_type_skip(opt_codec_ctx, pd)) { |
Lev Walkin | 06230f7 | 2007-06-26 09:52:44 +0000 | [diff] [blame] | 1274 | FREEMEM(epres); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1275 | ASN__DECODE_STARVED; |
Lev Walkin | 06230f7 | 2007-06-26 09:52:44 +0000 | [diff] [blame] | 1276 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1277 | } |
Lev Walkin | 06230f7 | 2007-06-26 09:52:44 +0000 | [diff] [blame] | 1278 | break; |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1279 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1280 | |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1281 | FREEMEM(epres); |
| 1282 | } |
| 1283 | |
| 1284 | /* Fill DEFAULT members in extensions */ |
| 1285 | for(edx = specs->roms_count; edx < specs->roms_count |
| 1286 | + specs->aoms_count; edx++) { |
| 1287 | asn_TYPE_member_t *elm = &td->elements[edx]; |
| 1288 | void **memb_ptr2; /* Pointer to member pointer */ |
| 1289 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1290 | if(!elm->default_value_set) continue; |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1291 | |
| 1292 | /* Fetch the pointer to this member */ |
| 1293 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1294 | memb_ptr2 = (void **)((char *)st |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 1295 | + elm->memb_offset); |
| 1296 | if(*memb_ptr2) continue; |
| 1297 | } else { |
| 1298 | continue; /* Extensions are all optionals */ |
| 1299 | } |
| 1300 | |
| 1301 | /* Set default value */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1302 | if(elm->default_value_set(memb_ptr2)) { |
| 1303 | ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | rv.consumed = 0; |
| 1308 | rv.code = RC_OK; |
| 1309 | return rv; |
| 1310 | } |
| 1311 | |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1312 | static int |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1313 | SEQUENCE__handle_extensions(const asn_TYPE_descriptor_t *td, const void *sptr, |
| 1314 | asn_per_outp_t *po1, asn_per_outp_t *po2) { |
| 1315 | const asn_SEQUENCE_specifics_t *specs |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 1316 | = (const asn_SEQUENCE_specifics_t *)td->specifics; |
Lev Walkin | f55a6dd | 2007-06-24 00:35:51 +0000 | [diff] [blame] | 1317 | int exts_present = 0; |
| 1318 | int exts_count = 0; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 1319 | size_t edx; |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1320 | |
| 1321 | if(specs->ext_before < 0) |
| 1322 | return 0; |
| 1323 | |
| 1324 | /* Find out which extensions are present */ |
| 1325 | for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) { |
| 1326 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1327 | const void *memb_ptr; /* Pointer to the member */ |
| 1328 | const void *const *memb_ptr2; /* Pointer to that pointer */ |
| 1329 | int present; |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1330 | |
Lev Walkin | f55a6dd | 2007-06-24 00:35:51 +0000 | [diff] [blame] | 1331 | if(!IN_EXTENSION_GROUP(specs, edx)) { |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 1332 | ASN_DEBUG("%s (@%zu) is not extension", elm->type->name, edx); |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1333 | continue; |
Lev Walkin | f55a6dd | 2007-06-24 00:35:51 +0000 | [diff] [blame] | 1334 | } |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1335 | |
| 1336 | /* Fetch the pointer to this member */ |
| 1337 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1338 | memb_ptr2 = |
| 1339 | (const void *const *)((const char *)sptr + elm->memb_offset); |
| 1340 | present = (*memb_ptr2 != 0); |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1341 | } else { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1342 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 1343 | memb_ptr2 = &memb_ptr; |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1344 | present = 1; |
| 1345 | } |
| 1346 | |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 1347 | ASN_DEBUG("checking %s (@%zu) present => %d", |
Lev Walkin | 06230f7 | 2007-06-26 09:52:44 +0000 | [diff] [blame] | 1348 | elm->type->name, edx, present); |
Lev Walkin | f55a6dd | 2007-06-24 00:35:51 +0000 | [diff] [blame] | 1349 | exts_count++; |
| 1350 | exts_present += present; |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1351 | |
| 1352 | /* Encode as presence marker */ |
| 1353 | if(po1 && per_put_few_bits(po1, present, 1)) |
| 1354 | return -1; |
| 1355 | /* Encode as open type field */ |
Lev Walkin | 9218bc1 | 2007-06-27 04:09:37 +0000 | [diff] [blame] | 1356 | if(po2 && present && uper_open_type_put(elm->type, |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1357 | elm->encoding_constraints.per_constraints, *memb_ptr2, po2)) |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1358 | return -1; |
| 1359 | |
| 1360 | } |
| 1361 | |
Lev Walkin | f55a6dd | 2007-06-24 00:35:51 +0000 | [diff] [blame] | 1362 | return exts_present ? exts_count : 0; |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1365 | asn_enc_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1366 | SEQUENCE_encode_uper(const asn_TYPE_descriptor_t *td, |
| 1367 | const asn_per_constraints_t *constraints, const void *sptr, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 1368 | asn_per_outp_t *po) { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1369 | const asn_SEQUENCE_specifics_t *specs |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 1370 | = (const asn_SEQUENCE_specifics_t *)td->specifics; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1371 | asn_enc_rval_t er; |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1372 | int n_extensions; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 1373 | size_t edx; |
| 1374 | size_t i; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1375 | |
| 1376 | (void)constraints; |
| 1377 | |
| 1378 | if(!sptr) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1379 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1380 | |
| 1381 | er.encoded = 0; |
| 1382 | |
| 1383 | ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name); |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1384 | |
| 1385 | |
| 1386 | /* |
| 1387 | * X.691#18.1 Whether structure is extensible |
| 1388 | * and whether to encode extensions |
| 1389 | */ |
| 1390 | if(specs->ext_before >= 0) { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1391 | n_extensions = SEQUENCE__handle_extensions(td, sptr, 0, 0); |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1392 | if(n_extensions < 0) |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 1393 | ASN__ENCODE_FAILED; |
| 1394 | if(per_put_few_bits(po, n_extensions ? 1 : 0, 1)) |
| 1395 | ASN__ENCODE_FAILED; |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1396 | } else { |
| 1397 | n_extensions = 0; /* There are no extensions to encode */ |
| 1398 | } |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1399 | |
| 1400 | /* Encode a presence bitmap */ |
| 1401 | for(i = 0; i < specs->roms_count; i++) { |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 1402 | asn_TYPE_member_t *elm; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1403 | const void *memb_ptr; /* Pointer to the member */ |
| 1404 | const void *const *memb_ptr2; /* Pointer to that pointer */ |
| 1405 | int present; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1406 | |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 1407 | edx = specs->oms[i]; |
| 1408 | elm = &td->elements[edx]; |
| 1409 | |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1410 | /* Fetch the pointer to this member */ |
| 1411 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1412 | memb_ptr2 = |
| 1413 | (const void *const *)((const char *)sptr + elm->memb_offset); |
| 1414 | present = (*memb_ptr2 != 0); |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1415 | } else { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1416 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 1417 | memb_ptr2 = &memb_ptr; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1418 | present = 1; |
| 1419 | } |
| 1420 | |
| 1421 | /* Eliminate default values */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1422 | if(present && elm->default_value_cmp |
| 1423 | && elm->default_value_cmp(*memb_ptr2) == 0) |
| 1424 | present = 0; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1425 | |
| 1426 | ASN_DEBUG("Element %s %s %s->%s is %s", |
| 1427 | elm->flags & ATF_POINTER ? "ptr" : "inline", |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1428 | elm->default_value_cmp ? "def" : "wtv", |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1429 | td->name, elm->name, present ? "present" : "absent"); |
| 1430 | if(per_put_few_bits(po, present, 1)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1431 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | /* |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1435 | * Encode the sequence ROOT elements. |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1436 | */ |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1437 | ASN_DEBUG("ext_after = %d, ec = %d, eb = %d", specs->ext_after, td->elements_count, specs->ext_before); |
Lev Walkin | 4d22b62 | 2007-07-23 09:58:25 +0000 | [diff] [blame] | 1438 | for(edx = 0; edx < ((specs->ext_after < 0) |
Vasil Velichkov | c3e0019 | 2017-10-18 21:12:01 +0300 | [diff] [blame] | 1439 | ? td->elements_count : (size_t)specs->ext_before - 1); edx++) { |
Lev Walkin | 4d22b62 | 2007-07-23 09:58:25 +0000 | [diff] [blame] | 1440 | |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1441 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1442 | const void *memb_ptr; /* Pointer to the member */ |
| 1443 | const void *const *memb_ptr2; /* Pointer to that pointer */ |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1444 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1445 | if(IN_EXTENSION_GROUP(specs, edx)) |
Lev Walkin | 4d22b62 | 2007-07-23 09:58:25 +0000 | [diff] [blame] | 1446 | continue; |
| 1447 | |
Lev Walkin | a2987ea | 2007-06-26 23:56:54 +0000 | [diff] [blame] | 1448 | ASN_DEBUG("About to encode %s", elm->type->name); |
| 1449 | |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1450 | /* Fetch the pointer to this member */ |
| 1451 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1452 | memb_ptr2 = |
| 1453 | (const void *const *)((const char *)sptr + elm->memb_offset); |
| 1454 | if(!*memb_ptr2) { |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 1455 | ASN_DEBUG("Element %s %zu not present", |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1456 | elm->name, edx); |
| 1457 | if(elm->optional) |
| 1458 | continue; |
| 1459 | /* Mandatory element is missing */ |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1460 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1461 | } |
| 1462 | } else { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1463 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 1464 | memb_ptr2 = &memb_ptr; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | /* Eliminate default values */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1468 | if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0) |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1469 | continue; |
| 1470 | |
Lev Walkin | 4d22b62 | 2007-07-23 09:58:25 +0000 | [diff] [blame] | 1471 | ASN_DEBUG("Encoding %s->%s", td->name, elm->name); |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1472 | er = elm->type->op->uper_encoder(elm->type, elm->encoding_constraints.per_constraints, |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1473 | *memb_ptr2, po); |
| 1474 | if(er.encoded == -1) |
| 1475 | return er; |
| 1476 | } |
| 1477 | |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1478 | /* No extensions to encode */ |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1479 | if(!n_extensions) ASN__ENCODED_OK(er); |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1480 | |
| 1481 | ASN_DEBUG("Length of %d bit-map", n_extensions); |
| 1482 | /* #18.8. Write down the presence bit-map length. */ |
| 1483 | if(uper_put_nslength(po, n_extensions)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1484 | ASN__ENCODE_FAILED; |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1485 | |
| 1486 | ASN_DEBUG("Bit-map of %d elements", n_extensions); |
| 1487 | /* #18.7. Encoding the extensions presence bit-map. */ |
| 1488 | /* TODO: act upon NOTE in #18.7 for canonical PER */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1489 | if(SEQUENCE__handle_extensions(td, sptr, po, 0) != n_extensions) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1490 | ASN__ENCODE_FAILED; |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1491 | |
| 1492 | ASN_DEBUG("Writing %d extensions", n_extensions); |
| 1493 | /* #18.9. Encode extensions as open type fields. */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1494 | if(SEQUENCE__handle_extensions(td, sptr, 0, po) != n_extensions) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1495 | ASN__ENCODE_FAILED; |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 1496 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1497 | ASN__ENCODED_OK(er); |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 1500 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
| 1501 | |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 1502 | int |
| 1503 | SEQUENCE_compare(const asn_TYPE_descriptor_t *td, const void *aptr, |
| 1504 | const void *bptr) { |
| 1505 | size_t edx; |
| 1506 | |
| 1507 | for(edx = 0; edx < td->elements_count; edx++) { |
| 1508 | asn_TYPE_member_t *elm = &td->elements[edx]; |
| 1509 | const void *amemb; |
| 1510 | const void *bmemb; |
| 1511 | int ret; |
| 1512 | |
| 1513 | if(elm->flags & ATF_POINTER) { |
| 1514 | amemb = |
| 1515 | *(const void *const *)((const char *)aptr + elm->memb_offset); |
| 1516 | bmemb = |
| 1517 | *(const void *const *)((const char *)bptr + elm->memb_offset); |
| 1518 | if(!amemb) { |
| 1519 | if(!bmemb) continue; |
| 1520 | return -1; |
| 1521 | } else if(!bmemb) { |
| 1522 | return 1; |
| 1523 | } |
| 1524 | } else { |
| 1525 | amemb = (const void *)((const char *)aptr + elm->memb_offset); |
| 1526 | bmemb = (const void *)((const char *)bptr + elm->memb_offset); |
| 1527 | } |
| 1528 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1529 | ret = elm->type->op->compare_struct(elm->type, amemb, bmemb); |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 1530 | if(ret != 0) return ret; |
| 1531 | } |
| 1532 | |
| 1533 | return 0; |
| 1534 | } |
| 1535 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1536 | asn_TYPE_operation_t asn_OP_SEQUENCE = { |
| 1537 | SEQUENCE_free, |
| 1538 | SEQUENCE_print, |
| 1539 | SEQUENCE_compare, |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1540 | SEQUENCE_decode_ber, |
| 1541 | SEQUENCE_encode_der, |
| 1542 | SEQUENCE_decode_xer, |
| 1543 | SEQUENCE_encode_xer, |
| 1544 | #ifdef ASN_DISABLE_OER_SUPPORT |
| 1545 | 0, |
| 1546 | 0, |
| 1547 | #else |
| 1548 | SEQUENCE_decode_oer, |
| 1549 | SEQUENCE_encode_oer, |
| 1550 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
| 1551 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 1552 | 0, |
| 1553 | 0, |
| 1554 | #else |
| 1555 | SEQUENCE_decode_uper, |
| 1556 | SEQUENCE_encode_uper, |
| 1557 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1558 | SEQUENCE_random_fill, |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1559 | 0 /* Use generic outmost tag fetcher */ |
| 1560 | }; |
| 1561 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1562 | |
| 1563 | asn_random_fill_result_t |
| 1564 | SEQUENCE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, |
| 1565 | const asn_encoding_constraints_t *constr, |
| 1566 | size_t max_length) { |
| 1567 | const asn_SEQUENCE_specifics_t *specs = |
| 1568 | (const asn_SEQUENCE_specifics_t *)td->specifics; |
| 1569 | asn_random_fill_result_t result_ok = {ARFILL_OK, 0}; |
| 1570 | asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0}; |
| 1571 | asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0}; |
| 1572 | void *st = *sptr; |
| 1573 | size_t edx; |
| 1574 | |
| 1575 | if(max_length == 0) return result_skipped; |
| 1576 | |
| 1577 | (void)constr; |
| 1578 | |
| 1579 | if(st == NULL) { |
| 1580 | st = CALLOC(1, specs->struct_size); |
| 1581 | if(st == NULL) { |
| 1582 | return result_failed; |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | for(edx = 0; edx < td->elements_count; edx++) { |
| 1587 | const asn_TYPE_member_t *elm = &td->elements[edx]; |
| 1588 | void *memb_ptr; /* Pointer to the member */ |
| 1589 | void **memb_ptr2; /* Pointer to that pointer */ |
| 1590 | asn_random_fill_result_t tmpres; |
| 1591 | |
| 1592 | if(elm->optional && asn_random_between(0, 4) == 2) { |
| 1593 | /* Sometimes decide not to fill the optional value */ |
| 1594 | continue; |
| 1595 | } |
| 1596 | |
| 1597 | if(elm->flags & ATF_POINTER) { |
| 1598 | /* Member is a pointer to another structure */ |
| 1599 | memb_ptr2 = (void **)((char *)st + elm->memb_offset); |
| 1600 | } else { |
| 1601 | memb_ptr = (char *)st + elm->memb_offset; |
| 1602 | memb_ptr2 = &memb_ptr; |
| 1603 | } |
| 1604 | |
| 1605 | tmpres = elm->type->op->random_fill( |
| 1606 | elm->type, memb_ptr2, &elm->encoding_constraints, |
| 1607 | max_length > result_ok.length ? max_length - result_ok.length : 0); |
| 1608 | switch(tmpres.code) { |
| 1609 | case ARFILL_OK: |
| 1610 | result_ok.length += tmpres.length; |
| 1611 | continue; |
| 1612 | case ARFILL_SKIPPED: |
| 1613 | assert(!(elm->flags & ATF_POINTER) || *memb_ptr2 == NULL); |
| 1614 | continue; |
| 1615 | case ARFILL_FAILED: |
| 1616 | if(st == *sptr) { |
| 1617 | ASN_STRUCT_RESET(*td, st); |
| 1618 | } else { |
| 1619 | ASN_STRUCT_FREE(*td, st); |
| 1620 | } |
| 1621 | return tmpres; |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | *sptr = st; |
| 1626 | |
| 1627 | return result_ok; |
| 1628 | } |
| 1629 | |