Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm/include/llvm/Support/Compression.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ Error decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
Error decompress(ArrayRef<uint8_t> Input, SmallVectorImpl<uint8_t> &Output,
size_t UncompressedSize);

// Get the size of the decompressed data.
Error getDecompressedSize(ArrayRef<uint8_t> Input, size_t &UncompressedSize);
} // End of namespace zstd

enum class Format {
Expand Down
18 changes: 18 additions & 0 deletions llvm/lib/Support/Compression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ Error zstd::decompress(ArrayRef<uint8_t> Input,
return E;
}

Error zstd::getDecompressedSize(ArrayRef<uint8_t> Input,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more conventional to return Expected<uint64_t> and avoid output parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in c9046d7

size_t &UncompressedSize) {

unsigned long long Res = ZSTD_getFrameContentSize(Input.data(), Input.size());

// ZSTD_getFrameContentSize returns unsigned long long, but the size
// of uncompressed data should be bounded by size_t.
UncompressedSize = static_cast<size_t>(Res);

return ZSTD_isError(Res) ? make_error<StringError>(ZSTD_getErrorName(Res),
inconvertibleErrorCode())
: Error::success();
}

#else
bool zstd::isAvailable() { return false; }
void zstd::compress(ArrayRef<uint8_t> Input,
Expand All @@ -240,4 +254,8 @@ Error zstd::decompress(ArrayRef<uint8_t> Input,
size_t UncompressedSize) {
llvm_unreachable("zstd::decompress is unavailable");
}
Error zstd::getDecompressedSize(ArrayRef<uint8_t> Input,
size_t &UncompressedSize) {
llvm_unreachable("zstd::getDecompressedSize is unavailable");
}
#endif
9 changes: 8 additions & 1 deletion llvm/unittests/Support/CompressionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ static void testZstdCompression(StringRef Input, int Level) {
SmallVector<uint8_t, 0> Uncompressed;
zstd::compress(arrayRefFromStringRef(Input), Compressed, Level);

// Check that getDecompressedSize returns the size of the original buffer.
size_t DecompressedSize;
Error E = zstd::getDecompressedSize(Compressed, DecompressedSize);
EXPECT_FALSE(std::move(E));

EXPECT_EQ(DecompressedSize, Input.size());

// Check that uncompressed buffer is the same as original.
Error E = zstd::decompress(Compressed, Uncompressed, Input.size());
E = zstd::decompress(Compressed, Uncompressed, Input.size());
EXPECT_FALSE(std::move(E));
EXPECT_EQ(Input, toStringRef(Uncompressed));

Expand Down