1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
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
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15
16 #include <stddef.h>
17 #include <stdint.h>
18
19 #include "pw_preprocessor/compiler.h"
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 // Expose a subset of the varint API for use in C code.
26
27 typedef enum {
28 PW_VARINT_ZERO_TERMINATED_LEAST_SIGNIFICANT = 0,
29 PW_VARINT_ZERO_TERMINATED_MOST_SIGNIFICANT = 1,
30 PW_VARINT_ONE_TERMINATED_LEAST_SIGNIFICANT = 2,
31 PW_VARINT_ONE_TERMINATED_MOST_SIGNIFICANT = 3,
32 } pw_varint_Format;
33
34 size_t pw_varint_EncodeCustom(uint64_t integer,
35 void* output,
36 size_t output_size,
37 pw_varint_Format format);
38 size_t pw_varint_DecodeCustom(const void* input,
39 size_t input_size,
40 uint64_t* output,
41 pw_varint_Format format);
42
pw_varint_Encode(uint64_t integer,void * output,size_t output_size)43 static inline size_t pw_varint_Encode(uint64_t integer,
44 void* output,
45 size_t output_size) {
46 return pw_varint_EncodeCustom(
47 integer, output, output_size, PW_VARINT_ZERO_TERMINATED_MOST_SIGNIFICANT);
48 }
49
50 size_t pw_varint_ZigZagEncode(int64_t integer,
51 void* output,
52 size_t output_size);
53
pw_varint_Decode(const void * input,size_t input_size,uint64_t * output)54 static inline size_t pw_varint_Decode(const void* input,
55 size_t input_size,
56 uint64_t* output) {
57 return pw_varint_DecodeCustom(
58 input, input_size, output, PW_VARINT_ZERO_TERMINATED_MOST_SIGNIFICANT);
59 }
60
61 size_t pw_varint_ZigZagDecode(const void* input,
62 size_t input_size,
63 int64_t* output);
64
65 // Returns the size of an when encoded as a varint.
66 size_t pw_varint_EncodedSize(uint64_t integer);
67 size_t pw_varint_ZigZagEncodedSize(int64_t integer);
68
69 #ifdef __cplusplus
70
71 } // extern "C"
72
73 #include <limits>
74 #include <type_traits>
75
76 #include "pw_polyfill/language_feature_macros.h"
77 #include "pw_span/span.h"
78
79 namespace pw {
80 namespace varint {
81
82 // The maximum number of bytes occupied by an encoded varint.
83 PW_INLINE_VARIABLE constexpr size_t kMaxVarint32SizeBytes = 5;
84 PW_INLINE_VARIABLE constexpr size_t kMaxVarint64SizeBytes = 10;
85
86 // ZigZag encodes a signed integer. This maps small negative numbers to small,
87 // unsigned positive numbers, which improves their density for LEB128 encoding.
88 //
89 // ZigZag encoding works by moving the sign bit from the most-significant bit to
90 // the least-significant bit. For the signed k-bit integer n, the formula is
91 //
92 // (n << 1) ^ (n >> (k - 1))
93 //
94 // See the following for a description of ZigZag encoding:
95 // https://developers.google.com/protocol-buffers/docs/encoding#types
96 template <typename T>
ZigZagEncode(T n)97 constexpr std::make_unsigned_t<T> ZigZagEncode(T n) {
98 static_assert(std::is_signed<T>(), "Zig-zag encoding is for signed integers");
99 using U = std::make_unsigned_t<T>;
100 return (static_cast<U>(n) << 1) ^ static_cast<U>(n >> (sizeof(T) * 8 - 1));
101 }
102
103 // ZigZag decodes a signed integer.
104 // The calculation is done modulo std::numeric_limits<T>::max()+1, so the
105 // unsigned integer overflows are intentional.
106 template <typename T>
ZigZagDecode(T n)107 constexpr std::make_signed_t<T> ZigZagDecode(T n)
108 PW_NO_SANITIZE("unsigned-integer-overflow") {
109 static_assert(std::is_unsigned<T>(),
110 "Zig-zag decoding is for unsigned integers");
111 return static_cast<std::make_signed_t<T>>((n >> 1) ^ (~(n & 1) + 1));
112 }
113
114 // Encodes a uint64_t with Little-Endian Base 128 (LEB128) encoding.
EncodeLittleEndianBase128(uint64_t integer,const span<std::byte> & output)115 inline size_t EncodeLittleEndianBase128(uint64_t integer,
116 const span<std::byte>& output) {
117 return pw_varint_Encode(integer, output.data(), output.size());
118 }
119
120 // Encodes the provided integer using a variable-length encoding and returns the
121 // number of bytes written.
122 //
123 // The encoding is the same as used in protocol buffers. Signed integers are
124 // ZigZag encoded to remove leading 1s from small negative numbers, then the
125 // resulting number is encoded as Little Endian Base 128 (LEB128). Unsigned
126 // integers are encoded directly as LEB128.
127 //
128 // Returns the number of bytes written or 0 if the result didn't fit in the
129 // encoding buffer.
130 template <typename T>
Encode(T integer,const span<std::byte> & output)131 size_t Encode(T integer, const span<std::byte>& output) {
132 if (std::is_signed<T>()) {
133 return pw_varint_ZigZagEncode(integer, output.data(), output.size());
134 } else {
135 return pw_varint_Encode(integer, output.data(), output.size());
136 }
137 }
138
139 // Decodes a varint-encoded value. If reading into a signed integer, the value
140 // is ZigZag decoded.
141 //
142 // Returns the number of bytes read from the input if successful. Returns zero
143 // if the result does not fit in a int64_t / uint64_t or if the input is
144 // exhausted before the number terminates. Reads a maximum of 10 bytes.
145 //
146 // The following example decodes multiple varints from a buffer:
147 //
148 // while (!data.empty()) {
149 // int64_t value;
150 // size_t bytes = Decode(data, &value);
151 //
152 // if (bytes == 0u) {
153 // return Status::DataLoss();
154 // }
155 // results.push_back(value);
156 // data = data.subspan(bytes)
157 // }
158 //
Decode(const span<const std::byte> & input,int64_t * value)159 inline size_t Decode(const span<const std::byte>& input, int64_t* value) {
160 return pw_varint_ZigZagDecode(input.data(), input.size(), value);
161 }
162
Decode(const span<const std::byte> & input,uint64_t * value)163 inline size_t Decode(const span<const std::byte>& input, uint64_t* value) {
164 return pw_varint_Decode(input.data(), input.size(), value);
165 }
166
167 enum class Format {
168 kZeroTerminatedLeastSignificant = PW_VARINT_ZERO_TERMINATED_LEAST_SIGNIFICANT,
169 kZeroTerminatedMostSignificant = PW_VARINT_ZERO_TERMINATED_MOST_SIGNIFICANT,
170 kOneTerminatedLeastSignificant = PW_VARINT_ONE_TERMINATED_LEAST_SIGNIFICANT,
171 kOneTerminatedMostSignificant = PW_VARINT_ONE_TERMINATED_MOST_SIGNIFICANT,
172 };
173
174 // Encodes a varint in a custom format.
Encode(uint64_t value,span<std::byte> output,Format format)175 inline size_t Encode(uint64_t value, span<std::byte> output, Format format) {
176 return pw_varint_EncodeCustom(value,
177 output.data(),
178 output.size(),
179 static_cast<pw_varint_Format>(format));
180 }
181
182 // Decodes a varint from a custom format.
Decode(span<const std::byte> input,uint64_t * value,Format format)183 inline size_t Decode(span<const std::byte> input,
184 uint64_t* value,
185 Format format) {
186 return pw_varint_DecodeCustom(
187 input.data(), input.size(), value, static_cast<pw_varint_Format>(format));
188 }
189
190 // Returns a size of an integer when encoded as a varint.
EncodedSize(uint64_t integer)191 constexpr size_t EncodedSize(uint64_t integer) {
192 return integer == 0 ? 1 : (64 - __builtin_clzll(integer) + 6) / 7;
193 }
194
195 // Returns a size of an signed integer when ZigZag encoded as a varint.
ZigZagEncodedSize(int64_t integer)196 constexpr size_t ZigZagEncodedSize(int64_t integer) {
197 return EncodedSize(ZigZagEncode(integer));
198 }
199
200 // Returns the maximum integer value that can be encoded in a varint of the
201 // specified number of bytes.
202 //
203 // These values are also listed in the table below. Zigzag encoding cuts these
204 // in half, as positive and negative integers are alternated.
205 //
206 // Bytes Max value
207 // 1 127
208 // 2 16,383
209 // 3 2,097,151
210 // 4 268,435,455
211 // 5 34,359,738,367 -- needed for max uint32 value
212 // 6 4,398,046,511,103
213 // 7 562,949,953,421,311
214 // 8 72,057,594,037,927,935
215 // 9 9,223,372,036,854,775,807
216 // 10 uint64 max value
217 //
MaxValueInBytes(size_t bytes)218 constexpr uint64_t MaxValueInBytes(size_t bytes) {
219 return bytes >= kMaxVarint64SizeBytes ? std::numeric_limits<uint64_t>::max()
220 : (uint64_t(1) << (7 * bytes)) - 1;
221 }
222
223 } // namespace varint
224 } // namespace pw
225
226 #endif // __cplusplus
227