Issue-ID: SIM-18
[sim/e2-interface.git] / e2sim / e2apv1sim / e2sim / src / ASN1 / asn / per / integer.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/elements.hpp"
25 #include "asn/per/common.hpp"
26 #include "asn/per/binary_integer.hpp"
27 #include "asn/per/whole_number.hpp"
28
29 namespace asn {
30 namespace per {
31
32 template<class IE, class Enable = void>
33 struct Integer;
34
35 template<class IE>
36 struct Integer<IE, std::enable_if_t<IE::constraint_t::type == constraint_type::CONSTRAINED> >
37 {
38         static void inline run(IE const& ie, EncoderCtx& ctx)
39         {
40                 if(IE::constraint_t::is_extended(ie.get()))
41                 {
42                         ctx.refErrorCtx().valueRangeError(static_cast<size_t>(ie.get()));
43                         return;
44                 }
45                 ConstrainedWholeNumber<typename IE::constraint_t, typename IE::value_type>::run(ctx, ie.get());
46         }
47         static void inline run(IE& ie, DecoderCtx& ctx)
48         {
49                 ie.set(ConstrainedWholeNumber<typename IE::constraint_t, typename IE::value_type>::run(ctx));
50         }
51 };
52
53 template<class IE>
54 struct Integer<IE, std::enable_if_t<IE::constraint_t::type == constraint_type::CONSTRAINED_EXTENDED> >
55 {
56         //X.691 12.1
57         static void inline run(IE const& ie, EncoderCtx& ctx)
58         {
59                 if (!IE::constraint_t::is_extended(ie.get()))
60                 {
61                         Tools::bit_accessor::put(0, 1, ctx.refBuffer());
62                         //X.691 12.2
63                         ConstrainedWholeNumber<typename IE::constraint_t, typename IE::value_type>::run(ctx, ie.get());
64                 }
65                 else
66                 {
67                         Tools::bit_accessor::put(1, 1, ctx.refBuffer());
68                         //X.691 12.2.4, 12.2.6, 10.8s
69                         TwosComplementBinaryInteger<IE>::run(ie, ctx);
70                 }
71         }
72         static void inline run(IE& ie, DecoderCtx& ctx)
73         {
74
75                 u8 ext = Tools::bit_accessor::get(1, ctx.refBuffer());
76                 if (ext)
77                         TwosComplementBinaryInteger<IE>::run(ie, ctx);
78                 else
79                         ie.set(ConstrainedWholeNumber<typename IE::constraint_t, typename IE::value_type>::run(ctx));
80         }
81 };
82
83 // (X.691 10.7)
84 template<class IE>
85 struct Integer<IE, std::enable_if_t<IE::constraint_t::type == constraint_type::SEMICONSTRAINED> >
86 {
87         static void inline run(IE const& ie, EncoderCtx& ctx)
88         {
89                 if(IE::constraint_t::is_extended(ie.get()))
90                 {
91                         ctx.refErrorCtx().valueRangeError(static_cast<size_t>(ie.get()));
92                         return;
93                 }
94                 NonnegativeBinaryInteger<typename IE::constraint_t>::run(ie.get() - IE::constraint_t::lower_bound, ctx);
95         }
96         static void inline run(IE& ie, DecoderCtx& ctx)
97         {
98                 typename IE::value_type val = 0;
99                 NonnegativeBinaryInteger<typename IE::constraint_t>::run(val, ctx, false);
100                 ie.set(IE::constraint_t::lower_bound + val);
101         }
102 };
103
104 template<class IE>
105 struct Integer<IE, std::enable_if_t<IE::constraint_t::type == constraint_type::SEMICONSTRAINED_EXTENDED> >
106 {
107         static void inline run(IE const& ie, EncoderCtx& ctx)
108         {
109                 if (!IE::constraint_t::is_extended(ie.get()))
110                 {
111                         Tools::bit_accessor::put(0, 1, ctx.refBuffer());
112                         NonnegativeBinaryInteger<typename IE::constraint_t>::run(ie.get() - IE::constraint_t::lower_bound, ctx);
113                 }
114                 else
115                 {
116                         Tools::bit_accessor::put(1, 1, ctx.refBuffer());
117                         TwosComplementBinaryInteger<IE>::run(ie, ctx);
118                 }
119         }
120         static void inline run(IE& ie, DecoderCtx& ctx)
121         {
122                 u8 ext = Tools::bit_accessor::get(1, ctx.refBuffer());
123
124                 if (ext)
125                         TwosComplementBinaryInteger<IE>::run(ie, ctx);
126                 else
127                 {
128                         typename IE::value_type val = 0;
129                         NonnegativeBinaryInteger<typename IE::constraint_t>::run(val, ctx, false);
130                         ie.set(val + IE::constraint_t::lower_bound);
131                 }
132         }
133 };
134
135 /***************************************************************************************
136 * Encoding of an unconstrained whole number (X.691 10.8)
137 ***************************************************************************************/
138
139 template<class IE>
140 struct Integer<IE, std::enable_if_t<IE::constraint_t::type == constraint_type::UNCONSTRAINED> >
141 {
142         static void inline run(IE const& ie, EncoderCtx& ctx)
143         {
144                 TwosComplementBinaryInteger<IE>::run(ie, ctx);
145         }
146         static void inline run(IE& ie, DecoderCtx& ctx)
147         {
148                 TwosComplementBinaryInteger<IE>::run(ie, ctx);
149         }
150 };
151
152 } //namespace per
153 } //namespace asn
154