3 * Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>.
5 * Redistribution and modifications are permitted subject to BSD license.
7 #include <asn_internal.h>
8 #include <constr_SEQUENCE_OF.h>
9 #include <asn_SEQUENCE_OF.h>
12 * The DER encoder of the SEQUENCE OF type.
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};
25 ASN_DEBUG("Estimating size of SEQUENCE OF %s", td->name);
28 * Gather the length of the underlying members sequence.
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,
36 if(erval.encoded == -1)
38 computed_size += erval.encoded;
42 * Encode the TLV for the sequence itself.
44 encoding_size = der_write_tags(td, computed_size, tag_mode, 1, tag,
46 if(encoding_size == -1) {
48 erval.failed_type = td;
49 erval.structure_ptr = ptr;
53 computed_size += encoding_size;
55 erval.encoded = computed_size;
56 ASN__ENCODED_OK(erval);
59 ASN_DEBUG("Encoding members of SEQUENCE OF %s", td->name);
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,
70 if(erval.encoded == -1)
72 encoding_size += erval.encoded;
75 if(computed_size != (size_t)encoding_size) {
77 * Encoded size is not equal to the computed size.
80 erval.failed_type = td;
81 erval.structure_ptr = ptr;
83 erval.encoded = computed_size;
84 erval.structure_ptr = 0;
85 erval.failed_type = 0;
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
101 : ((*elm->name) ? elm->name : elm->type->xml_tag);
102 size_t mlen = mname ? strlen(mname) : 0;
103 int xcan = (flags & XER_F_CANONICAL);
106 if(!sptr) ASN__ENCODE_FAILED;
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;
116 if(!xcan) ASN__TEXT_INDENT(1, ilevel);
117 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
120 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, ilevel + 1,
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);
132 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
136 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
143 #ifndef ASN_DISABLE_PER_SUPPORT
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;
155 if(!sptr) ASN__ENCODE_FAILED;
156 list = _A_CSEQUENCE_FROM_VOID(sptr);
160 ASN_DEBUG("Encoding %s as SEQUENCE OF (%d)", td->name, list->count);
162 if(constraints) ct = &constraints->size;
163 else if(td->encoding_constraints.per_constraints)
164 ct = &td->encoding_constraints.per_constraints->size;
167 /* If extensible constraint, check if size is 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) {
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,
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
192 if (uper_put_length(po, 0, 0)) {
198 for(encoded_edx = 0; (ssize_t)encoded_edx < list->count;) {
203 if(ct && ct->effective_bits >= 0) {
204 may_encode = list->count;
207 uper_put_length(po, list->count - encoded_edx, &need_eom);
208 if(may_encode < 0) ASN__ENCODE_FAILED;
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,
217 if(er.encoded == -1) ASN__ENCODE_FAILED;
220 if(need_eom && uper_put_length(po, 0, 0))
221 ASN__ENCODE_FAILED; /* End of Message length */
223 encoded_edx += may_encode;
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;
239 if(!sptr) ASN__ENCODE_FAILED;
240 list = _A_CSEQUENCE_FROM_VOID(sptr);
244 ASN_DEBUG("Encoding %s as SEQUENCE OF size (%d) using ALIGNED PER", td->name, list->count);
246 if(constraints) ct = &constraints->size;
247 else if(td->encoding_constraints.per_constraints)
248 ct = &td->encoding_constraints.per_constraints->size;
251 /* If extensible constraint, check if size is in root */
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))
262 if(not_in_root) ct = 0;
263 } else if(not_in_root && ct->effective_bits >= 0)
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,
273 if (aper_put_length(po, ct->upper_bound - ct->lower_bound + 1, list->count - ct->lower_bound) < 0)
277 for(seq = -1; seq < list->count;) {
280 if(ct && ct->effective_bits >= 0) {
281 mayEncode = list->count;
283 mayEncode = aper_put_length(po, -1, list->count - seq);
284 if(mayEncode < 0) ASN__ENCODE_FAILED;
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);
299 #endif /* ASN_DISABLE_PER_SUPPORT */
302 SEQUENCE_OF_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
304 const asn_anonymous_sequence_ *a = _A_CSEQUENCE_FROM_VOID(aptr);
305 const asn_anonymous_sequence_ *b = _A_CSEQUENCE_FROM_VOID(bptr);
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]);
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;
330 asn_TYPE_operation_t asn_OP_SEQUENCE_OF = {
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
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
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 */