1 #include <asn_application.h>
2 #include <asn_internal.h>
3 #include <per_encoder.h>
5 /* Flush partially filled buffer */
6 static int _uper_encode_flush_outp(asn_per_outp_t *po);
9 uper_encode(asn_TYPE_descriptor_t *td, void *sptr, asn_app_consume_bytes_f *cb, void *app_key) {
14 * Invoke type-specific encoder.
16 if(!td || !td->uper_encoder)
17 _ASN_ENCODE_FAILED; /* PER is not compiled in */
19 po.buffer = po.tmpspace;
21 po.nbits = 8 * sizeof(po.tmpspace);
26 er = td->uper_encoder(td, 0, sptr, &po);
27 if(er.encoded != -1) {
30 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
32 /* Set number of bits encoded to a firm value */
33 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
35 if(_uper_encode_flush_outp(&po))
43 * Argument type and callback necessary for uper_encode_to_buffer().
45 typedef struct enc_to_buf_arg {
49 static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
50 enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
53 return -1; /* Data exceeds the available buffer size */
55 memcpy(arg->buffer, buffer, size);
56 arg->buffer = ((char *)arg->buffer) + size;
63 uper_encode_to_buffer(asn_TYPE_descriptor_t *td, void *sptr, void *buffer, size_t buffer_size) {
67 * Invoke type-specific encoder.
69 if(!td || !td->uper_encoder)
70 _ASN_ENCODE_FAILED; /* PER is not compiled in */
73 key.left = buffer_size;
75 ASN_DEBUG("Encoding \"%s\" using UNALIGNED PER", td->name);
77 return uper_encode(td, sptr, encode_to_buffer_cb, &key);
81 _uper_encode_flush_outp(asn_per_outp_t *po) {
84 if(po->nboff == 0 && po->buffer == po->tmpspace)
87 buf = po->buffer + (po->nboff >> 3);
88 /* Make sure we account for the last, partially filled */
89 if(po->nboff & 0x07) {
90 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
94 return po->outper(po->tmpspace, buf - po->tmpspace, po->op_key);