2 * Copyright 2019 AT&T Intellectual Property
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
24 #include <asn_application.h>
25 #include <asn_internal.h>
26 #include <per_decoder.h>
29 * Decode a "Production of a complete encoding", X.691#10.1.
30 * The complete encoding contains at least one byte, and is an integral
31 * multiple of 8 bytes.
34 uper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx,
35 const asn_TYPE_descriptor_t *td, void **sptr,
36 const void *buffer, size_t size) {
39 rval = uper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
42 * We've always given 8-aligned data,
43 * so convert bits to integral bytes.
47 } else if(rval.code == RC_OK) {
49 if(((const uint8_t *)buffer)[0] == 0) {
50 rval.consumed = 1; /* 1 byte */
52 ASN_DEBUG("Expecting single zeroed byte");
56 /* Must contain at least 8 bits. */
65 uper_decode(const asn_codec_ctx_t *opt_codec_ctx,
66 const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer,
67 size_t size, int skip_bits, int unused_bits) {
68 asn_codec_ctx_t s_codec_ctx;
72 if(skip_bits < 0 || skip_bits > 7
73 || unused_bits < 0 || unused_bits > 7
74 || (unused_bits > 0 && !size))
78 * Stack checker requires that the codec context
79 * must be allocated on the stack.
82 if(opt_codec_ctx->max_stack_size) {
83 s_codec_ctx = *opt_codec_ctx;
84 opt_codec_ctx = &s_codec_ctx;
87 /* If context is not given, be security-conscious anyway */
88 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
89 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
90 opt_codec_ctx = &s_codec_ctx;
93 /* Fill in the position indicator */
94 memset(&pd, 0, sizeof(pd));
95 pd.buffer = (const uint8_t *)buffer;
97 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
98 if(pd.nboff > pd.nbits)
102 * Invoke type-specific decoder.
104 if(!td->op->uper_decoder)
105 ASN__DECODE_FAILED; /* PER is not compiled in */
106 rval = td->op->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
107 if(rval.code == RC_OK) {
108 /* Return the number of consumed bits */
109 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
110 + pd.nboff - skip_bits;
111 ASN_DEBUG("PER decoding consumed %ld, counted %ld",
112 (long)rval.consumed, (long)pd.moved);
113 assert(rval.consumed == pd.moved);
115 /* PER codec is not a restartable */
122 aper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx,
123 const asn_TYPE_descriptor_t *td, void **sptr,
124 const void *buffer, size_t size) {
127 rval = aper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
130 * We've always given 8-aligned data,
131 * so convert bits to integral bytes.
135 } else if(rval.code == RC_OK) {
137 if(((const uint8_t *)buffer)[0] == 0) {
138 rval.consumed = 1; /* 1 byte */
140 ASN_DEBUG("Expecting single zeroed byte");
144 /* Must contain at least 8 bits. */
145 rval.code = RC_WMORE;
153 aper_decode(const asn_codec_ctx_t *opt_codec_ctx,
154 const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer,
155 size_t size, int skip_bits, int unused_bits) {
156 asn_codec_ctx_t s_codec_ctx;
160 if(skip_bits < 0 || skip_bits > 7
161 || unused_bits < 0 || unused_bits > 7
162 || (unused_bits > 0 && !size))
166 * Stack checker requires that the codec context
167 * must be allocated on the stack.
170 if(opt_codec_ctx->max_stack_size) {
171 s_codec_ctx = *opt_codec_ctx;
172 opt_codec_ctx = &s_codec_ctx;
175 /* If context is not given, be security-conscious anyway */
176 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
177 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
178 opt_codec_ctx = &s_codec_ctx;
181 /* Fill in the position indicator */
182 memset(&pd, 0, sizeof(pd));
183 pd.buffer = (const uint8_t *)buffer;
184 pd.nboff = skip_bits;
185 pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
186 if(pd.nboff > pd.nbits)
190 * Invoke type-specific decoder.
192 if(!td->op->aper_decoder)
193 ASN__DECODE_FAILED; /* PER is not compiled in */
194 rval = td->op->aper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
195 if(rval.code == RC_OK) {
196 /* Return the number of consumed bits */
197 rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
198 + pd.nboff - skip_bits;
199 ASN_DEBUG("PER decoding consumed %zu, counted %zu",
200 rval.consumed, pd.moved);
201 assert(rval.consumed == pd.moved);
203 /* PER codec is not a restartable */