Implement Xz compressor functions.
The new XzCompress() function is similar to BzipCompress() function but
uses the Xz compression algorithm.
This patch simplifies the unittests of the compressors and reuses the
client-side decompresor implementation instead of repeating the
implementation in the delta generator. This patch removes the unused
compression/decompression functions.
Bug: 24578399
TEST=Added unittests.
Change-Id: Id858112b50f4aa2597f184dc23a86af772f4f190
diff --git a/payload_generator/bzip.cc b/payload_generator/bzip.cc
index 9040193..c2388ca 100644
--- a/payload_generator/bzip.cc
+++ b/payload_generator/bzip.cc
@@ -24,63 +24,32 @@
#include "update_engine/common/utils.h"
-using std::string;
-
namespace chromeos_update_engine {
-namespace {
-
-// BzipData compresses or decompresses the input to the output.
-// Returns true on success.
-// Use one of BzipBuffToBuff*ompress as the template parameter to BzipData().
-int BzipBuffToBuffDecompress(uint8_t* out,
- uint32_t* out_length,
- const void* in,
- uint32_t in_length) {
- return BZ2_bzBuffToBuffDecompress(
- reinterpret_cast<char*>(out),
- out_length,
- reinterpret_cast<char*>(const_cast<void*>(in)),
- in_length,
- 0, // Silent verbosity
- 0); // Normal algorithm
-}
-
-int BzipBuffToBuffCompress(uint8_t* out,
- uint32_t* out_length,
- const void* in,
- uint32_t in_length) {
- return BZ2_bzBuffToBuffCompress(
- reinterpret_cast<char*>(out),
- out_length,
- reinterpret_cast<char*>(const_cast<void*>(in)),
- in_length,
- 9, // Best compression
- 0, // Silent verbosity
- 0); // Default work factor
-}
-
-template<int F(uint8_t* out,
- uint32_t* out_length,
- const void* in,
- uint32_t in_length)>
-bool BzipData(const void* const in,
- const size_t in_size,
- brillo::Blob* const out) {
+bool BzipCompress(const brillo::Blob& in, brillo::Blob* out) {
TEST_AND_RETURN_FALSE(out);
out->clear();
- if (in_size == 0) {
+ if (in.size() == 0)
return true;
- }
- // Try increasing buffer size until it works
- size_t buf_size = in_size;
+
+ // We expect a compression ratio of about 35% with bzip2, so we start with
+ // that much output space, which will then be doubled if needed.
+ size_t buf_size = 40 + in.size() * 35 / 100;
out->resize(buf_size);
+ // Try increasing buffer size until it works
for (;;) {
if (buf_size > std::numeric_limits<uint32_t>::max())
return false;
uint32_t data_size = buf_size;
- int rc = F(out->data(), &data_size, in, in_size);
+ int rc = BZ2_bzBuffToBuffCompress(
+ reinterpret_cast<char*>(out->data()),
+ &data_size,
+ reinterpret_cast<char*>(const_cast<uint8_t*>(in.data())),
+ in.size(),
+ 9, // Best compression
+ 0, // Silent verbosity
+ 0); // Default work factor
TEST_AND_RETURN_FALSE(rc == BZ_OUTBUFF_FULL || rc == BZ_OK);
if (rc == BZ_OK) {
// we're done!
@@ -94,37 +63,4 @@
}
}
-} // namespace
-
-bool BzipDecompress(const brillo::Blob& in, brillo::Blob* out) {
- return BzipData<BzipBuffToBuffDecompress>(in.data(), in.size(), out);
-}
-
-bool BzipCompress(const brillo::Blob& in, brillo::Blob* out) {
- return BzipData<BzipBuffToBuffCompress>(in.data(), in.size(), out);
-}
-
-namespace {
-template<bool F(const void* const in,
- const size_t in_size,
- brillo::Blob* const out)>
-bool BzipString(const string& str,
- brillo::Blob* out) {
- TEST_AND_RETURN_FALSE(out);
- brillo::Blob temp;
- TEST_AND_RETURN_FALSE(F(str.data(), str.size(), &temp));
- out->clear();
- out->insert(out->end(), temp.begin(), temp.end());
- return true;
-}
-} // namespace
-
-bool BzipCompressString(const string& str, brillo::Blob* out) {
- return BzipString<BzipData<BzipBuffToBuffCompress>>(str, out);
-}
-
-bool BzipDecompressString(const string& str, brillo::Blob* out) {
- return BzipString<BzipData<BzipBuffToBuffDecompress>>(str, out);
-}
-
} // namespace chromeos_update_engine