3 * Copyright (c) 2004-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
10 asn_OCTET_STRING_specifics_t asn_SPC_ANY_specs = {
12 offsetof(ANY_t, _asn_ctx),
15 asn_TYPE_operation_t asn_OP_ANY = {
19 OCTET_STRING_decode_ber,
20 OCTET_STRING_encode_der,
21 OCTET_STRING_decode_xer_hex,
23 #ifdef ASN_DISABLE_OER_SUPPORT
29 #endif /* ASN_DISABLE_OER_SUPPORT */
30 #ifdef ASN_DISABLE_PER_SUPPORT
37 #endif /* ASN_DISABLE_PER_SUPPORT */
38 0, /* Random fill is not defined for ANY type */
39 0 /* Use generic outmost tag fetcher */
41 asn_TYPE_descriptor_t asn_DEF_ANY = {
46 { 0, 0, asn_generic_no_constraint }, /* No constraints */
47 0, 0, /* No members */
52 #define RETURN(_code) \
54 asn_dec_rval_t tmprval; \
55 tmprval.code = _code; \
56 tmprval.consumed = consumed_myself; \
61 ANY_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
62 enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
64 if(flags & XER_F_CANONICAL) {
66 * Canonical XER-encoding of ANY type is not supported.
72 return OCTET_STRING_encode_xer(td, sptr, ilevel, flags, cb, app_key);
75 struct _callback_arg {
81 static int ANY__consume_bytes(const void *buffer, size_t size, void *key);
84 ANY_fromType(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
85 struct _callback_arg arg;
86 asn_enc_rval_t erval = {0,0,0};
94 if(st->buf) FREEMEM(st->buf);
99 arg.offset = arg.size = 0;
102 erval = der_encode(td, sptr, ANY__consume_bytes, &arg);
103 if(erval.encoded == -1) {
104 if(arg.buffer) FREEMEM(arg.buffer);
107 assert((size_t)erval.encoded == arg.offset);
109 if(st->buf) FREEMEM(st->buf);
110 st->buf = arg.buffer;
111 st->size = arg.offset;
117 ANY_fromType_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
118 uint8_t *buffer = NULL;
127 if(st->buf) FREEMEM(st->buf);
132 erval = aper_encode_to_new_buffer(td, td->encoding_constraints.per_constraints, sptr, (void**)&buffer);
135 if(buffer) FREEMEM(buffer);
138 assert((size_t)erval > 0);
140 if(st->buf) FREEMEM(st->buf);
148 ANY_new_fromType(asn_TYPE_descriptor_t *td, void *sptr) {
157 memset(&tmp, 0, sizeof(tmp));
159 if(ANY_fromType(&tmp, td, sptr)) return 0;
161 st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
172 ANY_new_fromType_aper(asn_TYPE_descriptor_t *td, void *sptr) {
181 memset(&tmp, 0, sizeof(tmp));
183 if(ANY_fromType_aper(&tmp, td, sptr)) return 0;
185 st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
196 ANY_to_type(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
200 if(!st || !td || !struct_ptr) {
206 /* Nothing to convert, make it empty. */
207 *struct_ptr = (void *)0;
211 rval = ber_decode(0, td, (void **)&newst, st->buf, st->size);
212 if(rval.code == RC_OK) {
216 /* Remove possibly partially decoded data. */
217 ASN_STRUCT_FREE(*td, newst);
223 ANY_to_type_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
227 if(!st || !td || !struct_ptr) {
233 /* Nothing to convert, make it empty. */
234 *struct_ptr = (void *)0;
238 rval = aper_decode(0, td, (void **)&newst, st->buf, st->size, 0, 0);
239 if(rval.code == RC_OK) {
243 /* Remove possibly partially decoded data. */
244 ASN_STRUCT_FREE(*td, newst);
249 static int ANY__consume_bytes(const void *buffer, size_t size, void *key) {
250 struct _callback_arg *arg = (struct _callback_arg *)key;
252 if((arg->offset + size) >= arg->size) {
253 size_t nsize = (arg->size ? arg->size << 2 : 16) + size;
254 void *p = REALLOC(arg->buffer, nsize);
256 arg->buffer = (uint8_t *)p;
260 memcpy(arg->buffer + arg->offset, buffer, size);
262 assert(arg->offset < arg->size);
267 #ifndef ASN_DISABLE_PER_SUPPORT
270 ANY_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
271 const asn_TYPE_descriptor_t *td,
272 const asn_per_constraints_t *constraints, void **sptr,
273 asn_per_data_t *pd) {
274 const asn_OCTET_STRING_specifics_t *specs =
275 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
276 : &asn_SPC_ANY_specs;
277 size_t consumed_myself = 0;
279 ANY_t *st = (ANY_t *)*sptr;
285 * Allocate the structure.
288 st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
289 if(!st) RETURN(RC_FAIL);
292 ASN_DEBUG("UPER Decoding ANY type");
302 /* Get the PER length */
303 raw_len = uper_get_length(pd, -1, 0, &repeat);
304 if(raw_len < 0) RETURN(RC_WMORE);
305 if(raw_len == 0 && st->buf) break;
307 ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
308 repeat ? "repeat" : "once", td->name);
310 len_bits = len_bytes * 8;
312 p = REALLOC(st->buf, st->size + len_bytes + 1);
313 if(!p) RETURN(RC_FAIL);
314 st->buf = (uint8_t *)p;
316 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
317 if(ret < 0) RETURN(RC_WMORE);
318 consumed_myself += len_bits;
319 st->size += len_bytes;
321 st->buf[st->size] = 0; /* nul-terminate */
327 ANY_encode_uper(const asn_TYPE_descriptor_t *td,
328 const asn_per_constraints_t *constraints, const void *sptr,
329 asn_per_outp_t *po) {
330 const ANY_t *st = (const ANY_t *)sptr;
331 asn_enc_rval_t er = {0, 0, 0};
338 if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
344 ssize_t may_save = uper_put_length(po, size, &need_eom);
345 if(may_save < 0) ASN__ENCODE_FAILED;
347 ret = per_put_many_bits(po, buf, may_save * 8);
348 if(ret) ASN__ENCODE_FAILED;
352 assert(!(may_save & 0x07) || !size);
353 if(need_eom && uper_put_length(po, 0, 0))
354 ASN__ENCODE_FAILED; /* End of Message length */
361 ANY_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
362 const asn_TYPE_descriptor_t *td,
363 const asn_per_constraints_t *constraints, void **sptr,
364 asn_per_data_t *pd) {
365 const asn_OCTET_STRING_specifics_t *specs =
366 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
367 : &asn_SPC_ANY_specs;
368 size_t consumed_myself = 0;
370 ANY_t *st = (ANY_t *)*sptr;
376 * Allocate the structure.
379 st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
380 if(!st) RETURN(RC_FAIL);
383 ASN_DEBUG("APER Decoding ANY type");
393 /* Get the PER length */
394 raw_len = aper_get_length(pd, -1, 0, &repeat);
395 if(raw_len < 0) RETURN(RC_WMORE);
396 if(raw_len == 0 && st->buf) break;
398 ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
399 repeat ? "repeat" : "once", td->name);
401 len_bits = len_bytes * 8;
403 p = REALLOC(st->buf, st->size + len_bytes + 1);
404 if(!p) RETURN(RC_FAIL);
405 st->buf = (uint8_t *)p;
407 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
408 if(ret < 0) RETURN(RC_WMORE);
409 consumed_myself += len_bits;
410 st->size += len_bytes;
412 st->buf[st->size] = 0; /* nul-terminate */
418 ANY_encode_aper(const asn_TYPE_descriptor_t *td,
419 const asn_per_constraints_t *constraints, const void *sptr,
420 asn_per_outp_t *po) {
421 const ANY_t *st = (const ANY_t *)sptr;
422 asn_enc_rval_t er = {0, 0, 0};
429 if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
435 ssize_t may_save = uper_put_length(po, size, &need_eom);
436 if(may_save < 0) ASN__ENCODE_FAILED;
438 ret = per_put_many_bits(po, buf, may_save * 8);
439 if(ret) ASN__ENCODE_FAILED;
443 assert(!(may_save & 0x07) || !size);
444 if(need_eom && uper_put_length(po, 0, 0))
445 ASN__ENCODE_FAILED; /* End of Message length */
450 #endif /* ASN_DISABLE_PER_SUPPORT */