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
28 constexpr size_t indefinite_length = std::numeric_limits<size_t>::max();
30 /***************************************************************************************
32 ***************************************************************************************/
35 static size_t inline get(int64_t value)
41 size_t extra_sign_bits = __builtin_clrsbll(value);
42 length = sizeof(value) - (extra_sign_bits >> 3) ;
48 static void inline encode(size_t length, EncoderCtx& ctx)
50 auto & buffer = ctx.refBuffer();
54 buffer.putByte(static_cast<uint8_t>(length));
58 size_t lenlen = get(length);
61 ctx.refErrorCtx().sizeRangeError(length, 0, 0x7F);
65 uint8_t bt = static_cast<uint8_t>(lenlen);
68 Tools::put_bytes(length, lenlen, ctx);
74 static size_t inline decode(DecoderCtx& ctx)
76 auto & buffer = ctx.refBuffer();
79 uint8_t const* data = buffer.getBytes(1);
89 size_t lenlen = data[0] & 0x7F;
92 Tools::get_bytes(length, lenlen, ctx);
94 length = indefinite_length;