Sync from Azure to LF
[ric-plt/resource-status-manager.git] / RSM / asn1codec / e2ap_engine / per_decoder.c
1 /*
2  *
3  * Copyright 2019 AT&T Intellectual Property
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18
19
20 #include <asn_application.h>
21 #include <asn_internal.h>
22 #include <per_decoder.h>
23
24 /*
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.
28  */
29 asn_dec_rval_t
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) {
33     asn_dec_rval_t rval;
34
35         rval = uper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
36         if(rval.consumed) {
37                 /*
38                  * We've always given 8-aligned data,
39                  * so convert bits to integral bytes.
40                  */
41                 rval.consumed += 7;
42                 rval.consumed >>= 3;
43         } else if(rval.code == RC_OK) {
44                 if(size) {
45                         if(((const uint8_t *)buffer)[0] == 0) {
46                                 rval.consumed = 1;      /* 1 byte */
47                         } else {
48                                 ASN_DEBUG("Expecting single zeroed byte");
49                                 rval.code = RC_FAIL;
50                         }
51                 } else {
52                         /* Must contain at least 8 bits. */
53                         rval.code = RC_WMORE;
54                 }
55         }
56
57         return rval;
58 }
59
60 asn_dec_rval_t
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;
65         asn_dec_rval_t rval;
66         asn_per_data_t pd;
67
68         if(skip_bits < 0 || skip_bits > 7
69         || unused_bits < 0 || unused_bits > 7
70         || (unused_bits > 0 && !size))
71                 ASN__DECODE_FAILED;
72
73         /*
74          * Stack checker requires that the codec context
75          * must be allocated on the stack.
76          */
77         if(opt_codec_ctx) {
78                 if(opt_codec_ctx->max_stack_size) {
79                         s_codec_ctx = *opt_codec_ctx;
80                         opt_codec_ctx = &s_codec_ctx;
81                 }
82         } else {
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;
87         }
88
89         /* Fill in the position indicator */
90         memset(&pd, 0, sizeof(pd));
91         pd.buffer = (const uint8_t *)buffer;
92         pd.nboff = skip_bits;
93         pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
94         if(pd.nboff > pd.nbits)
95                 ASN__DECODE_FAILED;
96
97         /*
98          * Invoke type-specific decoder.
99          */
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);
110         } else {
111                 /* PER codec is not a restartable */
112                 rval.consumed = 0;
113         }
114         return rval;
115 }
116
117 asn_dec_rval_t
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) {
121         asn_dec_rval_t rval;
122
123         rval = aper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
124         if(rval.consumed) {
125                 /*
126                  * We've always given 8-aligned data,
127                  * so convert bits to integral bytes.
128                  */
129                 rval.consumed += 7;
130                 rval.consumed >>= 3;
131         } else if(rval.code == RC_OK) {
132                 if(size) {
133                         if(((const uint8_t *)buffer)[0] == 0) {
134                                 rval.consumed = 1;      /* 1 byte */
135                         } else {
136                                 ASN_DEBUG("Expecting single zeroed byte");
137                                 rval.code = RC_FAIL;
138                         }
139                 } else {
140                         /* Must contain at least 8 bits. */
141                         rval.code = RC_WMORE;
142                 }
143         }
144
145         return rval;
146 }
147
148 asn_dec_rval_t
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;
153         asn_dec_rval_t rval;
154         asn_per_data_t pd;
155
156         if(skip_bits < 0 || skip_bits > 7
157                 || unused_bits < 0 || unused_bits > 7
158                 || (unused_bits > 0 && !size))
159                 ASN__DECODE_FAILED;
160
161         /*
162          * Stack checker requires that the codec context
163          * must be allocated on the stack.
164          */
165         if(opt_codec_ctx) {
166                 if(opt_codec_ctx->max_stack_size) {
167                         s_codec_ctx = *opt_codec_ctx;
168                         opt_codec_ctx = &s_codec_ctx;
169                 }
170         } else {
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;
175         }
176
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)
183                 ASN__DECODE_FAILED;
184
185         /*
186          * Invoke type-specific decoder.
187          */
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);
198         } else {
199                 /* PER codec is not a restartable */
200                 rval.consumed = 0;
201         }
202         return rval;
203 }
204