#pragma once /****************************************************************************** * * Copyright (c) 2019 AT&T Intellectual Property. * Copyright (c) 2018-2019 Nokia. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ******************************************************************************/ // Standard Includes: ANSI C/C++, MSA, and Third-Party Libraries // Local Includes: Application specific classes, functions, and libraries #include "asn/per/common.hpp" #include "asn/elements.hpp" #include "asn/per/context.hpp" #include "asn/per/length.hpp" #include "asn/per/integer.hpp" #include "asn/per/enumerated.hpp" #include "asn/per/ostring.hpp" #include "asn/per/bstring.hpp" #include "asn/per/sequence_of.hpp" #include "asn/per/sequence.hpp" #include "asn/per/choice.hpp" #include "asn/per/visitor.hpp" #include "asn/per/oid.hpp" namespace asn { namespace per { /******************************************************************************** pack *********************************************************************************/ template bool pack(IE const& ie, EncoderCtx& ctx) { ctx.refErrorCtx().reset(); Element::run(ie, ctx); if (ctx) Tools::bit_accessor::padByte(ctx.refBuffer()); return static_cast(ctx); } /******************************************************************************** unpack *********************************************************************************/ template bool unpack(IE& ie, DecoderCtx& ctx) { Element::run(ie, ctx); if (ctx) Tools::bit_accessor::padByte(ctx.refBuffer()); if(ctx && ctx.refBuffer().getBytesLeft()) { ctx.ie_name(IE::name()); ctx.refErrorCtx().lengthErrorBytes(ctx.refBuffer().getBytesLeft(), 0); } return static_cast(ctx); } /*************************************************************************************** * ElementType ***************************************************************************************/ template struct ElementType; /*************************************************************************************** * BOOLEAN: Encoding the boolean type (X.691 11) ***************************************************************************************/ template struct ElementType { protected: template friend struct Element; static void inline run(IE const& ie, EncoderCtx& ctx) { Tools::bit_accessor::put(static_cast(ie.get()), 1, ctx.refBuffer()); } static void inline run(IE& ie, DecoderCtx& ctx) { ie.set(0 != Tools::bit_accessor::get(1, ctx.refBuffer())); } }; /*************************************************************************************** * INTEGER: Encoding the integer type (X.691 12) ***************************************************************************************/ template struct ElementType { protected: template friend struct Element; static void inline run(IE const& ie, EncoderCtx& ctx) { Integer::run(ie, ctx); } static void inline run(IE& ie, DecoderCtx& ctx) { Integer::run(ie, ctx); } }; /*************************************************************************************** * ENUMERATED: Encoding the enumerated type (X.691 13) ***************************************************************************************/ template struct ElementType { protected: template friend struct Element; static void inline run(IE const& ie, EncoderCtx& ctx) { Enumerated::run(ie, ctx); } static void inline run(IE& ie, DecoderCtx& ctx) { Enumerated::run(ie, ctx); } }; /*************************************************************************************** * BIT STRING: Encoding the bitstring type (X.691 15) ***************************************************************************************/ template struct ElementType { protected: template friend struct Element; static void inline run(IE const& ie, EncoderCtx& ctx) { Bitstring::run(ie, ctx); } static void inline run(IE& ie, DecoderCtx& ctx) { ie.clear(); Bitstring::run(ie, ctx); } }; /*************************************************************************************** * OCTET STRING: Encoding the octetstring type (X.691 16) ***************************************************************************************/ template struct ElementType { protected: template friend struct Element; static void inline run(IE const& ie, EncoderCtx& ctx) { Octetstring::run(ie, ctx); } static void inline run(IE& ie, DecoderCtx& ctx) { ie.clear(); Octetstring::run(ie, ctx); } }; /*************************************************************************************** * NULL: Encoding the null type (X.691 17) ***************************************************************************************/ template struct ElementType { protected: template friend struct Element; static void inline run(IE const& ie, EncoderCtx& ctx) { /*do nothing*/ } static void inline run(IE& ie, DecoderCtx& ctx) { ie.setpresent(true); } }; /*************************************************************************************** * SEQUENCE: Encoding the sequence type (X.691 18) ***************************************************************************************/ template struct ElementType { protected: template friend struct Element; static void inline run(IE const& ie, EncoderCtx& ctx) { ctx.container_name(IE::name()); Seq::run(ie, ctx); } static void inline run(IE& ie, DecoderCtx& ctx) { ctx.m_container = &ie; Seq::run(ie, ctx); ctx.m_container = nullptr; } }; /*************************************************************************************** * SEQUENCE OF: Encoding the sequence-of type (X.691 19) ***************************************************************************************/ template struct ElementType { protected: template friend struct Element; static void inline run(IE const& ie, EncoderCtx& ctx) { ctx.container_name(IE::name()); SequenceOf::run(ie, ctx); for (auto& elem : ie) Element::run(elem, ctx); } static void inline run(IE& ie, DecoderCtx& ctx) { ie.clear(); SequenceOf::run(ie, ctx); for (auto & elem : ie) Element::run(elem, ctx); } }; /*************************************************************************************** * SET: Encoding the set type (X.691 20) ***************************************************************************************/ /*************************************************************************************** * SET OF: Encoding the set-of type (X.691 21) ***************************************************************************************/ /*************************************************************************************** * CHOICE: Encoding the choice type (X.691 22) ***************************************************************************************/ template struct ElementType { protected: template friend struct Element; static void inline run(IE const& ie, EncoderCtx& ctx) { if(ie.is_valid()) { ctx.container_name(IE::name()); Choice::run(ie, ctx); } else ctx.refErrorCtx().tagError(ie.get_index()); } static void inline run(IE& ie, DecoderCtx& ctx) { ie.clear(); ctx.m_container = &ie; Choice::run(ie, ctx); ctx.m_container = nullptr; } }; /*************************************************************************************** * IE_OBJECT_IDENTIFIER: Encoding the object identifier type (X.691 23) ***************************************************************************************/ template struct ElementType { protected: template friend struct Element; static void inline run(IE const& ie, EncoderCtx& ctx) { Oid::run(ie, ctx); } static void inline run(IE& ie, DecoderCtx& ctx) { ie.clear(); Oid::run(ie, ctx); } }; /*************************************************************************************** * COMMON: Element ***************************************************************************************/ template struct Element { static void inline run(IE const& ie, EncoderCtx& ctx) { if (ctx) { ASN_ENCODER_TRACE("IE buffer: %s", static_cast(IE_TYPE), IE::name(), ctx.refBuffer().toString()); ctx.ie_name(IE::name()); if (IE_TYPE != element_type::T_SEQUENCE && !ie.is_valid()) { ctx.refErrorCtx().errorNoMandatory(); } else ElementType::run(ie, ctx); } } static void inline run(IE& ie, DecoderCtx& ctx) { if (ctx) { ASN_DECODER_TRACE("IE buffer: %s", static_cast(IE_TYPE), IE::name(), ctx.refBuffer().toString()); ctx.ie_name(IE::name()); ElementType::run(ie, ctx); } } }; } //namespace per } //namespace asn