2 * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
5 #include <asn_internal.h>
9 static asn_OCTET_STRING_specifics_t asn_DEF_ANY_specs = {
11 offsetof(ANY_t, _asn_ctx),
14 asn_TYPE_descriptor_t asn_DEF_ANY = {
19 asn_generic_no_constraint,
20 OCTET_STRING_decode_ber,
21 OCTET_STRING_encode_der,
22 OCTET_STRING_decode_xer_hex,
25 0, /* Use generic outmost tag fetcher */
27 0, /* No PER visible constraints */
28 0, 0, /* No members */
34 ANY_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
35 int ilevel, enum xer_encoder_flags_e flags,
36 asn_app_consume_bytes_f *cb, void *app_key) {
38 if(flags & XER_F_CANONICAL) {
40 * Canonical XER-encoding of ANY type is not supported.
46 return OCTET_STRING_encode_xer(td, sptr, ilevel, flags, cb, app_key);
49 struct _callback_arg {
55 static int ANY__consume_bytes(const void *buffer, size_t size, void *key);
58 ANY_fromType(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
59 struct _callback_arg arg;
68 if(st->buf) FREEMEM(st->buf);
73 arg.offset = arg.size = 0;
76 erval = der_encode(td, sptr, ANY__consume_bytes, &arg);
77 if(erval.encoded == -1) {
78 if(arg.buffer) FREEMEM(arg.buffer);
81 assert((size_t)erval.encoded == arg.offset);
83 if(st->buf) FREEMEM(st->buf);
85 st->size = arg.offset;
91 ANY_new_fromType(asn_TYPE_descriptor_t *td, void *sptr) {
100 memset(&tmp, 0, sizeof(tmp));
102 if(ANY_fromType(&tmp, td, sptr)) return 0;
104 st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
115 ANY_to_type(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
119 if(!st || !td || !struct_ptr) {
125 /* Nothing to convert, make it empty. */
126 *struct_ptr = (void *)0;
130 rval = ber_decode(0, td, (void **)&newst, st->buf, st->size);
131 if(rval.code == RC_OK) {
135 /* Remove possibly partially decoded data. */
136 ASN_STRUCT_FREE(*td, newst);
141 static int ANY__consume_bytes(const void *buffer, size_t size, void *key) {
142 struct _callback_arg *arg = (struct _callback_arg *)key;
144 if((arg->offset + size) >= arg->size) {
145 size_t nsize = (arg->size ? arg->size << 2 : 16) + size;
146 void *p = REALLOC(arg->buffer, nsize);
148 arg->buffer = (uint8_t *)p;
152 memcpy(arg->buffer + arg->offset, buffer, size);
154 assert(arg->offset < arg->size);