Lev Walkin | f15320b | 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 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <constr_SET.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | #include <assert.h> /* for assert() */ |
| 8 | |
Lev Walkin | 2c96273 | 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 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 15 | /* Check that all the mandatory members are present */ |
| 16 | static int _SET_is_populated(asn_TYPE_descriptor_t *td, void *st); |
| 17 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 18 | /* |
| 19 | * Number of bytes left for this structure. |
| 20 | * (ctx->left) indicates the number of bytes _transferred_ for the structure. |
| 21 | * (size) contains the number of bytes in the buffer passed. |
| 22 | */ |
Lev Walkin | ec1ffd4 | 2004-08-18 04:53:32 +0000 | [diff] [blame] | 23 | #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * If the subprocessor function returns with an indication that it wants |
| 27 | * more data, it may well be a fatal decoding problem, because the |
| 28 | * size is constrained by the <TLV>'s L, even if the buffer size allows |
| 29 | * reading more data. |
| 30 | * For example, consider the buffer containing the following TLVs: |
| 31 | * <T:5><L:1><V> <T:6>... |
| 32 | * The TLV length clearly indicates that one byte is expected in V, but |
| 33 | * if the V processor returns with "want more data" even if the buffer |
| 34 | * contains way more data than the V processor have seen. |
| 35 | */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 36 | #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * This macro "eats" the part of the buffer which is definitely "consumed", |
| 40 | * i.e. was correctly converted into local representation or rightfully skipped. |
| 41 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 42 | #undef ADVANCE |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 43 | #define ADVANCE(num_bytes) do { \ |
| 44 | size_t num = num_bytes; \ |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 45 | ptr = ((char *)ptr) + num; \ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 46 | size -= num; \ |
| 47 | if(ctx->left >= 0) \ |
| 48 | ctx->left -= num; \ |
| 49 | consumed_myself += num; \ |
| 50 | } while(0) |
| 51 | |
| 52 | /* |
| 53 | * Switch to the next phase of parsing. |
| 54 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 55 | #undef NEXT_PHASE |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 56 | #define NEXT_PHASE(ctx) do { \ |
| 57 | ctx->phase++; \ |
| 58 | ctx->step = 0; \ |
| 59 | } while(0) |
| 60 | |
| 61 | /* |
| 62 | * Return a standardized complex structure. |
| 63 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 64 | #undef RETURN |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 65 | #define RETURN(_code) do { \ |
| 66 | rval.code = _code; \ |
| 67 | rval.consumed = consumed_myself;\ |
| 68 | return rval; \ |
| 69 | } while(0) |
| 70 | |
| 71 | /* |
| 72 | * Tags are canonically sorted in the tag2element map. |
| 73 | */ |
| 74 | static int |
| 75 | _t2e_cmp(const void *ap, const void *bp) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 76 | const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap; |
| 77 | const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 78 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 79 | int a_class = BER_TAG_CLASS(a->el_tag); |
| 80 | int b_class = BER_TAG_CLASS(b->el_tag); |
| 81 | |
| 82 | if(a_class == b_class) { |
| 83 | ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag); |
| 84 | ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag); |
| 85 | |
| 86 | if(a_value == b_value) |
| 87 | return 0; |
| 88 | else if(a_value < b_value) |
| 89 | return -1; |
| 90 | else |
| 91 | return 1; |
| 92 | } else if(a_class < b_class) { |
| 93 | return -1; |
| 94 | } else { |
| 95 | return 1; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * The decoder of the SET type. |
| 101 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 102 | asn_dec_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 103 | SET_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 104 | void **struct_ptr, void *ptr, size_t size, int tag_mode) { |
| 105 | /* |
| 106 | * Bring closer parts of structure description. |
| 107 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 108 | asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics; |
| 109 | asn_TYPE_member_t *elements = td->elements; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * Parts of the structure being constructed. |
| 113 | */ |
| 114 | void *st = *struct_ptr; /* Target structure. */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 115 | asn_struct_ctx_t *ctx; /* Decoder context */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 116 | |
| 117 | ber_tlv_tag_t tlv_tag; /* T from TLV */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 118 | asn_dec_rval_t rval; /* Return code from subparsers */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 119 | |
| 120 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
| 121 | int edx; /* SET element's index */ |
| 122 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 123 | ASN_DEBUG("Decoding %s as SET", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * Create the target structure if it is not present already. |
| 127 | */ |
| 128 | if(st == 0) { |
| 129 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 130 | if(st == 0) { |
| 131 | RETURN(RC_FAIL); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Restore parsing context. |
| 137 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 138 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Start to parse where left previously |
| 142 | */ |
| 143 | switch(ctx->phase) { |
| 144 | case 0: |
| 145 | /* |
| 146 | * PHASE 0. |
| 147 | * Check that the set of tags associated with given structure |
| 148 | * perfectly fits our expectations. |
| 149 | */ |
| 150 | |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 151 | rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 152 | tag_mode, 1, &ctx->left, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 153 | if(rval.code != RC_OK) { |
| 154 | ASN_DEBUG("%s tagging check failed: %d", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 155 | td->name, rval.code); |
Lev Walkin | 4ab42cd | 2004-10-05 06:36:44 +0000 | [diff] [blame] | 156 | return rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | if(ctx->left >= 0) |
| 160 | ctx->left += rval.consumed; /* ?Substracted below! */ |
| 161 | ADVANCE(rval.consumed); |
| 162 | |
| 163 | NEXT_PHASE(ctx); |
| 164 | |
| 165 | ASN_DEBUG("Structure advertised %ld bytes, " |
| 166 | "buffer contains %ld", (long)ctx->left, (long)size); |
| 167 | |
| 168 | /* Fall through */ |
| 169 | case 1: |
| 170 | /* |
| 171 | * PHASE 1. |
| 172 | * From the place where we've left it previously, |
| 173 | * try to decode the next member from the list of |
| 174 | * this structure's elements. |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 175 | * Note that elements in BER may arrive out of |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 176 | * order, yet DER mandates that they shall arive in the |
| 177 | * canonical order of their tags. So, there is a room |
| 178 | * for optimization. |
| 179 | */ |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 180 | for(;; ctx->step = 0) { |
| 181 | asn_TYPE_tag2member_t *t2m; |
| 182 | asn_TYPE_tag2member_t key; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 183 | void *memb_ptr; /* Pointer to the member */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 184 | void **memb_ptr2; /* Pointer to that pointer */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 185 | ssize_t tag_len; /* Length of TLV's T */ |
| 186 | |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 187 | if(ctx->step & 1) { |
| 188 | edx = ctx->step >> 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 189 | goto microphase2; |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 190 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * MICROPHASE 1: Synchronize decoding. |
| 194 | */ |
| 195 | |
| 196 | if(ctx->left == 0) |
| 197 | /* |
| 198 | * No more things to decode. |
| 199 | * Exit out of here and check whether all mandatory |
| 200 | * elements have been received (in the next phase). |
| 201 | */ |
| 202 | break; |
| 203 | |
| 204 | /* |
| 205 | * Fetch the T from TLV. |
| 206 | */ |
| 207 | tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 208 | switch(tag_len) { |
| 209 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 210 | /* Fall through */ |
| 211 | case -1: RETURN(RC_FAIL); |
| 212 | } |
| 213 | |
| 214 | if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) { |
| 215 | if(LEFT < 2) { |
| 216 | if(SIZE_VIOLATION) |
| 217 | RETURN(RC_FAIL); |
| 218 | else |
| 219 | RETURN(RC_WMORE); |
| 220 | } else if(((uint8_t *)ptr)[1] == 0) { |
| 221 | /* |
| 222 | * Found the terminator of the |
| 223 | * indefinite length structure. |
| 224 | * Invoke the generic finalization function. |
| 225 | */ |
| 226 | goto phase3; |
| 227 | } |
| 228 | } |
| 229 | |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 230 | key.el_tag = tlv_tag; |
Lev Walkin | c17d90f | 2005-01-17 14:32:45 +0000 | [diff] [blame] | 231 | t2m = (asn_TYPE_tag2member_t *)bsearch(&key, |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 232 | specs->tag2el, specs->tag2el_count, |
| 233 | sizeof(specs->tag2el[0]), _t2e_cmp); |
| 234 | if(t2m) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 235 | /* |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 236 | * Found the element corresponding to the tag. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 237 | */ |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 238 | edx = t2m->el_no; |
| 239 | ctx->step = (edx << 1) + 1; |
| 240 | ASN_DEBUG("Got tag %s (%s), edx %d", |
| 241 | ber_tlv_tag_string(tlv_tag), td->name, edx); |
| 242 | } else if(specs->extensible == 0) { |
| 243 | ASN_DEBUG("Unexpected tag %s " |
| 244 | "in non-extensible SET %s", |
| 245 | ber_tlv_tag_string(tlv_tag), td->name); |
| 246 | RETURN(RC_FAIL); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 247 | } else { |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 248 | /* Skip this tag */ |
| 249 | ssize_t skip; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 250 | |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 251 | ASN_DEBUG("Skipping unknown tag %s", |
| 252 | ber_tlv_tag_string(tlv_tag)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 253 | |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 254 | skip = ber_skip_length(opt_codec_ctx, |
| 255 | BER_TLV_CONSTRUCTED(ptr), |
| 256 | (char *)ptr + tag_len, LEFT - tag_len); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 257 | |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 258 | switch(skip) { |
| 259 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 260 | /* Fall through */ |
| 261 | case -1: RETURN(RC_FAIL); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 262 | } |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 263 | |
| 264 | ADVANCE(skip + tag_len); |
| 265 | continue; /* Try again with the next tag */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * MICROPHASE 2: Invoke the member-specific decoder. |
| 270 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 271 | microphase2: |
| 272 | |
| 273 | /* |
| 274 | * Check for duplications: must not overwrite |
| 275 | * already decoded elements. |
| 276 | */ |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 277 | if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, edx)) { |
Lev Walkin | bbe0475 | 2004-07-01 00:47:16 +0000 | [diff] [blame] | 278 | ASN_DEBUG("SET %s: Duplicate element %s (%d)", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 279 | td->name, elements[edx].name, edx); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 280 | RETURN(RC_FAIL); |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Compute the position of the member inside a structure, |
| 285 | * and also a type of containment (it may be contained |
| 286 | * as pointer or using inline inclusion). |
| 287 | */ |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 288 | if(elements[edx].flags & ATF_POINTER) { |
| 289 | /* Member is a pointer to another structure */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 290 | memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 291 | } else { |
| 292 | /* |
| 293 | * A pointer to a pointer |
| 294 | * holding the start of the structure |
| 295 | */ |
| 296 | memb_ptr = (char *)st + elements[edx].memb_offset; |
| 297 | memb_ptr2 = &memb_ptr; |
| 298 | } |
| 299 | /* |
| 300 | * Invoke the member fetch routine according to member's type |
| 301 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 302 | rval = elements[edx].type->ber_decoder(opt_codec_ctx, |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 303 | elements[edx].type, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 304 | memb_ptr2, ptr, LEFT, |
| 305 | elements[edx].tag_mode); |
| 306 | switch(rval.code) { |
| 307 | case RC_OK: |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 308 | ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 309 | break; |
| 310 | case RC_WMORE: /* More data expected */ |
| 311 | if(!SIZE_VIOLATION) { |
| 312 | ADVANCE(rval.consumed); |
| 313 | RETURN(RC_WMORE); |
| 314 | } |
| 315 | /* Fail through */ |
| 316 | case RC_FAIL: /* Fatal error */ |
| 317 | RETURN(RC_FAIL); |
| 318 | } /* switch(rval) */ |
| 319 | |
| 320 | ADVANCE(rval.consumed); |
| 321 | } /* for(all structure members) */ |
| 322 | |
| 323 | phase3: |
| 324 | ctx->phase = 3; |
| 325 | /* Fall through */ |
| 326 | case 3: |
| 327 | case 4: /* Only 00 is expected */ |
| 328 | ASN_DEBUG("SET %s Leftover: %ld, size = %ld", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 329 | td->name, (long)ctx->left, (long)size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 330 | |
| 331 | /* |
| 332 | * Skip everything until the end of the SET. |
| 333 | */ |
| 334 | while(ctx->left) { |
| 335 | ssize_t tl, ll; |
| 336 | |
| 337 | tl = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 338 | switch(tl) { |
| 339 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 340 | /* Fall through */ |
| 341 | case -1: RETURN(RC_FAIL); |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * If expected <0><0>... |
| 346 | */ |
| 347 | if(ctx->left < 0 |
| 348 | && ((uint8_t *)ptr)[0] == 0) { |
| 349 | if(LEFT < 2) { |
| 350 | if(SIZE_VIOLATION) |
| 351 | RETURN(RC_FAIL); |
| 352 | else |
| 353 | RETURN(RC_WMORE); |
| 354 | } else if(((uint8_t *)ptr)[1] == 0) { |
| 355 | /* |
| 356 | * Correctly finished with <0><0>. |
| 357 | */ |
| 358 | ADVANCE(2); |
| 359 | ctx->left++; |
| 360 | ctx->phase = 4; |
| 361 | continue; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | if(specs->extensible == 0 || ctx->phase == 4) { |
| 366 | ASN_DEBUG("Unexpected continuation " |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 367 | "of a non-extensible type %s " |
| 368 | "(ptr=%02x)", |
| 369 | td->name, *(uint8_t *)ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 370 | RETURN(RC_FAIL); |
| 371 | } |
| 372 | |
Lev Walkin | baaa24f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 373 | ll = ber_skip_length(opt_codec_ctx, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 374 | BER_TLV_CONSTRUCTED(ptr), |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 375 | (char *)ptr + tl, LEFT - tl); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 376 | switch(ll) { |
| 377 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 378 | /* Fall through */ |
| 379 | case -1: RETURN(RC_FAIL); |
| 380 | } |
| 381 | |
| 382 | ADVANCE(tl + ll); |
| 383 | } |
| 384 | |
| 385 | ctx->phase = 5; |
| 386 | case 5: |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 387 | /* Check that all mandatory elements are present. */ |
| 388 | if(!_SET_is_populated(td, st)) |
| 389 | RETURN(RC_FAIL); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 390 | |
| 391 | NEXT_PHASE(ctx); |
| 392 | } |
| 393 | |
| 394 | RETURN(RC_OK); |
| 395 | } |
| 396 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 397 | static int |
| 398 | _SET_is_populated(asn_TYPE_descriptor_t *td, void *st) { |
| 399 | asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics; |
| 400 | int edx; |
| 401 | |
| 402 | /* |
| 403 | * Check that all mandatory elements are present. |
| 404 | */ |
| 405 | for(edx = 0; edx < td->elements_count; |
| 406 | edx += (8 * sizeof(specs->_mandatory_elements[0]))) { |
| 407 | unsigned int midx, pres, must; |
| 408 | |
| 409 | midx = edx/(8 * sizeof(specs->_mandatory_elements[0])); |
| 410 | pres = ((unsigned int *)((char *)st+specs->pres_offset))[midx]; |
| 411 | must = ntohl(specs->_mandatory_elements[midx]); |
| 412 | |
| 413 | if((pres & must) == must) { |
| 414 | /* |
| 415 | * Yes, everything seems to be in place. |
| 416 | */ |
| 417 | } else { |
| 418 | ASN_DEBUG("One or more mandatory elements " |
| 419 | "of a SET %s %d (%08x.%08x)=%08x " |
| 420 | "are not present", |
| 421 | td->name, |
| 422 | midx, |
| 423 | pres, |
| 424 | must, |
| 425 | (~(pres & must) & must) |
| 426 | ); |
| 427 | return 0; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | return 1; |
| 432 | } |
| 433 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 434 | /* |
| 435 | * The DER encoder of the SET type. |
| 436 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 437 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 438 | SET_encode_der(asn_TYPE_descriptor_t *td, |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 439 | void *sptr, int tag_mode, ber_tlv_tag_t tag, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 440 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 441 | asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 442 | size_t computed_size = 0; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 443 | asn_enc_rval_t er; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 444 | int t2m_build_own = (specs->tag2el_count != td->elements_count); |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 445 | asn_TYPE_tag2member_t *t2m; |
Lev Walkin | f15320b | 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) { |
Lev Walkin | c17d90f | 2005-01-17 14:32:45 +0000 | [diff] [blame] | 454 | t2m = (asn_TYPE_tag2member_t *)alloca( |
| 455 | td->elements_count * sizeof(t2m[0])); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 456 | if(!t2m) _ASN_ENCODE_FAILED; /* There are such platforms */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 457 | t2m_count = 0; |
Lev Walkin | f15320b | 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 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 470 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 471 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 472 | asn_enc_rval_t tmper; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 473 | void *memb_ptr; |
| 474 | |
| 475 | /* |
| 476 | * Compute the length of the encoding of this member. |
| 477 | */ |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 478 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 479 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 { |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 489 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 490 | } |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 491 | tmper = elm->type->der_encoder(elm->type, memb_ptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 492 | elm->tag_mode, elm->tag, |
| 493 | 0, 0); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 494 | if(tmper.encoded == -1) |
| 495 | return tmper; |
| 496 | computed_size += tmper.encoded; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 497 | |
| 498 | /* |
| 499 | * Remember the outmost tag of this member. |
| 500 | */ |
| 501 | if(t2m_build_own) { |
| 502 | t2m[t2m_count].el_no = edx; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 503 | t2m[t2m_count].el_tag = asn_TYPE_outmost_tag( |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 516 | assert(t2m_count == td->elements_count); |
Lev Walkin | f15320b | 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 | */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 532 | ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 533 | if(ret == -1) _ASN_ENCODE_FAILED; |
| 534 | er.encoded = computed_size + ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 535 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 536 | if(!cb) return er; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 537 | |
| 538 | /* |
| 539 | * Encode all members. |
| 540 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 541 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 542 | asn_TYPE_member_t *elm; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 543 | asn_enc_rval_t tmper; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 544 | void *memb_ptr; |
| 545 | |
| 546 | /* Encode according to the tag order */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 547 | elm = &td->elements[t2m[edx].el_no]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 548 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 549 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 550 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 551 | if(!memb_ptr) continue; |
| 552 | } else { |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 553 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 554 | } |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 555 | tmper = elm->type->der_encoder(elm->type, memb_ptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 556 | elm->tag_mode, elm->tag, |
| 557 | cb, app_key); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 558 | if(tmper.encoded == -1) |
| 559 | return tmper; |
| 560 | computed_size -= tmper.encoded; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | if(computed_size != 0) { |
| 564 | /* |
| 565 | * Encoded size is not equal to the computed size. |
| 566 | */ |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 567 | _ASN_ENCODE_FAILED; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 570 | return er; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 573 | #undef XER_ADVANCE |
| 574 | #define XER_ADVANCE(num_bytes) do { \ |
| 575 | size_t num = num_bytes; \ |
| 576 | buf_ptr = ((char *)buf_ptr) + num; \ |
| 577 | size -= num; \ |
| 578 | consumed_myself += num; \ |
| 579 | } while(0) |
| 580 | |
| 581 | /* |
| 582 | * Decode the XER (XML) data. |
| 583 | */ |
| 584 | asn_dec_rval_t |
| 585 | SET_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 586 | void **struct_ptr, const char *opt_mname, |
| 587 | void *buf_ptr, size_t size) { |
| 588 | /* |
| 589 | * Bring closer parts of structure description. |
| 590 | */ |
| 591 | asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics; |
| 592 | asn_TYPE_member_t *elements = td->elements; |
| 593 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 594 | |
| 595 | /* |
| 596 | * ... and parts of the structure being constructed. |
| 597 | */ |
| 598 | void *st = *struct_ptr; /* Target structure. */ |
| 599 | asn_struct_ctx_t *ctx; /* Decoder context */ |
| 600 | |
| 601 | asn_dec_rval_t rval; /* Return value from a decoder */ |
| 602 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 603 | int edx; /* Element index */ |
| 604 | |
| 605 | /* |
| 606 | * Create the target structure if it is not present already. |
| 607 | */ |
| 608 | if(st == 0) { |
| 609 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 610 | if(st == 0) RETURN(RC_FAIL); |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * Restore parsing context. |
| 615 | */ |
| 616 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
| 617 | |
| 618 | /* |
| 619 | * Phases of XER/XML processing: |
| 620 | * Phase 0: Check that the opening tag matches our expectations. |
| 621 | * Phase 1: Processing body and reacting on closing tag. |
| 622 | * Phase 2: Processing inner type. |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 623 | * Phase 3: Skipping unknown extensions. |
| 624 | * Phase 4: PHASED OUT |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 625 | */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 626 | for(edx = ctx->step; ctx->phase <= 3;) { |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 627 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 628 | ssize_t ch_size; /* Chunk size */ |
| 629 | xer_check_tag_e tcv; /* Tag check value */ |
| 630 | asn_TYPE_member_t *elm; |
| 631 | |
| 632 | /* |
| 633 | * Go inside the inner member of a set. |
| 634 | */ |
| 635 | if(ctx->phase == 2) { |
| 636 | asn_dec_rval_t tmprval; |
| 637 | void *memb_ptr; /* Pointer to the member */ |
| 638 | void **memb_ptr2; /* Pointer to that pointer */ |
| 639 | |
| 640 | if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, |
| 641 | edx)) { |
| 642 | ASN_DEBUG("SET %s: Duplicate element %s (%d)", |
| 643 | td->name, elements[edx].name, edx); |
| 644 | RETURN(RC_FAIL); |
| 645 | } |
| 646 | |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 647 | elm = &elements[edx]; |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 648 | |
| 649 | if(elm->flags & ATF_POINTER) { |
| 650 | /* Member is a pointer to another structure */ |
| 651 | memb_ptr2 = (void **)((char *)st |
| 652 | + elm->memb_offset); |
| 653 | } else { |
| 654 | memb_ptr = (char *)st + elm->memb_offset; |
| 655 | memb_ptr2 = &memb_ptr; |
| 656 | } |
| 657 | |
| 658 | /* Invoke the inner type decoder, m.b. multiple times */ |
| 659 | tmprval = elm->type->xer_decoder(opt_codec_ctx, |
| 660 | elm->type, memb_ptr2, elm->name, |
| 661 | buf_ptr, size); |
| 662 | XER_ADVANCE(tmprval.consumed); |
| 663 | if(tmprval.code != RC_OK) |
| 664 | RETURN(tmprval.code); |
| 665 | ctx->phase = 1; /* Back to body processing */ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 666 | ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx); |
| 667 | ASN_DEBUG("XER/SET phase => %d", ctx->phase); |
| 668 | /* Fall through */ |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * Get the next part of the XML stream. |
| 673 | */ |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 674 | ch_size = xer_next_token(&ctx->context, |
| 675 | buf_ptr, size, &ch_type); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 676 | switch(ch_size) { |
| 677 | case -1: RETURN(RC_FAIL); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 678 | case 0: RETURN(RC_WMORE); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 679 | default: |
| 680 | switch(ch_type) { |
| 681 | case PXER_COMMENT: /* Got XML comment */ |
| 682 | case PXER_TEXT: /* Ignore free-standing text */ |
| 683 | XER_ADVANCE(ch_size); /* Skip silently */ |
| 684 | continue; |
| 685 | case PXER_TAG: |
| 686 | break; /* Check the rest down there */ |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | tcv = xer_check_tag(buf_ptr, ch_size, xml_tag); |
| 691 | ASN_DEBUG("XER/SET: tcv = %d, ph=%d", tcv, ctx->phase); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 692 | |
| 693 | /* Skip the extensions section */ |
| 694 | if(ctx->phase == 3) { |
| 695 | switch(xer_skip_unknown(tcv, &ctx->left)) { |
| 696 | case -1: |
| 697 | ctx->phase = 4; |
| 698 | RETURN(RC_FAIL); |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 699 | case 1: |
| 700 | ctx->phase = 1; |
| 701 | /* Fall through */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 702 | case 0: |
| 703 | XER_ADVANCE(ch_size); |
| 704 | continue; |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 705 | case 2: |
| 706 | ctx->phase = 1; |
| 707 | break; |
| 708 | } |
| 709 | } |
| 710 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 711 | switch(tcv) { |
| 712 | case XCT_CLOSING: |
| 713 | if(ctx->phase == 0) break; |
| 714 | ctx->phase = 0; |
| 715 | /* Fall through */ |
| 716 | case XCT_BOTH: |
| 717 | if(ctx->phase == 0) { |
| 718 | if(_SET_is_populated(td, st)) { |
| 719 | XER_ADVANCE(ch_size); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 720 | ctx->phase = 4; /* Phase out */ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 721 | RETURN(RC_OK); |
| 722 | } else { |
| 723 | ASN_DEBUG("Premature end of XER SET"); |
| 724 | RETURN(RC_FAIL); |
| 725 | } |
| 726 | } |
| 727 | /* Fall through */ |
| 728 | case XCT_OPENING: |
| 729 | if(ctx->phase == 0) { |
| 730 | XER_ADVANCE(ch_size); |
| 731 | ctx->phase = 1; /* Processing body phase */ |
| 732 | continue; |
| 733 | } |
| 734 | /* Fall through */ |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 735 | case XCT_UNKNOWN_OP: |
| 736 | case XCT_UNKNOWN_BO: |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 737 | |
| 738 | ASN_DEBUG("XER/SET: tcv=%d, ph=%d", tcv, ctx->phase); |
| 739 | if(ctx->phase != 1) |
| 740 | break; /* Really unexpected */ |
| 741 | |
| 742 | /* |
| 743 | * Search which member corresponds to this tag. |
| 744 | */ |
| 745 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 746 | switch(xer_check_tag(buf_ptr, ch_size, |
| 747 | elements[edx].name)) { |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 748 | case XCT_BOTH: |
| 749 | case XCT_OPENING: |
| 750 | /* |
| 751 | * Process this member. |
| 752 | */ |
| 753 | ctx->step = edx; |
| 754 | ctx->phase = 2; |
| 755 | break; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 756 | case XCT_UNKNOWN_OP: |
| 757 | case XCT_UNKNOWN_BO: |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 758 | continue; |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 759 | default: |
| 760 | edx = td->elements_count; |
| 761 | break; /* Phase out */ |
| 762 | } |
| 763 | break; |
| 764 | } |
| 765 | if(edx != td->elements_count) |
| 766 | continue; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 767 | |
| 768 | /* It is expected extension */ |
| 769 | if(specs->extensible) { |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 770 | ASN_DEBUG("Got anticipated extension"); |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 771 | /* |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 772 | * Check for (XCT_BOTH or XCT_UNKNOWN_BO) |
| 773 | * By using a mask. Only record a pure |
| 774 | * <opening> tags. |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 775 | */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 776 | if(tcv & XCT_CLOSING) { |
| 777 | /* Found </extension> without body */ |
| 778 | } else { |
| 779 | ctx->left = 1; |
| 780 | ctx->phase = 3; /* Skip ...'s */ |
| 781 | } |
| 782 | XER_ADVANCE(ch_size); |
| 783 | continue; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 786 | /* Fall through */ |
| 787 | default: |
| 788 | break; |
| 789 | } |
| 790 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 791 | ASN_DEBUG("Unexpected XML tag in SET, expected \"%s\"", |
| 792 | xml_tag); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 793 | break; |
| 794 | } |
| 795 | |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 796 | ctx->phase = 4; /* "Phase out" on hard failure */ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 797 | RETURN(RC_FAIL); |
| 798 | } |
| 799 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 800 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 801 | SET_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 802 | int ilevel, enum xer_encoder_flags_e flags, |
| 803 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 804 | asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 805 | asn_enc_rval_t er; |
| 806 | int xcan = (flags & XER_F_CANONICAL); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 807 | asn_TYPE_tag2member_t *t2m = specs->tag2el_cxer; |
| 808 | int t2m_count = specs->tag2el_cxer_count; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 809 | int edx; |
| 810 | |
| 811 | if(!sptr) |
| 812 | _ASN_ENCODE_FAILED; |
| 813 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 814 | assert(t2m_count == td->elements_count); |
| 815 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 816 | er.encoded = 0; |
| 817 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 818 | for(edx = 0; edx < t2m_count; edx++) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 819 | asn_enc_rval_t tmper; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 820 | asn_TYPE_member_t *elm; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 821 | void *memb_ptr; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 822 | const char *mname; |
| 823 | unsigned int mlen; |
| 824 | |
| 825 | elm = &td->elements[t2m[edx].el_no]; |
| 826 | mname = elm->name; |
| 827 | mlen = strlen(elm->name); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 828 | |
| 829 | if(elm->flags & ATF_POINTER) { |
| 830 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
| 831 | if(!memb_ptr) continue; /* OPTIONAL element? */ |
| 832 | } else { |
| 833 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
| 834 | } |
| 835 | |
| 836 | if(!xcan) |
| 837 | _i_ASN_TEXT_INDENT(1, ilevel); |
| 838 | _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1); |
| 839 | |
| 840 | /* Print the member itself */ |
| 841 | tmper = elm->type->xer_encoder(elm->type, memb_ptr, |
| 842 | ilevel + 1, flags, cb, app_key); |
| 843 | if(tmper.encoded == -1) return tmper; |
| 844 | |
| 845 | _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1); |
| 846 | |
| 847 | er.encoded += 5 + (2 * mlen) + tmper.encoded; |
| 848 | } |
| 849 | |
| 850 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1); |
| 851 | |
| 852 | return er; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 853 | cb_failed: |
| 854 | _ASN_ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 857 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 858 | SET_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 859 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 860 | int edx; |
| 861 | int ret; |
| 862 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 863 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 864 | |
| 865 | /* Dump preamble */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 866 | if(cb(td->name, strlen(td->name), app_key) < 0 |
| 867 | || cb(" ::= {", 6, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 868 | return -1; |
| 869 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 870 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 871 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 872 | const void *memb_ptr; |
| 873 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 874 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 875 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
| 876 | if(!memb_ptr) continue; |
| 877 | } else { |
| 878 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 879 | } |
| 880 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 881 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 882 | |
| 883 | /* Print the member's name and stuff */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 884 | if(cb(elm->name, strlen(elm->name), app_key) < 0 |
| 885 | || cb(": ", 2, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 886 | return -1; |
| 887 | |
| 888 | /* Print the member itself */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 889 | ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 890 | cb, app_key); |
| 891 | if(ret) return ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 894 | ilevel--; |
| 895 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 896 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 897 | return (cb("}", 1, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | void |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 901 | SET_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 902 | int edx; |
| 903 | |
| 904 | if(!td || !ptr) |
| 905 | return; |
| 906 | |
| 907 | ASN_DEBUG("Freeing %s as SET", td->name); |
| 908 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 909 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 910 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 911 | void *memb_ptr; |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 912 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 913 | memb_ptr = *(void **)((char *)ptr + elm->memb_offset); |
| 914 | if(memb_ptr) |
| 915 | elm->type->free_struct(elm->type, memb_ptr, 0); |
| 916 | } else { |
| 917 | memb_ptr = (void *)((char *)ptr + elm->memb_offset); |
| 918 | elm->type->free_struct(elm->type, memb_ptr, 1); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | if(!contents_only) { |
| 923 | FREEMEM(ptr); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 928 | SET_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 929 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 930 | int edx; |
| 931 | |
| 932 | if(!sptr) { |
Lev Walkin | ba4e518 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 933 | _ASN_ERRLOG(app_errlog, app_key, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 934 | "%s: value not given (%s:%d)", |
| 935 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 936 | return -1; |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * Iterate over structure members and check their validity. |
| 941 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 942 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 943 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 944 | const void *memb_ptr; |
| 945 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 946 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 947 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
| 948 | if(!memb_ptr) { |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 949 | if(elm->optional) |
| 950 | continue; |
| 951 | _ASN_ERRLOG(app_errlog, app_key, |
| 952 | "%s: mandatory element %s absent (%s:%d)", |
| 953 | td->name, elm->name, __FILE__, __LINE__); |
| 954 | return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 955 | } |
| 956 | } else { |
| 957 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 958 | } |
| 959 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 960 | if(elm->memb_constraints) { |
| 961 | int ret = elm->memb_constraints(elm->type, memb_ptr, |
| 962 | app_errlog, app_key); |
| 963 | if(ret) return ret; |
| 964 | } else { |
| 965 | int ret = elm->type->check_constraints(elm->type, |
| 966 | memb_ptr, app_errlog, app_key); |
| 967 | if(ret) return ret; |
| 968 | /* |
| 969 | * Cannot inherit it earlier: |
| 970 | * need to make sure we get the updated version. |
| 971 | */ |
| 972 | elm->memb_constraints = elm->type->check_constraints; |
| 973 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | return 0; |
| 977 | } |