Alexei Frolov | 82d3cb3 | 2019-11-27 14:38:39 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Pigweed Authors |
| 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 | |
| 19 | namespace pw::varint { |
| 20 | namespace { |
| 21 | |
| 22 | constexpr int64_t ZigZagDecode64(uint64_t n) { |
| 23 | return static_cast<int64_t>((n >> 1) ^ (~(n & 1) + 1)); |
| 24 | } |
| 25 | |
| 26 | } // namespace |
| 27 | |
| 28 | size_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 | |
| 45 | size_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 | |
| 51 | size_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 |