SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / aper_encoder.c
1 #include <asn_application.h>
2 #include <asn_internal.h>
3 #include <aper_encoder.h>
4
5 /*
6  * Argument type and callback necessary for aper_encode_to_buffer().
7  */
8 typedef struct enc_to_buf_arg {
9         void *buffer;
10         size_t left;
11 } enc_to_buf_arg;
12 static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
13         enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
14
15         if(arg->left < size)
16                 return -1;      /* Data exceeds the available buffer size */
17
18         memcpy(arg->buffer, buffer, size);
19         arg->buffer = ((char *)arg->buffer) + size;
20         arg->left -= size;
21
22         return 0;
23 }
24
25 asn_enc_rval_t
26 aper_encode_to_buffer(const asn_TYPE_descriptor_t *td,
27                       const asn_per_constraints_t *constraints,
28                       const void *sptr, void *buffer, size_t buffer_size) {
29     enc_to_buf_arg key;
30
31     key.buffer = buffer;
32     key.left = buffer_size;
33
34     if(td) ASN_DEBUG("Encoding \"%s\" using ALIGNED PER", td->name);
35
36     return aper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
37 }
38
39 ssize_t
40 aper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
41                           const asn_per_constraints_t *constraints,
42                           const void *sptr, void **buffer_r) {
43     asn_enc_rval_t er = {0,0,0};
44         enc_dyn_arg key;
45
46         memset(&key, 0, sizeof(key));
47
48         er = aper_encode(td, constraints, sptr, encode_dyn_cb, &key);
49         switch(er.encoded) {
50         case -1:
51                 FREEMEM(key.buffer);
52                 return -1;
53         case 0:
54                 FREEMEM(key.buffer);
55                 key.buffer = MALLOC(1);
56                 if(key.buffer) {
57                         *(char *)key.buffer = '\0';
58                         *buffer_r = key.buffer;
59                         return 1;
60                 } else {
61                         return -1;
62                 }
63         default:
64                 *buffer_r = key.buffer;
65                 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
66                 return ((er.encoded + 7) >> 3);
67         }
68 }
69
70 /*
71  * Internally useful functions.
72  */
73
74 /* Flush partially filled buffer */
75 static int
76 _aper_encode_flush_outp(asn_per_outp_t *po) {
77         uint8_t *buf;
78
79         if(po->nboff == 0 && po->buffer == po->tmpspace)
80                 return 0;
81
82         buf = po->buffer + (po->nboff >> 3);
83         /* Make sure we account for the last, partially filled */
84         if(po->nboff & 0x07) {
85                 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
86                 buf++;
87         }
88
89         if (po->output) {
90                 return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
91         }
92         return 0;
93 }
94
95 asn_enc_rval_t
96 aper_encode(const asn_TYPE_descriptor_t *td,
97         const asn_per_constraints_t *constraints,
98         const void *sptr, asn_app_consume_bytes_f *cb, void *app_key) {
99         asn_per_outp_t po;
100         asn_enc_rval_t er = {0,0,0};
101
102         /*
103          * Invoke type-specific encoder.
104          */
105         if(!td || !td->op->aper_encoder)
106                 ASN__ENCODE_FAILED;      /* PER is not compiled in */
107
108         po.buffer = po.tmpspace;
109         po.nboff = 0;
110         po.nbits = 8 * sizeof(po.tmpspace);
111         po.output = cb ? cb : ignore_output;
112         po.op_key = app_key;
113         po.flushed_bytes = 0;
114
115         er = td->op->aper_encoder(td, constraints, sptr, &po);
116         if(er.encoded != -1) {
117                 size_t bits_to_flush;
118
119                 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
120
121                 /* Set number of bits encoded to a firm value */
122                 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
123
124                 if(_aper_encode_flush_outp(&po))
125                         ASN__ENCODE_FAILED;
126         }
127
128         return er;
129 }