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