Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: net/filter/brotli_filter.cc

Issue 1431723002: Add brotli content-encoding filter. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix histograms.xml Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/filter/brotli_filter.h"
6
7 #include "base/logging.h"
8 #include "third_party/brotli/dec/decode.h"
9
10 namespace net {
11
12 BrotliFilter::BrotliFilter(FilterType type)
13 : Filter(type), decoding_status_(DECODING_UNINITIALIZED) {}
14
15 BrotliFilter::~BrotliFilter() {
16 if (decoding_status_ != DECODING_UNINITIALIZED)
17 BrotliStateCleanup(brotli_state_.get());
18 }
19
20 bool BrotliFilter::InitDecoding() {
21 if (decoding_status_ != DECODING_UNINITIALIZED)
22 return false;
23
24 brotli_state_.reset(new BrotliState);
25 if (!brotli_state_.get())
26 return false;
27
28 BrotliStateInit(brotli_state_.get());
29
30 decoding_status_ = DECODING_IN_PROGRESS;
31 return true;
32 }
33
34 Filter::FilterStatus BrotliFilter::ReadFilteredData(char* dest_buffer,
35 int* dest_len) {
36 if (!dest_buffer || !dest_len || *dest_len <= 0)
37 return Filter::FILTER_ERROR;
38
39 if (decoding_status_ == DECODING_DONE) {
40 *dest_len = 0;
41 return Filter::FILTER_DONE;
42 }
43
44 if (decoding_status_ != DECODING_IN_PROGRESS)
45 return Filter::FILTER_ERROR;
46
47 size_t available_in = stream_data_len_;
48 const uint8_t* next_in = bit_cast<uint8_t*>(next_stream_data_);
49 size_t available_out = *dest_len;
50 uint8_t* next_out = bit_cast<uint8_t*>(dest_buffer);
51 size_t total_out;
52 BrotliResult result =
53 BrotliDecompressStream(&available_in, &next_in, &available_out, &next_out,
54 &total_out, brotli_state_.get());
55 int bytes_written = *dest_len - available_out;
56
57 Filter::FilterStatus status;
58 switch (result) {
59 case BROTLI_RESULT_ERROR: {
60 status = Filter::FILTER_ERROR;
61 break;
62 }
63 // Fall through.
64 case BROTLI_RESULT_NEEDS_MORE_OUTPUT:
65 case BROTLI_RESULT_SUCCESS: {
66 status = Filter::FILTER_OK;
67 *dest_len = bytes_written;
68 stream_data_len_ = available_in;
69 next_stream_data_ = bit_cast<char*>(next_in);
70 if (result == BROTLI_RESULT_SUCCESS)
71 status = Filter::FILTER_DONE;
72 break;
73 }
74 case BROTLI_RESULT_NEEDS_MORE_INPUT: {
75 status = Filter::FILTER_NEED_MORE_DATA;
76 *dest_len = bytes_written;
77 stream_data_len_ = 0;
78 next_stream_data_ = NULL;
79 break;
80 }
81 default: {
82 status = Filter::FILTER_ERROR;
83 break;
84 }
85 }
86
87 if (status == Filter::FILTER_DONE) {
88 decoding_status_ = DECODING_DONE;
89 } else if (status == Filter::FILTER_ERROR) {
90 decoding_status_ = DECODING_ERROR;
91 }
92
93 return status;
94 }
95
96 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698