blob: 17e5fce5e7fc8a48849de44870b9fb2c8068d3e1 [file] [log] [blame]
Lev Walkin523de9e2006-08-18 01:34:18 +00001#include <asn_application.h>
2#include <asn_internal.h>
3#include <per_encoder.h>
4
Lev Walkin56153042017-10-24 00:47:03 -07005static int _uper_encode_flush_outp(asn_per_outp_t *po);
Lev Walkin523de9e2006-08-18 01:34:18 +00006
7asn_enc_rval_t
Lev Walkin56153042017-10-24 00:47:03 -07008uper_encode(const asn_TYPE_descriptor_t *td,
9 const asn_per_constraints_t *constraints, const void *sptr,
Lev Walkin20696a42017-10-17 21:27:33 -070010 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin56153042017-10-24 00:47:03 -070011 asn_per_outp_t po;
12 asn_enc_rval_t er;
13
14 /*
15 * Invoke type-specific encoder.
16 */
17 if(!td || !td->op->uper_encoder)
18 ASN__ENCODE_FAILED; /* PER is not compiled in */
19
20 po.buffer = po.tmpspace;
21 po.nboff = 0;
22 po.nbits = 8 * sizeof(po.tmpspace);
23 po.output = cb;
24 po.op_key = app_key;
25 po.flushed_bytes = 0;
26
27 er = td->op->uper_encoder(td, constraints, sptr, &po);
28 if(er.encoded != -1) {
29 size_t bits_to_flush;
30
31 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
32
33 /* Set number of bits encoded to a firm value */
34 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
35
36 if(_uper_encode_flush_outp(&po)) ASN__ENCODE_FAILED;
37 }
38
39 return er;
Lev Walkin523de9e2006-08-18 01:34:18 +000040}
41
42/*
43 * Argument type and callback necessary for uper_encode_to_buffer().
44 */
45typedef struct enc_to_buf_arg {
46 void *buffer;
47 size_t left;
48} enc_to_buf_arg;
49static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
50 enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
51
52 if(arg->left < size)
53 return -1; /* Data exceeds the available buffer size */
54
55 memcpy(arg->buffer, buffer, size);
56 arg->buffer = ((char *)arg->buffer) + size;
57 arg->left -= size;
58
59 return 0;
60}
61
62asn_enc_rval_t
Lev Walkin56153042017-10-24 00:47:03 -070063uper_encode_to_buffer(const asn_TYPE_descriptor_t *td,
64 const asn_per_constraints_t *constraints,
65 const void *sptr, void *buffer, size_t buffer_size) {
Lev Walkin20696a42017-10-17 21:27:33 -070066 enc_to_buf_arg key;
Lev Walkin523de9e2006-08-18 01:34:18 +000067
Lev Walkin56153042017-10-24 00:47:03 -070068 key.buffer = buffer;
69 key.left = buffer_size;
Lev Walkin523de9e2006-08-18 01:34:18 +000070
Lev Walkin56153042017-10-24 00:47:03 -070071 if(td) ASN_DEBUG("Encoding \"%s\" using UNALIGNED PER", td->name);
Lev Walkin523de9e2006-08-18 01:34:18 +000072
Lev Walkin56153042017-10-24 00:47:03 -070073 return uper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
Lev Walkin523de9e2006-08-18 01:34:18 +000074}
75
Lev Walkin62258e22007-06-23 23:50:25 +000076typedef struct enc_dyn_arg {
77 void *buffer;
78 size_t length;
79 size_t allocated;
80} enc_dyn_arg;
81static int
82encode_dyn_cb(const void *buffer, size_t size, void *key) {
Lev Walkind8e6bbf2017-10-24 03:26:40 -070083 enc_dyn_arg *arg = key;
84 if(arg->length + size >= arg->allocated) {
85 size_t new_size = arg->allocated ? arg->allocated : 8;
86 void *p;
87
88 do {
89 new_size <<= 2;
90 } while(arg->length + size >= new_size);
91
92 p = REALLOC(arg->buffer, new_size);
93 if(!p) {
94 FREEMEM(arg->buffer);
95 memset(arg, 0, sizeof(*arg));
96 return -1;
97 }
98 arg->buffer = p;
99 arg->allocated = new_size;
100 }
101 memcpy(((char *)arg->buffer) + arg->length, buffer, size);
102 arg->length += size;
103 return 0;
Lev Walkin62258e22007-06-23 23:50:25 +0000104}
105ssize_t
Lev Walkin20696a42017-10-17 21:27:33 -0700106uper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
107 const asn_per_constraints_t *constraints,
108 const void *sptr, void **buffer_r) {
Lev Walkin494fb702017-08-07 20:07:00 -0700109 asn_enc_rval_t er;
Lev Walkin62258e22007-06-23 23:50:25 +0000110 enc_dyn_arg key;
111
112 memset(&key, 0, sizeof(key));
113
Lev Walkin56153042017-10-24 00:47:03 -0700114 er = uper_encode(td, constraints, sptr, encode_dyn_cb, &key);
Lev Walkin62258e22007-06-23 23:50:25 +0000115 switch(er.encoded) {
116 case -1:
117 FREEMEM(key.buffer);
118 return -1;
119 case 0:
120 FREEMEM(key.buffer);
121 key.buffer = MALLOC(1);
122 if(key.buffer) {
123 *(char *)key.buffer = '\0';
124 *buffer_r = key.buffer;
125 return 1;
126 } else {
127 return -1;
128 }
129 default:
130 *buffer_r = key.buffer;
Lev Walkinfe1ffaf2010-10-25 21:07:59 -0700131 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
Lev Walkin62258e22007-06-23 23:50:25 +0000132 return ((er.encoded + 7) >> 3);
133 }
134}
135
136/*
137 * Internally useful functions.
138 */
139
140/* Flush partially filled buffer */
Lev Walkin523de9e2006-08-18 01:34:18 +0000141static int
142_uper_encode_flush_outp(asn_per_outp_t *po) {
143 uint8_t *buf;
144
145 if(po->nboff == 0 && po->buffer == po->tmpspace)
146 return 0;
147
148 buf = po->buffer + (po->nboff >> 3);
149 /* Make sure we account for the last, partially filled */
150 if(po->nboff & 0x07) {
151 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
152 buf++;
153 }
154
Lev Walkin6cd0d562017-08-25 11:57:01 -0700155 return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
Lev Walkin523de9e2006-08-18 01:34:18 +0000156}
Lev Walkin62258e22007-06-23 23:50:25 +0000157