1 #include <asn_application.h>
2 #include <asn_internal.h>
3 #include <per_encoder.h>
5 static int _uper_encode_flush_outp(asn_per_outp_t *po);
8 ignore_output(const void *data, size_t size, void *app_key) {
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) {
20 asn_enc_rval_t er = {0,0,0};
23 * Invoke type-specific encoder.
25 if(!td || !td->op->uper_encoder)
26 ASN__ENCODE_FAILED; /* PER is not compiled in */
28 po.buffer = po.tmpspace;
30 po.nbits = 8 * sizeof(po.tmpspace);
31 po.output = cb ? cb : ignore_output;
35 er = td->op->uper_encoder(td, constraints, sptr, &po);
36 if(er.encoded != -1) {
39 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
41 /* Set number of bits encoded to a firm value */
42 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
44 if(_uper_encode_flush_outp(&po)) ASN__ENCODE_FAILED;
48 printf("else return val er.encoded %u, er.failed_type->name '%s'\n", er.encoded, er.failed_type->name);
55 * Argument type and callback necessary for uper_encode_to_buffer().
57 typedef struct 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;
65 return -1; /* Data exceeds the available buffer size */
67 memcpy(arg->buffer, buffer, size);
68 arg->buffer = ((char *)arg->buffer) + size;
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) {
81 key.left = buffer_size;
83 if(td) ASN_DEBUG("Encoding \"%s\" using UNALIGNED PER", td->name);
85 return uper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
88 typedef struct enc_dyn_arg {
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;
102 } while(arg->length + size >= new_size);
104 p = REALLOC(arg->buffer, new_size);
106 FREEMEM(arg->buffer);
107 memset(arg, 0, sizeof(*arg));
111 arg->allocated = new_size;
113 memcpy(((char *)arg->buffer) + arg->length, buffer, size);
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};
124 memset(&key, 0, sizeof(key));
126 er = uper_encode(td, constraints, sptr, encode_dyn_cb, &key);
133 key.buffer = MALLOC(1);
135 *(char *)key.buffer = '\0';
136 *buffer_r = key.buffer;
142 *buffer_r = key.buffer;
143 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
144 return ((er.encoded + 7) >> 3);
149 * Internally useful functions.
152 /* Flush partially filled buffer */
154 _uper_encode_flush_outp(asn_per_outp_t *po) {
157 if(po->nboff == 0 && po->buffer == po->tmpspace)
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));
167 return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
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) {
177 key.left = buffer_size;
179 if(td) ASN_DEBUG("Encoding \"%s\" using ALIGNED PER", td->name);
181 return aper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
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};
191 memset(&key, 0, sizeof(key));
193 er = aper_encode(td, constraints, sptr, encode_dyn_cb, &key);
200 key.buffer = MALLOC(1);
202 *(char *)key.buffer = '\0';
203 *buffer_r = key.buffer;
209 *buffer_r = key.buffer;
210 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
211 return ((er.encoded + 7) >> 3);
216 _aper_encode_flush_outp(asn_per_outp_t *po) {
219 if(po->nboff == 0 && po->buffer == po->tmpspace)
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));
230 return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
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) {
240 asn_enc_rval_t er = {0,0,0};
243 * Invoke type-specific encoder.
245 if(!td || !td->op->aper_encoder)
246 ASN__ENCODE_FAILED; /* PER is not compiled in */
248 po.buffer = po.tmpspace;
250 po.nbits = 8 * sizeof(po.tmpspace);
253 po.flushed_bytes = 0;
255 er = td->op->aper_encoder(td, constraints, sptr, &po);
256 if(er.encoded != -1) {
257 size_t bits_to_flush;
259 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
261 /* Set number of bits encoded to a firm value */
262 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
264 if(_aper_encode_flush_outp(&po))