SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / constr_SEQUENCE_OF_aper.c
1 /*
2  * Copyright (c) 2017 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 asn_enc_rval_t
11 SEQUENCE_OF_encode_aper(const asn_TYPE_descriptor_t *td,
12                         const asn_per_constraints_t *constraints,
13                         const void *sptr, asn_per_outp_t *po) {
14     const asn_anonymous_sequence_ *list;
15     const asn_per_constraint_t *ct;
16     asn_enc_rval_t er = {0,0,0};
17     asn_TYPE_member_t *elm = td->elements;
18     int seq;
19
20     if(!sptr) ASN__ENCODE_FAILED;
21     list = _A_CSEQUENCE_FROM_VOID(sptr);
22
23     er.encoded = 0;
24
25     ASN_DEBUG("Encoding %s as SEQUENCE OF size (%d) using ALIGNED PER", td->name, list->count);
26
27     if(constraints) ct = &constraints->size;
28     else if(td->encoding_constraints.per_constraints)
29         ct = &td->encoding_constraints.per_constraints->size;
30     else ct = 0;
31
32     /* If extensible constraint, check if size is in root */
33     if(ct) {
34         int not_in_root = (list->count < ct->lower_bound
35                 || list->count > ct->upper_bound);
36         ASN_DEBUG("lb %lld ub %lld %s",
37                   (long long int)ct->lower_bound,
38                   (long long int)ct->upper_bound,
39                   ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
40         if(ct->flags & APC_EXTENSIBLE) {
41             /* Declare whether size is in extension root */
42             if(per_put_few_bits(po, not_in_root, 1))
43                 ASN__ENCODE_FAILED;
44             if(not_in_root) ct = 0;
45         } else if(not_in_root && ct->effective_bits >= 0)
46             ASN__ENCODE_FAILED;
47     }
48
49     if(ct && ct->effective_bits >= 0) {
50         /* X.691, #19.5: No length determinant */
51 /*
52         if(per_put_few_bits(po, list->count - ct->lower_bound,
53                             ct->effective_bits))
54             ASN__ENCODE_FAILED;
55 */
56         if (ct->lower_bound == ct->upper_bound && ct->upper_bound < 65536) {
57             /* No length determinant */
58     } else if (aper_put_length(po, ct->lower_bound, ct->upper_bound, list->count - ct->lower_bound, 0) < 0)
59             ASN__ENCODE_FAILED;
60     }
61
62     for(seq = -1; seq < list->count;) {
63         ssize_t mayEncode;
64         int need_eom = 0;
65         if(seq < 0) seq = 0;
66         if(ct && ct->effective_bits >= 0) {
67             mayEncode = list->count;
68         } else {
69             mayEncode = aper_put_length(po, -1, -1, list->count - seq, &need_eom);
70             if(mayEncode < 0) ASN__ENCODE_FAILED;
71         }
72
73         while(mayEncode--) {
74             void *memb_ptr = list->array[seq++];
75             if(!memb_ptr) ASN__ENCODE_FAILED;
76             er = elm->type->op->aper_encoder(elm->type,
77                                              elm->encoding_constraints.per_constraints,
78                                              memb_ptr, po);
79             if(er.encoded == -1)
80                 ASN__ENCODE_FAILED;
81         }
82
83         if(need_eom && (aper_put_length(po, -1, -1, 0, NULL) < 0))
84             ASN__ENCODE_FAILED;  /* End of Message length */
85     }
86
87     ASN__ENCODED_OK(er);
88 }