blob: 960981adbdd5590a372da3dcf970afd5b6d420e3 [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;
91 return ((er.encoded + 7) >> 3);
92 }
93}
94
95/*
96 * Internally useful functions.
97 */
98
99/* Flush partially filled buffer */
vlm18dd82c2006-08-18 01:34:18 +0000100static int
101_uper_encode_flush_outp(asn_per_outp_t *po) {
102 uint8_t *buf;
103
104 if(po->nboff == 0 && po->buffer == po->tmpspace)
105 return 0;
106
107 buf = po->buffer + (po->nboff >> 3);
108 /* Make sure we account for the last, partially filled */
109 if(po->nboff & 0x07) {
110 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
111 buf++;
112 }
113
114 return po->outper(po->tmpspace, buf - po->tmpspace, po->op_key);
115}
vlm252357f2007-06-23 23:50:25 +0000116
117static asn_enc_rval_t
118uper_encode_internal(asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints, void *sptr, asn_app_consume_bytes_f *cb, void *app_key) {
119 asn_per_outp_t po;
120 asn_enc_rval_t er;
121
122 /*
123 * Invoke type-specific encoder.
124 */
125 if(!td || !td->uper_encoder)
126 _ASN_ENCODE_FAILED; /* PER is not compiled in */
127
128 po.buffer = po.tmpspace;
129 po.nboff = 0;
130 po.nbits = 8 * sizeof(po.tmpspace);
131 po.outper = cb;
132 po.op_key = app_key;
133 po.flushed_bytes = 0;
134
135 er = td->uper_encoder(td, constraints, sptr, &po);
136 if(er.encoded != -1) {
137 size_t bits_to_flush;
138
139 bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
140
141 /* Set number of bits encoded to a firm value */
142 er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
143
144 if(_uper_encode_flush_outp(&po))
145 _ASN_ENCODE_FAILED;
146 }
147
148 return er;
149}
150