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