1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
5 # Licensed under the Apache License, Version 2.0 (the "License"); *
6 # you may not use this file except in compliance with the License. *
7 # You may obtain a copy of the License at *
9 # http://www.apache.org/licenses/LICENSE-2.0 *
11 # Unless required by applicable law or agreed to in writing, software *
12 # distributed under the License is distributed on an "AS IS" BASIS, *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 # See the License for the specific language governing permissions and *
15 # limitations under the License. *
17 ******************************************************************************/
19 #include <asn_application.h>
20 #include <asn_internal.h>
21 #include <per_decoder.h>
24 * Decode a "Production of a complete encoding", X.691#10.1.
25 * The complete encoding contains at least one byte, and is an integral
26 * multiple of 8 bytes.
29 uper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx,
30 const asn_TYPE_descriptor_t *td, void **sptr,
31 const void *buffer, size_t size) {
34 rval = uper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
37 * We've always given 8-aligned data,
38 * so convert bits to integral bytes.
42 } else if(rval.code == RC_OK) {
44 if(((const uint8_t *)buffer)[0] == 0) {
45 rval.consumed = 1; /* 1 byte */
47 ASN_DEBUG("Expecting single zeroed byte");
51 /* Must contain at least 8 bits. */
60 uper_decode(const asn_codec_ctx_t *opt_codec_ctx,
61 const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer,
62 size_t size, int skip_bits, int unused_bits) {
63 asn_codec_ctx_t s_codec_ctx;
67 if(skip_bits < 0 || skip_bits > 7
68 || unused_bits < 0 || unused_bits > 7
69 || (unused_bits > 0 && !size))
73 * Stack checker requires that the codec context
74 * must be allocated on the stack.
77 if(opt_codec_ctx->max_stack_size) {
78 s_codec_ctx = *opt_codec_ctx;
79 opt_codec_ctx = &s_codec_ctx;
82 /* If context is not given, be security-conscious anyway */
83 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
84 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
85 opt_codec_ctx = &s_codec_ctx;
88 /* Fill in the position indicator */
89 memset(&pd, 0, sizeof(pd));
90 pd.buffer = (const uint8_t *)buffer;
92 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
93 if(pd.nboff > pd.nbits)
97 * Invoke type-specific decoder.
99 if(!td->op->uper_decoder)
100 ASN__DECODE_FAILED; /* PER is not compiled in */
101 rval = td->op->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
102 if(rval.code == RC_OK) {
103 /* Return the number of consumed bits */
104 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
105 + pd.nboff - skip_bits;
106 ASN_DEBUG("PER decoding consumed %ld, counted %ld",
107 (long)rval.consumed, (long)pd.moved);
108 assert(rval.consumed == pd.moved);
110 /* PER codec is not a restartable */
117 aper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx,
118 const asn_TYPE_descriptor_t *td, void **sptr,
119 const void *buffer, size_t size) {
122 rval = aper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
125 * We've always given 8-aligned data,
126 * so convert bits to integral bytes.
130 } else if(rval.code == RC_OK) {
132 if(((const uint8_t *)buffer)[0] == 0) {
133 rval.consumed = 1; /* 1 byte */
135 ASN_DEBUG("Expecting single zeroed byte");
139 /* Must contain at least 8 bits. */
140 rval.code = RC_WMORE;
148 aper_decode(const asn_codec_ctx_t *opt_codec_ctx,
149 const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer,
150 size_t size, int skip_bits, int unused_bits) {
151 asn_codec_ctx_t s_codec_ctx;
155 if(skip_bits < 0 || skip_bits > 7
156 || unused_bits < 0 || unused_bits > 7
157 || (unused_bits > 0 && !size))
161 * Stack checker requires that the codec context
162 * must be allocated on the stack.
165 if(opt_codec_ctx->max_stack_size) {
166 s_codec_ctx = *opt_codec_ctx;
167 opt_codec_ctx = &s_codec_ctx;
170 /* If context is not given, be security-conscious anyway */
171 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
172 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
173 opt_codec_ctx = &s_codec_ctx;
176 /* Fill in the position indicator */
177 memset(&pd, 0, sizeof(pd));
178 pd.buffer = (const uint8_t *)buffer;
179 pd.nboff = skip_bits;
180 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
181 if(pd.nboff > pd.nbits)
185 * Invoke type-specific decoder.
187 if(!td->op->aper_decoder)
188 ASN__DECODE_FAILED; /* PER is not compiled in */
189 rval = td->op->aper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
190 if(rval.code == RC_OK) {
191 /* Return the number of consumed bits */
192 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
193 + pd.nboff - skip_bits;
194 ASN_DEBUG("PER decoding consumed %zu, counted %zu",
195 rval.consumed, pd.moved);
196 assert(rval.consumed == pd.moved);
198 /* PER codec is not a restartable */