Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / src / ASN1 / asn / per / context.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 #include <climits>
23
24 // Local Includes: Application specific classes, functions, and libraries
25 #include "asn/utility.hpp"
26 #include "asn/buffer.hpp"
27 #include "asn/error_context.hpp"
28
29 namespace asn {
30 namespace per {
31
32 /********************************************************************************
33 EncoderCtx
34 *********************************************************************************/
35 class EncoderCtx : boost::noncopyable
36 {
37 public:
38         typedef buffer<u8*> buf_type;
39
40         EncoderCtx(u8* data, size_t size)
41                 : m_buffer{ m_errCtx }
42         {
43                 Reset(data, size);
44         }
45
46         template <size_t SIZE>
47         explicit EncoderCtx(u8(&buff)[SIZE])
48                 : EncoderCtx(buff, SIZE)
49         {
50         }
51
52         explicit EncoderCtx()
53                 : EncoderCtx(nullptr, 0)
54         {
55         }
56
57         buf_type& refBuffer() { return m_buffer; }
58         error_context& refErrorCtx() { return m_errCtx; }
59         explicit operator bool() const { return static_cast<bool>(m_errCtx); }
60
61         void Reset(void* data = nullptr, size_t size = 0)
62         {
63                 m_buffer.reset(static_cast<u8*>(data), size);
64                 m_errCtx.reset();
65         }
66
67         //Name of the being processed IE
68         void ie_name(const char* name) { m_errCtx.ie_name(name); }
69         char const* ie_name() const { return m_errCtx.ie_name(); }
70
71         void container_name(const char* name) { m_errCtx.container_name(name); }
72         char const* container_name() const { return m_errCtx.container_name(); }
73
74 private:
75         template <class IE, int IE_TYPE>
76         friend struct Encode;
77
78         error_context                   m_errCtx;
79         buf_type                                m_buffer;
80 };
81
82
83 /********************************************************************************
84 DecoderCtx
85 *********************************************************************************/
86 class DecoderCtx : boost::noncopyable
87 {
88 public:
89         typedef buffer<u8 const*>       buf_type;
90         typedef allocator                       alloc_type;
91         typedef u64                                     map_type;
92
93         DecoderCtx(const void* data = nullptr, size_t size = 0, void* alloc_buffer = nullptr, size_t alloc_buffer_size = 0)
94                 : m_buffer{ m_errCtx }
95         {
96                 reset(data, size, alloc_buffer, alloc_buffer_size);
97         }
98
99         buf_type& refBuffer() { return m_buffer; }
100         alloc_type& refAllocator() { return m_allocator; }
101         error_context& refErrorCtx() { return m_errCtx; }
102         explicit operator bool() const { return static_cast<bool>(m_errCtx); }
103
104         void reset(const void* data = nullptr, size_t size = 0, void* alloc_buffer = nullptr, size_t alloc_buffer_size = 0)
105         {
106                 m_allocator.reset(alloc_buffer, alloc_buffer_size);
107                 m_buffer.reset(static_cast<u8 const*>(data), size);
108                 m_errCtx.reset();
109                 m_map = 0;
110                 m_container = nullptr;
111         }
112
113         bool map_elm()
114         {
115                 //m_map = (m_map << 1) | (m_map >> (sizeof(m_map)*CHAR_BIT - 1));
116                 //return m_map & 1u;
117
118                 constexpr map_type mask = ((map_type)1) << (sizeof(map_type)*CHAR_BIT - 1);
119
120                 bool rv = m_map & mask;
121                 m_map = m_map << 1;
122
123                 return rv;
124         }
125
126         map_type set_map(map_type map)
127         {
128                 map_type rval = m_map;
129                 m_map = map;
130                 return rval;
131         }
132
133         map_type get_map() const {return m_map;}
134
135         //Pointer to container (SEQ | CHO)
136         void*                                   m_container{ nullptr };
137
138         //Name of the being processed IE
139         void ie_name(const char* name) { m_errCtx.ie_name(name); }
140         char const* ie_name() const { return m_errCtx.ie_name(); }
141
142         void container_name(const char* name) { m_errCtx.container_name(name); }
143         char const* container_name() const { return m_errCtx.container_name(); }
144
145 private:
146         //Optional or Extension elements' presence bitmap. Used in sequences
147         map_type                                m_map{ 0 };
148         error_context                   m_errCtx;
149         buf_type                                m_buffer;
150         alloc_type                              m_allocator;
151 };
152
153 } //namespace per
154 } //namespace asn