SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / constr_SEQUENCE_OF_uper.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_uper(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         const asn_TYPE_member_t *elm = td->elements;
18         size_t encoded_edx;
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 (%d)", 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 =
35             (list->count < ct->lower_bound || list->count > ct->upper_bound);
36         ASN_DEBUG("lb %ld ub %ld %s", ct->lower_bound, ct->upper_bound,
37                   ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
38         if(ct->flags & APC_EXTENSIBLE) {
39             /* Declare whether size is in extension root */
40             if(per_put_few_bits(po, not_in_root, 1)) ASN__ENCODE_FAILED;
41             if(not_in_root) ct = 0;
42         } else if(not_in_root && ct->effective_bits >= 0) {
43             ASN__ENCODE_FAILED;
44         }
45
46     }
47
48     if(ct && ct->effective_bits >= 0) {
49         /* X.691, #19.5: No length determinant */
50         if(per_put_few_bits(po, list->count - ct->lower_bound,
51                             ct->effective_bits))
52             ASN__ENCODE_FAILED;
53     } else if(list->count == 0) {
54         /* When the list is empty add only the length determinant
55          * X.691, #20.6 and #11.9.4.1
56          */
57         if (uper_put_length(po, 0, 0)) {
58             ASN__ENCODE_FAILED;
59         }
60         ASN__ENCODED_OK(er);
61     }
62
63     for(encoded_edx = 0; (ssize_t)encoded_edx < list->count;) {
64         ssize_t may_encode;
65         size_t edx;
66         int need_eom = 0;
67
68         if(ct && ct->effective_bits >= 0) {
69             may_encode = list->count;
70         } else {
71             may_encode =
72                 uper_put_length(po, list->count - encoded_edx, &need_eom);
73             if(may_encode < 0) ASN__ENCODE_FAILED;
74         }
75
76         for(edx = encoded_edx; edx < encoded_edx + may_encode; edx++) {
77             void *memb_ptr = list->array[edx];
78             if(!memb_ptr) ASN__ENCODE_FAILED;
79             er = elm->type->op->uper_encoder(
80                 elm->type, elm->encoding_constraints.per_constraints, memb_ptr,
81                 po);
82             if(er.encoded == -1) ASN__ENCODE_FAILED;
83         }
84
85         if(need_eom && uper_put_length(po, 0, 0))
86             ASN__ENCODE_FAILED; /* End of Message length */
87
88         encoded_edx += may_encode;
89     }
90
91         ASN__ENCODED_OK(er);
92 }