2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
11 #define RETURN(_code) \
13 asn_dec_rval_t tmprval; \
14 tmprval.code = _code; \
15 tmprval.consumed = consumed_myself; \
20 ANY_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
21 const asn_TYPE_descriptor_t *td,
22 const asn_per_constraints_t *constraints, void **sptr,
24 const asn_OCTET_STRING_specifics_t *specs =
25 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
27 size_t consumed_myself = 0;
29 ANY_t *st = (ANY_t *)*sptr;
35 * Allocate the structure.
38 st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
39 if(!st) RETURN(RC_FAIL);
42 ASN_DEBUG("UPER Decoding ANY type");
52 /* Get the PER length */
53 raw_len = uper_get_length(pd, -1, 0, &repeat);
54 if(raw_len < 0) RETURN(RC_WMORE);
55 if(raw_len == 0 && st->buf) break;
57 ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
58 repeat ? "repeat" : "once", td->name);
60 len_bits = len_bytes * 8;
62 p = REALLOC(st->buf, st->size + len_bytes + 1);
63 if(!p) RETURN(RC_FAIL);
64 st->buf = (uint8_t *)p;
66 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
67 if(ret < 0) RETURN(RC_WMORE);
68 consumed_myself += len_bits;
69 st->size += len_bytes;
71 st->buf[st->size] = 0; /* nul-terminate */
77 ANY_encode_uper(const asn_TYPE_descriptor_t *td,
78 const asn_per_constraints_t *constraints, const void *sptr,
80 const ANY_t *st = (const ANY_t *)sptr;
81 asn_enc_rval_t er = {0, 0, 0};
88 if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
94 ssize_t may_save = uper_put_length(po, size, &need_eom);
95 if(may_save < 0) ASN__ENCODE_FAILED;
97 ret = per_put_many_bits(po, buf, may_save * 8);
98 if(ret) ASN__ENCODE_FAILED;
102 assert(!(may_save & 0x07) || !size);
103 if(need_eom && uper_put_length(po, 0, 0))
104 ASN__ENCODE_FAILED; /* End of Message length */