Skip to content
Snippets Groups Projects
Commit 8ce28544 authored by hajimehoshi's avatar hajimehoshi Committed by Commit bot
Browse files

Move components/compression to third_party/zlib/google

We are now implementing CompressibleString in third_party/WebKit/Source/
wtf/text and want to use compression algorithm, but wtf/text can't
depend on components. This CL moves components/compression to
third_party and enables wtf/text to use the compression utility.

The purpose of this CL is similar to crrev.com/14021015.

BUG=574317
TEST=n/a

Review URL: https://codereview.chromium.org/1564773002

Cr-Original-Commit-Position: refs/heads/master@{#369491}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 1e1cc1a7060ab8eb95a62da608f333e31aaba475
parent 97bdded9
No related branches found
No related tags found
No related merge requests found
......@@ -149,3 +149,13 @@ static_library("zip") {
"//base",
]
}
static_library("compression_utils") {
sources = [
"google/compression_utils.cc",
"google/compression_utils.h",
]
deps = [
":zlib",
]
}
hshi@chromium.org
satorux@chromium.org
# compression_utils*
asvitkine@chromium.org
isherman@chromium.org
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/zlib/google/compression_utils.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <vector>
#include "base/bit_cast.h"
#include "base/logging.h"
#include "base/sys_byteorder.h"
#include "third_party/zlib/zlib.h"
namespace {
// The difference in bytes between a zlib header and a gzip header.
const size_t kGzipZlibHeaderDifferenceBytes = 16;
// Pass an integer greater than the following get a gzip header instead of a
// zlib header when calling deflateInit2() and inflateInit2().
const int kWindowBitsToGetGzipHeader = 16;
// This describes the amount of memory zlib uses to compress data. It can go
// from 1 to 9, with 8 being the default. For details, see:
// http://www.zlib.net/manual.html (search for memLevel).
const int kZlibMemoryLevel = 8;
// This code is taken almost verbatim from third_party/zlib/compress.c. The only
// difference is deflateInit2() is called which sets the window bits to be > 16.
// That causes a gzip header to be emitted rather than a zlib header.
int GzipCompressHelper(Bytef* dest,
uLongf* dest_length,
const Bytef* source,
uLong source_length) {
z_stream stream;
stream.next_in = bit_cast<Bytef*>(source);
stream.avail_in = static_cast<uInt>(source_length);
stream.next_out = dest;
stream.avail_out = static_cast<uInt>(*dest_length);
if (static_cast<uLong>(stream.avail_out) != *dest_length)
return Z_BUF_ERROR;
stream.zalloc = static_cast<alloc_func>(0);
stream.zfree = static_cast<free_func>(0);
stream.opaque = static_cast<voidpf>(0);
gz_header gzip_header;
memset(&gzip_header, 0, sizeof(gzip_header));
int err = deflateInit2(&stream,
Z_DEFAULT_COMPRESSION,
Z_DEFLATED,
MAX_WBITS + kWindowBitsToGetGzipHeader,
kZlibMemoryLevel,
Z_DEFAULT_STRATEGY);
if (err != Z_OK)
return err;
err = deflateSetHeader(&stream, &gzip_header);
if (err != Z_OK)
return err;
err = deflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
deflateEnd(&stream);
return err == Z_OK ? Z_BUF_ERROR : err;
}
*dest_length = stream.total_out;
err = deflateEnd(&stream);
return err;
}
// This code is taken almost verbatim from third_party/zlib/uncompr.c. The only
// difference is inflateInit2() is called which sets the window bits to be > 16.
// That causes a gzip header to be parsed rather than a zlib header.
int GzipUncompressHelper(Bytef* dest,
uLongf* dest_length,
const Bytef* source,
uLong source_length) {
z_stream stream;
stream.next_in = bit_cast<Bytef*>(source);
stream.avail_in = static_cast<uInt>(source_length);
if (static_cast<uLong>(stream.avail_in) != source_length)
return Z_BUF_ERROR;
stream.next_out = dest;
stream.avail_out = static_cast<uInt>(*dest_length);
if (static_cast<uLong>(stream.avail_out) != *dest_length)
return Z_BUF_ERROR;
stream.zalloc = static_cast<alloc_func>(0);
stream.zfree = static_cast<free_func>(0);
int err = inflateInit2(&stream, MAX_WBITS + kWindowBitsToGetGzipHeader);
if (err != Z_OK)
return err;
err = inflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
inflateEnd(&stream);
if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
return Z_DATA_ERROR;
return err;
}
*dest_length = stream.total_out;
err = inflateEnd(&stream);
return err;
}
// Returns the uncompressed size from GZIP-compressed |compressed_data|.
uint32_t GetUncompressedSize(const std::string& compressed_data) {
// The uncompressed size is stored in the last 4 bytes of |input| in LE.
uint32_t size;
if (compressed_data.length() < sizeof(size))
return 0;
memcpy(&size, &compressed_data[compressed_data.length() - sizeof(size)],
sizeof(size));
return base::ByteSwapToLE32(size);
}
} // namespace
namespace compression {
bool GzipCompress(const std::string& input, std::string* output) {
const uLongf input_size = static_cast<uLongf>(input.size());
std::vector<Bytef> compressed_data(kGzipZlibHeaderDifferenceBytes +
compressBound(input_size));
uLongf compressed_size = static_cast<uLongf>(compressed_data.size());
if (GzipCompressHelper(&compressed_data.front(),
&compressed_size,
bit_cast<const Bytef*>(input.data()),
input_size) != Z_OK) {
return false;
}
compressed_data.resize(compressed_size);
output->assign(compressed_data.begin(), compressed_data.end());
DCHECK_EQ(input_size, GetUncompressedSize(*output));
return true;
}
bool GzipUncompress(const std::string& input, std::string* output) {
std::string uncompressed_output;
uLongf uncompressed_size = static_cast<uLongf>(GetUncompressedSize(input));
uncompressed_output.resize(uncompressed_size);
if (GzipUncompressHelper(bit_cast<Bytef*>(uncompressed_output.data()),
&uncompressed_size,
bit_cast<const Bytef*>(input.data()),
static_cast<uLongf>(input.length())) == Z_OK) {
output->swap(uncompressed_output);
return true;
}
return false;
}
} // namespace compression
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_ZLIB_GOOGLE_COMPRESSION_UTILS_H_
#define THIRD_PARTY_ZLIB_GOOGLE_COMPRESSION_UTILS_H_
#include <string>
namespace compression {
// Compresses the data in |input| using gzip, storing the result in |output|.
// |input| and |output| are allowed to be the same string (in-place operation).
bool GzipCompress(const std::string& input, std::string* output);
// Uncompresses the data in |input| using gzip, storing the result in |output|.
// |input| and |output| are allowed to be the same string (in-place operation).
bool GzipUncompress(const std::string& input, std::string* output);
} // namespace compression
#endif // THIRD_PARTY_ZLIB_GOOGLE_COMPRESSION_UTILS_H_
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/zlib/google/compression_utils.h"
#include <stddef.h>
#include <stdint.h>
#include <string>
#include "base/logging.h"
#include "base/macros.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace compression {
namespace {
// The data to be compressed by gzip. This is the hex representation of "hello
// world".
const uint8_t kData[] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
0x77, 0x6f, 0x72, 0x6c, 0x64};
// This is the string representation of gzip compressed string above. It was
// obtained by running echo -n "hello world" | gzip -c | hexdump -e '8 1 ",
// 0x%x"' followed by 0'ing out the OS byte (10th byte) in the header. This is
// so that the test passes on all platforms (that run various OS'es).
const uint8_t kCompressedData[] = {
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcb,
0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0x01,
0x00, 0x85, 0x11, 0x4a, 0x0d, 0x0b, 0x00, 0x00, 0x00};
} // namespace
TEST(CompressionUtilsTest, GzipCompression) {
std::string data(reinterpret_cast<const char*>(kData), arraysize(kData));
std::string compressed_data;
EXPECT_TRUE(GzipCompress(data, &compressed_data));
std::string golden_compressed_data(
reinterpret_cast<const char*>(kCompressedData),
arraysize(kCompressedData));
EXPECT_EQ(golden_compressed_data, compressed_data);
}
TEST(CompressionUtilsTest, GzipUncompression) {
std::string compressed_data(reinterpret_cast<const char*>(kCompressedData),
arraysize(kCompressedData));
std::string uncompressed_data;
EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data));
std::string golden_data(reinterpret_cast<const char*>(kData),
arraysize(kData));
EXPECT_EQ(golden_data, uncompressed_data);
}
// Checks that compressing/decompressing input > 256 bytes works as expected.
TEST(CompressionUtilsTest, LargeInput) {
const size_t kSize = 32 * 1024;
// Generate a data string of |kSize| for testing.
std::string data;
data.resize(kSize);
for (size_t i = 0; i < kSize; ++i)
data[i] = static_cast<char>(i & 0xFF);
std::string compressed_data;
EXPECT_TRUE(GzipCompress(data, &compressed_data));
std::string uncompressed_data;
EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data));
EXPECT_EQ(data, uncompressed_data);
}
TEST(CompressionUtilsTest, InPlace) {
const std::string original_data(reinterpret_cast<const char*>(kData),
arraysize(kData));
const std::string golden_compressed_data(
reinterpret_cast<const char*>(kCompressedData),
arraysize(kCompressedData));
std::string data(original_data);
EXPECT_TRUE(GzipCompress(data, &data));
EXPECT_EQ(golden_compressed_data, data);
EXPECT_TRUE(GzipUncompress(data, &data));
EXPECT_EQ(original_data, data);
}
} // namespace compression
......@@ -23,5 +23,19 @@
'zip_reader.h',
],
},
{
'target_name': 'compression_utils',
'type': 'static_library',
'dependencies': [
'../zlib.gyp:zlib',
],
'include_dirs': [
'../../..',
],
'sources': [
'compression_utils.cc',
'compression_utils.h',
],
},
],
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment