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_CHOICE.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | |
| 9 | /* |
| 10 | * Number of bytes left for this structure. |
| 11 | * (ctx->left) indicates the number of bytes _transferred_ for the structure. |
| 12 | * (size) contains the number of bytes in the buffer passed. |
| 13 | */ |
vlm | c519061 | 2004-08-18 04:53:32 +0000 | [diff] [blame] | 14 | #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * If the subprocessor function returns with an indication that it wants |
| 18 | * more data, it may well be a fatal decoding problem, because the |
| 19 | * size is constrained by the <TLV>'s L, even if the buffer size allows |
| 20 | * reading more data. |
| 21 | * For example, consider the buffer containing the following TLVs: |
| 22 | * <T:5><L:1><V> <T:6>... |
| 23 | * The TLV length clearly indicates that one byte is expected in V, but |
| 24 | * if the V processor returns with "want more data" even if the buffer |
| 25 | * contains way more data than the V processor have seen. |
| 26 | */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 27 | #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * This macro "eats" the part of the buffer which is definitely "consumed", |
| 31 | * i.e. was correctly converted into local representation or rightfully skipped. |
| 32 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 33 | #undef ADVANCE |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | #define ADVANCE(num_bytes) do { \ |
| 35 | size_t num = num_bytes; \ |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 36 | ptr = ((char *)ptr) + num; \ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | size -= num; \ |
| 38 | if(ctx->left >= 0) \ |
| 39 | ctx->left -= num; \ |
| 40 | consumed_myself += num; \ |
| 41 | } while(0) |
| 42 | |
| 43 | /* |
| 44 | * Switch to the next phase of parsing. |
| 45 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 46 | #undef NEXT_PHASE |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | #define NEXT_PHASE(ctx) do { \ |
| 48 | ctx->phase++; \ |
| 49 | ctx->step = 0; \ |
| 50 | } while(0) |
| 51 | |
| 52 | /* |
| 53 | * Return a standardized complex structure. |
| 54 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 55 | #undef RETURN |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 56 | #define RETURN(_code) do { \ |
| 57 | rval.code = _code; \ |
| 58 | rval.consumed = consumed_myself;\ |
| 59 | return rval; \ |
| 60 | } while(0) |
| 61 | |
| 62 | /* |
| 63 | * See the definitions. |
| 64 | */ |
vlm | e55716a | 2004-08-11 09:10:59 +0000 | [diff] [blame] | 65 | static int _fetch_present_idx(const void *struct_ptr, int off, int size); |
| 66 | static void _set_present_idx(void *sptr, int offset, int size, int pres); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * Tags are canonically sorted in the tag to member table. |
| 70 | */ |
| 71 | static int |
| 72 | _search4tag(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 CHOICE type. |
| 98 | */ |
| 99 | ber_dec_rval_t |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 100 | CHOICE_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_CHOICE_specifics_t *specs = (asn1_CHOICE_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 | ssize_t tag_len; /* Length of TLV's T */ |
| 116 | //ber_tlv_len_t tlv_len; /* L from TLV */ |
| 117 | ber_dec_rval_t rval; /* Return code from subparsers */ |
| 118 | |
| 119 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
| 120 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 121 | ASN_DEBUG("Decoding %s as CHOICE", 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 | if(tag_mode || td->tags_count) { |
| 150 | rval = ber_check_tags(td, ctx, ptr, size, |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 151 | tag_mode, -1, &ctx->left, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 152 | if(rval.code != RC_OK) { |
| 153 | ASN_DEBUG("%s tagging check failed: %d", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 154 | td->name, rval.code); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 155 | consumed_myself += rval.consumed; |
| 156 | RETURN(rval.code); |
| 157 | } |
| 158 | |
| 159 | if(ctx->left >= 0) { |
| 160 | /* ?Substracted below! */ |
| 161 | ctx->left += rval.consumed; |
| 162 | } |
| 163 | ADVANCE(rval.consumed); |
| 164 | } else { |
| 165 | ctx->left = -1; |
| 166 | } |
| 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 | * Fetch the T from TLV. |
| 177 | */ |
| 178 | tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 179 | ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 180 | switch(tag_len) { |
| 181 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 182 | /* Fall through */ |
| 183 | case -1: RETURN(RC_FAIL); |
| 184 | } |
| 185 | |
| 186 | do { |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 187 | asn1_TYPE_tag2member_t *t2m; |
| 188 | asn1_TYPE_tag2member_t key; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 189 | |
| 190 | key.el_tag = tlv_tag; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 191 | (void *)t2m = bsearch(&key, |
| 192 | specs->tag2el, specs->tag2el_count, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 193 | sizeof(specs->tag2el[0]), _search4tag); |
| 194 | if(t2m) { |
| 195 | /* |
| 196 | * Found the element corresponding to the tag. |
| 197 | */ |
| 198 | NEXT_PHASE(ctx); |
| 199 | ctx->step = t2m->el_no; |
| 200 | break; |
| 201 | } else if(specs->extensible == 0) { |
| 202 | ASN_DEBUG("Unexpected tag %s " |
| 203 | "in non-extensible CHOICE %s", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 204 | ber_tlv_tag_string(tlv_tag), td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 205 | RETURN(RC_FAIL); |
| 206 | } else { |
| 207 | /* Skip this tag */ |
| 208 | ssize_t skip; |
| 209 | |
| 210 | ASN_DEBUG("Skipping unknown tag %s", |
| 211 | ber_tlv_tag_string(tlv_tag)); |
| 212 | |
| 213 | skip = ber_skip_length( |
| 214 | BER_TLV_CONSTRUCTED(ptr), |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 215 | (char *)ptr + tag_len, LEFT - tag_len); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 216 | |
| 217 | switch(skip) { |
| 218 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 219 | /* Fall through */ |
| 220 | case -1: RETURN(RC_FAIL); |
| 221 | } |
| 222 | |
| 223 | ADVANCE(skip + tag_len); |
| 224 | RETURN(RC_OK); |
| 225 | } |
| 226 | } while(0); |
| 227 | |
| 228 | case 2: |
| 229 | /* |
| 230 | * PHASE 2. |
| 231 | * Read in the element. |
| 232 | */ |
| 233 | do { |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 234 | asn1_TYPE_member_t *elm;/* CHOICE's element */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 235 | void *memb_ptr; /* Pointer to the member */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 236 | void **memb_ptr2; /* Pointer to that pointer */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 237 | |
| 238 | elm = &elements[ctx->step]; |
| 239 | |
| 240 | /* |
| 241 | * Compute the position of the member inside a structure, |
| 242 | * and also a type of containment (it may be contained |
| 243 | * as pointer or using inline inclusion). |
| 244 | */ |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 245 | if(elm->flags & ATF_POINTER) { |
| 246 | /* Member is a pointer to another structure */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 247 | memb_ptr2 = (void **)((char *)st + elm->memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 248 | } else { |
| 249 | /* |
| 250 | * A pointer to a pointer |
| 251 | * holding the start of the structure |
| 252 | */ |
| 253 | memb_ptr = (char *)st + elm->memb_offset; |
| 254 | memb_ptr2 = &memb_ptr; |
| 255 | } |
| 256 | /* |
| 257 | * Invoke the member fetch routine according to member's type |
| 258 | */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 259 | rval = elm->type->ber_decoder(elm->type, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 260 | memb_ptr2, ptr, LEFT, |
| 261 | elm->tag_mode); |
| 262 | switch(rval.code) { |
| 263 | case RC_OK: |
| 264 | _set_present_idx(st, specs->pres_offset, |
| 265 | specs->pres_size, ctx->step + 1); |
| 266 | break; |
| 267 | case RC_WMORE: /* More data expected */ |
| 268 | if(!SIZE_VIOLATION) { |
| 269 | ADVANCE(rval.consumed); |
| 270 | RETURN(RC_WMORE); |
| 271 | } |
| 272 | RETURN(RC_FAIL); |
| 273 | case RC_FAIL: /* Fatal error */ |
| 274 | RETURN(rval.code); |
| 275 | } /* switch(rval) */ |
| 276 | |
| 277 | ADVANCE(rval.consumed); |
| 278 | } while(0); |
| 279 | |
| 280 | NEXT_PHASE(ctx); |
| 281 | |
| 282 | /* Fall through */ |
| 283 | case 3: |
| 284 | ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 285 | td->name, (long)ctx->left, (long)size, |
| 286 | tag_mode, td->tags_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 287 | |
| 288 | if(ctx->left > 0) { |
| 289 | /* |
| 290 | * The type must be fully decoded |
| 291 | * by the CHOICE member-specific decoder. |
| 292 | */ |
| 293 | RETURN(RC_FAIL); |
| 294 | } |
| 295 | |
| 296 | if(ctx->left == -1 |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 297 | && !(tag_mode || td->tags_count)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 298 | /* |
| 299 | * This is an untagged CHOICE. |
| 300 | * It doesn't contain nothing |
| 301 | * except for the member itself, including all its tags. |
| 302 | * The decoding is completed. |
| 303 | */ |
| 304 | NEXT_PHASE(ctx); |
| 305 | break; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Read in the "end of data chunks"'s. |
| 310 | */ |
| 311 | while(ctx->left < 0) { |
| 312 | ssize_t tl; |
| 313 | |
| 314 | tl = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 315 | switch(tl) { |
| 316 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 317 | /* Fall through */ |
| 318 | case -1: RETURN(RC_FAIL); |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Expected <0><0>... |
| 323 | */ |
| 324 | if(((uint8_t *)ptr)[0] == 0) { |
| 325 | if(LEFT < 2) { |
| 326 | if(SIZE_VIOLATION) |
| 327 | RETURN(RC_FAIL); |
| 328 | else |
| 329 | RETURN(RC_WMORE); |
| 330 | } else if(((uint8_t *)ptr)[1] == 0) { |
| 331 | /* |
| 332 | * Correctly finished with <0><0>. |
| 333 | */ |
| 334 | continue; |
| 335 | } |
| 336 | } else { |
| 337 | ASN_DEBUG("Unexpected continuation in %s", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 338 | td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 339 | RETURN(RC_FAIL); |
| 340 | } |
| 341 | |
| 342 | ADVANCE(2); |
| 343 | ctx->left++; |
| 344 | } |
| 345 | |
| 346 | NEXT_PHASE(ctx); |
| 347 | case 4: |
| 348 | /* No meaningful work here */ |
| 349 | break; |
| 350 | } |
| 351 | |
| 352 | RETURN(RC_OK); |
| 353 | } |
| 354 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 355 | asn_enc_rval_t |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 356 | CHOICE_encode_der(asn1_TYPE_descriptor_t *td, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 357 | void *struct_ptr, |
| 358 | int tag_mode, ber_tlv_tag_t tag, |
| 359 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 360 | asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics; |
| 361 | asn1_TYPE_member_t *elm; /* CHOICE element */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 362 | asn_enc_rval_t erval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 363 | void *memb_ptr; |
| 364 | size_t computed_size = 0; |
| 365 | int present; |
| 366 | |
| 367 | ASN_DEBUG("%s %s as CHOICE", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 368 | cb?"Encoding":"Estimating", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 369 | |
| 370 | present = _fetch_present_idx(struct_ptr, |
| 371 | specs->pres_offset, specs->pres_size); |
| 372 | |
| 373 | /* |
| 374 | * If the structure was not initialized, it cannot be encoded: |
| 375 | * can't deduce what to encode in the choice type. |
| 376 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 377 | if(present <= 0 || present > td->elements_count) { |
| 378 | if(present == 0 && td->elements_count == 0) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 379 | /* The CHOICE is empty?! */ |
| 380 | erval.encoded = 0; |
| 381 | return erval; |
| 382 | } |
| 383 | erval.encoded = -1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 384 | erval.failed_type = td; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 385 | erval.structure_ptr = struct_ptr; |
| 386 | return erval; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Seek over the present member of the structure. |
| 391 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 392 | elm = &td->elements[present-1]; |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 393 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 394 | memb_ptr = *(void **)((char *)struct_ptr + elm->memb_offset); |
| 395 | if(memb_ptr == 0) { |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 396 | if(elm->optional) { |
| 397 | erval.encoded = 0; |
| 398 | } else { |
| 399 | /* Mandatory element absent */ |
| 400 | erval.encoded = -1; |
| 401 | erval.failed_type = td; |
| 402 | erval.structure_ptr = struct_ptr; |
| 403 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 404 | return erval; |
| 405 | } |
| 406 | } else { |
| 407 | memb_ptr = (void *)((char *)struct_ptr + elm->memb_offset); |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * If the CHOICE itself is tagged EXPLICIT: |
| 412 | * T ::= [2] EXPLICIT CHOICE { ... } |
| 413 | * Then emit the appropriate tags. |
| 414 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 415 | if(tag_mode == 1 || td->tags_count) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 416 | /* |
| 417 | * For this, we need to pre-compute the member. |
| 418 | */ |
| 419 | ssize_t ret; |
| 420 | |
| 421 | /* Encode member with its tag */ |
| 422 | erval = elm->type->der_encoder(elm->type, memb_ptr, |
| 423 | elm->tag_mode, elm->tag, 0, 0); |
| 424 | if(erval.encoded == -1) |
| 425 | return erval; |
| 426 | |
| 427 | /* Encode CHOICE with parent or my own tag */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 428 | ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 429 | cb, app_key); |
| 430 | if(ret == -1) { |
| 431 | erval.encoded = -1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 432 | erval.failed_type = td; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 433 | erval.structure_ptr = struct_ptr; |
| 434 | return erval; |
| 435 | } |
| 436 | computed_size += ret; |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Encode the single underlying member. |
| 441 | */ |
| 442 | erval = elm->type->der_encoder(elm->type, memb_ptr, |
| 443 | elm->tag_mode, elm->tag, cb, app_key); |
| 444 | if(erval.encoded == -1) |
| 445 | return erval; |
| 446 | |
| 447 | ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)", |
| 448 | (long)erval.encoded, (long)computed_size); |
| 449 | |
| 450 | erval.encoded += computed_size; |
| 451 | |
| 452 | return erval; |
| 453 | } |
| 454 | |
| 455 | ber_tlv_tag_t |
| 456 | CHOICE_outmost_tag(asn1_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber_tlv_tag_t tag) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 457 | asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 458 | int present; |
| 459 | |
| 460 | assert(tag_mode == 0); |
| 461 | assert(tag == 0); |
| 462 | |
| 463 | /* |
| 464 | * Figure out which CHOICE element is encoded. |
| 465 | */ |
| 466 | present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size); |
| 467 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 468 | if(present > 0 || present <= td->elements_count) { |
| 469 | asn1_TYPE_member_t *elm = &td->elements[present-1]; |
vlm | 4d951ef | 2004-06-05 01:45:48 +0000 | [diff] [blame] | 470 | const void *memb_ptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 471 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 472 | if(elm->flags & ATF_POINTER) { |
vlm | 4d951ef | 2004-06-05 01:45:48 +0000 | [diff] [blame] | 473 | memb_ptr = *(const void * const *) |
| 474 | ((const char *)ptr + elm->memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 475 | } else { |
vlm | 4d951ef | 2004-06-05 01:45:48 +0000 | [diff] [blame] | 476 | memb_ptr = (const void *) |
| 477 | ((const char *)ptr + elm->memb_offset); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | return asn1_TYPE_outmost_tag(elm->type, memb_ptr, |
| 481 | elm->tag_mode, elm->tag); |
| 482 | } else { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 483 | return (ber_tlv_tag_t)-1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| 487 | int |
| 488 | CHOICE_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 489 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 490 | asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 491 | int present; |
| 492 | |
| 493 | if(!sptr) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 494 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 495 | "%s: value not given (%s:%d)", |
| 496 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 497 | return -1; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * Figure out which CHOICE element is encoded. |
| 502 | */ |
| 503 | present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size); |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 504 | if(present > 0 && present <= td->elements_count) { |
| 505 | asn1_TYPE_member_t *elm = &td->elements[present-1]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 506 | const void *memb_ptr; |
| 507 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 508 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 509 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 510 | if(!memb_ptr) { |
| 511 | if(elm->optional) |
| 512 | return 0; |
| 513 | _ASN_ERRLOG(app_errlog, app_key, |
| 514 | "%s: mandatory CHOICE element %s absent (%s:%d)", |
| 515 | td->name, elm->name, __FILE__, __LINE__); |
| 516 | return -1; |
| 517 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 518 | } else { |
| 519 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 520 | } |
| 521 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 522 | if(elm->memb_constraints) { |
| 523 | return elm->memb_constraints(elm->type, memb_ptr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 524 | app_errlog, app_key); |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 525 | } else { |
| 526 | int ret = elm->type->check_constraints(elm->type, |
| 527 | memb_ptr, app_errlog, app_key); |
| 528 | /* |
| 529 | * Cannot inherit it eralier: |
| 530 | * need to make sure we get the updated version. |
| 531 | */ |
| 532 | elm->memb_constraints = elm->type->check_constraints; |
| 533 | return ret; |
| 534 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 535 | } else { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 536 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 63a2ec9 | 2004-08-22 13:55:49 +0000 | [diff] [blame] | 537 | "%s: no CHOICE element given (%s:%d)", |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 538 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 539 | return -1; |
| 540 | } |
| 541 | } |
| 542 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 543 | asn_enc_rval_t |
| 544 | CHOICE_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr, |
| 545 | int ilevel, enum xer_encoder_flags_e flags, |
| 546 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 547 | asn1_CHOICE_specifics_t *specs=(asn1_CHOICE_specifics_t *)td->specifics; |
| 548 | asn_enc_rval_t er; |
| 549 | int present; |
| 550 | |
| 551 | if(!sptr) |
| 552 | _ASN_ENCODE_FAILED; |
| 553 | |
| 554 | /* |
| 555 | * Figure out which CHOICE element is encoded. |
| 556 | */ |
| 557 | present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size); |
| 558 | |
| 559 | if(present <= 0 || present > td->elements_count) { |
| 560 | _ASN_ENCODE_FAILED; |
| 561 | } else { |
| 562 | asn_enc_rval_t tmper; |
| 563 | asn1_TYPE_member_t *elm = &td->elements[present-1]; |
| 564 | void *memb_ptr; |
| 565 | const char *mname = elm->name; |
| 566 | unsigned int mlen = strlen(mname); |
| 567 | |
| 568 | if(elm->flags & ATF_POINTER) { |
| 569 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
| 570 | if(!memb_ptr) _ASN_ENCODE_FAILED; |
| 571 | } else { |
| 572 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
| 573 | } |
| 574 | |
| 575 | er.encoded = 0; |
| 576 | |
| 577 | if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel); |
| 578 | _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1); |
| 579 | |
| 580 | tmper = elm->type->xer_encoder(elm->type, memb_ptr, |
| 581 | ilevel + 1, flags, cb, app_key); |
| 582 | if(tmper.encoded == -1) return tmper; |
| 583 | |
| 584 | _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1); |
| 585 | |
| 586 | er.encoded += 5 + (2 * mlen) + tmper.encoded; |
| 587 | } |
| 588 | |
| 589 | if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel - 1); |
| 590 | |
| 591 | return er; |
| 592 | } |
| 593 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 594 | int |
| 595 | CHOICE_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 596 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 597 | asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 598 | int present; |
| 599 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 600 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 601 | |
| 602 | /* |
| 603 | * Figure out which CHOICE element is encoded. |
| 604 | */ |
| 605 | present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size); |
| 606 | |
| 607 | /* |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 608 | * Print that element. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 609 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 610 | if(present > 0 && present <= td->elements_count) { |
| 611 | asn1_TYPE_member_t *elm = &td->elements[present-1]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 612 | const void *memb_ptr; |
| 613 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 614 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 615 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 616 | if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 617 | } else { |
| 618 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 619 | } |
| 620 | |
| 621 | /* Print member's name and stuff */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 622 | if(0) { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 623 | if(cb(elm->name, strlen(elm->name), app_key) < 0 |
| 624 | || cb(": ", 2, app_key) < 0) |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 625 | return -1; |
| 626 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 627 | |
| 628 | return elm->type->print_struct(elm->type, memb_ptr, ilevel, |
| 629 | cb, app_key); |
| 630 | } else { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 631 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
| 635 | void |
| 636 | CHOICE_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 637 | asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 638 | int present; |
| 639 | |
| 640 | if(!td || !ptr) |
| 641 | return; |
| 642 | |
| 643 | ASN_DEBUG("Freeing %s as CHOICE", td->name); |
| 644 | |
| 645 | /* |
| 646 | * Figure out which CHOICE element is encoded. |
| 647 | */ |
| 648 | present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size); |
| 649 | |
| 650 | /* |
| 651 | * Free that element. |
| 652 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 653 | if(present > 0 && present <= td->elements_count) { |
| 654 | asn1_TYPE_member_t *elm = &td->elements[present-1]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 655 | void *memb_ptr; |
| 656 | |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 657 | if(elm->flags & ATF_POINTER) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 658 | memb_ptr = *(void **)((char *)ptr + elm->memb_offset); |
| 659 | if(memb_ptr) |
| 660 | elm->type->free_struct(elm->type, memb_ptr, 0); |
| 661 | } else { |
| 662 | memb_ptr = (void *)((char *)ptr + elm->memb_offset); |
| 663 | elm->type->free_struct(elm->type, memb_ptr, 1); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | if(!contents_only) { |
| 668 | FREEMEM(ptr); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | |
| 673 | /* |
| 674 | * The following functions functions offer protection against -fshort-enums, |
| 675 | * compatible with little- and big-endian machines. |
| 676 | * If assertion is triggered, either disable -fshort-enums, or add an entry |
| 677 | * here with the ->pres_size of your target stracture. |
| 678 | * Unless the target structure is packed, the ".present" member |
| 679 | * is guaranteed to be aligned properly. ASN.1 compiler itself does not |
| 680 | * produce packed code. |
| 681 | */ |
vlm | e55716a | 2004-08-11 09:10:59 +0000 | [diff] [blame] | 682 | static int |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 683 | _fetch_present_idx(const void *struct_ptr, int pres_offset, int pres_size) { |
| 684 | const void *present_ptr; |
| 685 | int present; |
| 686 | |
| 687 | present_ptr = ((const char *)struct_ptr) + pres_offset; |
| 688 | |
| 689 | switch(pres_size) { |
| 690 | case sizeof(int): present = *(const int *)present_ptr; break; |
| 691 | case sizeof(short): present = *(const short *)present_ptr; break; |
| 692 | case sizeof(char): present = *(const char *)present_ptr; break; |
| 693 | default: |
| 694 | /* ANSI C mandates enum to be equivalent to integer */ |
| 695 | assert(pres_size != sizeof(int)); |
| 696 | return 0; /* If not aborted, pass back safe value */ |
| 697 | } |
| 698 | |
| 699 | return present; |
| 700 | } |
| 701 | |
vlm | e55716a | 2004-08-11 09:10:59 +0000 | [diff] [blame] | 702 | static void |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 703 | _set_present_idx(void *struct_ptr, int pres_offset, int pres_size, int present) { |
| 704 | void *present_ptr; |
| 705 | present_ptr = ((char *)struct_ptr) + pres_offset; |
| 706 | |
| 707 | switch(pres_size) { |
| 708 | case sizeof(int): *(int *)present_ptr = present; break; |
| 709 | case sizeof(short): *(short *)present_ptr = present; break; |
| 710 | case sizeof(char): *(char *)present_ptr = present; break; |
| 711 | default: |
| 712 | /* ANSI C mandates enum to be equivalent to integer */ |
| 713 | assert(pres_size != sizeof(int)); |
| 714 | } |
| 715 | } |