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