vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1 | #include <asn_application.h> |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 2 | #include <asn_internal.h> |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 3 | #include <per_decoder.h> |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 4 | |
| 5 | asn_dec_rval_t |
vlm | 619448e | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 6 | uper_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) { |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 7 | asn_codec_ctx_t s_codec_ctx; |
vlm | 76a5277 | 2006-09-17 11:31:55 +0000 | [diff] [blame] | 8 | asn_dec_rval_t rval; |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 9 | asn_per_data_t pd; |
| 10 | |
vlm | 619448e | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 11 | if(skip_bits < 0 || skip_bits > 7 |
| 12 | || unused_bits < 0 || unused_bits > 7 |
| 13 | || (unused_bits > 0 && !size)) |
vlm | e7e9bd9 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 14 | _ASN_DECODE_FAILED; |
| 15 | |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 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 */ |
vlm | 02cd28b | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 33 | memset(&pd, 0, sizeof(pd)); |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 34 | pd.buffer = (const uint8_t *)buffer; |
vlm | e7e9bd9 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 35 | pd.nboff = skip_bits; |
vlm | 619448e | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 36 | pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */ |
| 37 | if(pd.nboff > pd.nbits) |
| 38 | _ASN_DECODE_FAILED; |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * Invoke type-specific decoder. |
| 42 | */ |
| 43 | if(!td->uper_decoder) |
| 44 | _ASN_DECODE_FAILED; /* PER is not compiled in */ |
vlm | 76a5277 | 2006-09-17 11:31:55 +0000 | [diff] [blame] | 45 | rval = td->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd); |
vlm | 619448e | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 46 | if(rval.code == RC_OK) { |
vlm | 76a5277 | 2006-09-17 11:31:55 +0000 | [diff] [blame] | 47 | /* Return the number of consumed bits */ |
| 48 | rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3) |
| 49 | + pd.nboff - skip_bits; |
vlm | 619448e | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 50 | } else { |
| 51 | /* PER codec is not a restartable */ |
| 52 | rval.consumed = 0; |
vlm | 76a5277 | 2006-09-17 11:31:55 +0000 | [diff] [blame] | 53 | } |
| 54 | return rval; |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 55 | } |
| 56 | |