1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
5 # Licensed under the Apache License, Version 2.0 (the "License"); *
6 # you may not use this file except in compliance with the License. *
7 # You may obtain a copy of the License at *
9 # http://www.apache.org/licenses/LICENSE-2.0 *
11 # Unless required by applicable law or agreed to in writing, software *
12 # distributed under the License is distributed on an "AS IS" BASIS, *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 # See the License for the specific language governing permissions and *
15 # limitations under the License. *
17 ******************************************************************************/
20 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>.
21 * All rights reserved.
22 * Redistribution and modifications are permitted subject to BSD license.
24 #include <asn_internal.h>
25 #include <constr_SET_OF.h>
26 #include <asn_SET_OF.h>
29 * Number of bytes left for this structure.
30 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
31 * (size) contains the number of bytes in the buffer passed.
33 #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
36 * If the subprocessor function returns with an indication that it wants
37 * more data, it may well be a fatal decoding problem, because the
38 * size is constrained by the <TLV>'s L, even if the buffer size allows
40 * For example, consider the buffer containing the following TLVs:
41 * <T:5><L:1><V> <T:6>...
42 * The TLV length clearly indicates that one byte is expected in V, but
43 * if the V processor returns with "want more data" even if the buffer
44 * contains way more data than the V processor have seen.
46 #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
49 * This macro "eats" the part of the buffer which is definitely "consumed",
50 * i.e. was correctly converted into local representation or rightfully skipped.
53 #define ADVANCE(num_bytes) do { \
54 size_t num = num_bytes; \
55 ptr = ((const char *)ptr) + num;\
59 consumed_myself += num; \
63 * Switch to the next phase of parsing.
67 #define NEXT_PHASE(ctx) do { \
71 #define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
74 * Return a standardized complex structure.
77 #define RETURN(_code) do { \
79 rval.consumed = consumed_myself;\
84 * The decoder of the SET OF type.
87 SET_OF_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
88 const asn_TYPE_descriptor_t *td, void **struct_ptr,
89 const void *ptr, size_t size, int tag_mode) {
91 * Bring closer parts of structure description.
93 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
94 const asn_TYPE_member_t *elm = td->elements; /* Single one */
97 * Parts of the structure being constructed.
99 void *st = *struct_ptr; /* Target structure. */
100 asn_struct_ctx_t *ctx; /* Decoder context */
102 ber_tlv_tag_t tlv_tag; /* T from TLV */
103 asn_dec_rval_t rval; /* Return code from subparsers */
105 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
107 ASN_DEBUG("Decoding %s as SET OF", td->name);
110 * Create the target structure if it is not present already.
113 st = *struct_ptr = CALLOC(1, specs->struct_size);
120 * Restore parsing context.
122 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
125 * Start to parse where left previously
131 * Check that the set of tags associated with given structure
132 * perfectly fits our expectations.
135 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
136 tag_mode, 1, &ctx->left, 0);
137 if(rval.code != RC_OK) {
138 ASN_DEBUG("%s tagging check failed: %d",
139 td->name, rval.code);
144 ctx->left += rval.consumed; /* ?Substracted below! */
145 ADVANCE(rval.consumed);
147 ASN_DEBUG("Structure consumes %ld bytes, "
148 "buffer %ld", (long)ctx->left, (long)size);
155 * From the place where we've left it previously,
156 * try to decode the next item.
158 for(;; ctx->step = 0) {
159 ssize_t tag_len; /* Length of TLV's T */
165 * MICROPHASE 1: Synchronize decoding.
169 ASN_DEBUG("End of SET OF %s", td->name);
171 * No more things to decode.
179 * Fetch the T from TLV.
181 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
183 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
185 case -1: RETURN(RC_FAIL);
188 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
194 } else if(((const uint8_t *)ptr)[1] == 0) {
196 * Found the terminator of the
197 * indefinite length structure.
203 /* Outmost tag may be unknown and cannot be fetched/compared */
204 if(elm->tag != (ber_tlv_tag_t)-1) {
205 if(BER_TAGS_EQUAL(tlv_tag, elm->tag)) {
207 * The new list member of expected type has arrived.
210 ASN_DEBUG("Unexpected tag %s fixed SET OF %s",
211 ber_tlv_tag_string(tlv_tag), td->name);
212 ASN_DEBUG("%s SET OF has tag %s",
213 td->name, ber_tlv_tag_string(elm->tag));
219 * MICROPHASE 2: Invoke the member-specific decoder.
221 ctx->step |= 1; /* Confirm entering next microphase */
225 * Invoke the member fetch routine according to member's type
227 rval = elm->type->op->ber_decoder(opt_codec_ctx,
228 elm->type, &ctx->ptr, ptr, LEFT, 0);
229 ASN_DEBUG("In %s SET OF %s code %d consumed %d",
230 td->name, elm->type->name,
231 rval.code, (int)rval.consumed);
235 asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
236 if(ASN_SET_ADD(list, ctx->ptr) != 0)
242 case RC_WMORE: /* More data expected */
243 if(!SIZE_VIOLATION) {
244 ADVANCE(rval.consumed);
248 case RC_FAIL: /* Fatal error */
249 ASN_STRUCT_FREE(*elm->type, ctx->ptr);
254 ADVANCE(rval.consumed);
255 } /* for(all list members) */
260 * Read in all "end of content" TLVs.
262 while(ctx->left < 0) {
264 if(LEFT > 0 && ((const char *)ptr)[0] != 0) {
271 if(((const char *)ptr)[0] == 0
272 && ((const char *)ptr)[1] == 0) {
287 * Internally visible buffer holding a single encoded element.
292 size_t allocated_size;
293 unsigned bits_unused;
295 /* Append bytes to the above structure */
296 static int _el_addbytes(const void *buffer, size_t size, void *el_buf_ptr) {
297 struct _el_buffer *el_buf = (struct _el_buffer *)el_buf_ptr;
299 if(el_buf->length + size > el_buf->allocated_size) {
300 size_t new_size = el_buf->allocated_size ? el_buf->allocated_size : 8;
305 } while(el_buf->length + size > new_size);
307 p = REALLOC(el_buf->buf, new_size);
310 el_buf->allocated_size = new_size;
316 memcpy(el_buf->buf + el_buf->length, buffer, size);
318 el_buf->length += size;
322 static void assert_unused_bits(const struct _el_buffer* p) {
324 assert((p->buf[p->length-1] & ~(0xff << p->bits_unused)) == 0);
326 assert(p->bits_unused == 0);
330 static int _el_buf_cmp(const void *ap, const void *bp) {
331 const struct _el_buffer *a = (const struct _el_buffer *)ap;
332 const struct _el_buffer *b = (const struct _el_buffer *)bp;
336 if(a->length < b->length)
337 common_len = a->length;
339 common_len = b->length;
341 if (a->buf && b->buf) {
342 ret = memcmp(a->buf, b->buf, common_len);
345 if(a->length < b->length)
347 else if(a->length > b->length)
349 /* Ignore unused bits. */
350 assert_unused_bits(a);
351 assert_unused_bits(b);
358 SET_OF__encode_sorted_free(struct _el_buffer *el_buf, size_t count) {
361 for(i = 0; i < count; i++) {
362 FREEMEM(el_buf[i].buf);
368 enum SET_OF__encode_method {
369 SOES_DER, /* Distinguished Encoding Rules */
370 SOES_CUPER /* Canonical Unaligned Packed Encoding Rules */
373 static struct _el_buffer *
374 SET_OF__encode_sorted(const asn_TYPE_member_t *elm,
375 const asn_anonymous_set_ *list,
376 enum SET_OF__encode_method method) {
377 struct _el_buffer *encoded_els;
381 (struct _el_buffer *)CALLOC(list->count, sizeof(encoded_els[0]));
382 if(encoded_els == NULL) {
387 * Encode all members.
389 for(edx = 0; edx < list->count; edx++) {
390 const void *memb_ptr = list->array[edx];
391 struct _el_buffer *encoding_el = &encoded_els[edx];
392 asn_enc_rval_t erval = {0,0,0};
397 * Encode the member into the prepared space.
401 erval = elm->type->op->der_encoder(elm->type, memb_ptr, 0, elm->tag,
402 _el_addbytes, encoding_el);
405 erval = uper_encode(elm->type,
406 elm->encoding_constraints.per_constraints,
407 memb_ptr, _el_addbytes, encoding_el);
408 if(erval.encoded != -1) {
409 size_t extra_bits = erval.encoded % 8;
410 assert(encoding_el->length == (size_t)(erval.encoded + 7) / 8);
411 encoding_el->bits_unused = (8 - extra_bits) & 0x7;
415 assert(!"Unreachable");
418 if(erval.encoded < 0) break;
421 if(edx == list->count) {
423 * Sort the encoded elements according to their encoding.
425 qsort(encoded_els, list->count, sizeof(encoded_els[0]), _el_buf_cmp);
429 SET_OF__encode_sorted_free(encoded_els, edx);
436 * The DER encoder of the SET OF type.
439 SET_OF_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
440 int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
442 const asn_TYPE_member_t *elm = td->elements;
443 const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
444 size_t computed_size = 0;
445 ssize_t encoding_size = 0;
446 struct _el_buffer *encoded_els;
449 ASN_DEBUG("Estimating size for SET OF %s", td->name);
452 * Gather the length of the underlying members sequence.
454 for(edx = 0; edx < list->count; edx++) {
455 void *memb_ptr = list->array[edx];
456 asn_enc_rval_t erval = {0,0,0};
458 if(!memb_ptr) ASN__ENCODE_FAILED;
461 elm->type->op->der_encoder(elm->type, memb_ptr, 0, elm->tag, 0, 0);
462 if(erval.encoded == -1) return erval;
463 computed_size += erval.encoded;
468 * Encode the TLV for the sequence itself.
471 der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
472 if(encoding_size < 0) {
475 computed_size += encoding_size;
477 if(!cb || list->count == 0) {
478 asn_enc_rval_t erval = {0,0,0};
479 erval.encoded = computed_size;
480 ASN__ENCODED_OK(erval);
483 ASN_DEBUG("Encoding members of %s SET OF", td->name);
486 * DER mandates dynamic sorting of the SET OF elements
487 * according to their encodings. Build an array of the
490 encoded_els = SET_OF__encode_sorted(elm, list, SOES_DER);
493 * Report encoded elements to the application.
494 * Dispose of temporary sorted members table.
496 for(edx = 0; edx < list->count; edx++) {
497 struct _el_buffer *encoded_el = &encoded_els[edx];
498 /* Report encoded chunks to the application */
499 if(cb(encoded_el->buf, encoded_el->length, app_key) < 0) {
502 encoding_size += encoded_el->length;
506 SET_OF__encode_sorted_free(encoded_els, list->count);
508 if(edx == list->count) {
509 asn_enc_rval_t erval = {0,0,0};
510 assert(computed_size == (size_t)encoding_size);
511 erval.encoded = computed_size;
512 ASN__ENCODED_OK(erval);
519 #define XER_ADVANCE(num_bytes) do { \
520 size_t num = num_bytes; \
521 buf_ptr = ((const char *)buf_ptr) + num;\
523 consumed_myself += num; \
527 * Decode the XER (XML) data.
530 SET_OF_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
531 const asn_TYPE_descriptor_t *td, void **struct_ptr,
532 const char *opt_mname, const void *buf_ptr, size_t size) {
534 * Bring closer parts of structure description.
536 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
537 const asn_TYPE_member_t *element = td->elements;
539 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
542 * ... and parts of the structure being constructed.
544 void *st = *struct_ptr; /* Target structure. */
545 asn_struct_ctx_t *ctx; /* Decoder context */
547 asn_dec_rval_t rval = {RC_OK, 0};/* Return value from a decoder */
548 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
551 * Create the target structure if it is not present already.
554 st = *struct_ptr = CALLOC(1, specs->struct_size);
555 if(st == 0) RETURN(RC_FAIL);
558 /* Which tag is expected for the downstream */
559 if(specs->as_XMLValueList) {
560 elm_tag = (specs->as_XMLValueList == 1) ? 0 : "";
562 elm_tag = (*element->name)
563 ? element->name : element->type->xml_tag;
567 * Restore parsing context.
569 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
572 * Phases of XER/XML processing:
573 * Phase 0: Check that the opening tag matches our expectations.
574 * Phase 1: Processing body and reacting on closing tag.
575 * Phase 2: Processing inner type.
577 for(; ctx->phase <= 2;) {
578 pxer_chunk_type_e ch_type; /* XER chunk type */
579 ssize_t ch_size; /* Chunk size */
580 xer_check_tag_e tcv; /* Tag check value */
583 * Go inside the inner member of a set.
585 if(ctx->phase == 2) {
586 asn_dec_rval_t tmprval = {RC_OK, 0};
588 /* Invoke the inner type decoder, m.b. multiple times */
589 ASN_DEBUG("XER/SET OF element [%s]", elm_tag);
590 tmprval = element->type->op->xer_decoder(opt_codec_ctx,
591 element->type, &ctx->ptr, elm_tag,
593 if(tmprval.code == RC_OK) {
594 asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
595 if(ASN_SET_ADD(list, ctx->ptr) != 0)
598 XER_ADVANCE(tmprval.consumed);
600 XER_ADVANCE(tmprval.consumed);
601 RETURN(tmprval.code);
603 ctx->phase = 1; /* Back to body processing */
604 ASN_DEBUG("XER/SET OF phase => %d", ctx->phase);
609 * Get the next part of the XML stream.
611 ch_size = xer_next_token(&ctx->context,
612 buf_ptr, size, &ch_type);
619 case PXER_COMMENT: /* Got XML comment */
620 case PXER_TEXT: /* Ignore free-standing text */
621 XER_ADVANCE(ch_size); /* Skip silently */
624 break; /* Check the rest down there */
628 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
629 ASN_DEBUG("XER/SET OF: tcv = %d, ph=%d t=%s",
630 tcv, ctx->phase, xml_tag);
633 if(ctx->phase == 0) break;
637 if(ctx->phase == 0) {
638 /* No more things to decode */
639 XER_ADVANCE(ch_size);
640 ctx->phase = 3; /* Phase out */
645 if(ctx->phase == 0) {
646 XER_ADVANCE(ch_size);
647 ctx->phase = 1; /* Processing body phase */
654 ASN_DEBUG("XER/SET OF: tcv=%d, ph=%d", tcv, ctx->phase);
655 if(ctx->phase == 1) {
657 * Process a single possible member.
667 ASN_DEBUG("Unexpected XML tag in SET OF");
671 ctx->phase = 3; /* "Phase out" on hard failure */
677 typedef struct xer_tmp_enc_s {
683 SET_OF_encode_xer_callback(const void *buffer, size_t size, void *key) {
684 xer_tmp_enc_t *t = (xer_tmp_enc_t *)key;
685 if(t->offset + size >= t->size) {
686 size_t newsize = (t->size << 2) + size;
687 void *p = REALLOC(t->buffer, newsize);
692 memcpy((char *)t->buffer + t->offset, buffer, size);
697 SET_OF_xer_order(const void *aptr, const void *bptr) {
698 const xer_tmp_enc_t *a = (const xer_tmp_enc_t *)aptr;
699 const xer_tmp_enc_t *b = (const xer_tmp_enc_t *)bptr;
700 size_t minlen = a->offset;
702 if(b->offset < minlen) minlen = b->offset;
703 /* Well-formed UTF-8 has this nice lexicographical property... */
704 ret = memcmp(a->buffer, b->buffer, minlen);
705 if(ret != 0) return ret;
706 if(a->offset == b->offset)
708 if(a->offset == minlen)
715 SET_OF_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
716 enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
718 asn_enc_rval_t er = {0,0,0};
719 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
720 const asn_TYPE_member_t *elm = td->elements;
721 const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
722 const char *mname = specs->as_XMLValueList
723 ? 0 : ((*elm->name) ? elm->name : elm->type->xml_tag);
724 size_t mlen = mname ? strlen(mname) : 0;
725 int xcan = (flags & XER_F_CANONICAL);
726 xer_tmp_enc_t *encs = 0;
727 size_t encs_count = 0;
728 void *original_app_key = app_key;
729 asn_app_consume_bytes_f *original_cb = cb;
732 if(!sptr) ASN__ENCODE_FAILED;
735 encs = (xer_tmp_enc_t *)MALLOC(list->count * sizeof(encs[0]));
736 if(!encs) ASN__ENCODE_FAILED;
737 cb = SET_OF_encode_xer_callback;
742 for(i = 0; i < list->count; i++) {
743 asn_enc_rval_t tmper = {0,0,0};
745 void *memb_ptr = list->array[i];
746 if(!memb_ptr) continue;
749 memset(&encs[encs_count], 0, sizeof(encs[0]));
750 app_key = &encs[encs_count];
755 if(!xcan) ASN__TEXT_INDENT(1, ilevel);
756 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
759 if(!xcan && specs->as_XMLValueList == 1)
760 ASN__TEXT_INDENT(1, ilevel + 1);
761 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr,
762 ilevel + (specs->as_XMLValueList != 2),
764 if(tmper.encoded == -1) return tmper;
765 er.encoded += tmper.encoded;
766 if(tmper.encoded == 0 && specs->as_XMLValueList) {
767 const char *name = elm->type->xml_tag;
768 size_t len = strlen(name);
769 ASN__CALLBACK3("<", 1, name, len, "/>", 2);
773 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
778 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
781 xer_tmp_enc_t *enc = encs;
782 xer_tmp_enc_t *end = encs + encs_count;
783 ssize_t control_size = 0;
787 app_key = original_app_key;
788 qsort(encs, encs_count, sizeof(encs[0]), SET_OF_xer_order);
790 for(; enc < end; enc++) {
791 ASN__CALLBACK(enc->buffer, enc->offset);
792 FREEMEM(enc->buffer);
794 control_size += enc->offset;
796 assert(control_size == er.encoded);
805 for(n = 0; n < encs_count; n++) {
806 FREEMEM(encs[n].buffer);
814 SET_OF_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
815 asn_app_consume_bytes_f *cb, void *app_key) {
816 asn_TYPE_member_t *elm = td->elements;
817 const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
821 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
824 if(cb(td->name, strlen(td->name), app_key) < 0
825 || cb(" ::= {", 6, app_key) < 0)
828 for(i = 0; i < list->count; i++) {
829 const void *memb_ptr = list->array[i];
830 if(!memb_ptr) continue;
834 ret = elm->type->op->print_struct(elm->type, memb_ptr,
835 ilevel + 1, cb, app_key);
842 return (cb("}", 1, app_key) < 0) ? -1 : 0;
846 SET_OF_free(const asn_TYPE_descriptor_t *td, void *ptr,
847 enum asn_struct_free_method method) {
849 const asn_SET_OF_specifics_t *specs;
850 asn_TYPE_member_t *elm = td->elements;
851 asn_anonymous_set_ *list = _A_SET_FROM_VOID(ptr);
852 asn_struct_ctx_t *ctx; /* Decoder context */
856 * Could not use set_of_empty() because of (*free)
859 for(i = 0; i < list->count; i++) {
860 void *memb_ptr = list->array[i];
862 ASN_STRUCT_FREE(*elm->type, memb_ptr);
864 list->count = 0; /* No meaningful elements left */
866 asn_set_empty(list); /* Remove (list->array) */
868 specs = (const asn_SET_OF_specifics_t *)td->specifics;
869 ctx = (asn_struct_ctx_t *)((char *)ptr + specs->ctx_offset);
871 ASN_STRUCT_FREE(*elm->type, ctx->ptr);
876 case ASFM_FREE_EVERYTHING:
879 case ASFM_FREE_UNDERLYING:
881 case ASFM_FREE_UNDERLYING_AND_RESET:
882 memset(ptr, 0, specs->struct_size);
889 SET_OF_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
890 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
891 const asn_TYPE_member_t *elm = td->elements;
892 asn_constr_check_f *constr;
893 const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
897 ASN__CTFAIL(app_key, td, sptr,
898 "%s: value not given (%s:%d)",
899 td->name, __FILE__, __LINE__);
903 constr = elm->encoding_constraints.general_constraints;
904 if(!constr) constr = elm->type->encoding_constraints.general_constraints;
907 * Iterate over the members of an array.
908 * Validate each in turn, until one fails.
910 for(i = 0; i < list->count; i++) {
911 const void *memb_ptr = list->array[i];
914 if(!memb_ptr) continue;
916 ret = constr(elm->type, memb_ptr, ctfailcb, app_key);
923 #ifndef ASN_DISABLE_PER_SUPPORT
926 SET_OF_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
927 const asn_TYPE_descriptor_t *td,
928 const asn_per_constraints_t *constraints, void **sptr,
929 asn_per_data_t *pd) {
930 asn_dec_rval_t rv = {RC_OK, 0};
931 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
932 const asn_TYPE_member_t *elm = td->elements; /* Single one */
934 asn_anonymous_set_ *list;
935 const asn_per_constraint_t *ct;
939 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
943 * Create the target structure if it is not present already.
946 st = *sptr = CALLOC(1, specs->struct_size);
947 if(!st) ASN__DECODE_FAILED;
949 list = _A_SET_FROM_VOID(st);
951 /* Figure out which constraints to use */
952 if(constraints) ct = &constraints->size;
953 else if(td->encoding_constraints.per_constraints)
954 ct = &td->encoding_constraints.per_constraints->size;
957 if(ct && ct->flags & APC_EXTENSIBLE) {
958 int value = per_get_few_bits(pd, 1);
959 if(value < 0) ASN__DECODE_STARVED;
960 if(value) ct = 0; /* Not restricted! */
963 if(ct && ct->effective_bits >= 0) {
964 /* X.691, #19.5: No length determinant */
965 nelems = per_get_few_bits(pd, ct->effective_bits);
966 ASN_DEBUG("Preparing to fetch %ld+%ld elements from %s",
967 (long)nelems, ct->lower_bound, td->name);
968 if(nelems < 0) ASN__DECODE_STARVED;
969 nelems += ct->lower_bound;
977 nelems = uper_get_length(pd, -1, 0, &repeat);
978 ASN_DEBUG("Got to decode %" ASN_PRI_SSIZE " elements (eff %d)",
979 nelems, (int)(ct ? ct->effective_bits : -1));
980 if(nelems < 0) ASN__DECODE_STARVED;
983 for(i = 0; i < nelems; i++) {
985 ASN_DEBUG("SET OF %s decoding", elm->type->name);
986 rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
987 elm->encoding_constraints.per_constraints, &ptr, pd);
988 ASN_DEBUG("%s SET OF %s decoded %d, %p",
989 td->name, elm->type->name, rv.code, ptr);
990 if(rv.code == RC_OK) {
991 if(ASN_SET_ADD(list, ptr) == 0) {
992 if(rv.consumed == 0 && nelems > 200) {
993 /* Protect from SET OF NULL compression bombs. */
998 ASN_DEBUG("Failed to add element into %s",
1003 ASN_DEBUG("Failed decoding %s of %s (SET OF)",
1004 elm->type->name, td->name);
1006 if(ptr) ASN_STRUCT_FREE(*elm->type, ptr);
1010 nelems = -1; /* Allow uper_get_length() */
1013 ASN_DEBUG("Decoded %s as SET OF", td->name);
1021 SET_OF_encode_uper(const asn_TYPE_descriptor_t *td,
1022 const asn_per_constraints_t *constraints, const void *sptr,
1023 asn_per_outp_t *po) {
1024 const asn_anonymous_set_ *list;
1025 const asn_per_constraint_t *ct;
1026 const asn_TYPE_member_t *elm = td->elements;
1027 struct _el_buffer *encoded_els;
1028 asn_enc_rval_t er = {0,0,0};
1031 if(!sptr) ASN__ENCODE_FAILED;
1033 list = _A_CSET_FROM_VOID(sptr);
1037 ASN_DEBUG("Encoding %s as SEQUENCE OF (%d)", td->name, list->count);
1039 if(constraints) ct = &constraints->size;
1040 else if(td->encoding_constraints.per_constraints)
1041 ct = &td->encoding_constraints.per_constraints->size;
1044 /* If extensible constraint, check if size is in root */
1047 (list->count < ct->lower_bound || list->count > ct->upper_bound);
1048 ASN_DEBUG("lb %ld ub %ld %s", ct->lower_bound, ct->upper_bound,
1049 ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
1050 if(ct->flags & APC_EXTENSIBLE) {
1051 /* Declare whether size is in extension root */
1052 if(per_put_few_bits(po, not_in_root, 1)) ASN__ENCODE_FAILED;
1053 if(not_in_root) ct = 0;
1054 } else if(not_in_root && ct->effective_bits >= 0) {
1060 if(ct && ct->effective_bits >= 0) {
1061 /* X.691, #19.5: No length determinant */
1062 if(per_put_few_bits(po, list->count - ct->lower_bound,
1063 ct->effective_bits))
1065 } else if(list->count == 0) {
1066 /* When the list is empty add only the length determinant
1067 * X.691, #20.6 and #11.9.4.1
1069 if (uper_put_length(po, 0, 0)) {
1072 ASN__ENCODED_OK(er);
1077 * Canonical UPER #22.1 mandates dynamic sorting of the SET OF elements
1078 * according to their encodings. Build an array of the encoded elements.
1080 encoded_els = SET_OF__encode_sorted(elm, list, SOES_CUPER);
1082 for(encoded_edx = 0; (ssize_t)encoded_edx < list->count;) {
1087 if(ct && ct->effective_bits >= 0) {
1088 may_encode = list->count;
1091 uper_put_length(po, list->count - encoded_edx, &need_eom);
1092 if(may_encode < 0) ASN__ENCODE_FAILED;
1095 for(edx = encoded_edx; edx < encoded_edx + may_encode; edx++) {
1096 const struct _el_buffer *el = &encoded_els[edx];
1097 if(asn_put_many_bits(po, el->buf,
1098 (8 * el->length) - el->bits_unused) < 0) {
1103 if(need_eom && uper_put_length(po, 0, 0))
1104 ASN__ENCODE_FAILED; /* End of Message length */
1106 encoded_edx += may_encode;
1109 SET_OF__encode_sorted_free(encoded_els, list->count);
1111 if((ssize_t)encoded_edx == list->count) {
1112 ASN__ENCODED_OK(er);
1119 SET_OF_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
1120 const asn_TYPE_descriptor_t *td,
1121 const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
1122 asn_dec_rval_t rv = {RC_OK, 0};
1123 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
1124 const asn_TYPE_member_t *elm = td->elements; /* Single one */
1126 asn_anonymous_set_ *list;
1127 const asn_per_constraint_t *ct;
1131 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
1135 * Create the target structure if it is not present already.
1138 st = *sptr = CALLOC(1, specs->struct_size);
1139 if(!st) ASN__DECODE_FAILED;
1141 list = _A_SET_FROM_VOID(st);
1143 /* Figure out which constraints to use */
1144 if(constraints) ct = &constraints->size;
1145 else if(td->encoding_constraints.per_constraints)
1146 ct = &td->encoding_constraints.per_constraints->size;
1149 if(ct && ct->flags & APC_EXTENSIBLE) {
1150 int value = per_get_few_bits(pd, 1);
1151 if(value < 0) ASN__DECODE_STARVED;
1152 if(value) ct = 0; /* Not restricted! */
1155 if(ct && ct->effective_bits >= 0) {
1156 /* X.691, #19.5: No length determinant */
1157 nelems = aper_get_nsnnwn(pd, ct->upper_bound - ct->lower_bound + 1);
1158 ASN_DEBUG("Preparing to fetch %ld+%ld elements from %s",
1159 (long)nelems, ct->lower_bound, td->name);
1160 if(nelems < 0) ASN__DECODE_STARVED;
1161 nelems += ct->lower_bound;
1169 nelems = aper_get_length(pd, ct ? ct->upper_bound - ct->lower_bound + 1 : -1,
1170 ct ? ct->effective_bits : -1, &repeat);
1171 ASN_DEBUG("Got to decode %d elements (eff %d)",
1172 (int)nelems, (int)(ct ? ct->effective_bits : -1));
1173 if(nelems < 0) ASN__DECODE_STARVED;
1176 for(i = 0; i < nelems; i++) {
1178 ASN_DEBUG("SET OF %s decoding", elm->type->name);
1179 rv = elm->type->op->aper_decoder(opt_codec_ctx, elm->type,
1180 elm->encoding_constraints.per_constraints, &ptr, pd);
1181 ASN_DEBUG("%s SET OF %s decoded %d, %p",
1182 td->name, elm->type->name, rv.code, ptr);
1183 if(rv.code == RC_OK) {
1184 if(ASN_SET_ADD(list, ptr) == 0)
1186 ASN_DEBUG("Failed to add element into %s",
1191 ASN_DEBUG("Failed decoding %s of %s (SET OF)",
1192 elm->type->name, td->name);
1194 if(ptr) ASN_STRUCT_FREE(*elm->type, ptr);
1198 nelems = -1; /* Allow uper_get_length() */
1201 ASN_DEBUG("Decoded %s as SET OF", td->name);
1208 #endif /* ASN_DISABLE_PER_SUPPORT */
1210 struct comparable_ptr {
1211 const asn_TYPE_descriptor_t *td;
1216 SET_OF__compare_cb(const void *aptr, const void *bptr) {
1217 const struct comparable_ptr *a = aptr;
1218 const struct comparable_ptr *b = bptr;
1219 assert(a->td == b->td);
1220 return a->td->op->compare_struct(a->td, a->sptr, b->sptr);
1224 SET_OF_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1226 const asn_anonymous_set_ *a = _A_CSET_FROM_VOID(aptr);
1227 const asn_anonymous_set_ *b = _A_CSET_FROM_VOID(bptr);
1230 struct comparable_ptr *asorted;
1231 struct comparable_ptr *bsorted;
1232 ssize_t common_length;
1236 if(b->count) return -1;
1238 } else if(b->count == 0) {
1242 asorted = MALLOC(a->count * sizeof(asorted[0]));
1243 bsorted = MALLOC(b->count * sizeof(bsorted[0]));
1244 if(!asorted || !bsorted) {
1250 for(idx = 0; idx < a->count; idx++) {
1251 asorted[idx].td = td->elements->type;
1252 asorted[idx].sptr = a->array[idx];
1255 for(idx = 0; idx < b->count; idx++) {
1256 bsorted[idx].td = td->elements->type;
1257 bsorted[idx].sptr = b->array[idx];
1260 qsort(asorted, a->count, sizeof(asorted[0]), SET_OF__compare_cb);
1261 qsort(bsorted, b->count, sizeof(bsorted[0]), SET_OF__compare_cb);
1263 common_length = (a->count < b->count ? a->count : b->count);
1264 for(idx = 0; idx < common_length; idx++) {
1265 int ret = td->elements->type->op->compare_struct(
1266 td->elements->type, asorted[idx].sptr, bsorted[idx].sptr);
1277 if(idx < b->count) /* more elements in b */
1278 return -1; /* a is shorter, so put it first */
1279 if(idx < a->count) return 1;
1290 asn_TYPE_operation_t asn_OP_SET_OF = {
1298 #ifdef ASN_DISABLE_OER_SUPPORT
1305 #ifdef ASN_DISABLE_PER_SUPPORT
1314 0, /* SET_OF_encode_aper */
1315 #endif /* ASN_DISABLE_PER_SUPPORT */
1317 0 /* Use generic outmost tag fetcher */
1321 asn_random_fill_result_t
1322 SET_OF_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1323 const asn_encoding_constraints_t *constraints,
1324 size_t max_length) {
1325 const asn_SET_OF_specifics_t *specs =
1326 (const asn_SET_OF_specifics_t *)td->specifics;
1327 asn_random_fill_result_t res_ok = {ARFILL_OK, 0};
1328 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1329 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1330 const asn_TYPE_member_t *elm = td->elements;
1332 long max_elements = 5;
1333 long slb = 0; /* Lower size bound */
1334 long sub = 0; /* Upper size bound */
1337 if(max_length == 0) return result_skipped;
1340 st = (*sptr = CALLOC(1, specs->struct_size));
1342 return result_failed;
1346 switch(asn_random_between(0, 6)) {
1347 case 0: max_elements = 0; break;
1348 case 1: max_elements = 1; break;
1349 case 2: max_elements = 5; break;
1350 case 3: max_elements = max_length; break;
1351 case 4: max_elements = max_length / 2; break;
1352 case 5: max_elements = max_length / 4; break;
1355 sub = slb + max_elements;
1357 if(!constraints || !constraints->per_constraints)
1358 constraints = &td->encoding_constraints;
1359 if(constraints->per_constraints) {
1360 const asn_per_constraint_t *pc = &constraints->per_constraints->size;
1361 if(pc->flags & APC_SEMI_CONSTRAINED) {
1362 slb = pc->lower_bound;
1363 sub = pc->lower_bound + max_elements;
1364 } else if(pc->flags & APC_CONSTRAINED) {
1365 slb = pc->lower_bound;
1366 sub = pc->upper_bound;
1367 if(sub - slb > max_elements) sub = slb + max_elements;
1371 /* Bias towards edges of allowed space */
1372 switch(asn_random_between(-1, 4)) {
1375 /* Prepare lengths somewhat outside of constrained range. */
1376 if(constraints->per_constraints
1377 && (constraints->per_constraints->size.flags & APC_EXTENSIBLE)) {
1378 switch(asn_random_between(0, 5)) {
1391 rnd_len = asn_random_between(0, slb);
1394 if(sub < (ssize_t)max_length) {
1397 rnd_len = max_length;
1401 if(sub < (ssize_t)max_length) {
1402 rnd_len = asn_random_between(sub + 1, max_length);
1404 rnd_len = max_length;
1408 rnd_len = max_length;
1415 rnd_len = asn_random_between(slb, sub);
1419 rnd_len = asn_random_between(slb + 1, sub);
1424 rnd_len = asn_random_between(slb, slb);
1428 rnd_len = asn_random_between(slb, sub - 1);
1433 rnd_len = asn_random_between(sub, sub);
1437 for(; rnd_len > 0; rnd_len--) {
1438 asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
1440 asn_random_fill_result_t tmpres = elm->type->op->random_fill(
1441 elm->type, &ptr, &elm->encoding_constraints,
1442 (max_length > res_ok.length ? max_length - res_ok.length : 0)
1444 switch(tmpres.code) {
1446 ASN_SET_ADD(list, ptr);
1447 res_ok.length += tmpres.length;
1449 case ARFILL_SKIPPED: