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..e5ef5c30aaaaa3c348024e266369fc0b754341fd |
--- /dev/null |
+++ b/net/filter/brotli_filter.cc |
@@ -0,0 +1,107 @@ |
+// 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/numerics/safe_conversions.h" |
+#include "third_party/brotli/dec/decode.h" |
+ |
+namespace net { |
+ |
+// BrotliFilter applies Brotli content decoding to a data stream. |
+// Brotli format specification: http://www.ietf.org/id/draft-alakuijala-brotli |
+// |
+// BrotliFilter is a subclass of Filter. See the latter's header file filter.h |
+// for sample usage. |
+class BrotliFilter : public Filter { |
+ public: |
+ BrotliFilter(FilterType type) |
+ : Filter(type), decoding_status_(DECODING_IN_PROGRESS) { |
+ BrotliStateInit(&brotli_state_); |
+ } |
+ |
+ ~BrotliFilter() override { BrotliStateCleanup(&brotli_state_); } |
+ |
+ // 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) |
+ return Filter::FILTER_ERROR; |
+ |
+ if (!base::IsValueInRangeForNumericType<size_t>(*dest_len)) |
+ return Filter::FILTER_ERROR; |
Ryan Sleevi
2015/12/29 20:59:11
If this ever happens, isn't it a programmer error?
eustas
2015/12/30 11:38:35
Surely.
|
+ size_t output_buffer_size = static_cast<size_t>(*dest_len); |
+ |
+ 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 = output_buffer_size; |
+ 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_); |
+ int bytes_written = |
+ base::checked_cast<int>(output_buffer_size - available_out); |
Ryan Sleevi
2015/12/29 20:59:11
So this isn't actually safe (or at least, jschuh a
eustas
2015/12/30 11:38:34
Done.
I've also added explicit decompressor contra
|
+ |
+ switch (result) { |
+ case BROTLI_RESULT_NEEDS_MORE_OUTPUT: |
+ // Fall through. |
+ case BROTLI_RESULT_SUCCESS: |
+ *dest_len = bytes_written; |
+ stream_data_len_ = available_in; |
+ next_stream_data_ = bit_cast<char*>(next_in); |
+ if (result == BROTLI_RESULT_SUCCESS) { |
+ decoding_status_ = DECODING_DONE; |
+ return Filter::FILTER_DONE; |
+ } |
+ return Filter::FILTER_OK; |
+ |
+ case BROTLI_RESULT_NEEDS_MORE_INPUT: |
+ *dest_len = bytes_written; |
+ stream_data_len_ = 0; |
+ next_stream_data_ = nullptr; |
+ return Filter::FILTER_NEED_MORE_DATA; |
+ |
+ default: |
+ decoding_status_ = DECODING_ERROR; |
+ return Filter::FILTER_ERROR; |
+ } |
+ } |
+ |
+ private: |
+ enum DecodingStatus { DECODING_IN_PROGRESS, DECODING_DONE, DECODING_ERROR }; |
+ |
+ // Tracks the status of decoding. |
+ // This variable is updated only by ReadFilteredData. |
+ DecodingStatus decoding_status_; |
+ |
+ BrotliState brotli_state_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BrotliFilter); |
+}; |
+ |
+Filter* CreateBrotliFilter(Filter::FilterType type_id) { |
+ return new BrotliFilter(type_id); |
+} |
+ |
+} // namespace net |