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.
20 #include <asn_application.h>
21 #include <asn_internal.h>
22 #include <per_decoder.h>
25 * Decode a "Production of a complete encoding", X.691#10.1.
26 * The complete encoding contains at least one byte, and is an integral
27 * multiple of 8 bytes.
30 uper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx,
31 const asn_TYPE_descriptor_t *td, void **sptr,
32 const void *buffer, size_t size) {
35 rval = uper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
38 * We've always given 8-aligned data,
39 * so convert bits to integral bytes.
43 } else if(rval.code == RC_OK) {
45 if(((const uint8_t *)buffer)[0] == 0) {
46 rval.consumed = 1; /* 1 byte */
48 ASN_DEBUG("Expecting single zeroed byte");
52 /* Must contain at least 8 bits. */
61 uper_decode(const asn_codec_ctx_t *opt_codec_ctx,
62 const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer,
63 size_t size, int skip_bits, int unused_bits) {
64 asn_codec_ctx_t s_codec_ctx;
68 if(skip_bits < 0 || skip_bits > 7
69 || unused_bits < 0 || unused_bits > 7
70 || (unused_bits > 0 && !size))
74 * Stack checker requires that the codec context
75 * must be allocated on the stack.
78 if(opt_codec_ctx->max_stack_size) {
79 s_codec_ctx = *opt_codec_ctx;
80 opt_codec_ctx = &s_codec_ctx;
83 /* If context is not given, be security-conscious anyway */
84 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
85 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
86 opt_codec_ctx = &s_codec_ctx;
89 /* Fill in the position indicator */
90 memset(&pd, 0, sizeof(pd));
91 pd.buffer = (const uint8_t *)buffer;
93 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
94 if(pd.nboff > pd.nbits)
98 * Invoke type-specific decoder.
100 if(!td->op->uper_decoder)
101 ASN__DECODE_FAILED; /* PER is not compiled in */
102 rval = td->op->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
103 if(rval.code == RC_OK) {
104 /* Return the number of consumed bits */
105 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
106 + pd.nboff - skip_bits;
107 ASN_DEBUG("PER decoding consumed %ld, counted %ld",
108 (long)rval.consumed, (long)pd.moved);
109 assert(rval.consumed == pd.moved);
111 /* PER codec is not a restartable */
118 aper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx,
119 const asn_TYPE_descriptor_t *td, void **sptr,
120 const void *buffer, size_t size) {
123 rval = aper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
126 * We've always given 8-aligned data,
127 * so convert bits to integral bytes.
131 } else if(rval.code == RC_OK) {
133 if(((const uint8_t *)buffer)[0] == 0) {
134 rval.consumed = 1; /* 1 byte */
136 ASN_DEBUG("Expecting single zeroed byte");
140 /* Must contain at least 8 bits. */
141 rval.code = RC_WMORE;
149 aper_decode(const asn_codec_ctx_t *opt_codec_ctx,
150 const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer,
151 size_t size, int skip_bits, int unused_bits) {
152 asn_codec_ctx_t s_codec_ctx;
156 if(skip_bits < 0 || skip_bits > 7
157 || unused_bits < 0 || unused_bits > 7
158 || (unused_bits > 0 && !size))
162 * Stack checker requires that the codec context
163 * must be allocated on the stack.
166 if(opt_codec_ctx->max_stack_size) {
167 s_codec_ctx = *opt_codec_ctx;
168 opt_codec_ctx = &s_codec_ctx;
171 /* If context is not given, be security-conscious anyway */
172 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
173 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
174 opt_codec_ctx = &s_codec_ctx;
177 /* Fill in the position indicator */
178 memset(&pd, 0, sizeof(pd));
179 pd.buffer = (const uint8_t *)buffer;
180 pd.nboff = skip_bits;
181 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
182 if(pd.nboff > pd.nbits)
186 * Invoke type-specific decoder.
188 if(!td->op->aper_decoder)
189 ASN__DECODE_FAILED; /* PER is not compiled in */
190 rval = td->op->aper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
191 if(rval.code == RC_OK) {
192 /* Return the number of consumed bits */
193 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
194 + pd.nboff - skip_bits;
195 ASN_DEBUG("PER decoding consumed %zu, counted %zu",
196 rval.consumed, pd.moved);
197 assert(rval.consumed == pd.moved);
199 /* PER codec is not a restartable */