vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>. |
| 3 | * All rights reserved. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 4 | * Redistribution and modifications are permitted subject to BSD license. |
| 5 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 6 | #include <asn_internal.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | #include <constr_SEQUENCE.h> |
| 8 | |
| 9 | /* |
| 10 | * Number of bytes left for this structure. |
| 11 | * (ctx->left) indicates the number of bytes _transferred_ for the structure. |
| 12 | * (size) contains the number of bytes in the buffer passed. |
| 13 | */ |
vlm | c519061 | 2004-08-18 04:53:32 +0000 | [diff] [blame] | 14 | #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * If the subprocessor function returns with an indication that it wants |
| 18 | * more data, it may well be a fatal decoding problem, because the |
| 19 | * size is constrained by the <TLV>'s L, even if the buffer size allows |
| 20 | * reading more data. |
| 21 | * For example, consider the buffer containing the following TLVs: |
| 22 | * <T:5><L:1><V> <T:6>... |
| 23 | * The TLV length clearly indicates that one byte is expected in V, but |
| 24 | * if the V processor returns with "want more data" even if the buffer |
| 25 | * contains way more data than the V processor have seen. |
| 26 | */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 27 | #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * This macro "eats" the part of the buffer which is definitely "consumed", |
| 31 | * i.e. was correctly converted into local representation or rightfully skipped. |
| 32 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 33 | #undef ADVANCE |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | #define ADVANCE(num_bytes) do { \ |
| 35 | size_t num = num_bytes; \ |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 36 | ptr = ((const char *)ptr) + num;\ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | size -= num; \ |
| 38 | if(ctx->left >= 0) \ |
| 39 | ctx->left -= num; \ |
| 40 | consumed_myself += num; \ |
| 41 | } while(0) |
| 42 | |
| 43 | /* |
| 44 | * Switch to the next phase of parsing. |
| 45 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 46 | #undef NEXT_PHASE |
| 47 | #undef PHASE_OUT |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | #define NEXT_PHASE(ctx) do { \ |
| 49 | ctx->phase++; \ |
| 50 | ctx->step = 0; \ |
| 51 | } while(0) |
| 52 | #define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0) |
| 53 | |
| 54 | /* |
| 55 | * Return a standardized complex structure. |
| 56 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 57 | #undef RETURN |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 58 | #define RETURN(_code) do { \ |
| 59 | rval.code = _code; \ |
| 60 | rval.consumed = consumed_myself;\ |
| 61 | return rval; \ |
| 62 | } while(0) |
| 63 | |
| 64 | /* |
| 65 | * Check whether we are inside the extensions group. |
| 66 | */ |
| 67 | #define IN_EXTENSION_GROUP(specs, memb_idx) \ |
| 68 | ( ((memb_idx) > (specs)->ext_after) \ |
| 69 | &&((memb_idx) < (specs)->ext_before)) |
| 70 | |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * Tags are canonically sorted in the tag2element map. |
| 74 | */ |
| 75 | static int |
| 76 | _t2e_cmp(const void *ap, const void *bp) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 77 | const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap; |
| 78 | const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 79 | |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 80 | int a_class = BER_TAG_CLASS(a->el_tag); |
| 81 | int b_class = BER_TAG_CLASS(b->el_tag); |
| 82 | |
| 83 | if(a_class == b_class) { |
| 84 | ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag); |
| 85 | ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag); |
| 86 | |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 87 | if(a_value == b_value) { |
vlm | e4ee6ee | 2004-06-14 14:11:14 +0000 | [diff] [blame] | 88 | if(a->el_no > b->el_no) |
| 89 | return 1; |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 90 | /* |
| 91 | * Important: we do not check |
vlm | e4ee6ee | 2004-06-14 14:11:14 +0000 | [diff] [blame] | 92 | * for a->el_no <= b->el_no! |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 93 | */ |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 94 | return 0; |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 95 | } else if(a_value < b_value) |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 96 | return -1; |
| 97 | else |
| 98 | return 1; |
| 99 | } else if(a_class < b_class) { |
| 100 | return -1; |
| 101 | } else { |
| 102 | return 1; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 107 | /* |
| 108 | * The decoder of the SEQUENCE type. |
| 109 | */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 110 | asn_dec_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 111 | SEQUENCE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 112 | void **struct_ptr, const void *ptr, size_t size, int tag_mode) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 113 | /* |
| 114 | * Bring closer parts of structure description. |
| 115 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 116 | asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics; |
| 117 | asn_TYPE_member_t *elements = td->elements; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * Parts of the structure being constructed. |
| 121 | */ |
| 122 | void *st = *struct_ptr; /* Target structure. */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 123 | asn_struct_ctx_t *ctx; /* Decoder context */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 124 | |
| 125 | ber_tlv_tag_t tlv_tag; /* T from TLV */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 126 | asn_dec_rval_t rval; /* Return code from subparsers */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | |
| 128 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
| 129 | int edx; /* SEQUENCE element's index */ |
| 130 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 131 | ASN_DEBUG("Decoding %s as SEQUENCE", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * Create the target structure if it is not present already. |
| 135 | */ |
| 136 | if(st == 0) { |
| 137 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 138 | if(st == 0) { |
| 139 | RETURN(RC_FAIL); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Restore parsing context. |
| 145 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 146 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * Start to parse where left previously |
| 150 | */ |
| 151 | switch(ctx->phase) { |
| 152 | case 0: |
| 153 | /* |
| 154 | * PHASE 0. |
| 155 | * Check that the set of tags associated with given structure |
| 156 | * perfectly fits our expectations. |
| 157 | */ |
| 158 | |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 159 | rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size, |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 160 | tag_mode, 1, &ctx->left, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 161 | if(rval.code != RC_OK) { |
| 162 | ASN_DEBUG("%s tagging check failed: %d", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 163 | td->name, rval.code); |
vlm | 1cc0f2e | 2004-10-05 06:36:57 +0000 | [diff] [blame] | 164 | return rval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if(ctx->left >= 0) |
| 168 | ctx->left += rval.consumed; /* ?Substracted below! */ |
| 169 | ADVANCE(rval.consumed); |
| 170 | |
| 171 | NEXT_PHASE(ctx); |
| 172 | |
| 173 | ASN_DEBUG("Structure consumes %ld bytes, buffer %ld", |
| 174 | (long)ctx->left, (long)size); |
| 175 | |
| 176 | /* Fall through */ |
| 177 | case 1: |
| 178 | /* |
| 179 | * PHASE 1. |
| 180 | * From the place where we've left it previously, |
| 181 | * try to decode the next member from the list of |
| 182 | * this structure's elements. |
| 183 | * (ctx->step) stores the member being processed |
| 184 | * between invocations and the microphase {0,1} of parsing |
| 185 | * that member: |
| 186 | * step = (<member_number> * 2 + <microphase>). |
| 187 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 188 | for(edx = (ctx->step >> 1); edx < td->elements_count; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 189 | edx++, ctx->step = (ctx->step & ~1) + 2) { |
| 190 | void *memb_ptr; /* Pointer to the member */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 191 | void **memb_ptr2; /* Pointer to that pointer */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 192 | ssize_t tag_len; /* Length of TLV's T */ |
| 193 | int opt_edx_end; /* Next non-optional element */ |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 194 | int use_bsearch; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 195 | int n; |
| 196 | |
| 197 | if(ctx->step & 1) |
| 198 | goto microphase2; |
| 199 | |
| 200 | /* |
| 201 | * MICROPHASE 1: Synchronize decoding. |
| 202 | */ |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 203 | ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d" |
| 204 | " opt=%d ec=%d", |
| 205 | td->name, (int)ctx->left, edx, |
| 206 | elements[edx].flags, elements[edx].optional, |
| 207 | td->elements_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 208 | |
| 209 | if(ctx->left == 0 /* No more stuff is expected */ |
| 210 | && ( |
| 211 | /* Explicit OPTIONAL specification reaches the end */ |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 212 | (edx + elements[edx].optional |
| 213 | == td->elements_count) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 214 | || |
| 215 | /* All extensions are optional */ |
| 216 | (IN_EXTENSION_GROUP(specs, edx) |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 217 | && specs->ext_before > td->elements_count) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 218 | ) |
| 219 | ) { |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 220 | ASN_DEBUG("End of SEQUENCE %s", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 221 | /* |
| 222 | * Found the legitimate end of the structure. |
| 223 | */ |
| 224 | PHASE_OUT(ctx); |
| 225 | RETURN(RC_OK); |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Fetch the T from TLV. |
| 230 | */ |
| 231 | tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
vlm | 1308d2b | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 232 | ASN_DEBUG("Current tag in %s SEQUENCE for element %d " |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 233 | "(%s) is %s encoded in %d bytes, of frame %ld", |
vlm | 1308d2b | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 234 | td->name, edx, elements[edx].name, |
| 235 | ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 236 | switch(tag_len) { |
| 237 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 238 | /* Fall through */ |
| 239 | case -1: RETURN(RC_FAIL); |
| 240 | } |
| 241 | |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 242 | if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) { |
vlm | 1cc0f2e | 2004-10-05 06:36:57 +0000 | [diff] [blame] | 243 | if(LEFT < 2) { |
| 244 | if(SIZE_VIOLATION) |
| 245 | RETURN(RC_FAIL); |
| 246 | else |
| 247 | RETURN(RC_WMORE); |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 248 | } else if(((const uint8_t *)ptr)[1] == 0) { |
vlm | 1cc0f2e | 2004-10-05 06:36:57 +0000 | [diff] [blame] | 249 | ASN_DEBUG("edx = %d, opt = %d, ec=%d", |
| 250 | edx, elements[edx].optional, |
| 251 | td->elements_count); |
| 252 | if((edx + elements[edx].optional |
| 253 | == td->elements_count) |
| 254 | || (IN_EXTENSION_GROUP(specs, edx) |
| 255 | && specs->ext_before |
| 256 | > td->elements_count)) { |
| 257 | /* |
| 258 | * Yeah, baby! Found the terminator |
| 259 | * of the indefinite length structure. |
| 260 | */ |
| 261 | /* |
| 262 | * Proceed to the canonical |
| 263 | * finalization function. |
| 264 | * No advancing is necessary. |
| 265 | */ |
| 266 | goto phase3; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 271 | /* |
| 272 | * Find the next available type with this tag. |
| 273 | */ |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 274 | use_bsearch = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 275 | opt_edx_end = edx + elements[edx].optional + 1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 276 | if(opt_edx_end > td->elements_count) |
| 277 | opt_edx_end = td->elements_count; /* Cap */ |
vlm | 5d219bf | 2004-07-01 00:48:04 +0000 | [diff] [blame] | 278 | else if(opt_edx_end - edx > 8) { |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 279 | /* Limit the scope of linear search... */ |
vlm | 5d219bf | 2004-07-01 00:48:04 +0000 | [diff] [blame] | 280 | opt_edx_end = edx + 8; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 281 | use_bsearch = 1; |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 282 | /* ... and resort to bsearch() */ |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 283 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 284 | for(n = edx; n < opt_edx_end; n++) { |
| 285 | if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) { |
| 286 | /* |
| 287 | * Found element corresponding to the tag |
| 288 | * being looked at. |
| 289 | * Reposition over the right element. |
| 290 | */ |
| 291 | edx = n; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 292 | ctx->step = 1 + 2 * edx; /* Remember! */ |
| 293 | goto microphase2; |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 294 | } else if(elements[n].flags & ATF_OPEN_TYPE) { |
| 295 | /* |
| 296 | * This is the ANY type, which may bear |
| 297 | * any flag whatsoever. |
| 298 | */ |
| 299 | edx = n; |
| 300 | ctx->step = 1 + 2 * edx; /* Remember! */ |
| 301 | goto microphase2; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 302 | } else if(elements[n].tag == (ber_tlv_tag_t)-1) { |
| 303 | use_bsearch = 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | } |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 307 | if(use_bsearch) { |
| 308 | /* |
vlm | 141874a | 2004-09-04 06:17:35 +0000 | [diff] [blame] | 309 | * Resort to a binary search over |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 310 | * sorted array of tags. |
| 311 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 312 | asn_TYPE_tag2member_t *t2m; |
| 313 | asn_TYPE_tag2member_t key; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 314 | key.el_tag = tlv_tag; |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 315 | key.el_no = edx; |
vlm | a8f4a4c | 2005-01-17 14:32:45 +0000 | [diff] [blame] | 316 | t2m = (asn_TYPE_tag2member_t *)bsearch(&key, |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 317 | specs->tag2el, specs->tag2el_count, |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 318 | sizeof(specs->tag2el[0]), _t2e_cmp); |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 319 | if(t2m) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 320 | asn_TYPE_tag2member_t *best = 0; |
| 321 | asn_TYPE_tag2member_t *t2m_f, *t2m_l; |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 322 | int edx_max = edx + elements[edx].optional; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 323 | /* |
| 324 | * Rewind to the first element with that tag, |
| 325 | * `cause bsearch() does not guarantee order. |
| 326 | */ |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 327 | t2m_f = t2m + t2m->toff_first; |
| 328 | t2m_l = t2m + t2m->toff_last; |
| 329 | for(t2m = t2m_f; t2m <= t2m_l; t2m++) { |
| 330 | if(t2m->el_no > edx_max) break; |
| 331 | if(t2m->el_no < edx) continue; |
| 332 | best = t2m; |
| 333 | } |
| 334 | if(best) { |
| 335 | edx = best->el_no; |
| 336 | ctx->step = 1 + 2 * edx; |
| 337 | goto microphase2; |
| 338 | } |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 339 | } |
| 340 | n = opt_edx_end; |
| 341 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 342 | if(n == opt_edx_end) { |
| 343 | /* |
| 344 | * If tag is unknown, it may be either |
| 345 | * an unknown (thus, incorrect) tag, |
| 346 | * or an extension (...), |
| 347 | * or an end of the indefinite-length structure. |
| 348 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 349 | if(!IN_EXTENSION_GROUP(specs, edx)) { |
vlm | 1cc0f2e | 2004-10-05 06:36:57 +0000 | [diff] [blame] | 350 | ASN_DEBUG("Unexpected tag %s (at %d)", |
| 351 | ber_tlv_tag_string(tlv_tag), edx); |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 352 | ASN_DEBUG("Expected tag %s (%s)%s", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 353 | ber_tlv_tag_string(elements[edx].tag), |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 354 | elements[edx].name, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 355 | elements[edx].optional |
| 356 | ?" or alternatives":""); |
| 357 | RETURN(RC_FAIL); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 358 | } else { |
| 359 | /* Skip this tag */ |
| 360 | ssize_t skip; |
| 361 | |
vlm | 6130a2f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 362 | skip = ber_skip_length(opt_codec_ctx, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 363 | BER_TLV_CONSTRUCTED(ptr), |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 364 | (const char *)ptr + tag_len, |
| 365 | LEFT - tag_len); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 366 | ASN_DEBUG("Skip length %d in %s", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 367 | (int)skip, td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 368 | switch(skip) { |
| 369 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 370 | /* Fall through */ |
| 371 | case -1: RETURN(RC_FAIL); |
| 372 | } |
| 373 | |
| 374 | ADVANCE(skip + tag_len); |
| 375 | ctx->step -= 2; |
| 376 | edx--; |
| 377 | continue; /* Try again with the next tag */ |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * MICROPHASE 2: Invoke the member-specific decoder. |
| 383 | */ |
| 384 | ctx->step |= 1; /* Confirm entering next microphase */ |
| 385 | microphase2: |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 386 | ASN_DEBUG("Inside SEQUENCE %s MF2", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 387 | |
| 388 | /* |
| 389 | * Compute the position of the member inside a structure, |
| 390 | * and also a type of containment (it may be contained |
| 391 | * as pointer or using inline inclusion). |
| 392 | */ |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 393 | if(elements[edx].flags & ATF_POINTER) { |
| 394 | /* Member is a pointer to another structure */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 395 | memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 396 | } else { |
| 397 | /* |
| 398 | * A pointer to a pointer |
| 399 | * holding the start of the structure |
| 400 | */ |
| 401 | memb_ptr = (char *)st + elements[edx].memb_offset; |
| 402 | memb_ptr2 = &memb_ptr; |
| 403 | } |
| 404 | /* |
| 405 | * Invoke the member fetch routine according to member's type |
| 406 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 407 | rval = elements[edx].type->ber_decoder(opt_codec_ctx, |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 408 | elements[edx].type, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 409 | memb_ptr2, ptr, LEFT, |
| 410 | elements[edx].tag_mode); |
vlm | ef16e8c | 2004-09-04 04:44:11 +0000 | [diff] [blame] | 411 | ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d " |
vlm | eca9d62 | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 412 | "in %d bytes rval.code %d, size=%d", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 413 | td->name, edx, elements[edx].type->name, |
vlm | eca9d62 | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 414 | (int)LEFT, (int)rval.consumed, rval.code, (int)size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 415 | switch(rval.code) { |
| 416 | case RC_OK: |
| 417 | break; |
| 418 | case RC_WMORE: /* More data expected */ |
| 419 | if(!SIZE_VIOLATION) { |
| 420 | ADVANCE(rval.consumed); |
| 421 | RETURN(RC_WMORE); |
| 422 | } |
vlm | eca9d62 | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 423 | ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)", |
| 424 | (long)ctx->left, (long)size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 425 | /* Fall through */ |
| 426 | case RC_FAIL: /* Fatal error */ |
| 427 | RETURN(RC_FAIL); |
| 428 | } /* switch(rval) */ |
| 429 | |
| 430 | ADVANCE(rval.consumed); |
| 431 | } /* for(all structure members) */ |
| 432 | |
| 433 | phase3: |
| 434 | ctx->phase = 3; |
| 435 | case 3: /* 00 and other tags expected */ |
| 436 | case 4: /* only 00's expected */ |
| 437 | |
| 438 | ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 439 | td->name, (long)ctx->left, (long)size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 440 | |
| 441 | /* |
| 442 | * Skip everything until the end of the SEQUENCE. |
| 443 | */ |
| 444 | while(ctx->left) { |
| 445 | ssize_t tl, ll; |
| 446 | |
| 447 | tl = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 448 | switch(tl) { |
| 449 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 450 | /* Fall through */ |
| 451 | case -1: RETURN(RC_FAIL); |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * If expected <0><0>... |
| 456 | */ |
| 457 | if(ctx->left < 0 |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 458 | && ((const uint8_t *)ptr)[0] == 0) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 459 | if(LEFT < 2) { |
| 460 | if(SIZE_VIOLATION) |
| 461 | RETURN(RC_FAIL); |
| 462 | else |
| 463 | RETURN(RC_WMORE); |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 464 | } else if(((const uint8_t *)ptr)[1] == 0) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 465 | /* |
| 466 | * Correctly finished with <0><0>. |
| 467 | */ |
| 468 | ADVANCE(2); |
| 469 | ctx->left++; |
| 470 | ctx->phase = 4; |
| 471 | continue; |
| 472 | } |
| 473 | } |
| 474 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 475 | if(!IN_EXTENSION_GROUP(specs, td->elements_count) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 476 | || ctx->phase == 4) { |
| 477 | ASN_DEBUG("Unexpected continuation " |
| 478 | "of a non-extensible type " |
| 479 | "%s (SEQUENCE): %s", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 480 | td->name, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 481 | ber_tlv_tag_string(tlv_tag)); |
| 482 | RETURN(RC_FAIL); |
| 483 | } |
| 484 | |
vlm | 6130a2f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 485 | ll = ber_skip_length(opt_codec_ctx, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 486 | BER_TLV_CONSTRUCTED(ptr), |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 487 | (const char *)ptr + tl, LEFT - tl); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 488 | switch(ll) { |
| 489 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 490 | /* Fall through */ |
| 491 | case -1: RETURN(RC_FAIL); |
| 492 | } |
| 493 | |
| 494 | ADVANCE(tl + ll); |
| 495 | } |
| 496 | |
| 497 | PHASE_OUT(ctx); |
| 498 | } |
| 499 | |
| 500 | RETURN(RC_OK); |
| 501 | } |
| 502 | |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 503 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 504 | /* |
| 505 | * The DER encoder of the SEQUENCE type. |
| 506 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 507 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 508 | SEQUENCE_encode_der(asn_TYPE_descriptor_t *td, |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 509 | void *sptr, int tag_mode, ber_tlv_tag_t tag, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 510 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 511 | size_t computed_size = 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 512 | asn_enc_rval_t erval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 513 | ssize_t ret; |
| 514 | int edx; |
| 515 | |
| 516 | ASN_DEBUG("%s %s as SEQUENCE", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 517 | cb?"Encoding":"Estimating", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 518 | |
| 519 | /* |
| 520 | * Gather the length of the underlying members sequence. |
| 521 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 522 | for(edx = 0; edx < td->elements_count; edx++) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 523 | asn_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 524 | void *memb_ptr; |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 525 | if(elm->flags & ATF_POINTER) { |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 526 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
| 527 | if(!memb_ptr) { |
| 528 | if(elm->optional) continue; |
| 529 | /* Mandatory element is missing */ |
| 530 | _ASN_ENCODE_FAILED; |
| 531 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 532 | } else { |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 533 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 534 | } |
| 535 | erval = elm->type->der_encoder(elm->type, memb_ptr, |
| 536 | elm->tag_mode, elm->tag, |
| 537 | 0, 0); |
| 538 | if(erval.encoded == -1) |
| 539 | return erval; |
| 540 | computed_size += erval.encoded; |
| 541 | ASN_DEBUG("Member %d %s estimated %ld bytes", |
| 542 | edx, elm->name, (long)erval.encoded); |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Encode the TLV for the sequence itself. |
| 547 | */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 548 | ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 549 | ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size); |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 550 | if(ret == -1) |
| 551 | _ASN_ENCODE_FAILED; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 552 | erval.encoded = computed_size + ret; |
| 553 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 554 | if(!cb) _ASN_ENCODED_OK(erval); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 555 | |
| 556 | /* |
| 557 | * Encode all members. |
| 558 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 559 | for(edx = 0; edx < td->elements_count; edx++) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 560 | asn_TYPE_member_t *elm = &td->elements[edx]; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 561 | asn_enc_rval_t tmperval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 562 | void *memb_ptr; |
| 563 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 564 | if(elm->flags & ATF_POINTER) { |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 565 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 566 | if(!memb_ptr) continue; |
| 567 | } else { |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 568 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 569 | } |
| 570 | tmperval = elm->type->der_encoder(elm->type, memb_ptr, |
| 571 | elm->tag_mode, elm->tag, |
| 572 | cb, app_key); |
| 573 | if(tmperval.encoded == -1) |
| 574 | return tmperval; |
| 575 | computed_size -= tmperval.encoded; |
vlm | dc20efc | 2005-03-02 01:54:28 +0000 | [diff] [blame] | 576 | ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %ld bytes", |
| 577 | edx, elm->name, td->name, (long)tmperval.encoded); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 578 | } |
| 579 | |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 580 | if(computed_size != 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 581 | /* |
| 582 | * Encoded size is not equal to the computed size. |
| 583 | */ |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 584 | _ASN_ENCODE_FAILED; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 585 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 586 | _ASN_ENCODED_OK(erval); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 587 | } |
| 588 | |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 589 | |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 590 | #undef XER_ADVANCE |
| 591 | #define XER_ADVANCE(num_bytes) do { \ |
| 592 | size_t num = num_bytes; \ |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 593 | buf_ptr = ((const char *)buf_ptr) + num;\ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 594 | size -= num; \ |
| 595 | consumed_myself += num; \ |
| 596 | } while(0) |
| 597 | |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 598 | /* |
| 599 | * Decode the XER (XML) data. |
| 600 | */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 601 | asn_dec_rval_t |
| 602 | SEQUENCE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 603 | void **struct_ptr, const char *opt_mname, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 604 | const void *buf_ptr, size_t size) { |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 605 | /* |
| 606 | * Bring closer parts of structure description. |
| 607 | */ |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 608 | asn_SEQUENCE_specifics_t *specs |
| 609 | = (asn_SEQUENCE_specifics_t *)td->specifics; |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 610 | asn_TYPE_member_t *elements = td->elements; |
| 611 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 612 | |
| 613 | /* |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 614 | * ... and parts of the structure being constructed. |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 615 | */ |
| 616 | void *st = *struct_ptr; /* Target structure. */ |
| 617 | asn_struct_ctx_t *ctx; /* Decoder context */ |
| 618 | |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 619 | asn_dec_rval_t rval; /* Return value from a decoder */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 620 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 621 | int edx; /* Element index */ |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 622 | int edx_end; |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 623 | |
| 624 | /* |
| 625 | * Create the target structure if it is not present already. |
| 626 | */ |
| 627 | if(st == 0) { |
| 628 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 629 | if(st == 0) RETURN(RC_FAIL); |
| 630 | } |
| 631 | |
| 632 | /* |
| 633 | * Restore parsing context. |
| 634 | */ |
| 635 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
| 636 | |
| 637 | |
| 638 | /* |
| 639 | * Phases of XER/XML processing: |
| 640 | * Phase 0: Check that the opening tag matches our expectations. |
| 641 | * Phase 1: Processing body and reacting on closing tag. |
| 642 | * Phase 2: Processing inner type. |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 643 | * Phase 3: Skipping unknown extensions. |
| 644 | * Phase 4: PHASED OUT |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 645 | */ |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 646 | for(edx = ctx->step; ctx->phase <= 3;) { |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 647 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 648 | ssize_t ch_size; /* Chunk size */ |
| 649 | xer_check_tag_e tcv; /* Tag check value */ |
| 650 | asn_TYPE_member_t *elm; |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 651 | int n; |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 652 | |
| 653 | /* |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 654 | * Go inside the inner member of a sequence. |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 655 | */ |
| 656 | if(ctx->phase == 2) { |
| 657 | asn_dec_rval_t tmprval; |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 658 | void *memb_ptr; /* Pointer to the member */ |
| 659 | void **memb_ptr2; /* Pointer to that pointer */ |
| 660 | |
vlm | a351c2b | 2004-10-26 10:12:14 +0000 | [diff] [blame] | 661 | elm = &td->elements[edx]; |
| 662 | |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 663 | if(elm->flags & ATF_POINTER) { |
| 664 | /* Member is a pointer to another structure */ |
| 665 | memb_ptr2 = (void **)((char *)st |
| 666 | + elm->memb_offset); |
| 667 | } else { |
| 668 | memb_ptr = (char *)st + elm->memb_offset; |
| 669 | memb_ptr2 = &memb_ptr; |
| 670 | } |
| 671 | |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 672 | /* Invoke the inner type decoder, m.b. multiple times */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 673 | tmprval = elm->type->xer_decoder(opt_codec_ctx, |
| 674 | elm->type, memb_ptr2, elm->name, |
| 675 | buf_ptr, size); |
| 676 | XER_ADVANCE(tmprval.consumed); |
| 677 | if(tmprval.code != RC_OK) |
| 678 | RETURN(tmprval.code); |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 679 | ctx->phase = 1; /* Back to body processing */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 680 | ctx->step = ++edx; |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 681 | ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d", |
| 682 | ctx->phase, ctx->step); |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 683 | /* Fall through */ |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Get the next part of the XML stream. |
| 688 | */ |
vlm | c70f8f3 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 689 | ch_size = xer_next_token(&ctx->context, buf_ptr, size, |
| 690 | &ch_type); |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 691 | switch(ch_size) { |
| 692 | case -1: RETURN(RC_FAIL); |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 693 | case 0: RETURN(RC_WMORE); |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 694 | default: |
| 695 | switch(ch_type) { |
| 696 | case PXER_COMMENT: /* Got XML comment */ |
| 697 | case PXER_TEXT: /* Ignore free-standing text */ |
| 698 | XER_ADVANCE(ch_size); /* Skip silently */ |
| 699 | continue; |
| 700 | case PXER_TAG: |
| 701 | break; /* Check the rest down there */ |
| 702 | } |
| 703 | } |
| 704 | |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 705 | tcv = xer_check_tag(buf_ptr, ch_size, xml_tag); |
| 706 | ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d", tcv, ctx->phase); |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 707 | |
| 708 | /* Skip the extensions section */ |
| 709 | if(ctx->phase == 3) { |
| 710 | switch(xer_skip_unknown(tcv, &ctx->left)) { |
| 711 | case -1: |
| 712 | ctx->phase = 4; |
| 713 | RETURN(RC_FAIL); |
| 714 | case 0: |
| 715 | XER_ADVANCE(ch_size); |
| 716 | continue; |
| 717 | case 1: |
| 718 | XER_ADVANCE(ch_size); |
| 719 | ctx->phase = 1; |
| 720 | continue; |
| 721 | case 2: |
| 722 | ctx->phase = 1; |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 727 | switch(tcv) { |
| 728 | case XCT_CLOSING: |
| 729 | if(ctx->phase == 0) break; |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 730 | ctx->phase = 0; |
| 731 | /* Fall through */ |
| 732 | case XCT_BOTH: |
| 733 | if(ctx->phase == 0) { |
| 734 | if(edx >= td->elements_count |
| 735 | || |
| 736 | /* Explicit OPTIONAL specs reaches the end */ |
| 737 | (edx + elements[edx].optional |
| 738 | == td->elements_count) |
| 739 | || |
| 740 | /* All extensions are optional */ |
| 741 | (IN_EXTENSION_GROUP(specs, edx) |
| 742 | && specs->ext_before |
| 743 | > td->elements_count) |
| 744 | ) { |
| 745 | XER_ADVANCE(ch_size); |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 746 | ctx->phase = 4; /* Phase out */ |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 747 | RETURN(RC_OK); |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 748 | } else { |
| 749 | ASN_DEBUG("Premature end of XER SEQUENCE"); |
| 750 | RETURN(RC_FAIL); |
| 751 | } |
| 752 | } |
| 753 | /* Fall through */ |
| 754 | case XCT_OPENING: |
| 755 | if(ctx->phase == 0) { |
| 756 | XER_ADVANCE(ch_size); |
| 757 | ctx->phase = 1; /* Processing body phase */ |
| 758 | continue; |
| 759 | } |
vlm | 22d3f5d | 2004-10-23 14:58:15 +0000 | [diff] [blame] | 760 | /* Fall through */ |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 761 | case XCT_UNKNOWN_OP: |
| 762 | case XCT_UNKNOWN_BO: |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 763 | |
vlm | d960a86 | 2005-07-03 05:31:02 +0000 | [diff] [blame] | 764 | ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%d", |
| 765 | tcv, ctx->phase, edx); |
| 766 | if(ctx->phase != 1) { |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 767 | break; /* Really unexpected */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 768 | } |
vlm | d960a86 | 2005-07-03 05:31:02 +0000 | [diff] [blame] | 769 | |
| 770 | if(edx < td->elements_count) { |
| 771 | /* |
| 772 | * Search which member corresponds to this tag. |
| 773 | */ |
| 774 | edx_end = edx + elements[edx].optional + 1; |
| 775 | if(edx_end > td->elements_count) |
| 776 | edx_end = td->elements_count; |
| 777 | for(n = edx; n < edx_end; n++) { |
| 778 | elm = &td->elements[n]; |
| 779 | tcv = xer_check_tag(buf_ptr, |
| 780 | ch_size, elm->name); |
| 781 | switch(tcv) { |
| 782 | case XCT_BOTH: |
| 783 | case XCT_OPENING: |
| 784 | /* |
| 785 | * Process this member. |
| 786 | */ |
| 787 | ctx->step = edx = n; |
| 788 | ctx->phase = 2; |
| 789 | break; |
| 790 | case XCT_UNKNOWN_OP: |
| 791 | case XCT_UNKNOWN_BO: |
| 792 | continue; |
| 793 | default: |
| 794 | n = edx_end; |
| 795 | break; /* Phase out */ |
| 796 | } |
| 797 | break; |
| 798 | } |
| 799 | if(n != edx_end) |
| 800 | continue; |
| 801 | } else { |
| 802 | ASN_DEBUG("Out of defined members: %d/%d", |
| 803 | edx, td->elements_count); |
| 804 | } |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 805 | |
| 806 | /* It is expected extension */ |
| 807 | if(IN_EXTENSION_GROUP(specs, |
vlm | d960a86 | 2005-07-03 05:31:02 +0000 | [diff] [blame] | 808 | edx + (edx < td->elements_count |
| 809 | ? elements[edx].optional : 0))) { |
| 810 | ASN_DEBUG("Got anticipated extension at %d", |
| 811 | edx); |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 812 | /* |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 813 | * Check for (XCT_BOTH or XCT_UNKNOWN_BO) |
| 814 | * By using a mask. Only record a pure |
| 815 | * <opening> tags. |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 816 | */ |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 817 | if(tcv & XCT_CLOSING) { |
| 818 | /* Found </extension> without body */ |
| 819 | } else { |
| 820 | ctx->left = 1; |
| 821 | ctx->phase = 3; /* Skip ...'s */ |
| 822 | } |
| 823 | XER_ADVANCE(ch_size); |
| 824 | continue; |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | /* Fall through */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 828 | default: |
| 829 | break; |
| 830 | } |
| 831 | |
| 832 | ASN_DEBUG("Unexpected XML tag in SEQUENCE"); |
| 833 | break; |
| 834 | } |
| 835 | |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 836 | ctx->phase = 4; /* "Phase out" on hard failure */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 837 | RETURN(RC_FAIL); |
| 838 | } |
| 839 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 840 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 841 | SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 842 | int ilevel, enum xer_encoder_flags_e flags, |
| 843 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 844 | asn_enc_rval_t er; |
| 845 | int xcan = (flags & XER_F_CANONICAL); |
| 846 | int edx; |
| 847 | |
| 848 | if(!sptr) |
| 849 | _ASN_ENCODE_FAILED; |
| 850 | |
| 851 | er.encoded = 0; |
| 852 | |
| 853 | for(edx = 0; edx < td->elements_count; edx++) { |
| 854 | asn_enc_rval_t tmper; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 855 | asn_TYPE_member_t *elm = &td->elements[edx]; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 856 | void *memb_ptr; |
| 857 | const char *mname = elm->name; |
| 858 | unsigned int mlen = strlen(mname); |
| 859 | |
| 860 | if(elm->flags & ATF_POINTER) { |
| 861 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 862 | if(!memb_ptr) { |
| 863 | if(elm->optional) |
| 864 | continue; |
| 865 | /* Mandatory element is missing */ |
| 866 | _ASN_ENCODE_FAILED; |
| 867 | } |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 868 | } else { |
| 869 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
| 870 | } |
| 871 | |
| 872 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel); |
| 873 | _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1); |
| 874 | |
| 875 | /* Print the member itself */ |
| 876 | tmper = elm->type->xer_encoder(elm->type, memb_ptr, |
| 877 | ilevel + 1, flags, cb, app_key); |
| 878 | if(tmper.encoded == -1) return tmper; |
| 879 | |
| 880 | _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1); |
| 881 | er.encoded += 5 + (2 * mlen) + tmper.encoded; |
| 882 | } |
| 883 | |
| 884 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1); |
| 885 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 886 | _ASN_ENCODED_OK(er); |
vlm | 84d551b | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 887 | cb_failed: |
| 888 | _ASN_ENCODE_FAILED; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 889 | } |
| 890 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 891 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 892 | SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 893 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 894 | int edx; |
| 895 | int ret; |
| 896 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 897 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 898 | |
| 899 | /* Dump preamble */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 900 | if(cb(td->name, strlen(td->name), app_key) < 0 |
| 901 | || cb(" ::= {", 6, app_key) < 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 902 | return -1; |
| 903 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 904 | for(edx = 0; edx < td->elements_count; edx++) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 905 | asn_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 906 | const void *memb_ptr; |
| 907 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 908 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 909 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 910 | if(!memb_ptr) { |
| 911 | if(elm->optional) continue; |
| 912 | /* Print <absent> line */ |
| 913 | /* Fall through */ |
| 914 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 915 | } else { |
| 916 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 917 | } |
| 918 | |
| 919 | /* Indentation */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 920 | _i_INDENT(1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 921 | |
| 922 | /* Print the member's name and stuff */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 923 | if(cb(elm->name, strlen(elm->name), app_key) < 0 |
| 924 | || cb(": ", 2, app_key) < 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 925 | return -1; |
| 926 | |
| 927 | /* Print the member itself */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 928 | ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 929 | cb, app_key); |
| 930 | if(ret) return ret; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 931 | } |
| 932 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 933 | ilevel--; |
| 934 | _i_INDENT(1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 935 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 936 | return (cb("}", 1, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | void |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 940 | SEQUENCE_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 941 | int edx; |
| 942 | |
| 943 | if(!td || !sptr) |
| 944 | return; |
| 945 | |
| 946 | ASN_DEBUG("Freeing %s as SEQUENCE", td->name); |
| 947 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 948 | for(edx = 0; edx < td->elements_count; edx++) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 949 | asn_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 950 | void *memb_ptr; |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 951 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 952 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
| 953 | if(memb_ptr) |
| 954 | elm->type->free_struct(elm->type, memb_ptr, 0); |
| 955 | } else { |
| 956 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
| 957 | elm->type->free_struct(elm->type, memb_ptr, 1); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | if(!contents_only) { |
| 962 | FREEMEM(sptr); |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 967 | SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 968 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 969 | int edx; |
| 970 | |
| 971 | if(!sptr) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 972 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 973 | "%s: value not given (%s:%d)", |
| 974 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 975 | return -1; |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Iterate over structure members and check their validity. |
| 980 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 981 | for(edx = 0; edx < td->elements_count; edx++) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 982 | asn_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 983 | const void *memb_ptr; |
| 984 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 985 | if(elm->flags & ATF_POINTER) { |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 986 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 987 | if(!memb_ptr) { |
| 988 | if(elm->optional) |
| 989 | continue; |
| 990 | _ASN_ERRLOG(app_errlog, app_key, |
| 991 | "%s: mandatory element %s absent (%s:%d)", |
| 992 | td->name, elm->name, __FILE__, __LINE__); |
| 993 | return -1; |
| 994 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 995 | } else { |
| 996 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 997 | } |
| 998 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 999 | if(elm->memb_constraints) { |
| 1000 | int ret = elm->memb_constraints(elm->type, memb_ptr, |
| 1001 | app_errlog, app_key); |
| 1002 | if(ret) return ret; |
| 1003 | } else { |
| 1004 | int ret = elm->type->check_constraints(elm->type, |
| 1005 | memb_ptr, app_errlog, app_key); |
| 1006 | if(ret) return ret; |
| 1007 | /* |
| 1008 | * Cannot inherit it earlier: |
| 1009 | * need to make sure we get the updated version. |
| 1010 | */ |
| 1011 | elm->memb_constraints = elm->type->check_constraints; |
| 1012 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | return 0; |
| 1016 | } |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1017 | |
| 1018 | asn_dec_rval_t |
| 1019 | SEQUENCE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 1020 | asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) { |
| 1021 | asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics; |
| 1022 | void *st = *sptr; /* Target structure. */ |
| 1023 | int extpresent = 0; /* Extension additions are present */ |
| 1024 | uint8_t *opres; /* Presence of optional root members */ |
| 1025 | asn_per_data_t opmd; |
| 1026 | asn_dec_rval_t rv; |
| 1027 | int edx; |
| 1028 | |
| 1029 | (void)constraints; |
| 1030 | |
| 1031 | if(!st) { |
| 1032 | st = *sptr = CALLOC(1, specs->struct_size); |
| 1033 | if(!st) _ASN_DECODE_FAILED; |
| 1034 | } |
| 1035 | |
| 1036 | ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name); |
| 1037 | |
| 1038 | /* Handle extensions */ |
| 1039 | if(specs->ext_before >= 0) { |
| 1040 | extpresent = per_get_few_bits(pd, 1); |
| 1041 | if(extpresent < 0) _ASN_DECODE_FAILED; |
| 1042 | } |
| 1043 | |
| 1044 | /* Prepare a place and read-in the presence bitmap */ |
| 1045 | if(specs->roms_count) { |
| 1046 | opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1); |
| 1047 | if(!opres) _ASN_DECODE_FAILED; |
| 1048 | /* Get the presence map */ |
| 1049 | if(per_get_many_bits(pd, opres, 0, specs->roms_count)) { |
| 1050 | FREEMEM(opres); |
| 1051 | _ASN_DECODE_FAILED; |
| 1052 | } |
| 1053 | opmd.buffer = opres; |
| 1054 | opmd.nboff = 0; |
| 1055 | opmd.nbits = specs->roms_count; |
| 1056 | ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)", |
| 1057 | td->name, specs->roms_count, *opres); |
| 1058 | } else { |
| 1059 | opres = 0; |
| 1060 | memset(&opmd, 0, sizeof opmd); |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | * Get the sequence ROOT elements. |
| 1065 | */ |
| 1066 | for(edx = 0; edx < ((specs->ext_before < 0) |
| 1067 | ? td->elements_count : specs->ext_before + 1); edx++) { |
| 1068 | asn_TYPE_member_t *elm = &td->elements[edx]; |
| 1069 | void *memb_ptr; /* Pointer to the member */ |
| 1070 | void **memb_ptr2; /* Pointer to that pointer */ |
| 1071 | |
| 1072 | /* Fetch the pointer to this member */ |
| 1073 | if(elm->flags & ATF_POINTER) { |
| 1074 | memb_ptr2 = (void **)((char *)st + elm->memb_offset); |
| 1075 | } else { |
| 1076 | memb_ptr = (char *)st + elm->memb_offset; |
| 1077 | memb_ptr2 = &memb_ptr; |
| 1078 | } |
| 1079 | |
| 1080 | /* Deal with optionality */ |
| 1081 | if(elm->optional) { |
| 1082 | int present = per_get_few_bits(&opmd, 1); |
| 1083 | ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)", |
| 1084 | td->name, elm->name, present, |
| 1085 | (int)opmd.nboff, (int)opmd.nbits); |
| 1086 | if(present == 0) { |
| 1087 | /* This element is not present */ |
| 1088 | if(elm->default_value) { |
| 1089 | /* Fill-in DEFAULT */ |
| 1090 | if(elm->default_value(memb_ptr2)) { |
| 1091 | FREEMEM(opres); |
| 1092 | _ASN_DECODE_FAILED; |
| 1093 | } |
| 1094 | } |
| 1095 | /* The member is just not present */ |
| 1096 | continue; |
| 1097 | } |
| 1098 | /* Fall through */ |
| 1099 | } |
| 1100 | |
| 1101 | /* Fetch the member from the stream */ |
| 1102 | ASN_DEBUG("Decoding member %s in %s", elm->name, td->name); |
| 1103 | rv = elm->type->uper_decoder(opt_codec_ctx, elm->type, |
| 1104 | elm->per_constraints, memb_ptr2, pd); |
| 1105 | if(rv.code != RC_OK) { |
| 1106 | ASN_DEBUG("Failed decode %s in %s", |
| 1107 | elm->name, td->name); |
| 1108 | FREEMEM(opres); |
| 1109 | return rv; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | /* |
| 1114 | * Deal with extensions. |
| 1115 | */ |
| 1116 | if(extpresent) { |
| 1117 | ASN_DEBUG("Extensibility for %s: NOT IMPLEMENTED", td->name); |
| 1118 | _ASN_DECODE_FAILED; |
| 1119 | } else { |
| 1120 | for(edx = specs->roms_count; edx < specs->roms_count |
| 1121 | + specs->aoms_count; edx++) { |
| 1122 | asn_TYPE_member_t *elm = &td->elements[edx]; |
| 1123 | void *memb_ptr; /* Pointer to the member */ |
| 1124 | void **memb_ptr2; /* Pointer to that pointer */ |
| 1125 | |
| 1126 | if(!elm->default_value) continue; |
| 1127 | |
| 1128 | /* Fetch the pointer to this member */ |
| 1129 | if(elm->flags & ATF_POINTER) { |
| 1130 | memb_ptr2 = (void **)((char *)st + elm->memb_offset); |
| 1131 | } else { |
| 1132 | memb_ptr = (char *)st + elm->memb_offset; |
| 1133 | memb_ptr2 = &memb_ptr; |
| 1134 | } |
| 1135 | |
| 1136 | /* Set default value */ |
| 1137 | if(elm->default_value(memb_ptr2)) { |
| 1138 | FREEMEM(opres); |
| 1139 | _ASN_DECODE_FAILED; |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | rv.consumed = 0; |
| 1145 | rv.code = RC_OK; |
| 1146 | return rv; |
| 1147 | } |
| 1148 | |