blob: f4bace060a350dd6b70fb3512acb0117be35ef90 [file] [log] [blame]
vlm18dd82c2006-08-18 01:34:18 +00001#include <asn_application.h>
2#include <asn_internal.h>
3#include <per_encoder.h>
4
vlm252357f2007-06-23 23:50:25 +00005static asn_enc_rval_t uper_encode_internal(asn_TYPE_descriptor_t *td, asn_per_constraints_t *, void *sptr, asn_app_consume_bytes_f *cb, void *app_key);
vlm18dd82c2006-08-18 01:34:18 +00006
7asn_enc_rval_t
8uper_encode(asn_TYPE_descriptor_t *td, void *sptr, asn_app_consume_bytes_f *cb, void *app_key) {
vlm252357f2007-06-23 23:50:25 +00009 return uper_encode_internal(td, 0, sptr, cb, app_key);
vlm18dd82c2006-08-18 01:34:18 +000010}
11
12/*
13 * Argument type and callback necessary for uper_encode_to_buffer().
14 */
15typedef struct enc_to_buf_arg {
16 void *buffer;
17 size_t left;
18} enc_to_buf_arg;
19static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
20 enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
21
22 if(arg->left < size)
23 return -1; /* Data exceeds the available buffer size */
24
25 memcpy(arg->buffer, buffer, size);
26 arg->buffer = ((char *)arg->buffer) + size;
27 arg->left -= size;
28
29 return 0;
30}
31
32asn_enc_rval_t
33uper_encode_to_buffer(asn_TYPE_descriptor_t *td, void *sptr, void *buffer, size_t buffer_size) {
34 enc_to_buf_arg key;
vlm18dd82c2006-08-18 01:34:18 +000035
vlm18dd82c2006-08-18 01:34:18 +000036 key.buffer = buffer;
37 key.left = buffer_size;
38
vlm252357f2007-06-23 23:50:25 +000039 if(td) ASN_DEBUG("Encoding \"%s\" using UNALIGNED PER", td->name);
vlm18dd82c2006-08-18 01:34:18 +000040
vlm252357f2007-06-23 23:50:25 +000041 return uper_encode_internal(td, 0, sptr, encode_to_buffer_cb, &key);
vlm18dd82c2006-08-18 01:34:18 +000042}
43
vlm252357f2007-06-23 23:50:25 +000044typedef struct enc_dyn_arg {
45 void *buffer;
46 size_t length;
47 size_t allocated;
48} enc_dyn_arg;
49static int
50encode_dyn_cb(const void *buffer, size_t size, void *key) {
51 enc_dyn_arg *arg = key;
52 if(arg->length + size >= arg->allocated) {
53 void *p;
54 arg->allocated = arg->allocated ? (arg->allocated << 2) : size;
55 p = REALLOC(arg->buffer, arg->allocated);
56 if(!p) {
57 FREEMEM(arg->buffer);
58 memset(arg, 0, sizeof(*arg));
59 return -1;
60 }
61 arg->buffer = p;
62 }
63 memcpy(((char *)arg->buffer) + arg->length, buffer, size);
64 arg->length += size;
65 return 0;
66}
67ssize_t
68uper_encode_to_new_buffer(asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints, void *sptr, void **buffer_r) {
69 asn_enc_rval_t er;
70 enc_dyn_arg key;
71
72 memset(&key, 0, sizeof(key));
73
74 er = uper_encode_internal(td, constraints, sptr, encode_dyn_cb, &key);
75 switch(er.encoded) {
76 case -1:
77 FREEMEM(key.buffer);
78 return -1;
79 case 0:
80 FREEMEM(key.buffer);
81 key.buffer = MALLOC(1);
82 if(key.buffer) {
83 *(char *)key.buffer = '\0';
84 *buffer_r = key.buffer;
85 return 1;
86 } else {
87 return -1;
88 }
89 default:
90 *buffer_r = key.buffer;
vlm182758f2007-06-26 23:56:54 +000091 ASN_DEBUG("Complete encoded in %d bits", er.encoded);
vlm252357f2007-06-23 23:50:25 +000092 return ((er.encoded + 7) >> 3);
93 }
94}
95
96/*
97 * Internally useful functions.
98 */
99
100/* Flush partially filled buffer */
vlm18dd82c2006-08-18 01:34:18 +0000101static int
102_uper_encode_flush_outp(asn_per_outp_t *po) {
103 uint8_t *buf;
104
105 if(po->nboff == 0 && po->buffer == po->tmpspace)
106 return 0;
107
108 buf = po->buffer + (po->nboff >> 3);
109 /* Make sure we account for the last, partially filled */
110 if(po->nboff & 0x07) {
111 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
112 buf++;
113 }
114
115 return po->outper(po->tmpspace, buf - po->tmpspace, po->op_key);
116}
vlm252357f2007-06-23 23:50:25 +0000117
118static asn_enc_rval_t
119uper_encode_internal(asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints, void *sptr, asn_app_consume_bytes_f *cb, void *app_key) {
120 asn_per_outp_t po;
121 asn_enc_rval_t er;
122
123 /*
124 * Invoke type-specific encoder.
125 */
126 if(!td || !td->uper_encoder)
127 _ASN_ENCODE_FAILED; /* PER is not compiled in */
128
129 po.buffer = po.tmpspace;
130 po.nboff = 0;
131 po.nbits = 8 * sizeof(po.tmpspace);
132 po.outper = cb;
133 po.op_key = app_key;
134 po.flushed_bytes = 0;
135
136 er = td->uper_encoder(td, constraints, sptr, &po);
137 if(er.encoded != -1) {
138 size_t bits_to_flush;
139
140 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
141
142 /* Set number of bits encoded to a firm value */
143 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
144
145 if(_uper_encode_flush_outp(&po))
146 _ASN_ENCODE_FAILED;
147 }
148
149 return er;
150}
151