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