SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / constr_SET_OF_aper.c
1 /*
2  * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
6 #include <asn_internal.h>
7 #include <constr_SET_OF.h>
8
9 asn_enc_rval_t
10 SET_OF_encode_aper(const asn_TYPE_descriptor_t *td,
11                    const asn_per_constraints_t *constraints, const void *sptr,
12                    asn_per_outp_t *po) {
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};
18     int seq;
19
20     if(!sptr) ASN__ENCODE_FAILED;
21
22     list = _A_CSET_FROM_VOID(sptr);
23
24     er.encoded = 0;
25
26     ASN_DEBUG("Encoding %s as SET OF (%d)", td->name, list->count);
27
28     if(constraints) ct = &constraints->size;
29     else if(td->encoding_constraints.per_constraints)
30         ct = &td->encoding_constraints.per_constraints->size;
31     else ct = 0;
32
33     /* If extensible constraint, check if size is in root */
34     if(ct) {
35         int not_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) {
46             ASN__ENCODE_FAILED;
47         }
48
49     }
50
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,
54                             ct->effective_bits))
55             ASN__ENCODE_FAILED;*/
56
57         if (aper_put_length(po, ct->lower_bound, ct->upper_bound, list->count - ct->lower_bound, 0) < 0) {
58             ASN__ENCODE_FAILED;
59         }
60     }
61
62     /*
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.
65      */
66     encoded_els = SET_OF__encode_sorted(elm, list, SOES_CAPER);
67
68     for(seq = 0; seq < list->count;) {
69         ssize_t may_encode;
70         int need_eom = 0;
71         if(ct && ct->effective_bits >= 0) {
72             may_encode = list->count;
73         } else {
74             may_encode =
75                 aper_put_length(po, -1, -1, list->count - seq, &need_eom);
76             if(may_encode < 0) ASN__ENCODE_FAILED;
77         }
78
79         while(may_encode--) {
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) {
83                 break;
84             }
85         }
86         if(need_eom && (aper_put_length(po, -1, -1, 0, NULL) < 0))
87             ASN__ENCODE_FAILED;  /* End of Message length */
88     }
89
90     SET_OF__encode_sorted_free(encoded_els, list->count);
91
92     ASN__ENCODED_OK(er);
93 }
94
95 asn_dec_rval_t
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 */
102     void *st = *sptr;
103     asn_anonymous_set_ *list;
104     const asn_per_constraint_t *ct;
105     int repeat = 0;
106     ssize_t nelems;
107
108     if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
109         ASN__DECODE_FAILED;
110
111     /*
112      * Create the target structure if it is not present already.
113      */
114     if(!st) {
115         st = *sptr = CALLOC(1, specs->struct_size);
116         if(!st) ASN__DECODE_FAILED;
117     }
118     list = _A_SET_FROM_VOID(st);
119
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;
124     else ct = 0;
125
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! */
130     }
131
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;
139     } else {
140         nelems = -1;
141     }
142
143     do {
144         int i;
145         if(nelems < 0) {
146             if (ct)
147                 nelems = aper_get_length(pd, ct->lower_bound, ct->upper_bound,
148                                          ct->effective_bits, &repeat);
149             else
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;
154         }
155
156         for(i = 0; i < nelems; i++) {
157             void *ptr = 0;
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)
165                     continue;
166                 ASN_DEBUG("Failed to add element into %s",
167                           td->name);
168                 /* Fall through */
169                 rv.code = RC_FAIL;
170             } else {
171                 ASN_DEBUG("Failed decoding %s of %s (SET OF)",
172                           elm->type->name, td->name);
173             }
174             if(ptr) ASN_STRUCT_FREE(*elm->type, ptr);
175             return rv;
176         }
177
178         nelems = -1;  /* Allow uper_get_length() */
179     } while(repeat);
180
181     ASN_DEBUG("Decoded %s as SET OF", td->name);
182
183     rv.code = RC_OK;
184     rv.consumed = 0;
185     return rv;
186 }