3 * Copyright 2019 AT&T Intellectual Property
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <asn_application.h>
20 #include <asn_internal.h>
21 #include <per_encoder.h>
23 static int _uper_encode_flush_outp(asn_per_outp_t *po);
26 ignore_output(const void *data, size_t size, void *app_key) {
34 uper_encode(const asn_TYPE_descriptor_t *td,
35 const asn_per_constraints_t *constraints, const void *sptr,
36 asn_app_consume_bytes_f *cb, void *app_key) {
38 asn_enc_rval_t er = {0,0,0};
41 * Invoke type-specific encoder.
43 if(!td || !td->op->uper_encoder)
44 ASN__ENCODE_FAILED; /* PER is not compiled in */
46 po.buffer = po.tmpspace;
48 po.nbits = 8 * sizeof(po.tmpspace);
49 po.output = cb ? cb : ignore_output;
53 er = td->op->uper_encoder(td, constraints, sptr, &po);
54 if(er.encoded != -1) {
57 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
59 /* Set number of bits encoded to a firm value */
60 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
62 if(_uper_encode_flush_outp(&po)) ASN__ENCODE_FAILED;
69 * Argument type and callback necessary for uper_encode_to_buffer().
71 typedef struct enc_to_buf_arg {
75 static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
76 enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
79 return -1; /* Data exceeds the available buffer size */
81 memcpy(arg->buffer, buffer, size);
82 arg->buffer = ((char *)arg->buffer) + size;
89 uper_encode_to_buffer(const asn_TYPE_descriptor_t *td,
90 const asn_per_constraints_t *constraints,
91 const void *sptr, void *buffer, size_t buffer_size) {
95 key.left = buffer_size;
97 if(td) ASN_DEBUG("Encoding \"%s\" using UNALIGNED PER", td->name);
99 return uper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
102 typedef struct enc_dyn_arg {
108 encode_dyn_cb(const void *buffer, size_t size, void *key) {
109 enc_dyn_arg *arg = key;
110 if(arg->length + size >= arg->allocated) {
111 size_t new_size = arg->allocated ? arg->allocated : 8;
116 } while(arg->length + size >= new_size);
118 p = REALLOC(arg->buffer, new_size);
120 FREEMEM(arg->buffer);
121 memset(arg, 0, sizeof(*arg));
125 arg->allocated = new_size;
127 memcpy(((char *)arg->buffer) + arg->length, buffer, size);
132 uper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
133 const asn_per_constraints_t *constraints,
134 const void *sptr, void **buffer_r) {
135 asn_enc_rval_t er = {0,0,0};
138 memset(&key, 0, sizeof(key));
140 er = uper_encode(td, constraints, sptr, encode_dyn_cb, &key);
147 key.buffer = MALLOC(1);
149 *(char *)key.buffer = '\0';
150 *buffer_r = key.buffer;
156 *buffer_r = key.buffer;
157 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
158 return ((er.encoded + 7) >> 3);
163 * Internally useful functions.
166 /* Flush partially filled buffer */
168 _uper_encode_flush_outp(asn_per_outp_t *po) {
171 if(po->nboff == 0 && po->buffer == po->tmpspace)
174 buf = po->buffer + (po->nboff >> 3);
175 /* Make sure we account for the last, partially filled */
176 if(po->nboff & 0x07) {
177 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
181 return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
185 aper_encode_to_buffer(const asn_TYPE_descriptor_t *td,
186 const asn_per_constraints_t *constraints,
187 const void *sptr, void *buffer, size_t buffer_size) {
191 key.left = buffer_size;
193 if(td) ASN_DEBUG("Encoding \"%s\" using ALIGNED PER", td->name);
195 return aper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
199 aper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
200 const asn_per_constraints_t *constraints,
201 const void *sptr, void **buffer_r) {
202 asn_enc_rval_t er = {0,0,0};
205 memset(&key, 0, sizeof(key));
207 er = aper_encode(td, constraints, sptr, encode_dyn_cb, &key);
214 key.buffer = MALLOC(1);
216 *(char *)key.buffer = '\0';
217 *buffer_r = key.buffer;
223 *buffer_r = key.buffer;
224 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
225 return ((er.encoded + 7) >> 3);
230 _aper_encode_flush_outp(asn_per_outp_t *po) {
233 if(po->nboff == 0 && po->buffer == po->tmpspace)
236 buf = po->buffer + (po->nboff >> 3);
237 /* Make sure we account for the last, partially filled */
238 if(po->nboff & 0x07) {
239 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
244 return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
250 aper_encode(const asn_TYPE_descriptor_t *td,
251 const asn_per_constraints_t *constraints,
252 const void *sptr, asn_app_consume_bytes_f *cb, void *app_key) {
254 asn_enc_rval_t er = {0,0,0};
257 * Invoke type-specific encoder.
259 if(!td || !td->op->aper_encoder)
260 ASN__ENCODE_FAILED; /* PER is not compiled in */
262 po.buffer = po.tmpspace;
264 po.nbits = 8 * sizeof(po.tmpspace);
267 po.flushed_bytes = 0;
269 er = td->op->aper_encoder(td, constraints, sptr, &po);
270 if(er.encoded != -1) {
271 size_t bits_to_flush;
273 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
275 /* Set number of bits encoded to a firm value */
276 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
278 if(_aper_encode_flush_outp(&po))