2 * Copyright 2019 AT&T Intellectual Property
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
23 #include <asn_application.h>
24 #include <asn_internal.h>
25 #include <per_encoder.h>
27 static int _uper_encode_flush_outp(asn_per_outp_t *po);
30 ignore_output(const void *data, size_t size, void *app_key) {
38 uper_encode(const asn_TYPE_descriptor_t *td,
39 const asn_per_constraints_t *constraints, const void *sptr,
40 asn_app_consume_bytes_f *cb, void *app_key) {
42 asn_enc_rval_t er = {0,0,0};
45 * Invoke type-specific encoder.
47 if(!td || !td->op->uper_encoder)
48 ASN__ENCODE_FAILED; /* PER is not compiled in */
50 po.buffer = po.tmpspace;
52 po.nbits = 8 * sizeof(po.tmpspace);
53 po.output = cb ? cb : ignore_output;
57 er = td->op->uper_encoder(td, constraints, sptr, &po);
58 if(er.encoded != -1) {
61 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
63 /* Set number of bits encoded to a firm value */
64 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
66 if(_uper_encode_flush_outp(&po)) ASN__ENCODE_FAILED;
73 * Argument type and callback necessary for uper_encode_to_buffer().
75 typedef struct enc_to_buf_arg {
79 static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
80 enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
83 return -1; /* Data exceeds the available buffer size */
85 memcpy(arg->buffer, buffer, size);
86 arg->buffer = ((char *)arg->buffer) + size;
93 uper_encode_to_buffer(const asn_TYPE_descriptor_t *td,
94 const asn_per_constraints_t *constraints,
95 const void *sptr, void *buffer, size_t buffer_size) {
99 key.left = buffer_size;
101 if(td) ASN_DEBUG("Encoding \"%s\" using UNALIGNED PER", td->name);
103 return uper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
106 typedef struct enc_dyn_arg {
112 encode_dyn_cb(const void *buffer, size_t size, void *key) {
113 enc_dyn_arg *arg = key;
114 if(arg->length + size >= arg->allocated) {
115 size_t new_size = arg->allocated ? arg->allocated : 8;
120 } while(arg->length + size >= new_size);
122 p = REALLOC(arg->buffer, new_size);
124 FREEMEM(arg->buffer);
125 memset(arg, 0, sizeof(*arg));
129 arg->allocated = new_size;
131 memcpy(((char *)arg->buffer) + arg->length, buffer, size);
136 uper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
137 const asn_per_constraints_t *constraints,
138 const void *sptr, void **buffer_r) {
139 asn_enc_rval_t er = {0,0,0};
142 memset(&key, 0, sizeof(key));
144 er = uper_encode(td, constraints, sptr, encode_dyn_cb, &key);
151 key.buffer = MALLOC(1);
153 *(char *)key.buffer = '\0';
154 *buffer_r = key.buffer;
160 *buffer_r = key.buffer;
161 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
162 return ((er.encoded + 7) >> 3);
167 * Internally useful functions.
170 /* Flush partially filled buffer */
172 _uper_encode_flush_outp(asn_per_outp_t *po) {
175 if(po->nboff == 0 && po->buffer == po->tmpspace)
178 buf = po->buffer + (po->nboff >> 3);
179 /* Make sure we account for the last, partially filled */
180 if(po->nboff & 0x07) {
181 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
185 return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
189 aper_encode_to_buffer(const asn_TYPE_descriptor_t *td,
190 const asn_per_constraints_t *constraints,
191 const void *sptr, void *buffer, size_t buffer_size) {
195 key.left = buffer_size;
197 if(td) ASN_DEBUG("Encoding \"%s\" using ALIGNED PER", td->name);
199 return aper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
203 aper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
204 const asn_per_constraints_t *constraints,
205 const void *sptr, void **buffer_r) {
206 asn_enc_rval_t er = {0,0,0};
209 memset(&key, 0, sizeof(key));
211 er = aper_encode(td, constraints, sptr, encode_dyn_cb, &key);
218 key.buffer = MALLOC(1);
220 *(char *)key.buffer = '\0';
221 *buffer_r = key.buffer;
227 *buffer_r = key.buffer;
228 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
229 return ((er.encoded + 7) >> 3);
234 _aper_encode_flush_outp(asn_per_outp_t *po) {
237 if(po->nboff == 0 && po->buffer == po->tmpspace)
240 buf = po->buffer + (po->nboff >> 3);
241 /* Make sure we account for the last, partially filled */
242 if(po->nboff & 0x07) {
243 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
248 return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
254 aper_encode(const asn_TYPE_descriptor_t *td,
255 const asn_per_constraints_t *constraints,
256 const void *sptr, asn_app_consume_bytes_f *cb, void *app_key) {
258 asn_enc_rval_t er = {0,0,0};
261 * Invoke type-specific encoder.
263 if(!td || !td->op->aper_encoder)
264 ASN__ENCODE_FAILED; /* PER is not compiled in */
266 po.buffer = po.tmpspace;
268 po.nbits = 8 * sizeof(po.tmpspace);
271 po.flushed_bytes = 0;
273 er = td->op->aper_encoder(td, constraints, sptr, &po);
274 if(er.encoded != -1) {
275 size_t bits_to_flush;
277 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
279 /* Set number of bits encoded to a firm value */
280 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
282 if(_aper_encode_flush_outp(&po))