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