Moving in e2sim originally from it/test/simulators
[sim/e2-interface.git] / e2sim / ASN1c / 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 #include <asn_application.h>
20 #include <asn_internal.h>
21 #include <per_decoder.h>
22
23 /*
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.
27  */
28 asn_dec_rval_t
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) {
32     asn_dec_rval_t rval;
33
34         rval = uper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
35         if(rval.consumed) {
36                 /*
37                  * We've always given 8-aligned data,
38                  * so convert bits to integral bytes.
39                  */
40                 rval.consumed += 7;
41                 rval.consumed >>= 3;
42         } else if(rval.code == RC_OK) {
43                 if(size) {
44                         if(((const uint8_t *)buffer)[0] == 0) {
45                                 rval.consumed = 1;      /* 1 byte */
46                         } else {
47                                 ASN_DEBUG("Expecting single zeroed byte");
48                                 rval.code = RC_FAIL;
49                         }
50                 } else {
51                         /* Must contain at least 8 bits. */
52                         rval.code = RC_WMORE;
53                 }
54         }
55
56         return rval;
57 }
58
59 asn_dec_rval_t
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;
64         asn_dec_rval_t rval;
65         asn_per_data_t pd;
66
67         if(skip_bits < 0 || skip_bits > 7
68         || unused_bits < 0 || unused_bits > 7
69         || (unused_bits > 0 && !size))
70                 ASN__DECODE_FAILED;
71
72         /*
73          * Stack checker requires that the codec context
74          * must be allocated on the stack.
75          */
76         if(opt_codec_ctx) {
77                 if(opt_codec_ctx->max_stack_size) {
78                         s_codec_ctx = *opt_codec_ctx;
79                         opt_codec_ctx = &s_codec_ctx;
80                 }
81         } else {
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;
86         }
87
88         /* Fill in the position indicator */
89         memset(&pd, 0, sizeof(pd));
90         pd.buffer = (const uint8_t *)buffer;
91         pd.nboff = skip_bits;
92         pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
93         if(pd.nboff > pd.nbits)
94                 ASN__DECODE_FAILED;
95
96         /*
97          * Invoke type-specific decoder.
98          */
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);
109         } else {
110                 /* PER codec is not a restartable */
111                 rval.consumed = 0;
112         }
113         return rval;
114 }
115
116 asn_dec_rval_t
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) {
120         asn_dec_rval_t rval;
121
122         rval = aper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
123         if(rval.consumed) {
124                 /*
125                  * We've always given 8-aligned data,
126                  * so convert bits to integral bytes.
127                  */
128                 rval.consumed += 7;
129                 rval.consumed >>= 3;
130         } else if(rval.code == RC_OK) {
131                 if(size) {
132                         if(((const uint8_t *)buffer)[0] == 0) {
133                                 rval.consumed = 1;      /* 1 byte */
134                         } else {
135                                 ASN_DEBUG("Expecting single zeroed byte");
136                                 rval.code = RC_FAIL;
137                         }
138                 } else {
139                         /* Must contain at least 8 bits. */
140                         rval.code = RC_WMORE;
141                 }
142         }
143
144         return rval;
145 }
146
147 asn_dec_rval_t
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;
152         asn_dec_rval_t rval;
153         asn_per_data_t pd;
154
155         if(skip_bits < 0 || skip_bits > 7
156                 || unused_bits < 0 || unused_bits > 7
157                 || (unused_bits > 0 && !size))
158                 ASN__DECODE_FAILED;
159
160         /*
161          * Stack checker requires that the codec context
162          * must be allocated on the stack.
163          */
164         if(opt_codec_ctx) {
165                 if(opt_codec_ctx->max_stack_size) {
166                         s_codec_ctx = *opt_codec_ctx;
167                         opt_codec_ctx = &s_codec_ctx;
168                 }
169         } else {
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;
174         }
175
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)
182                 ASN__DECODE_FAILED;
183
184         /*
185          * Invoke type-specific decoder.
186          */
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);
197         } else {
198                 /* PER codec is not a restartable */
199                 rval.consumed = 0;
200         }
201         return rval;
202 }
203