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 "third_party/zlib/google/compression_utils.h" | 5 #include "third_party/zlib/google/compression_utils.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) | 107 if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) |
108 return Z_DATA_ERROR; | 108 return Z_DATA_ERROR; |
109 return err; | 109 return err; |
110 } | 110 } |
111 *dest_length = stream.total_out; | 111 *dest_length = stream.total_out; |
112 | 112 |
113 err = inflateEnd(&stream); | 113 err = inflateEnd(&stream); |
114 return err; | 114 return err; |
115 } | 115 } |
116 | 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 | 117 } // namespace |
129 | 118 |
130 namespace compression { | 119 namespace compression { |
131 | 120 |
132 bool GzipCompress(const std::string& input, std::string* output) { | 121 bool GzipCompress(const std::string& input, std::string* output) { |
133 const uLongf input_size = static_cast<uLongf>(input.size()); | 122 const uLongf input_size = static_cast<uLongf>(input.size()); |
134 std::vector<Bytef> compressed_data(kGzipZlibHeaderDifferenceBytes + | 123 std::vector<Bytef> compressed_data(kGzipZlibHeaderDifferenceBytes + |
135 compressBound(input_size)); | 124 compressBound(input_size)); |
136 | 125 |
137 uLongf compressed_size = static_cast<uLongf>(compressed_data.size()); | 126 uLongf compressed_size = static_cast<uLongf>(compressed_data.size()); |
(...skipping 17 matching lines...) Expand all Loading... |
155 if (GzipUncompressHelper(bit_cast<Bytef*>(uncompressed_output.data()), | 144 if (GzipUncompressHelper(bit_cast<Bytef*>(uncompressed_output.data()), |
156 &uncompressed_size, | 145 &uncompressed_size, |
157 bit_cast<const Bytef*>(input.data()), | 146 bit_cast<const Bytef*>(input.data()), |
158 static_cast<uLongf>(input.length())) == Z_OK) { | 147 static_cast<uLongf>(input.length())) == Z_OK) { |
159 output->swap(uncompressed_output); | 148 output->swap(uncompressed_output); |
160 return true; | 149 return true; |
161 } | 150 } |
162 return false; | 151 return false; |
163 } | 152 } |
164 | 153 |
| 154 bool GzipUncompress(base::StringPiece input, base::StringPiece output) { |
| 155 uLongf uncompressed_size = GetUncompressedSize(input); |
| 156 if (uncompressed_size > output.size()) |
| 157 return false; |
| 158 return GzipUncompressHelper(bit_cast<Bytef*>(output.data()), |
| 159 &uncompressed_size, |
| 160 bit_cast<const Bytef*>(input.data()), |
| 161 static_cast<uLongf>(input.length())) == Z_OK; |
| 162 } |
| 163 |
| 164 uint32_t GetUncompressedSize(base::StringPiece compressed_data) { |
| 165 // The uncompressed size is stored in the last 4 bytes of |input| in LE. |
| 166 uint32_t size; |
| 167 if (compressed_data.length() < sizeof(size)) |
| 168 return 0; |
| 169 memcpy(&size, |
| 170 &compressed_data.data()[compressed_data.length() - sizeof(size)], |
| 171 sizeof(size)); |
| 172 return base::ByteSwapToLE32(size); |
| 173 } |
| 174 |
165 } // namespace compression | 175 } // namespace compression |
OLD | NEW |