2020-01-13 18:40:08 +00:00
|
|
|
// Copyright 2020 The Pigweed Authors
|
2019-11-27 22:38:39 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
2019-12-02 22:49:21 +00:00
|
|
|
// use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
// the License at
|
2019-11-27 22:38:39 +00:00
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2019-12-02 22:49:21 +00:00
|
|
|
// License for the specific language governing permissions and limitations under
|
|
|
|
// the License.
|
2019-11-27 22:38:39 +00:00
|
|
|
|
|
|
|
#include "pw_varint/varint.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace pw::varint {
|
|
|
|
|
2020-01-13 18:40:08 +00:00
|
|
|
extern "C" size_t pw_VarintEncode(uint64_t integer,
|
|
|
|
void* output,
|
|
|
|
size_t output_size) {
|
2019-11-27 22:38:39 +00:00
|
|
|
size_t written = 0;
|
2020-01-13 18:40:08 +00:00
|
|
|
std::byte* buffer = static_cast<std::byte*>(output);
|
|
|
|
|
2019-11-27 22:38:39 +00:00
|
|
|
do {
|
2020-01-13 18:40:08 +00:00
|
|
|
if (written >= output_size) {
|
2019-11-27 22:38:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab 7 bits; the eighth bit is set to 1 to indicate more data coming.
|
2020-01-13 18:40:08 +00:00
|
|
|
buffer[written++] = static_cast<std::byte>(integer) | std::byte{0x80};
|
2019-11-27 22:38:39 +00:00
|
|
|
integer >>= 7;
|
|
|
|
} while (integer != 0u);
|
|
|
|
|
2020-01-13 18:40:08 +00:00
|
|
|
buffer[written - 1] &= std::byte{0x7f}; // clear the top bit of the last byte
|
2019-11-27 22:38:39 +00:00
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2020-01-13 18:40:08 +00:00
|
|
|
extern "C" size_t pw_VarintZigZagEncode(int64_t integer,
|
|
|
|
void* output,
|
|
|
|
size_t output_size) {
|
|
|
|
return pw_VarintEncode(ZigZagEncode(integer), output, output_size);
|
2019-11-27 22:38:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 18:40:08 +00:00
|
|
|
extern "C" size_t pw_VarintDecode(const void* input,
|
|
|
|
size_t input_size,
|
|
|
|
uint64_t* output) {
|
2019-11-27 22:38:39 +00:00
|
|
|
uint64_t decoded_value = 0;
|
|
|
|
uint_fast8_t count = 0;
|
2020-01-13 18:40:08 +00:00
|
|
|
const std::byte* buffer = static_cast<const std::byte*>(input);
|
2019-11-27 22:38:39 +00:00
|
|
|
|
|
|
|
// The largest 64-bit ints require 10 B.
|
2020-01-13 18:40:08 +00:00
|
|
|
const size_t max_count = std::min(kMaxVarintSizeBytes, input_size);
|
2019-11-27 22:38:39 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (count >= max_count) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the bottom seven bits of the next byte to the result.
|
2020-01-13 18:40:08 +00:00
|
|
|
decoded_value |= static_cast<uint64_t>(buffer[count] & std::byte{0x7f})
|
2019-11-27 22:38:39 +00:00
|
|
|
<< (7 * count);
|
|
|
|
|
|
|
|
// Stop decoding if the top bit is not set.
|
2020-01-13 18:40:08 +00:00
|
|
|
if ((buffer[count++] & std::byte{0x80}) == std::byte{0}) {
|
2019-11-27 22:38:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 18:40:08 +00:00
|
|
|
*output = decoded_value;
|
2019-11-27 22:38:39 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2020-01-13 18:40:08 +00:00
|
|
|
extern "C" size_t pw_VarintZigZagDecode(const void* input,
|
|
|
|
size_t input_size,
|
|
|
|
int64_t* output) {
|
|
|
|
uint64_t value = 0;
|
|
|
|
size_t bytes = pw_VarintDecode(input, input_size, &value);
|
|
|
|
*output = ZigZagDecode(value);
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2019-11-27 22:38:39 +00:00
|
|
|
} // namespace pw::varint
|