blob: a9051fb7981beea276ec9c9a873cb5bbf1115855 [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
Lev Walkin20696a42017-10-17 21:27:33 -070011uper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx,
12 const asn_TYPE_descriptor_t *td, void **sptr,
13 const void *buffer, size_t size) {
14 asn_dec_rval_t rval;
Lev Walkin08b30bb2007-06-26 08:24:50 +000015
16 rval = uper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
17 if(rval.consumed) {
18 /*
19 * We've always given 8-aligned data,
20 * so convert bits to integral bytes.
21 */
22 rval.consumed += 7;
23 rval.consumed >>= 3;
24 } else if(rval.code == RC_OK) {
25 if(size) {
Lev Walkinfe1ffaf2010-10-25 21:07:59 -070026 if(((const uint8_t *)buffer)[0] == 0) {
Lev Walkin08b30bb2007-06-26 08:24:50 +000027 rval.consumed = 1; /* 1 byte */
28 } else {
29 ASN_DEBUG("Expecting single zeroed byte");
30 rval.code = RC_FAIL;
31 }
32 } else {
33 /* Must contain at least 8 bits. */
34 rval.code = RC_WMORE;
35 }
36 }
37
38 return rval;
39}
40
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000041asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -070042uper_decode(const asn_codec_ctx_t *opt_codec_ctx,
43 const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer,
44 size_t size, int skip_bits, int unused_bits) {
45 asn_codec_ctx_t s_codec_ctx;
Lev Walkin21f92772006-09-17 11:31:55 +000046 asn_dec_rval_t rval;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000047 asn_per_data_t pd;
48
Lev Walkin0a8aa602006-09-18 20:05:55 +000049 if(skip_bits < 0 || skip_bits > 7
50 || unused_bits < 0 || unused_bits > 7
51 || (unused_bits > 0 && !size))
Lev Walkin7c1dc052016-03-14 03:08:15 -070052 ASN__DECODE_FAILED;
Lev Walkinbc691772006-09-17 11:02:53 +000053
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000054 /*
55 * Stack checker requires that the codec context
56 * must be allocated on the stack.
57 */
58 if(opt_codec_ctx) {
59 if(opt_codec_ctx->max_stack_size) {
60 s_codec_ctx = *opt_codec_ctx;
61 opt_codec_ctx = &s_codec_ctx;
62 }
63 } else {
64 /* If context is not given, be security-conscious anyway */
65 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
Lev Walkin7c1dc052016-03-14 03:08:15 -070066 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000067 opt_codec_ctx = &s_codec_ctx;
68 }
69
70 /* Fill in the position indicator */
Lev Walkin5b78e1c2007-06-24 06:26:47 +000071 memset(&pd, 0, sizeof(pd));
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000072 pd.buffer = (const uint8_t *)buffer;
Lev Walkinbc691772006-09-17 11:02:53 +000073 pd.nboff = skip_bits;
Lev Walkin0a8aa602006-09-18 20:05:55 +000074 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
75 if(pd.nboff > pd.nbits)
Lev Walkin7c1dc052016-03-14 03:08:15 -070076 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000077
78 /*
79 * Invoke type-specific decoder.
80 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080081 if(!td->op->uper_decoder)
Lev Walkin7c1dc052016-03-14 03:08:15 -070082 ASN__DECODE_FAILED; /* PER is not compiled in */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080083 rval = td->op->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
Lev Walkin0a8aa602006-09-18 20:05:55 +000084 if(rval.code == RC_OK) {
Lev Walkin21f92772006-09-17 11:31:55 +000085 /* Return the number of consumed bits */
86 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
87 + pd.nboff - skip_bits;
Lev Walkinfe1ffaf2010-10-25 21:07:59 -070088 ASN_DEBUG("PER decoding consumed %ld, counted %ld",
89 (long)rval.consumed, (long)pd.moved);
Lev Walkind00657f2007-06-26 02:51:10 +000090 assert(rval.consumed == pd.moved);
Lev Walkin0a8aa602006-09-18 20:05:55 +000091 } else {
92 /* PER codec is not a restartable */
93 rval.consumed = 0;
Lev Walkin21f92772006-09-17 11:31:55 +000094 }
95 return rval;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000096}
97