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