This source file includes following definitions.
- GzipCompressHelper
- GzipCompress
#include "chrome/browser/metrics/compression_utils.h"
#include <vector>
#include "base/basictypes.h"
#include "third_party/zlib/zlib.h"
namespace {
const size_t kGzipZlibHeaderDifferenceBytes = 16;
const int kWindowBitsToGetGzipHeader = 16;
const int kZlibMemoryLevel = 8;
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,
ZLIB_VERSION,
sizeof(z_stream));
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;
}
}
namespace chrome {
bool GzipCompress(const std::string& input, std::string* output) {
std::vector<Bytef> compressed_data(kGzipZlibHeaderDifferenceBytes +
compressBound(input.size()));
uLongf compressed_size = 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());
return true;
}
}