Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | adf863f | 2006-09-05 16:18:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>. |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 3 | * All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 4 | * Redistribution and modifications are permitted subject to BSD license. |
| 5 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 6 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | #include <constr_SET.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 9 | /* Check that all the mandatory members are present */ |
| 10 | static int _SET_is_populated(asn_TYPE_descriptor_t *td, void *st); |
| 11 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 12 | /* |
| 13 | * Number of bytes left for this structure. |
| 14 | * (ctx->left) indicates the number of bytes _transferred_ for the structure. |
| 15 | * (size) contains the number of bytes in the buffer passed. |
| 16 | */ |
Lev Walkin | ec1ffd4 | 2004-08-18 04:53:32 +0000 | [diff] [blame] | 17 | #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * If the subprocessor function returns with an indication that it wants |
| 21 | * more data, it may well be a fatal decoding problem, because the |
| 22 | * size is constrained by the <TLV>'s L, even if the buffer size allows |
| 23 | * reading more data. |
| 24 | * For example, consider the buffer containing the following TLVs: |
| 25 | * <T:5><L:1><V> <T:6>... |
| 26 | * The TLV length clearly indicates that one byte is expected in V, but |
| 27 | * if the V processor returns with "want more data" even if the buffer |
| 28 | * contains way more data than the V processor have seen. |
| 29 | */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 30 | #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * This macro "eats" the part of the buffer which is definitely "consumed", |
| 34 | * i.e. was correctly converted into local representation or rightfully skipped. |
| 35 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 36 | #undef ADVANCE |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | #define ADVANCE(num_bytes) do { \ |
| 38 | size_t num = num_bytes; \ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 39 | ptr = ((const char *)ptr) + num;\ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | size -= num; \ |
| 41 | if(ctx->left >= 0) \ |
| 42 | ctx->left -= num; \ |
| 43 | consumed_myself += num; \ |
| 44 | } while(0) |
| 45 | |
| 46 | /* |
| 47 | * Switch to the next phase of parsing. |
| 48 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 49 | #undef NEXT_PHASE |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 50 | #define NEXT_PHASE(ctx) do { \ |
| 51 | ctx->phase++; \ |
| 52 | ctx->step = 0; \ |
| 53 | } while(0) |
| 54 | |
| 55 | /* |
| 56 | * Return a standardized complex structure. |
| 57 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 58 | #undef RETURN |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 59 | #define RETURN(_code) do { \ |
| 60 | rval.code = _code; \ |
| 61 | rval.consumed = consumed_myself;\ |
| 62 | return rval; \ |
| 63 | } while(0) |
| 64 | |
| 65 | /* |
| 66 | * Tags are canonically sorted in the tag2element map. |
| 67 | */ |
| 68 | static int |
| 69 | _t2e_cmp(const void *ap, const void *bp) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 70 | const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap; |
| 71 | const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 72 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 73 | int a_class = BER_TAG_CLASS(a->el_tag); |
| 74 | int b_class = BER_TAG_CLASS(b->el_tag); |
| 75 | |
| 76 | if(a_class == b_class) { |
| 77 | ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag); |
| 78 | ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag); |
| 79 | |
| 80 | if(a_value == b_value) |
| 81 | return 0; |
| 82 | else if(a_value < b_value) |
| 83 | return -1; |
| 84 | else |
| 85 | return 1; |
| 86 | } else if(a_class < b_class) { |
| 87 | return -1; |
| 88 | } else { |
| 89 | return 1; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * The decoder of the SET type. |
| 95 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 96 | asn_dec_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 97 | SET_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 98 | void **struct_ptr, const void *ptr, size_t size, int tag_mode) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 99 | /* |
| 100 | * Bring closer parts of structure description. |
| 101 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 102 | asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics; |
| 103 | asn_TYPE_member_t *elements = td->elements; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * Parts of the structure being constructed. |
| 107 | */ |
| 108 | void *st = *struct_ptr; /* Target structure. */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 109 | asn_struct_ctx_t *ctx; /* Decoder context */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | |
| 111 | ber_tlv_tag_t tlv_tag; /* T from TLV */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 112 | asn_dec_rval_t rval; /* Return code from subparsers */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 113 | |
| 114 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
| 115 | int edx; /* SET element's index */ |
| 116 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 117 | ASN_DEBUG("Decoding %s as SET", td->name); |
Lev Walkin | adf863f | 2006-09-05 16:18:34 +0000 | [diff] [blame] | 118 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 119 | if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx)) |
| 120 | ASN__DECODE_FAILED; |
Lev Walkin | adf863f | 2006-09-05 16:18:34 +0000 | [diff] [blame] | 121 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 | aa61a0f | 2014-01-13 23:08:47 -0800 | [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) { |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 178 | const asn_TYPE_tag2member_t *t2m; |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 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 | |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 211 | if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 212 | if(LEFT < 2) { |
| 213 | if(SIZE_VIOLATION) |
| 214 | RETURN(RC_FAIL); |
| 215 | else |
| 216 | RETURN(RC_WMORE); |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 217 | } else if(((const uint8_t *)ptr)[1] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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; |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 228 | t2m = (const asn_TYPE_tag2member_t *)bsearch(&key, |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 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), |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 253 | (const 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 | aa61a0f | 2014-01-13 23:08:47 -0800 | [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 | aa61a0f | 2014-01-13 23:08:47 -0800 | [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 | */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 293 | memb_ptr = (char *)st + elements[edx].memb_offset; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 | aa61a0f | 2014-01-13 23:08:47 -0800 | [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 |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 345 | && ((const uint8_t *)ptr)[0] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 346 | if(LEFT < 2) { |
| 347 | if(SIZE_VIOLATION) |
| 348 | RETURN(RC_FAIL); |
| 349 | else |
| 350 | RETURN(RC_WMORE); |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 351 | } else if(((const uint8_t *)ptr)[1] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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)", |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 366 | td->name, *(const 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 | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 372 | (const 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: |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 384 | /* Check that all mandatory elements are present. */ |
| 385 | if(!_SET_is_populated(td, st)) |
| 386 | RETURN(RC_FAIL); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 387 | |
| 388 | NEXT_PHASE(ctx); |
| 389 | } |
| 390 | |
| 391 | RETURN(RC_OK); |
| 392 | } |
| 393 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 394 | static int |
| 395 | _SET_is_populated(asn_TYPE_descriptor_t *td, void *st) { |
| 396 | asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics; |
| 397 | int edx; |
| 398 | |
| 399 | /* |
| 400 | * Check that all mandatory elements are present. |
| 401 | */ |
| 402 | for(edx = 0; edx < td->elements_count; |
| 403 | edx += (8 * sizeof(specs->_mandatory_elements[0]))) { |
| 404 | unsigned int midx, pres, must; |
| 405 | |
| 406 | midx = edx/(8 * sizeof(specs->_mandatory_elements[0])); |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 407 | pres = ((unsigned int *)((char *)st + specs->pres_offset))[midx]; |
Lev Walkin | 217f240 | 2006-11-21 10:38:50 +0000 | [diff] [blame] | 408 | must = sys_ntohl(specs->_mandatory_elements[midx]); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 409 | |
| 410 | if((pres & must) == must) { |
| 411 | /* |
| 412 | * Yes, everything seems to be in place. |
| 413 | */ |
| 414 | } else { |
| 415 | ASN_DEBUG("One or more mandatory elements " |
| 416 | "of a SET %s %d (%08x.%08x)=%08x " |
| 417 | "are not present", |
| 418 | td->name, |
| 419 | midx, |
| 420 | pres, |
| 421 | must, |
| 422 | (~(pres & must) & must) |
| 423 | ); |
| 424 | return 0; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | return 1; |
| 429 | } |
| 430 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 431 | /* |
| 432 | * The DER encoder of the SET type. |
| 433 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 434 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 435 | SET_encode_der(asn_TYPE_descriptor_t *td, |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 436 | void *sptr, int tag_mode, ber_tlv_tag_t tag, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 437 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 438 | asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 439 | size_t computed_size = 0; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 440 | asn_enc_rval_t er; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 441 | int t2m_build_own = (specs->tag2el_count != td->elements_count); |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 442 | const asn_TYPE_tag2member_t *t2m; |
| 443 | asn_TYPE_tag2member_t *t2m_build; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 444 | int t2m_count; |
| 445 | ssize_t ret; |
| 446 | int edx; |
| 447 | |
| 448 | /* |
| 449 | * Use existing, or build our own tags map. |
| 450 | */ |
| 451 | if(t2m_build_own) { |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 452 | t2m_build = (asn_TYPE_tag2member_t *)alloca( |
| 453 | td->elements_count * sizeof(t2m_build[0])); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 454 | if(!t2m_build) ASN__ENCODE_FAILED; /* There are such platforms */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 455 | t2m_count = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 456 | } else { |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 457 | t2m_build = NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 458 | /* |
| 459 | * There is no untagged CHOICE in this SET. |
| 460 | * Employ existing table. |
| 461 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* |
| 465 | * Gather the length of the underlying members sequence. |
| 466 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 467 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 468 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 469 | asn_enc_rval_t tmper; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 470 | void *memb_ptr; |
| 471 | |
| 472 | /* |
| 473 | * Compute the length of the encoding of this member. |
| 474 | */ |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 475 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 476 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 477 | if(!memb_ptr) { |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 478 | if(!elm->optional) |
| 479 | /* Mandatory elements missing */ |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 480 | ASN__ENCODE_FAILED; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 481 | if(t2m_build_own) { |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 482 | t2m_build[t2m_count].el_no = edx; |
| 483 | t2m_build[t2m_count].el_tag = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 484 | t2m_count++; |
| 485 | } |
| 486 | continue; |
| 487 | } |
| 488 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [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) { |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 502 | t2m_build[t2m_count].el_no = edx; |
| 503 | t2m_build[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 | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 516 | if(t2m_build_own) { |
| 517 | /* |
| 518 | * Sort the underlying members according to their |
| 519 | * canonical tags order. DER encoding mandates it. |
| 520 | */ |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 521 | qsort(t2m_build, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp); |
| 522 | t2m = t2m_build; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 523 | } else { |
| 524 | /* |
| 525 | * Tags are already sorted by the compiler. |
| 526 | */ |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 527 | t2m = specs->tag2el; |
| 528 | t2m_count = specs->tag2el_count; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 529 | } |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 530 | assert(t2m_count == td->elements_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 531 | |
| 532 | /* |
| 533 | * Encode the TLV for the sequence itself. |
| 534 | */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 535 | ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 536 | if(ret == -1) ASN__ENCODE_FAILED; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 537 | er.encoded = computed_size + ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 538 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 539 | if(!cb) ASN__ENCODED_OK(er); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 540 | |
| 541 | /* |
| 542 | * Encode all members. |
| 543 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 544 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 545 | asn_TYPE_member_t *elm; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 546 | asn_enc_rval_t tmper; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 547 | void *memb_ptr; |
| 548 | |
| 549 | /* Encode according to the tag order */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 550 | elm = &td->elements[t2m[edx].el_no]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 551 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 552 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 553 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 554 | if(!memb_ptr) continue; |
| 555 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 556 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 557 | } |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 558 | tmper = elm->type->der_encoder(elm->type, memb_ptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 559 | elm->tag_mode, elm->tag, |
| 560 | cb, app_key); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 561 | if(tmper.encoded == -1) |
| 562 | return tmper; |
| 563 | computed_size -= tmper.encoded; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | if(computed_size != 0) { |
| 567 | /* |
| 568 | * Encoded size is not equal to the computed size. |
| 569 | */ |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 570 | ASN__ENCODE_FAILED; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 573 | ASN__ENCODED_OK(er); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 576 | #undef XER_ADVANCE |
| 577 | #define XER_ADVANCE(num_bytes) do { \ |
| 578 | size_t num = num_bytes; \ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 579 | buf_ptr = ((const char *)buf_ptr) + num;\ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 580 | size -= num; \ |
| 581 | consumed_myself += num; \ |
| 582 | } while(0) |
| 583 | |
| 584 | /* |
| 585 | * Decode the XER (XML) data. |
| 586 | */ |
| 587 | asn_dec_rval_t |
| 588 | SET_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 589 | void **struct_ptr, const char *opt_mname, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 590 | const void *buf_ptr, size_t size) { |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 591 | /* |
| 592 | * Bring closer parts of structure description. |
| 593 | */ |
| 594 | asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics; |
| 595 | asn_TYPE_member_t *elements = td->elements; |
| 596 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 597 | |
| 598 | /* |
| 599 | * ... and parts of the structure being constructed. |
| 600 | */ |
| 601 | void *st = *struct_ptr; /* Target structure. */ |
| 602 | asn_struct_ctx_t *ctx; /* Decoder context */ |
| 603 | |
| 604 | asn_dec_rval_t rval; /* Return value from a decoder */ |
| 605 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 606 | int edx; /* Element index */ |
| 607 | |
| 608 | /* |
| 609 | * Create the target structure if it is not present already. |
| 610 | */ |
| 611 | if(st == 0) { |
| 612 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 613 | if(st == 0) RETURN(RC_FAIL); |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * Restore parsing context. |
| 618 | */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 619 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 620 | |
| 621 | /* |
| 622 | * Phases of XER/XML processing: |
| 623 | * Phase 0: Check that the opening tag matches our expectations. |
| 624 | * Phase 1: Processing body and reacting on closing tag. |
| 625 | * Phase 2: Processing inner type. |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 626 | * Phase 3: Skipping unknown extensions. |
| 627 | * Phase 4: PHASED OUT |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 628 | */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 629 | for(edx = ctx->step; ctx->phase <= 3;) { |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 630 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 631 | ssize_t ch_size; /* Chunk size */ |
| 632 | xer_check_tag_e tcv; /* Tag check value */ |
| 633 | asn_TYPE_member_t *elm; |
| 634 | |
| 635 | /* |
| 636 | * Go inside the inner member of a set. |
| 637 | */ |
| 638 | if(ctx->phase == 2) { |
| 639 | asn_dec_rval_t tmprval; |
| 640 | void *memb_ptr; /* Pointer to the member */ |
| 641 | void **memb_ptr2; /* Pointer to that pointer */ |
| 642 | |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 643 | if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 644 | edx)) { |
| 645 | ASN_DEBUG("SET %s: Duplicate element %s (%d)", |
| 646 | td->name, elements[edx].name, edx); |
| 647 | RETURN(RC_FAIL); |
| 648 | } |
| 649 | |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 650 | elm = &elements[edx]; |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 651 | |
| 652 | if(elm->flags & ATF_POINTER) { |
| 653 | /* Member is a pointer to another structure */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 654 | memb_ptr2 = (void **)((char *)st + elm->memb_offset); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 655 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 656 | memb_ptr = (char *)st + elm->memb_offset; |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 657 | memb_ptr2 = &memb_ptr; |
| 658 | } |
| 659 | |
| 660 | /* Invoke the inner type decoder, m.b. multiple times */ |
| 661 | tmprval = elm->type->xer_decoder(opt_codec_ctx, |
| 662 | elm->type, memb_ptr2, elm->name, |
| 663 | buf_ptr, size); |
| 664 | XER_ADVANCE(tmprval.consumed); |
| 665 | if(tmprval.code != RC_OK) |
| 666 | RETURN(tmprval.code); |
| 667 | ctx->phase = 1; /* Back to body processing */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 668 | ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 669 | ASN_DEBUG("XER/SET phase => %d", ctx->phase); |
| 670 | /* Fall through */ |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | * Get the next part of the XML stream. |
| 675 | */ |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 676 | ch_size = xer_next_token(&ctx->context, |
| 677 | buf_ptr, size, &ch_type); |
Lev Walkin | 9736348 | 2016-01-24 19:23:02 -0800 | [diff] [blame] | 678 | if(ch_size == -1) { |
| 679 | RETURN(RC_FAIL); |
| 680 | } else { |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 681 | switch(ch_type) { |
Lev Walkin | 9736348 | 2016-01-24 19:23:02 -0800 | [diff] [blame] | 682 | case PXER_WMORE: |
| 683 | RETURN(RC_WMORE); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 684 | case PXER_COMMENT: /* Got XML comment */ |
| 685 | case PXER_TEXT: /* Ignore free-standing text */ |
| 686 | XER_ADVANCE(ch_size); /* Skip silently */ |
| 687 | continue; |
| 688 | case PXER_TAG: |
| 689 | break; /* Check the rest down there */ |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | tcv = xer_check_tag(buf_ptr, ch_size, xml_tag); |
| 694 | ASN_DEBUG("XER/SET: tcv = %d, ph=%d", tcv, ctx->phase); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 695 | |
| 696 | /* Skip the extensions section */ |
| 697 | if(ctx->phase == 3) { |
| 698 | switch(xer_skip_unknown(tcv, &ctx->left)) { |
| 699 | case -1: |
| 700 | ctx->phase = 4; |
| 701 | RETURN(RC_FAIL); |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 702 | case 1: |
| 703 | ctx->phase = 1; |
| 704 | /* Fall through */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 705 | case 0: |
| 706 | XER_ADVANCE(ch_size); |
| 707 | continue; |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 708 | case 2: |
| 709 | ctx->phase = 1; |
| 710 | break; |
| 711 | } |
| 712 | } |
| 713 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 714 | switch(tcv) { |
| 715 | case XCT_CLOSING: |
| 716 | if(ctx->phase == 0) break; |
| 717 | ctx->phase = 0; |
| 718 | /* Fall through */ |
| 719 | case XCT_BOTH: |
| 720 | if(ctx->phase == 0) { |
| 721 | if(_SET_is_populated(td, st)) { |
| 722 | XER_ADVANCE(ch_size); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 723 | ctx->phase = 4; /* Phase out */ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 724 | RETURN(RC_OK); |
| 725 | } else { |
| 726 | ASN_DEBUG("Premature end of XER SET"); |
| 727 | RETURN(RC_FAIL); |
| 728 | } |
| 729 | } |
| 730 | /* Fall through */ |
| 731 | case XCT_OPENING: |
| 732 | if(ctx->phase == 0) { |
| 733 | XER_ADVANCE(ch_size); |
| 734 | ctx->phase = 1; /* Processing body phase */ |
| 735 | continue; |
| 736 | } |
| 737 | /* Fall through */ |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 738 | case XCT_UNKNOWN_OP: |
| 739 | case XCT_UNKNOWN_BO: |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 740 | |
| 741 | ASN_DEBUG("XER/SET: tcv=%d, ph=%d", tcv, ctx->phase); |
| 742 | if(ctx->phase != 1) |
| 743 | break; /* Really unexpected */ |
| 744 | |
| 745 | /* |
| 746 | * Search which member corresponds to this tag. |
| 747 | */ |
| 748 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 749 | switch(xer_check_tag(buf_ptr, ch_size, |
| 750 | elements[edx].name)) { |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 751 | case XCT_BOTH: |
| 752 | case XCT_OPENING: |
| 753 | /* |
| 754 | * Process this member. |
| 755 | */ |
| 756 | ctx->step = edx; |
| 757 | ctx->phase = 2; |
| 758 | break; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 759 | case XCT_UNKNOWN_OP: |
| 760 | case XCT_UNKNOWN_BO: |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 761 | continue; |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 762 | default: |
| 763 | edx = td->elements_count; |
| 764 | break; /* Phase out */ |
| 765 | } |
| 766 | break; |
| 767 | } |
| 768 | if(edx != td->elements_count) |
| 769 | continue; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 770 | |
| 771 | /* It is expected extension */ |
| 772 | if(specs->extensible) { |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 773 | ASN_DEBUG("Got anticipated extension"); |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 774 | /* |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 775 | * Check for (XCT_BOTH or XCT_UNKNOWN_BO) |
| 776 | * By using a mask. Only record a pure |
| 777 | * <opening> tags. |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 778 | */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 779 | if(tcv & XCT_CLOSING) { |
| 780 | /* Found </extension> without body */ |
| 781 | } else { |
| 782 | ctx->left = 1; |
| 783 | ctx->phase = 3; /* Skip ...'s */ |
| 784 | } |
| 785 | XER_ADVANCE(ch_size); |
| 786 | continue; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 789 | /* Fall through */ |
| 790 | default: |
| 791 | break; |
| 792 | } |
| 793 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 794 | ASN_DEBUG("Unexpected XML tag in SET, expected \"%s\"", |
| 795 | xml_tag); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 796 | break; |
| 797 | } |
| 798 | |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 799 | ctx->phase = 4; /* "Phase out" on hard failure */ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 800 | RETURN(RC_FAIL); |
| 801 | } |
| 802 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 803 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 804 | SET_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 805 | int ilevel, enum xer_encoder_flags_e flags, |
| 806 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 807 | asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 808 | asn_enc_rval_t er; |
| 809 | int xcan = (flags & XER_F_CANONICAL); |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 810 | const asn_TYPE_tag2member_t *t2m = specs->tag2el_cxer; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 811 | int t2m_count = specs->tag2el_cxer_count; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 812 | int edx; |
| 813 | |
| 814 | if(!sptr) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 815 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 816 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 817 | assert(t2m_count == td->elements_count); |
| 818 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 819 | er.encoded = 0; |
| 820 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 821 | for(edx = 0; edx < t2m_count; edx++) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 822 | asn_enc_rval_t tmper; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 823 | asn_TYPE_member_t *elm; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 824 | void *memb_ptr; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 825 | const char *mname; |
| 826 | unsigned int mlen; |
| 827 | |
| 828 | elm = &td->elements[t2m[edx].el_no]; |
| 829 | mname = elm->name; |
| 830 | mlen = strlen(elm->name); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 831 | |
| 832 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 833 | memb_ptr = *(void **)((char *)sptr + elm->memb_offset); |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 834 | if(!memb_ptr) { |
| 835 | if(elm->optional) |
| 836 | continue; |
| 837 | /* Mandatory element missing */ |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 838 | ASN__ENCODE_FAILED; |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 839 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 840 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 841 | memb_ptr = (void *)((char *)sptr + elm->memb_offset); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | if(!xcan) |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 845 | ASN__TEXT_INDENT(1, ilevel); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 846 | ASN__CALLBACK3("<", 1, mname, mlen, ">", 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 847 | |
| 848 | /* Print the member itself */ |
| 849 | tmper = elm->type->xer_encoder(elm->type, memb_ptr, |
| 850 | ilevel + 1, flags, cb, app_key); |
| 851 | if(tmper.encoded == -1) return tmper; |
| 852 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 853 | ASN__CALLBACK3("</", 2, mname, mlen, ">", 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 854 | |
| 855 | er.encoded += 5 + (2 * mlen) + tmper.encoded; |
| 856 | } |
| 857 | |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 858 | if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 859 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 860 | ASN__ENCODED_OK(er); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 861 | cb_failed: |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 862 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 865 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 866 | SET_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 867 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 868 | int edx; |
| 869 | int ret; |
| 870 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 871 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 872 | |
| 873 | /* Dump preamble */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 874 | if(cb(td->name, strlen(td->name), app_key) < 0 |
| 875 | || cb(" ::= {", 6, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 876 | return -1; |
| 877 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 878 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 879 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 880 | const void *memb_ptr; |
| 881 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 882 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 883 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 884 | if(!memb_ptr) { |
| 885 | if(elm->optional) continue; |
| 886 | /* Print <absent> line */ |
| 887 | /* Fall through */ |
| 888 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 889 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 890 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 893 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 894 | |
| 895 | /* Print the member's name and stuff */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 896 | if(cb(elm->name, strlen(elm->name), app_key) < 0 |
| 897 | || cb(": ", 2, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 898 | return -1; |
| 899 | |
| 900 | /* Print the member itself */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 901 | ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 902 | cb, app_key); |
| 903 | if(ret) return ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 906 | ilevel--; |
| 907 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 908 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 909 | return (cb("}", 1, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | void |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 913 | SET_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 914 | int edx; |
| 915 | |
| 916 | if(!td || !ptr) |
| 917 | return; |
| 918 | |
| 919 | ASN_DEBUG("Freeing %s as SET", td->name); |
| 920 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 921 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 922 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 923 | void *memb_ptr; |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 924 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 925 | memb_ptr = *(void **)((char *)ptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 926 | if(memb_ptr) |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 927 | ASN_STRUCT_FREE(*elm->type, memb_ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 928 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 929 | memb_ptr = (void *)((char *)ptr + elm->memb_offset); |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 930 | ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
| 934 | if(!contents_only) { |
| 935 | FREEMEM(ptr); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 940 | SET_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 941 | asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 942 | int edx; |
| 943 | |
| 944 | if(!sptr) { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 945 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 946 | "%s: value not given (%s:%d)", |
| 947 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 948 | return -1; |
| 949 | } |
| 950 | |
| 951 | /* |
| 952 | * Iterate over structure members and check their validity. |
| 953 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 954 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 955 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 956 | const void *memb_ptr; |
| 957 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 958 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 959 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 960 | if(!memb_ptr) { |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 961 | if(elm->optional) |
| 962 | continue; |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 963 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 964 | "%s: mandatory element %s absent (%s:%d)", |
| 965 | td->name, elm->name, __FILE__, __LINE__); |
| 966 | return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 967 | } |
| 968 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 969 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 972 | if(elm->memb_constraints) { |
| 973 | int ret = elm->memb_constraints(elm->type, memb_ptr, |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 974 | ctfailcb, app_key); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 975 | if(ret) return ret; |
| 976 | } else { |
| 977 | int ret = elm->type->check_constraints(elm->type, |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 978 | memb_ptr, ctfailcb, app_key); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 979 | if(ret) return ret; |
| 980 | /* |
| 981 | * Cannot inherit it earlier: |
| 982 | * need to make sure we get the updated version. |
| 983 | */ |
| 984 | elm->memb_constraints = elm->type->check_constraints; |
| 985 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | return 0; |
| 989 | } |