OLD | NEW |
---|---|
(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/basictypes.h" | |
8 #include "base/logging.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "third_party/brotli/dec/decode.h" | |
11 | |
12 namespace net { | |
13 | |
14 #if !defined(DISABLE_BROTLI_SUPPORT) | |
15 class BrotliFilter : public Filter { | |
16 public: | |
17 ~BrotliFilter() override { | |
18 if (decoding_status_ != DECODING_UNINITIALIZED) | |
19 BrotliStateCleanup(brotli_state_.get()); | |
20 } | |
21 | |
22 BrotliFilter(FilterType type,int buffer_size) | |
xunjieli
2015/12/02 18:26:30
nit: can we have constructor before destructor?
eustas
2015/12/03 12:32:06
Of course. Done.
| |
23 : Filter(type), decoding_status_(DECODING_UNINITIALIZED) { | |
24 InitBuffer(buffer_size); | |
25 } | |
26 | |
27 // Initializes filter decoding mode and internal control blocks. | |
28 // The function returns true if success and false otherwise. | |
29 // The filter can only be initialized once. | |
30 bool InitDecoding() { | |
31 if (decoding_status_ != DECODING_UNINITIALIZED) | |
32 return false; | |
33 | |
34 brotli_state_.reset(new BrotliState); | |
35 if (!brotli_state_.get()) | |
xunjieli
2015/12/02 18:26:30
This if-block looks weird. If |brotli_state_| is s
eustas
2015/12/03 12:32:06
C-style allocation check. Removed.
| |
36 return false; | |
37 | |
38 BrotliStateInit(brotli_state_.get()); | |
39 | |
40 decoding_status_ = DECODING_IN_PROGRESS; | |
41 return true; | |
42 } | |
43 | |
44 // Decodes the pre-filter data and writes the output into the |dest_buffer| | |
45 // passed in. | |
46 // The function returns FilterStatus. See filter.h for its description. | |
47 // | |
48 // Upon entry, |*dest_len| is the total size (in number of chars) of the | |
49 // destination buffer. Upon exit, |*dest_len| is the actual number of chars | |
50 // written into the destination buffer. | |
51 // | |
52 // This function will fail if there is no pre-filter data in the | |
53 // |stream_buffer_|. On the other hand, |*dest_len| can be 0 upon successful | |
54 // return. For example, decompressor may process some pre-filter data | |
55 // but not produce output yet. | |
56 FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len) override { | |
57 if (!dest_buffer || !dest_len || *dest_len <= 0) | |
58 return Filter::FILTER_ERROR; | |
59 | |
60 if (decoding_status_ == DECODING_DONE) { | |
61 *dest_len = 0; | |
62 return Filter::FILTER_DONE; | |
63 } | |
64 | |
65 if (decoding_status_ != DECODING_IN_PROGRESS) | |
66 return Filter::FILTER_ERROR; | |
67 | |
68 size_t available_in = stream_data_len_; | |
69 const uint8_t* next_in = bit_cast<uint8_t*>(next_stream_data_); | |
70 size_t available_out = *dest_len; | |
71 uint8_t* next_out = bit_cast<uint8_t*>(dest_buffer); | |
72 size_t total_out; | |
73 BrotliResult result = | |
74 BrotliDecompressStream(&available_in, &next_in, &available_out, | |
75 &next_out, &total_out, brotli_state_.get()); | |
76 int bytes_written = *dest_len - available_out; | |
77 | |
78 Filter::FilterStatus status; | |
79 switch (result) { | |
80 case BROTLI_RESULT_ERROR: { | |
81 status = Filter::FILTER_ERROR; | |
82 break; | |
83 } | |
84 // Fall through. | |
85 case BROTLI_RESULT_NEEDS_MORE_OUTPUT: | |
86 case BROTLI_RESULT_SUCCESS: { | |
87 status = Filter::FILTER_OK; | |
88 *dest_len = bytes_written; | |
89 stream_data_len_ = available_in; | |
90 next_stream_data_ = bit_cast<char*>(next_in); | |
91 if (result == BROTLI_RESULT_SUCCESS) | |
92 status = Filter::FILTER_DONE; | |
93 break; | |
94 } | |
95 case BROTLI_RESULT_NEEDS_MORE_INPUT: { | |
96 status = Filter::FILTER_NEED_MORE_DATA; | |
97 *dest_len = bytes_written; | |
98 stream_data_len_ = 0; | |
99 next_stream_data_ = NULL; | |
xunjieli
2015/12/02 18:26:30
nit: nullptr here.
eustas
2015/12/03 12:32:06
Done.
| |
100 break; | |
101 } | |
102 default: { | |
103 status = Filter::FILTER_ERROR; | |
104 break; | |
105 } | |
106 } | |
107 | |
108 if (status == Filter::FILTER_DONE) { | |
109 decoding_status_ = DECODING_DONE; | |
110 } else if (status == Filter::FILTER_ERROR) { | |
111 decoding_status_ = DECODING_ERROR; | |
112 } | |
113 | |
114 return status; | |
115 } | |
116 | |
117 private: | |
118 enum DecodingStatus { | |
119 DECODING_UNINITIALIZED, | |
120 DECODING_IN_PROGRESS, | |
121 DECODING_DONE, | |
122 DECODING_ERROR | |
123 }; | |
124 | |
125 // Tracks the status of decoding. | |
126 // This variable is initialized by InitDecoding and updated only by | |
127 // ReadFilteredData. | |
128 DecodingStatus decoding_status_; | |
129 | |
130 scoped_ptr<BrotliStateStruct> brotli_state_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(BrotliFilter); | |
xunjieli
2015/12/02 18:26:30
nit: need to include base/macro.h for DISALLOW_COP
eustas
2015/12/03 12:32:06
Done. Thank you.
| |
133 }; | |
134 #endif | |
135 | |
136 Filter* CreateBrotliFilter(Filter::FilterType type_id, int buffer_size) { | |
137 #if !defined(DISABLE_BROTLI_SUPPORT) | |
138 scoped_ptr<BrotliFilter> brotli_filter( | |
139 new BrotliFilter(type_id, buffer_size)); | |
140 return brotli_filter->InitDecoding() ? brotli_filter.release() : NULL; | |
xunjieli
2015/12/02 18:26:30
nit: nullptr this line and on the next line.
eustas
2015/12/03 12:32:06
Fixed everywhere.
| |
141 #else | |
142 return NULL; | |
143 #endif | |
144 } | |
145 | |
146 } // namespace net | |
OLD | NEW |