Alexei Frolov | 8ecefe9 | 2020-01-13 10:40:08 -0800 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
Wyatt Hepler | 8c493bb | 2019-12-02 14:49:21 -0800 | [diff] [blame] | 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 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 |
Wyatt Hepler | 8c493bb | 2019-12-02 14:49:21 -0800 | [diff] [blame] | 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 14 | |
| 15 | #include "pw_varint/varint.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | |
Wyatt Hepler | 588907a | 2020-01-16 16:34:58 -0800 | [diff] [blame] | 19 | namespace pw { |
| 20 | namespace varint { |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 21 | |
Alexei Frolov | 8ecefe9 | 2020-01-13 10:40:08 -0800 | [diff] [blame] | 22 | extern "C" size_t pw_VarintEncode(uint64_t integer, |
| 23 | void* output, |
| 24 | size_t output_size) { |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 25 | size_t written = 0; |
Alexei Frolov | 8ecefe9 | 2020-01-13 10:40:08 -0800 | [diff] [blame] | 26 | std::byte* buffer = static_cast<std::byte*>(output); |
| 27 | |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 28 | do { |
Alexei Frolov | 8ecefe9 | 2020-01-13 10:40:08 -0800 | [diff] [blame] | 29 | if (written >= output_size) { |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | // Grab 7 bits; the eighth bit is set to 1 to indicate more data coming. |
Wyatt Hepler | 588907a | 2020-01-16 16:34:58 -0800 | [diff] [blame] | 34 | buffer[written++] = static_cast<std::byte>(integer) | std::byte(0x80); |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 35 | integer >>= 7; |
| 36 | } while (integer != 0u); |
| 37 | |
Wyatt Hepler | 588907a | 2020-01-16 16:34:58 -0800 | [diff] [blame] | 38 | buffer[written - 1] &= std::byte(0x7f); // clear the top bit of the last byte |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 39 | return written; |
| 40 | } |
| 41 | |
Alexei Frolov | 8ecefe9 | 2020-01-13 10:40:08 -0800 | [diff] [blame] | 42 | extern "C" size_t pw_VarintZigZagEncode(int64_t integer, |
| 43 | void* output, |
| 44 | size_t output_size) { |
| 45 | return pw_VarintEncode(ZigZagEncode(integer), output, output_size); |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Alexei Frolov | 8ecefe9 | 2020-01-13 10:40:08 -0800 | [diff] [blame] | 48 | extern "C" size_t pw_VarintDecode(const void* input, |
| 49 | size_t input_size, |
| 50 | uint64_t* output) { |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 51 | uint64_t decoded_value = 0; |
| 52 | uint_fast8_t count = 0; |
Alexei Frolov | 8ecefe9 | 2020-01-13 10:40:08 -0800 | [diff] [blame] | 53 | const std::byte* buffer = static_cast<const std::byte*>(input); |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 54 | |
| 55 | // The largest 64-bit ints require 10 B. |
Alexei Frolov | 8ecefe9 | 2020-01-13 10:40:08 -0800 | [diff] [blame] | 56 | const size_t max_count = std::min(kMaxVarintSizeBytes, input_size); |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 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. |
Wyatt Hepler | 588907a | 2020-01-16 16:34:58 -0800 | [diff] [blame] | 64 | decoded_value |= static_cast<uint64_t>(buffer[count] & std::byte(0x7f)) |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 65 | << (7 * count); |
| 66 | |
| 67 | // Stop decoding if the top bit is not set. |
Wyatt Hepler | 588907a | 2020-01-16 16:34:58 -0800 | [diff] [blame] | 68 | if ((buffer[count++] & std::byte(0x80)) == std::byte(0)) { |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
Alexei Frolov | 8ecefe9 | 2020-01-13 10:40:08 -0800 | [diff] [blame] | 73 | *output = decoded_value; |
Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 74 | return count; |
| 75 | } |
| 76 | |
Alexei Frolov | 8ecefe9 | 2020-01-13 10:40:08 -0800 | [diff] [blame] | 77 | extern "C" size_t pw_VarintZigZagDecode(const void* input, |
| 78 | size_t input_size, |
| 79 | int64_t* output) { |
| 80 | uint64_t value = 0; |
| 81 | size_t bytes = pw_VarintDecode(input, input_size, &value); |
| 82 | *output = ZigZagDecode(value); |
| 83 | return bytes; |
| 84 | } |
| 85 | |
Alexei Frolov | 388d4b9 | 2020-05-20 13:30:07 -0700 | [diff] [blame] | 86 | extern "C" size_t pw_VarintEncodedSize(uint64_t integer) { |
| 87 | return integer == 0 ? 1 : (64 - __builtin_clzll(integer) + 6) / 7; |
| 88 | } |
| 89 | |
| 90 | extern "C" size_t pw_VarintZigZagEncodedSize(int64_t integer) { |
| 91 | return pw_VarintEncodedSize(ZigZagEncode(integer)); |
| 92 | } |
| 93 | |
Wyatt Hepler | 588907a | 2020-01-16 16:34:58 -0800 | [diff] [blame] | 94 | } // namespace varint |
| 95 | } // namespace pw |