blob: 4643d454d6a26e34aa1b4f37cc8b594cf6e12106 [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) {
83 enc_dyn_arg *arg = key;
84 if(arg->length + size >= arg->allocated) {
85 void *p;
86 arg->allocated = arg->allocated ? (arg->allocated << 2) : size;
87 p = REALLOC(arg->buffer, arg->allocated);
88 if(!p) {
89 FREEMEM(arg->buffer);
90 memset(arg, 0, sizeof(*arg));
91 return -1;
92 }
93 arg->buffer = p;
94 }
95 memcpy(((char *)arg->buffer) + arg->length, buffer, size);
96 arg->length += size;
97 return 0;
98}
99ssize_t
Lev Walkin20696a42017-10-17 21:27:33 -0700100uper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
101 const asn_per_constraints_t *constraints,
102 const void *sptr, void **buffer_r) {
Lev Walkin494fb702017-08-07 20:07:00 -0700103 asn_enc_rval_t er;
Lev Walkin62258e22007-06-23 23:50:25 +0000104 enc_dyn_arg key;
105
106 memset(&key, 0, sizeof(key));
107
Lev Walkin56153042017-10-24 00:47:03 -0700108 er = uper_encode(td, constraints, sptr, encode_dyn_cb, &key);
Lev Walkin62258e22007-06-23 23:50:25 +0000109 switch(er.encoded) {
110 case -1:
111 FREEMEM(key.buffer);
112 return -1;
113 case 0:
114 FREEMEM(key.buffer);
115 key.buffer = MALLOC(1);
116 if(key.buffer) {
117 *(char *)key.buffer = '\0';
118 *buffer_r = key.buffer;
119 return 1;
120 } else {
121 return -1;
122 }
123 default:
124 *buffer_r = key.buffer;
Lev Walkinfe1ffaf2010-10-25 21:07:59 -0700125 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
Lev Walkin62258e22007-06-23 23:50:25 +0000126 return ((er.encoded + 7) >> 3);
127 }
128}
129
130/*
131 * Internally useful functions.
132 */
133
134/* Flush partially filled buffer */
Lev Walkin523de9e2006-08-18 01:34:18 +0000135static int
136_uper_encode_flush_outp(asn_per_outp_t *po) {
137 uint8_t *buf;
138
139 if(po->nboff == 0 && po->buffer == po->tmpspace)
140 return 0;
141
142 buf = po->buffer + (po->nboff >> 3);
143 /* Make sure we account for the last, partially filled */
144 if(po->nboff & 0x07) {
145 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
146 buf++;
147 }
148
Lev Walkin6cd0d562017-08-25 11:57:01 -0700149 return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
Lev Walkin523de9e2006-08-18 01:34:18 +0000150}
Lev Walkin62258e22007-06-23 23:50:25 +0000151