blob: 317897d1c7dceb7224b70e76d329bafabcd3cbb5 [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 Walkin20696a42017-10-17 21:27:33 -07005static asn_enc_rval_t uper_encode_internal(const asn_TYPE_descriptor_t *td,
6 const asn_per_constraints_t *,
7 const void *sptr,
8 asn_app_consume_bytes_f *cb,
9 void *app_key);
Lev Walkin523de9e2006-08-18 01:34:18 +000010
11asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -070012uper_encode(const asn_TYPE_descriptor_t *td, const void *sptr,
13 asn_app_consume_bytes_f *cb, void *app_key) {
14 return uper_encode_internal(td, 0, sptr, cb, app_key);
Lev Walkin523de9e2006-08-18 01:34:18 +000015}
16
17/*
18 * Argument type and callback necessary for uper_encode_to_buffer().
19 */
20typedef struct enc_to_buf_arg {
21 void *buffer;
22 size_t left;
23} enc_to_buf_arg;
24static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
25 enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
26
27 if(arg->left < size)
28 return -1; /* Data exceeds the available buffer size */
29
30 memcpy(arg->buffer, buffer, size);
31 arg->buffer = ((char *)arg->buffer) + size;
32 arg->left -= size;
33
34 return 0;
35}
36
37asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -070038uper_encode_to_buffer(const asn_TYPE_descriptor_t *td, const void *sptr,
39 void *buffer, size_t buffer_size) {
40 enc_to_buf_arg key;
Lev Walkin523de9e2006-08-18 01:34:18 +000041
Lev Walkin523de9e2006-08-18 01:34:18 +000042 key.buffer = buffer;
43 key.left = buffer_size;
44
Lev Walkin62258e22007-06-23 23:50:25 +000045 if(td) ASN_DEBUG("Encoding \"%s\" using UNALIGNED PER", td->name);
Lev Walkin523de9e2006-08-18 01:34:18 +000046
Lev Walkin62258e22007-06-23 23:50:25 +000047 return uper_encode_internal(td, 0, sptr, encode_to_buffer_cb, &key);
Lev Walkin523de9e2006-08-18 01:34:18 +000048}
49
Lev Walkin62258e22007-06-23 23:50:25 +000050typedef struct enc_dyn_arg {
51 void *buffer;
52 size_t length;
53 size_t allocated;
54} enc_dyn_arg;
55static int
56encode_dyn_cb(const void *buffer, size_t size, void *key) {
57 enc_dyn_arg *arg = key;
58 if(arg->length + size >= arg->allocated) {
59 void *p;
60 arg->allocated = arg->allocated ? (arg->allocated << 2) : size;
61 p = REALLOC(arg->buffer, arg->allocated);
62 if(!p) {
63 FREEMEM(arg->buffer);
64 memset(arg, 0, sizeof(*arg));
65 return -1;
66 }
67 arg->buffer = p;
68 }
69 memcpy(((char *)arg->buffer) + arg->length, buffer, size);
70 arg->length += size;
71 return 0;
72}
73ssize_t
Lev Walkin20696a42017-10-17 21:27:33 -070074uper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
75 const asn_per_constraints_t *constraints,
76 const void *sptr, void **buffer_r) {
Lev Walkin494fb702017-08-07 20:07:00 -070077 asn_enc_rval_t er;
Lev Walkin62258e22007-06-23 23:50:25 +000078 enc_dyn_arg key;
79
80 memset(&key, 0, sizeof(key));
81
82 er = uper_encode_internal(td, constraints, sptr, encode_dyn_cb, &key);
83 switch(er.encoded) {
84 case -1:
85 FREEMEM(key.buffer);
86 return -1;
87 case 0:
88 FREEMEM(key.buffer);
89 key.buffer = MALLOC(1);
90 if(key.buffer) {
91 *(char *)key.buffer = '\0';
92 *buffer_r = key.buffer;
93 return 1;
94 } else {
95 return -1;
96 }
97 default:
98 *buffer_r = key.buffer;
Lev Walkinfe1ffaf2010-10-25 21:07:59 -070099 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
Lev Walkin62258e22007-06-23 23:50:25 +0000100 return ((er.encoded + 7) >> 3);
101 }
102}
103
104/*
105 * Internally useful functions.
106 */
107
108/* Flush partially filled buffer */
Lev Walkin523de9e2006-08-18 01:34:18 +0000109static int
110_uper_encode_flush_outp(asn_per_outp_t *po) {
111 uint8_t *buf;
112
113 if(po->nboff == 0 && po->buffer == po->tmpspace)
114 return 0;
115
116 buf = po->buffer + (po->nboff >> 3);
117 /* Make sure we account for the last, partially filled */
118 if(po->nboff & 0x07) {
119 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
120 buf++;
121 }
122
Lev Walkin6cd0d562017-08-25 11:57:01 -0700123 return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
Lev Walkin523de9e2006-08-18 01:34:18 +0000124}
Lev Walkin62258e22007-06-23 23:50:25 +0000125
126static asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700127uper_encode_internal(const asn_TYPE_descriptor_t *td,
128 const asn_per_constraints_t *constraints, const void *sptr,
Lev Walkin494fb702017-08-07 20:07:00 -0700129 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin20696a42017-10-17 21:27:33 -0700130 asn_per_outp_t po;
Lev Walkin62258e22007-06-23 23:50:25 +0000131 asn_enc_rval_t er;
132
133 /*
134 * Invoke type-specific encoder.
135 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800136 if(!td || !td->op->uper_encoder)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700137 ASN__ENCODE_FAILED; /* PER is not compiled in */
Lev Walkin62258e22007-06-23 23:50:25 +0000138
139 po.buffer = po.tmpspace;
140 po.nboff = 0;
141 po.nbits = 8 * sizeof(po.tmpspace);
Lev Walkin6cd0d562017-08-25 11:57:01 -0700142 po.output = cb;
Lev Walkin62258e22007-06-23 23:50:25 +0000143 po.op_key = app_key;
144 po.flushed_bytes = 0;
145
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800146 er = td->op->uper_encoder(td, constraints, sptr, &po);
Lev Walkin62258e22007-06-23 23:50:25 +0000147 if(er.encoded != -1) {
148 size_t bits_to_flush;
149
150 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
151
152 /* Set number of bits encoded to a firm value */
153 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
154
155 if(_uper_encode_flush_outp(&po))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700156 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +0000157 }
158
159 return er;
160}
161