vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <constr_SEQUENCE.h> |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 7 | #include <assert.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 36 | ptr = ((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 | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 77 | const asn1_TYPE_tag2member_t *a = (const asn1_TYPE_tag2member_t *)ap; |
| 78 | const asn1_TYPE_tag2member_t *b = (const asn1_TYPE_tag2member_t *)bp; |
| 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 | */ |
| 110 | ber_dec_rval_t |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 111 | SEQUENCE_decode_ber(asn1_TYPE_descriptor_t *td, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 112 | void **struct_ptr, void *ptr, size_t size, int tag_mode) { |
| 113 | /* |
| 114 | * Bring closer parts of structure description. |
| 115 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 116 | asn1_SEQUENCE_specifics_t *specs = (asn1_SEQUENCE_specifics_t *)td->specifics; |
| 117 | asn1_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. */ |
| 123 | ber_dec_ctx_t *ctx; /* Decoder context */ |
| 124 | |
| 125 | ber_tlv_tag_t tlv_tag; /* T from TLV */ |
| 126 | //ber_tlv_len_t tlv_len; /* L from TLV */ |
| 127 | ber_dec_rval_t rval; /* Return code from subparsers */ |
| 128 | |
| 129 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
| 130 | int edx; /* SEQUENCE element's index */ |
| 131 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 132 | ASN_DEBUG("Decoding %s as SEQUENCE", td->name); |
vlm | fa67ddc | 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 | */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 147 | ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset); |
vlm | fa67ddc | 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 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 160 | rval = ber_check_tags(td, ctx, ptr, size, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 161 | tag_mode, &ctx->left, 0); |
| 162 | if(rval.code != RC_OK) { |
| 163 | ASN_DEBUG("%s tagging check failed: %d", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 164 | td->name, rval.code); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 165 | consumed_myself += rval.consumed; |
| 166 | RETURN(rval.code); |
| 167 | } |
| 168 | |
| 169 | if(ctx->left >= 0) |
| 170 | ctx->left += rval.consumed; /* ?Substracted below! */ |
| 171 | ADVANCE(rval.consumed); |
| 172 | |
| 173 | NEXT_PHASE(ctx); |
| 174 | |
| 175 | ASN_DEBUG("Structure consumes %ld bytes, buffer %ld", |
| 176 | (long)ctx->left, (long)size); |
| 177 | |
| 178 | /* Fall through */ |
| 179 | case 1: |
| 180 | /* |
| 181 | * PHASE 1. |
| 182 | * From the place where we've left it previously, |
| 183 | * try to decode the next member from the list of |
| 184 | * this structure's elements. |
| 185 | * (ctx->step) stores the member being processed |
| 186 | * between invocations and the microphase {0,1} of parsing |
| 187 | * that member: |
| 188 | * step = (<member_number> * 2 + <microphase>). |
| 189 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 190 | for(edx = (ctx->step >> 1); edx < td->elements_count; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 191 | edx++, ctx->step = (ctx->step & ~1) + 2) { |
| 192 | void *memb_ptr; /* Pointer to the member */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 193 | void **memb_ptr2; /* Pointer to that pointer */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 194 | ssize_t tag_len; /* Length of TLV's T */ |
| 195 | int opt_edx_end; /* Next non-optional element */ |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 196 | int use_bsearch; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 197 | int n; |
| 198 | |
| 199 | if(ctx->step & 1) |
| 200 | goto microphase2; |
| 201 | |
| 202 | /* |
| 203 | * MICROPHASE 1: Synchronize decoding. |
| 204 | */ |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 205 | ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d" |
| 206 | " opt=%d ec=%d", |
| 207 | td->name, (int)ctx->left, edx, |
| 208 | elements[edx].flags, elements[edx].optional, |
| 209 | td->elements_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | |
| 211 | if(ctx->left == 0 /* No more stuff is expected */ |
| 212 | && ( |
| 213 | /* Explicit OPTIONAL specification reaches the end */ |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 214 | (edx + elements[edx].optional |
| 215 | == td->elements_count) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 216 | || |
| 217 | /* All extensions are optional */ |
| 218 | (IN_EXTENSION_GROUP(specs, edx) |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 219 | && specs->ext_before > td->elements_count) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 220 | ) |
| 221 | ) { |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 222 | ASN_DEBUG("End of SEQUENCE %s", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 223 | /* |
| 224 | * Found the legitimate end of the structure. |
| 225 | */ |
| 226 | PHASE_OUT(ctx); |
| 227 | RETURN(RC_OK); |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Fetch the T from TLV. |
| 232 | */ |
| 233 | tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
vlm | 1308d2b | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 234 | ASN_DEBUG("Current tag in %s SEQUENCE for element %d " |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 235 | "(%s) is %s encoded in %d bytes, of frame %ld", |
vlm | 1308d2b | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 236 | td->name, edx, elements[edx].name, |
| 237 | ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 238 | switch(tag_len) { |
| 239 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 240 | /* Fall through */ |
| 241 | case -1: RETURN(RC_FAIL); |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Find the next available type with this tag. |
| 246 | */ |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 247 | use_bsearch = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 248 | opt_edx_end = edx + elements[edx].optional + 1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 249 | if(opt_edx_end > td->elements_count) |
| 250 | opt_edx_end = td->elements_count; /* Cap */ |
vlm | 5d219bf | 2004-07-01 00:48:04 +0000 | [diff] [blame] | 251 | else if(opt_edx_end - edx > 8) { |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 252 | /* Limit the scope of linear search... */ |
vlm | 5d219bf | 2004-07-01 00:48:04 +0000 | [diff] [blame] | 253 | opt_edx_end = edx + 8; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 254 | use_bsearch = 1; |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 255 | /* ... and resort to bsearch() */ |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 256 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 257 | for(n = edx; n < opt_edx_end; n++) { |
| 258 | if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) { |
| 259 | /* |
| 260 | * Found element corresponding to the tag |
| 261 | * being looked at. |
| 262 | * Reposition over the right element. |
| 263 | */ |
| 264 | edx = n; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 265 | ctx->step = 1 + 2 * edx; /* Remember! */ |
| 266 | goto microphase2; |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 267 | } else if(elements[n].flags & ATF_OPEN_TYPE) { |
| 268 | /* |
| 269 | * This is the ANY type, which may bear |
| 270 | * any flag whatsoever. |
| 271 | */ |
| 272 | edx = n; |
| 273 | ctx->step = 1 + 2 * edx; /* Remember! */ |
| 274 | goto microphase2; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 275 | } else if(elements[n].tag == (ber_tlv_tag_t)-1) { |
| 276 | use_bsearch = 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 277 | break; |
| 278 | } |
| 279 | } |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 280 | if(use_bsearch) { |
| 281 | /* |
vlm | 141874a | 2004-09-04 06:17:35 +0000 | [diff] [blame] | 282 | * Resort to a binary search over |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 283 | * sorted array of tags. |
| 284 | */ |
| 285 | asn1_TYPE_tag2member_t *t2m; |
| 286 | asn1_TYPE_tag2member_t key; |
| 287 | key.el_tag = tlv_tag; |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 288 | key.el_no = edx; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 289 | (void *)t2m = bsearch(&key, |
| 290 | specs->tag2el, specs->tag2el_count, |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 291 | sizeof(specs->tag2el[0]), _t2e_cmp); |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 292 | if(t2m) { |
| 293 | asn1_TYPE_tag2member_t *best = 0; |
| 294 | asn1_TYPE_tag2member_t *t2m_f, *t2m_l; |
| 295 | int edx_max = edx + elements[edx].optional; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 296 | /* |
| 297 | * Rewind to the first element with that tag, |
| 298 | * `cause bsearch() does not guarantee order. |
| 299 | */ |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 300 | t2m_f = t2m + t2m->toff_first; |
| 301 | t2m_l = t2m + t2m->toff_last; |
| 302 | for(t2m = t2m_f; t2m <= t2m_l; t2m++) { |
| 303 | if(t2m->el_no > edx_max) break; |
| 304 | if(t2m->el_no < edx) continue; |
| 305 | best = t2m; |
| 306 | } |
| 307 | if(best) { |
| 308 | edx = best->el_no; |
| 309 | ctx->step = 1 + 2 * edx; |
| 310 | goto microphase2; |
| 311 | } |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 312 | } |
| 313 | n = opt_edx_end; |
| 314 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 315 | if(n == opt_edx_end) { |
| 316 | /* |
| 317 | * If tag is unknown, it may be either |
| 318 | * an unknown (thus, incorrect) tag, |
| 319 | * or an extension (...), |
| 320 | * or an end of the indefinite-length structure. |
| 321 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 322 | if(!IN_EXTENSION_GROUP(specs, edx)) { |
| 323 | ASN_DEBUG("Unexpected tag %s", |
| 324 | ber_tlv_tag_string(tlv_tag)); |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 325 | ASN_DEBUG("Expected tag %s (%s)%s", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 326 | ber_tlv_tag_string(elements[edx].tag), |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 327 | elements[edx].name, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 328 | elements[edx].optional |
| 329 | ?" or alternatives":""); |
| 330 | RETURN(RC_FAIL); |
| 331 | } |
| 332 | |
| 333 | if(ctx->left < 0 |
| 334 | && ((uint8_t *)ptr)[0] == 0) { |
| 335 | if(LEFT < 2) { |
| 336 | if(SIZE_VIOLATION) |
| 337 | RETURN(RC_FAIL); |
| 338 | else |
| 339 | RETURN(RC_WMORE); |
| 340 | } else if(((uint8_t *)ptr)[1] == 0) { |
| 341 | /* |
| 342 | * Yeah, baby! Found the terminator |
| 343 | * of the indefinite length structure. |
| 344 | */ |
| 345 | /* |
| 346 | * Proceed to the canonical |
| 347 | * finalization function. |
| 348 | * No advancing is necessary. |
| 349 | */ |
| 350 | goto phase3; |
| 351 | } |
| 352 | } else { |
| 353 | /* Skip this tag */ |
| 354 | ssize_t skip; |
| 355 | |
| 356 | skip = ber_skip_length( |
| 357 | BER_TLV_CONSTRUCTED(ptr), |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 358 | (char *)ptr + tag_len, LEFT - tag_len); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 359 | ASN_DEBUG("Skip length %d in %s", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 360 | (int)skip, td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 361 | switch(skip) { |
| 362 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 363 | /* Fall through */ |
| 364 | case -1: RETURN(RC_FAIL); |
| 365 | } |
| 366 | |
| 367 | ADVANCE(skip + tag_len); |
| 368 | ctx->step -= 2; |
| 369 | edx--; |
| 370 | continue; /* Try again with the next tag */ |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * MICROPHASE 2: Invoke the member-specific decoder. |
| 376 | */ |
| 377 | ctx->step |= 1; /* Confirm entering next microphase */ |
| 378 | microphase2: |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 379 | ASN_DEBUG("Inside SEQUENCE %s MF2", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 380 | |
| 381 | /* |
| 382 | * Compute the position of the member inside a structure, |
| 383 | * and also a type of containment (it may be contained |
| 384 | * as pointer or using inline inclusion). |
| 385 | */ |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 386 | if(elements[edx].flags & ATF_POINTER) { |
| 387 | /* Member is a pointer to another structure */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 388 | memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 389 | } else { |
| 390 | /* |
| 391 | * A pointer to a pointer |
| 392 | * holding the start of the structure |
| 393 | */ |
| 394 | memb_ptr = (char *)st + elements[edx].memb_offset; |
| 395 | memb_ptr2 = &memb_ptr; |
| 396 | } |
| 397 | /* |
| 398 | * Invoke the member fetch routine according to member's type |
| 399 | */ |
| 400 | rval = elements[edx].type->ber_decoder( |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 401 | elements[edx].type, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 402 | memb_ptr2, ptr, LEFT, |
| 403 | elements[edx].tag_mode); |
vlm | ef16e8c | 2004-09-04 04:44:11 +0000 | [diff] [blame] | 404 | ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d " |
vlm | eca9d62 | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 405 | "in %d bytes rval.code %d, size=%d", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 406 | td->name, edx, elements[edx].type->name, |
vlm | eca9d62 | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 407 | (int)LEFT, (int)rval.consumed, rval.code, (int)size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 408 | switch(rval.code) { |
| 409 | case RC_OK: |
| 410 | break; |
| 411 | case RC_WMORE: /* More data expected */ |
| 412 | if(!SIZE_VIOLATION) { |
| 413 | ADVANCE(rval.consumed); |
| 414 | RETURN(RC_WMORE); |
| 415 | } |
vlm | eca9d62 | 2004-09-24 20:59:27 +0000 | [diff] [blame] | 416 | ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)", |
| 417 | (long)ctx->left, (long)size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 418 | /* Fall through */ |
| 419 | case RC_FAIL: /* Fatal error */ |
| 420 | RETURN(RC_FAIL); |
| 421 | } /* switch(rval) */ |
| 422 | |
| 423 | ADVANCE(rval.consumed); |
| 424 | } /* for(all structure members) */ |
| 425 | |
| 426 | phase3: |
| 427 | ctx->phase = 3; |
| 428 | case 3: /* 00 and other tags expected */ |
| 429 | case 4: /* only 00's expected */ |
| 430 | |
| 431 | ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 432 | td->name, (long)ctx->left, (long)size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 433 | |
| 434 | /* |
| 435 | * Skip everything until the end of the SEQUENCE. |
| 436 | */ |
| 437 | while(ctx->left) { |
| 438 | ssize_t tl, ll; |
| 439 | |
| 440 | tl = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 441 | switch(tl) { |
| 442 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 443 | /* Fall through */ |
| 444 | case -1: RETURN(RC_FAIL); |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * If expected <0><0>... |
| 449 | */ |
| 450 | if(ctx->left < 0 |
| 451 | && ((uint8_t *)ptr)[0] == 0) { |
| 452 | if(LEFT < 2) { |
| 453 | if(SIZE_VIOLATION) |
| 454 | RETURN(RC_FAIL); |
| 455 | else |
| 456 | RETURN(RC_WMORE); |
| 457 | } else if(((uint8_t *)ptr)[1] == 0) { |
| 458 | /* |
| 459 | * Correctly finished with <0><0>. |
| 460 | */ |
| 461 | ADVANCE(2); |
| 462 | ctx->left++; |
| 463 | ctx->phase = 4; |
| 464 | continue; |
| 465 | } |
| 466 | } |
| 467 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 468 | if(!IN_EXTENSION_GROUP(specs, td->elements_count) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 469 | || ctx->phase == 4) { |
| 470 | ASN_DEBUG("Unexpected continuation " |
| 471 | "of a non-extensible type " |
| 472 | "%s (SEQUENCE): %s", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 473 | td->name, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 474 | ber_tlv_tag_string(tlv_tag)); |
| 475 | RETURN(RC_FAIL); |
| 476 | } |
| 477 | |
| 478 | ll = ber_skip_length( |
| 479 | BER_TLV_CONSTRUCTED(ptr), |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 480 | (char *)ptr + tl, LEFT - tl); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 481 | switch(ll) { |
| 482 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 483 | /* Fall through */ |
| 484 | case -1: RETURN(RC_FAIL); |
| 485 | } |
| 486 | |
| 487 | ADVANCE(tl + ll); |
| 488 | } |
| 489 | |
| 490 | PHASE_OUT(ctx); |
| 491 | } |
| 492 | |
| 493 | RETURN(RC_OK); |
| 494 | } |
| 495 | |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 496 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 497 | /* |
| 498 | * The DER encoder of the SEQUENCE type. |
| 499 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 500 | asn_enc_rval_t |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 501 | SEQUENCE_encode_der(asn1_TYPE_descriptor_t *td, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 502 | void *ptr, int tag_mode, ber_tlv_tag_t tag, |
| 503 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 504 | size_t computed_size = 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 505 | asn_enc_rval_t erval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 506 | ssize_t ret; |
| 507 | int edx; |
| 508 | |
| 509 | ASN_DEBUG("%s %s as SEQUENCE", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 510 | cb?"Encoding":"Estimating", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 511 | |
| 512 | /* |
| 513 | * Gather the length of the underlying members sequence. |
| 514 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 515 | for(edx = 0; edx < td->elements_count; edx++) { |
| 516 | asn1_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 517 | void *memb_ptr; |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 518 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 519 | memb_ptr = *(void **)((char *)ptr + elm->memb_offset); |
| 520 | if(!memb_ptr) continue; |
| 521 | } else { |
| 522 | memb_ptr = (void *)((char *)ptr + elm->memb_offset); |
| 523 | } |
| 524 | erval = elm->type->der_encoder(elm->type, memb_ptr, |
| 525 | elm->tag_mode, elm->tag, |
| 526 | 0, 0); |
| 527 | if(erval.encoded == -1) |
| 528 | return erval; |
| 529 | computed_size += erval.encoded; |
| 530 | ASN_DEBUG("Member %d %s estimated %ld bytes", |
| 531 | edx, elm->name, (long)erval.encoded); |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Encode the TLV for the sequence itself. |
| 536 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 537 | ret = der_write_tags(td, computed_size, tag_mode, tag, cb, app_key); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 538 | ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size); |
| 539 | if(ret == -1) { |
| 540 | erval.encoded = -1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 541 | erval.failed_type = td; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 542 | erval.structure_ptr = ptr; |
| 543 | return erval; |
| 544 | } |
| 545 | erval.encoded = computed_size + ret; |
| 546 | |
| 547 | if(!cb) return erval; |
| 548 | |
| 549 | /* |
| 550 | * Encode all members. |
| 551 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 552 | for(edx = 0; edx < td->elements_count; edx++) { |
| 553 | asn1_TYPE_member_t *elm = &td->elements[edx]; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 554 | asn_enc_rval_t tmperval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 555 | void *memb_ptr; |
| 556 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 557 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 558 | memb_ptr = *(void **)((char *)ptr + elm->memb_offset); |
| 559 | if(!memb_ptr) continue; |
| 560 | } else { |
| 561 | memb_ptr = (void *)((char *)ptr + elm->memb_offset); |
| 562 | } |
| 563 | tmperval = elm->type->der_encoder(elm->type, memb_ptr, |
| 564 | elm->tag_mode, elm->tag, |
| 565 | cb, app_key); |
| 566 | if(tmperval.encoded == -1) |
| 567 | return tmperval; |
| 568 | computed_size -= tmperval.encoded; |
| 569 | ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %d bytes", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 570 | edx, elm->name, td->name, tmperval.encoded); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | if(computed_size != 0) { |
| 574 | /* |
| 575 | * Encoded size is not equal to the computed size. |
| 576 | */ |
| 577 | erval.encoded = -1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 578 | erval.failed_type = td; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 579 | erval.structure_ptr = ptr; |
| 580 | } |
| 581 | |
| 582 | return erval; |
| 583 | } |
| 584 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 585 | asn_enc_rval_t |
| 586 | SEQUENCE_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr, |
| 587 | int ilevel, enum xer_encoder_flags_e flags, |
| 588 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 589 | asn_enc_rval_t er; |
| 590 | int xcan = (flags & XER_F_CANONICAL); |
| 591 | int edx; |
| 592 | |
| 593 | if(!sptr) |
| 594 | _ASN_ENCODE_FAILED; |
| 595 | |
| 596 | er.encoded = 0; |
| 597 | |
| 598 | for(edx = 0; edx < td->elements_count; edx++) { |
| 599 | asn_enc_rval_t tmper; |
| 600 | asn1_TYPE_member_t *elm = &td->elements[edx]; |
| 601 | void *memb_ptr; |
| 602 | const char *mname = elm->name; |
| 603 | unsigned int mlen = strlen(mname); |
| 604 | |
| 605 | if(elm->flags & ATF_POINTER) { |
| 606 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
| 607 | if(!memb_ptr) continue; /* OPTIONAL element? */ |
| 608 | } else { |
| 609 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
| 610 | } |
| 611 | |
| 612 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel); |
| 613 | _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1); |
| 614 | |
| 615 | /* Print the member itself */ |
| 616 | tmper = elm->type->xer_encoder(elm->type, memb_ptr, |
| 617 | ilevel + 1, flags, cb, app_key); |
| 618 | if(tmper.encoded == -1) return tmper; |
| 619 | |
| 620 | _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1); |
| 621 | er.encoded += 5 + (2 * mlen) + tmper.encoded; |
| 622 | } |
| 623 | |
| 624 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1); |
| 625 | |
| 626 | return er; |
| 627 | } |
| 628 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 629 | int |
| 630 | SEQUENCE_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 631 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 632 | int edx; |
| 633 | int ret; |
| 634 | |
| 635 | if(!sptr) return cb("<absent>", 8, app_key); |
| 636 | |
| 637 | /* Dump preamble */ |
| 638 | if(cb(td->name, strlen(td->name), app_key) |
| 639 | || cb(" ::= {\n", 7, app_key)) |
| 640 | return -1; |
| 641 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 642 | for(edx = 0; edx < td->elements_count; edx++) { |
| 643 | asn1_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 644 | const void *memb_ptr; |
| 645 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 646 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 647 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
| 648 | if(!memb_ptr) continue; |
| 649 | } else { |
| 650 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 651 | } |
| 652 | |
| 653 | /* Indentation */ |
| 654 | for(ret = 0; ret < ilevel; ret++) cb(" ", 1, app_key); |
| 655 | |
| 656 | /* Print the member's name and stuff */ |
| 657 | if(cb(elm->name, strlen(elm->name), app_key) |
| 658 | || cb(": ", 2, app_key)) |
| 659 | return -1; |
| 660 | |
| 661 | /* Print the member itself */ |
| 662 | ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 4, |
| 663 | cb, app_key); |
| 664 | if(ret) return ret; |
| 665 | |
| 666 | /* Print out the terminator */ |
| 667 | ret = cb("\n", 1, app_key); |
| 668 | if(ret) return ret; |
| 669 | } |
| 670 | |
| 671 | /* Indentation */ |
| 672 | for(ret = 0; ret < ilevel - 4; ret++) cb(" ", 1, app_key); |
| 673 | |
| 674 | return cb("}", 1, app_key); |
| 675 | } |
| 676 | |
| 677 | void |
| 678 | SEQUENCE_free(asn1_TYPE_descriptor_t *td, void *sptr, int contents_only) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 679 | int edx; |
| 680 | |
| 681 | if(!td || !sptr) |
| 682 | return; |
| 683 | |
| 684 | ASN_DEBUG("Freeing %s as SEQUENCE", td->name); |
| 685 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 686 | for(edx = 0; edx < td->elements_count; edx++) { |
| 687 | asn1_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 688 | void *memb_ptr; |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 689 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 690 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
| 691 | if(memb_ptr) |
| 692 | elm->type->free_struct(elm->type, memb_ptr, 0); |
| 693 | } else { |
| 694 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
| 695 | elm->type->free_struct(elm->type, memb_ptr, 1); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | if(!contents_only) { |
| 700 | FREEMEM(sptr); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | int |
| 705 | SEQUENCE_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 706 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 707 | int edx; |
| 708 | |
| 709 | if(!sptr) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 710 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 711 | "%s: value not given (%s:%d)", |
| 712 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 713 | return -1; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Iterate over structure members and check their validity. |
| 718 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 719 | for(edx = 0; edx < td->elements_count; edx++) { |
| 720 | asn1_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 721 | const void *memb_ptr; |
| 722 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 723 | if(elm->flags & ATF_POINTER) { |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 724 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 725 | if(!memb_ptr) continue; |
| 726 | } else { |
| 727 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 728 | } |
| 729 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 730 | if(elm->memb_constraints) { |
| 731 | int ret = elm->memb_constraints(elm->type, memb_ptr, |
| 732 | app_errlog, app_key); |
| 733 | if(ret) return ret; |
| 734 | } else { |
| 735 | int ret = elm->type->check_constraints(elm->type, |
| 736 | memb_ptr, app_errlog, app_key); |
| 737 | if(ret) return ret; |
| 738 | /* |
| 739 | * Cannot inherit it earlier: |
| 740 | * need to make sure we get the updated version. |
| 741 | */ |
| 742 | elm->memb_constraints = elm->type->check_constraints; |
| 743 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | return 0; |
| 747 | } |