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"
29 /********************************************************************************
31 *********************************************************************************/
34 explicit VisitorEncoder(EncoderCtx& ctx) : m_ctx(ctx) {}
36 template <typename IE>
37 bool operator() (IE const& ie) const
39 Element<IE, IE::ie_type>::run(ie, m_ctx);
40 return static_cast<bool>(m_ctx);
45 /********************************************************************************
47 *********************************************************************************/
50 VisitorDecoder(DecoderCtx& ctx) : m_ctx(ctx) {}
52 template <typename IE>
53 bool operator() (IE& ie) const
55 Element<IE, IE::ie_type>::run(ie, m_ctx);
56 return static_cast<bool>(m_ctx);
61 /***************************************************************************************
62 * Open Type Visitor // Encode Open Type (10.2)
63 ***************************************************************************************/
64 struct OpenTypeVisitorEncoder
66 explicit OpenTypeVisitorEncoder(EncoderCtx& ctx) : m_ctx(ctx) {}
68 template <typename IE>
69 bool operator() (IE const& ie) const
71 size_t reserved_size = 1; //1 byte length is most likely
72 Tools::bit_accessor::padByte(m_ctx.refBuffer());
74 if (m_ctx.refBuffer().getBytesLeft())
76 EncoderCtx::buf_type::pointer p = m_ctx.refBuffer().advance(reserved_size);
77 EncoderCtx::buf_type::pointer start = p + reserved_size;
79 Element<IE, IE::ie_type>::run(ie, m_ctx);
83 Tools::bit_accessor::padByte(m_ctx.refBuffer());
84 EncoderCtx::buf_type::pointer p_new = m_ctx.refBuffer().begin();
85 size_t size = p_new - start;
86 size_t needed = LengthDeterminantDefault::bytes_needed(size) - reserved_size;
87 if (needed) //1 byte is not enough for the length determinant. it is hardly possible
89 if (m_ctx.refBuffer().getBytesLeft() < needed) {
90 m_ctx.refErrorCtx().lengthErrorBytes(m_ctx.refBuffer().getBytesLeft(), needed);
93 memmove(start + needed, start, size);
96 m_ctx.refBuffer().set_begin(p, 0);
97 LengthDeterminantDefault::run(m_ctx, size);
99 m_ctx.refBuffer().set_begin(p_new, 0);
100 Tools::bit_accessor::padByte(m_ctx.refBuffer());
106 m_ctx.refErrorCtx().lengthErrorBytes(m_ctx.refBuffer().getBytesLeft(), reserved_size);
108 return static_cast<bool>(m_ctx);
113 /***************************************************************************************
114 * Open Type Visitor // Decode Open Type (10.2)
115 ***************************************************************************************/
116 struct OpenTypeVisitorDecoder
118 explicit OpenTypeVisitorDecoder(DecoderCtx& ctx) : m_ctx(ctx) {}
120 template <typename IE>
121 bool operator() (IE& ie) const
123 size_t size = LengthDeterminantDefault::run(m_ctx);
124 DecoderCtx::buf_type& buffer = m_ctx.refBuffer();
125 if (buffer.getBytesLeft() < size)
127 m_ctx.refErrorCtx().lengthErrorBytes(buffer.getBytesLeft(), size);
130 DecoderCtx::buf_type::pointer end = buffer.end();
131 buffer.set_end(buffer.begin() + size);
132 Element<IE, IE::ie_type>::run(ie, m_ctx);
135 return static_cast<bool>(m_ctx);