Index: net/filter/brotli_filter.cc |
diff --git a/net/filter/brotli_filter.cc b/net/filter/brotli_filter.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..91bcec79932d760fc5fa690740b382ec5ffe02fd |
--- /dev/null |
+++ b/net/filter/brotli_filter.cc |
@@ -0,0 +1,133 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "net/filter/brotli_filter.h" |
+ |
+#include "base/macros.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "third_party/brotli/dec/decode.h" |
+ |
+namespace net { |
+ |
+class BrotliFilter : public Filter { |
+ public: |
+ BrotliFilter(FilterType type) |
+ : Filter(type), decoding_status_(DECODING_UNINITIALIZED) {} |
+ |
+ ~BrotliFilter() override { |
+ if (decoding_status_ != DECODING_UNINITIALIZED) |
+ BrotliStateCleanup(brotli_state_.get()); |
+ } |
+ |
+ // Initializes filter decoding mode and internal control blocks. |
+ // The function returns true if success and false otherwise. |
+ // The filter can only be initialized once. |
+ bool InitDecoding() { |
+ if (decoding_status_ != DECODING_UNINITIALIZED) |
+ return false; |
+ |
+ brotli_state_.reset(new BrotliState); |
Ryan Sleevi
2015/12/28 19:30:53
Does this actually need to be a pointer? It seems
eustas
2015/12/29 13:15:39
No need. It just moved from previous patches. Done
|
+ BrotliStateInit(brotli_state_.get()); |
+ |
+ decoding_status_ = DECODING_IN_PROGRESS; |
+ return true; |
+ } |
+ |
+ // Decodes the pre-filter data and writes the output into the |dest_buffer| |
+ // passed in. |
+ // The function returns FilterStatus. See filter.h for its description. |
+ // |
+ // Upon entry, |*dest_len| is the total size (in number of chars) of the |
+ // destination buffer. Upon exit, |*dest_len| is the actual number of chars |
+ // written into the destination buffer. |
+ // |
+ // This function will fail if there is no pre-filter data in the |
+ // |stream_buffer_|. On the other hand, |*dest_len| can be 0 upon successful |
+ // return. For example, decompressor may process some pre-filter data |
+ // but not produce output yet. |
+ FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len) override { |
+ if (!dest_buffer || !dest_len || *dest_len <= 0) |
+ return Filter::FILTER_ERROR; |
+ |
+ if (decoding_status_ == DECODING_DONE) { |
+ *dest_len = 0; |
+ return Filter::FILTER_DONE; |
+ } |
+ |
+ if (decoding_status_ != DECODING_IN_PROGRESS) |
+ return Filter::FILTER_ERROR; |
+ |
+ size_t available_in = stream_data_len_; |
+ const uint8_t* next_in = bit_cast<uint8_t*>(next_stream_data_); |
+ size_t available_out = *dest_len; |
+ uint8_t* next_out = bit_cast<uint8_t*>(dest_buffer); |
+ size_t total_out = 0; |
+ BrotliResult result = |
+ BrotliDecompressStream(&available_in, &next_in, &available_out, |
+ &next_out, &total_out, brotli_state_.get()); |
+ int bytes_written = *dest_len - available_out; |
Ryan Sleevi
2015/12/28 19:30:53
DANGER: You're mixing signed and unsigned types in
eustas
2015/12/29 13:15:39
Good idea. Done.
|
+ |
+ Filter::FilterStatus status; |
+ switch (result) { |
+ case BROTLI_RESULT_ERROR: { |
+ status = Filter::FILTER_ERROR; |
+ break; |
+ } |
+ // Fall through. |
Ryan Sleevi
2015/12/28 19:30:53
This comment doesn't seem correct; you break on li
eustas
2015/12/29 13:15:39
I meant that "case BROTLI_RESULT_NEEDS_MORE_OUTPUT
|
+ case BROTLI_RESULT_NEEDS_MORE_OUTPUT: |
+ case BROTLI_RESULT_SUCCESS: { |
+ status = Filter::FILTER_OK; |
+ *dest_len = bytes_written; |
+ stream_data_len_ = available_in; |
+ next_stream_data_ = bit_cast<char*>(next_in); |
+ if (result == BROTLI_RESULT_SUCCESS) |
+ status = Filter::FILTER_DONE; |
+ break; |
+ } |
+ case BROTLI_RESULT_NEEDS_MORE_INPUT: { |
+ status = Filter::FILTER_NEED_MORE_DATA; |
+ *dest_len = bytes_written; |
+ stream_data_len_ = 0; |
+ next_stream_data_ = nullptr; |
+ break; |
+ } |
+ default: { |
+ status = Filter::FILTER_ERROR; |
+ break; |
+ } |
+ } |
+ |
+ if (status == Filter::FILTER_DONE) { |
+ decoding_status_ = DECODING_DONE; |
+ } else if (status == Filter::FILTER_ERROR) { |
+ decoding_status_ = DECODING_ERROR; |
+ } |
Ryan Sleevi
2015/12/28 19:30:53
Should this be a switch, rather than a conditional
eustas
2015/12/29 13:15:39
Backed this conditional to previous switch; now it
|
+ |
+ return status; |
+ } |
+ |
+ private: |
+ enum DecodingStatus { |
+ DECODING_UNINITIALIZED, |
+ DECODING_IN_PROGRESS, |
+ DECODING_DONE, |
+ DECODING_ERROR |
+ }; |
+ |
+ // Tracks the status of decoding. |
+ // This variable is initialized by InitDecoding and updated only by |
+ // ReadFilteredData. |
+ DecodingStatus decoding_status_; |
+ |
+ scoped_ptr<BrotliStateStruct> brotli_state_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BrotliFilter); |
+}; |
+ |
+Filter* CreateBrotliFilter(Filter::FilterType type_id) { |
+ scoped_ptr<BrotliFilter> brotli_filter(new BrotliFilter(type_id)); |
+ return brotli_filter->InitDecoding() ? brotli_filter.release() : nullptr; |
+} |
+ |
+} // namespace net |