| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/metrics/net/compression_utils.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "third_party/zlib/zlib.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 // The difference in bytes between a zlib header and a gzip header. | |
| 15 const size_t kGzipZlibHeaderDifferenceBytes = 16; | |
| 16 | |
| 17 // Pass an integer greater than the following get a gzip header instead of a | |
| 18 // zlib header when calling deflateInit2_. | |
| 19 const int kWindowBitsToGetGzipHeader = 16; | |
| 20 | |
| 21 // This describes the amount of memory zlib uses to compress data. It can go | |
| 22 // from 1 to 9, with 8 being the default. For details, see: | |
| 23 // http://www.zlib.net/manual.html (search for memLevel). | |
| 24 const int kZlibMemoryLevel = 8; | |
| 25 | |
| 26 // This code is taken almost verbatim from third_party/zlib/compress.c. The only | |
| 27 // difference is deflateInit2_ is called which sets the window bits to be > 16. | |
| 28 // That causes a gzip header to be emitted rather than a zlib header. | |
| 29 int GzipCompressHelper(Bytef* dest, | |
| 30 uLongf* dest_length, | |
| 31 const Bytef* source, | |
| 32 uLong source_length) { | |
| 33 z_stream stream; | |
| 34 | |
| 35 stream.next_in = bit_cast<Bytef*>(source); | |
| 36 stream.avail_in = static_cast<uInt>(source_length); | |
| 37 stream.next_out = dest; | |
| 38 stream.avail_out = static_cast<uInt>(*dest_length); | |
| 39 if (static_cast<uLong>(stream.avail_out) != *dest_length) | |
| 40 return Z_BUF_ERROR; | |
| 41 | |
| 42 stream.zalloc = static_cast<alloc_func>(0); | |
| 43 stream.zfree = static_cast<free_func>(0); | |
| 44 stream.opaque = static_cast<voidpf>(0); | |
| 45 | |
| 46 gz_header gzip_header; | |
| 47 memset(&gzip_header, 0, sizeof(gzip_header)); | |
| 48 int err = deflateInit2_(&stream, | |
| 49 Z_DEFAULT_COMPRESSION, | |
| 50 Z_DEFLATED, | |
| 51 MAX_WBITS + kWindowBitsToGetGzipHeader, | |
| 52 kZlibMemoryLevel, | |
| 53 Z_DEFAULT_STRATEGY, | |
| 54 ZLIB_VERSION, | |
| 55 sizeof(z_stream)); | |
| 56 if (err != Z_OK) | |
| 57 return err; | |
| 58 | |
| 59 err = deflateSetHeader(&stream, &gzip_header); | |
| 60 if (err != Z_OK) | |
| 61 return err; | |
| 62 | |
| 63 err = deflate(&stream, Z_FINISH); | |
| 64 if (err != Z_STREAM_END) { | |
| 65 deflateEnd(&stream); | |
| 66 return err == Z_OK ? Z_BUF_ERROR : err; | |
| 67 } | |
| 68 *dest_length = stream.total_out; | |
| 69 | |
| 70 err = deflateEnd(&stream); | |
| 71 return err; | |
| 72 } | |
| 73 | |
| 74 } // namespace | |
| 75 | |
| 76 namespace metrics { | |
| 77 | |
| 78 bool GzipCompress(const std::string& input, std::string* output) { | |
| 79 const uLongf input_size = static_cast<uLongf>(input.size()); | |
| 80 std::vector<Bytef> compressed_data(kGzipZlibHeaderDifferenceBytes + | |
| 81 compressBound(input_size)); | |
| 82 | |
| 83 uLongf compressed_size = static_cast<uLongf>(compressed_data.size()); | |
| 84 if (GzipCompressHelper(&compressed_data.front(), | |
| 85 &compressed_size, | |
| 86 bit_cast<const Bytef*>(input.data()), | |
| 87 input_size) != Z_OK) { | |
| 88 return false; | |
| 89 } | |
| 90 | |
| 91 compressed_data.resize(compressed_size); | |
| 92 output->assign(compressed_data.begin(), compressed_data.end()); | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 } // namespace metrics | |
| OLD | NEW |