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 | */ |
| 5 | #include <constr_SEQUENCE.h> |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 6 | #include <assert.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | |
| 8 | /* |
| 9 | * Number of bytes left for this structure. |
| 10 | * (ctx->left) indicates the number of bytes _transferred_ for the structure. |
| 11 | * (size) contains the number of bytes in the buffer passed. |
| 12 | */ |
vlm | c519061 | 2004-08-18 04:53:32 +0000 | [diff] [blame] | 13 | #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | |
| 15 | /* |
| 16 | * If the subprocessor function returns with an indication that it wants |
| 17 | * more data, it may well be a fatal decoding problem, because the |
| 18 | * size is constrained by the <TLV>'s L, even if the buffer size allows |
| 19 | * reading more data. |
| 20 | * For example, consider the buffer containing the following TLVs: |
| 21 | * <T:5><L:1><V> <T:6>... |
| 22 | * The TLV length clearly indicates that one byte is expected in V, but |
| 23 | * if the V processor returns with "want more data" even if the buffer |
| 24 | * contains way more data than the V processor have seen. |
| 25 | */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 26 | #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * This macro "eats" the part of the buffer which is definitely "consumed", |
| 30 | * i.e. was correctly converted into local representation or rightfully skipped. |
| 31 | */ |
| 32 | #define ADVANCE(num_bytes) do { \ |
| 33 | size_t num = num_bytes; \ |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 34 | ptr = ((char *)ptr) + num; \ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 35 | size -= num; \ |
| 36 | if(ctx->left >= 0) \ |
| 37 | ctx->left -= num; \ |
| 38 | consumed_myself += num; \ |
| 39 | } while(0) |
| 40 | |
| 41 | /* |
| 42 | * Switch to the next phase of parsing. |
| 43 | */ |
| 44 | #define NEXT_PHASE(ctx) do { \ |
| 45 | ctx->phase++; \ |
| 46 | ctx->step = 0; \ |
| 47 | } while(0) |
| 48 | #define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0) |
| 49 | |
| 50 | /* |
| 51 | * Return a standardized complex structure. |
| 52 | */ |
| 53 | #define RETURN(_code) do { \ |
| 54 | rval.code = _code; \ |
| 55 | rval.consumed = consumed_myself;\ |
| 56 | return rval; \ |
| 57 | } while(0) |
| 58 | |
| 59 | /* |
| 60 | * Check whether we are inside the extensions group. |
| 61 | */ |
| 62 | #define IN_EXTENSION_GROUP(specs, memb_idx) \ |
| 63 | ( ((memb_idx) > (specs)->ext_after) \ |
| 64 | &&((memb_idx) < (specs)->ext_before)) |
| 65 | |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * Tags are canonically sorted in the tag2element map. |
| 69 | */ |
| 70 | static int |
| 71 | _t2e_cmp(const void *ap, const void *bp) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 72 | const asn1_TYPE_tag2member_t *a = (const asn1_TYPE_tag2member_t *)ap; |
| 73 | const asn1_TYPE_tag2member_t *b = (const asn1_TYPE_tag2member_t *)bp; |
| 74 | |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 75 | int a_class = BER_TAG_CLASS(a->el_tag); |
| 76 | int b_class = BER_TAG_CLASS(b->el_tag); |
| 77 | |
| 78 | if(a_class == b_class) { |
| 79 | ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag); |
| 80 | ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag); |
| 81 | |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 82 | if(a_value == b_value) { |
vlm | e4ee6ee | 2004-06-14 14:11:14 +0000 | [diff] [blame] | 83 | if(a->el_no > b->el_no) |
| 84 | return 1; |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 85 | /* |
| 86 | * Important: we do not check |
vlm | e4ee6ee | 2004-06-14 14:11:14 +0000 | [diff] [blame] | 87 | * for a->el_no <= b->el_no! |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 88 | */ |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 89 | return 0; |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 90 | } else if(a_value < b_value) |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 91 | return -1; |
| 92 | else |
| 93 | return 1; |
| 94 | } else if(a_class < b_class) { |
| 95 | return -1; |
| 96 | } else { |
| 97 | return 1; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | /* |
| 103 | * The decoder of the SEQUENCE type. |
| 104 | */ |
| 105 | ber_dec_rval_t |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 106 | SEQUENCE_decode_ber(asn1_TYPE_descriptor_t *td, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 107 | void **struct_ptr, void *ptr, size_t size, int tag_mode) { |
| 108 | /* |
| 109 | * Bring closer parts of structure description. |
| 110 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 111 | asn1_SEQUENCE_specifics_t *specs = (asn1_SEQUENCE_specifics_t *)td->specifics; |
| 112 | asn1_TYPE_member_t *elements = td->elements; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * Parts of the structure being constructed. |
| 116 | */ |
| 117 | void *st = *struct_ptr; /* Target structure. */ |
| 118 | ber_dec_ctx_t *ctx; /* Decoder context */ |
| 119 | |
| 120 | ber_tlv_tag_t tlv_tag; /* T from TLV */ |
| 121 | //ber_tlv_len_t tlv_len; /* L from TLV */ |
| 122 | ber_dec_rval_t rval; /* Return code from subparsers */ |
| 123 | |
| 124 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
| 125 | int edx; /* SEQUENCE element's index */ |
| 126 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 127 | ASN_DEBUG("Decoding %s as SEQUENCE", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 128 | |
| 129 | /* |
| 130 | * Create the target structure if it is not present already. |
| 131 | */ |
| 132 | if(st == 0) { |
| 133 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 134 | if(st == 0) { |
| 135 | RETURN(RC_FAIL); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Restore parsing context. |
| 141 | */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 142 | ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * Start to parse where left previously |
| 146 | */ |
| 147 | switch(ctx->phase) { |
| 148 | case 0: |
| 149 | /* |
| 150 | * PHASE 0. |
| 151 | * Check that the set of tags associated with given structure |
| 152 | * perfectly fits our expectations. |
| 153 | */ |
| 154 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 155 | rval = ber_check_tags(td, ctx, ptr, size, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 156 | tag_mode, &ctx->left, 0); |
| 157 | if(rval.code != RC_OK) { |
| 158 | ASN_DEBUG("%s tagging check failed: %d", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 159 | td->name, rval.code); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 160 | consumed_myself += rval.consumed; |
| 161 | RETURN(rval.code); |
| 162 | } |
| 163 | |
| 164 | if(ctx->left >= 0) |
| 165 | ctx->left += rval.consumed; /* ?Substracted below! */ |
| 166 | ADVANCE(rval.consumed); |
| 167 | |
| 168 | NEXT_PHASE(ctx); |
| 169 | |
| 170 | ASN_DEBUG("Structure consumes %ld bytes, buffer %ld", |
| 171 | (long)ctx->left, (long)size); |
| 172 | |
| 173 | /* Fall through */ |
| 174 | case 1: |
| 175 | /* |
| 176 | * PHASE 1. |
| 177 | * From the place where we've left it previously, |
| 178 | * try to decode the next member from the list of |
| 179 | * this structure's elements. |
| 180 | * (ctx->step) stores the member being processed |
| 181 | * between invocations and the microphase {0,1} of parsing |
| 182 | * that member: |
| 183 | * step = (<member_number> * 2 + <microphase>). |
| 184 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 185 | for(edx = (ctx->step >> 1); edx < td->elements_count; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 186 | edx++, ctx->step = (ctx->step & ~1) + 2) { |
| 187 | void *memb_ptr; /* Pointer to the member */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 188 | void **memb_ptr2; /* Pointer to that pointer */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 189 | ssize_t tag_len; /* Length of TLV's T */ |
| 190 | int opt_edx_end; /* Next non-optional element */ |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 191 | int use_bsearch; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 192 | int n; |
| 193 | |
| 194 | if(ctx->step & 1) |
| 195 | goto microphase2; |
| 196 | |
| 197 | /* |
| 198 | * MICROPHASE 1: Synchronize decoding. |
| 199 | */ |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 200 | ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d" |
| 201 | " opt=%d ec=%d", |
| 202 | td->name, (int)ctx->left, edx, |
| 203 | elements[edx].flags, elements[edx].optional, |
| 204 | td->elements_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 205 | |
| 206 | if(ctx->left == 0 /* No more stuff is expected */ |
| 207 | && ( |
| 208 | /* Explicit OPTIONAL specification reaches the end */ |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 209 | (edx + elements[edx].optional |
| 210 | == td->elements_count) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 211 | || |
| 212 | /* All extensions are optional */ |
| 213 | (IN_EXTENSION_GROUP(specs, edx) |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 214 | && specs->ext_before > td->elements_count) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 215 | ) |
| 216 | ) { |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 217 | ASN_DEBUG("End of SEQUENCE %s", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 218 | /* |
| 219 | * Found the legitimate end of the structure. |
| 220 | */ |
| 221 | PHASE_OUT(ctx); |
| 222 | RETURN(RC_OK); |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Fetch the T from TLV. |
| 227 | */ |
| 228 | tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 229 | ASN_DEBUG("In %s SEQUENCE for %d %s next tag length %d", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 230 | td->name, edx, elements[edx].name, (int)tag_len); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 231 | switch(tag_len) { |
| 232 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 233 | /* Fall through */ |
| 234 | case -1: RETURN(RC_FAIL); |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Find the next available type with this tag. |
| 239 | */ |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 240 | use_bsearch = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 241 | opt_edx_end = edx + elements[edx].optional + 1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 242 | if(opt_edx_end > td->elements_count) |
| 243 | opt_edx_end = td->elements_count; /* Cap */ |
vlm | 5d219bf | 2004-07-01 00:48:04 +0000 | [diff] [blame] | 244 | else if(opt_edx_end - edx > 8) { |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 245 | /* Limit the scope of linear search... */ |
vlm | 5d219bf | 2004-07-01 00:48:04 +0000 | [diff] [blame] | 246 | opt_edx_end = edx + 8; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 247 | use_bsearch = 1; |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 248 | /* ... and resort to bsearch() */ |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 249 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 250 | for(n = edx; n < opt_edx_end; n++) { |
| 251 | if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) { |
| 252 | /* |
| 253 | * Found element corresponding to the tag |
| 254 | * being looked at. |
| 255 | * Reposition over the right element. |
| 256 | */ |
| 257 | edx = n; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 258 | ctx->step = 1 + 2 * edx; /* Remember! */ |
| 259 | goto microphase2; |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 260 | } else if(elements[n].flags & ATF_OPEN_TYPE) { |
| 261 | /* |
| 262 | * This is the ANY type, which may bear |
| 263 | * any flag whatsoever. |
| 264 | */ |
| 265 | edx = n; |
| 266 | ctx->step = 1 + 2 * edx; /* Remember! */ |
| 267 | goto microphase2; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 268 | } else if(elements[n].tag == (ber_tlv_tag_t)-1) { |
| 269 | use_bsearch = 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | } |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 273 | if(use_bsearch) { |
| 274 | /* |
vlm | 141874a | 2004-09-04 06:17:35 +0000 | [diff] [blame] | 275 | * Resort to a binary search over |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 276 | * sorted array of tags. |
| 277 | */ |
| 278 | asn1_TYPE_tag2member_t *t2m; |
| 279 | asn1_TYPE_tag2member_t key; |
| 280 | key.el_tag = tlv_tag; |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 281 | key.el_no = edx; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 282 | (void *)t2m = bsearch(&key, |
| 283 | specs->tag2el, specs->tag2el_count, |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 284 | sizeof(specs->tag2el[0]), _t2e_cmp); |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 285 | if(t2m) { |
| 286 | asn1_TYPE_tag2member_t *best = 0; |
| 287 | asn1_TYPE_tag2member_t *t2m_f, *t2m_l; |
| 288 | int edx_max = edx + elements[edx].optional; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 289 | /* |
| 290 | * Rewind to the first element with that tag, |
| 291 | * `cause bsearch() does not guarantee order. |
| 292 | */ |
vlm | 1692da0 | 2004-06-14 13:42:23 +0000 | [diff] [blame] | 293 | t2m_f = t2m + t2m->toff_first; |
| 294 | t2m_l = t2m + t2m->toff_last; |
| 295 | for(t2m = t2m_f; t2m <= t2m_l; t2m++) { |
| 296 | if(t2m->el_no > edx_max) break; |
| 297 | if(t2m->el_no < edx) continue; |
| 298 | best = t2m; |
| 299 | } |
| 300 | if(best) { |
| 301 | edx = best->el_no; |
| 302 | ctx->step = 1 + 2 * edx; |
| 303 | goto microphase2; |
| 304 | } |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 305 | } |
| 306 | n = opt_edx_end; |
| 307 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 308 | if(n == opt_edx_end) { |
| 309 | /* |
| 310 | * If tag is unknown, it may be either |
| 311 | * an unknown (thus, incorrect) tag, |
| 312 | * or an extension (...), |
| 313 | * or an end of the indefinite-length structure. |
| 314 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 315 | if(!IN_EXTENSION_GROUP(specs, edx)) { |
| 316 | ASN_DEBUG("Unexpected tag %s", |
| 317 | ber_tlv_tag_string(tlv_tag)); |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 318 | ASN_DEBUG("Expected tag %s (%s)%s", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 319 | ber_tlv_tag_string(elements[edx].tag), |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 320 | elements[edx].name, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 321 | elements[edx].optional |
| 322 | ?" or alternatives":""); |
| 323 | RETURN(RC_FAIL); |
| 324 | } |
| 325 | |
| 326 | if(ctx->left < 0 |
| 327 | && ((uint8_t *)ptr)[0] == 0) { |
| 328 | if(LEFT < 2) { |
| 329 | if(SIZE_VIOLATION) |
| 330 | RETURN(RC_FAIL); |
| 331 | else |
| 332 | RETURN(RC_WMORE); |
| 333 | } else if(((uint8_t *)ptr)[1] == 0) { |
| 334 | /* |
| 335 | * Yeah, baby! Found the terminator |
| 336 | * of the indefinite length structure. |
| 337 | */ |
| 338 | /* |
| 339 | * Proceed to the canonical |
| 340 | * finalization function. |
| 341 | * No advancing is necessary. |
| 342 | */ |
| 343 | goto phase3; |
| 344 | } |
| 345 | } else { |
| 346 | /* Skip this tag */ |
| 347 | ssize_t skip; |
| 348 | |
| 349 | skip = ber_skip_length( |
| 350 | BER_TLV_CONSTRUCTED(ptr), |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 351 | (char *)ptr + tag_len, LEFT - tag_len); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 352 | ASN_DEBUG("Skip length %d in %s", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 353 | (int)skip, td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 354 | switch(skip) { |
| 355 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 356 | /* Fall through */ |
| 357 | case -1: RETURN(RC_FAIL); |
| 358 | } |
| 359 | |
| 360 | ADVANCE(skip + tag_len); |
| 361 | ctx->step -= 2; |
| 362 | edx--; |
| 363 | continue; /* Try again with the next tag */ |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * MICROPHASE 2: Invoke the member-specific decoder. |
| 369 | */ |
| 370 | ctx->step |= 1; /* Confirm entering next microphase */ |
| 371 | microphase2: |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 372 | ASN_DEBUG("Inside SEQUENCE %s MF2", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | * Compute the position of the member inside a structure, |
| 376 | * and also a type of containment (it may be contained |
| 377 | * as pointer or using inline inclusion). |
| 378 | */ |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 379 | if(elements[edx].flags & ATF_POINTER) { |
| 380 | /* Member is a pointer to another structure */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 381 | memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 382 | } else { |
| 383 | /* |
| 384 | * A pointer to a pointer |
| 385 | * holding the start of the structure |
| 386 | */ |
| 387 | memb_ptr = (char *)st + elements[edx].memb_offset; |
| 388 | memb_ptr2 = &memb_ptr; |
| 389 | } |
| 390 | /* |
| 391 | * Invoke the member fetch routine according to member's type |
| 392 | */ |
| 393 | rval = elements[edx].type->ber_decoder( |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 394 | elements[edx].type, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 395 | memb_ptr2, ptr, LEFT, |
| 396 | elements[edx].tag_mode); |
vlm | ef16e8c | 2004-09-04 04:44:11 +0000 | [diff] [blame] | 397 | ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d " |
| 398 | "in %d bytes rval.code %d", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 399 | td->name, edx, elements[edx].type->name, |
vlm | ef16e8c | 2004-09-04 04:44:11 +0000 | [diff] [blame] | 400 | (int)LEFT, (int)rval.consumed, rval.code); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 401 | switch(rval.code) { |
| 402 | case RC_OK: |
| 403 | break; |
| 404 | case RC_WMORE: /* More data expected */ |
| 405 | if(!SIZE_VIOLATION) { |
| 406 | ADVANCE(rval.consumed); |
| 407 | RETURN(RC_WMORE); |
| 408 | } |
| 409 | /* Fall through */ |
| 410 | case RC_FAIL: /* Fatal error */ |
| 411 | RETURN(RC_FAIL); |
| 412 | } /* switch(rval) */ |
| 413 | |
| 414 | ADVANCE(rval.consumed); |
| 415 | } /* for(all structure members) */ |
| 416 | |
| 417 | phase3: |
| 418 | ctx->phase = 3; |
| 419 | case 3: /* 00 and other tags expected */ |
| 420 | case 4: /* only 00's expected */ |
| 421 | |
| 422 | ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 423 | td->name, (long)ctx->left, (long)size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * Skip everything until the end of the SEQUENCE. |
| 427 | */ |
| 428 | while(ctx->left) { |
| 429 | ssize_t tl, ll; |
| 430 | |
| 431 | tl = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 432 | switch(tl) { |
| 433 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 434 | /* Fall through */ |
| 435 | case -1: RETURN(RC_FAIL); |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * If expected <0><0>... |
| 440 | */ |
| 441 | if(ctx->left < 0 |
| 442 | && ((uint8_t *)ptr)[0] == 0) { |
| 443 | if(LEFT < 2) { |
| 444 | if(SIZE_VIOLATION) |
| 445 | RETURN(RC_FAIL); |
| 446 | else |
| 447 | RETURN(RC_WMORE); |
| 448 | } else if(((uint8_t *)ptr)[1] == 0) { |
| 449 | /* |
| 450 | * Correctly finished with <0><0>. |
| 451 | */ |
| 452 | ADVANCE(2); |
| 453 | ctx->left++; |
| 454 | ctx->phase = 4; |
| 455 | continue; |
| 456 | } |
| 457 | } |
| 458 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 459 | if(!IN_EXTENSION_GROUP(specs, td->elements_count) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 460 | || ctx->phase == 4) { |
| 461 | ASN_DEBUG("Unexpected continuation " |
| 462 | "of a non-extensible type " |
| 463 | "%s (SEQUENCE): %s", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 464 | td->name, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 465 | ber_tlv_tag_string(tlv_tag)); |
| 466 | RETURN(RC_FAIL); |
| 467 | } |
| 468 | |
| 469 | ll = ber_skip_length( |
| 470 | BER_TLV_CONSTRUCTED(ptr), |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 471 | (char *)ptr + tl, LEFT - tl); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 472 | switch(ll) { |
| 473 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 474 | /* Fall through */ |
| 475 | case -1: RETURN(RC_FAIL); |
| 476 | } |
| 477 | |
| 478 | ADVANCE(tl + ll); |
| 479 | } |
| 480 | |
| 481 | PHASE_OUT(ctx); |
| 482 | } |
| 483 | |
| 484 | RETURN(RC_OK); |
| 485 | } |
| 486 | |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 487 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 488 | /* |
| 489 | * The DER encoder of the SEQUENCE type. |
| 490 | */ |
| 491 | der_enc_rval_t |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 492 | SEQUENCE_encode_der(asn1_TYPE_descriptor_t *td, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 493 | void *ptr, int tag_mode, ber_tlv_tag_t tag, |
| 494 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 495 | size_t computed_size = 0; |
| 496 | der_enc_rval_t erval; |
| 497 | ssize_t ret; |
| 498 | int edx; |
| 499 | |
| 500 | ASN_DEBUG("%s %s as SEQUENCE", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 501 | cb?"Encoding":"Estimating", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 502 | |
| 503 | /* |
| 504 | * Gather the length of the underlying members sequence. |
| 505 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 506 | for(edx = 0; edx < td->elements_count; edx++) { |
| 507 | asn1_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 508 | void *memb_ptr; |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 509 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 510 | memb_ptr = *(void **)((char *)ptr + elm->memb_offset); |
| 511 | if(!memb_ptr) continue; |
| 512 | } else { |
| 513 | memb_ptr = (void *)((char *)ptr + elm->memb_offset); |
| 514 | } |
| 515 | erval = elm->type->der_encoder(elm->type, memb_ptr, |
| 516 | elm->tag_mode, elm->tag, |
| 517 | 0, 0); |
| 518 | if(erval.encoded == -1) |
| 519 | return erval; |
| 520 | computed_size += erval.encoded; |
| 521 | ASN_DEBUG("Member %d %s estimated %ld bytes", |
| 522 | edx, elm->name, (long)erval.encoded); |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * Encode the TLV for the sequence itself. |
| 527 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 528 | ret = der_write_tags(td, computed_size, tag_mode, tag, cb, app_key); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 529 | ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size); |
| 530 | if(ret == -1) { |
| 531 | erval.encoded = -1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 532 | erval.failed_type = td; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 533 | erval.structure_ptr = ptr; |
| 534 | return erval; |
| 535 | } |
| 536 | erval.encoded = computed_size + ret; |
| 537 | |
| 538 | if(!cb) return erval; |
| 539 | |
| 540 | /* |
| 541 | * Encode all members. |
| 542 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 543 | for(edx = 0; edx < td->elements_count; edx++) { |
| 544 | asn1_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 545 | der_enc_rval_t tmperval; |
| 546 | void *memb_ptr; |
| 547 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 548 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 549 | memb_ptr = *(void **)((char *)ptr + elm->memb_offset); |
| 550 | if(!memb_ptr) continue; |
| 551 | } else { |
| 552 | memb_ptr = (void *)((char *)ptr + elm->memb_offset); |
| 553 | } |
| 554 | tmperval = elm->type->der_encoder(elm->type, memb_ptr, |
| 555 | elm->tag_mode, elm->tag, |
| 556 | cb, app_key); |
| 557 | if(tmperval.encoded == -1) |
| 558 | return tmperval; |
| 559 | computed_size -= tmperval.encoded; |
| 560 | ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %d bytes", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 561 | edx, elm->name, td->name, tmperval.encoded); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | if(computed_size != 0) { |
| 565 | /* |
| 566 | * Encoded size is not equal to the computed size. |
| 567 | */ |
| 568 | erval.encoded = -1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 569 | erval.failed_type = td; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 570 | erval.structure_ptr = ptr; |
| 571 | } |
| 572 | |
| 573 | return erval; |
| 574 | } |
| 575 | |
| 576 | int |
| 577 | SEQUENCE_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 578 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 579 | int edx; |
| 580 | int ret; |
| 581 | |
| 582 | if(!sptr) return cb("<absent>", 8, app_key); |
| 583 | |
| 584 | /* Dump preamble */ |
| 585 | if(cb(td->name, strlen(td->name), app_key) |
| 586 | || cb(" ::= {\n", 7, app_key)) |
| 587 | return -1; |
| 588 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 589 | for(edx = 0; edx < td->elements_count; edx++) { |
| 590 | asn1_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 591 | const void *memb_ptr; |
| 592 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 593 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 594 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
| 595 | if(!memb_ptr) continue; |
| 596 | } else { |
| 597 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 598 | } |
| 599 | |
| 600 | /* Indentation */ |
| 601 | for(ret = 0; ret < ilevel; ret++) cb(" ", 1, app_key); |
| 602 | |
| 603 | /* Print the member's name and stuff */ |
| 604 | if(cb(elm->name, strlen(elm->name), app_key) |
| 605 | || cb(": ", 2, app_key)) |
| 606 | return -1; |
| 607 | |
| 608 | /* Print the member itself */ |
| 609 | ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 4, |
| 610 | cb, app_key); |
| 611 | if(ret) return ret; |
| 612 | |
| 613 | /* Print out the terminator */ |
| 614 | ret = cb("\n", 1, app_key); |
| 615 | if(ret) return ret; |
| 616 | } |
| 617 | |
| 618 | /* Indentation */ |
| 619 | for(ret = 0; ret < ilevel - 4; ret++) cb(" ", 1, app_key); |
| 620 | |
| 621 | return cb("}", 1, app_key); |
| 622 | } |
| 623 | |
| 624 | void |
| 625 | SEQUENCE_free(asn1_TYPE_descriptor_t *td, void *sptr, int contents_only) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 626 | int edx; |
| 627 | |
| 628 | if(!td || !sptr) |
| 629 | return; |
| 630 | |
| 631 | ASN_DEBUG("Freeing %s as SEQUENCE", td->name); |
| 632 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 633 | for(edx = 0; edx < td->elements_count; edx++) { |
| 634 | asn1_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 635 | void *memb_ptr; |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 636 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 637 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
| 638 | if(memb_ptr) |
| 639 | elm->type->free_struct(elm->type, memb_ptr, 0); |
| 640 | } else { |
| 641 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
| 642 | elm->type->free_struct(elm->type, memb_ptr, 1); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | if(!contents_only) { |
| 647 | FREEMEM(sptr); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | int |
| 652 | SEQUENCE_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 653 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 654 | int edx; |
| 655 | |
| 656 | if(!sptr) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 657 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 658 | "%s: value not given (%s:%d)", |
| 659 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 660 | return -1; |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Iterate over structure members and check their validity. |
| 665 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 666 | for(edx = 0; edx < td->elements_count; edx++) { |
| 667 | asn1_TYPE_member_t *elm = &td->elements[edx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 668 | const void *memb_ptr; |
| 669 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 670 | if(elm->flags & ATF_POINTER) { |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 671 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 672 | if(!memb_ptr) continue; |
| 673 | } else { |
| 674 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 675 | } |
| 676 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 677 | if(elm->memb_constraints) { |
| 678 | int ret = elm->memb_constraints(elm->type, memb_ptr, |
| 679 | app_errlog, app_key); |
| 680 | if(ret) return ret; |
| 681 | } else { |
| 682 | int ret = elm->type->check_constraints(elm->type, |
| 683 | memb_ptr, app_errlog, app_key); |
| 684 | if(ret) return ret; |
| 685 | /* |
| 686 | * Cannot inherit it earlier: |
| 687 | * need to make sure we get the updated version. |
| 688 | */ |
| 689 | elm->memb_constraints = elm->type->check_constraints; |
| 690 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | return 0; |
| 694 | } |