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