2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <constr_SET_OF.h>
10 SET_OF_encode_aper(const asn_TYPE_descriptor_t *td,
11 const asn_per_constraints_t *constraints, const void *sptr,
13 const asn_anonymous_set_ *list;
14 const asn_per_constraint_t *ct;
15 const asn_TYPE_member_t *elm = td->elements;
16 struct _el_buffer *encoded_els;
17 asn_enc_rval_t er = {0,0,0};
20 if(!sptr) ASN__ENCODE_FAILED;
22 list = _A_CSET_FROM_VOID(sptr);
26 ASN_DEBUG("Encoding %s as SET OF (%d)", td->name, list->count);
28 if(constraints) ct = &constraints->size;
29 else if(td->encoding_constraints.per_constraints)
30 ct = &td->encoding_constraints.per_constraints->size;
33 /* If extensible constraint, check if size is in root */
36 (list->count < ct->lower_bound || list->count > ct->upper_bound);
37 ASN_DEBUG("lb %lld ub %lld %s",
38 (long long int)ct->lower_bound,
39 (long long int)ct->upper_bound,
40 ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
41 if(ct->flags & APC_EXTENSIBLE) {
42 /* Declare whether size is in extension root */
43 if(per_put_few_bits(po, not_in_root, 1)) ASN__ENCODE_FAILED;
44 if(not_in_root) ct = 0;
45 } else if(not_in_root && ct->effective_bits >= 0) {
51 if(ct && ct->effective_bits >= 0) {
52 /* X.691, #19.5: No length determinant */
53 /*if(per_put_few_bits(po, list->count - ct->lower_bound,
57 if (aper_put_length(po, ct->lower_bound, ct->upper_bound, list->count - ct->lower_bound, 0) < 0) {
63 * Canonical PER #22.1 mandates dynamic sorting of the SET OF elements
64 * according to their encodings. Build an array of the encoded elements.
66 encoded_els = SET_OF__encode_sorted(elm, list, SOES_CAPER);
68 for(seq = 0; seq < list->count;) {
71 if(ct && ct->effective_bits >= 0) {
72 may_encode = list->count;
75 aper_put_length(po, -1, -1, list->count - seq, &need_eom);
76 if(may_encode < 0) ASN__ENCODE_FAILED;
80 const struct _el_buffer *el = &encoded_els[seq++];
81 if(asn_put_many_bits(po, el->buf,
82 (8 * el->length) - el->bits_unused) < 0) {
86 if(need_eom && (aper_put_length(po, -1, -1, 0, NULL) < 0))
87 ASN__ENCODE_FAILED; /* End of Message length */
90 SET_OF__encode_sorted_free(encoded_els, list->count);
96 SET_OF_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
97 const asn_TYPE_descriptor_t *td,
98 const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
99 asn_dec_rval_t rv = {RC_OK, 0};
100 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
101 const asn_TYPE_member_t *elm = td->elements; /* Single one */
103 asn_anonymous_set_ *list;
104 const asn_per_constraint_t *ct;
108 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
112 * Create the target structure if it is not present already.
115 st = *sptr = CALLOC(1, specs->struct_size);
116 if(!st) ASN__DECODE_FAILED;
118 list = _A_SET_FROM_VOID(st);
120 /* Figure out which constraints to use */
121 if(constraints) ct = &constraints->size;
122 else if(td->encoding_constraints.per_constraints)
123 ct = &td->encoding_constraints.per_constraints->size;
126 if(ct && ct->flags & APC_EXTENSIBLE) {
127 int value = per_get_few_bits(pd, 1);
128 if(value < 0) ASN__DECODE_STARVED;
129 if(value) ct = 0; /* Not restricted! */
132 if(ct && ct->effective_bits >= 0) {
133 /* X.691, #19.5: No length determinant */
134 nelems = aper_get_nsnnwn(pd, ct->upper_bound - ct->lower_bound + 1);
135 ASN_DEBUG("Preparing to fetch %ld+%lld elements from %s",
136 (long)nelems, (long long int)ct->lower_bound, td->name);
137 if(nelems < 0) ASN__DECODE_STARVED;
138 nelems += ct->lower_bound;
147 nelems = aper_get_length(pd, ct->lower_bound, ct->upper_bound,
148 ct->effective_bits, &repeat);
150 nelems = aper_get_length(pd, -1, -1, -1, &repeat);
151 ASN_DEBUG("Got to decode %d elements (eff %d)",
152 (int)nelems, (int)(ct ? ct->effective_bits : -1));
153 if(nelems < 0) ASN__DECODE_STARVED;
156 for(i = 0; i < nelems; i++) {
158 ASN_DEBUG("SET OF %s decoding", elm->type->name);
159 rv = elm->type->op->aper_decoder(opt_codec_ctx, elm->type,
160 elm->encoding_constraints.per_constraints, &ptr, pd);
161 ASN_DEBUG("%s SET OF %s decoded %d, %p",
162 td->name, elm->type->name, rv.code, ptr);
163 if(rv.code == RC_OK) {
164 if(ASN_SET_ADD(list, ptr) == 0)
166 ASN_DEBUG("Failed to add element into %s",
171 ASN_DEBUG("Failed decoding %s of %s (SET OF)",
172 elm->type->name, td->name);
174 if(ptr) ASN_STRUCT_FREE(*elm->type, ptr);
178 nelems = -1; /* Allow uper_get_length() */
181 ASN_DEBUG("Decoded %s as SET OF", td->name);