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