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/elements.hpp"
25 #include "asn/per/common.hpp"
26 #include "asn/per/binary_integer.hpp"
27 #include "asn/per/whole_number.hpp"
32 template<class IE, class Enable = void>
36 struct Integer<IE, std::enable_if_t<IE::constraint_t::type == constraint_type::CONSTRAINED> >
38 static void inline run(IE const& ie, EncoderCtx& ctx)
40 if(IE::constraint_t::is_extended(ie.get()))
42 ctx.refErrorCtx().valueRangeError(static_cast<size_t>(ie.get()));
45 ConstrainedWholeNumber<typename IE::constraint_t, typename IE::value_type>::run(ctx, ie.get());
47 static void inline run(IE& ie, DecoderCtx& ctx)
49 ie.set(ConstrainedWholeNumber<typename IE::constraint_t, typename IE::value_type>::run(ctx));
54 struct Integer<IE, std::enable_if_t<IE::constraint_t::type == constraint_type::CONSTRAINED_EXTENDED> >
57 static void inline run(IE const& ie, EncoderCtx& ctx)
59 if (!IE::constraint_t::is_extended(ie.get()))
61 Tools::bit_accessor::put(0, 1, ctx.refBuffer());
63 ConstrainedWholeNumber<typename IE::constraint_t, typename IE::value_type>::run(ctx, ie.get());
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);
72 static void inline run(IE& ie, DecoderCtx& ctx)
75 u8 ext = Tools::bit_accessor::get(1, ctx.refBuffer());
77 TwosComplementBinaryInteger<IE>::run(ie, ctx);
79 ie.set(ConstrainedWholeNumber<typename IE::constraint_t, typename IE::value_type>::run(ctx));
85 struct Integer<IE, std::enable_if_t<IE::constraint_t::type == constraint_type::SEMICONSTRAINED> >
87 static void inline run(IE const& ie, EncoderCtx& ctx)
89 if(IE::constraint_t::is_extended(ie.get()))
91 ctx.refErrorCtx().valueRangeError(static_cast<size_t>(ie.get()));
94 NonnegativeBinaryInteger<typename IE::constraint_t>::run(ie.get() - IE::constraint_t::lower_bound, ctx);
96 static void inline run(IE& ie, DecoderCtx& ctx)
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);
105 struct Integer<IE, std::enable_if_t<IE::constraint_t::type == constraint_type::SEMICONSTRAINED_EXTENDED> >
107 static void inline run(IE const& ie, EncoderCtx& ctx)
109 if (!IE::constraint_t::is_extended(ie.get()))
111 Tools::bit_accessor::put(0, 1, ctx.refBuffer());
112 NonnegativeBinaryInteger<typename IE::constraint_t>::run(ie.get() - IE::constraint_t::lower_bound, ctx);
116 Tools::bit_accessor::put(1, 1, ctx.refBuffer());
117 TwosComplementBinaryInteger<IE>::run(ie, ctx);
120 static void inline run(IE& ie, DecoderCtx& ctx)
122 u8 ext = Tools::bit_accessor::get(1, ctx.refBuffer());
125 TwosComplementBinaryInteger<IE>::run(ie, ctx);
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);
135 /***************************************************************************************
136 * Encoding of an unconstrained whole number (X.691 10.8)
137 ***************************************************************************************/
140 struct Integer<IE, std::enable_if_t<IE::constraint_t::type == constraint_type::UNCONSTRAINED> >
142 static void inline run(IE const& ie, EncoderCtx& ctx)
144 TwosComplementBinaryInteger<IE>::run(ie, ctx);
146 static void inline run(IE& ie, DecoderCtx& ctx)
148 TwosComplementBinaryInteger<IE>::run(ie, ctx);