blob: 28b8af0ca08c2d8ef81531b2d528ae185807dbcc [file] [log] [blame]
Alexei Frolov82d3cb32019-11-27 14:38:39 -08001// Copyright 2019 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
Wyatt Hepler8c493bb2019-12-02 14:49:21 -08004// use this file except in compliance with the License. You may obtain a copy of
5// the License at
Alexei Frolov82d3cb32019-11-27 14:38:39 -08006//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Wyatt Hepler8c493bb2019-12-02 14:49:21 -080012// License for the specific language governing permissions and limitations under
13// the License.
Alexei Frolov82d3cb32019-11-27 14:38:39 -080014
15#include "pw_varint/varint.h"
16
17#include <algorithm>
18
19namespace pw::varint {
20namespace {
21
22constexpr int64_t ZigZagDecode64(uint64_t n) {
23 return static_cast<int64_t>((n >> 1) ^ (~(n & 1) + 1));
24}
25
26} // namespace
27
28size_t EncodeLittleEndianBase128(uint64_t integer,
29 const span<uint8_t>& output) {
30 size_t written = 0;
31 do {
32 if (written >= output.size()) {
33 return 0;
34 }
35
36 // Grab 7 bits; the eighth bit is set to 1 to indicate more data coming.
37 output[written++] = static_cast<uint8_t>(integer) | '\x80';
38 integer >>= 7;
39 } while (integer != 0u);
40
41 output[written - 1] &= '\x7f'; // clear the top bit of the last byte
42 return written;
43}
44
45size_t DecodeVarint(const span<const uint8_t>& input, int64_t* value) {
46 const size_t bytes = DecodeVarint(input, reinterpret_cast<uint64_t*>(value));
47 *value = ZigZagDecode64(*value);
48 return bytes;
49}
50
51size_t DecodeVarint(const span<const uint8_t>& input, uint64_t* value) {
52 uint64_t decoded_value = 0;
53 uint_fast8_t count = 0;
54
55 // The largest 64-bit ints require 10 B.
56 const size_t max_count = std::min(kMaxVarintSizeBytes, input.size());
57
58 while (true) {
59 if (count >= max_count) {
60 return 0;
61 }
62
63 // Add the bottom seven bits of the next byte to the result.
64 decoded_value |= static_cast<uint64_t>(input[count] & '\x7f')
65 << (7 * count);
66
67 // Stop decoding if the top bit is not set.
68 if ((input[count++] & '\x80') == 0) {
69 break;
70 }
71 }
72
73 *value = decoded_value;
74 return count;
75}
76
77} // namespace pw::varint