Moving in e2sim originally from it/test/simulators
[sim/e2-interface.git] / e2sim / ASN1c / constr_SEQUENCE_OF.c
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2019 AT&T Intellectual Property                                  *
4 #                                                                            *
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                                    *
8 #                                                                            *
9 #      http://www.apache.org/licenses/LICENSE-2.0                            *
10 #                                                                            *
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.                                             *
16 #                                                                            *
17 ******************************************************************************/
18
19 /*-
20  * Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>.
21  * All rights reserved.
22  * Redistribution and modifications are permitted subject to BSD license.
23  */
24 #include <asn_internal.h>
25 #include <constr_SEQUENCE_OF.h>
26 #include <asn_SEQUENCE_OF.h>
27
28 /*
29  * The DER encoder of the SEQUENCE OF type.
30  */
31 asn_enc_rval_t
32 SEQUENCE_OF_encode_der(const asn_TYPE_descriptor_t *td, const void *ptr,
33                        int tag_mode, ber_tlv_tag_t tag,
34                        asn_app_consume_bytes_f *cb, void *app_key) {
35     asn_TYPE_member_t *elm = td->elements;
36         const asn_anonymous_sequence_ *list = _A_CSEQUENCE_FROM_VOID(ptr);
37         size_t computed_size = 0;
38         ssize_t encoding_size = 0;
39         asn_enc_rval_t erval = {0,0,0};
40         int edx;
41
42         ASN_DEBUG("Estimating size of SEQUENCE OF %s", td->name);
43
44         /*
45          * Gather the length of the underlying members sequence.
46          */
47         for(edx = 0; edx < list->count; edx++) {
48                 void *memb_ptr = list->array[edx];
49                 if(!memb_ptr) continue;
50                 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
51                         0, elm->tag,
52                         0, 0);
53                 if(erval.encoded == -1)
54                         return erval;
55                 computed_size += erval.encoded;
56         }
57
58         /*
59          * Encode the TLV for the sequence itself.
60          */
61         encoding_size = der_write_tags(td, computed_size, tag_mode, 1, tag,
62                 cb, app_key);
63         if(encoding_size == -1) {
64                 erval.encoded = -1;
65                 erval.failed_type = td;
66                 erval.structure_ptr = ptr;
67                 return erval;
68         }
69
70         computed_size += encoding_size;
71         if(!cb) {
72                 erval.encoded = computed_size;
73                 ASN__ENCODED_OK(erval);
74         }
75
76         ASN_DEBUG("Encoding members of SEQUENCE OF %s", td->name);
77
78         /*
79          * Encode all members.
80          */
81         for(edx = 0; edx < list->count; edx++) {
82                 void *memb_ptr = list->array[edx];
83                 if(!memb_ptr) continue;
84                 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
85                         0, elm->tag,
86                         cb, app_key);
87                 if(erval.encoded == -1)
88                         return erval;
89                 encoding_size += erval.encoded;
90         }
91
92         if(computed_size != (size_t)encoding_size) {
93                 /*
94                  * Encoded size is not equal to the computed size.
95                  */
96                 erval.encoded = -1;
97                 erval.failed_type = td;
98                 erval.structure_ptr = ptr;
99         } else {
100                 erval.encoded = computed_size;
101                 erval.structure_ptr = 0;
102                 erval.failed_type = 0;
103         }
104
105         return erval;
106 }
107
108 asn_enc_rval_t
109 SEQUENCE_OF_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
110                        int ilevel, enum xer_encoder_flags_e flags,
111                        asn_app_consume_bytes_f *cb, void *app_key) {
112     asn_enc_rval_t er = {0,0,0};
113     const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
114     const asn_TYPE_member_t *elm = td->elements;
115     const asn_anonymous_sequence_ *list = _A_CSEQUENCE_FROM_VOID(sptr);
116     const char *mname = specs->as_XMLValueList
117                             ? 0
118                             : ((*elm->name) ? elm->name : elm->type->xml_tag);
119     size_t mlen = mname ? strlen(mname) : 0;
120     int xcan = (flags & XER_F_CANONICAL);
121     int i;
122
123     if(!sptr) ASN__ENCODE_FAILED;
124
125     er.encoded = 0;
126
127     for(i = 0; i < list->count; i++) {
128         asn_enc_rval_t tmper = {0,0,0};
129         void *memb_ptr = list->array[i];
130         if(!memb_ptr) continue;
131
132         if(mname) {
133             if(!xcan) ASN__TEXT_INDENT(1, ilevel);
134             ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
135         }
136
137         tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, ilevel + 1,
138                                            flags, cb, app_key);
139         if(tmper.encoded == -1) return tmper;
140         er.encoded += tmper.encoded;
141         if(tmper.encoded == 0 && specs->as_XMLValueList) {
142             const char *name = elm->type->xml_tag;
143             size_t len = strlen(name);
144             if(!xcan) ASN__TEXT_INDENT(1, ilevel + 1);
145             ASN__CALLBACK3("<", 1, name, len, "/>", 2);
146         }
147
148         if(mname) {
149             ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
150         }
151     }
152
153     if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
154
155     ASN__ENCODED_OK(er);
156 cb_failed:
157     ASN__ENCODE_FAILED;
158 }
159
160 #ifndef ASN_DISABLE_PER_SUPPORT
161
162 asn_enc_rval_t
163 SEQUENCE_OF_encode_uper(const asn_TYPE_descriptor_t *td,
164                         const asn_per_constraints_t *constraints,
165                         const void *sptr, asn_per_outp_t *po) {
166     const asn_anonymous_sequence_ *list;
167         const asn_per_constraint_t *ct;
168         asn_enc_rval_t er = {0,0,0};
169         const asn_TYPE_member_t *elm = td->elements;
170         size_t encoded_edx;
171
172         if(!sptr) ASN__ENCODE_FAILED;
173     list = _A_CSEQUENCE_FROM_VOID(sptr);
174
175     er.encoded = 0;
176
177         ASN_DEBUG("Encoding %s as SEQUENCE OF (%d)", td->name, list->count);
178
179     if(constraints) ct = &constraints->size;
180     else if(td->encoding_constraints.per_constraints)
181         ct = &td->encoding_constraints.per_constraints->size;
182     else ct = 0;
183
184     /* If extensible constraint, check if size is in root */
185     if(ct) {
186         int not_in_root =
187             (list->count < ct->lower_bound || list->count > ct->upper_bound);
188         ASN_DEBUG("lb %ld ub %ld %s", ct->lower_bound, ct->upper_bound,
189                   ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
190         if(ct->flags & APC_EXTENSIBLE) {
191             /* Declare whether size is in extension root */
192             if(per_put_few_bits(po, not_in_root, 1)) ASN__ENCODE_FAILED;
193             if(not_in_root) ct = 0;
194         } else if(not_in_root && ct->effective_bits >= 0) {
195             ASN__ENCODE_FAILED;
196         }
197
198     }
199
200     if(ct && ct->effective_bits >= 0) {
201         /* X.691, #19.5: No length determinant */
202         if(per_put_few_bits(po, list->count - ct->lower_bound,
203                             ct->effective_bits))
204             ASN__ENCODE_FAILED;
205     } else if(list->count == 0) {
206         /* When the list is empty add only the length determinant
207          * X.691, #20.6 and #11.9.4.1
208          */
209         if (uper_put_length(po, 0, 0)) {
210             ASN__ENCODE_FAILED;
211         }
212         ASN__ENCODED_OK(er);
213     }
214
215     for(encoded_edx = 0; (ssize_t)encoded_edx < list->count;) {
216         ssize_t may_encode;
217         size_t edx;
218         int need_eom = 0;
219
220         if(ct && ct->effective_bits >= 0) {
221             may_encode = list->count;
222         } else {
223             may_encode =
224                 uper_put_length(po, list->count - encoded_edx, &need_eom);
225             if(may_encode < 0) ASN__ENCODE_FAILED;
226         }
227
228         for(edx = encoded_edx; edx < encoded_edx + may_encode; edx++) {
229             void *memb_ptr = list->array[edx];
230             if(!memb_ptr) ASN__ENCODE_FAILED;
231             er = elm->type->op->uper_encoder(
232                 elm->type, elm->encoding_constraints.per_constraints, memb_ptr,
233                 po);
234             if(er.encoded == -1) ASN__ENCODE_FAILED;
235         }
236
237         if(need_eom && uper_put_length(po, 0, 0))
238             ASN__ENCODE_FAILED; /* End of Message length */
239
240         encoded_edx += may_encode;
241     }
242
243         ASN__ENCODED_OK(er);
244 }
245
246 asn_enc_rval_t
247 SEQUENCE_OF_encode_aper(const asn_TYPE_descriptor_t *td,
248                         const asn_per_constraints_t *constraints,
249                         const void *sptr, asn_per_outp_t *po) {
250         const asn_anonymous_sequence_ *list;
251         const asn_per_constraint_t *ct;
252         asn_enc_rval_t er = {0,0,0};
253         asn_TYPE_member_t *elm = td->elements;
254         int seq;
255
256         if(!sptr) ASN__ENCODE_FAILED;
257         list = _A_CSEQUENCE_FROM_VOID(sptr);
258
259         er.encoded = 0;
260
261         ASN_DEBUG("Encoding %s as SEQUENCE OF size (%d) using ALIGNED PER", td->name, list->count);
262
263         if(constraints) ct = &constraints->size;
264         else if(td->encoding_constraints.per_constraints)
265                 ct = &td->encoding_constraints.per_constraints->size;
266         else ct = 0;
267
268         /* If extensible constraint, check if size is in root */
269         if(ct) {
270                 int not_in_root = (list->count < ct->lower_bound
271                                 || list->count > ct->upper_bound);
272                 ASN_DEBUG("lb %ld ub %ld %s",
273                         ct->lower_bound, ct->upper_bound,
274                         ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
275                 if(ct->flags & APC_EXTENSIBLE) {
276                         /* Declare whether size is in extension root */
277                         if(per_put_few_bits(po, not_in_root, 1))
278                                 ASN__ENCODE_FAILED;
279                         if(not_in_root) ct = 0;
280                 } else if(not_in_root && ct->effective_bits >= 0)
281                         ASN__ENCODE_FAILED;
282         }
283
284         if(ct && ct->effective_bits >= 0) {
285                 /* X.691, #19.5: No length determinant */
286 /*               if(per_put_few_bits(po, list->count - ct->lower_bound,
287                                  ct->effective_bits))
288                          ASN__ENCODE_FAILED;
289 */
290                 if (aper_put_length(po, ct->upper_bound - ct->lower_bound + 1, list->count - ct->lower_bound) < 0)
291                         ASN__ENCODE_FAILED;
292         }
293
294         for(seq = -1; seq < list->count;) {
295                 ssize_t mayEncode;
296                 if(seq < 0) seq = 0;
297                 if(ct && ct->effective_bits >= 0) {
298                         mayEncode = list->count;
299                 } else {
300                         mayEncode = aper_put_length(po, -1, list->count - seq);
301                         if(mayEncode < 0) ASN__ENCODE_FAILED;
302                 }
303
304                 while(mayEncode--) {
305                         void *memb_ptr = list->array[seq++];
306                         if(!memb_ptr) ASN__ENCODE_FAILED;
307                         er = elm->type->op->aper_encoder(elm->type,
308                                 elm->encoding_constraints.per_constraints, memb_ptr, po);
309                         if(er.encoded == -1)
310                                 ASN__ENCODE_FAILED;
311                 }
312         }
313
314         ASN__ENCODED_OK(er);
315 }
316 #endif  /* ASN_DISABLE_PER_SUPPORT */
317
318 int
319 SEQUENCE_OF_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
320                const void *bptr) {
321     const asn_anonymous_sequence_ *a = _A_CSEQUENCE_FROM_VOID(aptr);
322     const asn_anonymous_sequence_ *b = _A_CSEQUENCE_FROM_VOID(bptr);
323     ssize_t idx;
324
325     if(a && b) {
326         ssize_t common_length = (a->count < b->count ? a->count : b->count);
327         for(idx = 0; idx < common_length; idx++) {
328             int ret = td->elements->type->op->compare_struct(
329                 td->elements->type, a->array[idx], b->array[idx]);
330             if(ret) return ret;
331         }
332
333         if(idx < b->count) /* more elements in b */
334             return -1;    /* a is shorter, so put it first */
335         if(idx < a->count) return 1;
336
337     } else if(!a) {
338         return -1;
339     } else if(!b) {
340         return 1;
341     }
342
343     return 0;
344 }
345
346
347 asn_TYPE_operation_t asn_OP_SEQUENCE_OF = {
348         SEQUENCE_OF_free,
349         SEQUENCE_OF_print,
350         SEQUENCE_OF_compare,
351         SEQUENCE_OF_decode_ber,
352         SEQUENCE_OF_encode_der,
353         SEQUENCE_OF_decode_xer,
354         SEQUENCE_OF_encode_xer,
355 #ifdef  ASN_DISABLE_OER_SUPPORT
356         0,
357         0,
358 #else
359         SEQUENCE_OF_decode_oer, /* Same as SET OF decoder. */
360         SEQUENCE_OF_encode_oer, /* Same as SET OF encoder */
361 #endif  /* ASN_DISABLE_OER_SUPPORT */
362 #ifdef ASN_DISABLE_PER_SUPPORT
363         0,
364         0,
365         0,
366         0,
367 #else
368         SEQUENCE_OF_decode_uper, /* Same as SET OF decoder */
369         SEQUENCE_OF_encode_uper,
370         SEQUENCE_OF_decode_aper,
371         SEQUENCE_OF_encode_aper,
372 #endif /* ASN_DISABLE_PER_SUPPORT */
373         SEQUENCE_OF_random_fill,
374         0       /* Use generic outmost tag fetcher */
375 };
376