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