Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / src / ASN1 / asn / per / codec.hpp
1 #pragma once
2 /******************************************************************************
3 *
4 *   Copyright (c) 2019 AT&T Intellectual Property.
5 *   Copyright (c) 2018-2019 Nokia.
6 *
7 *   Licensed under the Apache License, Version 2.0 (the "License");
8 *   you may not use this file except in compliance with the License.
9 *   You may obtain a copy of the License at
10 *
11 *       http://www.apache.org/licenses/LICENSE-2.0
12 *
13 *   Unless required by applicable law or agreed to in writing, software
14 *   distributed under the License is distributed on an "AS IS" BASIS,
15 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 *   See the License for the specific language governing permissions and
17 *   limitations under the License.
18 *
19 ******************************************************************************/
20
21 // Standard Includes: ANSI C/C++, MSA, and Third-Party Libraries
22
23 // Local Includes: Application specific classes, functions, and libraries
24 #include "asn/per/common.hpp"
25 #include "asn/elements.hpp"
26 #include "asn/per/context.hpp"
27 #include "asn/per/length.hpp"
28 #include "asn/per/integer.hpp"
29 #include "asn/per/enumerated.hpp"
30 #include "asn/per/ostring.hpp"
31 #include "asn/per/bstring.hpp"
32 #include "asn/per/sequence_of.hpp"
33 #include "asn/per/sequence.hpp"
34 #include "asn/per/choice.hpp"
35 #include "asn/per/visitor.hpp"
36 #include "asn/per/oid.hpp"
37
38 namespace asn {
39 namespace per {
40
41 /********************************************************************************
42 pack
43 *********************************************************************************/
44 template <class IE>
45 bool pack(IE const& ie, EncoderCtx& ctx)
46 {
47         ctx.refErrorCtx().reset();
48         Element<IE, IE::ie_type>::run(ie, ctx);
49
50         if (ctx)
51                 Tools::bit_accessor::padByte(ctx.refBuffer());
52
53         return static_cast<bool>(ctx);
54 }
55 /********************************************************************************
56 unpack
57 *********************************************************************************/
58 template <class IE>
59 bool unpack(IE& ie, DecoderCtx& ctx)
60 {
61         Element<IE, IE::ie_type>::run(ie, ctx);
62         
63         if (ctx)
64                 Tools::bit_accessor::padByte(ctx.refBuffer());
65         
66         if(ctx && ctx.refBuffer().getBytesLeft())
67         {
68                 ctx.ie_name(IE::name());
69                 ctx.refErrorCtx().lengthErrorBytes(ctx.refBuffer().getBytesLeft(), 0);
70         }
71         
72         return static_cast<bool>(ctx);
73 }
74
75 /***************************************************************************************
76 * ElementType
77 ***************************************************************************************/
78 template <class IE, element_type IE_TYPE>
79 struct ElementType;
80
81 /***************************************************************************************
82 * BOOLEAN: Encoding the boolean type (X.691 11)
83 ***************************************************************************************/
84 template <class IE>
85 struct ElementType<IE, element_type::T_BOOLEAN>
86 {
87 protected:
88         template <class, element_type> friend struct Element;
89
90         static void inline run(IE const& ie, EncoderCtx& ctx)
91         {
92                 Tools::bit_accessor::put(static_cast<u8>(ie.get()), 1, ctx.refBuffer());
93         }
94
95         static void inline run(IE& ie, DecoderCtx& ctx)
96         {
97                 ie.set(0 != Tools::bit_accessor::get(1, ctx.refBuffer()));
98         }
99 };
100 /***************************************************************************************
101 * INTEGER: Encoding the integer type (X.691 12)
102 ***************************************************************************************/
103 template <class IE>
104 struct ElementType<IE, element_type::T_INTEGER>
105 {
106 protected:
107         template <class, element_type> friend struct Element;
108
109         static void inline run(IE const& ie, EncoderCtx& ctx)
110         {
111                 Integer<IE>::run(ie, ctx);
112         }
113         static void inline run(IE& ie, DecoderCtx& ctx)
114         {
115                 Integer<IE>::run(ie, ctx);
116         }
117 };
118 /***************************************************************************************
119 * ENUMERATED: Encoding the enumerated type (X.691 13)
120 ***************************************************************************************/
121 template <class IE>
122 struct ElementType<IE, element_type::T_ENUMERATED>
123 {
124 protected:
125         template <class, element_type> friend struct Element;
126
127         static void inline run(IE const& ie, EncoderCtx& ctx)
128         {
129                 Enumerated<IE>::run(ie, ctx);
130         }
131         static void inline run(IE& ie, DecoderCtx& ctx)
132         {
133                 Enumerated<IE>::run(ie, ctx);
134         }
135 };
136 /***************************************************************************************
137 * BIT STRING: Encoding the bitstring type (X.691 15)
138 ***************************************************************************************/
139 template <class IE>
140 struct ElementType<IE, element_type::T_BITSTRING>
141 {
142 protected:
143         template <class, element_type> friend struct Element;
144
145         static void inline run(IE const& ie, EncoderCtx& ctx)
146         {
147                 Bitstring<IE>::run(ie, ctx);
148         }
149         static void inline run(IE& ie, DecoderCtx& ctx)
150         {
151                 ie.clear();
152                 Bitstring<IE>::run(ie, ctx);
153         }
154 };
155 /***************************************************************************************
156 * OCTET STRING: Encoding the octetstring type (X.691 16)
157 ***************************************************************************************/
158 template <class IE>
159 struct ElementType<IE, element_type::T_OCTETSTRING>
160 {
161 protected:
162         template <class, element_type> friend struct Element;
163
164         static void inline run(IE const& ie, EncoderCtx& ctx)
165         {
166                 Octetstring<IE>::run(ie, ctx);
167         }
168         static void inline run(IE& ie, DecoderCtx& ctx)
169         {
170                 ie.clear();
171                 Octetstring<IE>::run(ie, ctx);
172         }
173 };
174 /***************************************************************************************
175 * NULL: Encoding the null type (X.691 17)
176 ***************************************************************************************/
177 template <class IE>
178 struct ElementType<IE, element_type::T_NULL>
179 {
180 protected:
181         template <class, element_type> friend struct Element;
182
183         static void inline run(IE const& ie, EncoderCtx& ctx) { /*do nothing*/ }
184         static void inline run(IE& ie, DecoderCtx& ctx)
185         {
186                 ie.setpresent(true);
187         }
188 };
189 /***************************************************************************************
190 * SEQUENCE: Encoding the sequence type (X.691 18)
191 ***************************************************************************************/
192 template <class IE>
193 struct ElementType<IE, element_type::T_SEQUENCE>
194 {
195 protected:
196         template <class, element_type> friend struct Element;
197
198         static void inline run(IE const& ie, EncoderCtx& ctx)
199         {
200                 ctx.container_name(IE::name());
201                 Seq<IE>::run(ie, ctx);
202         }
203         static void inline run(IE& ie, DecoderCtx& ctx)
204         {
205                 ctx.m_container = &ie;
206                 Seq<IE>::run(ie, ctx);
207                 ctx.m_container = nullptr;
208         }
209 };
210 /***************************************************************************************
211 * SEQUENCE OF: Encoding the sequence-of type (X.691 19)
212 ***************************************************************************************/
213 template <class IE>
214 struct ElementType<IE, element_type::T_SEQUENCE_OF>
215 {
216 protected:
217         template <class, element_type> friend struct Element;
218
219         static void inline run(IE const& ie, EncoderCtx& ctx)
220         {
221                 ctx.container_name(IE::name());
222                 SequenceOf<IE>::run(ie, ctx);
223
224                 for (auto& elem : ie)
225                         Element<typename IE::element_t, IE::element_t::ie_type>::run(elem, ctx);
226         }
227         static void inline run(IE& ie, DecoderCtx& ctx)
228         {
229                 ie.clear();
230                 SequenceOf<IE>::run(ie, ctx);
231
232                 for (auto & elem : ie)
233                         Element<typename IE::element_t, IE::element_t::ie_type>::run(elem, ctx);
234         }
235 };
236 /***************************************************************************************
237 * SET: Encoding the set type (X.691 20)
238 ***************************************************************************************/
239
240
241 /***************************************************************************************
242 * SET OF: Encoding the set-of type (X.691 21)
243 ***************************************************************************************/
244
245 /***************************************************************************************
246 * CHOICE: Encoding the choice type (X.691 22)
247 ***************************************************************************************/
248 template <class IE>
249 struct ElementType<IE, element_type::T_CHOICE>
250 {
251 protected:
252         template <class, element_type> friend struct Element;
253
254         static void inline run(IE const& ie, EncoderCtx& ctx)
255         {
256                 if(ie.is_valid())
257                 {
258                         ctx.container_name(IE::name());
259                         Choice<IE>::run(ie, ctx);
260                 }
261                 else
262                         ctx.refErrorCtx().tagError(ie.get_index());
263         }
264         static void inline run(IE& ie, DecoderCtx& ctx)
265         {
266                 ie.clear();
267                 ctx.m_container = &ie;
268                 Choice<IE>::run(ie, ctx);
269                 ctx.m_container = nullptr;
270         }
271 };
272
273 /***************************************************************************************
274 * IE_OBJECT_IDENTIFIER: Encoding the object identifier type (X.691 23)
275 ***************************************************************************************/
276 template <class IE>
277 struct ElementType<IE, element_type::T_OBJECTIDENTIFIER>
278 {
279 protected:
280         template <class, element_type> friend struct Element;
281
282         static void inline run(IE const& ie, EncoderCtx& ctx)
283         {
284                 Oid<IE>::run(ie, ctx);
285         }
286         static void inline run(IE& ie, DecoderCtx& ctx)
287         {
288                 ie.clear();
289                 Oid<IE>::run(ie, ctx);
290         }
291 };
292
293 /***************************************************************************************
294 * COMMON: Element
295 ***************************************************************************************/
296 template <class IE, element_type IE_TYPE>
297 struct Element
298 {
299         static void inline run(IE const& ie, EncoderCtx& ctx)
300         {
301                 if (ctx)
302                 {
303                         ASN_ENCODER_TRACE("IE<type=%d name=%s> buffer: %s", static_cast<int>(IE_TYPE), IE::name(), ctx.refBuffer().toString());
304                         ctx.ie_name(IE::name());
305                         if (IE_TYPE != element_type::T_SEQUENCE && !ie.is_valid())
306                         {
307                                 ctx.refErrorCtx().errorNoMandatory();
308                         }
309                         else
310                                 ElementType<IE, IE_TYPE>::run(ie, ctx);
311                 }
312         }
313         static void inline run(IE& ie, DecoderCtx& ctx)
314         {
315                 if (ctx)
316                 {
317                         ASN_DECODER_TRACE("IE<type=%d name=%s> buffer: %s", static_cast<int>(IE_TYPE), IE::name(), ctx.refBuffer().toString());
318                         ctx.ie_name(IE::name());
319                         ElementType<IE, IE_TYPE>::run(ie, ctx);
320                 }
321         }
322 };
323
324 } //namespace per
325 } //namespace asn