Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | e2bbbf8 | 2017-10-08 18:52:37 -0700 | [diff] [blame] | 2 | * Copyright (c) 2003-2017 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 */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 10 | static int _SET_is_populated(const asn_TYPE_descriptor_t *td, const void *st); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 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 | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 97 | SET_decode_ber(const asn_codec_ctx_t *opt_codec_ctx, |
| 98 | const asn_TYPE_descriptor_t *td, void **struct_ptr, |
| 99 | const void *ptr, size_t size, int tag_mode) { |
| 100 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 101 | * Bring closer parts of structure description. |
| 102 | */ |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 103 | const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 104 | const asn_TYPE_member_t *elements = td->elements; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * Parts of the structure being constructed. |
| 108 | */ |
| 109 | void *st = *struct_ptr; /* Target structure. */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 110 | asn_struct_ctx_t *ctx; /* Decoder context */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | |
| 112 | ber_tlv_tag_t tlv_tag; /* T from TLV */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 113 | asn_dec_rval_t rval; /* Return code from subparsers */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 114 | |
| 115 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 116 | size_t edx; /* SET element's index */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 117 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 118 | ASN_DEBUG("Decoding %s as SET", td->name); |
Lev Walkin | adf863f | 2006-09-05 16:18:34 +0000 | [diff] [blame] | 119 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 120 | if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx)) |
| 121 | ASN__DECODE_FAILED; |
Lev Walkin | adf863f | 2006-09-05 16:18:34 +0000 | [diff] [blame] | 122 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 123 | /* |
| 124 | * Create the target structure if it is not present already. |
| 125 | */ |
| 126 | if(st == 0) { |
| 127 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 128 | if(st == 0) { |
| 129 | RETURN(RC_FAIL); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Restore parsing context. |
| 135 | */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 136 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * Start to parse where left previously |
| 140 | */ |
| 141 | switch(ctx->phase) { |
| 142 | case 0: |
| 143 | /* |
| 144 | * PHASE 0. |
| 145 | * Check that the set of tags associated with given structure |
| 146 | * perfectly fits our expectations. |
| 147 | */ |
| 148 | |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 149 | rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 150 | tag_mode, 1, &ctx->left, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 151 | if(rval.code != RC_OK) { |
| 152 | ASN_DEBUG("%s tagging check failed: %d", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 153 | td->name, rval.code); |
Lev Walkin | 4ab42cd | 2004-10-05 06:36:44 +0000 | [diff] [blame] | 154 | return rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if(ctx->left >= 0) |
| 158 | ctx->left += rval.consumed; /* ?Substracted below! */ |
| 159 | ADVANCE(rval.consumed); |
| 160 | |
| 161 | NEXT_PHASE(ctx); |
| 162 | |
| 163 | ASN_DEBUG("Structure advertised %ld bytes, " |
| 164 | "buffer contains %ld", (long)ctx->left, (long)size); |
| 165 | |
| 166 | /* Fall through */ |
| 167 | case 1: |
| 168 | /* |
| 169 | * PHASE 1. |
| 170 | * From the place where we've left it previously, |
| 171 | * try to decode the next member from the list of |
| 172 | * this structure's elements. |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 173 | * Note that elements in BER may arrive out of |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 174 | * order, yet DER mandates that they shall arive in the |
| 175 | * canonical order of their tags. So, there is a room |
| 176 | * for optimization. |
| 177 | */ |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 178 | for(;; ctx->step = 0) { |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 179 | const asn_TYPE_tag2member_t *t2m; |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 180 | asn_TYPE_tag2member_t key; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 181 | void *memb_ptr; /* Pointer to the member */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 182 | void **memb_ptr2; /* Pointer to that pointer */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 183 | ssize_t tag_len; /* Length of TLV's T */ |
| 184 | |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 185 | if(ctx->step & 1) { |
| 186 | edx = ctx->step >> 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 187 | goto microphase2; |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 188 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * MICROPHASE 1: Synchronize decoding. |
| 192 | */ |
| 193 | |
| 194 | if(ctx->left == 0) |
| 195 | /* |
| 196 | * No more things to decode. |
| 197 | * Exit out of here and check whether all mandatory |
| 198 | * elements have been received (in the next phase). |
| 199 | */ |
| 200 | break; |
| 201 | |
| 202 | /* |
| 203 | * Fetch the T from TLV. |
| 204 | */ |
| 205 | tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 206 | switch(tag_len) { |
| 207 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 208 | /* Fall through */ |
| 209 | case -1: RETURN(RC_FAIL); |
| 210 | } |
| 211 | |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 212 | if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 213 | if(LEFT < 2) { |
| 214 | if(SIZE_VIOLATION) |
| 215 | RETURN(RC_FAIL); |
| 216 | else |
| 217 | RETURN(RC_WMORE); |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 218 | } else if(((const uint8_t *)ptr)[1] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 219 | /* |
| 220 | * Found the terminator of the |
| 221 | * indefinite length structure. |
| 222 | * Invoke the generic finalization function. |
| 223 | */ |
| 224 | goto phase3; |
| 225 | } |
| 226 | } |
| 227 | |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 228 | key.el_tag = tlv_tag; |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 229 | t2m = (const asn_TYPE_tag2member_t *)bsearch(&key, |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 230 | specs->tag2el, specs->tag2el_count, |
| 231 | sizeof(specs->tag2el[0]), _t2e_cmp); |
| 232 | if(t2m) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 233 | /* |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 234 | * Found the element corresponding to the tag. |
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 | edx = t2m->el_no; |
| 237 | ctx->step = (edx << 1) + 1; |
Lev Walkin | f3f7f94 | 2017-09-26 22:45:46 -0700 | [diff] [blame] | 238 | ASN_DEBUG("Got tag %s (%s), edx %zd", |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 239 | ber_tlv_tag_string(tlv_tag), td->name, edx); |
| 240 | } else if(specs->extensible == 0) { |
| 241 | ASN_DEBUG("Unexpected tag %s " |
| 242 | "in non-extensible SET %s", |
| 243 | ber_tlv_tag_string(tlv_tag), td->name); |
| 244 | RETURN(RC_FAIL); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 245 | } else { |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 246 | /* Skip this tag */ |
| 247 | ssize_t skip; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 248 | |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 249 | ASN_DEBUG("Skipping unknown tag %s", |
| 250 | ber_tlv_tag_string(tlv_tag)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 251 | |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 252 | skip = ber_skip_length(opt_codec_ctx, |
| 253 | BER_TLV_CONSTRUCTED(ptr), |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 254 | (const char *)ptr + tag_len, LEFT - tag_len); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 255 | |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 256 | switch(skip) { |
| 257 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 258 | /* Fall through */ |
| 259 | case -1: RETURN(RC_FAIL); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 260 | } |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 261 | |
| 262 | ADVANCE(skip + tag_len); |
| 263 | continue; /* Try again with the next tag */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /* |
| 267 | * MICROPHASE 2: Invoke the member-specific decoder. |
| 268 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 269 | microphase2: |
| 270 | |
| 271 | /* |
| 272 | * Check for duplications: must not overwrite |
| 273 | * already decoded elements. |
| 274 | */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 275 | if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, edx)) { |
Lev Walkin | f3f7f94 | 2017-09-26 22:45:46 -0700 | [diff] [blame] | 276 | ASN_DEBUG("SET %s: Duplicate element %s (%zd)", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 277 | td->name, elements[edx].name, edx); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 278 | RETURN(RC_FAIL); |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Compute the position of the member inside a structure, |
| 283 | * and also a type of containment (it may be contained |
| 284 | * as pointer or using inline inclusion). |
| 285 | */ |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 286 | if(elements[edx].flags & ATF_POINTER) { |
| 287 | /* Member is a pointer to another structure */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 288 | memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 289 | } else { |
| 290 | /* |
| 291 | * A pointer to a pointer |
| 292 | * holding the start of the structure |
| 293 | */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 294 | memb_ptr = (char *)st + elements[edx].memb_offset; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 295 | memb_ptr2 = &memb_ptr; |
| 296 | } |
| 297 | /* |
| 298 | * Invoke the member fetch routine according to member's type |
| 299 | */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 300 | rval = elements[edx].type->op->ber_decoder(opt_codec_ctx, |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 301 | elements[edx].type, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 302 | memb_ptr2, ptr, LEFT, |
| 303 | elements[edx].tag_mode); |
| 304 | switch(rval.code) { |
| 305 | case RC_OK: |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 306 | ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 307 | break; |
| 308 | case RC_WMORE: /* More data expected */ |
| 309 | if(!SIZE_VIOLATION) { |
| 310 | ADVANCE(rval.consumed); |
| 311 | RETURN(RC_WMORE); |
| 312 | } |
| 313 | /* Fail through */ |
| 314 | case RC_FAIL: /* Fatal error */ |
| 315 | RETURN(RC_FAIL); |
| 316 | } /* switch(rval) */ |
| 317 | |
| 318 | ADVANCE(rval.consumed); |
| 319 | } /* for(all structure members) */ |
| 320 | |
| 321 | phase3: |
| 322 | ctx->phase = 3; |
| 323 | /* Fall through */ |
| 324 | case 3: |
| 325 | case 4: /* Only 00 is expected */ |
| 326 | ASN_DEBUG("SET %s Leftover: %ld, size = %ld", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 327 | td->name, (long)ctx->left, (long)size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 328 | |
| 329 | /* |
| 330 | * Skip everything until the end of the SET. |
| 331 | */ |
| 332 | while(ctx->left) { |
| 333 | ssize_t tl, ll; |
| 334 | |
| 335 | tl = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 336 | switch(tl) { |
| 337 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 338 | /* Fall through */ |
| 339 | case -1: RETURN(RC_FAIL); |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * If expected <0><0>... |
| 344 | */ |
| 345 | if(ctx->left < 0 |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 346 | && ((const uint8_t *)ptr)[0] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 347 | if(LEFT < 2) { |
| 348 | if(SIZE_VIOLATION) |
| 349 | RETURN(RC_FAIL); |
| 350 | else |
| 351 | RETURN(RC_WMORE); |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 352 | } else if(((const uint8_t *)ptr)[1] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 353 | /* |
| 354 | * Correctly finished with <0><0>. |
| 355 | */ |
| 356 | ADVANCE(2); |
| 357 | ctx->left++; |
| 358 | ctx->phase = 4; |
| 359 | continue; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | if(specs->extensible == 0 || ctx->phase == 4) { |
| 364 | ASN_DEBUG("Unexpected continuation " |
Lev Walkin | 3e478ed | 2004-10-28 13:04:02 +0000 | [diff] [blame] | 365 | "of a non-extensible type %s " |
| 366 | "(ptr=%02x)", |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 367 | td->name, *(const uint8_t *)ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 368 | RETURN(RC_FAIL); |
| 369 | } |
| 370 | |
Lev Walkin | baaa24f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 371 | ll = ber_skip_length(opt_codec_ctx, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 372 | BER_TLV_CONSTRUCTED(ptr), |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 373 | (const char *)ptr + tl, LEFT - tl); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 374 | switch(ll) { |
| 375 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 376 | /* Fall through */ |
| 377 | case -1: RETURN(RC_FAIL); |
| 378 | } |
| 379 | |
| 380 | ADVANCE(tl + ll); |
| 381 | } |
| 382 | |
| 383 | ctx->phase = 5; |
| 384 | case 5: |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 385 | /* Check that all mandatory elements are present. */ |
| 386 | if(!_SET_is_populated(td, st)) |
| 387 | RETURN(RC_FAIL); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 388 | |
| 389 | NEXT_PHASE(ctx); |
| 390 | } |
| 391 | |
| 392 | RETURN(RC_OK); |
| 393 | } |
| 394 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 395 | static int |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 396 | _SET_is_populated(const asn_TYPE_descriptor_t *td, const void *st) { |
| 397 | const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 398 | size_t edx; |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 399 | |
| 400 | /* |
| 401 | * Check that all mandatory elements are present. |
| 402 | */ |
| 403 | for(edx = 0; edx < td->elements_count; |
| 404 | edx += (8 * sizeof(specs->_mandatory_elements[0]))) { |
| 405 | unsigned int midx, pres, must; |
| 406 | |
| 407 | midx = edx/(8 * sizeof(specs->_mandatory_elements[0])); |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 408 | pres = ((const unsigned int *)((const char *)st |
| 409 | + specs->pres_offset))[midx]; |
| 410 | must = sys_ntohl(specs->_mandatory_elements[midx]); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 411 | |
| 412 | if((pres & must) == must) { |
| 413 | /* |
| 414 | * Yes, everything seems to be in place. |
| 415 | */ |
| 416 | } else { |
| 417 | ASN_DEBUG("One or more mandatory elements " |
| 418 | "of a SET %s %d (%08x.%08x)=%08x " |
| 419 | "are not present", |
| 420 | td->name, |
| 421 | midx, |
| 422 | pres, |
| 423 | must, |
| 424 | (~(pres & must) & must) |
| 425 | ); |
| 426 | return 0; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | return 1; |
| 431 | } |
| 432 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 433 | /* |
| 434 | * The DER encoder of the SET type. |
| 435 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 436 | asn_enc_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 437 | SET_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr, int tag_mode, |
| 438 | ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) { |
| 439 | const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 440 | size_t computed_size = 0; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 441 | asn_enc_rval_t er; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 442 | int t2m_build_own = (specs->tag2el_count != td->elements_count); |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 443 | const asn_TYPE_tag2member_t *t2m; |
| 444 | asn_TYPE_tag2member_t *t2m_build; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 445 | size_t t2m_count; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 446 | ssize_t ret; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 447 | size_t edx; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * Use existing, or build our own tags map. |
| 451 | */ |
| 452 | if(t2m_build_own) { |
Lev Walkin | e2bbbf8 | 2017-10-08 18:52:37 -0700 | [diff] [blame] | 453 | t2m_build = (asn_TYPE_tag2member_t *)CALLOC(td->elements_count, |
| 454 | sizeof(t2m_build[0])); |
| 455 | if(!t2m_build) ASN__ENCODE_FAILED; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 456 | t2m_count = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 457 | } else { |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 458 | t2m_build = NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 459 | /* |
| 460 | * There is no untagged CHOICE in this SET. |
| 461 | * Employ existing table. |
| 462 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* |
| 466 | * Gather the length of the underlying members sequence. |
| 467 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 468 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 469 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 470 | asn_enc_rval_t tmper; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 471 | const void *memb_ptr_dontuse; /* Pointer to the member */ |
| 472 | const void *const *memb_ptr2; /* Pointer to that pointer */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 473 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 474 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 475 | * Compute the length of the encoding of this member. |
| 476 | */ |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 477 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 478 | memb_ptr2 = |
| 479 | (const void *const *)((const char *)sptr + elm->memb_offset); |
| 480 | if(!*memb_ptr2) { |
Lev Walkin | e2bbbf8 | 2017-10-08 18:52:37 -0700 | [diff] [blame] | 481 | if(!elm->optional) { |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 482 | /* Mandatory elements missing */ |
Lev Walkin | e2bbbf8 | 2017-10-08 18:52:37 -0700 | [diff] [blame] | 483 | FREEMEM(t2m_build); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 484 | ASN__ENCODE_FAILED; |
Lev Walkin | e2bbbf8 | 2017-10-08 18:52:37 -0700 | [diff] [blame] | 485 | } |
| 486 | if(t2m_build) { |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 487 | t2m_build[t2m_count].el_no = edx; |
| 488 | t2m_build[t2m_count].el_tag = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 489 | t2m_count++; |
| 490 | } |
| 491 | continue; |
| 492 | } |
| 493 | } else { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 494 | memb_ptr_dontuse = |
| 495 | (const void *)((const char *)sptr + elm->memb_offset); |
| 496 | memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 497 | } |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 498 | |
| 499 | /* Eliminate default values */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 500 | if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0) { |
| 501 | if(t2m_build) { |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 502 | t2m_build[t2m_count].el_no = edx; |
| 503 | t2m_build[t2m_count].el_tag = 0; |
| 504 | t2m_count++; |
| 505 | } |
| 506 | continue; |
| 507 | } |
| 508 | |
| 509 | tmper = elm->type->op->der_encoder(elm->type, *memb_ptr2, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 510 | elm->tag_mode, elm->tag, |
| 511 | 0, 0); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 512 | if(tmper.encoded == -1) |
| 513 | return tmper; |
| 514 | computed_size += tmper.encoded; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 515 | |
| 516 | /* |
| 517 | * Remember the outmost tag of this member. |
| 518 | */ |
Lev Walkin | e2bbbf8 | 2017-10-08 18:52:37 -0700 | [diff] [blame] | 519 | if(t2m_build) { |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 520 | t2m_build[t2m_count].el_no = edx; |
| 521 | t2m_build[t2m_count].el_tag = asn_TYPE_outmost_tag( |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 522 | elm->type, *memb_ptr2, elm->tag_mode, elm->tag); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 523 | t2m_count++; |
| 524 | } else { |
| 525 | /* |
| 526 | * No dynamic sorting is necessary. |
| 527 | */ |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Finalize order of the components. |
| 533 | */ |
Lev Walkin | e2bbbf8 | 2017-10-08 18:52:37 -0700 | [diff] [blame] | 534 | if(t2m_build) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 535 | /* |
| 536 | * Sort the underlying members according to their |
| 537 | * canonical tags order. DER encoding mandates it. |
| 538 | */ |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 539 | qsort(t2m_build, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp); |
| 540 | t2m = t2m_build; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 541 | } else { |
| 542 | /* |
| 543 | * Tags are already sorted by the compiler. |
| 544 | */ |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 545 | t2m = specs->tag2el; |
| 546 | t2m_count = specs->tag2el_count; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 547 | } |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 548 | assert(t2m_count == td->elements_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 549 | |
| 550 | /* |
| 551 | * Encode the TLV for the sequence itself. |
| 552 | */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 553 | ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key); |
Lev Walkin | e2bbbf8 | 2017-10-08 18:52:37 -0700 | [diff] [blame] | 554 | if(ret == -1) { |
| 555 | FREEMEM(t2m_build); |
| 556 | ASN__ENCODE_FAILED; |
| 557 | } |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 558 | er.encoded = computed_size + ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 559 | |
Lev Walkin | e2bbbf8 | 2017-10-08 18:52:37 -0700 | [diff] [blame] | 560 | if(!cb) { |
| 561 | FREEMEM(t2m_build); |
| 562 | ASN__ENCODED_OK(er); |
| 563 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 564 | |
| 565 | /* |
| 566 | * Encode all members. |
| 567 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 568 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 569 | asn_TYPE_member_t *elm; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 570 | asn_enc_rval_t tmper; |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 571 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 572 | const void *memb_ptr_dontuse; /* Pointer to the member */ |
| 573 | const void *const *memb_ptr2; /* Pointer to that pointer */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 574 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 575 | /* Encode according to the tag order */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 576 | elm = &td->elements[t2m[edx].el_no]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 577 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 578 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 579 | memb_ptr2 = |
| 580 | (const void *const *)((const char *)sptr + elm->memb_offset); |
| 581 | if(!*memb_ptr2) continue; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 582 | } else { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 583 | memb_ptr_dontuse = |
| 584 | (const void *)((const char *)sptr + elm->memb_offset); |
| 585 | memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 586 | } |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 587 | |
| 588 | /* Eliminate default values */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 589 | if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0) |
| 590 | continue; |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 591 | |
| 592 | tmper = elm->type->op->der_encoder(elm->type, *memb_ptr2, |
| 593 | elm->tag_mode, elm->tag, cb, app_key); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 594 | if(tmper.encoded == -1) |
| 595 | return tmper; |
| 596 | computed_size -= tmper.encoded; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | if(computed_size != 0) { |
| 600 | /* |
| 601 | * Encoded size is not equal to the computed size. |
| 602 | */ |
Lev Walkin | e2bbbf8 | 2017-10-08 18:52:37 -0700 | [diff] [blame] | 603 | FREEMEM(t2m_build); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 604 | ASN__ENCODE_FAILED; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Lev Walkin | e2bbbf8 | 2017-10-08 18:52:37 -0700 | [diff] [blame] | 607 | FREEMEM(t2m_build); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 608 | ASN__ENCODED_OK(er); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 611 | #undef XER_ADVANCE |
| 612 | #define XER_ADVANCE(num_bytes) do { \ |
| 613 | size_t num = num_bytes; \ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 614 | buf_ptr = ((const char *)buf_ptr) + num;\ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 615 | size -= num; \ |
| 616 | consumed_myself += num; \ |
| 617 | } while(0) |
| 618 | |
| 619 | /* |
| 620 | * Decode the XER (XML) data. |
| 621 | */ |
| 622 | asn_dec_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 623 | SET_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, |
| 624 | const asn_TYPE_descriptor_t *td, void **struct_ptr, |
| 625 | const char *opt_mname, const void *buf_ptr, size_t size) { |
| 626 | /* |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 627 | * Bring closer parts of structure description. |
| 628 | */ |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 629 | const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 630 | const asn_TYPE_member_t *elements = td->elements; |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 631 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 632 | |
| 633 | /* |
| 634 | * ... and parts of the structure being constructed. |
| 635 | */ |
| 636 | void *st = *struct_ptr; /* Target structure. */ |
| 637 | asn_struct_ctx_t *ctx; /* Decoder context */ |
| 638 | |
| 639 | asn_dec_rval_t rval; /* Return value from a decoder */ |
| 640 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 641 | size_t edx; /* Element index */ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 642 | |
| 643 | /* |
| 644 | * Create the target structure if it is not present already. |
| 645 | */ |
| 646 | if(st == 0) { |
| 647 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 648 | if(st == 0) RETURN(RC_FAIL); |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Restore parsing context. |
| 653 | */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 654 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 655 | |
| 656 | /* |
| 657 | * Phases of XER/XML processing: |
| 658 | * Phase 0: Check that the opening tag matches our expectations. |
| 659 | * Phase 1: Processing body and reacting on closing tag. |
| 660 | * Phase 2: Processing inner type. |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 661 | * Phase 3: Skipping unknown extensions. |
| 662 | * Phase 4: PHASED OUT |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 663 | */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 664 | for(edx = ctx->step; ctx->phase <= 3;) { |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 665 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 666 | ssize_t ch_size; /* Chunk size */ |
| 667 | xer_check_tag_e tcv; /* Tag check value */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 668 | const asn_TYPE_member_t *elm; |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 669 | |
| 670 | /* |
| 671 | * Go inside the inner member of a set. |
| 672 | */ |
| 673 | if(ctx->phase == 2) { |
| 674 | asn_dec_rval_t tmprval; |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 675 | void *memb_ptr_dontuse; /* Pointer to the member */ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 676 | void **memb_ptr2; /* Pointer to that pointer */ |
| 677 | |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 678 | if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 679 | edx)) { |
Lev Walkin | f3f7f94 | 2017-09-26 22:45:46 -0700 | [diff] [blame] | 680 | ASN_DEBUG("SET %s: Duplicate element %s (%zd)", |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 681 | td->name, elements[edx].name, edx); |
| 682 | RETURN(RC_FAIL); |
| 683 | } |
| 684 | |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 685 | elm = &elements[edx]; |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 686 | |
| 687 | if(elm->flags & ATF_POINTER) { |
| 688 | /* Member is a pointer to another structure */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 689 | memb_ptr2 = (void **)((char *)st + elm->memb_offset); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 690 | } else { |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 691 | memb_ptr_dontuse = (char *)st + elm->memb_offset; |
| 692 | memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /* Invoke the inner type decoder, m.b. multiple times */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 696 | tmprval = elm->type->op->xer_decoder(opt_codec_ctx, |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 697 | elm->type, memb_ptr2, elm->name, |
| 698 | buf_ptr, size); |
| 699 | XER_ADVANCE(tmprval.consumed); |
| 700 | if(tmprval.code != RC_OK) |
| 701 | RETURN(tmprval.code); |
| 702 | ctx->phase = 1; /* Back to body processing */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 703 | ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 704 | ASN_DEBUG("XER/SET phase => %d", ctx->phase); |
| 705 | /* Fall through */ |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * Get the next part of the XML stream. |
| 710 | */ |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 711 | ch_size = xer_next_token(&ctx->context, |
| 712 | buf_ptr, size, &ch_type); |
Lev Walkin | 9736348 | 2016-01-24 19:23:02 -0800 | [diff] [blame] | 713 | if(ch_size == -1) { |
Ryan Sleevi | ae8a6e4 | 2017-08-16 15:28:58 -0400 | [diff] [blame] | 714 | RETURN(RC_FAIL); |
| 715 | } else { |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 716 | switch(ch_type) { |
Lev Walkin | 9736348 | 2016-01-24 19:23:02 -0800 | [diff] [blame] | 717 | case PXER_WMORE: |
| 718 | RETURN(RC_WMORE); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 719 | case PXER_COMMENT: /* Got XML comment */ |
| 720 | case PXER_TEXT: /* Ignore free-standing text */ |
| 721 | XER_ADVANCE(ch_size); /* Skip silently */ |
| 722 | continue; |
| 723 | case PXER_TAG: |
| 724 | break; /* Check the rest down there */ |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | tcv = xer_check_tag(buf_ptr, ch_size, xml_tag); |
| 729 | ASN_DEBUG("XER/SET: tcv = %d, ph=%d", tcv, ctx->phase); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 730 | |
| 731 | /* Skip the extensions section */ |
| 732 | if(ctx->phase == 3) { |
| 733 | switch(xer_skip_unknown(tcv, &ctx->left)) { |
| 734 | case -1: |
| 735 | ctx->phase = 4; |
| 736 | RETURN(RC_FAIL); |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 737 | case 1: |
| 738 | ctx->phase = 1; |
| 739 | /* Fall through */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 740 | case 0: |
| 741 | XER_ADVANCE(ch_size); |
| 742 | continue; |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 743 | case 2: |
| 744 | ctx->phase = 1; |
| 745 | break; |
| 746 | } |
| 747 | } |
| 748 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 749 | switch(tcv) { |
| 750 | case XCT_CLOSING: |
| 751 | if(ctx->phase == 0) break; |
| 752 | ctx->phase = 0; |
| 753 | /* Fall through */ |
| 754 | case XCT_BOTH: |
| 755 | if(ctx->phase == 0) { |
| 756 | if(_SET_is_populated(td, st)) { |
| 757 | XER_ADVANCE(ch_size); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 758 | ctx->phase = 4; /* Phase out */ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 759 | RETURN(RC_OK); |
| 760 | } else { |
| 761 | ASN_DEBUG("Premature end of XER SET"); |
| 762 | RETURN(RC_FAIL); |
| 763 | } |
| 764 | } |
| 765 | /* Fall through */ |
| 766 | case XCT_OPENING: |
| 767 | if(ctx->phase == 0) { |
| 768 | XER_ADVANCE(ch_size); |
| 769 | ctx->phase = 1; /* Processing body phase */ |
| 770 | continue; |
| 771 | } |
| 772 | /* Fall through */ |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 773 | case XCT_UNKNOWN_OP: |
| 774 | case XCT_UNKNOWN_BO: |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 775 | |
| 776 | ASN_DEBUG("XER/SET: tcv=%d, ph=%d", tcv, ctx->phase); |
| 777 | if(ctx->phase != 1) |
| 778 | break; /* Really unexpected */ |
| 779 | |
| 780 | /* |
| 781 | * Search which member corresponds to this tag. |
| 782 | */ |
| 783 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 784 | switch(xer_check_tag(buf_ptr, ch_size, |
| 785 | elements[edx].name)) { |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 786 | case XCT_BOTH: |
| 787 | case XCT_OPENING: |
| 788 | /* |
| 789 | * Process this member. |
| 790 | */ |
| 791 | ctx->step = edx; |
| 792 | ctx->phase = 2; |
| 793 | break; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 794 | case XCT_UNKNOWN_OP: |
| 795 | case XCT_UNKNOWN_BO: |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 796 | continue; |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 797 | default: |
| 798 | edx = td->elements_count; |
| 799 | break; /* Phase out */ |
| 800 | } |
| 801 | break; |
| 802 | } |
| 803 | if(edx != td->elements_count) |
| 804 | continue; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 805 | |
| 806 | /* It is expected extension */ |
| 807 | if(specs->extensible) { |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 808 | ASN_DEBUG("Got anticipated extension"); |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 809 | /* |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 810 | * Check for (XCT_BOTH or XCT_UNKNOWN_BO) |
| 811 | * By using a mask. Only record a pure |
| 812 | * <opening> tags. |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 813 | */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 814 | if(tcv & XCT_CLOSING) { |
| 815 | /* Found </extension> without body */ |
| 816 | } else { |
| 817 | ctx->left = 1; |
| 818 | ctx->phase = 3; /* Skip ...'s */ |
| 819 | } |
| 820 | XER_ADVANCE(ch_size); |
| 821 | continue; |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 824 | /* Fall through */ |
| 825 | default: |
| 826 | break; |
| 827 | } |
| 828 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 829 | ASN_DEBUG("Unexpected XML tag in SET, expected \"%s\"", |
| 830 | xml_tag); |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 831 | break; |
| 832 | } |
| 833 | |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 834 | ctx->phase = 4; /* "Phase out" on hard failure */ |
Lev Walkin | 6c0df20 | 2005-02-14 19:03:17 +0000 | [diff] [blame] | 835 | RETURN(RC_FAIL); |
| 836 | } |
| 837 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 838 | asn_enc_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 839 | SET_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 840 | enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb, |
| 841 | void *app_key) { |
| 842 | const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 843 | asn_enc_rval_t er; |
| 844 | int xcan = (flags & XER_F_CANONICAL); |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 845 | const asn_TYPE_tag2member_t *t2m = specs->tag2el_cxer; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 846 | size_t t2m_count = specs->tag2el_cxer_count; |
| 847 | size_t edx; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 848 | |
| 849 | if(!sptr) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 850 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 851 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 852 | assert(t2m_count == td->elements_count); |
| 853 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 854 | er.encoded = 0; |
| 855 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 856 | for(edx = 0; edx < t2m_count; edx++) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 857 | asn_enc_rval_t tmper; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 858 | asn_TYPE_member_t *elm; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 859 | const void *memb_ptr; |
| 860 | const char *mname; |
| 861 | size_t mlen; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 862 | |
| 863 | elm = &td->elements[t2m[edx].el_no]; |
| 864 | mname = elm->name; |
| 865 | mlen = strlen(elm->name); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 866 | |
| 867 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 868 | memb_ptr = |
| 869 | *(const void *const *)((const char *)sptr + elm->memb_offset); |
| 870 | if(!memb_ptr) { |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 871 | if(elm->optional) |
| 872 | continue; |
| 873 | /* Mandatory element missing */ |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 874 | ASN__ENCODE_FAILED; |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 875 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 876 | } else { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 877 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
| 878 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 879 | |
| 880 | if(!xcan) |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 881 | ASN__TEXT_INDENT(1, ilevel); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 882 | ASN__CALLBACK3("<", 1, mname, mlen, ">", 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 883 | |
| 884 | /* Print the member itself */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 885 | tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 886 | ilevel + 1, flags, cb, app_key); |
| 887 | if(tmper.encoded == -1) return tmper; |
Lev Walkin | 4fe2882 | 2017-09-18 02:57:34 -0700 | [diff] [blame] | 888 | er.encoded += tmper.encoded; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 889 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 890 | ASN__CALLBACK3("</", 2, mname, mlen, ">", 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 893 | if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 894 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 895 | ASN__ENCODED_OK(er); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 896 | cb_failed: |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 897 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 900 | int |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 901 | SET_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 902 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 903 | size_t edx; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 904 | int ret; |
| 905 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 906 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 907 | |
| 908 | /* Dump preamble */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 909 | if(cb(td->name, strlen(td->name), app_key) < 0 |
| 910 | || cb(" ::= {", 6, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 911 | return -1; |
| 912 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 913 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 914 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 915 | const void *memb_ptr; |
| 916 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 917 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 918 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | ac58933 | 2005-08-22 14:19:28 +0000 | [diff] [blame] | 919 | if(!memb_ptr) { |
| 920 | if(elm->optional) continue; |
| 921 | /* Print <absent> line */ |
| 922 | /* Fall through */ |
| 923 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 924 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 925 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 928 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 929 | |
| 930 | /* Print the member's name and stuff */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 931 | if(cb(elm->name, strlen(elm->name), app_key) < 0 |
| 932 | || cb(": ", 2, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 933 | return -1; |
| 934 | |
| 935 | /* Print the member itself */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 936 | ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 937 | cb, app_key); |
| 938 | if(ret) return ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 941 | ilevel--; |
| 942 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 943 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 944 | return (cb("}", 1, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | void |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 948 | SET_free(const asn_TYPE_descriptor_t *td, void *ptr, |
| 949 | enum asn_struct_free_method method) { |
| 950 | size_t edx; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 951 | |
| 952 | if(!td || !ptr) |
| 953 | return; |
| 954 | |
| 955 | ASN_DEBUG("Freeing %s as SET", td->name); |
| 956 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 957 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 958 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 959 | void *memb_ptr; |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 960 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 961 | memb_ptr = *(void **)((char *)ptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 962 | if(memb_ptr) |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 963 | ASN_STRUCT_FREE(*elm->type, memb_ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 964 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 965 | memb_ptr = (void *)((char *)ptr + elm->memb_offset); |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 966 | ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 970 | switch(method) { |
| 971 | case ASFM_FREE_EVERYTHING: |
| 972 | FREEMEM(ptr); |
| 973 | break; |
| 974 | case ASFM_FREE_UNDERLYING: |
| 975 | break; |
| 976 | case ASFM_FREE_UNDERLYING_AND_RESET: |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 977 | memset(ptr, 0, |
| 978 | ((const asn_SET_specifics_t *)(td->specifics))->struct_size); |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 979 | break; |
| 980 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | int |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 984 | SET_constraint(const asn_TYPE_descriptor_t *td, const void *sptr, |
| 985 | asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
| 986 | size_t edx; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 987 | |
| 988 | if(!sptr) { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 989 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 990 | "%s: value not given (%s:%d)", |
| 991 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 992 | return -1; |
| 993 | } |
| 994 | |
| 995 | /* |
| 996 | * Iterate over structure members and check their validity. |
| 997 | */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 998 | for(edx = 0; edx < td->elements_count; edx++) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 999 | asn_TYPE_member_t *elm = &td->elements[edx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1000 | const void *memb_ptr; |
| 1001 | |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 1002 | if(elm->flags & ATF_POINTER) { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1003 | memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1004 | if(!memb_ptr) { |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 1005 | if(elm->optional) |
| 1006 | continue; |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 1007 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 1008 | "%s: mandatory element %s absent (%s:%d)", |
| 1009 | td->name, elm->name, __FILE__, __LINE__); |
| 1010 | return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } else { |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 1013 | memb_ptr = (const void *)((const char *)sptr + elm->memb_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1016 | if(elm->encoding_constraints.general_constraints) { |
| 1017 | return elm->encoding_constraints.general_constraints( |
| 1018 | elm->type, memb_ptr, ctfailcb, app_key); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 1019 | } else { |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1020 | return elm->type->encoding_constraints.general_constraints( |
| 1021 | elm->type, memb_ptr, ctfailcb, app_key); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 1022 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | return 0; |
| 1026 | } |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 1027 | |
| 1028 | int |
| 1029 | SET_compare(const asn_TYPE_descriptor_t *td, const void *aptr, |
| 1030 | const void *bptr) { |
| 1031 | size_t edx; |
| 1032 | |
| 1033 | for(edx = 0; edx < td->elements_count; edx++) { |
| 1034 | asn_TYPE_member_t *elm = &td->elements[edx]; |
| 1035 | const void *amemb; |
| 1036 | const void *bmemb; |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1037 | int ret; |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 1038 | |
| 1039 | if(elm->flags & ATF_POINTER) { |
| 1040 | amemb = |
| 1041 | *(const void *const *)((const char *)aptr + elm->memb_offset); |
| 1042 | bmemb = |
| 1043 | *(const void *const *)((const char *)bptr + elm->memb_offset); |
| 1044 | if(!amemb) { |
| 1045 | if(!bmemb) continue; |
| 1046 | return -1; |
| 1047 | } else if(!bmemb) { |
| 1048 | return 1; |
| 1049 | } |
| 1050 | } else { |
| 1051 | amemb = (const void *)((const char *)aptr + elm->memb_offset); |
| 1052 | bmemb = (const void *)((const char *)bptr + elm->memb_offset); |
| 1053 | } |
| 1054 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1055 | ret = elm->type->op->compare_struct(elm->type, amemb, bmemb); |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 1056 | if(ret != 0) return ret; |
| 1057 | } |
| 1058 | |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1062 | |
| 1063 | asn_TYPE_operation_t asn_OP_SET = { |
| 1064 | SET_free, |
| 1065 | SET_print, |
| 1066 | SET_compare, |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1067 | SET_decode_ber, |
| 1068 | SET_encode_der, |
| 1069 | SET_decode_xer, |
| 1070 | SET_encode_xer, |
Lev Walkin | ca0d40b | 2017-08-24 13:38:05 -0700 | [diff] [blame] | 1071 | 0, /* SET_decode_oer */ |
| 1072 | 0, /* SET_encode_oer */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1073 | 0, /* SET_decode_uper */ |
| 1074 | 0, /* SET_encode_uper */ |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1075 | SET_random_fill, |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 1076 | 0 /* Use generic outmost tag fetcher */ |
| 1077 | }; |
| 1078 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1079 | |
| 1080 | asn_random_fill_result_t |
| 1081 | SET_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, |
| 1082 | const asn_encoding_constraints_t *constr, |
| 1083 | size_t max_length) { |
| 1084 | const asn_SET_specifics_t *specs = |
| 1085 | (const asn_SET_specifics_t *)td->specifics; |
| 1086 | asn_random_fill_result_t result_ok = {ARFILL_OK, 0}; |
| 1087 | asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0}; |
| 1088 | asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0}; |
| 1089 | void *st = *sptr; |
| 1090 | size_t edx; |
| 1091 | |
| 1092 | if(max_length == 0) return result_skipped; |
| 1093 | |
| 1094 | (void)constr; |
| 1095 | |
| 1096 | if(st == NULL) { |
| 1097 | st = CALLOC(1, specs->struct_size); |
| 1098 | if(st == NULL) { |
| 1099 | return result_failed; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | for(edx = 0; edx < td->elements_count; edx++) { |
| 1104 | const asn_TYPE_member_t *elm = &td->elements[edx]; |
| 1105 | void *memb_ptr; /* Pointer to the member */ |
| 1106 | void **memb_ptr2; /* Pointer to that pointer */ |
| 1107 | asn_random_fill_result_t tmpres; |
| 1108 | |
| 1109 | if(elm->optional && asn_random_between(0, 4) == 2) { |
| 1110 | /* Sometimes decide not to fill the optional value */ |
| 1111 | continue; |
| 1112 | } |
| 1113 | |
| 1114 | if(elm->flags & ATF_POINTER) { |
| 1115 | /* Member is a pointer to another structure */ |
| 1116 | memb_ptr2 = (void **)((char *)st + elm->memb_offset); |
| 1117 | } else { |
| 1118 | memb_ptr = (char *)st + elm->memb_offset; |
| 1119 | memb_ptr2 = &memb_ptr; |
| 1120 | } |
| 1121 | |
| 1122 | tmpres = elm->type->op->random_fill( |
| 1123 | elm->type, memb_ptr2, &elm->encoding_constraints, |
| 1124 | max_length > result_ok.length ? max_length - result_ok.length : 0); |
| 1125 | switch(tmpres.code) { |
| 1126 | case ARFILL_OK: |
| 1127 | result_ok.length += tmpres.length; |
| 1128 | continue; |
| 1129 | case ARFILL_SKIPPED: |
| 1130 | assert(!(elm->flags & ATF_POINTER) || *memb_ptr2 == NULL); |
| 1131 | continue; |
| 1132 | case ARFILL_FAILED: |
| 1133 | if(st == *sptr) { |
| 1134 | ASN_STRUCT_RESET(*td, st); |
| 1135 | } else { |
| 1136 | ASN_STRUCT_FREE(*td, st); |
| 1137 | } |
| 1138 | return tmpres; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | *sptr = st; |
| 1143 | |
| 1144 | return result_ok; |
| 1145 | } |
| 1146 | |