blob: 16dee369624bbf28bb8957e222abe77059980d9f [file] [log] [blame]
vlm337167e2005-11-26 11:25:14 +00001#include <asn_application.h>
vlm4d2ca122005-12-07 05:46:03 +00002#include <asn_internal.h>
vlm337167e2005-11-26 11:25:14 +00003#include <per_decoder.h>
vlm4d2ca122005-12-07 05:46:03 +00004
5asn_dec_rval_t
vlm619448e2006-09-18 20:05:55 +00006uper_decode(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, void **sptr, const void *buffer, size_t size, int skip_bits, int unused_bits) {
vlm4d2ca122005-12-07 05:46:03 +00007 asn_codec_ctx_t s_codec_ctx;
vlm76a52772006-09-17 11:31:55 +00008 asn_dec_rval_t rval;
vlm4d2ca122005-12-07 05:46:03 +00009 asn_per_data_t pd;
10
vlm619448e2006-09-18 20:05:55 +000011 if(skip_bits < 0 || skip_bits > 7
12 || unused_bits < 0 || unused_bits > 7
13 || (unused_bits > 0 && !size))
vlme7e9bd92006-09-17 11:02:53 +000014 _ASN_DECODE_FAILED;
15
vlm4d2ca122005-12-07 05:46:03 +000016 /*
17 * Stack checker requires that the codec context
18 * must be allocated on the stack.
19 */
20 if(opt_codec_ctx) {
21 if(opt_codec_ctx->max_stack_size) {
22 s_codec_ctx = *opt_codec_ctx;
23 opt_codec_ctx = &s_codec_ctx;
24 }
25 } else {
26 /* If context is not given, be security-conscious anyway */
27 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
28 s_codec_ctx.max_stack_size = _ASN_DEFAULT_STACK_MAX;
29 opt_codec_ctx = &s_codec_ctx;
30 }
31
32 /* Fill in the position indicator */
33 pd.buffer = (const uint8_t *)buffer;
vlme7e9bd92006-09-17 11:02:53 +000034 pd.nboff = skip_bits;
vlm619448e2006-09-18 20:05:55 +000035 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
36 if(pd.nboff > pd.nbits)
37 _ASN_DECODE_FAILED;
vlm4d2ca122005-12-07 05:46:03 +000038
39 /*
40 * Invoke type-specific decoder.
41 */
42 if(!td->uper_decoder)
43 _ASN_DECODE_FAILED; /* PER is not compiled in */
vlm76a52772006-09-17 11:31:55 +000044 rval = td->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
vlm619448e2006-09-18 20:05:55 +000045 if(rval.code == RC_OK) {
vlm76a52772006-09-17 11:31:55 +000046 /* Return the number of consumed bits */
47 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
48 + pd.nboff - skip_bits;
vlm619448e2006-09-18 20:05:55 +000049 } else {
50 /* PER codec is not a restartable */
51 rval.consumed = 0;
vlm76a52772006-09-17 11:31:55 +000052 }
53 return rval;
vlm4d2ca122005-12-07 05:46:03 +000054}
55