Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>. |
| 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_OF.h> |
| 8 | #include <asn_SET_OF.h> |
| 9 | |
| 10 | /* |
| 11 | * Number of bytes left for this structure. |
| 12 | * (ctx->left) indicates the number of bytes _transferred_ for the structure. |
| 13 | * (size) contains the number of bytes in the buffer passed. |
| 14 | */ |
Lev Walkin | ec1ffd4 | 2004-08-18 04:53:32 +0000 | [diff] [blame] | 15 | #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * If the subprocessor function returns with an indication that it wants |
| 19 | * more data, it may well be a fatal decoding problem, because the |
| 20 | * size is constrained by the <TLV>'s L, even if the buffer size allows |
| 21 | * reading more data. |
| 22 | * For example, consider the buffer containing the following TLVs: |
| 23 | * <T:5><L:1><V> <T:6>... |
| 24 | * The TLV length clearly indicates that one byte is expected in V, but |
| 25 | * if the V processor returns with "want more data" even if the buffer |
| 26 | * contains way more data than the V processor have seen. |
| 27 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 28 | #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * This macro "eats" the part of the buffer which is definitely "consumed", |
| 32 | * i.e. was correctly converted into local representation or rightfully skipped. |
| 33 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 34 | #undef ADVANCE |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 35 | #define ADVANCE(num_bytes) do { \ |
| 36 | size_t num = num_bytes; \ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 37 | ptr = ((const char *)ptr) + num;\ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | size -= num; \ |
| 39 | if(ctx->left >= 0) \ |
| 40 | ctx->left -= num; \ |
| 41 | consumed_myself += num; \ |
| 42 | } while(0) |
| 43 | |
| 44 | /* |
| 45 | * Switch to the next phase of parsing. |
| 46 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 47 | #undef NEXT_PHASE |
| 48 | #undef PHASE_OUT |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | #define NEXT_PHASE(ctx) do { \ |
| 50 | ctx->phase++; \ |
| 51 | ctx->step = 0; \ |
| 52 | } while(0) |
| 53 | #define PHASE_OUT(ctx) do { ctx->phase = 10; } 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 | * The decoder of the SET OF type. |
| 67 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 68 | asn_dec_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 69 | SET_OF_decode_ber(const asn_codec_ctx_t *opt_codec_ctx, |
| 70 | const asn_TYPE_descriptor_t *td, void **struct_ptr, |
| 71 | const void *ptr, size_t size, int tag_mode) { |
| 72 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 73 | * Bring closer parts of structure description. |
| 74 | */ |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 75 | const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 76 | const asn_TYPE_member_t *elm = td->elements; /* Single one */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 77 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 78 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 79 | * Parts of the structure being constructed. |
| 80 | */ |
| 81 | void *st = *struct_ptr; /* Target structure. */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 82 | asn_struct_ctx_t *ctx; /* Decoder context */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 83 | |
| 84 | ber_tlv_tag_t tlv_tag; /* T from TLV */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 85 | asn_dec_rval_t rval; /* Return code from subparsers */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 86 | |
| 87 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
| 88 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 89 | ASN_DEBUG("Decoding %s as SET OF", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * Create the target structure if it is not present already. |
| 93 | */ |
| 94 | if(st == 0) { |
| 95 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 96 | if(st == 0) { |
| 97 | RETURN(RC_FAIL); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Restore parsing context. |
| 103 | */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 104 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * Start to parse where left previously |
| 108 | */ |
| 109 | switch(ctx->phase) { |
| 110 | case 0: |
| 111 | /* |
| 112 | * PHASE 0. |
| 113 | * Check that the set of tags associated with given structure |
| 114 | * perfectly fits our expectations. |
| 115 | */ |
| 116 | |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 117 | rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 118 | tag_mode, 1, &ctx->left, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 119 | if(rval.code != RC_OK) { |
| 120 | ASN_DEBUG("%s tagging check failed: %d", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 121 | td->name, rval.code); |
Lev Walkin | 4ab42cd | 2004-10-05 06:36:44 +0000 | [diff] [blame] | 122 | return rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if(ctx->left >= 0) |
| 126 | ctx->left += rval.consumed; /* ?Substracted below! */ |
| 127 | ADVANCE(rval.consumed); |
| 128 | |
| 129 | ASN_DEBUG("Structure consumes %ld bytes, " |
| 130 | "buffer %ld", (long)ctx->left, (long)size); |
| 131 | |
| 132 | NEXT_PHASE(ctx); |
| 133 | /* Fall through */ |
| 134 | case 1: |
| 135 | /* |
| 136 | * PHASE 1. |
| 137 | * From the place where we've left it previously, |
| 138 | * try to decode the next item. |
| 139 | */ |
| 140 | for(;; ctx->step = 0) { |
| 141 | ssize_t tag_len; /* Length of TLV's T */ |
| 142 | |
| 143 | if(ctx->step & 1) |
| 144 | goto microphase2; |
| 145 | |
| 146 | /* |
| 147 | * MICROPHASE 1: Synchronize decoding. |
| 148 | */ |
| 149 | |
| 150 | if(ctx->left == 0) { |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 151 | ASN_DEBUG("End of SET OF %s", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 152 | /* |
| 153 | * No more things to decode. |
| 154 | * Exit out of here. |
| 155 | */ |
| 156 | PHASE_OUT(ctx); |
| 157 | RETURN(RC_OK); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Fetch the T from TLV. |
| 162 | */ |
| 163 | tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 164 | switch(tag_len) { |
| 165 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 166 | /* Fall through */ |
| 167 | case -1: RETURN(RC_FAIL); |
| 168 | } |
| 169 | |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 170 | if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 171 | if(LEFT < 2) { |
| 172 | if(SIZE_VIOLATION) |
| 173 | RETURN(RC_FAIL); |
| 174 | else |
| 175 | RETURN(RC_WMORE); |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 176 | } else if(((const uint8_t *)ptr)[1] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 177 | /* |
| 178 | * Found the terminator of the |
| 179 | * indefinite length structure. |
| 180 | */ |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* Outmost tag may be unknown and cannot be fetched/compared */ |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 186 | if(elm->tag != (ber_tlv_tag_t)-1) { |
| 187 | if(BER_TAGS_EQUAL(tlv_tag, elm->tag)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 188 | /* |
| 189 | * The new list member of expected type has arrived. |
| 190 | */ |
| 191 | } else { |
| 192 | ASN_DEBUG("Unexpected tag %s fixed SET OF %s", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 193 | ber_tlv_tag_string(tlv_tag), td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 194 | ASN_DEBUG("%s SET OF has tag %s", |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 195 | td->name, ber_tlv_tag_string(elm->tag)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 196 | RETURN(RC_FAIL); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * MICROPHASE 2: Invoke the member-specific decoder. |
| 202 | */ |
| 203 | ctx->step |= 1; /* Confirm entering next microphase */ |
| 204 | microphase2: |
| 205 | |
| 206 | /* |
| 207 | * Invoke the member fetch routine according to member's type |
| 208 | */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 209 | rval = elm->type->op->ber_decoder(opt_codec_ctx, |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 210 | elm->type, &ctx->ptr, ptr, LEFT, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 211 | ASN_DEBUG("In %s SET OF %s code %d consumed %d", |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 212 | td->name, elm->type->name, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 213 | rval.code, (int)rval.consumed); |
| 214 | switch(rval.code) { |
| 215 | case RC_OK: |
| 216 | { |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 217 | asn_anonymous_set_ *list = _A_SET_FROM_VOID(st); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 218 | if(ASN_SET_ADD(list, ctx->ptr) != 0) |
| 219 | RETURN(RC_FAIL); |
| 220 | else |
| 221 | ctx->ptr = 0; |
| 222 | } |
| 223 | break; |
| 224 | case RC_WMORE: /* More data expected */ |
| 225 | if(!SIZE_VIOLATION) { |
| 226 | ADVANCE(rval.consumed); |
| 227 | RETURN(RC_WMORE); |
| 228 | } |
| 229 | /* Fall through */ |
| 230 | case RC_FAIL: /* Fatal error */ |
Lev Walkin | 4f530b5 | 2006-10-19 05:27:54 +0000 | [diff] [blame] | 231 | ASN_STRUCT_FREE(*elm->type, ctx->ptr); |
| 232 | ctx->ptr = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 233 | RETURN(RC_FAIL); |
| 234 | } /* switch(rval) */ |
| 235 | |
| 236 | ADVANCE(rval.consumed); |
| 237 | } /* for(all list members) */ |
| 238 | |
| 239 | NEXT_PHASE(ctx); |
| 240 | case 2: |
| 241 | /* |
| 242 | * Read in all "end of content" TLVs. |
| 243 | */ |
| 244 | while(ctx->left < 0) { |
| 245 | if(LEFT < 2) { |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 246 | if(LEFT > 0 && ((const char *)ptr)[0] != 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 247 | /* Unexpected tag */ |
| 248 | RETURN(RC_FAIL); |
| 249 | } else { |
| 250 | RETURN(RC_WMORE); |
| 251 | } |
| 252 | } |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 253 | if(((const char *)ptr)[0] == 0 |
| 254 | && ((const char *)ptr)[1] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 255 | ADVANCE(2); |
| 256 | ctx->left++; |
| 257 | } else { |
| 258 | RETURN(RC_FAIL); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | PHASE_OUT(ctx); |
| 263 | } |
| 264 | |
| 265 | RETURN(RC_OK); |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Internally visible buffer holding a single encoded element. |
| 270 | */ |
| 271 | struct _el_buffer { |
| 272 | uint8_t *buf; |
| 273 | size_t length; |
| 274 | size_t size; |
| 275 | }; |
| 276 | /* Append bytes to the above structure */ |
| 277 | static int _el_addbytes(const void *buffer, size_t size, void *el_buf_ptr) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 278 | struct _el_buffer *el_buf = (struct _el_buffer *)el_buf_ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 279 | |
| 280 | if(el_buf->length + size > el_buf->size) |
| 281 | return -1; |
| 282 | |
| 283 | memcpy(el_buf->buf + el_buf->length, buffer, size); |
| 284 | |
| 285 | el_buf->length += size; |
| 286 | return 0; |
| 287 | } |
| 288 | static int _el_buf_cmp(const void *ap, const void *bp) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 289 | const struct _el_buffer *a = (const struct _el_buffer *)ap; |
| 290 | const struct _el_buffer *b = (const struct _el_buffer *)bp; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 291 | int ret; |
| 292 | size_t common_len; |
| 293 | |
| 294 | if(a->length < b->length) |
| 295 | common_len = a->length; |
| 296 | else |
| 297 | common_len = b->length; |
| 298 | |
| 299 | ret = memcmp(a->buf, b->buf, common_len); |
| 300 | if(ret == 0) { |
| 301 | if(a->length < b->length) |
| 302 | ret = -1; |
| 303 | else if(a->length > b->length) |
| 304 | ret = 1; |
| 305 | } |
| 306 | |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * The DER encoder of the SET OF type. |
| 312 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 313 | asn_enc_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 314 | SET_OF_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr, |
| 315 | int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, |
| 316 | void *app_key) { |
| 317 | const asn_TYPE_member_t *elm = td->elements; |
| 318 | const asn_TYPE_descriptor_t *elm_type = elm->type; |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 319 | der_type_encoder_f *der_encoder = elm_type->op->der_encoder; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 320 | const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr); |
| 321 | size_t computed_size = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 322 | ssize_t encoding_size = 0; |
| 323 | struct _el_buffer *encoded_els; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 324 | ssize_t eels_count = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 325 | size_t max_encoded_len = 1; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 326 | asn_enc_rval_t erval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 327 | int ret; |
| 328 | int edx; |
| 329 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 330 | ASN_DEBUG("Estimating size for SET OF %s", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 331 | |
| 332 | /* |
| 333 | * Gather the length of the underlying members sequence. |
| 334 | */ |
| 335 | for(edx = 0; edx < list->count; edx++) { |
| 336 | void *memb_ptr = list->array[edx]; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 337 | if(!memb_ptr) continue; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 338 | erval = der_encoder(elm_type, memb_ptr, 0, elm->tag, 0, 0); |
| 339 | if(erval.encoded == -1) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 340 | return erval; |
| 341 | computed_size += erval.encoded; |
| 342 | |
| 343 | /* Compute maximum encoding's size */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 344 | if(max_encoded_len < (size_t)erval.encoded) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 345 | max_encoded_len = erval.encoded; |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Encode the TLV for the sequence itself. |
| 350 | */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 351 | encoding_size = der_write_tags(td, computed_size, tag_mode, 1, tag, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 352 | cb, app_key); |
| 353 | if(encoding_size == -1) { |
| 354 | erval.encoded = -1; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 355 | erval.failed_type = td; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 356 | erval.structure_ptr = sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 357 | return erval; |
| 358 | } |
| 359 | computed_size += encoding_size; |
| 360 | |
Lev Walkin | 3d231c0 | 2006-03-16 22:20:47 +0000 | [diff] [blame] | 361 | if(!cb || list->count == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 362 | erval.encoded = computed_size; |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 363 | ASN__ENCODED_OK(erval); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /* |
| 367 | * DER mandates dynamic sorting of the SET OF elements |
| 368 | * according to their encodings. Build an array of the |
| 369 | * encoded elements. |
| 370 | */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 371 | encoded_els = |
| 372 | (struct _el_buffer *)MALLOC(list->count * sizeof(encoded_els[0])); |
| 373 | if(encoded_els == NULL) { |
| 374 | ASN__ENCODE_FAILED; |
| 375 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 376 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 377 | ASN_DEBUG("Encoding members of %s SET OF", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 378 | |
| 379 | /* |
| 380 | * Encode all members. |
| 381 | */ |
| 382 | for(edx = 0; edx < list->count; edx++) { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 383 | const void *memb_ptr = list->array[edx]; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 384 | struct _el_buffer *encoded_el = &encoded_els[eels_count]; |
| 385 | |
| 386 | if(!memb_ptr) continue; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 387 | |
| 388 | /* |
| 389 | * Prepare space for encoding. |
| 390 | */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 391 | encoded_el->buf = (uint8_t *)MALLOC(max_encoded_len); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 392 | if(encoded_el->buf) { |
| 393 | encoded_el->length = 0; |
| 394 | encoded_el->size = max_encoded_len; |
| 395 | } else { |
| 396 | for(edx--; edx >= 0; edx--) |
| 397 | FREEMEM(encoded_els[edx].buf); |
| 398 | FREEMEM(encoded_els); |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 399 | ASN__ENCODE_FAILED; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* |
| 403 | * Encode the member into the prepared space. |
| 404 | */ |
| 405 | erval = der_encoder(elm_type, memb_ptr, 0, elm->tag, |
| 406 | _el_addbytes, encoded_el); |
| 407 | if(erval.encoded == -1) { |
| 408 | for(; edx >= 0; edx--) |
| 409 | FREEMEM(encoded_els[edx].buf); |
| 410 | FREEMEM(encoded_els); |
| 411 | return erval; |
| 412 | } |
| 413 | encoding_size += erval.encoded; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 414 | eels_count++; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Sort the encoded elements according to their encoding. |
| 419 | */ |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 420 | qsort(encoded_els, eels_count, sizeof(encoded_els[0]), _el_buf_cmp); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 421 | |
| 422 | /* |
| 423 | * Report encoded elements to the application. |
| 424 | * Dispose of temporary sorted members table. |
| 425 | */ |
| 426 | ret = 0; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 427 | for(edx = 0; edx < eels_count; edx++) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 428 | struct _el_buffer *encoded_el = &encoded_els[edx]; |
| 429 | /* Report encoded chunks to the application */ |
| 430 | if(ret == 0 |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 431 | && cb(encoded_el->buf, encoded_el->length, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 432 | ret = -1; |
| 433 | FREEMEM(encoded_el->buf); |
| 434 | } |
| 435 | FREEMEM(encoded_els); |
| 436 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 437 | if(ret || computed_size != (size_t)encoding_size) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 438 | /* |
| 439 | * Standard callback failed, or |
| 440 | * encoded size is not equal to the computed size. |
| 441 | */ |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 442 | ASN__ENCODE_FAILED; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 443 | } else { |
| 444 | erval.encoded = computed_size; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 445 | ASN__ENCODED_OK(erval); |
| 446 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 449 | #undef XER_ADVANCE |
| 450 | #define XER_ADVANCE(num_bytes) do { \ |
| 451 | size_t num = num_bytes; \ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 452 | buf_ptr = ((const char *)buf_ptr) + num;\ |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 453 | size -= num; \ |
| 454 | consumed_myself += num; \ |
| 455 | } while(0) |
| 456 | |
| 457 | /* |
| 458 | * Decode the XER (XML) data. |
| 459 | */ |
| 460 | asn_dec_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 461 | SET_OF_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, |
| 462 | const asn_TYPE_descriptor_t *td, void **struct_ptr, |
| 463 | const char *opt_mname, const void *buf_ptr, size_t size) { |
| 464 | /* |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 465 | * Bring closer parts of structure description. |
| 466 | */ |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 467 | const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 468 | const asn_TYPE_member_t *element = td->elements; |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 469 | const char *elm_tag; |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 470 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 471 | |
| 472 | /* |
| 473 | * ... and parts of the structure being constructed. |
| 474 | */ |
| 475 | void *st = *struct_ptr; /* Target structure. */ |
| 476 | asn_struct_ctx_t *ctx; /* Decoder context */ |
| 477 | |
| 478 | asn_dec_rval_t rval; /* Return value from a decoder */ |
| 479 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * Create the target structure if it is not present already. |
| 483 | */ |
| 484 | if(st == 0) { |
| 485 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 486 | if(st == 0) RETURN(RC_FAIL); |
| 487 | } |
| 488 | |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 489 | /* Which tag is expected for the downstream */ |
| 490 | if(specs->as_XMLValueList) { |
| 491 | elm_tag = (specs->as_XMLValueList == 1) ? 0 : ""; |
| 492 | } else { |
| 493 | elm_tag = (*element->name) |
| 494 | ? element->name : element->type->xml_tag; |
| 495 | } |
| 496 | |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 497 | /* |
| 498 | * Restore parsing context. |
| 499 | */ |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 500 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 501 | |
| 502 | /* |
| 503 | * Phases of XER/XML processing: |
| 504 | * Phase 0: Check that the opening tag matches our expectations. |
| 505 | * Phase 1: Processing body and reacting on closing tag. |
| 506 | * Phase 2: Processing inner type. |
| 507 | */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 508 | for(; ctx->phase <= 2;) { |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 509 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 510 | ssize_t ch_size; /* Chunk size */ |
| 511 | xer_check_tag_e tcv; /* Tag check value */ |
| 512 | |
| 513 | /* |
| 514 | * Go inside the inner member of a set. |
| 515 | */ |
| 516 | if(ctx->phase == 2) { |
| 517 | asn_dec_rval_t tmprval; |
| 518 | |
| 519 | /* Invoke the inner type decoder, m.b. multiple times */ |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 520 | ASN_DEBUG("XER/SET OF element [%s]", elm_tag); |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 521 | tmprval = element->type->op->xer_decoder(opt_codec_ctx, |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 522 | element->type, &ctx->ptr, elm_tag, |
| 523 | buf_ptr, size); |
| 524 | if(tmprval.code == RC_OK) { |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 525 | asn_anonymous_set_ *list = _A_SET_FROM_VOID(st); |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 526 | if(ASN_SET_ADD(list, ctx->ptr) != 0) |
| 527 | RETURN(RC_FAIL); |
| 528 | ctx->ptr = 0; |
| 529 | XER_ADVANCE(tmprval.consumed); |
| 530 | } else { |
| 531 | XER_ADVANCE(tmprval.consumed); |
| 532 | RETURN(tmprval.code); |
| 533 | } |
| 534 | ctx->phase = 1; /* Back to body processing */ |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 535 | ASN_DEBUG("XER/SET OF phase => %d", ctx->phase); |
| 536 | /* Fall through */ |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Get the next part of the XML stream. |
| 541 | */ |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 542 | ch_size = xer_next_token(&ctx->context, |
| 543 | buf_ptr, size, &ch_type); |
Lev Walkin | 9736348 | 2016-01-24 19:23:02 -0800 | [diff] [blame] | 544 | if(ch_size == -1) { |
| 545 | RETURN(RC_FAIL); |
| 546 | } else { |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 547 | switch(ch_type) { |
Lev Walkin | 9736348 | 2016-01-24 19:23:02 -0800 | [diff] [blame] | 548 | case PXER_WMORE: |
| 549 | RETURN(RC_WMORE); |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 550 | case PXER_COMMENT: /* Got XML comment */ |
| 551 | case PXER_TEXT: /* Ignore free-standing text */ |
| 552 | XER_ADVANCE(ch_size); /* Skip silently */ |
| 553 | continue; |
| 554 | case PXER_TAG: |
| 555 | break; /* Check the rest down there */ |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | tcv = xer_check_tag(buf_ptr, ch_size, xml_tag); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 560 | ASN_DEBUG("XER/SET OF: tcv = %d, ph=%d t=%s", |
| 561 | tcv, ctx->phase, xml_tag); |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 562 | switch(tcv) { |
| 563 | case XCT_CLOSING: |
| 564 | if(ctx->phase == 0) break; |
| 565 | ctx->phase = 0; |
| 566 | /* Fall through */ |
| 567 | case XCT_BOTH: |
| 568 | if(ctx->phase == 0) { |
| 569 | /* No more things to decode */ |
| 570 | XER_ADVANCE(ch_size); |
| 571 | ctx->phase = 3; /* Phase out */ |
| 572 | RETURN(RC_OK); |
| 573 | } |
| 574 | /* Fall through */ |
| 575 | case XCT_OPENING: |
| 576 | if(ctx->phase == 0) { |
| 577 | XER_ADVANCE(ch_size); |
| 578 | ctx->phase = 1; /* Processing body phase */ |
| 579 | continue; |
| 580 | } |
| 581 | /* Fall through */ |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 582 | case XCT_UNKNOWN_OP: |
| 583 | case XCT_UNKNOWN_BO: |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 584 | |
| 585 | ASN_DEBUG("XER/SET OF: tcv=%d, ph=%d", tcv, ctx->phase); |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 586 | if(ctx->phase == 1) { |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 587 | /* |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 588 | * Process a single possible member. |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 589 | */ |
| 590 | ctx->phase = 2; |
| 591 | continue; |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 592 | } |
| 593 | /* Fall through */ |
| 594 | default: |
| 595 | break; |
| 596 | } |
| 597 | |
| 598 | ASN_DEBUG("Unexpected XML tag in SET OF"); |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | ctx->phase = 3; /* "Phase out" on hard failure */ |
| 603 | RETURN(RC_FAIL); |
| 604 | } |
| 605 | |
| 606 | |
| 607 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 608 | typedef struct xer_tmp_enc_s { |
| 609 | void *buffer; |
| 610 | size_t offset; |
| 611 | size_t size; |
| 612 | } xer_tmp_enc_t; |
| 613 | static int |
| 614 | SET_OF_encode_xer_callback(const void *buffer, size_t size, void *key) { |
| 615 | xer_tmp_enc_t *t = (xer_tmp_enc_t *)key; |
| 616 | if(t->offset + size >= t->size) { |
| 617 | size_t newsize = (t->size << 2) + size; |
| 618 | void *p = REALLOC(t->buffer, newsize); |
| 619 | if(!p) return -1; |
| 620 | t->buffer = p; |
| 621 | t->size = newsize; |
| 622 | } |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 623 | memcpy((char *)t->buffer + t->offset, buffer, size); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 624 | t->offset += size; |
| 625 | return 0; |
| 626 | } |
| 627 | static int |
| 628 | SET_OF_xer_order(const void *aptr, const void *bptr) { |
| 629 | const xer_tmp_enc_t *a = (const xer_tmp_enc_t *)aptr; |
| 630 | const xer_tmp_enc_t *b = (const xer_tmp_enc_t *)bptr; |
| 631 | size_t minlen = a->offset; |
| 632 | int ret; |
| 633 | if(b->offset < minlen) minlen = b->offset; |
| 634 | /* Well-formed UTF-8 has this nice lexicographical property... */ |
| 635 | ret = memcmp(a->buffer, b->buffer, minlen); |
| 636 | if(ret != 0) return ret; |
| 637 | if(a->offset == b->offset) |
| 638 | return 0; |
| 639 | if(a->offset == minlen) |
| 640 | return -1; |
| 641 | return 1; |
| 642 | } |
| 643 | |
| 644 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 645 | asn_enc_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 646 | SET_OF_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 647 | enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb, |
| 648 | void *app_key) { |
| 649 | asn_enc_rval_t er; |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 650 | const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 651 | const asn_TYPE_member_t *elm = td->elements; |
| 652 | const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr); |
| 653 | const char *mname = specs->as_XMLValueList |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 654 | ? 0 : ((*elm->name) ? elm->name : elm->type->xml_tag); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 655 | size_t mlen = mname ? strlen(mname) : 0; |
| 656 | int xcan = (flags & XER_F_CANONICAL); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 657 | xer_tmp_enc_t *encs = 0; |
| 658 | size_t encs_count = 0; |
| 659 | void *original_app_key = app_key; |
| 660 | asn_app_consume_bytes_f *original_cb = cb; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 661 | int i; |
| 662 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 663 | if(!sptr) ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 664 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 665 | if(xcan) { |
| 666 | encs = (xer_tmp_enc_t *)MALLOC(list->count * sizeof(encs[0])); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 667 | if(!encs) ASN__ENCODE_FAILED; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 668 | cb = SET_OF_encode_xer_callback; |
| 669 | } |
| 670 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 671 | er.encoded = 0; |
| 672 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 673 | for(i = 0; i < list->count; i++) { |
| 674 | asn_enc_rval_t tmper; |
| 675 | |
| 676 | void *memb_ptr = list->array[i]; |
| 677 | if(!memb_ptr) continue; |
| 678 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 679 | if(encs) { |
| 680 | memset(&encs[encs_count], 0, sizeof(encs[0])); |
| 681 | app_key = &encs[encs_count]; |
| 682 | encs_count++; |
| 683 | } |
| 684 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 685 | if(mname) { |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 686 | if(!xcan) ASN__TEXT_INDENT(1, ilevel); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 687 | ASN__CALLBACK3("<", 1, mname, mlen, ">", 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 690 | if(!xcan && specs->as_XMLValueList == 1) |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 691 | ASN__TEXT_INDENT(1, ilevel + 1); |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 692 | tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 693 | ilevel + (specs->as_XMLValueList != 2), |
| 694 | flags, cb, app_key); |
Lev Walkin | 4fe2882 | 2017-09-18 02:57:34 -0700 | [diff] [blame] | 695 | if(tmper.encoded == -1) return tmper; |
| 696 | er.encoded += tmper.encoded; |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 697 | if(tmper.encoded == 0 && specs->as_XMLValueList) { |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 698 | const char *name = elm->type->xml_tag; |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 699 | size_t len = strlen(name); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 700 | ASN__CALLBACK3("<", 1, name, len, "/>", 2); |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 701 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 702 | |
| 703 | if(mname) { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 704 | ASN__CALLBACK3("</", 2, mname, mlen, ">", 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 709 | if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 710 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 711 | if(encs) { |
| 712 | xer_tmp_enc_t *enc = encs; |
| 713 | xer_tmp_enc_t *end = encs + encs_count; |
| 714 | ssize_t control_size = 0; |
| 715 | |
Lev Walkin | 4fe2882 | 2017-09-18 02:57:34 -0700 | [diff] [blame] | 716 | er.encoded = 0; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 717 | cb = original_cb; |
| 718 | app_key = original_app_key; |
| 719 | qsort(encs, encs_count, sizeof(encs[0]), SET_OF_xer_order); |
| 720 | |
| 721 | for(; enc < end; enc++) { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 722 | ASN__CALLBACK(enc->buffer, enc->offset); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 723 | FREEMEM(enc->buffer); |
| 724 | enc->buffer = 0; |
| 725 | control_size += enc->offset; |
| 726 | } |
| 727 | assert(control_size == er.encoded); |
| 728 | } |
| 729 | |
| 730 | goto cleanup; |
| 731 | cb_failed: |
Lev Walkin | 4fe2882 | 2017-09-18 02:57:34 -0700 | [diff] [blame] | 732 | ASN__ENCODE_FAILED; |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 733 | cleanup: |
| 734 | if(encs) { |
Lev Walkin | e835b3a | 2017-09-18 00:14:40 -0700 | [diff] [blame] | 735 | size_t n; |
| 736 | for(n = 0; n < encs_count; n++) { |
| 737 | FREEMEM(encs[n].buffer); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 738 | } |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 739 | FREEMEM(encs); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 740 | } |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 741 | ASN__ENCODED_OK(er); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 744 | int |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 745 | SET_OF_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 746 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 747 | asn_TYPE_member_t *elm = td->elements; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 748 | const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 749 | int ret; |
| 750 | int i; |
| 751 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 752 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 753 | |
| 754 | /* Dump preamble */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 755 | if(cb(td->name, strlen(td->name), app_key) < 0 |
| 756 | || cb(" ::= {", 6, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 757 | return -1; |
| 758 | |
| 759 | for(i = 0; i < list->count; i++) { |
| 760 | const void *memb_ptr = list->array[i]; |
| 761 | if(!memb_ptr) continue; |
| 762 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 763 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 764 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 765 | ret = elm->type->op->print_struct(elm->type, memb_ptr, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 766 | ilevel + 1, cb, app_key); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 767 | if(ret) return ret; |
| 768 | } |
| 769 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 770 | ilevel--; |
| 771 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 772 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 773 | return (cb("}", 1, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | void |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 777 | SET_OF_free(const asn_TYPE_descriptor_t *td, void *ptr, |
| 778 | enum asn_struct_free_method method) { |
| 779 | if(td && ptr) { |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 780 | const asn_SET_OF_specifics_t *specs; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 781 | asn_TYPE_member_t *elm = td->elements; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 782 | asn_anonymous_set_ *list = _A_SET_FROM_VOID(ptr); |
Lev Walkin | 4f530b5 | 2006-10-19 05:27:54 +0000 | [diff] [blame] | 783 | asn_struct_ctx_t *ctx; /* Decoder context */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 784 | int i; |
| 785 | |
| 786 | /* |
| 787 | * Could not use set_of_empty() because of (*free) |
| 788 | * incompatibility. |
| 789 | */ |
| 790 | for(i = 0; i < list->count; i++) { |
| 791 | void *memb_ptr = list->array[i]; |
| 792 | if(memb_ptr) |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 793 | ASN_STRUCT_FREE(*elm->type, memb_ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 794 | } |
Lev Walkin | 246a2af | 2004-07-15 10:51:26 +0000 | [diff] [blame] | 795 | list->count = 0; /* No meaningful elements left */ |
| 796 | |
| 797 | asn_set_empty(list); /* Remove (list->array) */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 798 | |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 799 | specs = (const asn_SET_OF_specifics_t *)td->specifics; |
Lev Walkin | aa61a0f | 2014-01-13 23:08:47 -0800 | [diff] [blame] | 800 | ctx = (asn_struct_ctx_t *)((char *)ptr + specs->ctx_offset); |
Lev Walkin | 4f530b5 | 2006-10-19 05:27:54 +0000 | [diff] [blame] | 801 | if(ctx->ptr) { |
| 802 | ASN_STRUCT_FREE(*elm->type, ctx->ptr); |
| 803 | ctx->ptr = 0; |
| 804 | } |
Lev Walkin | 525fc25 | 2007-02-23 19:30:10 +0000 | [diff] [blame] | 805 | |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 806 | switch(method) { |
| 807 | case ASFM_FREE_EVERYTHING: |
| 808 | FREEMEM(ptr); |
| 809 | break; |
| 810 | case ASFM_FREE_UNDERLYING: |
| 811 | break; |
| 812 | case ASFM_FREE_UNDERLYING_AND_RESET: |
| 813 | memset(ptr, 0, specs->struct_size); |
| 814 | break; |
| 815 | } |
| 816 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | int |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 820 | SET_OF_constraint(const asn_TYPE_descriptor_t *td, const void *sptr, |
| 821 | asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
| 822 | const asn_TYPE_member_t *elm = td->elements; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 823 | asn_constr_check_f *constr; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 824 | const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 825 | int i; |
| 826 | |
| 827 | if(!sptr) { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 828 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 829 | "%s: value not given (%s:%d)", |
| 830 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 831 | return -1; |
| 832 | } |
| 833 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 834 | constr = elm->encoding_constraints.general_constraints; |
| 835 | if(!constr) constr = elm->type->encoding_constraints.general_constraints; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 836 | |
| 837 | /* |
| 838 | * Iterate over the members of an array. |
| 839 | * Validate each in turn, until one fails. |
| 840 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 841 | for(i = 0; i < list->count; i++) { |
| 842 | const void *memb_ptr = list->array[i]; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 843 | int ret; |
| 844 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 845 | if(!memb_ptr) continue; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 846 | |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 847 | ret = constr(elm->type, memb_ptr, ctfailcb, app_key); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 848 | if(ret) return ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 851 | return 0; |
| 852 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 853 | |
| 854 | asn_dec_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 855 | SET_OF_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, |
| 856 | const asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 857 | const asn_per_constraints_t *constraints, void **sptr, |
| 858 | asn_per_data_t *pd) { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 859 | asn_dec_rval_t rv; |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 860 | const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 861 | const asn_TYPE_member_t *elm = td->elements; /* Single one */ |
| 862 | void *st = *sptr; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 863 | asn_anonymous_set_ *list; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 864 | const asn_per_constraint_t *ct; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 865 | int repeat = 0; |
| 866 | ssize_t nelems; |
| 867 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 868 | if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx)) |
| 869 | ASN__DECODE_FAILED; |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 870 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 871 | /* |
| 872 | * Create the target structure if it is not present already. |
| 873 | */ |
| 874 | if(!st) { |
| 875 | st = *sptr = CALLOC(1, specs->struct_size); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 876 | if(!st) ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 877 | } |
| 878 | list = _A_SET_FROM_VOID(st); |
| 879 | |
| 880 | /* Figure out which constraints to use */ |
| 881 | if(constraints) ct = &constraints->size; |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 882 | else if(td->encoding_constraints.per_constraints) |
| 883 | ct = &td->encoding_constraints.per_constraints->size; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 884 | else ct = 0; |
| 885 | |
| 886 | if(ct && ct->flags & APC_EXTENSIBLE) { |
| 887 | int value = per_get_few_bits(pd, 1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 888 | if(value < 0) ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 889 | if(value) ct = 0; /* Not restricted! */ |
| 890 | } |
| 891 | |
| 892 | if(ct && ct->effective_bits >= 0) { |
| 893 | /* X.691, #19.5: No length determinant */ |
| 894 | nelems = per_get_few_bits(pd, ct->effective_bits); |
| 895 | ASN_DEBUG("Preparing to fetch %ld+%ld elements from %s", |
| 896 | (long)nelems, ct->lower_bound, td->name); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 897 | if(nelems < 0) ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 898 | nelems += ct->lower_bound; |
| 899 | } else { |
| 900 | nelems = -1; |
| 901 | } |
| 902 | |
| 903 | do { |
| 904 | int i; |
| 905 | if(nelems < 0) { |
Lev Walkin | 9d1b45f | 2017-10-01 17:04:48 -0700 | [diff] [blame] | 906 | nelems = uper_get_length(pd, -1, 0, &repeat); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 907 | ASN_DEBUG("Got to decode %d elements (eff %d)", |
Lev Walkin | 2e76399 | 2011-07-21 01:17:37 +0400 | [diff] [blame] | 908 | (int)nelems, (int)(ct ? ct->effective_bits : -1)); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 909 | if(nelems < 0) ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | for(i = 0; i < nelems; i++) { |
| 913 | void *ptr = 0; |
| 914 | ASN_DEBUG("SET OF %s decoding", elm->type->name); |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 915 | rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type, |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 916 | elm->encoding_constraints.per_constraints, &ptr, pd); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 917 | ASN_DEBUG("%s SET OF %s decoded %d, %p", |
| 918 | td->name, elm->type->name, rv.code, ptr); |
| 919 | if(rv.code == RC_OK) { |
| 920 | if(ASN_SET_ADD(list, ptr) == 0) |
| 921 | continue; |
| 922 | ASN_DEBUG("Failed to add element into %s", |
| 923 | td->name); |
| 924 | /* Fall through */ |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 925 | rv.code = RC_FAIL; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 926 | } else { |
| 927 | ASN_DEBUG("Failed decoding %s of %s (SET OF)", |
| 928 | elm->type->name, td->name); |
| 929 | } |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 930 | if(ptr) ASN_STRUCT_FREE(*elm->type, ptr); |
Lev Walkin | 0a8aa60 | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 931 | return rv; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | nelems = -1; /* Allow uper_get_length() */ |
| 935 | } while(repeat); |
| 936 | |
| 937 | ASN_DEBUG("Decoded %s as SET OF", td->name); |
| 938 | |
| 939 | rv.code = RC_OK; |
| 940 | rv.consumed = 0; |
| 941 | return rv; |
| 942 | } |
| 943 | |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 944 | int |
| 945 | SET_OF_compare(const asn_TYPE_descriptor_t *td, const void *aptr, |
| 946 | const void *bptr) { |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 947 | (void)td; |
| 948 | (void)aptr; |
| 949 | (void)bptr; |
| 950 | /* Not implemented yet. */ |
| 951 | return 0; |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 952 | } |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 953 | |
| 954 | |
| 955 | asn_TYPE_operation_t asn_OP_SET_OF = { |
| 956 | SET_OF_free, |
| 957 | SET_OF_print, |
| 958 | SET_OF_compare, |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 959 | SET_OF_decode_ber, |
| 960 | SET_OF_encode_der, |
| 961 | SET_OF_decode_xer, |
| 962 | SET_OF_encode_xer, |
Lev Walkin | 96f9921 | 2017-08-29 23:38:31 -0700 | [diff] [blame] | 963 | #ifdef ASN_DISABLE_OER_SUPPORT |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 964 | 0, |
| 965 | 0, |
Lev Walkin | 96f9921 | 2017-08-29 23:38:31 -0700 | [diff] [blame] | 966 | #else |
| 967 | SET_OF_decode_oer, |
Lev Walkin | c6bd359 | 2017-08-30 17:36:23 -0700 | [diff] [blame] | 968 | SET_OF_encode_oer, |
Lev Walkin | 96f9921 | 2017-08-29 23:38:31 -0700 | [diff] [blame] | 969 | #endif |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 970 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 971 | 0, |
| 972 | 0, |
| 973 | #else |
| 974 | SET_OF_decode_uper, |
| 975 | 0, /* SET_OF_encode_uper */ |
| 976 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 977 | SET_OF_random_fill, |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 978 | 0 /* Use generic outmost tag fetcher */ |
| 979 | }; |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 980 | |
| 981 | |
| 982 | asn_random_fill_result_t |
| 983 | SET_OF_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, |
| 984 | const asn_encoding_constraints_t *constr, |
| 985 | size_t max_length) { |
| 986 | const asn_SET_OF_specifics_t *specs = |
| 987 | (const asn_SET_OF_specifics_t *)td->specifics; |
| 988 | asn_random_fill_result_t res_ok = {ARFILL_OK, 0}; |
| 989 | asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0}; |
| 990 | asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0}; |
| 991 | const asn_TYPE_member_t *elm = td->elements; |
| 992 | void *st = *sptr; |
| 993 | size_t rnd_len; |
| 994 | |
| 995 | if(max_length == 0) return result_skipped; |
| 996 | |
| 997 | (void)constr; |
| 998 | |
| 999 | if(st == NULL) { |
| 1000 | st = (*sptr = CALLOC(1, specs->struct_size)); |
| 1001 | if(st == NULL) { |
| 1002 | return result_failed; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | rnd_len = asn_random_between(0, 5); |
| 1007 | for(; rnd_len > 0; rnd_len--) { |
| 1008 | asn_anonymous_set_ *list = _A_SET_FROM_VOID(st); |
| 1009 | void *ptr = 0; |
| 1010 | asn_random_fill_result_t tmpres = elm->type->op->random_fill( |
| 1011 | elm->type, &ptr, &elm->encoding_constraints, |
| 1012 | max_length > res_ok.length ? max_length - res_ok.length : 0); |
| 1013 | switch(tmpres.code) { |
| 1014 | case ARFILL_OK: |
| 1015 | ASN_SET_ADD(list, ptr); |
| 1016 | res_ok.length += tmpres.code; |
| 1017 | break; |
| 1018 | case ARFILL_SKIPPED: |
| 1019 | break; |
| 1020 | case ARFILL_FAILED: |
| 1021 | assert(ptr == 0); |
| 1022 | return tmpres; |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | return res_ok; |
| 1027 | } |
| 1028 | |