]> git.stg.codes - stg.git/blob - libs/smux/constr_SEQUENCE_OF.c
Fix build on Darwin.
[stg.git] / libs / smux / constr_SEQUENCE_OF.c
1 /*-
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  */
13 asn_enc_rval_t
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;
21         asn_enc_rval_t erval;
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
90 asn_enc_rval_t
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) {
94     asn_enc_rval_t er;
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;
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);
138 cb_failed:
139     ASN__ENCODE_FAILED;
140 }
141
142 #ifndef ASN_DISABLE_PER_SUPPORT
143
144 asn_enc_rval_t
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;
150         asn_enc_rval_t er;
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
228 #endif  /* ASN_DISABLE_PER_SUPPORT */
229
230 int
231 SEQUENCE_OF_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
232                const void *bptr) {
233     const asn_anonymous_sequence_ *a = _A_CSEQUENCE_FROM_VOID(aptr);
234     const asn_anonymous_sequence_ *b = _A_CSEQUENCE_FROM_VOID(bptr);
235     ssize_t idx;
236
237     if(a && b) {
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]);
242             if(ret) return ret;
243         }
244
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;
248
249     } else if(!a) {
250         return -1;
251     } else if(!b) {
252         return 1;
253     }
254
255     return 0;
256 }
257
258
259 asn_TYPE_operation_t asn_OP_SEQUENCE_OF = {
260         SEQUENCE_OF_free,
261         SEQUENCE_OF_print,
262         SEQUENCE_OF_compare,
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
268         0,
269         0,
270 #else
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
275         0,
276         0,
277 #else
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 */
283 };
284