| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "net/base/gzip_filter.h" | 5 #include "net/base/gzip_filter.h" |
| 6 | 6 |
| 7 #if defined(USE_SYSTEM_ZLIB) | 7 #if defined(USE_SYSTEM_ZLIB) |
| 8 #include <zlib.h> | 8 #include <zlib.h> |
| 9 // The code below uses the MOZ_Z_ forms of these functions in order that things | |
| 10 // should work on Windows. In order to make this code cross platform, we map | |
| 11 // back to the normal functions here in the case that we are using the system | |
| 12 // zlib. | |
| 13 #define MOZ_Z_inflate inflate | |
| 14 #define MOZ_Z_inflateEnd inflateEnd | |
| 15 #define MOZ_Z_inflateInit2_ inflateInit2_ | |
| 16 #define MOZ_Z_inflateInit_ inflateInit_ | |
| 17 #define MOZ_Z_inflateReset inflateReset | |
| 18 #else | 9 #else |
| 19 #include "third_party/zlib/zlib.h" | 10 #include "third_party/zlib/zlib.h" |
| 20 #endif | 11 #endif |
| 21 | 12 |
| 22 #include "base/logging.h" | 13 #include "base/logging.h" |
| 23 #include "net/base/gzip_header.h" | 14 #include "net/base/gzip_header.h" |
| 24 | 15 |
| 25 namespace net { | 16 namespace net { |
| 26 | 17 |
| 27 GZipFilter::GZipFilter(const FilterContext& filter_context) | 18 GZipFilter::GZipFilter(const FilterContext& filter_context) |
| 28 : Filter(filter_context), | 19 : Filter(filter_context), |
| 29 decoding_status_(DECODING_UNINITIALIZED), | 20 decoding_status_(DECODING_UNINITIALIZED), |
| 30 decoding_mode_(DECODE_MODE_UNKNOWN), | 21 decoding_mode_(DECODE_MODE_UNKNOWN), |
| 31 gzip_header_status_(GZIP_CHECK_HEADER_IN_PROGRESS), | 22 gzip_header_status_(GZIP_CHECK_HEADER_IN_PROGRESS), |
| 32 zlib_header_added_(false), | 23 zlib_header_added_(false), |
| 33 gzip_footer_bytes_(0), | 24 gzip_footer_bytes_(0), |
| 34 possible_sdch_pass_through_(false) { | 25 possible_sdch_pass_through_(false) { |
| 35 } | 26 } |
| 36 | 27 |
| 37 GZipFilter::~GZipFilter() { | 28 GZipFilter::~GZipFilter() { |
| 38 if (decoding_status_ != DECODING_UNINITIALIZED) { | 29 if (decoding_status_ != DECODING_UNINITIALIZED) { |
| 39 MOZ_Z_inflateEnd(zlib_stream_.get()); | 30 inflateEnd(zlib_stream_.get()); |
| 40 } | 31 } |
| 41 } | 32 } |
| 42 | 33 |
| 43 bool GZipFilter::InitDecoding(Filter::FilterType filter_type) { | 34 bool GZipFilter::InitDecoding(Filter::FilterType filter_type) { |
| 44 if (decoding_status_ != DECODING_UNINITIALIZED) | 35 if (decoding_status_ != DECODING_UNINITIALIZED) |
| 45 return false; | 36 return false; |
| 46 | 37 |
| 47 // Initialize zlib control block | 38 // Initialize zlib control block |
| 48 zlib_stream_.reset(new z_stream); | 39 zlib_stream_.reset(new z_stream); |
| 49 if (!zlib_stream_.get()) | 40 if (!zlib_stream_.get()) |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 *dest_len = 0; | 208 *dest_len = 0; |
| 218 return Filter::FILTER_NEED_MORE_DATA; | 209 return Filter::FILTER_NEED_MORE_DATA; |
| 219 } | 210 } |
| 220 | 211 |
| 221 // Fill in zlib control block | 212 // Fill in zlib control block |
| 222 zlib_stream_.get()->next_in = bit_cast<Bytef*>(next_stream_data_); | 213 zlib_stream_.get()->next_in = bit_cast<Bytef*>(next_stream_data_); |
| 223 zlib_stream_.get()->avail_in = stream_data_len_; | 214 zlib_stream_.get()->avail_in = stream_data_len_; |
| 224 zlib_stream_.get()->next_out = bit_cast<Bytef*>(dest_buffer); | 215 zlib_stream_.get()->next_out = bit_cast<Bytef*>(dest_buffer); |
| 225 zlib_stream_.get()->avail_out = *dest_len; | 216 zlib_stream_.get()->avail_out = *dest_len; |
| 226 | 217 |
| 227 int inflate_code = MOZ_Z_inflate(zlib_stream_.get(), Z_NO_FLUSH); | 218 int inflate_code = inflate(zlib_stream_.get(), Z_NO_FLUSH); |
| 228 int bytesWritten = *dest_len - zlib_stream_.get()->avail_out; | 219 int bytesWritten = *dest_len - zlib_stream_.get()->avail_out; |
| 229 | 220 |
| 230 Filter::FilterStatus status; | 221 Filter::FilterStatus status; |
| 231 | 222 |
| 232 switch (inflate_code) { | 223 switch (inflate_code) { |
| 233 case Z_STREAM_END: { | 224 case Z_STREAM_END: { |
| 234 *dest_len = bytesWritten; | 225 *dest_len = bytesWritten; |
| 235 | 226 |
| 236 stream_data_len_ = zlib_stream_.get()->avail_in; | 227 stream_data_len_ = zlib_stream_.get()->avail_in; |
| 237 next_stream_data_ = bit_cast<char*>(zlib_stream_.get()->next_in); | 228 next_stream_data_ = bit_cast<char*>(zlib_stream_.get()->next_in); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 | 268 |
| 278 bool GZipFilter::InsertZlibHeader() { | 269 bool GZipFilter::InsertZlibHeader() { |
| 279 static char dummy_head[2] = { 0x78, 0x1 }; | 270 static char dummy_head[2] = { 0x78, 0x1 }; |
| 280 | 271 |
| 281 char dummy_output[4]; | 272 char dummy_output[4]; |
| 282 | 273 |
| 283 // We only try add additional header once. | 274 // We only try add additional header once. |
| 284 if (zlib_header_added_) | 275 if (zlib_header_added_) |
| 285 return false; | 276 return false; |
| 286 | 277 |
| 287 MOZ_Z_inflateReset(zlib_stream_.get()); | 278 inflateReset(zlib_stream_.get()); |
| 288 zlib_stream_.get()->next_in = bit_cast<Bytef*>(&dummy_head[0]); | 279 zlib_stream_.get()->next_in = bit_cast<Bytef*>(&dummy_head[0]); |
| 289 zlib_stream_.get()->avail_in = sizeof(dummy_head); | 280 zlib_stream_.get()->avail_in = sizeof(dummy_head); |
| 290 zlib_stream_.get()->next_out = bit_cast<Bytef*>(&dummy_output[0]); | 281 zlib_stream_.get()->next_out = bit_cast<Bytef*>(&dummy_output[0]); |
| 291 zlib_stream_.get()->avail_out = sizeof(dummy_output); | 282 zlib_stream_.get()->avail_out = sizeof(dummy_output); |
| 292 | 283 |
| 293 int code = MOZ_Z_inflate(zlib_stream_.get(), Z_NO_FLUSH); | 284 int code = inflate(zlib_stream_.get(), Z_NO_FLUSH); |
| 294 zlib_header_added_ = true; | 285 zlib_header_added_ = true; |
| 295 | 286 |
| 296 return (code == Z_OK); | 287 return (code == Z_OK); |
| 297 } | 288 } |
| 298 | 289 |
| 299 | 290 |
| 300 void GZipFilter::SkipGZipFooter() { | 291 void GZipFilter::SkipGZipFooter() { |
| 301 int footer_bytes_expected = kGZipFooterSize - gzip_footer_bytes_; | 292 int footer_bytes_expected = kGZipFooterSize - gzip_footer_bytes_; |
| 302 if (footer_bytes_expected > 0) { | 293 if (footer_bytes_expected > 0) { |
| 303 int footer_byte_avail = std::min(footer_bytes_expected, stream_data_len_); | 294 int footer_byte_avail = std::min(footer_bytes_expected, stream_data_len_); |
| 304 stream_data_len_ -= footer_byte_avail; | 295 stream_data_len_ -= footer_byte_avail; |
| 305 next_stream_data_ += footer_byte_avail; | 296 next_stream_data_ += footer_byte_avail; |
| 306 gzip_footer_bytes_ += footer_byte_avail; | 297 gzip_footer_bytes_ += footer_byte_avail; |
| 307 | 298 |
| 308 if (stream_data_len_ == 0) | 299 if (stream_data_len_ == 0) |
| 309 next_stream_data_ = NULL; | 300 next_stream_data_ = NULL; |
| 310 } | 301 } |
| 311 } | 302 } |
| 312 | 303 |
| 313 } // namespace net | 304 } // namespace net |
| OLD | NEW |