| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/filter/brotli_source_stream.h" | 5 #include "net/filter/brotli_source_stream.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bit_cast.h" | 8 #include "base/bit_cast.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 | 86 |
| 87 // SourceStream implementation | 87 // SourceStream implementation |
| 88 std::string GetTypeAsString() const override { return kBrotli; } | 88 std::string GetTypeAsString() const override { return kBrotli; } |
| 89 | 89 |
| 90 int FilterData(IOBuffer* output_buffer, | 90 int FilterData(IOBuffer* output_buffer, |
| 91 int output_buffer_size, | 91 int output_buffer_size, |
| 92 IOBuffer* input_buffer, | 92 IOBuffer* input_buffer, |
| 93 int input_buffer_size, | 93 int input_buffer_size, |
| 94 int* consumed_bytes, | 94 int* consumed_bytes, |
| 95 bool /*upstream_eof_reached*/) override { | 95 bool /*upstream_eof_reached*/) override { |
| 96 if (decoding_status_ == DecodingStatus::DECODING_DONE) { |
| 97 *consumed_bytes = input_buffer_size; |
| 98 return OK; |
| 99 } |
| 100 |
| 101 if (decoding_status_ != DecodingStatus::DECODING_IN_PROGRESS) |
| 102 return ERR_CONTENT_DECODING_FAILED; |
| 103 |
| 96 const uint8_t* next_in = bit_cast<uint8_t*>(input_buffer->data()); | 104 const uint8_t* next_in = bit_cast<uint8_t*>(input_buffer->data()); |
| 97 size_t available_in = input_buffer_size; | 105 size_t available_in = input_buffer_size; |
| 98 uint8_t* next_out = bit_cast<uint8_t*>(output_buffer->data()); | 106 uint8_t* next_out = bit_cast<uint8_t*>(output_buffer->data()); |
| 99 size_t available_out = output_buffer_size; | 107 size_t available_out = output_buffer_size; |
| 100 size_t total_out = 0; | 108 size_t total_out = 0; |
| 101 // Check if start of the input stream looks like gzip stream. | 109 // Check if start of the input stream looks like gzip stream. |
| 102 for (size_t i = consumed_bytes_; i < sizeof(kGzipHeader); ++i) { | 110 for (size_t i = consumed_bytes_; i < sizeof(kGzipHeader); ++i) { |
| 103 if (!gzip_header_detected_) | 111 if (!gzip_header_detected_) |
| 104 break; | 112 break; |
| 105 size_t j = i - consumed_bytes_; | 113 size_t j = i - consumed_bytes_; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 118 produced_bytes_ += bytes_written; | 126 produced_bytes_ += bytes_written; |
| 119 consumed_bytes_ += bytes_used; | 127 consumed_bytes_ += bytes_used; |
| 120 | 128 |
| 121 *consumed_bytes = bytes_used; | 129 *consumed_bytes = bytes_used; |
| 122 | 130 |
| 123 switch (result) { | 131 switch (result) { |
| 124 case BROTLI_RESULT_NEEDS_MORE_OUTPUT: | 132 case BROTLI_RESULT_NEEDS_MORE_OUTPUT: |
| 125 return bytes_written; | 133 return bytes_written; |
| 126 case BROTLI_RESULT_SUCCESS: | 134 case BROTLI_RESULT_SUCCESS: |
| 127 decoding_status_ = DecodingStatus::DECODING_DONE; | 135 decoding_status_ = DecodingStatus::DECODING_DONE; |
| 136 // Consume remaining bytes to avoid DCHECK in FilterSourceStream. |
| 137 // See crbug.com/659311. |
| 138 *consumed_bytes = input_buffer_size; |
| 128 return bytes_written; | 139 return bytes_written; |
| 129 case BROTLI_RESULT_NEEDS_MORE_INPUT: | 140 case BROTLI_RESULT_NEEDS_MORE_INPUT: |
| 130 // Decompress needs more input has consumed all existing input. | 141 // Decompress needs more input has consumed all existing input. |
| 131 DCHECK_EQ(*consumed_bytes, input_buffer_size); | 142 DCHECK_EQ(*consumed_bytes, input_buffer_size); |
| 132 decoding_status_ = DecodingStatus::DECODING_IN_PROGRESS; | 143 decoding_status_ = DecodingStatus::DECODING_IN_PROGRESS; |
| 133 return bytes_written; | 144 return bytes_written; |
| 134 // If the decompressor threw an error, fail synchronously. | 145 // If the decompressor threw an error, fail synchronously. |
| 135 default: | 146 default: |
| 136 decoding_status_ = DecodingStatus::DECODING_ERROR; | 147 decoding_status_ = DecodingStatus::DECODING_ERROR; |
| 137 return ERR_CONTENT_DECODING_FAILED; | 148 return ERR_CONTENT_DECODING_FAILED; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 }; | 193 }; |
| 183 | 194 |
| 184 } // namespace | 195 } // namespace |
| 185 | 196 |
| 186 std::unique_ptr<FilterSourceStream> CreateBrotliSourceStream( | 197 std::unique_ptr<FilterSourceStream> CreateBrotliSourceStream( |
| 187 std::unique_ptr<SourceStream> previous) { | 198 std::unique_ptr<SourceStream> previous) { |
| 188 return base::WrapUnique(new BrotliSourceStream(std::move(previous))); | 199 return base::WrapUnique(new BrotliSourceStream(std::move(previous))); |
| 189 } | 200 } |
| 190 | 201 |
| 191 } // namespace net | 202 } // namespace net |
| OLD | NEW |