1 /*
2 * Copyright (c) 2004-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5 #include "asn1/asn1c/asn_internal.h"
6 #include "asn1/asn1c/ANY.h"
7 #include <errno.h>
8
9 asn_OCTET_STRING_specifics_t asn_SPC_ANY_specs = {
10 sizeof(ANY_t),
11 offsetof(ANY_t, _asn_ctx),
12 ASN_OSUBV_ANY
13 };
14 asn_TYPE_operation_t asn_OP_ANY = {
15 OCTET_STRING_free,
16 OCTET_STRING_print,
17 OCTET_STRING_compare,
18 OCTET_STRING_decode_ber,
19 OCTET_STRING_encode_der,
20 OCTET_STRING_decode_xer_hex,
21 ANY_encode_xer,
22 #ifdef ASN_DISABLE_OER_SUPPORT
23 0,
24 0,
25 #else
26 0,
27 0,
28 #endif /* ASN_DISABLE_OER_SUPPORT */
29 #ifdef ASN_DISABLE_PER_SUPPORT
30 0, 0,
31 #else
32 ANY_decode_uper,
33 ANY_encode_uper,
34 #endif /* ASN_DISABLE_PER_SUPPORT */
35 0, /* Random fill is not defined for ANY type */
36 0 /* Use generic outmost tag fetcher */
37 };
38 asn_TYPE_descriptor_t asn_DEF_ANY = {
39 "ANY",
40 "ANY",
41 &asn_OP_ANY,
42 0, 0, 0, 0,
43 { 0, 0, asn_generic_no_constraint }, /* No constraints */
44 0, 0, /* No members */
45 &asn_SPC_ANY_specs,
46 };
47
48 #undef RETURN
49 #define RETURN(_code) \
50 do { \
51 asn_dec_rval_t tmprval; \
52 tmprval.code = _code; \
53 tmprval.consumed = consumed_myself; \
54 return tmprval; \
55 } while(0)
56
57 asn_enc_rval_t
ANY_encode_xer(const asn_TYPE_descriptor_t * td,const void * sptr,int ilevel,enum xer_encoder_flags_e flags,asn_app_consume_bytes_f * cb,void * app_key)58 ANY_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
59 enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
60 void *app_key) {
61 if(flags & XER_F_CANONICAL) {
62 /*
63 * Canonical XER-encoding of ANY type is not supported.
64 */
65 ASN__ENCODE_FAILED;
66 }
67
68 /* Dump as binary */
69 return OCTET_STRING_encode_xer(td, sptr, ilevel, flags, cb, app_key);
70 }
71
72 struct _callback_arg {
73 uint8_t *buffer;
74 size_t offset;
75 size_t size;
76 };
77
78 static int ANY__consume_bytes(const void *buffer, size_t size, void *key);
79
80 int
ANY_fromType(ANY_t * st,asn_TYPE_descriptor_t * td,void * sptr)81 ANY_fromType(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
82 struct _callback_arg arg;
83 asn_enc_rval_t erval;
84
85 if(!st || !td) {
86 errno = EINVAL;
87 return -1;
88 }
89
90 if(!sptr) {
91 if(st->buf) FREEMEM(st->buf);
92 st->size = 0;
93 return 0;
94 }
95
96 arg.offset = arg.size = 0;
97 arg.buffer = 0;
98
99 erval = der_encode(td, sptr, ANY__consume_bytes, &arg);
100 if(erval.encoded == -1) {
101 if(arg.buffer) FREEMEM(arg.buffer);
102 return -1;
103 }
104 assert((size_t)erval.encoded == arg.offset);
105
106 if(st->buf) FREEMEM(st->buf);
107 st->buf = arg.buffer;
108 st->size = arg.offset;
109
110 return 0;
111 }
112
113 ANY_t *
ANY_new_fromType(asn_TYPE_descriptor_t * td,void * sptr)114 ANY_new_fromType(asn_TYPE_descriptor_t *td, void *sptr) {
115 ANY_t tmp;
116 ANY_t *st;
117
118 if(!td || !sptr) {
119 errno = EINVAL;
120 return 0;
121 }
122
123 memset(&tmp, 0, sizeof(tmp));
124
125 if(ANY_fromType(&tmp, td, sptr)) return 0;
126
127 st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
128 if(st) {
129 *st = tmp;
130 return st;
131 } else {
132 FREEMEM(tmp.buf);
133 return 0;
134 }
135 }
136
137 int
ANY_to_type(ANY_t * st,asn_TYPE_descriptor_t * td,void ** struct_ptr)138 ANY_to_type(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
139 asn_dec_rval_t rval;
140 void *newst = 0;
141
142 if(!st || !td || !struct_ptr) {
143 errno = EINVAL;
144 return -1;
145 }
146
147 if(st->buf == 0) {
148 /* Nothing to convert, make it empty. */
149 *struct_ptr = (void *)0;
150 return 0;
151 }
152
153 rval = ber_decode(0, td, (void **)&newst, st->buf, st->size);
154 if(rval.code == RC_OK) {
155 *struct_ptr = newst;
156 return 0;
157 } else {
158 /* Remove possibly partially decoded data. */
159 ASN_STRUCT_FREE(*td, newst);
160 return -1;
161 }
162 }
163
ANY__consume_bytes(const void * buffer,size_t size,void * key)164 static int ANY__consume_bytes(const void *buffer, size_t size, void *key) {
165 struct _callback_arg *arg = (struct _callback_arg *)key;
166
167 if((arg->offset + size) >= arg->size) {
168 size_t nsize = (arg->size ? arg->size << 2 : 16) + size;
169 void *p = REALLOC(arg->buffer, nsize);
170 if(!p) return -1;
171 arg->buffer = (uint8_t *)p;
172 arg->size = nsize;
173 }
174
175 memcpy(arg->buffer + arg->offset, buffer, size);
176 arg->offset += size;
177 assert(arg->offset < arg->size);
178
179 return 0;
180 }
181
182 #ifndef ASN_DISABLE_PER_SUPPORT
183
184 asn_dec_rval_t
ANY_decode_uper(const asn_codec_ctx_t * opt_codec_ctx,const asn_TYPE_descriptor_t * td,const asn_per_constraints_t * constraints,void ** sptr,asn_per_data_t * pd)185 ANY_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
186 const asn_TYPE_descriptor_t *td,
187 const asn_per_constraints_t *constraints, void **sptr,
188 asn_per_data_t *pd) {
189 const asn_OCTET_STRING_specifics_t *specs =
190 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
191 : &asn_SPC_ANY_specs;
192 size_t consumed_myself = 0;
193 int repeat;
194 ANY_t *st = (ANY_t *)*sptr;
195
196 (void)opt_codec_ctx;
197 (void)constraints;
198
199 /*
200 * Allocate the structure.
201 */
202 if(!st) {
203 st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
204 if(!st) RETURN(RC_FAIL);
205 }
206
207 ASN_DEBUG("PER Decoding ANY type");
208
209
210 st->size = 0;
211 do {
212 ssize_t raw_len;
213 ssize_t len_bytes;
214 ssize_t len_bits;
215 void *p;
216 int ret;
217
218 /* Get the PER length */
219 raw_len = uper_get_length(pd, -1, 0, &repeat);
220 if(raw_len < 0) RETURN(RC_WMORE);
221 if(raw_len == 0 && st->buf) break;
222
223 ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
224 repeat ? "repeat" : "once", td->name);
225 len_bytes = raw_len;
226 len_bits = len_bytes * 8;
227
228 p = REALLOC(st->buf, st->size + len_bytes + 1);
229 if(!p) RETURN(RC_FAIL);
230 st->buf = (uint8_t *)p;
231
232 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
233 if(ret < 0) RETURN(RC_WMORE);
234 consumed_myself += len_bits;
235 st->size += len_bytes;
236 } while(repeat);
237 st->buf[st->size] = 0; /* nul-terminate */
238
239 RETURN(RC_OK);
240 }
241
242 asn_enc_rval_t
ANY_encode_uper(const asn_TYPE_descriptor_t * td,const asn_per_constraints_t * constraints,const void * sptr,asn_per_outp_t * po)243 ANY_encode_uper(const asn_TYPE_descriptor_t *td,
244 const asn_per_constraints_t *constraints, const void *sptr,
245 asn_per_outp_t *po) {
246 const ANY_t *st = (const ANY_t *)sptr;
247 asn_enc_rval_t er = {0, 0, 0};
248 const uint8_t *buf;
249 size_t size;
250 int ret;
251
252 (void)constraints;
253
254 if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
255
256 buf = st->buf;
257 size = st->size;
258 do {
259 int need_eom = 0;
260 ssize_t may_save = uper_put_length(po, size, &need_eom);
261 if(may_save < 0) ASN__ENCODE_FAILED;
262
263 ret = per_put_many_bits(po, buf, may_save * 8);
264 if(ret) ASN__ENCODE_FAILED;
265
266 buf += may_save;
267 size -= may_save;
268 assert(!(may_save & 0x07) || !size);
269 if(need_eom && uper_put_length(po, 0, 0))
270 ASN__ENCODE_FAILED; /* End of Message length */
271 } while(size);
272
273 ASN__ENCODED_OK(er);
274 }
275
276 #endif /* ASN_DISABLE_PER_SUPPORT */
277
278