blob: 16dee369624bbf28bb8957e222abe77059980d9f [file] [log] [blame]
Harald Welte92c45f32010-06-12 18:59:38 +02001#include <asn_application.h>
2#include <asn_internal.h>
3#include <per_decoder.h>
4
5asn_dec_rval_t
6uper_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) {
7 asn_codec_ctx_t s_codec_ctx;
8 asn_dec_rval_t rval;
9 asn_per_data_t pd;
10
11 if(skip_bits < 0 || skip_bits > 7
12 || unused_bits < 0 || unused_bits > 7
13 || (unused_bits > 0 && !size))
14 _ASN_DECODE_FAILED;
15
16 /*
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;
34 pd.nboff = skip_bits;
35 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
36 if(pd.nboff > pd.nbits)
37 _ASN_DECODE_FAILED;
38
39 /*
40 * Invoke type-specific decoder.
41 */
42 if(!td->uper_decoder)
43 _ASN_DECODE_FAILED; /* PER is not compiled in */
44 rval = td->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
45 if(rval.code == RC_OK) {
46 /* Return the number of consumed bits */
47 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
48 + pd.nboff - skip_bits;
49 } else {
50 /* PER codec is not a restartable */
51 rval.consumed = 0;
52 }
53 return rval;
54}
55