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