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