vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>. |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 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 | f9230bf | 2006-10-03 06:39:36 +0000 | [diff] [blame] | 349 | if(!IN_EXTENSION_GROUP(specs, |
| 350 | edx + elements[edx].optional)) { |
vlm | 1cc0f2e | 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); |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 353 | ASN_DEBUG("Expected tag %s (%s)%s", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 354 | ber_tlv_tag_string(elements[edx].tag), |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 355 | elements[edx].name, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 356 | elements[edx].optional |
| 357 | ?" or alternatives":""); |
| 358 | RETURN(RC_FAIL); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 359 | } else { |
| 360 | /* Skip this tag */ |
| 361 | ssize_t skip; |
vlm | f9230bf | 2006-10-03 06:39:36 +0000 | [diff] [blame] | 362 | edx += elements[edx].optional; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 363 | |
vlm | f9230bf | 2006-10-03 06:39:36 +0000 | [diff] [blame] | 364 | ASN_DEBUG("Skipping unexpected %s (at %d)", |
| 365 | ber_tlv_tag_string(tlv_tag), edx); |
vlm | 6130a2f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 366 | skip = ber_skip_length(opt_codec_ctx, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 367 | BER_TLV_CONSTRUCTED(ptr), |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 368 | (const char *)ptr + tag_len, |
| 369 | LEFT - tag_len); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 370 | ASN_DEBUG("Skip length %d in %s", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 371 | (int)skip, td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 372 | switch(skip) { |
| 373 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 374 | /* Fall through */ |
| 375 | case -1: RETURN(RC_FAIL); |
| 376 | } |
| 377 | |
| 378 | ADVANCE(skip + tag_len); |
| 379 | ctx->step -= 2; |
| 380 | edx--; |
| 381 | continue; /* Try again with the next tag */ |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * MICROPHASE 2: Invoke the member-specific decoder. |
| 387 | */ |
| 388 | ctx->step |= 1; /* Confirm entering next microphase */ |
| 389 | microphase2: |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 390 | ASN_DEBUG("Inside SEQUENCE %s MF2", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 391 | |
| 392 | /* |
| 393 | * Compute the position of the member inside a structure, |
| 394 | * and also a type of containment (it may be contained |
| 395 | * as pointer or using inline inclusion). |
| 396 | */ |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 397 | if(elements[edx].flags & ATF_POINTER) { |
| 398 | /* Member is a pointer to another structure */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 399 | memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 400 | } else { |
| 401 | /* |
| 402 | * A pointer to a pointer |
| 403 | * holding the start of the structure |
| 404 | */ |
| 405 | memb_ptr = (char *)st + elements[edx].memb_offset; |
| 406 | memb_ptr2 = &memb_ptr; |
| 407 | } |
| 408 | /* |
| 409 | * Invoke the member fetch routine according to member's type |
| 410 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 411 | rval = elements[edx].type->ber_decoder(opt_codec_ctx, |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 412 | elements[edx].type, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 413 | memb_ptr2, ptr, LEFT, |
| 414 | elements[edx].tag_mode); |
vlm | ef16e8c | 2004-09-04 04:44:11 +0000 | [diff] [blame] | 415 | ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d " |
vlm | eca9d62 | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 416 | "in %d bytes rval.code %d, size=%d", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 417 | td->name, edx, elements[edx].type->name, |
vlm | eca9d62 | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 418 | (int)LEFT, (int)rval.consumed, rval.code, (int)size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 419 | switch(rval.code) { |
| 420 | case RC_OK: |
| 421 | break; |
| 422 | case RC_WMORE: /* More data expected */ |
| 423 | if(!SIZE_VIOLATION) { |
| 424 | ADVANCE(rval.consumed); |
| 425 | RETURN(RC_WMORE); |
| 426 | } |
vlm | eca9d62 | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 427 | ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)", |
| 428 | (long)ctx->left, (long)size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 429 | /* Fall through */ |
| 430 | case RC_FAIL: /* Fatal error */ |
| 431 | RETURN(RC_FAIL); |
| 432 | } /* switch(rval) */ |
| 433 | |
| 434 | ADVANCE(rval.consumed); |
| 435 | } /* for(all structure members) */ |
| 436 | |
| 437 | phase3: |
| 438 | ctx->phase = 3; |
| 439 | case 3: /* 00 and other tags expected */ |
| 440 | case 4: /* only 00's expected */ |
| 441 | |
| 442 | ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 443 | td->name, (long)ctx->left, (long)size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 444 | |
| 445 | /* |
| 446 | * Skip everything until the end of the SEQUENCE. |
| 447 | */ |
| 448 | while(ctx->left) { |
| 449 | ssize_t tl, ll; |
| 450 | |
| 451 | tl = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 452 | switch(tl) { |
| 453 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 454 | /* Fall through */ |
| 455 | case -1: RETURN(RC_FAIL); |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * If expected <0><0>... |
| 460 | */ |
| 461 | if(ctx->left < 0 |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 462 | && ((const uint8_t *)ptr)[0] == 0) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 463 | if(LEFT < 2) { |
| 464 | if(SIZE_VIOLATION) |
| 465 | RETURN(RC_FAIL); |
| 466 | else |
| 467 | RETURN(RC_WMORE); |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 468 | } else if(((const uint8_t *)ptr)[1] == 0) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 469 | /* |
| 470 | * Correctly finished with <0><0>. |
| 471 | */ |
| 472 | ADVANCE(2); |
| 473 | ctx->left++; |
| 474 | ctx->phase = 4; |
| 475 | continue; |
| 476 | } |
| 477 | } |
| 478 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 479 | if(!IN_EXTENSION_GROUP(specs, td->elements_count) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 480 | || ctx->phase == 4) { |
| 481 | ASN_DEBUG("Unexpected continuation " |
| 482 | "of a non-extensible type " |
| 483 | "%s (SEQUENCE): %s", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 484 | td->name, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 485 | ber_tlv_tag_string(tlv_tag)); |
| 486 | RETURN(RC_FAIL); |
| 487 | } |
| 488 | |
vlm | 6130a2f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 489 | ll = ber_skip_length(opt_codec_ctx, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 490 | BER_TLV_CONSTRUCTED(ptr), |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 491 | (const char *)ptr + tl, LEFT - tl); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 492 | switch(ll) { |
| 493 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 494 | /* Fall through */ |
| 495 | case -1: RETURN(RC_FAIL); |
| 496 | } |
| 497 | |
| 498 | ADVANCE(tl + ll); |
| 499 | } |
| 500 | |
| 501 | PHASE_OUT(ctx); |
| 502 | } |
| 503 | |
| 504 | RETURN(RC_OK); |
| 505 | } |
| 506 | |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 507 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 508 | /* |
| 509 | * The DER encoder of the SEQUENCE type. |
| 510 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 511 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 512 | SEQUENCE_encode_der(asn_TYPE_descriptor_t *td, |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 513 | void *sptr, int tag_mode, ber_tlv_tag_t tag, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 514 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 515 | size_t computed_size = 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 516 | asn_enc_rval_t erval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 517 | ssize_t ret; |
| 518 | int edx; |
| 519 | |
| 520 | ASN_DEBUG("%s %s as SEQUENCE", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 521 | cb?"Encoding":"Estimating", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 522 | |
| 523 | /* |
| 524 | * Gather the length of the underlying members sequence. |
| 525 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 526 | for(edx = 0; edx < td->elements_count; edx++) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 527 | asn_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 528 | void *memb_ptr; |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 529 | if(elm->flags & ATF_POINTER) { |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 530 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
| 531 | if(!memb_ptr) { |
| 532 | if(elm->optional) continue; |
| 533 | /* Mandatory element is missing */ |
| 534 | _ASN_ENCODE_FAILED; |
| 535 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 536 | } else { |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 537 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 538 | } |
| 539 | erval = elm->type->der_encoder(elm->type, memb_ptr, |
| 540 | elm->tag_mode, elm->tag, |
| 541 | 0, 0); |
| 542 | if(erval.encoded == -1) |
| 543 | return erval; |
| 544 | computed_size += erval.encoded; |
| 545 | ASN_DEBUG("Member %d %s estimated %ld bytes", |
| 546 | edx, elm->name, (long)erval.encoded); |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * Encode the TLV for the sequence itself. |
| 551 | */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 552 | 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] | 553 | ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size); |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 554 | if(ret == -1) |
| 555 | _ASN_ENCODE_FAILED; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 556 | erval.encoded = computed_size + ret; |
| 557 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 558 | if(!cb) _ASN_ENCODED_OK(erval); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 559 | |
| 560 | /* |
| 561 | * Encode all members. |
| 562 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 563 | for(edx = 0; edx < td->elements_count; edx++) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 564 | asn_TYPE_member_t *elm = &td->elements[edx]; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 565 | asn_enc_rval_t tmperval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 566 | void *memb_ptr; |
| 567 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 568 | if(elm->flags & ATF_POINTER) { |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 569 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 570 | if(!memb_ptr) continue; |
| 571 | } else { |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 572 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 573 | } |
| 574 | tmperval = elm->type->der_encoder(elm->type, memb_ptr, |
| 575 | elm->tag_mode, elm->tag, |
| 576 | cb, app_key); |
| 577 | if(tmperval.encoded == -1) |
| 578 | return tmperval; |
| 579 | computed_size -= tmperval.encoded; |
vlm | dc20efc | 2005-03-02 01:54:28 +0000 | [diff] [blame] | 580 | ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %ld bytes", |
| 581 | edx, elm->name, td->name, (long)tmperval.encoded); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 582 | } |
| 583 | |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 584 | if(computed_size != 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 585 | /* |
| 586 | * Encoded size is not equal to the computed size. |
| 587 | */ |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 588 | _ASN_ENCODE_FAILED; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 589 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 590 | _ASN_ENCODED_OK(erval); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 591 | } |
| 592 | |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 593 | |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 594 | #undef XER_ADVANCE |
| 595 | #define XER_ADVANCE(num_bytes) do { \ |
| 596 | size_t num = num_bytes; \ |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 597 | buf_ptr = ((const char *)buf_ptr) + num;\ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 598 | size -= num; \ |
| 599 | consumed_myself += num; \ |
| 600 | } while(0) |
| 601 | |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 602 | /* |
| 603 | * Decode the XER (XML) data. |
| 604 | */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 605 | asn_dec_rval_t |
| 606 | SEQUENCE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 607 | void **struct_ptr, const char *opt_mname, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 608 | const void *buf_ptr, size_t size) { |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 609 | /* |
| 610 | * Bring closer parts of structure description. |
| 611 | */ |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 612 | asn_SEQUENCE_specifics_t *specs |
| 613 | = (asn_SEQUENCE_specifics_t *)td->specifics; |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 614 | asn_TYPE_member_t *elements = td->elements; |
| 615 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 616 | |
| 617 | /* |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 618 | * ... and parts of the structure being constructed. |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 619 | */ |
| 620 | void *st = *struct_ptr; /* Target structure. */ |
| 621 | asn_struct_ctx_t *ctx; /* Decoder context */ |
| 622 | |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 623 | asn_dec_rval_t rval; /* Return value from a decoder */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 624 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 625 | int edx; /* Element index */ |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 626 | int edx_end; |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 627 | |
| 628 | /* |
| 629 | * Create the target structure if it is not present already. |
| 630 | */ |
| 631 | if(st == 0) { |
| 632 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 633 | if(st == 0) RETURN(RC_FAIL); |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Restore parsing context. |
| 638 | */ |
| 639 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
| 640 | |
| 641 | |
| 642 | /* |
| 643 | * Phases of XER/XML processing: |
| 644 | * Phase 0: Check that the opening tag matches our expectations. |
| 645 | * Phase 1: Processing body and reacting on closing tag. |
| 646 | * Phase 2: Processing inner type. |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 647 | * Phase 3: Skipping unknown extensions. |
| 648 | * Phase 4: PHASED OUT |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 649 | */ |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 650 | for(edx = ctx->step; ctx->phase <= 3;) { |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 651 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 652 | ssize_t ch_size; /* Chunk size */ |
| 653 | xer_check_tag_e tcv; /* Tag check value */ |
| 654 | asn_TYPE_member_t *elm; |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 655 | int n; |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 656 | |
| 657 | /* |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 658 | * Go inside the inner member of a sequence. |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 659 | */ |
| 660 | if(ctx->phase == 2) { |
| 661 | asn_dec_rval_t tmprval; |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 662 | void *memb_ptr; /* Pointer to the member */ |
| 663 | void **memb_ptr2; /* Pointer to that pointer */ |
| 664 | |
vlm | a351c2b | 2004-10-26 10:12:14 +0000 | [diff] [blame] | 665 | elm = &td->elements[edx]; |
| 666 | |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 667 | if(elm->flags & ATF_POINTER) { |
| 668 | /* Member is a pointer to another structure */ |
| 669 | memb_ptr2 = (void **)((char *)st |
| 670 | + elm->memb_offset); |
| 671 | } else { |
| 672 | memb_ptr = (char *)st + elm->memb_offset; |
| 673 | memb_ptr2 = &memb_ptr; |
| 674 | } |
| 675 | |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 676 | /* Invoke the inner type decoder, m.b. multiple times */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 677 | tmprval = elm->type->xer_decoder(opt_codec_ctx, |
| 678 | elm->type, memb_ptr2, elm->name, |
| 679 | buf_ptr, size); |
| 680 | XER_ADVANCE(tmprval.consumed); |
| 681 | if(tmprval.code != RC_OK) |
| 682 | RETURN(tmprval.code); |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 683 | ctx->phase = 1; /* Back to body processing */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 684 | ctx->step = ++edx; |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 685 | ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d", |
| 686 | ctx->phase, ctx->step); |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 687 | /* Fall through */ |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * Get the next part of the XML stream. |
| 692 | */ |
vlm | c70f8f3 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 693 | ch_size = xer_next_token(&ctx->context, buf_ptr, size, |
| 694 | &ch_type); |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 695 | switch(ch_size) { |
| 696 | case -1: RETURN(RC_FAIL); |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 697 | case 0: RETURN(RC_WMORE); |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 698 | default: |
| 699 | switch(ch_type) { |
| 700 | case PXER_COMMENT: /* Got XML comment */ |
| 701 | case PXER_TEXT: /* Ignore free-standing text */ |
| 702 | XER_ADVANCE(ch_size); /* Skip silently */ |
| 703 | continue; |
| 704 | case PXER_TAG: |
| 705 | break; /* Check the rest down there */ |
| 706 | } |
| 707 | } |
| 708 | |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 709 | tcv = xer_check_tag(buf_ptr, ch_size, xml_tag); |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 710 | ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]", |
| 711 | tcv, ctx->phase, xml_tag); |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 712 | |
| 713 | /* Skip the extensions section */ |
| 714 | if(ctx->phase == 3) { |
| 715 | switch(xer_skip_unknown(tcv, &ctx->left)) { |
| 716 | case -1: |
| 717 | ctx->phase = 4; |
| 718 | RETURN(RC_FAIL); |
| 719 | case 0: |
| 720 | XER_ADVANCE(ch_size); |
| 721 | continue; |
| 722 | case 1: |
| 723 | XER_ADVANCE(ch_size); |
| 724 | ctx->phase = 1; |
| 725 | continue; |
| 726 | case 2: |
| 727 | ctx->phase = 1; |
| 728 | break; |
| 729 | } |
| 730 | } |
| 731 | |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 732 | switch(tcv) { |
| 733 | case XCT_CLOSING: |
| 734 | if(ctx->phase == 0) break; |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 735 | ctx->phase = 0; |
| 736 | /* Fall through */ |
| 737 | case XCT_BOTH: |
| 738 | if(ctx->phase == 0) { |
| 739 | if(edx >= td->elements_count |
| 740 | || |
| 741 | /* Explicit OPTIONAL specs reaches the end */ |
| 742 | (edx + elements[edx].optional |
| 743 | == td->elements_count) |
| 744 | || |
| 745 | /* All extensions are optional */ |
| 746 | (IN_EXTENSION_GROUP(specs, edx) |
| 747 | && specs->ext_before |
| 748 | > td->elements_count) |
| 749 | ) { |
| 750 | XER_ADVANCE(ch_size); |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 751 | ctx->phase = 4; /* Phase out */ |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 752 | RETURN(RC_OK); |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 753 | } else { |
| 754 | ASN_DEBUG("Premature end of XER SEQUENCE"); |
| 755 | RETURN(RC_FAIL); |
| 756 | } |
| 757 | } |
| 758 | /* Fall through */ |
| 759 | case XCT_OPENING: |
| 760 | if(ctx->phase == 0) { |
| 761 | XER_ADVANCE(ch_size); |
| 762 | ctx->phase = 1; /* Processing body phase */ |
| 763 | continue; |
| 764 | } |
vlm | 22d3f5d | 2004-10-23 14:58:15 +0000 | [diff] [blame] | 765 | /* Fall through */ |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 766 | case XCT_UNKNOWN_OP: |
| 767 | case XCT_UNKNOWN_BO: |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 768 | |
vlm | d960a86 | 2005-07-03 05:31:02 +0000 | [diff] [blame] | 769 | ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%d", |
| 770 | tcv, ctx->phase, edx); |
| 771 | if(ctx->phase != 1) { |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 772 | break; /* Really unexpected */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 773 | } |
vlm | d960a86 | 2005-07-03 05:31:02 +0000 | [diff] [blame] | 774 | |
| 775 | if(edx < td->elements_count) { |
| 776 | /* |
| 777 | * Search which member corresponds to this tag. |
| 778 | */ |
| 779 | edx_end = edx + elements[edx].optional + 1; |
| 780 | if(edx_end > td->elements_count) |
| 781 | edx_end = td->elements_count; |
| 782 | for(n = edx; n < edx_end; n++) { |
| 783 | elm = &td->elements[n]; |
| 784 | tcv = xer_check_tag(buf_ptr, |
| 785 | ch_size, elm->name); |
| 786 | switch(tcv) { |
| 787 | case XCT_BOTH: |
| 788 | case XCT_OPENING: |
| 789 | /* |
| 790 | * Process this member. |
| 791 | */ |
| 792 | ctx->step = edx = n; |
| 793 | ctx->phase = 2; |
| 794 | break; |
| 795 | case XCT_UNKNOWN_OP: |
| 796 | case XCT_UNKNOWN_BO: |
| 797 | continue; |
| 798 | default: |
| 799 | n = edx_end; |
| 800 | break; /* Phase out */ |
| 801 | } |
| 802 | break; |
| 803 | } |
| 804 | if(n != edx_end) |
| 805 | continue; |
| 806 | } else { |
| 807 | ASN_DEBUG("Out of defined members: %d/%d", |
| 808 | edx, td->elements_count); |
| 809 | } |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 810 | |
| 811 | /* It is expected extension */ |
| 812 | if(IN_EXTENSION_GROUP(specs, |
vlm | d960a86 | 2005-07-03 05:31:02 +0000 | [diff] [blame] | 813 | edx + (edx < td->elements_count |
| 814 | ? elements[edx].optional : 0))) { |
| 815 | ASN_DEBUG("Got anticipated extension at %d", |
| 816 | edx); |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 817 | /* |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 818 | * Check for (XCT_BOTH or XCT_UNKNOWN_BO) |
| 819 | * By using a mask. Only record a pure |
| 820 | * <opening> tags. |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 821 | */ |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 822 | if(tcv & XCT_CLOSING) { |
| 823 | /* Found </extension> without body */ |
| 824 | } else { |
| 825 | ctx->left = 1; |
| 826 | ctx->phase = 3; /* Skip ...'s */ |
| 827 | } |
| 828 | XER_ADVANCE(ch_size); |
| 829 | continue; |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | /* Fall through */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 833 | default: |
| 834 | break; |
| 835 | } |
| 836 | |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 837 | ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]", |
| 838 | size>0?((const char *)buf_ptr)[0]:'.', |
| 839 | size>1?((const char *)buf_ptr)[1]:'.', |
| 840 | size>2?((const char *)buf_ptr)[2]:'.', |
| 841 | size>3?((const char *)buf_ptr)[3]:'.', |
| 842 | size>4?((const char *)buf_ptr)[4]:'.', |
| 843 | size>5?((const char *)buf_ptr)[5]:'.'); |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 844 | break; |
| 845 | } |
| 846 | |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 847 | ctx->phase = 4; /* "Phase out" on hard failure */ |
vlm | e1adad5 | 2004-10-23 13:27:30 +0000 | [diff] [blame] | 848 | RETURN(RC_FAIL); |
| 849 | } |
| 850 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 851 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 852 | SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 853 | int ilevel, enum xer_encoder_flags_e flags, |
| 854 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 855 | asn_enc_rval_t er; |
| 856 | int xcan = (flags & XER_F_CANONICAL); |
| 857 | int edx; |
| 858 | |
| 859 | if(!sptr) |
| 860 | _ASN_ENCODE_FAILED; |
| 861 | |
| 862 | er.encoded = 0; |
| 863 | |
| 864 | for(edx = 0; edx < td->elements_count; edx++) { |
| 865 | asn_enc_rval_t tmper; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 866 | asn_TYPE_member_t *elm = &td->elements[edx]; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 867 | void *memb_ptr; |
| 868 | const char *mname = elm->name; |
| 869 | unsigned int mlen = strlen(mname); |
| 870 | |
| 871 | if(elm->flags & ATF_POINTER) { |
| 872 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 873 | if(!memb_ptr) { |
| 874 | if(elm->optional) |
| 875 | continue; |
| 876 | /* Mandatory element is missing */ |
| 877 | _ASN_ENCODE_FAILED; |
| 878 | } |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 879 | } else { |
| 880 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
| 881 | } |
| 882 | |
| 883 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel); |
| 884 | _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1); |
| 885 | |
| 886 | /* Print the member itself */ |
| 887 | tmper = elm->type->xer_encoder(elm->type, memb_ptr, |
| 888 | ilevel + 1, flags, cb, app_key); |
| 889 | if(tmper.encoded == -1) return tmper; |
| 890 | |
| 891 | _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1); |
| 892 | er.encoded += 5 + (2 * mlen) + tmper.encoded; |
| 893 | } |
| 894 | |
| 895 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1); |
| 896 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 897 | _ASN_ENCODED_OK(er); |
vlm | 84d551b | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 898 | cb_failed: |
| 899 | _ASN_ENCODE_FAILED; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 900 | } |
| 901 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 902 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 903 | SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 904 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 905 | int edx; |
| 906 | int ret; |
| 907 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 908 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 909 | |
| 910 | /* Dump preamble */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 911 | if(cb(td->name, strlen(td->name), app_key) < 0 |
| 912 | || cb(" ::= {", 6, app_key) < 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 913 | return -1; |
| 914 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 915 | for(edx = 0; edx < td->elements_count; edx++) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 916 | asn_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 917 | const void *memb_ptr; |
| 918 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 919 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 920 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 921 | if(!memb_ptr) { |
| 922 | if(elm->optional) continue; |
| 923 | /* Print <absent> line */ |
| 924 | /* Fall through */ |
| 925 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 926 | } else { |
| 927 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 928 | } |
| 929 | |
| 930 | /* Indentation */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 931 | _i_INDENT(1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 932 | |
| 933 | /* Print the member's name and stuff */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 934 | if(cb(elm->name, strlen(elm->name), app_key) < 0 |
| 935 | || cb(": ", 2, app_key) < 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 936 | return -1; |
| 937 | |
| 938 | /* Print the member itself */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 939 | ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 940 | cb, app_key); |
| 941 | if(ret) return ret; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 942 | } |
| 943 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 944 | ilevel--; |
| 945 | _i_INDENT(1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 946 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 947 | return (cb("}", 1, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | void |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 951 | SEQUENCE_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 952 | int edx; |
| 953 | |
| 954 | if(!td || !sptr) |
| 955 | return; |
| 956 | |
| 957 | ASN_DEBUG("Freeing %s as SEQUENCE", td->name); |
| 958 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 959 | for(edx = 0; edx < td->elements_count; edx++) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 960 | asn_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 961 | void *memb_ptr; |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 962 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 963 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
| 964 | if(memb_ptr) |
vlm | be78c80 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 965 | ASN_STRUCT_FREE(*elm->type, memb_ptr); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 966 | } else { |
| 967 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
vlm | be78c80 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 968 | ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
| 972 | if(!contents_only) { |
| 973 | FREEMEM(sptr); |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 978 | SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
vlm | af68ef5 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 979 | asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 980 | int edx; |
| 981 | |
| 982 | if(!sptr) { |
vlm | 18269b6 | 2006-10-16 12:18:41 +0000 | [diff] [blame] | 983 | _ASN_CTFAIL(app_key, td, sptr, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 984 | "%s: value not given (%s:%d)", |
| 985 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 986 | return -1; |
| 987 | } |
| 988 | |
| 989 | /* |
| 990 | * Iterate over structure members and check their validity. |
| 991 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 992 | for(edx = 0; edx < td->elements_count; edx++) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 993 | asn_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 994 | const void *memb_ptr; |
| 995 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 996 | if(elm->flags & ATF_POINTER) { |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 997 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 998 | if(!memb_ptr) { |
| 999 | if(elm->optional) |
| 1000 | continue; |
vlm | 18269b6 | 2006-10-16 12:18:41 +0000 | [diff] [blame] | 1001 | _ASN_CTFAIL(app_key, td, sptr, |
vlm | 31f7a1c | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 1002 | "%s: mandatory element %s absent (%s:%d)", |
| 1003 | td->name, elm->name, __FILE__, __LINE__); |
| 1004 | return -1; |
| 1005 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1006 | } else { |
| 1007 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 1008 | } |
| 1009 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 1010 | if(elm->memb_constraints) { |
| 1011 | int ret = elm->memb_constraints(elm->type, memb_ptr, |
vlm | af68ef5 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 1012 | ctfailcb, app_key); |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 1013 | if(ret) return ret; |
| 1014 | } else { |
| 1015 | int ret = elm->type->check_constraints(elm->type, |
vlm | af68ef5 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 1016 | memb_ptr, ctfailcb, app_key); |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 1017 | if(ret) return ret; |
| 1018 | /* |
| 1019 | * Cannot inherit it earlier: |
| 1020 | * need to make sure we get the updated version. |
| 1021 | */ |
| 1022 | elm->memb_constraints = elm->type->check_constraints; |
| 1023 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | return 0; |
| 1027 | } |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1028 | |
| 1029 | asn_dec_rval_t |
| 1030 | SEQUENCE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 1031 | asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) { |
| 1032 | asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics; |
| 1033 | void *st = *sptr; /* Target structure. */ |
| 1034 | int extpresent = 0; /* Extension additions are present */ |
| 1035 | uint8_t *opres; /* Presence of optional root members */ |
| 1036 | asn_per_data_t opmd; |
| 1037 | asn_dec_rval_t rv; |
| 1038 | int edx; |
| 1039 | |
| 1040 | (void)constraints; |
| 1041 | |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 1042 | if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx)) |
| 1043 | _ASN_DECODE_FAILED; |
| 1044 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1045 | if(!st) { |
| 1046 | st = *sptr = CALLOC(1, specs->struct_size); |
| 1047 | if(!st) _ASN_DECODE_FAILED; |
| 1048 | } |
| 1049 | |
| 1050 | ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name); |
| 1051 | |
| 1052 | /* Handle extensions */ |
| 1053 | if(specs->ext_before >= 0) { |
| 1054 | extpresent = per_get_few_bits(pd, 1); |
vlm | 619448e | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 1055 | if(extpresent < 0) _ASN_DECODE_STARVED; |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | /* Prepare a place and read-in the presence bitmap */ |
| 1059 | if(specs->roms_count) { |
| 1060 | opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1); |
| 1061 | if(!opres) _ASN_DECODE_FAILED; |
| 1062 | /* Get the presence map */ |
| 1063 | if(per_get_many_bits(pd, opres, 0, specs->roms_count)) { |
| 1064 | FREEMEM(opres); |
vlm | 619448e | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 1065 | _ASN_DECODE_STARVED; |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1066 | } |
| 1067 | opmd.buffer = opres; |
| 1068 | opmd.nboff = 0; |
| 1069 | opmd.nbits = specs->roms_count; |
| 1070 | ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)", |
| 1071 | td->name, specs->roms_count, *opres); |
| 1072 | } else { |
| 1073 | opres = 0; |
| 1074 | memset(&opmd, 0, sizeof opmd); |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * Get the sequence ROOT elements. |
| 1079 | */ |
| 1080 | for(edx = 0; edx < ((specs->ext_before < 0) |
| 1081 | ? td->elements_count : specs->ext_before + 1); edx++) { |
| 1082 | asn_TYPE_member_t *elm = &td->elements[edx]; |
| 1083 | void *memb_ptr; /* Pointer to the member */ |
| 1084 | void **memb_ptr2; /* Pointer to that pointer */ |
| 1085 | |
| 1086 | /* Fetch the pointer to this member */ |
| 1087 | if(elm->flags & ATF_POINTER) { |
| 1088 | memb_ptr2 = (void **)((char *)st + elm->memb_offset); |
| 1089 | } else { |
| 1090 | memb_ptr = (char *)st + elm->memb_offset; |
| 1091 | memb_ptr2 = &memb_ptr; |
| 1092 | } |
| 1093 | |
| 1094 | /* Deal with optionality */ |
| 1095 | if(elm->optional) { |
| 1096 | int present = per_get_few_bits(&opmd, 1); |
| 1097 | ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)", |
| 1098 | td->name, elm->name, present, |
| 1099 | (int)opmd.nboff, (int)opmd.nbits); |
| 1100 | if(present == 0) { |
| 1101 | /* This element is not present */ |
| 1102 | if(elm->default_value) { |
| 1103 | /* Fill-in DEFAULT */ |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1104 | if(elm->default_value(1, memb_ptr2)) { |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1105 | FREEMEM(opres); |
| 1106 | _ASN_DECODE_FAILED; |
| 1107 | } |
| 1108 | } |
| 1109 | /* The member is just not present */ |
| 1110 | continue; |
| 1111 | } |
| 1112 | /* Fall through */ |
| 1113 | } |
| 1114 | |
| 1115 | /* Fetch the member from the stream */ |
| 1116 | ASN_DEBUG("Decoding member %s in %s", elm->name, td->name); |
| 1117 | rv = elm->type->uper_decoder(opt_codec_ctx, elm->type, |
| 1118 | elm->per_constraints, memb_ptr2, pd); |
| 1119 | if(rv.code != RC_OK) { |
| 1120 | ASN_DEBUG("Failed decode %s in %s", |
| 1121 | elm->name, td->name); |
| 1122 | FREEMEM(opres); |
| 1123 | return rv; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * Deal with extensions. |
| 1129 | */ |
| 1130 | if(extpresent) { |
| 1131 | ASN_DEBUG("Extensibility for %s: NOT IMPLEMENTED", td->name); |
| 1132 | _ASN_DECODE_FAILED; |
| 1133 | } else { |
| 1134 | for(edx = specs->roms_count; edx < specs->roms_count |
| 1135 | + specs->aoms_count; edx++) { |
| 1136 | asn_TYPE_member_t *elm = &td->elements[edx]; |
| 1137 | void *memb_ptr; /* Pointer to the member */ |
| 1138 | void **memb_ptr2; /* Pointer to that pointer */ |
| 1139 | |
| 1140 | if(!elm->default_value) continue; |
| 1141 | |
| 1142 | /* Fetch the pointer to this member */ |
| 1143 | if(elm->flags & ATF_POINTER) { |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1144 | memb_ptr2 = (void **)((char *)st |
| 1145 | + elm->memb_offset); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1146 | } else { |
| 1147 | memb_ptr = (char *)st + elm->memb_offset; |
| 1148 | memb_ptr2 = &memb_ptr; |
| 1149 | } |
| 1150 | |
| 1151 | /* Set default value */ |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1152 | if(elm->default_value(1, memb_ptr2)) { |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1153 | FREEMEM(opres); |
| 1154 | _ASN_DECODE_FAILED; |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | rv.consumed = 0; |
| 1160 | rv.code = RC_OK; |
vlm | 229ed3c | 2005-11-30 14:10:50 +0000 | [diff] [blame] | 1161 | FREEMEM(opres); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1162 | return rv; |
| 1163 | } |
| 1164 | |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1165 | asn_enc_rval_t |
| 1166 | SEQUENCE_encode_uper(asn_TYPE_descriptor_t *td, |
| 1167 | asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) { |
| 1168 | asn_SEQUENCE_specifics_t *specs |
| 1169 | = (asn_SEQUENCE_specifics_t *)td->specifics; |
| 1170 | asn_enc_rval_t er; |
| 1171 | int edx; |
| 1172 | int i; |
| 1173 | |
| 1174 | (void)constraints; |
| 1175 | |
| 1176 | if(!sptr) |
| 1177 | _ASN_ENCODE_FAILED; |
| 1178 | |
| 1179 | er.encoded = 0; |
| 1180 | |
| 1181 | ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name); |
| 1182 | if(specs->ext_before >= 0) |
| 1183 | _ASN_ENCODE_FAILED; /* We don't encode extensions yet */ |
| 1184 | |
| 1185 | /* Encode a presence bitmap */ |
| 1186 | for(i = 0; i < specs->roms_count; i++) { |
vlm | 1fcf759 | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 1187 | asn_TYPE_member_t *elm; |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1188 | void *memb_ptr; /* Pointer to the member */ |
| 1189 | void **memb_ptr2; /* Pointer to that pointer */ |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1190 | int present; |
| 1191 | |
vlm | 1fcf759 | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 1192 | edx = specs->oms[i]; |
| 1193 | elm = &td->elements[edx]; |
| 1194 | |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1195 | /* Fetch the pointer to this member */ |
| 1196 | if(elm->flags & ATF_POINTER) { |
| 1197 | memb_ptr2 = (void **)((char *)sptr + elm->memb_offset); |
| 1198 | present = (*memb_ptr2 != 0); |
| 1199 | } else { |
| 1200 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
| 1201 | memb_ptr2 = &memb_ptr; |
| 1202 | present = 1; |
| 1203 | } |
| 1204 | |
| 1205 | /* Eliminate default values */ |
| 1206 | if(present && elm->default_value |
| 1207 | && elm->default_value(0, memb_ptr2) == 1) |
| 1208 | present = 0; |
| 1209 | |
| 1210 | ASN_DEBUG("Element %s %s %s->%s is %s", |
| 1211 | elm->flags & ATF_POINTER ? "ptr" : "inline", |
| 1212 | elm->default_value ? "def" : "wtv", |
| 1213 | td->name, elm->name, present ? "present" : "absent"); |
| 1214 | if(per_put_few_bits(po, present, 1)) |
| 1215 | _ASN_ENCODE_FAILED; |
| 1216 | } |
| 1217 | |
| 1218 | /* |
| 1219 | * Get the sequence ROOT elements. |
| 1220 | */ |
| 1221 | for(edx = 0; edx < ((specs->ext_before < 0) |
| 1222 | ? td->elements_count : specs->ext_before + 1); edx++) { |
| 1223 | asn_TYPE_member_t *elm = &td->elements[edx]; |
| 1224 | void *memb_ptr; /* Pointer to the member */ |
| 1225 | void **memb_ptr2; /* Pointer to that pointer */ |
| 1226 | |
| 1227 | /* Fetch the pointer to this member */ |
| 1228 | if(elm->flags & ATF_POINTER) { |
| 1229 | memb_ptr2 = (void **)((char *)sptr + elm->memb_offset); |
| 1230 | if(!*memb_ptr2) { |
| 1231 | ASN_DEBUG("Element %s %d not present", |
| 1232 | elm->name, edx); |
| 1233 | if(elm->optional) |
| 1234 | continue; |
| 1235 | /* Mandatory element is missing */ |
| 1236 | _ASN_ENCODE_FAILED; |
| 1237 | } |
| 1238 | } else { |
| 1239 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
| 1240 | memb_ptr2 = &memb_ptr; |
| 1241 | } |
| 1242 | |
| 1243 | /* Eliminate default values */ |
| 1244 | if(elm->default_value && elm->default_value(0, memb_ptr2) == 1) |
| 1245 | continue; |
| 1246 | |
| 1247 | er = elm->type->uper_encoder(elm->type, elm->per_constraints, |
| 1248 | *memb_ptr2, po); |
| 1249 | if(er.encoded == -1) |
| 1250 | return er; |
| 1251 | } |
| 1252 | |
| 1253 | _ASN_ENCODED_OK(er); |
| 1254 | } |
| 1255 | |