2 * Copyright (c) 2004-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
5 #include <asn_internal.h>
9 asn_OCTET_STRING_specifics_t asn_SPC_ANY_specs = {
11 offsetof(ANY_t, _asn_ctx),
14 asn_TYPE_operation_t asn_OP_ANY = {
18 OCTET_STRING_decode_ber,
19 OCTET_STRING_encode_der,
20 OCTET_STRING_decode_xer_hex,
22 #ifdef ASN_DISABLE_OER_SUPPORT
28 #endif /* ASN_DISABLE_OER_SUPPORT */
29 #ifdef ASN_DISABLE_PER_SUPPORT
34 #endif /* ASN_DISABLE_PER_SUPPORT */
35 0, /* Random fill is not defined for ANY type */
36 0 /* Use generic outmost tag fetcher */
38 asn_TYPE_descriptor_t asn_DEF_ANY = {
43 { 0, 0, asn_generic_no_constraint }, /* No constraints */
44 0, 0, /* No members */
49 #define RETURN(_code) \
51 asn_dec_rval_t tmprval; \
52 tmprval.code = _code; \
53 tmprval.consumed = consumed_myself; \
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,
61 if(flags & XER_F_CANONICAL) {
63 * Canonical XER-encoding of ANY type is not supported.
69 return OCTET_STRING_encode_xer(td, sptr, ilevel, flags, cb, app_key);
72 struct _callback_arg {
78 static int ANY__consume_bytes(const void *buffer, size_t size, void *key);
81 ANY_fromType(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
82 struct _callback_arg arg;
91 if(st->buf) FREEMEM(st->buf);
96 arg.offset = arg.size = 0;
99 erval = der_encode(td, sptr, ANY__consume_bytes, &arg);
100 if(erval.encoded == -1) {
101 if(arg.buffer) FREEMEM(arg.buffer);
104 assert((size_t)erval.encoded == arg.offset);
106 if(st->buf) FREEMEM(st->buf);
107 st->buf = arg.buffer;
108 st->size = arg.offset;
114 ANY_new_fromType(asn_TYPE_descriptor_t *td, void *sptr) {
123 memset(&tmp, 0, sizeof(tmp));
125 if(ANY_fromType(&tmp, td, sptr)) return 0;
127 st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
138 ANY_to_type(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
142 if(!st || !td || !struct_ptr) {
148 /* Nothing to convert, make it empty. */
149 *struct_ptr = (void *)0;
153 rval = ber_decode(0, td, (void **)&newst, st->buf, st->size);
154 if(rval.code == RC_OK) {
158 /* Remove possibly partially decoded data. */
159 ASN_STRUCT_FREE(*td, newst);
164 static int ANY__consume_bytes(const void *buffer, size_t size, void *key) {
165 struct _callback_arg *arg = (struct _callback_arg *)key;
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);
171 arg->buffer = (uint8_t *)p;
175 memcpy(arg->buffer + arg->offset, buffer, size);
177 assert(arg->offset < arg->size);
182 #ifndef ASN_DISABLE_PER_SUPPORT
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;
194 ANY_t *st = (ANY_t *)*sptr;
200 * Allocate the structure.
203 st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
204 if(!st) RETURN(RC_FAIL);
207 ASN_DEBUG("PER Decoding ANY type");
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;
223 ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
224 repeat ? "repeat" : "once", td->name);
226 len_bits = len_bytes * 8;
228 p = REALLOC(st->buf, st->size + len_bytes + 1);
229 if(!p) RETURN(RC_FAIL);
230 st->buf = (uint8_t *)p;
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;
237 st->buf[st->size] = 0; /* nul-terminate */
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};
254 if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
260 ssize_t may_save = uper_put_length(po, size, &need_eom);
261 if(may_save < 0) ASN__ENCODE_FAILED;
263 ret = per_put_many_bits(po, buf, may_save * 8);
264 if(ret) ASN__ENCODE_FAILED;
268 assert(!(may_save & 0x07) || !size);
269 if(need_eom && uper_put_length(po, 0, 0))
270 ASN__ENCODE_FAILED; /* End of Message length */
276 #endif /* ASN_DISABLE_PER_SUPPORT */