diff options
Diffstat (limited to 'pw_varint')
-rw-r--r-- | pw_varint/Android.bp | 1 | ||||
-rw-r--r-- | pw_varint/BUILD.bazel | 5 | ||||
-rw-r--r-- | pw_varint/README.md | 1 | ||||
-rw-r--r-- | pw_varint/public/pw_varint/stream.h | 11 | ||||
-rw-r--r-- | pw_varint/stream.cc | 8 | ||||
-rw-r--r-- | pw_varint/stream_test.cc | 7 | ||||
-rw-r--r-- | pw_varint/varint_test.cc | 2 |
7 files changed, 21 insertions, 14 deletions
diff --git a/pw_varint/Android.bp b/pw_varint/Android.bp index 3d65acd69..84c99c826 100644 --- a/pw_varint/Android.bp +++ b/pw_varint/Android.bp @@ -33,6 +33,7 @@ cc_library_static { "pw_preprocessor_headers", "pw_result_headers", "pw_span_headers", + "pw_toolchain", ], export_header_lib_headers: [ "fuchsia_sdk_lib_stdcompat", diff --git a/pw_varint/BUILD.bazel b/pw_varint/BUILD.bazel index 4688787d6..d1a811000 100644 --- a/pw_varint/BUILD.bazel +++ b/pw_varint/BUILD.bazel @@ -14,7 +14,6 @@ load( "//pw_build:pigweed.bzl", - "pw_cc_library", "pw_cc_test", ) @@ -22,7 +21,7 @@ package(default_visibility = ["//visibility:public"]) licenses(["notice"]) -pw_cc_library( +cc_library( name = "pw_varint", srcs = [ "varint.cc", @@ -40,7 +39,7 @@ pw_cc_library( ], ) -pw_cc_library( +cc_library( name = "stream", srcs = [ "stream.cc", diff --git a/pw_varint/README.md b/pw_varint/README.md deleted file mode 100644 index 07e989db6..000000000 --- a/pw_varint/README.md +++ /dev/null @@ -1 +0,0 @@ -# pw\_varint: Variable-length integer encoding diff --git a/pw_varint/public/pw_varint/stream.h b/pw_varint/public/pw_varint/stream.h index effac3df5..1d86f597e 100644 --- a/pw_varint/public/pw_varint/stream.h +++ b/pw_varint/public/pw_varint/stream.h @@ -26,6 +26,8 @@ namespace varint { /// of a `pw::stream`. Reads a maximum of 10 bytes or `max_size`, whichever is /// smaller. /// +/// This method always returns the number of bytes read, even on error. +/// /// @param reader The `pw::stream` to read from. /// /// @param output The integer to read into. If reading into a signed integer, @@ -35,10 +37,11 @@ namespace varint { /// @param max_size The maximum number of bytes to read. The upper limit is 10 /// bytes. /// -/// @returns The number of bytes read from the stream if successful. The value -/// is placed in `output`. Returns `OutOfRange` if the varint does not fit into -/// `output`. Also returns `OutOfRange` if the input is exhausted before the -/// number terminates. +/// @retval OK The decoded value is placed in `output`. +/// @retval OUT_OF_RANGE No input is available, e.g. the stream is closed. +/// @retval DATA_LOSS The decoded value is too large for `output` or is +/// incomplete, e.g. the input was exhausted after a +/// partial varint was read. StatusWithSize Read(stream::Reader& reader, int64_t* output, size_t max_size = std::numeric_limits<size_t>::max()); diff --git a/pw_varint/stream.cc b/pw_varint/stream.cc index 83a80de08..fec3a4227 100644 --- a/pw_varint/stream.cc +++ b/pw_varint/stream.cc @@ -44,14 +44,14 @@ StatusWithSize Read(stream::Reader& reader, uint64_t* output, size_t max_size) { if (count >= varint::kMaxVarint64SizeBytes) { // Varint can't fit a uint64_t, this likely means we're reading binary // data that is not actually a varint. - return StatusWithSize::DataLoss(); + return StatusWithSize::DataLoss(count); } if (count >= max_size) { // Varint didn't fit within the range given; return OutOfRange() if // max_size was 0, but DataLoss if we were reading something we thought // was going to be a varint. - return count > 0 ? StatusWithSize::DataLoss() + return count > 0 ? StatusWithSize::DataLoss(count) : StatusWithSize::OutOfRange(); } @@ -62,9 +62,9 @@ StatusWithSize Read(stream::Reader& reader, uint64_t* output, size_t max_size) { // when we reached the end of file. But after the first byte it means we // failed to decode a varint we were in the middle of, and that's not // a normal error condition. - return StatusWithSize(Status::DataLoss(), 0); + return StatusWithSize::DataLoss(count); } - return StatusWithSize(result.status(), 0); + return StatusWithSize(result.status(), count); } value |= static_cast<uint64_t>(b & std::byte(0b01111111)) << (7 * count); diff --git a/pw_varint/stream_test.cc b/pw_varint/stream_test.cc index b2b1214c7..af0ea8471 100644 --- a/pw_varint/stream_test.cc +++ b/pw_varint/stream_test.cc @@ -20,8 +20,8 @@ #include <cstring> #include <limits> -#include "gtest/gtest.h" #include "pw_stream/memory_stream.h" +#include "pw_unit_test/framework.h" #include "pw_varint/varint.h" namespace pw::varint { @@ -251,6 +251,7 @@ TEST(VarintRead, Errors) { const auto sws = Read(reader, &value); EXPECT_FALSE(sws.ok()); EXPECT_EQ(sws.status(), Status::OutOfRange()); + EXPECT_EQ(sws.size(), buffer.size()); } { @@ -259,6 +260,7 @@ TEST(VarintRead, Errors) { const auto sws = Read(reader, &value); EXPECT_FALSE(sws.ok()); EXPECT_EQ(sws.status(), Status::DataLoss()); + EXPECT_EQ(sws.size(), buffer.size()); } { @@ -271,6 +273,7 @@ TEST(VarintRead, Errors) { const auto sws = Read(reader, &value); EXPECT_FALSE(sws.ok()); EXPECT_EQ(sws.status(), Status::DataLoss()); + EXPECT_EQ(sws.size(), varint::kMaxVarint64SizeBytes); } } @@ -285,6 +288,7 @@ TEST(VarintRead, SizeLimit) { const auto sws = Read(reader, &value, 4); EXPECT_FALSE(sws.ok()); EXPECT_EQ(sws.status(), Status::DataLoss()); + EXPECT_EQ(sws.size(), 4u); EXPECT_EQ(reader.Tell(), 4u); } @@ -296,6 +300,7 @@ TEST(VarintRead, SizeLimit) { const auto sws = Read(reader, &value, 0); EXPECT_FALSE(sws.ok()); EXPECT_EQ(sws.status(), Status::OutOfRange()); + EXPECT_EQ(sws.size(), 0u); EXPECT_EQ(reader.Tell(), 0u); } } diff --git a/pw_varint/varint_test.cc b/pw_varint/varint_test.cc index 02bfd0042..b3f34000c 100644 --- a/pw_varint/varint_test.cc +++ b/pw_varint/varint_test.cc @@ -19,8 +19,8 @@ #include <cstring> #include <limits> -#include "gtest/gtest.h" #include "pw_fuzzer/fuzztest.h" +#include "pw_unit_test/framework.h" namespace pw::varint { namespace { |