1 #include <asn_application.h>
2 #include <asn_internal.h>
3 #include <aper_encoder.h>
6 * Argument type and callback necessary for aper_encode_to_buffer().
8 typedef struct 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;
16 return -1; /* Data exceeds the available buffer size */
18 memcpy(arg->buffer, buffer, size);
19 arg->buffer = ((char *)arg->buffer) + size;
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) {
32 key.left = buffer_size;
34 if(td) ASN_DEBUG("Encoding \"%s\" using ALIGNED PER", td->name);
36 return aper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
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};
46 memset(&key, 0, sizeof(key));
48 er = aper_encode(td, constraints, sptr, encode_dyn_cb, &key);
55 key.buffer = MALLOC(1);
57 *(char *)key.buffer = '\0';
58 *buffer_r = key.buffer;
64 *buffer_r = key.buffer;
65 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
66 return ((er.encoded + 7) >> 3);
71 * Internally useful functions.
74 /* Flush partially filled buffer */
76 _aper_encode_flush_outp(asn_per_outp_t *po) {
79 if(po->nboff == 0 && po->buffer == po->tmpspace)
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));
90 return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
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) {
100 asn_enc_rval_t er = {0,0,0};
103 * Invoke type-specific encoder.
105 if(!td || !td->op->aper_encoder)
106 ASN__ENCODE_FAILED; /* PER is not compiled in */
108 po.buffer = po.tmpspace;
110 po.nbits = 8 * sizeof(po.tmpspace);
111 po.output = cb ? cb : ignore_output;
113 po.flushed_bytes = 0;
115 er = td->op->aper_encoder(td, constraints, sptr, &po);
116 if(er.encoded != -1) {
117 size_t bits_to_flush;
119 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
121 /* Set number of bits encoded to a firm value */
122 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
124 if(_aper_encode_flush_outp(&po))