3455236db15cf4348965e35d993a539bf3f20564
[o-du/l2.git] / src / codec_utils / common / per_encoder.c
1 #include <asn_application.h>
2 #include <asn_internal.h>
3 #include <per_encoder.h>
4
5 static int _uper_encode_flush_outp(asn_per_outp_t *po);
6
7 static int
8 ignore_output(const void *data, size_t size, void *app_key) {
9     (void)data;
10     (void)size;
11     (void)app_key;
12     return 0;
13 }
14
15 asn_enc_rval_t
16 uper_encode(const asn_TYPE_descriptor_t *td,
17             const asn_per_constraints_t *constraints, const void *sptr,
18             asn_app_consume_bytes_f *cb, void *app_key) {
19     asn_per_outp_t po;
20     asn_enc_rval_t er = {0,0,0};
21
22     /*
23      * Invoke type-specific encoder.
24      */
25     if(!td || !td->op->uper_encoder)
26         ASN__ENCODE_FAILED;     /* PER is not compiled in */
27
28     po.buffer = po.tmpspace;
29     po.nboff = 0;
30     po.nbits = 8 * sizeof(po.tmpspace);
31     po.output = cb ? cb : ignore_output;
32     po.op_key = app_key;
33     po.flushed_bytes = 0;
34
35     er = td->op->uper_encoder(td, constraints, sptr, &po);
36     if(er.encoded != -1) {
37         size_t bits_to_flush;
38
39         bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
40
41         /* Set number of bits encoded to a firm value */
42         er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
43
44         if(_uper_encode_flush_outp(&po)) ASN__ENCODE_FAILED;
45     }
46     else
47     {
48        printf("else return val er.encoded %u, er.failed_type->name '%s'\n", er.encoded, er.failed_type->name);
49    }
50
51     return er;
52 }
53
54 /*
55  * Argument type and callback necessary for uper_encode_to_buffer().
56  */
57 typedef struct enc_to_buf_arg {
58         void *buffer;
59         size_t left;
60 } enc_to_buf_arg;
61 static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
62         enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
63
64         if(arg->left < size)
65                 return -1;      /* Data exceeds the available buffer size */
66
67         memcpy(arg->buffer, buffer, size);
68         arg->buffer = ((char *)arg->buffer) + size;
69         arg->left -= size;
70
71         return 0;
72 }
73
74 asn_enc_rval_t
75 uper_encode_to_buffer(const asn_TYPE_descriptor_t *td,
76                       const asn_per_constraints_t *constraints,
77                       const void *sptr, void *buffer, size_t buffer_size) {
78     enc_to_buf_arg key;
79
80     key.buffer = buffer;
81     key.left = buffer_size;
82
83     if(td) ASN_DEBUG("Encoding \"%s\" using UNALIGNED PER", td->name);
84
85     return uper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
86 }
87
88 typedef struct enc_dyn_arg {
89         void *buffer;
90         size_t length;
91         size_t allocated;
92 } enc_dyn_arg;
93 static int
94 encode_dyn_cb(const void *buffer, size_t size, void *key) {
95     enc_dyn_arg *arg = key;
96     if(arg->length + size >= arg->allocated) {
97         size_t new_size = arg->allocated ? arg->allocated : 8;
98         void *p;
99
100         do {
101             new_size <<= 2;
102         } while(arg->length + size >= new_size);
103
104         p = REALLOC(arg->buffer, new_size);
105         if(!p) {
106             FREEMEM(arg->buffer);
107             memset(arg, 0, sizeof(*arg));
108             return -1;
109         }
110         arg->buffer = p;
111         arg->allocated = new_size;
112     }
113     memcpy(((char *)arg->buffer) + arg->length, buffer, size);
114     arg->length += size;
115     return 0;
116 }
117 ssize_t
118 uper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
119                           const asn_per_constraints_t *constraints,
120                           const void *sptr, void **buffer_r) {
121     asn_enc_rval_t er = {0,0,0};
122         enc_dyn_arg key;
123
124         memset(&key, 0, sizeof(key));
125
126         er = uper_encode(td, constraints, sptr, encode_dyn_cb, &key);
127         switch(er.encoded) {
128         case -1:
129                 FREEMEM(key.buffer);
130                 return -1;
131         case 0:
132                 FREEMEM(key.buffer);
133                 key.buffer = MALLOC(1);
134                 if(key.buffer) {
135                         *(char *)key.buffer = '\0';
136                         *buffer_r = key.buffer;
137                         return 1;
138                 } else {
139                         return -1;
140                 }
141         default:
142                 *buffer_r = key.buffer;
143                 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
144                 return ((er.encoded + 7) >> 3);
145         }
146 }
147
148 /*
149  * Internally useful functions.
150  */
151
152 /* Flush partially filled buffer */
153 static int
154 _uper_encode_flush_outp(asn_per_outp_t *po) {
155         uint8_t *buf;
156
157         if(po->nboff == 0 && po->buffer == po->tmpspace)
158                 return 0;
159
160         buf = po->buffer + (po->nboff >> 3);
161         /* Make sure we account for the last, partially filled */
162         if(po->nboff & 0x07) {
163                 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
164                 buf++;
165         }
166
167         return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
168 }
169
170 asn_enc_rval_t
171 aper_encode_to_buffer(const asn_TYPE_descriptor_t *td,
172                       const asn_per_constraints_t *constraints,
173                       const void *sptr, void *buffer, size_t buffer_size) {
174     enc_to_buf_arg key;
175
176     key.buffer = buffer;
177     key.left = buffer_size;
178
179     if(td) ASN_DEBUG("Encoding \"%s\" using ALIGNED PER", td->name);
180
181     return aper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
182 }
183
184 ssize_t
185 aper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
186                           const asn_per_constraints_t *constraints,
187                           const void *sptr, void **buffer_r) {
188     asn_enc_rval_t er = {0,0,0};
189         enc_dyn_arg key;
190
191         memset(&key, 0, sizeof(key));
192
193         er = aper_encode(td, constraints, sptr, encode_dyn_cb, &key);
194         switch(er.encoded) {
195         case -1:
196                 FREEMEM(key.buffer);
197                 return -1;
198         case 0:
199                 FREEMEM(key.buffer);
200                 key.buffer = MALLOC(1);
201                 if(key.buffer) {
202                         *(char *)key.buffer = '\0';
203                         *buffer_r = key.buffer;
204                         return 1;
205                 } else {
206                         return -1;
207                 }
208         default:
209                 *buffer_r = key.buffer;
210                 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
211                 return ((er.encoded + 7) >> 3);
212         }
213 }
214
215 static int
216 _aper_encode_flush_outp(asn_per_outp_t *po) {
217         uint8_t *buf;
218
219         if(po->nboff == 0 && po->buffer == po->tmpspace)
220                 return 0;
221
222         buf = po->buffer + (po->nboff >> 3);
223         /* Make sure we account for the last, partially filled */
224         if(po->nboff & 0x07) {
225                 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
226                 buf++;
227         }
228
229         if (po->output) {
230                 return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
231         }
232         return 0;
233 }
234
235 asn_enc_rval_t
236 aper_encode(const asn_TYPE_descriptor_t *td,
237         const asn_per_constraints_t *constraints,
238         const void *sptr, asn_app_consume_bytes_f *cb, void *app_key) {
239         asn_per_outp_t po;
240         asn_enc_rval_t er = {0,0,0};
241
242         /*
243          * Invoke type-specific encoder.
244          */
245         if(!td || !td->op->aper_encoder)
246                 ASN__ENCODE_FAILED;      /* PER is not compiled in */
247
248         po.buffer = po.tmpspace;
249         po.nboff = 0;
250         po.nbits = 8 * sizeof(po.tmpspace);
251         po.output = cb;
252         po.op_key = app_key;
253         po.flushed_bytes = 0;
254
255         er = td->op->aper_encoder(td, constraints, sptr, &po);
256         if(er.encoded != -1) {
257                 size_t bits_to_flush;
258
259                 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
260
261                 /* Set number of bits encoded to a firm value */
262                 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
263
264                 if(_aper_encode_flush_outp(&po))
265                         ASN__ENCODE_FAILED;
266         }
267
268         return er;
269 }