blob: 8a3e39df8dc64ef960237e0938e577159b5ae661 [file] [log] [blame]
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001#include <asn_application.h>
2#include <asn_internal.h>
3#include <per_decoder.h>
4
5/*
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(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;
15
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) {
26 if(((const uint8_t *)buffer)[0] == 0) {
27 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
41asn_dec_rval_t
42uper_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;
46 asn_dec_rval_t rval;
47 asn_per_data_t pd;
48
49 if(skip_bits < 0 || skip_bits > 7
50 || unused_bits < 0 || unused_bits > 7
51 || (unused_bits > 0 && !size))
52 ASN__DECODE_FAILED;
53
54 /*
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));
66 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
67 opt_codec_ctx = &s_codec_ctx;
68 }
69
70 /* Fill in the position indicator */
71 memset(&pd, 0, sizeof(pd));
72 pd.buffer = (const uint8_t *)buffer;
73 pd.nboff = skip_bits;
74 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
75 if(pd.nboff > pd.nbits)
76 ASN__DECODE_FAILED;
77
78 /*
79 * Invoke type-specific decoder.
80 */
81 if(!td->op->uper_decoder)
82 ASN__DECODE_FAILED; /* PER is not compiled in */
83 rval = td->op->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
84 if(rval.code == RC_OK) {
85 /* Return the number of consumed bits */
86 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
87 + pd.nboff - skip_bits;
88 ASN_DEBUG("PER decoding consumed %ld, counted %ld",
89 (long)rval.consumed, (long)pd.moved);
90 assert(rval.consumed == pd.moved);
91 } else {
92 /* PER codec is not a restartable */
93 rval.consumed = 0;
94 }
95 return rval;
96}
97
98asn_dec_rval_t
99aper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx,
100 const asn_TYPE_descriptor_t *td, void **sptr,
101 const void *buffer, size_t size) {
102 asn_dec_rval_t rval;
103
104 rval = aper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
105 if(rval.consumed) {
106 /*
107 * We've always given 8-aligned data,
108 * so convert bits to integral bytes.
109 */
110 rval.consumed += 7;
111 rval.consumed >>= 3;
112 } else if(rval.code == RC_OK) {
113 if(size) {
114 if(((const uint8_t *)buffer)[0] == 0) {
115 rval.consumed = 1; /* 1 byte */
116 } else {
117 ASN_DEBUG("Expecting single zeroed byte");
118 rval.code = RC_FAIL;
119 }
120 } else {
121 /* Must contain at least 8 bits. */
122 rval.code = RC_WMORE;
123 }
124 }
125
126 return rval;
127}
128
129asn_dec_rval_t
130aper_decode(const asn_codec_ctx_t *opt_codec_ctx,
131 const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer,
132 size_t size, int skip_bits, int unused_bits) {
133 asn_codec_ctx_t s_codec_ctx;
134 asn_dec_rval_t rval;
135 asn_per_data_t pd;
136
137 if(skip_bits < 0 || skip_bits > 7
138 || unused_bits < 0 || unused_bits > 7
139 || (unused_bits > 0 && !size))
140 ASN__DECODE_FAILED;
141
142 /*
143 * Stack checker requires that the codec context
144 * must be allocated on the stack.
145 */
146 if(opt_codec_ctx) {
147 if(opt_codec_ctx->max_stack_size) {
148 s_codec_ctx = *opt_codec_ctx;
149 opt_codec_ctx = &s_codec_ctx;
150 }
151 } else {
152 /* If context is not given, be security-conscious anyway */
153 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
154 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
155 opt_codec_ctx = &s_codec_ctx;
156 }
157
158 /* Fill in the position indicator */
159 memset(&pd, 0, sizeof(pd));
160 pd.buffer = (const uint8_t *)buffer;
161 pd.nboff = skip_bits;
162 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
163 if(pd.nboff > pd.nbits)
164 ASN__DECODE_FAILED;
165
166 /*
167 * Invoke type-specific decoder.
168 */
169 if(!td->op->aper_decoder)
170 ASN__DECODE_FAILED; /* PER is not compiled in */
171 rval = td->op->aper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
172 if(rval.code == RC_OK) {
173 /* Return the number of consumed bits */
174 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
175 + pd.nboff - skip_bits;
176 ASN_DEBUG("PER decoding consumed %zu, counted %zu",
177 rval.consumed, pd.moved);
178 assert(rval.consumed == pd.moved);
179 } else {
180 /* PER codec is not a restartable */
181 rval.consumed = 0;
182 }
183 return rval;
184}
185