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
36 #endif /* ASN_DISABLE_PER_SUPPORT */
37 0, /* Random fill is not defined for ANY type */
38 0 /* Use generic outmost tag fetcher */
40 asn_TYPE_descriptor_t asn_DEF_ANY = {
45 { 0, 0, asn_generic_no_constraint }, /* No constraints */
46 0, 0, /* No members */
51 #define RETURN(_code) \
53 asn_dec_rval_t tmprval; \
54 tmprval.code = _code; \
55 tmprval.consumed = consumed_myself; \
60 ANY_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
61 enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
63 if(flags & XER_F_CANONICAL) {
65 * Canonical XER-encoding of ANY type is not supported.
71 return OCTET_STRING_encode_xer(td, sptr, ilevel, flags, cb, app_key);
74 struct _callback_arg {
80 static int ANY__consume_bytes(const void *buffer, size_t size, void *key);
83 ANY_fromType(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
84 struct _callback_arg arg;
85 asn_enc_rval_t erval = {0,0,0};
93 if(st->buf) FREEMEM(st->buf);
98 arg.offset = arg.size = 0;
101 erval = der_encode(td, sptr, ANY__consume_bytes, &arg);
102 if(erval.encoded == -1) {
103 if(arg.buffer) FREEMEM(arg.buffer);
106 assert((size_t)erval.encoded == arg.offset);
108 if(st->buf) FREEMEM(st->buf);
109 st->buf = arg.buffer;
110 st->size = arg.offset;
116 ANY_fromType_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
117 uint8_t *buffer = NULL;
126 if(st->buf) FREEMEM(st->buf);
131 erval = aper_encode_to_new_buffer(td, td->encoding_constraints.per_constraints, sptr, (void**)&buffer);
134 if(buffer) FREEMEM(buffer);
137 assert((size_t)erval > 0);
139 if(st->buf) FREEMEM(st->buf);
147 ANY_new_fromType(asn_TYPE_descriptor_t *td, void *sptr) {
156 memset(&tmp, 0, sizeof(tmp));
158 if(ANY_fromType(&tmp, td, sptr)) return 0;
160 st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
171 ANY_new_fromType_aper(asn_TYPE_descriptor_t *td, void *sptr) {
180 memset(&tmp, 0, sizeof(tmp));
182 if(ANY_fromType_aper(&tmp, td, sptr)) return 0;
184 st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
195 ANY_to_type(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
199 if(!st || !td || !struct_ptr) {
205 /* Nothing to convert, make it empty. */
206 *struct_ptr = (void *)0;
210 rval = ber_decode(0, td, (void **)&newst, st->buf, st->size);
211 if(rval.code == RC_OK) {
215 /* Remove possibly partially decoded data. */
216 ASN_STRUCT_FREE(*td, newst);
222 ANY_to_type_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
226 if(!st || !td || !struct_ptr) {
232 /* Nothing to convert, make it empty. */
233 *struct_ptr = (void *)0;
237 rval = aper_decode(0, td, (void **)&newst, st->buf, st->size, 0, 0);
238 if(rval.code == RC_OK) {
242 /* Remove possibly partially decoded data. */
243 ASN_STRUCT_FREE(*td, newst);
248 static int ANY__consume_bytes(const void *buffer, size_t size, void *key) {
249 struct _callback_arg *arg = (struct _callback_arg *)key;
251 if((arg->offset + size) >= arg->size) {
252 size_t nsize = (arg->size ? arg->size << 2 : 16) + size;
253 void *p = REALLOC(arg->buffer, nsize);
255 arg->buffer = (uint8_t *)p;
259 memcpy(arg->buffer + arg->offset, buffer, size);
261 assert(arg->offset < arg->size);
266 #ifndef ASN_DISABLE_PER_SUPPORT
269 ANY_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
270 const asn_TYPE_descriptor_t *td,
271 const asn_per_constraints_t *constraints, void **sptr,
272 asn_per_data_t *pd) {
273 const asn_OCTET_STRING_specifics_t *specs =
274 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
275 : &asn_SPC_ANY_specs;
276 size_t consumed_myself = 0;
278 ANY_t *st = (ANY_t *)*sptr;
284 * Allocate the structure.
287 st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
288 if(!st) RETURN(RC_FAIL);
291 ASN_DEBUG("UPER Decoding ANY type");
301 /* Get the PER length */
302 raw_len = uper_get_length(pd, -1, 0, &repeat);
303 if(raw_len < 0) RETURN(RC_WMORE);
304 if(raw_len == 0 && st->buf) break;
306 ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
307 repeat ? "repeat" : "once", td->name);
309 len_bits = len_bytes * 8;
311 p = REALLOC(st->buf, st->size + len_bytes + 1);
312 if(!p) RETURN(RC_FAIL);
313 st->buf = (uint8_t *)p;
315 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
316 if(ret < 0) RETURN(RC_WMORE);
317 consumed_myself += len_bits;
318 st->size += len_bytes;
320 st->buf[st->size] = 0; /* nul-terminate */
326 ANY_encode_uper(const asn_TYPE_descriptor_t *td,
327 const asn_per_constraints_t *constraints, const void *sptr,
328 asn_per_outp_t *po) {
329 const ANY_t *st = (const ANY_t *)sptr;
330 asn_enc_rval_t er = {0, 0, 0};
337 if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
343 ssize_t may_save = uper_put_length(po, size, &need_eom);
344 if(may_save < 0) ASN__ENCODE_FAILED;
346 ret = per_put_many_bits(po, buf, may_save * 8);
347 if(ret) ASN__ENCODE_FAILED;
351 assert(!(may_save & 0x07) || !size);
352 if(need_eom && uper_put_length(po, 0, 0))
353 ASN__ENCODE_FAILED; /* End of Message length */
360 ANY_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
361 const asn_TYPE_descriptor_t *td,
362 const asn_per_constraints_t *constraints, void **sptr,
363 asn_per_data_t *pd) {
364 const asn_OCTET_STRING_specifics_t *specs =
365 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
366 : &asn_SPC_ANY_specs;
367 size_t consumed_myself = 0;
369 ANY_t *st = (ANY_t *)*sptr;
375 * Allocate the structure.
378 st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
379 if(!st) RETURN(RC_FAIL);
382 ASN_DEBUG("APER Decoding ANY type");
392 /* Get the PER length */
393 raw_len = aper_get_length(pd, -1, 0, &repeat);
394 if(raw_len < 0) RETURN(RC_WMORE);
395 if(raw_len == 0 && st->buf) break;
397 ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
398 repeat ? "repeat" : "once", td->name);
400 len_bits = len_bytes * 8;
402 p = REALLOC(st->buf, st->size + len_bytes + 1);
403 if(!p) RETURN(RC_FAIL);
404 st->buf = (uint8_t *)p;
406 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
407 if(ret < 0) RETURN(RC_WMORE);
408 consumed_myself += len_bits;
409 st->size += len_bytes;
411 st->buf[st->size] = 0; /* nul-terminate */
417 ANY_encode_aper(const asn_TYPE_descriptor_t *td,
418 const asn_per_constraints_t *constraints, const void *sptr,
419 asn_per_outp_t *po) {
420 const ANY_t *st = (const ANY_t *)sptr;
421 asn_enc_rval_t er = {0, 0, 0};
428 if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
434 ssize_t may_save = uper_put_length(po, size, &need_eom);
435 if(may_save < 0) ASN__ENCODE_FAILED;
437 ret = per_put_many_bits(po, buf, may_save * 8);
438 if(ret) ASN__ENCODE_FAILED;
442 assert(!(may_save & 0x07) || !size);
443 if(need_eom && uper_put_length(po, 0, 0))
444 ASN__ENCODE_FAILED; /* End of Message length */
449 #endif /* ASN_DISABLE_PER_SUPPORT */