| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/metrics/net/compression_utils.h" | 5 #include "components/metrics/compression_utils.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/sys_byteorder.h" |
| 10 #include "third_party/zlib/zlib.h" | 12 #include "third_party/zlib/zlib.h" |
| 11 | 13 |
| 12 namespace { | 14 namespace { |
| 13 | 15 |
| 14 // The difference in bytes between a zlib header and a gzip header. | 16 // The difference in bytes between a zlib header and a gzip header. |
| 15 const size_t kGzipZlibHeaderDifferenceBytes = 16; | 17 const size_t kGzipZlibHeaderDifferenceBytes = 16; |
| 16 | 18 |
| 17 // Pass an integer greater than the following get a gzip header instead of a | 19 // Pass an integer greater than the following get a gzip header instead of a |
| 18 // zlib header when calling deflateInit2_. | 20 // zlib header when calling deflateInit2() and inflateInit2(). |
| 19 const int kWindowBitsToGetGzipHeader = 16; | 21 const int kWindowBitsToGetGzipHeader = 16; |
| 20 | 22 |
| 21 // This describes the amount of memory zlib uses to compress data. It can go | 23 // 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: | 24 // from 1 to 9, with 8 being the default. For details, see: |
| 23 // http://www.zlib.net/manual.html (search for memLevel). | 25 // http://www.zlib.net/manual.html (search for memLevel). |
| 24 const int kZlibMemoryLevel = 8; | 26 const int kZlibMemoryLevel = 8; |
| 25 | 27 |
| 26 // This code is taken almost verbatim from third_party/zlib/compress.c. The only | 28 // 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. | 29 // 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. | 30 // That causes a gzip header to be emitted rather than a zlib header. |
| 29 int GzipCompressHelper(Bytef* dest, | 31 int GzipCompressHelper(Bytef* dest, |
| 30 uLongf* dest_length, | 32 uLongf* dest_length, |
| 31 const Bytef* source, | 33 const Bytef* source, |
| 32 uLong source_length) { | 34 uLong source_length) { |
| 33 z_stream stream; | 35 z_stream stream; |
| 34 | 36 |
| 35 stream.next_in = bit_cast<Bytef*>(source); | 37 stream.next_in = bit_cast<Bytef*>(source); |
| 36 stream.avail_in = static_cast<uInt>(source_length); | 38 stream.avail_in = static_cast<uInt>(source_length); |
| 37 stream.next_out = dest; | 39 stream.next_out = dest; |
| 38 stream.avail_out = static_cast<uInt>(*dest_length); | 40 stream.avail_out = static_cast<uInt>(*dest_length); |
| 39 if (static_cast<uLong>(stream.avail_out) != *dest_length) | 41 if (static_cast<uLong>(stream.avail_out) != *dest_length) |
| 40 return Z_BUF_ERROR; | 42 return Z_BUF_ERROR; |
| 41 | 43 |
| 42 stream.zalloc = static_cast<alloc_func>(0); | 44 stream.zalloc = static_cast<alloc_func>(0); |
| 43 stream.zfree = static_cast<free_func>(0); | 45 stream.zfree = static_cast<free_func>(0); |
| 44 stream.opaque = static_cast<voidpf>(0); | 46 stream.opaque = static_cast<voidpf>(0); |
| 45 | 47 |
| 46 gz_header gzip_header; | 48 gz_header gzip_header; |
| 47 memset(&gzip_header, 0, sizeof(gzip_header)); | 49 memset(&gzip_header, 0, sizeof(gzip_header)); |
| 48 int err = deflateInit2_(&stream, | 50 int err = deflateInit2(&stream, |
| 49 Z_DEFAULT_COMPRESSION, | 51 Z_DEFAULT_COMPRESSION, |
| 50 Z_DEFLATED, | 52 Z_DEFLATED, |
| 51 MAX_WBITS + kWindowBitsToGetGzipHeader, | 53 MAX_WBITS + kWindowBitsToGetGzipHeader, |
| 52 kZlibMemoryLevel, | 54 kZlibMemoryLevel, |
| 53 Z_DEFAULT_STRATEGY, | 55 Z_DEFAULT_STRATEGY); |
| 54 ZLIB_VERSION, | 56 if (err != Z_OK) |
| 55 sizeof(z_stream)); | 57 return err; |
| 56 if (err != Z_OK) | |
| 57 return err; | |
| 58 | 58 |
| 59 err = deflateSetHeader(&stream, &gzip_header); | 59 err = deflateSetHeader(&stream, &gzip_header); |
| 60 if (err != Z_OK) | 60 if (err != Z_OK) |
| 61 return err; | 61 return err; |
| 62 | 62 |
| 63 err = deflate(&stream, Z_FINISH); | 63 err = deflate(&stream, Z_FINISH); |
| 64 if (err != Z_STREAM_END) { | 64 if (err != Z_STREAM_END) { |
| 65 deflateEnd(&stream); | 65 deflateEnd(&stream); |
| 66 return err == Z_OK ? Z_BUF_ERROR : err; | 66 return err == Z_OK ? Z_BUF_ERROR : err; |
| 67 } | 67 } |
| 68 *dest_length = stream.total_out; | 68 *dest_length = stream.total_out; |
| 69 | 69 |
| 70 err = deflateEnd(&stream); | 70 err = deflateEnd(&stream); |
| 71 return err; |
| 72 } |
| 73 |
| 74 // This code is taken almost verbatim from third_party/zlib/uncompr.c. The only |
| 75 // difference is inflateInit2() is called which sets the window bits to be > 16. |
| 76 // That causes a gzip header to be parsed rather than a zlib header. |
| 77 int GzipUncompressHelper(Bytef* dest, |
| 78 uLongf* dest_length, |
| 79 const Bytef* source, |
| 80 uLong source_length) { |
| 81 z_stream stream; |
| 82 |
| 83 stream.next_in = bit_cast<Bytef*>(source); |
| 84 stream.avail_in = static_cast<uInt>(source_length); |
| 85 if (static_cast<uLong>(stream.avail_in) != source_length) |
| 86 return Z_BUF_ERROR; |
| 87 |
| 88 stream.next_out = dest; |
| 89 stream.avail_out = static_cast<uInt>(*dest_length); |
| 90 if (static_cast<uLong>(stream.avail_out) != *dest_length) |
| 91 return Z_BUF_ERROR; |
| 92 |
| 93 stream.zalloc = static_cast<alloc_func>(0); |
| 94 stream.zfree = static_cast<free_func>(0); |
| 95 |
| 96 int err = inflateInit2(&stream, MAX_WBITS + kWindowBitsToGetGzipHeader); |
| 97 if (err != Z_OK) |
| 71 return err; | 98 return err; |
| 99 |
| 100 err = inflate(&stream, Z_FINISH); |
| 101 if (err != Z_STREAM_END) { |
| 102 inflateEnd(&stream); |
| 103 if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) |
| 104 return Z_DATA_ERROR; |
| 105 return err; |
| 106 } |
| 107 *dest_length = stream.total_out; |
| 108 |
| 109 err = inflateEnd(&stream); |
| 110 return err; |
| 111 } |
| 112 |
| 113 // Returns the uncompressed size from GZIP-compressed |compressed_data|. |
| 114 uint32 GetUncompressedSize(const std::string& compressed_data) { |
| 115 // The uncompressed size is stored in the last 4 bytes of |input| in LE. |
| 116 uint32 size; |
| 117 if (compressed_data.length() < sizeof(size)) |
| 118 return 0; |
| 119 memcpy(&size, &compressed_data[compressed_data.length() - sizeof(size)], |
| 120 sizeof(size)); |
| 121 return base::ByteSwapToLE32(size); |
| 72 } | 122 } |
| 73 | 123 |
| 74 } // namespace | 124 } // namespace |
| 75 | 125 |
| 76 namespace metrics { | 126 namespace metrics { |
| 77 | 127 |
| 78 bool GzipCompress(const std::string& input, std::string* output) { | 128 bool GzipCompress(const std::string& input, std::string* output) { |
| 79 const uLongf input_size = static_cast<uLongf>(input.size()); | 129 const uLongf input_size = static_cast<uLongf>(input.size()); |
| 80 std::vector<Bytef> compressed_data(kGzipZlibHeaderDifferenceBytes + | 130 std::vector<Bytef> compressed_data(kGzipZlibHeaderDifferenceBytes + |
| 81 compressBound(input_size)); | 131 compressBound(input_size)); |
| 82 | 132 |
| 83 uLongf compressed_size = static_cast<uLongf>(compressed_data.size()); | 133 uLongf compressed_size = static_cast<uLongf>(compressed_data.size()); |
| 84 if (GzipCompressHelper(&compressed_data.front(), | 134 if (GzipCompressHelper(&compressed_data.front(), |
| 85 &compressed_size, | 135 &compressed_size, |
| 86 bit_cast<const Bytef*>(input.data()), | 136 bit_cast<const Bytef*>(input.data()), |
| 87 input_size) != Z_OK) { | 137 input_size) != Z_OK) { |
| 88 return false; | 138 return false; |
| 89 } | 139 } |
| 90 | 140 |
| 91 compressed_data.resize(compressed_size); | 141 compressed_data.resize(compressed_size); |
| 92 output->assign(compressed_data.begin(), compressed_data.end()); | 142 output->assign(compressed_data.begin(), compressed_data.end()); |
| 143 DCHECK_EQ(input.size(), GetUncompressedSize(*output)); |
| 93 return true; | 144 return true; |
| 94 } | 145 } |
| 95 | 146 |
| 147 bool GzipUncompress(const std::string& input, std::string* output) { |
| 148 output->resize(GetUncompressedSize(input)); |
| 149 uLongf uncompressed_size = static_cast<uLongf>(output->length()); |
| 150 return GzipUncompressHelper(bit_cast<Bytef*>(output->data()), |
| 151 &uncompressed_size, |
| 152 bit_cast<const Bytef*>(input.data()), |
| 153 static_cast<uLongf>(input.length())) == Z_OK; |
| 154 } |
| 155 |
| 96 } // namespace metrics | 156 } // namespace metrics |
| OLD | NEW |