2 * Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <constr_SEQUENCE_OF.h>
8 #include <asn_SEQUENCE_OF.h>
11 * The DER encoder of the SEQUENCE OF type.
14 SEQUENCE_OF_encode_der(const asn_TYPE_descriptor_t *td, const void *ptr,
15 int tag_mode, ber_tlv_tag_t tag,
16 asn_app_consume_bytes_f *cb, void *app_key) {
17 asn_TYPE_member_t *elm = td->elements;
18 const asn_anonymous_sequence_ *list = _A_CSEQUENCE_FROM_VOID(ptr);
19 size_t computed_size = 0;
20 ssize_t encoding_size = 0;
24 ASN_DEBUG("Estimating size of SEQUENCE OF %s", td->name);
27 * Gather the length of the underlying members sequence.
29 for(edx = 0; edx < list->count; edx++) {
30 void *memb_ptr = list->array[edx];
31 if(!memb_ptr) continue;
32 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
35 if(erval.encoded == -1)
37 computed_size += erval.encoded;
41 * Encode the TLV for the sequence itself.
43 encoding_size = der_write_tags(td, computed_size, tag_mode, 1, tag,
45 if(encoding_size == -1) {
47 erval.failed_type = td;
48 erval.structure_ptr = ptr;
52 computed_size += encoding_size;
54 erval.encoded = computed_size;
55 ASN__ENCODED_OK(erval);
58 ASN_DEBUG("Encoding members of SEQUENCE OF %s", td->name);
63 for(edx = 0; edx < list->count; edx++) {
64 void *memb_ptr = list->array[edx];
65 if(!memb_ptr) continue;
66 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
69 if(erval.encoded == -1)
71 encoding_size += erval.encoded;
74 if(computed_size != (size_t)encoding_size) {
76 * Encoded size is not equal to the computed size.
79 erval.failed_type = td;
80 erval.structure_ptr = ptr;
82 erval.encoded = computed_size;
83 erval.structure_ptr = 0;
84 erval.failed_type = 0;
91 SEQUENCE_OF_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
92 int ilevel, enum xer_encoder_flags_e flags,
93 asn_app_consume_bytes_f *cb, void *app_key) {
95 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
96 const asn_TYPE_member_t *elm = td->elements;
97 const asn_anonymous_sequence_ *list = _A_CSEQUENCE_FROM_VOID(sptr);
98 const char *mname = specs->as_XMLValueList
100 : ((*elm->name) ? elm->name : elm->type->xml_tag);
101 size_t mlen = mname ? strlen(mname) : 0;
102 int xcan = (flags & XER_F_CANONICAL);
105 if(!sptr) ASN__ENCODE_FAILED;
109 for(i = 0; i < list->count; i++) {
110 asn_enc_rval_t tmper;
111 void *memb_ptr = list->array[i];
112 if(!memb_ptr) continue;
115 if(!xcan) ASN__TEXT_INDENT(1, ilevel);
116 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
119 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, ilevel + 1,
121 if(tmper.encoded == -1) return tmper;
122 er.encoded += tmper.encoded;
123 if(tmper.encoded == 0 && specs->as_XMLValueList) {
124 const char *name = elm->type->xml_tag;
125 size_t len = strlen(name);
126 if(!xcan) ASN__TEXT_INDENT(1, ilevel + 1);
127 ASN__CALLBACK3("<", 1, name, len, "/>", 2);
131 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
135 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
142 #ifndef ASN_DISABLE_PER_SUPPORT
145 SEQUENCE_OF_encode_uper(const asn_TYPE_descriptor_t *td,
146 const asn_per_constraints_t *constraints,
147 const void *sptr, asn_per_outp_t *po) {
148 const asn_anonymous_sequence_ *list;
149 const asn_per_constraint_t *ct;
151 const asn_TYPE_member_t *elm = td->elements;
154 if(!sptr) ASN__ENCODE_FAILED;
155 list = _A_CSEQUENCE_FROM_VOID(sptr);
159 ASN_DEBUG("Encoding %s as SEQUENCE OF (%d)", td->name, list->count);
161 if(constraints) ct = &constraints->size;
162 else if(td->encoding_constraints.per_constraints)
163 ct = &td->encoding_constraints.per_constraints->size;
166 /* If extensible constraint, check if size is in root */
169 (list->count < ct->lower_bound || list->count > ct->upper_bound);
170 ASN_DEBUG("lb %ld ub %ld %s", ct->lower_bound, ct->upper_bound,
171 ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
172 if(ct->flags & APC_EXTENSIBLE) {
173 /* Declare whether size is in extension root */
174 if(per_put_few_bits(po, not_in_root, 1)) ASN__ENCODE_FAILED;
175 if(not_in_root) ct = 0;
176 } else if(not_in_root && ct->effective_bits >= 0) {
182 if(ct && ct->effective_bits >= 0) {
183 /* X.691, #19.5: No length determinant */
184 if(per_put_few_bits(po, list->count - ct->lower_bound,
187 } else if(list->count == 0) {
188 /* When the list is empty add only the length determinant
189 * X.691, #20.6 and #11.9.4.1
191 if (uper_put_length(po, 0, 0)) {
197 for(encoded_edx = 0; (ssize_t)encoded_edx < list->count;) {
202 if(ct && ct->effective_bits >= 0) {
203 may_encode = list->count;
206 uper_put_length(po, list->count - encoded_edx, &need_eom);
207 if(may_encode < 0) ASN__ENCODE_FAILED;
210 for(edx = encoded_edx; edx < encoded_edx + may_encode; edx++) {
211 void *memb_ptr = list->array[edx];
212 if(!memb_ptr) ASN__ENCODE_FAILED;
213 er = elm->type->op->uper_encoder(
214 elm->type, elm->encoding_constraints.per_constraints, memb_ptr,
216 if(er.encoded == -1) ASN__ENCODE_FAILED;
219 if(need_eom && uper_put_length(po, 0, 0))
220 ASN__ENCODE_FAILED; /* End of Message length */
222 encoded_edx += may_encode;
228 #endif /* ASN_DISABLE_PER_SUPPORT */
231 SEQUENCE_OF_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
233 const asn_anonymous_sequence_ *a = _A_CSEQUENCE_FROM_VOID(aptr);
234 const asn_anonymous_sequence_ *b = _A_CSEQUENCE_FROM_VOID(bptr);
238 ssize_t common_length = (a->count < b->count ? a->count : b->count);
239 for(idx = 0; idx < common_length; idx++) {
240 int ret = td->elements->type->op->compare_struct(
241 td->elements->type, a->array[idx], b->array[idx]);
245 if(idx < b->count) /* more elements in b */
246 return -1; /* a is shorter, so put it first */
247 if(idx < a->count) return 1;
259 asn_TYPE_operation_t asn_OP_SEQUENCE_OF = {
263 SEQUENCE_OF_decode_ber,
264 SEQUENCE_OF_encode_der,
265 SEQUENCE_OF_decode_xer,
266 SEQUENCE_OF_encode_xer,
267 #ifdef ASN_DISABLE_OER_SUPPORT
271 SEQUENCE_OF_decode_oer, /* Same as SET OF decoder. */
272 SEQUENCE_OF_encode_oer, /* Same as SET OF encoder */
273 #endif /* ASN_DISABLE_OER_SUPPORT */
274 #ifdef ASN_DISABLE_PER_SUPPORT
278 SEQUENCE_OF_decode_uper, /* Same as SET OF decoder */
279 SEQUENCE_OF_encode_uper,
280 #endif /* ASN_DISABLE_PER_SUPPORT */
281 SEQUENCE_OF_random_fill,
282 0 /* Use generic outmost tag fetcher */