blob: de9dc55033492505c866d8866c946d49f24c207a [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 */
vlm02cd28b2007-06-24 06:26:47 +000033 memset(&pd, 0, sizeof(pd));
vlm4d2ca122005-12-07 05:46:03 +000034 pd.buffer = (const uint8_t *)buffer;
vlme7e9bd92006-09-17 11:02:53 +000035 pd.nboff = skip_bits;
vlm619448e2006-09-18 20:05:55 +000036 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
37 if(pd.nboff > pd.nbits)
38 _ASN_DECODE_FAILED;
vlm4d2ca122005-12-07 05:46:03 +000039
40 /*
41 * Invoke type-specific decoder.
42 */
43 if(!td->uper_decoder)
44 _ASN_DECODE_FAILED; /* PER is not compiled in */
vlm76a52772006-09-17 11:31:55 +000045 rval = td->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
vlm619448e2006-09-18 20:05:55 +000046 if(rval.code == RC_OK) {
vlm76a52772006-09-17 11:31:55 +000047 /* Return the number of consumed bits */
48 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
49 + pd.nboff - skip_bits;
vlm8c2c5c42007-06-26 06:46:04 +000050 ASN_DEBUG("PER decoding consumed %d, counted %d",
51 rval.consumed, pd.moved);
vlm26b48ca2007-06-26 02:51:10 +000052 assert(rval.consumed == pd.moved);
vlm619448e2006-09-18 20:05:55 +000053 } else {
54 /* PER codec is not a restartable */
55 rval.consumed = 0;
vlm76a52772006-09-17 11:31:55 +000056 }
57 return rval;
vlm4d2ca122005-12-07 05:46:03 +000058}
59