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

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: Addressed comments, moved feature declatation to content Created 4 years, 11 months 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
« no previous file with comments | « net/filter/brotli_filter.h ('k') | net/filter/brotli_filter_disabled.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/macros.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "third_party/brotli/dec/decode.h"
10
11 namespace net {
12
13 // BrotliFilter applies Brotli content decoding to a data stream.
14 // Brotli format specification: http://www.ietf.org/id/draft-alakuijala-brotli
15 //
16 // BrotliFilter is a subclass of Filter. See the latter's header file filter.h
17 // for sample usage.
18 class BrotliFilter : public Filter {
19 public:
20 BrotliFilter(FilterType type)
21 : Filter(type), decoding_status_(DECODING_IN_PROGRESS) {
22 BrotliStateInit(&brotli_state_);
23 }
24
25 ~BrotliFilter() override { BrotliStateCleanup(&brotli_state_); }
26
27 // Decodes the pre-filter data and writes the output into the |dest_buffer|
28 // passed in.
29 // The function returns FilterStatus. See filter.h for its description.
30 //
31 // Upon entry, |*dest_len| is the total size (in number of chars) of the
32 // destination buffer. Upon exit, |*dest_len| is the actual number of chars
33 // written into the destination buffer.
34 //
35 // This function will fail if there is no pre-filter data in the
36 // |stream_buffer_|. On the other hand, |*dest_len| can be 0 upon successful
37 // return. For example, decompressor may process some pre-filter data
38 // but not produce output yet.
39 FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len) override {
40 if (!dest_buffer || !dest_len)
41 return Filter::FILTER_ERROR;
42
43 if (!base::IsValueInRangeForNumericType<size_t>(*dest_len))
44 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.
45 size_t output_buffer_size = static_cast<size_t>(*dest_len);
46
47 if (decoding_status_ == DECODING_DONE) {
48 *dest_len = 0;
49 return Filter::FILTER_DONE;
50 }
51
52 if (decoding_status_ != DECODING_IN_PROGRESS)
53 return Filter::FILTER_ERROR;
54
55 size_t available_in = stream_data_len_;
56 const uint8_t* next_in = bit_cast<uint8_t*>(next_stream_data_);
57 size_t available_out = output_buffer_size;
58 uint8_t* next_out = bit_cast<uint8_t*>(dest_buffer);
59 size_t total_out = 0;
60 BrotliResult result =
61 BrotliDecompressStream(&available_in, &next_in, &available_out,
62 &next_out, &total_out, &brotli_state_);
63 int bytes_written =
64 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
65
66 switch (result) {
67 case BROTLI_RESULT_NEEDS_MORE_OUTPUT:
68 // Fall through.
69 case BROTLI_RESULT_SUCCESS:
70 *dest_len = bytes_written;
71 stream_data_len_ = available_in;
72 next_stream_data_ = bit_cast<char*>(next_in);
73 if (result == BROTLI_RESULT_SUCCESS) {
74 decoding_status_ = DECODING_DONE;
75 return Filter::FILTER_DONE;
76 }
77 return Filter::FILTER_OK;
78
79 case BROTLI_RESULT_NEEDS_MORE_INPUT:
80 *dest_len = bytes_written;
81 stream_data_len_ = 0;
82 next_stream_data_ = nullptr;
83 return Filter::FILTER_NEED_MORE_DATA;
84
85 default:
86 decoding_status_ = DECODING_ERROR;
87 return Filter::FILTER_ERROR;
88 }
89 }
90
91 private:
92 enum DecodingStatus { DECODING_IN_PROGRESS, DECODING_DONE, DECODING_ERROR };
93
94 // Tracks the status of decoding.
95 // This variable is updated only by ReadFilteredData.
96 DecodingStatus decoding_status_;
97
98 BrotliState brotli_state_;
99
100 DISALLOW_COPY_AND_ASSIGN(BrotliFilter);
101 };
102
103 Filter* CreateBrotliFilter(Filter::FilterType type_id) {
104 return new BrotliFilter(type_id);
105 }
106
107 } // namespace net
OLDNEW
« no previous file with comments | « net/filter/brotli_filter.h ('k') | net/filter/brotli_filter_disabled.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698