2 /******************************************************************************
4 * Copyright (c) 2019 AT&T Intellectual Property.
5 * Copyright (c) 2018-2019 Nokia.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************************/
21 // Standard Includes: ANSI C/C++, MSA, and Third-Party Libraries
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"
41 /********************************************************************************
43 *********************************************************************************/
45 bool pack(IE const& ie, EncoderCtx& ctx)
47 ctx.refErrorCtx().reset();
48 Element<IE, IE::ie_type>::run(ie, ctx);
51 Tools::bit_accessor::padByte(ctx.refBuffer());
53 return static_cast<bool>(ctx);
55 /********************************************************************************
57 *********************************************************************************/
59 bool unpack(IE& ie, DecoderCtx& ctx)
61 Element<IE, IE::ie_type>::run(ie, ctx);
64 Tools::bit_accessor::padByte(ctx.refBuffer());
66 if(ctx && ctx.refBuffer().getBytesLeft())
68 ctx.ie_name(IE::name());
69 ctx.refErrorCtx().lengthErrorBytes(ctx.refBuffer().getBytesLeft(), 0);
72 return static_cast<bool>(ctx);
75 /***************************************************************************************
77 ***************************************************************************************/
78 template <class IE, element_type IE_TYPE>
81 /***************************************************************************************
82 * BOOLEAN: Encoding the boolean type (X.691 11)
83 ***************************************************************************************/
85 struct ElementType<IE, element_type::T_BOOLEAN>
88 template <class, element_type> friend struct Element;
90 static void inline run(IE const& ie, EncoderCtx& ctx)
92 Tools::bit_accessor::put(static_cast<u8>(ie.get()), 1, ctx.refBuffer());
95 static void inline run(IE& ie, DecoderCtx& ctx)
97 ie.set(0 != Tools::bit_accessor::get(1, ctx.refBuffer()));
100 /***************************************************************************************
101 * INTEGER: Encoding the integer type (X.691 12)
102 ***************************************************************************************/
104 struct ElementType<IE, element_type::T_INTEGER>
107 template <class, element_type> friend struct Element;
109 static void inline run(IE const& ie, EncoderCtx& ctx)
111 Integer<IE>::run(ie, ctx);
113 static void inline run(IE& ie, DecoderCtx& ctx)
115 Integer<IE>::run(ie, ctx);
118 /***************************************************************************************
119 * ENUMERATED: Encoding the enumerated type (X.691 13)
120 ***************************************************************************************/
122 struct ElementType<IE, element_type::T_ENUMERATED>
125 template <class, element_type> friend struct Element;
127 static void inline run(IE const& ie, EncoderCtx& ctx)
129 Enumerated<IE>::run(ie, ctx);
131 static void inline run(IE& ie, DecoderCtx& ctx)
133 Enumerated<IE>::run(ie, ctx);
136 /***************************************************************************************
137 * BIT STRING: Encoding the bitstring type (X.691 15)
138 ***************************************************************************************/
140 struct ElementType<IE, element_type::T_BITSTRING>
143 template <class, element_type> friend struct Element;
145 static void inline run(IE const& ie, EncoderCtx& ctx)
147 Bitstring<IE>::run(ie, ctx);
149 static void inline run(IE& ie, DecoderCtx& ctx)
152 Bitstring<IE>::run(ie, ctx);
155 /***************************************************************************************
156 * OCTET STRING: Encoding the octetstring type (X.691 16)
157 ***************************************************************************************/
159 struct ElementType<IE, element_type::T_OCTETSTRING>
162 template <class, element_type> friend struct Element;
164 static void inline run(IE const& ie, EncoderCtx& ctx)
166 Octetstring<IE>::run(ie, ctx);
168 static void inline run(IE& ie, DecoderCtx& ctx)
171 Octetstring<IE>::run(ie, ctx);
174 /***************************************************************************************
175 * NULL: Encoding the null type (X.691 17)
176 ***************************************************************************************/
178 struct ElementType<IE, element_type::T_NULL>
181 template <class, element_type> friend struct Element;
183 static void inline run(IE const& ie, EncoderCtx& ctx) { /*do nothing*/ }
184 static void inline run(IE& ie, DecoderCtx& ctx)
189 /***************************************************************************************
190 * SEQUENCE: Encoding the sequence type (X.691 18)
191 ***************************************************************************************/
193 struct ElementType<IE, element_type::T_SEQUENCE>
196 template <class, element_type> friend struct Element;
198 static void inline run(IE const& ie, EncoderCtx& ctx)
200 ctx.container_name(IE::name());
201 Seq<IE>::run(ie, ctx);
203 static void inline run(IE& ie, DecoderCtx& ctx)
205 ctx.m_container = &ie;
206 Seq<IE>::run(ie, ctx);
207 ctx.m_container = nullptr;
210 /***************************************************************************************
211 * SEQUENCE OF: Encoding the sequence-of type (X.691 19)
212 ***************************************************************************************/
214 struct ElementType<IE, element_type::T_SEQUENCE_OF>
217 template <class, element_type> friend struct Element;
219 static void inline run(IE const& ie, EncoderCtx& ctx)
221 ctx.container_name(IE::name());
222 SequenceOf<IE>::run(ie, ctx);
224 for (auto& elem : ie)
225 Element<typename IE::element_t, IE::element_t::ie_type>::run(elem, ctx);
227 static void inline run(IE& ie, DecoderCtx& ctx)
230 SequenceOf<IE>::run(ie, ctx);
232 for (auto & elem : ie)
233 Element<typename IE::element_t, IE::element_t::ie_type>::run(elem, ctx);
236 /***************************************************************************************
237 * SET: Encoding the set type (X.691 20)
238 ***************************************************************************************/
241 /***************************************************************************************
242 * SET OF: Encoding the set-of type (X.691 21)
243 ***************************************************************************************/
245 /***************************************************************************************
246 * CHOICE: Encoding the choice type (X.691 22)
247 ***************************************************************************************/
249 struct ElementType<IE, element_type::T_CHOICE>
252 template <class, element_type> friend struct Element;
254 static void inline run(IE const& ie, EncoderCtx& ctx)
258 ctx.container_name(IE::name());
259 Choice<IE>::run(ie, ctx);
262 ctx.refErrorCtx().tagError(ie.get_index());
264 static void inline run(IE& ie, DecoderCtx& ctx)
267 ctx.m_container = &ie;
268 Choice<IE>::run(ie, ctx);
269 ctx.m_container = nullptr;
273 /***************************************************************************************
274 * IE_OBJECT_IDENTIFIER: Encoding the object identifier type (X.691 23)
275 ***************************************************************************************/
277 struct ElementType<IE, element_type::T_OBJECTIDENTIFIER>
280 template <class, element_type> friend struct Element;
282 static void inline run(IE const& ie, EncoderCtx& ctx)
284 Oid<IE>::run(ie, ctx);
286 static void inline run(IE& ie, DecoderCtx& ctx)
289 Oid<IE>::run(ie, ctx);
293 /***************************************************************************************
295 ***************************************************************************************/
296 template <class IE, element_type IE_TYPE>
299 static void inline run(IE const& ie, EncoderCtx& ctx)
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())
307 ctx.refErrorCtx().errorNoMandatory();
310 ElementType<IE, IE_TYPE>::run(ie, ctx);
313 static void inline run(IE& ie, DecoderCtx& ctx)
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);