1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
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 *
9 # http://www.apache.org/licenses/LICENSE-2.0 *
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. *
17 ******************************************************************************/
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.
24 #include <asn_internal.h>
25 #include <constr_SEQUENCE_OF.h>
26 #include <asn_SEQUENCE_OF.h>
29 * The DER encoder of the SEQUENCE OF type.
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};
42 ASN_DEBUG("Estimating size of SEQUENCE OF %s", td->name);
45 * Gather the length of the underlying members sequence.
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,
53 if(erval.encoded == -1)
55 computed_size += erval.encoded;
59 * Encode the TLV for the sequence itself.
61 encoding_size = der_write_tags(td, computed_size, tag_mode, 1, tag,
63 if(encoding_size == -1) {
65 erval.failed_type = td;
66 erval.structure_ptr = ptr;
70 computed_size += encoding_size;
72 erval.encoded = computed_size;
73 ASN__ENCODED_OK(erval);
76 ASN_DEBUG("Encoding members of SEQUENCE OF %s", td->name);
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,
87 if(erval.encoded == -1)
89 encoding_size += erval.encoded;
92 if(computed_size != (size_t)encoding_size) {
94 * Encoded size is not equal to the computed size.
97 erval.failed_type = td;
98 erval.structure_ptr = ptr;
100 erval.encoded = computed_size;
101 erval.structure_ptr = 0;
102 erval.failed_type = 0;
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
118 : ((*elm->name) ? elm->name : elm->type->xml_tag);
119 size_t mlen = mname ? strlen(mname) : 0;
120 int xcan = (flags & XER_F_CANONICAL);
123 if(!sptr) ASN__ENCODE_FAILED;
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;
133 if(!xcan) ASN__TEXT_INDENT(1, ilevel);
134 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
137 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, ilevel + 1,
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);
149 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
153 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
160 #ifndef ASN_DISABLE_PER_SUPPORT
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;
172 if(!sptr) ASN__ENCODE_FAILED;
173 list = _A_CSEQUENCE_FROM_VOID(sptr);
177 ASN_DEBUG("Encoding %s as SEQUENCE OF (%d)", td->name, list->count);
179 if(constraints) ct = &constraints->size;
180 else if(td->encoding_constraints.per_constraints)
181 ct = &td->encoding_constraints.per_constraints->size;
184 /* If extensible constraint, check if size is 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) {
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,
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
209 if (uper_put_length(po, 0, 0)) {
215 for(encoded_edx = 0; (ssize_t)encoded_edx < list->count;) {
220 if(ct && ct->effective_bits >= 0) {
221 may_encode = list->count;
224 uper_put_length(po, list->count - encoded_edx, &need_eom);
225 if(may_encode < 0) ASN__ENCODE_FAILED;
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,
234 if(er.encoded == -1) ASN__ENCODE_FAILED;
237 if(need_eom && uper_put_length(po, 0, 0))
238 ASN__ENCODE_FAILED; /* End of Message length */
240 encoded_edx += may_encode;
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;
256 if(!sptr) ASN__ENCODE_FAILED;
257 list = _A_CSEQUENCE_FROM_VOID(sptr);
261 ASN_DEBUG("Encoding %s as SEQUENCE OF size (%d) using ALIGNED PER", td->name, list->count);
263 if(constraints) ct = &constraints->size;
264 else if(td->encoding_constraints.per_constraints)
265 ct = &td->encoding_constraints.per_constraints->size;
268 /* If extensible constraint, check if size is in root */
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))
279 if(not_in_root) ct = 0;
280 } else if(not_in_root && ct->effective_bits >= 0)
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,
290 if (aper_put_length(po, ct->upper_bound - ct->lower_bound + 1, list->count - ct->lower_bound) < 0)
294 for(seq = -1; seq < list->count;) {
297 if(ct && ct->effective_bits >= 0) {
298 mayEncode = list->count;
300 mayEncode = aper_put_length(po, -1, list->count - seq);
301 if(mayEncode < 0) ASN__ENCODE_FAILED;
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);
316 #endif /* ASN_DISABLE_PER_SUPPORT */
319 SEQUENCE_OF_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
321 const asn_anonymous_sequence_ *a = _A_CSEQUENCE_FROM_VOID(aptr);
322 const asn_anonymous_sequence_ *b = _A_CSEQUENCE_FROM_VOID(bptr);
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]);
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;
347 asn_TYPE_operation_t asn_OP_SEQUENCE_OF = {
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
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
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 */