Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>. |
| 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 | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 8 | #include <assert.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 | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 37 | ptr = ((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 | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 113 | void **struct_ptr, void *ptr, size_t size, int tag_mode) { |
| 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 | 5e03376 | 2004-09-29 13:26:15 +0000 | [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 | 566a567 | 2004-10-05 06:36:57 +0000 | [diff] [blame] | 243 | if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) { |
| 244 | if(LEFT < 2) { |
| 245 | if(SIZE_VIOLATION) |
| 246 | RETURN(RC_FAIL); |
| 247 | else |
| 248 | RETURN(RC_WMORE); |
| 249 | } else if(((uint8_t *)ptr)[1] == 0) { |
| 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 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 313 | asn_TYPE_tag2member_t *t2m; |
| 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; |
Lev Walkin | c17d90f | 2005-01-17 14:32:45 +0000 | [diff] [blame] | 317 | t2m = (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) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 321 | asn_TYPE_tag2member_t *best = 0; |
| 322 | 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 | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 350 | if(!IN_EXTENSION_GROUP(specs, edx)) { |
Lev Walkin | 566a567 | 2004-10-05 06:36:57 +0000 | [diff] [blame] | 351 | ASN_DEBUG("Unexpected tag %s (at %d)", |
| 352 | ber_tlv_tag_string(tlv_tag), edx); |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 353 | ASN_DEBUG("Expected tag %s (%s)%s", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 354 | ber_tlv_tag_string(elements[edx].tag), |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 355 | elements[edx].name, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 356 | elements[edx].optional |
| 357 | ?" or alternatives":""); |
| 358 | RETURN(RC_FAIL); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 359 | } else { |
| 360 | /* Skip this tag */ |
| 361 | ssize_t skip; |
| 362 | |
Lev Walkin | baaa24f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 363 | skip = ber_skip_length(opt_codec_ctx, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 364 | BER_TLV_CONSTRUCTED(ptr), |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 365 | (char *)ptr + tag_len, LEFT - tag_len); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 366 | ASN_DEBUG("Skip length %d in %s", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 367 | (int)skip, td->name); |
Lev Walkin | f15320b | 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: |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 386 | ASN_DEBUG("Inside SEQUENCE %s MF2", td->name); |
Lev Walkin | f15320b | 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 | */ |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 393 | if(elements[edx].flags & ATF_POINTER) { |
| 394 | /* Member is a pointer to another structure */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 395 | memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset); |
Lev Walkin | f15320b | 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 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 407 | rval = elements[edx].type->ber_decoder(opt_codec_ctx, |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 408 | elements[edx].type, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 409 | memb_ptr2, ptr, LEFT, |
| 410 | elements[edx].tag_mode); |
Lev Walkin | 60b7cff | 2004-09-04 04:44:11 +0000 | [diff] [blame] | 411 | ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d " |
Lev Walkin | 5beb7de | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 412 | "in %d bytes rval.code %d, size=%d", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 413 | td->name, edx, elements[edx].type->name, |
Lev Walkin | 5beb7de | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 414 | (int)LEFT, (int)rval.consumed, rval.code, (int)size); |
Lev Walkin | f15320b | 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 | } |
Lev Walkin | 5beb7de | 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); |
Lev Walkin | f15320b | 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", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 439 | td->name, (long)ctx->left, (long)size); |
Lev Walkin | f15320b | 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 |
| 458 | && ((uint8_t *)ptr)[0] == 0) { |
| 459 | if(LEFT < 2) { |
| 460 | if(SIZE_VIOLATION) |
| 461 | RETURN(RC_FAIL); |
| 462 | else |
| 463 | RETURN(RC_WMORE); |
| 464 | } else if(((uint8_t *)ptr)[1] == 0) { |
| 465 | /* |
| 466 | * Correctly finished with <0><0>. |
| 467 | */ |
| 468 | ADVANCE(2); |
| 469 | ctx->left++; |
| 470 | ctx->phase = 4; |
| 471 | continue; |
| 472 | } |
| 473 | } |
| 474 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 475 | if(!IN_EXTENSION_GROUP(specs, td->elements_count) |
Lev Walkin | f15320b | 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", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 480 | td->name, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 481 | ber_tlv_tag_string(tlv_tag)); |
| 482 | RETURN(RC_FAIL); |
| 483 | } |
| 484 | |
Lev Walkin | baaa24f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 485 | ll = ber_skip_length(opt_codec_ctx, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 486 | BER_TLV_CONSTRUCTED(ptr), |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 487 | (char *)ptr + tl, LEFT - tl); |
Lev Walkin | f15320b | 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 | |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 503 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 504 | /* |
| 505 | * The DER encoder of the SEQUENCE type. |
| 506 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 507 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 508 | SEQUENCE_encode_der(asn_TYPE_descriptor_t *td, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 509 | void *ptr, int tag_mode, ber_tlv_tag_t tag, |
| 510 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 511 | size_t computed_size = 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 512 | asn_enc_rval_t erval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 513 | ssize_t ret; |
| 514 | int edx; |
| 515 | |
| 516 | ASN_DEBUG("%s %s as SEQUENCE", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 517 | cb?"Encoding":"Estimating", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 518 | |
| 519 | /* |
| 520 | * Gather the length of the underlying members sequence. |
| 521 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 522 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 523 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 524 | void *memb_ptr; |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 525 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 526 | memb_ptr = *(void **)((char *)ptr + elm->memb_offset); |
| 527 | if(!memb_ptr) continue; |
| 528 | } else { |
| 529 | memb_ptr = (void *)((char *)ptr + elm->memb_offset); |
| 530 | } |
| 531 | erval = elm->type->der_encoder(elm->type, memb_ptr, |
| 532 | elm->tag_mode, elm->tag, |
| 533 | 0, 0); |
| 534 | if(erval.encoded == -1) |
| 535 | return erval; |
| 536 | computed_size += erval.encoded; |
| 537 | ASN_DEBUG("Member %d %s estimated %ld bytes", |
| 538 | edx, elm->name, (long)erval.encoded); |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Encode the TLV for the sequence itself. |
| 543 | */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 544 | 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] | 545 | ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size); |
| 546 | if(ret == -1) { |
| 547 | erval.encoded = -1; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 548 | erval.failed_type = td; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 549 | erval.structure_ptr = ptr; |
| 550 | return erval; |
| 551 | } |
| 552 | erval.encoded = computed_size + ret; |
| 553 | |
| 554 | if(!cb) return erval; |
| 555 | |
| 556 | /* |
| 557 | * Encode all members. |
| 558 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 559 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 560 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 561 | asn_enc_rval_t tmperval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 562 | void *memb_ptr; |
| 563 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 564 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 565 | memb_ptr = *(void **)((char *)ptr + elm->memb_offset); |
| 566 | if(!memb_ptr) continue; |
| 567 | } else { |
| 568 | memb_ptr = (void *)((char *)ptr + elm->memb_offset); |
| 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; |
Lev Walkin | a6a926a | 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); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | if(computed_size != 0) { |
| 581 | /* |
| 582 | * Encoded size is not equal to the computed size. |
| 583 | */ |
| 584 | erval.encoded = -1; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 585 | erval.failed_type = td; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 586 | erval.structure_ptr = ptr; |
| 587 | } |
| 588 | |
| 589 | return erval; |
| 590 | } |
| 591 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 592 | |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 593 | #undef XER_ADVANCE |
| 594 | #define XER_ADVANCE(num_bytes) do { \ |
| 595 | size_t num = num_bytes; \ |
| 596 | buf_ptr = ((char *)buf_ptr) + num; \ |
| 597 | size -= num; \ |
| 598 | consumed_myself += num; \ |
| 599 | } while(0) |
| 600 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 601 | /* |
| 602 | * Decode the XER (XML) data. |
| 603 | */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 604 | asn_dec_rval_t |
| 605 | SEQUENCE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 606 | void **struct_ptr, const char *opt_mname, |
| 607 | void *buf_ptr, size_t size) { |
| 608 | /* |
| 609 | * Bring closer parts of structure description. |
| 610 | */ |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 611 | asn_SEQUENCE_specifics_t *specs |
| 612 | = (asn_SEQUENCE_specifics_t *)td->specifics; |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 613 | asn_TYPE_member_t *elements = td->elements; |
| 614 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 615 | |
| 616 | /* |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 617 | * ... and parts of the structure being constructed. |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 618 | */ |
| 619 | void *st = *struct_ptr; /* Target structure. */ |
| 620 | asn_struct_ctx_t *ctx; /* Decoder context */ |
| 621 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 622 | asn_dec_rval_t rval; /* Return value from a decoder */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 623 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 624 | int edx; /* Element index */ |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 625 | int edx_end; |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 626 | |
| 627 | /* |
| 628 | * Create the target structure if it is not present already. |
| 629 | */ |
| 630 | if(st == 0) { |
| 631 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 632 | if(st == 0) RETURN(RC_FAIL); |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * Restore parsing context. |
| 637 | */ |
| 638 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
| 639 | |
| 640 | |
| 641 | /* |
| 642 | * Phases of XER/XML processing: |
| 643 | * Phase 0: Check that the opening tag matches our expectations. |
| 644 | * Phase 1: Processing body and reacting on closing tag. |
| 645 | * Phase 2: Processing inner type. |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 646 | * Phase 3: Skipping unknown extensions. |
| 647 | * Phase 4: PHASED OUT |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 648 | */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 649 | for(edx = ctx->step; ctx->phase <= 3;) { |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 650 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 651 | ssize_t ch_size; /* Chunk size */ |
| 652 | xer_check_tag_e tcv; /* Tag check value */ |
| 653 | asn_TYPE_member_t *elm; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 654 | int n; |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 655 | |
| 656 | /* |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 657 | * Go inside the inner member of a sequence. |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 658 | */ |
| 659 | if(ctx->phase == 2) { |
| 660 | asn_dec_rval_t tmprval; |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 661 | void *memb_ptr; /* Pointer to the member */ |
| 662 | void **memb_ptr2; /* Pointer to that pointer */ |
| 663 | |
Lev Walkin | abf6889 | 2004-10-26 10:12:14 +0000 | [diff] [blame] | 664 | elm = &td->elements[edx]; |
| 665 | |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 666 | if(elm->flags & ATF_POINTER) { |
| 667 | /* Member is a pointer to another structure */ |
| 668 | memb_ptr2 = (void **)((char *)st |
| 669 | + elm->memb_offset); |
| 670 | } else { |
| 671 | memb_ptr = (char *)st + elm->memb_offset; |
| 672 | memb_ptr2 = &memb_ptr; |
| 673 | } |
| 674 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 675 | /* Invoke the inner type decoder, m.b. multiple times */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 676 | tmprval = elm->type->xer_decoder(opt_codec_ctx, |
| 677 | elm->type, memb_ptr2, elm->name, |
| 678 | buf_ptr, size); |
| 679 | XER_ADVANCE(tmprval.consumed); |
| 680 | if(tmprval.code != RC_OK) |
| 681 | RETURN(tmprval.code); |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 682 | ctx->phase = 1; /* Back to body processing */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 683 | ctx->step = ++edx; |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 684 | ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d", |
| 685 | ctx->phase, ctx->step); |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 686 | /* Fall through */ |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | * Get the next part of the XML stream. |
| 691 | */ |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 692 | ch_size = xer_next_token(&ctx->context, buf_ptr, size, |
| 693 | &ch_type); |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 694 | switch(ch_size) { |
| 695 | case -1: RETURN(RC_FAIL); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 696 | case 0: RETURN(RC_WMORE); |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 697 | default: |
| 698 | switch(ch_type) { |
| 699 | case PXER_COMMENT: /* Got XML comment */ |
| 700 | case PXER_TEXT: /* Ignore free-standing text */ |
| 701 | XER_ADVANCE(ch_size); /* Skip silently */ |
| 702 | continue; |
| 703 | case PXER_TAG: |
| 704 | break; /* Check the rest down there */ |
| 705 | } |
| 706 | } |
| 707 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 708 | tcv = xer_check_tag(buf_ptr, ch_size, xml_tag); |
| 709 | ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d", tcv, ctx->phase); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 710 | |
| 711 | /* Skip the extensions section */ |
| 712 | if(ctx->phase == 3) { |
| 713 | switch(xer_skip_unknown(tcv, &ctx->left)) { |
| 714 | case -1: |
| 715 | ctx->phase = 4; |
| 716 | RETURN(RC_FAIL); |
| 717 | case 0: |
| 718 | XER_ADVANCE(ch_size); |
| 719 | continue; |
| 720 | case 1: |
| 721 | XER_ADVANCE(ch_size); |
| 722 | ctx->phase = 1; |
| 723 | continue; |
| 724 | case 2: |
| 725 | ctx->phase = 1; |
| 726 | break; |
| 727 | } |
| 728 | } |
| 729 | |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 730 | switch(tcv) { |
| 731 | case XCT_CLOSING: |
| 732 | if(ctx->phase == 0) break; |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 733 | ctx->phase = 0; |
| 734 | /* Fall through */ |
| 735 | case XCT_BOTH: |
| 736 | if(ctx->phase == 0) { |
| 737 | if(edx >= td->elements_count |
| 738 | || |
| 739 | /* Explicit OPTIONAL specs reaches the end */ |
| 740 | (edx + elements[edx].optional |
| 741 | == td->elements_count) |
| 742 | || |
| 743 | /* All extensions are optional */ |
| 744 | (IN_EXTENSION_GROUP(specs, edx) |
| 745 | && specs->ext_before |
| 746 | > td->elements_count) |
| 747 | ) { |
| 748 | XER_ADVANCE(ch_size); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 749 | ctx->phase = 4; /* Phase out */ |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 750 | RETURN(RC_OK); |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 751 | } else { |
| 752 | ASN_DEBUG("Premature end of XER SEQUENCE"); |
| 753 | RETURN(RC_FAIL); |
| 754 | } |
| 755 | } |
| 756 | /* Fall through */ |
| 757 | case XCT_OPENING: |
| 758 | if(ctx->phase == 0) { |
| 759 | XER_ADVANCE(ch_size); |
| 760 | ctx->phase = 1; /* Processing body phase */ |
| 761 | continue; |
| 762 | } |
Lev Walkin | da3ca41 | 2004-10-23 14:58:15 +0000 | [diff] [blame] | 763 | /* Fall through */ |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 764 | case XCT_UNKNOWN_OP: |
| 765 | case XCT_UNKNOWN_BO: |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 766 | |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 767 | ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d", |
| 768 | tcv, ctx->phase); |
| 769 | if(ctx->phase != 1 |
Lev Walkin | da3ca41 | 2004-10-23 14:58:15 +0000 | [diff] [blame] | 770 | || edx >= td->elements_count) |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 771 | break; /* Really unexpected */ |
| 772 | |
| 773 | /* |
| 774 | * Search which member corresponds to this tag. |
| 775 | */ |
| 776 | edx_end = edx + elements[edx].optional + 1; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 777 | if(edx_end > td->elements_count) |
| 778 | edx_end = td->elements_count; |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 779 | for(n = edx; n < edx_end; n++) { |
| 780 | elm = &td->elements[n]; |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 781 | tcv = xer_check_tag(buf_ptr,ch_size,elm->name); |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 782 | switch(tcv) { |
| 783 | case XCT_BOTH: |
| 784 | case XCT_OPENING: |
| 785 | /* |
| 786 | * Process this member. |
| 787 | */ |
| 788 | ctx->step = edx = n; |
| 789 | ctx->phase = 2; |
| 790 | break; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 791 | case XCT_UNKNOWN_OP: |
| 792 | case XCT_UNKNOWN_BO: |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 793 | continue; |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 794 | default: |
| 795 | n = edx_end; |
| 796 | break; /* Phase out */ |
| 797 | } |
| 798 | break; |
| 799 | } |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 800 | if(n != edx_end) |
| 801 | continue; |
| 802 | |
| 803 | /* It is expected extension */ |
| 804 | if(IN_EXTENSION_GROUP(specs, |
| 805 | edx + elements[edx].optional)) { |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 806 | ASN_DEBUG("Got anticipated extension at %d", edx); |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 807 | /* |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 808 | * Check for (XCT_BOTH or XCT_UNKNOWN_BO) |
| 809 | * By using a mask. Only record a pure |
| 810 | * <opening> tags. |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 811 | */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 812 | if(tcv & XCT_CLOSING) { |
| 813 | /* Found </extension> without body */ |
| 814 | } else { |
| 815 | ctx->left = 1; |
| 816 | ctx->phase = 3; /* Skip ...'s */ |
| 817 | } |
| 818 | XER_ADVANCE(ch_size); |
| 819 | continue; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /* Fall through */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 823 | default: |
| 824 | break; |
| 825 | } |
| 826 | |
| 827 | ASN_DEBUG("Unexpected XML tag in SEQUENCE"); |
| 828 | break; |
| 829 | } |
| 830 | |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 831 | ctx->phase = 4; /* "Phase out" on hard failure */ |
Lev Walkin | 1bbc200 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 832 | RETURN(RC_FAIL); |
| 833 | } |
| 834 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 835 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 836 | SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 837 | int ilevel, enum xer_encoder_flags_e flags, |
| 838 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 839 | asn_enc_rval_t er; |
| 840 | int xcan = (flags & XER_F_CANONICAL); |
| 841 | int edx; |
| 842 | |
| 843 | if(!sptr) |
| 844 | _ASN_ENCODE_FAILED; |
| 845 | |
| 846 | er.encoded = 0; |
| 847 | |
| 848 | for(edx = 0; edx < td->elements_count; edx++) { |
| 849 | asn_enc_rval_t tmper; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 850 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 851 | void *memb_ptr; |
| 852 | const char *mname = elm->name; |
| 853 | unsigned int mlen = strlen(mname); |
| 854 | |
| 855 | if(elm->flags & ATF_POINTER) { |
| 856 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
| 857 | if(!memb_ptr) continue; /* OPTIONAL element? */ |
| 858 | } else { |
| 859 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
| 860 | } |
| 861 | |
| 862 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel); |
| 863 | _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1); |
| 864 | |
| 865 | /* Print the member itself */ |
| 866 | tmper = elm->type->xer_encoder(elm->type, memb_ptr, |
| 867 | ilevel + 1, flags, cb, app_key); |
| 868 | if(tmper.encoded == -1) return tmper; |
| 869 | |
| 870 | _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1); |
| 871 | er.encoded += 5 + (2 * mlen) + tmper.encoded; |
| 872 | } |
| 873 | |
| 874 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1); |
| 875 | |
| 876 | return er; |
Lev Walkin | 942fd08 | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 877 | cb_failed: |
| 878 | _ASN_ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 881 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 882 | SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 883 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 884 | int edx; |
| 885 | int ret; |
| 886 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 887 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 888 | |
| 889 | /* Dump preamble */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 890 | if(cb(td->name, strlen(td->name), app_key) < 0 |
| 891 | || cb(" ::= {", 6, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 892 | return -1; |
| 893 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 894 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 895 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 896 | const void *memb_ptr; |
| 897 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 898 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 899 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
| 900 | if(!memb_ptr) continue; |
| 901 | } else { |
| 902 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 903 | } |
| 904 | |
| 905 | /* Indentation */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 906 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 907 | |
| 908 | /* Print the member's name and stuff */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 909 | if(cb(elm->name, strlen(elm->name), app_key) < 0 |
| 910 | || cb(": ", 2, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 911 | return -1; |
| 912 | |
| 913 | /* Print the member itself */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 914 | ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 915 | cb, app_key); |
| 916 | if(ret) return ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 919 | ilevel--; |
| 920 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 921 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 922 | return (cb("}", 1, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | void |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 926 | SEQUENCE_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 927 | int edx; |
| 928 | |
| 929 | if(!td || !sptr) |
| 930 | return; |
| 931 | |
| 932 | ASN_DEBUG("Freeing %s as SEQUENCE", td->name); |
| 933 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 934 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 935 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 936 | void *memb_ptr; |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 937 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 938 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
| 939 | if(memb_ptr) |
| 940 | elm->type->free_struct(elm->type, memb_ptr, 0); |
| 941 | } else { |
| 942 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
| 943 | elm->type->free_struct(elm->type, memb_ptr, 1); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | if(!contents_only) { |
| 948 | FREEMEM(sptr); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 953 | SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 954 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 955 | int edx; |
| 956 | |
| 957 | if(!sptr) { |
Lev Walkin | ba4e518 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 958 | _ASN_ERRLOG(app_errlog, app_key, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 959 | "%s: value not given (%s:%d)", |
| 960 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 961 | return -1; |
| 962 | } |
| 963 | |
| 964 | /* |
| 965 | * Iterate over structure members and check their validity. |
| 966 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 967 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 968 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 969 | const void *memb_ptr; |
| 970 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 971 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 972 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 973 | if(!memb_ptr) continue; |
| 974 | } else { |
| 975 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 976 | } |
| 977 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 978 | if(elm->memb_constraints) { |
| 979 | int ret = elm->memb_constraints(elm->type, memb_ptr, |
| 980 | app_errlog, app_key); |
| 981 | if(ret) return ret; |
| 982 | } else { |
| 983 | int ret = elm->type->check_constraints(elm->type, |
| 984 | memb_ptr, app_errlog, app_key); |
| 985 | if(ret) return ret; |
| 986 | /* |
| 987 | * Cannot inherit it earlier: |
| 988 | * need to make sure we get the updated version. |
| 989 | */ |
| 990 | elm->memb_constraints = elm->type->check_constraints; |
| 991 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | return 0; |
| 995 | } |