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