| 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..74ef5c533d775aaa971ca872f5c757e7d66cd36f
|
| --- /dev/null
|
| +++ b/net/filter/brotli_filter.cc
|
| @@ -0,0 +1,144 @@
|
| +// 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/basictypes.h"
|
| +#include "base/logging.h"
|
| +#include "base/macros.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "third_party/brotli/dec/decode.h"
|
| +
|
| +namespace net {
|
| +
|
| +#if !defined(DISABLE_BROTLI_SUPPORT)
|
| +class BrotliFilter : public Filter {
|
| + public:
|
| + BrotliFilter(FilterType type, int buffer_size)
|
| + : Filter(type), decoding_status_(DECODING_UNINITIALIZED) {
|
| + InitBuffer(buffer_size);
|
| + }
|
| +
|
| + ~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);
|
| + 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;
|
| +
|
| + Filter::FilterStatus status;
|
| + switch (result) {
|
| + case BROTLI_RESULT_ERROR: {
|
| + status = Filter::FILTER_ERROR;
|
| + break;
|
| + }
|
| + // Fall through.
|
| + 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;
|
| + }
|
| +
|
| + 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);
|
| +};
|
| +#endif
|
| +
|
| +Filter* CreateBrotliFilter(Filter::FilterType type_id, int buffer_size) {
|
| +#if !defined(DISABLE_BROTLI_SUPPORT)
|
| + scoped_ptr<BrotliFilter> brotli_filter(
|
| + new BrotliFilter(type_id, buffer_size));
|
| + return brotli_filter->InitDecoding() ? brotli_filter.release() : nullptr;
|
| +#else
|
| + return nullptr;
|
| +#endif
|
| +}
|
| +
|
| +} // namespace net
|
|
|