blob: 461b7262f1d9874cf33ce2c252c1abd8d1e2636e [file] [log] [blame]
Lev Walkin59b176e2005-11-26 11:25:14 +00001#include <asn_application.h>
Lev Walkin1d9e8dd2005-12-07 05:46:03 +00002#include <asn_internal.h>
Lev Walkin59b176e2005-11-26 11:25:14 +00003#include <per_decoder.h>
Lev Walkin1d9e8dd2005-12-07 05:46:03 +00004
Lev Walkin08b30bb2007-06-26 08:24:50 +00005/*
6 * Decode a "Production of a complete encoding", X.691#10.1.
7 * The complete encoding contains at least one byte, and is an integral
8 * multiple of 8 bytes.
9 */
10asn_dec_rval_t
11uper_decode_complete(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, void **sptr, const void *buffer, size_t size) {
12 asn_dec_rval_t rval;
13
14 rval = uper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
15 if(rval.consumed) {
16 /*
17 * We've always given 8-aligned data,
18 * so convert bits to integral bytes.
19 */
20 rval.consumed += 7;
21 rval.consumed >>= 3;
22 } else if(rval.code == RC_OK) {
23 if(size) {
Lev Walkinfe1ffaf2010-10-25 21:07:59 -070024 if(((const uint8_t *)buffer)[0] == 0) {
Lev Walkin08b30bb2007-06-26 08:24:50 +000025 rval.consumed = 1; /* 1 byte */
26 } else {
27 ASN_DEBUG("Expecting single zeroed byte");
28 rval.code = RC_FAIL;
29 }
30 } else {
31 /* Must contain at least 8 bits. */
32 rval.code = RC_WMORE;
33 }
34 }
35
36 return rval;
37}
38
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000039asn_dec_rval_t
Lev Walkin0a8aa602006-09-18 20:05:55 +000040uper_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) {
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000041 asn_codec_ctx_t s_codec_ctx;
Lev Walkin21f92772006-09-17 11:31:55 +000042 asn_dec_rval_t rval;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000043 asn_per_data_t pd;
44
Lev Walkin0a8aa602006-09-18 20:05:55 +000045 if(skip_bits < 0 || skip_bits > 7
46 || unused_bits < 0 || unused_bits > 7
47 || (unused_bits > 0 && !size))
Lev Walkin7c1dc052016-03-14 03:08:15 -070048 ASN__DECODE_FAILED;
Lev Walkinbc691772006-09-17 11:02:53 +000049
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000050 /*
51 * Stack checker requires that the codec context
52 * must be allocated on the stack.
53 */
54 if(opt_codec_ctx) {
55 if(opt_codec_ctx->max_stack_size) {
56 s_codec_ctx = *opt_codec_ctx;
57 opt_codec_ctx = &s_codec_ctx;
58 }
59 } else {
60 /* If context is not given, be security-conscious anyway */
61 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
Lev Walkin7c1dc052016-03-14 03:08:15 -070062 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000063 opt_codec_ctx = &s_codec_ctx;
64 }
65
66 /* Fill in the position indicator */
Lev Walkin5b78e1c2007-06-24 06:26:47 +000067 memset(&pd, 0, sizeof(pd));
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000068 pd.buffer = (const uint8_t *)buffer;
Lev Walkinbc691772006-09-17 11:02:53 +000069 pd.nboff = skip_bits;
Lev Walkin0a8aa602006-09-18 20:05:55 +000070 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
71 if(pd.nboff > pd.nbits)
Lev Walkin7c1dc052016-03-14 03:08:15 -070072 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000073
74 /*
75 * Invoke type-specific decoder.
76 */
77 if(!td->uper_decoder)
Lev Walkin7c1dc052016-03-14 03:08:15 -070078 ASN__DECODE_FAILED; /* PER is not compiled in */
Lev Walkin21f92772006-09-17 11:31:55 +000079 rval = td->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
Lev Walkin0a8aa602006-09-18 20:05:55 +000080 if(rval.code == RC_OK) {
Lev Walkin21f92772006-09-17 11:31:55 +000081 /* Return the number of consumed bits */
82 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
83 + pd.nboff - skip_bits;
Lev Walkinfe1ffaf2010-10-25 21:07:59 -070084 ASN_DEBUG("PER decoding consumed %ld, counted %ld",
85 (long)rval.consumed, (long)pd.moved);
Lev Walkind00657f2007-06-26 02:51:10 +000086 assert(rval.consumed == pd.moved);
Lev Walkin0a8aa602006-09-18 20:05:55 +000087 } else {
88 /* PER codec is not a restartable */
89 rval.consumed = 0;
Lev Walkin21f92772006-09-17 11:31:55 +000090 }
91 return rval;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000092}
93