+asn_enc_rval_t
+SET_OF_encode_uper(const asn_TYPE_descriptor_t *td,
+ const asn_per_constraints_t *constraints, const void *sptr,
+ asn_per_outp_t *po) {
+ const asn_anonymous_set_ *list;
+ const asn_per_constraint_t *ct;
+ const asn_TYPE_member_t *elm = td->elements;
+ struct _el_buffer *encoded_els;
+ asn_enc_rval_t er;
+ size_t encoded_edx;
+
+ if(!sptr) ASN__ENCODE_FAILED;
+
+ list = _A_CSET_FROM_VOID(sptr);
+
+ er.encoded = 0;
+
+ ASN_DEBUG("Encoding %s as SEQUENCE OF (%d)", td->name, list->count);
+
+ if(constraints) ct = &constraints->size;
+ else if(td->encoding_constraints.per_constraints)
+ ct = &td->encoding_constraints.per_constraints->size;
+ else ct = 0;
+
+ /* If extensible constraint, check if size is in root */
+ if(ct) {
+ int not_in_root =
+ (list->count < ct->lower_bound || list->count > ct->upper_bound);
+ ASN_DEBUG("lb %ld ub %ld %s", ct->lower_bound, ct->upper_bound,
+ ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
+ if(ct->flags & APC_EXTENSIBLE) {
+ /* Declare whether size is in extension root */
+ if(per_put_few_bits(po, not_in_root, 1)) ASN__ENCODE_FAILED;
+ if(not_in_root) ct = 0;
+ } else if(not_in_root && ct->effective_bits >= 0) {
+ ASN__ENCODE_FAILED;
+ }
+
+ }
+
+ if(ct && ct->effective_bits >= 0) {
+ /* X.691, #19.5: No length determinant */
+ if(per_put_few_bits(po, list->count - ct->lower_bound,
+ ct->effective_bits))
+ ASN__ENCODE_FAILED;
+ } else if(list->count == 0) {
+ /* When the list is empty add only the length determinant
+ * X.691, #20.6 and #11.9.4.1
+ */
+ if (uper_put_length(po, 0, 0)) {
+ ASN__ENCODE_FAILED;
+ }
+ ASN__ENCODED_OK(er);
+ }
+
+
+ /*
+ * Canonical UPER #22.1 mandates dynamic sorting of the SET OF elements
+ * according to their encodings. Build an array of the encoded elements.
+ */
+ encoded_els = SET_OF__encode_sorted(elm, list, SOES_CUPER);
+
+ for(encoded_edx = 0; (ssize_t)encoded_edx < list->count;) {
+ ssize_t may_encode;
+ size_t edx;
+ int need_eom = 0;
+
+ if(ct && ct->effective_bits >= 0) {
+ may_encode = list->count;
+ } else {
+ may_encode =
+ uper_put_length(po, list->count - encoded_edx, &need_eom);
+ if(may_encode < 0) ASN__ENCODE_FAILED;
+ }
+
+ for(edx = encoded_edx; edx < encoded_edx + may_encode; edx++) {
+ const struct _el_buffer *el = &encoded_els[edx];
+ if(asn_put_many_bits(po, el->buf,
+ (8 * el->length) - el->bits_unused) < 0) {
+ break;
+ }
+ }
+
+ if(need_eom && uper_put_length(po, 0, 0))
+ ASN__ENCODE_FAILED; /* End of Message length */
+
+ encoded_edx += may_encode;
+ }
+
+ SET_OF__encode_sorted_free(encoded_els, list->count);
+
+ if((ssize_t)encoded_edx == list->count) {
+ ASN__ENCODED_OK(er);
+ } else {
+ ASN__ENCODE_FAILED;
+ }
+}
+
+
+#endif /* ASN_DISABLE_PER_SUPPORT */
+
+struct comparable_ptr {
+ const asn_TYPE_descriptor_t *td;
+ const void *sptr;
+};
+
+static int
+SET_OF__compare_cb(const void *aptr, const void *bptr) {
+ const struct comparable_ptr *a = aptr;
+ const struct comparable_ptr *b = bptr;
+ assert(a->td == b->td);
+ return a->td->op->compare_struct(a->td, a->sptr, b->sptr);
+}
+
+int
+SET_OF_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
+ const void *bptr) {
+ const asn_anonymous_set_ *a = _A_CSET_FROM_VOID(aptr);
+ const asn_anonymous_set_ *b = _A_CSET_FROM_VOID(bptr);
+
+ if(a && b) {
+ struct comparable_ptr *asorted;
+ struct comparable_ptr *bsorted;
+ ssize_t common_length;
+ ssize_t idx;
+
+ if(a->count == 0) {
+ if(b->count) return -1;
+ return 0;
+ } else if(b->count == 0) {
+ return 1;
+ }
+
+ asorted = MALLOC(a->count * sizeof(asorted[0]));
+ bsorted = MALLOC(b->count * sizeof(bsorted[0]));
+ if(!asorted || !bsorted) {
+ FREEMEM(asorted);
+ FREEMEM(bsorted);
+ return -1;
+ }
+
+ for(idx = 0; idx < a->count; idx++) {
+ asorted[idx].td = td->elements->type;
+ asorted[idx].sptr = a->array[idx];
+ }
+
+ for(idx = 0; idx < b->count; idx++) {
+ bsorted[idx].td = td->elements->type;
+ bsorted[idx].sptr = b->array[idx];
+ }
+
+ qsort(asorted, a->count, sizeof(asorted[0]), SET_OF__compare_cb);
+ qsort(bsorted, b->count, sizeof(bsorted[0]), SET_OF__compare_cb);
+
+ common_length = (a->count < b->count ? a->count : b->count);
+ for(idx = 0; idx < common_length; idx++) {
+ int ret = td->elements->type->op->compare_struct(
+ td->elements->type, asorted[idx].sptr, bsorted[idx].sptr);
+ if(ret) {
+ FREEMEM(asorted);
+ FREEMEM(bsorted);
+ return ret;
+ }
+ }
+
+ FREEMEM(asorted);
+ FREEMEM(bsorted);
+
+ if(idx < b->count) /* more elements in b */
+ return -1; /* a is shorter, so put it first */
+ if(idx < a->count) return 1;
+ } else if(!a) {
+ return -1;
+ } else if(!b) {
+ return 1;
+ }
+
+ return 0;
+}
+
+
+asn_TYPE_operation_t asn_OP_SET_OF = {
+ SET_OF_free,
+ SET_OF_print,
+ SET_OF_compare,
+ SET_OF_decode_ber,
+ SET_OF_encode_der,
+ SET_OF_decode_xer,
+ SET_OF_encode_xer,
+#ifdef ASN_DISABLE_OER_SUPPORT
+ 0,
+ 0,
+#else
+ SET_OF_decode_oer,
+ SET_OF_encode_oer,
+#endif
+#ifdef ASN_DISABLE_PER_SUPPORT
+ 0,
+ 0,
+#else
+ SET_OF_decode_uper,
+ SET_OF_encode_uper,
+#endif /* ASN_DISABLE_PER_SUPPORT */
+ SET_OF_random_fill,
+ 0 /* Use generic outmost tag fetcher */
+};
+
+
+asn_random_fill_result_t
+SET_OF_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
+ const asn_encoding_constraints_t *constraints,
+ size_t max_length) {
+ const asn_SET_OF_specifics_t *specs =
+ (const asn_SET_OF_specifics_t *)td->specifics;
+ asn_random_fill_result_t res_ok = {ARFILL_OK, 0};
+ asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
+ asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
+ const asn_TYPE_member_t *elm = td->elements;
+ void *st = *sptr;
+ long max_elements = 5;
+ long slb = 0; /* Lower size bound */
+ long sub = 0; /* Upper size bound */
+ size_t rnd_len;
+
+ if(max_length == 0) return result_skipped;
+
+ if(st == NULL) {
+ st = (*sptr = CALLOC(1, specs->struct_size));
+ if(st == NULL) {
+ return result_failed;
+ }
+ }
+
+ switch(asn_random_between(0, 6)) {
+ case 0: max_elements = 0; break;
+ case 1: max_elements = 1; break;
+ case 2: max_elements = 5; break;
+ case 3: max_elements = max_length; break;
+ case 4: max_elements = max_length / 2; break;
+ case 5: max_elements = max_length / 4; break;
+ default: break;
+ }
+ sub = slb + max_elements;
+
+ if(!constraints || !constraints->per_constraints)
+ constraints = &td->encoding_constraints;
+ if(constraints->per_constraints) {
+ const asn_per_constraint_t *pc = &constraints->per_constraints->size;
+ if(pc->flags & APC_SEMI_CONSTRAINED) {
+ slb = pc->lower_bound;
+ sub = pc->lower_bound + max_elements;
+ } else if(pc->flags & APC_CONSTRAINED) {
+ slb = pc->lower_bound;
+ sub = pc->upper_bound;
+ if(sub - slb > max_elements) sub = slb + max_elements;
+ }
+ }
+
+ /* Bias towards edges of allowed space */
+ switch(asn_random_between(-1, 4)) {
+ default:
+ case -1:
+ /* Prepare lengths somewhat outside of constrained range. */
+ if(constraints->per_constraints
+ && (constraints->per_constraints->size.flags & APC_EXTENSIBLE)) {
+ switch(asn_random_between(0, 5)) {
+ default:
+ case 0:
+ rnd_len = 0;
+ break;
+ case 1:
+ if(slb > 0) {
+ rnd_len = slb - 1;
+ } else {
+ rnd_len = 0;
+ }
+ break;
+ case 2:
+ rnd_len = asn_random_between(0, slb);
+ break;
+ case 3:
+ if(sub < (ssize_t)max_length) {
+ rnd_len = sub + 1;
+ } else {
+ rnd_len = max_length;
+ }
+ break;
+ case 4:
+ if(sub < (ssize_t)max_length) {
+ rnd_len = asn_random_between(sub + 1, max_length);
+ } else {
+ rnd_len = max_length;
+ }
+ break;
+ case 5:
+ rnd_len = max_length;
+ break;
+ }
+ break;
+ }
+ /* Fall through */
+ case 0:
+ rnd_len = asn_random_between(slb, sub);
+ break;
+ case 1:
+ if(slb < sub) {
+ rnd_len = asn_random_between(slb + 1, sub);
+ break;
+ }
+ /* Fall through */
+ case 2:
+ rnd_len = asn_random_between(slb, slb);
+ break;
+ case 3:
+ if(slb < sub) {
+ rnd_len = asn_random_between(slb, sub - 1);
+ break;
+ }
+ /* Fall through */
+ case 4:
+ rnd_len = asn_random_between(sub, sub);
+ break;
+ }
+
+ for(; rnd_len > 0; rnd_len--) {
+ asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
+ void *ptr = 0;
+ asn_random_fill_result_t tmpres = elm->type->op->random_fill(
+ elm->type, &ptr, &elm->encoding_constraints,
+ (max_length > res_ok.length ? max_length - res_ok.length : 0)
+ / rnd_len);
+ switch(tmpres.code) {
+ case ARFILL_OK:
+ ASN_SET_ADD(list, ptr);
+ res_ok.length += tmpres.length;
+ break;
+ case ARFILL_SKIPPED:
+ break;
+ case ARFILL_FAILED:
+ assert(ptr == 0);
+ return tmpres;
+ }
+ }
+
+ return res_ok;
+}
+