blob: f3f52942dc9f6cce392d26bcafc0d0421a7c7532 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin59b176e2005-11-26 11:25:14 +00002 * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#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 Walkinec1ffd42004-08-18 04:53:32 +000015#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000016
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 Walkincc6a9102004-09-23 22:06:26 +000028#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000029
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 Walkincc6a9102004-09-23 22:06:26 +000034#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000035#define ADVANCE(num_bytes) do { \
36 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -080037 ptr = ((const char *)ptr) + num;\
Lev Walkinf15320b2004-06-03 03:38:44 +000038 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 Walkincc6a9102004-09-23 22:06:26 +000047#undef NEXT_PHASE
48#undef PHASE_OUT
Lev Walkinf15320b2004-06-03 03:38:44 +000049#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 Walkincc6a9102004-09-23 22:06:26 +000058#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000059#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 Walkindc06f6b2004-10-20 15:50:55 +000068asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -070069SET_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 Walkinf15320b2004-06-03 03:38:44 +000073 * Bring closer parts of structure description.
74 */
Lev Walkind84f6032017-10-03 16:33:59 -070075 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
Lev Walkin20696a42017-10-17 21:27:33 -070076 const asn_TYPE_member_t *elm = td->elements; /* Single one */
Lev Walkinf15320b2004-06-03 03:38:44 +000077
Lev Walkin20696a42017-10-17 21:27:33 -070078 /*
Lev Walkinf15320b2004-06-03 03:38:44 +000079 * Parts of the structure being constructed.
80 */
81 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +000082 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +000083
84 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkindc06f6b2004-10-20 15:50:55 +000085 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +000086
87 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
88
Lev Walkin449f8322004-08-20 13:23:42 +000089 ASN_DEBUG("Decoding %s as SET OF", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +000090
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 Walkinaa61a0f2014-01-13 23:08:47 -0800104 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000105
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 Walkin5e033762004-09-29 13:26:15 +0000117 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000118 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000119 if(rval.code != RC_OK) {
120 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000121 td->name, rval.code);
Lev Walkin4ab42cd2004-10-05 06:36:44 +0000122 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000123 }
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 Walkin449f8322004-08-20 13:23:42 +0000151 ASN_DEBUG("End of SET OF %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 /*
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 Walkin8c3b8542005-03-10 18:52:02 +0000170 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000171 if(LEFT < 2) {
172 if(SIZE_VIOLATION)
173 RETURN(RC_FAIL);
174 else
175 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000176 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 /*
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 Walkinda9a3b82005-08-16 17:00:21 +0000186 if(elm->tag != (ber_tlv_tag_t)-1) {
187 if(BER_TAGS_EQUAL(tlv_tag, elm->tag)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000188 /*
189 * The new list member of expected type has arrived.
190 */
191 } else {
192 ASN_DEBUG("Unexpected tag %s fixed SET OF %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000193 ber_tlv_tag_string(tlv_tag), td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 ASN_DEBUG("%s SET OF has tag %s",
Lev Walkinda9a3b82005-08-16 17:00:21 +0000195 td->name, ber_tlv_tag_string(elm->tag));
Lev Walkinf15320b2004-06-03 03:38:44 +0000196 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, Chiu1f87ac02017-08-20 01:25:45 +0800209 rval = elm->type->op->ber_decoder(opt_codec_ctx,
Lev Walkinda9a3b82005-08-16 17:00:21 +0000210 elm->type, &ctx->ptr, ptr, LEFT, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000211 ASN_DEBUG("In %s SET OF %s code %d consumed %d",
Lev Walkinda9a3b82005-08-16 17:00:21 +0000212 td->name, elm->type->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000213 rval.code, (int)rval.consumed);
214 switch(rval.code) {
215 case RC_OK:
216 {
Lev Walkin7e033b52005-07-02 08:19:17 +0000217 asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
Lev Walkinf15320b2004-06-03 03:38:44 +0000218 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 Walkin4f530b52006-10-19 05:27:54 +0000231 ASN_STRUCT_FREE(*elm->type, ctx->ptr);
232 ctx->ptr = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000233 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 Walkin8c3b8542005-03-10 18:52:02 +0000246 if(LEFT > 0 && ((const char *)ptr)[0] != 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000247 /* Unexpected tag */
248 RETURN(RC_FAIL);
249 } else {
250 RETURN(RC_WMORE);
251 }
252 }
Lev Walkin8c3b8542005-03-10 18:52:02 +0000253 if(((const char *)ptr)[0] == 0
254 && ((const char *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000255 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 */
271struct _el_buffer {
272 uint8_t *buf;
273 size_t length;
274 size_t size;
275};
276/* Append bytes to the above structure */
277static int _el_addbytes(const void *buffer, size_t size, void *el_buf_ptr) {
Lev Walkinc2346572004-08-11 09:07:36 +0000278 struct _el_buffer *el_buf = (struct _el_buffer *)el_buf_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000279
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}
288static int _el_buf_cmp(const void *ap, const void *bp) {
Lev Walkinc2346572004-08-11 09:07:36 +0000289 const struct _el_buffer *a = (const struct _el_buffer *)ap;
290 const struct _el_buffer *b = (const struct _el_buffer *)bp;
Lev Walkinf15320b2004-06-03 03:38:44 +0000291 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 Walkina9cc46e2004-09-22 16:06:28 +0000313asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700314SET_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, Chiu1f87ac02017-08-20 01:25:45 +0800319 der_type_encoder_f *der_encoder = elm_type->op->der_encoder;
Lev Walkin20696a42017-10-17 21:27:33 -0700320 const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
321 size_t computed_size = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000322 ssize_t encoding_size = 0;
323 struct _el_buffer *encoded_els;
Lev Walkinda9a3b82005-08-16 17:00:21 +0000324 ssize_t eels_count = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000325 size_t max_encoded_len = 1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000326 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000327 int ret;
328 int edx;
329
Lev Walkin449f8322004-08-20 13:23:42 +0000330 ASN_DEBUG("Estimating size for SET OF %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000331
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 Walkinda9a3b82005-08-16 17:00:21 +0000337 if(!memb_ptr) continue;
Lev Walkin20696a42017-10-17 21:27:33 -0700338 erval = der_encoder(elm_type, memb_ptr, 0, elm->tag, 0, 0);
339 if(erval.encoded == -1)
Lev Walkinf15320b2004-06-03 03:38:44 +0000340 return erval;
341 computed_size += erval.encoded;
342
343 /* Compute maximum encoding's size */
Lev Walkind9bd7752004-06-05 08:17:50 +0000344 if(max_encoded_len < (size_t)erval.encoded)
Lev Walkinf15320b2004-06-03 03:38:44 +0000345 max_encoded_len = erval.encoded;
346 }
347
348 /*
349 * Encode the TLV for the sequence itself.
350 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000351 encoding_size = der_write_tags(td, computed_size, tag_mode, 1, tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000352 cb, app_key);
353 if(encoding_size == -1) {
354 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000355 erval.failed_type = td;
Lev Walkin20696a42017-10-17 21:27:33 -0700356 erval.structure_ptr = sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000357 return erval;
358 }
359 computed_size += encoding_size;
360
Lev Walkin3d231c02006-03-16 22:20:47 +0000361 if(!cb || list->count == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000362 erval.encoded = computed_size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700363 ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000364 }
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 Walkin20696a42017-10-17 21:27:33 -0700371 encoded_els =
372 (struct _el_buffer *)MALLOC(list->count * sizeof(encoded_els[0]));
373 if(encoded_els == NULL) {
374 ASN__ENCODE_FAILED;
375 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000376
Lev Walkin20696a42017-10-17 21:27:33 -0700377 ASN_DEBUG("Encoding members of %s SET OF", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000378
379 /*
380 * Encode all members.
381 */
382 for(edx = 0; edx < list->count; edx++) {
Lev Walkin20696a42017-10-17 21:27:33 -0700383 const void *memb_ptr = list->array[edx];
Lev Walkinda9a3b82005-08-16 17:00:21 +0000384 struct _el_buffer *encoded_el = &encoded_els[eels_count];
385
386 if(!memb_ptr) continue;
Lev Walkinf15320b2004-06-03 03:38:44 +0000387
388 /*
389 * Prepare space for encoding.
390 */
Lev Walkinc2346572004-08-11 09:07:36 +0000391 encoded_el->buf = (uint8_t *)MALLOC(max_encoded_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000392 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 Walkin20696a42017-10-17 21:27:33 -0700399 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 }
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 Walkinda9a3b82005-08-16 17:00:21 +0000414 eels_count++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000415 }
416
417 /*
418 * Sort the encoded elements according to their encoding.
419 */
Lev Walkinda9a3b82005-08-16 17:00:21 +0000420 qsort(encoded_els, eels_count, sizeof(encoded_els[0]), _el_buf_cmp);
Lev Walkinf15320b2004-06-03 03:38:44 +0000421
422 /*
423 * Report encoded elements to the application.
424 * Dispose of temporary sorted members table.
425 */
426 ret = 0;
Lev Walkinda9a3b82005-08-16 17:00:21 +0000427 for(edx = 0; edx < eels_count; edx++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000428 struct _el_buffer *encoded_el = &encoded_els[edx];
429 /* Report encoded chunks to the application */
430 if(ret == 0
Lev Walkin8e8078a2004-09-26 13:10:40 +0000431 && cb(encoded_el->buf, encoded_el->length, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000432 ret = -1;
433 FREEMEM(encoded_el->buf);
434 }
435 FREEMEM(encoded_els);
436
Lev Walkind9bd7752004-06-05 08:17:50 +0000437 if(ret || computed_size != (size_t)encoding_size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000438 /*
439 * Standard callback failed, or
440 * encoded size is not equal to the computed size.
441 */
Lev Walkin20696a42017-10-17 21:27:33 -0700442 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000443 } else {
444 erval.encoded = computed_size;
Lev Walkin20696a42017-10-17 21:27:33 -0700445 ASN__ENCODED_OK(erval);
446 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000447}
448
Lev Walkin8bb4a952005-02-14 20:15:40 +0000449#undef XER_ADVANCE
450#define XER_ADVANCE(num_bytes) do { \
451 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800452 buf_ptr = ((const char *)buf_ptr) + num;\
Lev Walkin8bb4a952005-02-14 20:15:40 +0000453 size -= num; \
454 consumed_myself += num; \
455 } while(0)
456
457/*
458 * Decode the XER (XML) data.
459 */
460asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700461SET_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 Walkin8bb4a952005-02-14 20:15:40 +0000465 * Bring closer parts of structure description.
466 */
Lev Walkind84f6032017-10-03 16:33:59 -0700467 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
Lev Walkin20696a42017-10-17 21:27:33 -0700468 const asn_TYPE_member_t *element = td->elements;
Lev Walkind1bfea62005-11-08 03:06:16 +0000469 const char *elm_tag;
Lev Walkin8bb4a952005-02-14 20:15:40 +0000470 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 Walkin8bb4a952005-02-14 20:15:40 +0000480
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 Walkind1bfea62005-11-08 03:06:16 +0000489 /* 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 Walkin8bb4a952005-02-14 20:15:40 +0000497 /*
498 * Restore parsing context.
499 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800500 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkin8bb4a952005-02-14 20:15:40 +0000501
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 Walkin2eeeedc2005-02-18 16:10:40 +0000508 for(; ctx->phase <= 2;) {
Lev Walkin8bb4a952005-02-14 20:15:40 +0000509 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 Walkind1bfea62005-11-08 03:06:16 +0000520 ASN_DEBUG("XER/SET OF element [%s]", elm_tag);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800521 tmprval = element->type->op->xer_decoder(opt_codec_ctx,
Lev Walkin8bb4a952005-02-14 20:15:40 +0000522 element->type, &ctx->ptr, elm_tag,
523 buf_ptr, size);
524 if(tmprval.code == RC_OK) {
Lev Walkin7e033b52005-07-02 08:19:17 +0000525 asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
Lev Walkin8bb4a952005-02-14 20:15:40 +0000526 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 Walkin8bb4a952005-02-14 20:15:40 +0000535 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 Walkin1e443962005-02-18 18:06:36 +0000542 ch_size = xer_next_token(&ctx->context,
543 buf_ptr, size, &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800544 if(ch_size == -1) {
545 RETURN(RC_FAIL);
546 } else {
Lev Walkin8bb4a952005-02-14 20:15:40 +0000547 switch(ch_type) {
Lev Walkin97363482016-01-24 19:23:02 -0800548 case PXER_WMORE:
549 RETURN(RC_WMORE);
Lev Walkin8bb4a952005-02-14 20:15:40 +0000550 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 Walkind1bfea62005-11-08 03:06:16 +0000560 ASN_DEBUG("XER/SET OF: tcv = %d, ph=%d t=%s",
561 tcv, ctx->phase, xml_tag);
Lev Walkin8bb4a952005-02-14 20:15:40 +0000562 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 Walkin904e65b2005-02-18 14:23:48 +0000582 case XCT_UNKNOWN_OP:
583 case XCT_UNKNOWN_BO:
Lev Walkin8bb4a952005-02-14 20:15:40 +0000584
585 ASN_DEBUG("XER/SET OF: tcv=%d, ph=%d", tcv, ctx->phase);
Lev Walkin642962a2005-02-24 22:37:07 +0000586 if(ctx->phase == 1) {
Lev Walkin8bb4a952005-02-14 20:15:40 +0000587 /*
Lev Walkin642962a2005-02-24 22:37:07 +0000588 * Process a single possible member.
Lev Walkin8bb4a952005-02-14 20:15:40 +0000589 */
590 ctx->phase = 2;
591 continue;
Lev Walkin8bb4a952005-02-14 20:15:40 +0000592 }
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 Walkind5193802004-10-03 09:12:07 +0000608typedef struct xer_tmp_enc_s {
609 void *buffer;
610 size_t offset;
611 size_t size;
612} xer_tmp_enc_t;
613static int
614SET_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 Walkinaa61a0f2014-01-13 23:08:47 -0800623 memcpy((char *)t->buffer + t->offset, buffer, size);
Lev Walkind5193802004-10-03 09:12:07 +0000624 t->offset += size;
625 return 0;
626}
627static int
628SET_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 Walkina9cc46e2004-09-22 16:06:28 +0000645asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700646SET_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 Walkind84f6032017-10-03 16:33:59 -0700650 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
Lev Walkin20696a42017-10-17 21:27:33 -0700651 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 Walkinda9a3b82005-08-16 17:00:21 +0000654 ? 0 : ((*elm->name) ? elm->name : elm->type->xml_tag);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000655 size_t mlen = mname ? strlen(mname) : 0;
656 int xcan = (flags & XER_F_CANONICAL);
Lev Walkind5193802004-10-03 09:12:07 +0000657 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 Walkina9cc46e2004-09-22 16:06:28 +0000661 int i;
662
Lev Walkin7c1dc052016-03-14 03:08:15 -0700663 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000664
Lev Walkind5193802004-10-03 09:12:07 +0000665 if(xcan) {
666 encs = (xer_tmp_enc_t *)MALLOC(list->count * sizeof(encs[0]));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700667 if(!encs) ASN__ENCODE_FAILED;
Lev Walkind5193802004-10-03 09:12:07 +0000668 cb = SET_OF_encode_xer_callback;
669 }
670
Lev Walkina9cc46e2004-09-22 16:06:28 +0000671 er.encoded = 0;
672
Lev Walkina9cc46e2004-09-22 16:06:28 +0000673 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 Walkind5193802004-10-03 09:12:07 +0000679 if(encs) {
680 memset(&encs[encs_count], 0, sizeof(encs[0]));
681 app_key = &encs[encs_count];
682 encs_count++;
683 }
684
Lev Walkina9cc46e2004-09-22 16:06:28 +0000685 if(mname) {
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700686 if(!xcan) ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700687 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000688 }
689
Lev Walkind1bfea62005-11-08 03:06:16 +0000690 if(!xcan && specs->as_XMLValueList == 1)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700691 ASN__TEXT_INDENT(1, ilevel + 1);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800692 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr,
Lev Walkind1bfea62005-11-08 03:06:16 +0000693 ilevel + (specs->as_XMLValueList != 2),
694 flags, cb, app_key);
Lev Walkin4fe28822017-09-18 02:57:34 -0700695 if(tmper.encoded == -1) return tmper;
696 er.encoded += tmper.encoded;
Lev Walkin642962a2005-02-24 22:37:07 +0000697 if(tmper.encoded == 0 && specs->as_XMLValueList) {
Lev Walkind1bfea62005-11-08 03:06:16 +0000698 const char *name = elm->type->xml_tag;
Lev Walkin642962a2005-02-24 22:37:07 +0000699 size_t len = strlen(name);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700700 ASN__CALLBACK3("<", 1, name, len, "/>", 2);
Lev Walkin642962a2005-02-24 22:37:07 +0000701 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000702
703 if(mname) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700704 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000705 }
706
Lev Walkina9cc46e2004-09-22 16:06:28 +0000707 }
708
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700709 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000710
Lev Walkind5193802004-10-03 09:12:07 +0000711 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 Walkin4fe28822017-09-18 02:57:34 -0700716 er.encoded = 0;
Lev Walkind5193802004-10-03 09:12:07 +0000717 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 Walkin7c1dc052016-03-14 03:08:15 -0700722 ASN__CALLBACK(enc->buffer, enc->offset);
Lev Walkind5193802004-10-03 09:12:07 +0000723 FREEMEM(enc->buffer);
724 enc->buffer = 0;
725 control_size += enc->offset;
726 }
727 assert(control_size == er.encoded);
728 }
729
730 goto cleanup;
731cb_failed:
Lev Walkin4fe28822017-09-18 02:57:34 -0700732 ASN__ENCODE_FAILED;
Lev Walkind5193802004-10-03 09:12:07 +0000733cleanup:
734 if(encs) {
Lev Walkine835b3a2017-09-18 00:14:40 -0700735 size_t n;
736 for(n = 0; n < encs_count; n++) {
737 FREEMEM(encs[n].buffer);
Lev Walkind5193802004-10-03 09:12:07 +0000738 }
Lev Walkin419f6752006-09-13 04:02:00 +0000739 FREEMEM(encs);
Lev Walkind5193802004-10-03 09:12:07 +0000740 }
Lev Walkin7c1dc052016-03-14 03:08:15 -0700741 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000742}
743
Lev Walkinf15320b2004-06-03 03:38:44 +0000744int
Lev Walkin20696a42017-10-17 21:27:33 -0700745SET_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 Walkin7e033b52005-07-02 08:19:17 +0000748 const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000749 int ret;
750 int i;
751
Lev Walkin8e8078a2004-09-26 13:10:40 +0000752 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000753
754 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000755 if(cb(td->name, strlen(td->name), app_key) < 0
756 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000757 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 Walkin8e8078a2004-09-26 13:10:40 +0000763 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000764
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800765 ret = elm->type->op->print_struct(elm->type, memb_ptr,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000766 ilevel + 1, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000767 if(ret) return ret;
768 }
769
Lev Walkin8e8078a2004-09-26 13:10:40 +0000770 ilevel--;
771 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000772
Lev Walkin8e8078a2004-09-26 13:10:40 +0000773 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000774}
775
776void
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700777SET_OF_free(const asn_TYPE_descriptor_t *td, void *ptr,
778 enum asn_struct_free_method method) {
779 if(td && ptr) {
Lev Walkind84f6032017-10-03 16:33:59 -0700780 const asn_SET_OF_specifics_t *specs;
Lev Walkinda9a3b82005-08-16 17:00:21 +0000781 asn_TYPE_member_t *elm = td->elements;
Lev Walkin7e033b52005-07-02 08:19:17 +0000782 asn_anonymous_set_ *list = _A_SET_FROM_VOID(ptr);
Lev Walkin4f530b52006-10-19 05:27:54 +0000783 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000784 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 Walkinadcb5862006-03-17 02:11:12 +0000793 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000794 }
Lev Walkin246a2af2004-07-15 10:51:26 +0000795 list->count = 0; /* No meaningful elements left */
796
797 asn_set_empty(list); /* Remove (list->array) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000798
Lev Walkind84f6032017-10-03 16:33:59 -0700799 specs = (const asn_SET_OF_specifics_t *)td->specifics;
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800800 ctx = (asn_struct_ctx_t *)((char *)ptr + specs->ctx_offset);
Lev Walkin4f530b52006-10-19 05:27:54 +0000801 if(ctx->ptr) {
802 ASN_STRUCT_FREE(*elm->type, ctx->ptr);
803 ctx->ptr = 0;
804 }
Lev Walkin525fc252007-02-23 19:30:10 +0000805
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700806 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 Walkinf15320b2004-06-03 03:38:44 +0000817}
818
819int
Lev Walkin20696a42017-10-17 21:27:33 -0700820SET_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 Walkin449f8322004-08-20 13:23:42 +0000823 asn_constr_check_f *constr;
Lev Walkin7e033b52005-07-02 08:19:17 +0000824 const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000825 int i;
826
827 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700828 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000829 "%s: value not given (%s:%d)",
830 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000831 return -1;
832 }
833
Lev Walkina5972be2017-09-29 23:15:58 -0700834 constr = elm->encoding_constraints.general_constraints;
835 if(!constr) constr = elm->type->encoding_constraints.general_constraints;
Lev Walkin449f8322004-08-20 13:23:42 +0000836
837 /*
838 * Iterate over the members of an array.
839 * Validate each in turn, until one fails.
840 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000841 for(i = 0; i < list->count; i++) {
842 const void *memb_ptr = list->array[i];
Lev Walkin449f8322004-08-20 13:23:42 +0000843 int ret;
844
Lev Walkinf15320b2004-06-03 03:38:44 +0000845 if(!memb_ptr) continue;
Lev Walkin449f8322004-08-20 13:23:42 +0000846
Lev Walkin1eded352006-07-13 11:19:01 +0000847 ret = constr(elm->type, memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000848 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000849 }
850
Lev Walkinf15320b2004-06-03 03:38:44 +0000851 return 0;
852}
Lev Walkin59b176e2005-11-26 11:25:14 +0000853
854asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700855SET_OF_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
856 const asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700857 const asn_per_constraints_t *constraints, void **sptr,
858 asn_per_data_t *pd) {
Lev Walkin20696a42017-10-17 21:27:33 -0700859 asn_dec_rval_t rv;
Lev Walkind84f6032017-10-03 16:33:59 -0700860 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
Lev Walkin20696a42017-10-17 21:27:33 -0700861 const asn_TYPE_member_t *elm = td->elements; /* Single one */
862 void *st = *sptr;
Lev Walkin59b176e2005-11-26 11:25:14 +0000863 asn_anonymous_set_ *list;
Lev Walkin494fb702017-08-07 20:07:00 -0700864 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000865 int repeat = 0;
866 ssize_t nelems;
867
Lev Walkin7c1dc052016-03-14 03:08:15 -0700868 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
869 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000870
Lev Walkin59b176e2005-11-26 11:25:14 +0000871 /*
872 * Create the target structure if it is not present already.
873 */
874 if(!st) {
875 st = *sptr = CALLOC(1, specs->struct_size);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700876 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000877 }
878 list = _A_SET_FROM_VOID(st);
879
880 /* Figure out which constraints to use */
881 if(constraints) ct = &constraints->size;
Lev Walkina5972be2017-09-29 23:15:58 -0700882 else if(td->encoding_constraints.per_constraints)
883 ct = &td->encoding_constraints.per_constraints->size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000884 else ct = 0;
885
886 if(ct && ct->flags & APC_EXTENSIBLE) {
887 int value = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700888 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000889 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 Walkin7c1dc052016-03-14 03:08:15 -0700897 if(nelems < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000898 nelems += ct->lower_bound;
899 } else {
900 nelems = -1;
901 }
902
903 do {
904 int i;
905 if(nelems < 0) {
Lev Walkin9d1b45f2017-10-01 17:04:48 -0700906 nelems = uper_get_length(pd, -1, 0, &repeat);
Lev Walkin59b176e2005-11-26 11:25:14 +0000907 ASN_DEBUG("Got to decode %d elements (eff %d)",
Lev Walkin2e763992011-07-21 01:17:37 +0400908 (int)nelems, (int)(ct ? ct->effective_bits : -1));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700909 if(nelems < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000910 }
911
912 for(i = 0; i < nelems; i++) {
913 void *ptr = 0;
914 ASN_DEBUG("SET OF %s decoding", elm->type->name);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800915 rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
Lev Walkina5972be2017-09-29 23:15:58 -0700916 elm->encoding_constraints.per_constraints, &ptr, pd);
Lev Walkin59b176e2005-11-26 11:25:14 +0000917 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 Walkin725883b2006-10-09 12:07:58 +0000925 rv.code = RC_FAIL;
Lev Walkin59b176e2005-11-26 11:25:14 +0000926 } else {
927 ASN_DEBUG("Failed decoding %s of %s (SET OF)",
928 elm->type->name, td->name);
929 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000930 if(ptr) ASN_STRUCT_FREE(*elm->type, ptr);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000931 return rv;
Lev Walkin59b176e2005-11-26 11:25:14 +0000932 }
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 Walkincd2f48e2017-08-10 02:14:59 -0700944int
945SET_OF_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
946 const void *bptr) {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800947 (void)td;
948 (void)aptr;
949 (void)bptr;
950 /* Not implemented yet. */
951 return 0;
Lev Walkincd2f48e2017-08-10 02:14:59 -0700952}
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800953
954
955asn_TYPE_operation_t asn_OP_SET_OF = {
956 SET_OF_free,
957 SET_OF_print,
958 SET_OF_compare,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800959 SET_OF_decode_ber,
960 SET_OF_encode_der,
961 SET_OF_decode_xer,
962 SET_OF_encode_xer,
Lev Walkin96f99212017-08-29 23:38:31 -0700963#ifdef ASN_DISABLE_OER_SUPPORT
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800964 0,
965 0,
Lev Walkin96f99212017-08-29 23:38:31 -0700966#else
967 SET_OF_decode_oer,
Lev Walkinc6bd3592017-08-30 17:36:23 -0700968 SET_OF_encode_oer,
Lev Walkin96f99212017-08-29 23:38:31 -0700969#endif
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800970#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 Walkina5972be2017-09-29 23:15:58 -0700977 SET_OF_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800978 0 /* Use generic outmost tag fetcher */
979};
Lev Walkina5972be2017-09-29 23:15:58 -0700980
981
982asn_random_fill_result_t
983SET_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