1 #include <asn_application.h>
2 #include <asn_internal.h>
3 #include <aper_decoder.h>
6 aper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx,
7 const asn_TYPE_descriptor_t *td, void **sptr,
8 const void *buffer, size_t size) {
11 rval = aper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
14 * We've always given 8-aligned data,
15 * so convert bits to integral bytes.
19 } else if(rval.code == RC_OK) {
21 if(((const uint8_t *)buffer)[0] == 0) {
22 rval.consumed = 1; /* 1 byte */
24 ASN_DEBUG("Expecting single zeroed byte");
28 /* Must contain at least 8 bits. */
37 aper_decode(const asn_codec_ctx_t *opt_codec_ctx,
38 const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer,
39 size_t size, int skip_bits, int unused_bits) {
40 asn_codec_ctx_t s_codec_ctx;
44 if(skip_bits < 0 || skip_bits > 7
45 || unused_bits < 0 || unused_bits > 7
46 || (unused_bits > 0 && !size))
50 * Stack checker requires that the codec context
51 * must be allocated on the stack.
54 if(opt_codec_ctx->max_stack_size) {
55 s_codec_ctx = *opt_codec_ctx;
56 opt_codec_ctx = &s_codec_ctx;
59 /* If context is not given, be security-conscious anyway */
60 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
61 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
62 opt_codec_ctx = &s_codec_ctx;
65 /* Fill in the position indicator */
66 memset(&pd, 0, sizeof(pd));
67 pd.buffer = (const uint8_t *)buffer;
69 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
70 if(pd.nboff > pd.nbits)
74 * Invoke type-specific decoder.
76 if(!td->op->aper_decoder)
77 ASN__DECODE_FAILED; /* PER is not compiled in */
78 rval = td->op->aper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
79 if(rval.code == RC_OK) {
80 /* Return the number of consumed bits */
81 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
82 + pd.nboff - skip_bits;
83 ASN_DEBUG("PER decoding consumed %zu, counted %zu",
84 rval.consumed, pd.moved);
85 assert(rval.consumed == pd.moved);
87 /* PER codec is not a restartable */