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