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

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: Exclude brotli_filter_unittest on ios - needs to read input data files 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 "base/numerics/safe_math.h"
10 #include "third_party/brotli/dec/decode.h"
11
12 namespace net {
13
14 // BrotliFilter applies Brotli content decoding to a data stream.
15 // Brotli format specification: http://www.ietf.org/id/draft-alakuijala-brotli
16 //
17 // BrotliFilter is a subclass of Filter. See the latter's header file filter.h
18 // for sample usage.
19 class BrotliFilter : public Filter {
20 public:
21 BrotliFilter(FilterType type)
22 : Filter(type), decoding_status_(DECODING_IN_PROGRESS) {
23 BrotliStateInit(&brotli_state_);
24 }
25
26 ~BrotliFilter() override { BrotliStateCleanup(&brotli_state_); }
27
28 // Decodes the pre-filter data and writes the output into the |dest_buffer|
29 // passed in.
30 // The function returns FilterStatus. See filter.h for its description.
31 //
32 // Upon entry, |*dest_len| is the total size (in number of chars) of the
33 // destination buffer. Upon exit, |*dest_len| is the actual number of chars
34 // written into the destination buffer.
35 //
36 // This function will fail if there is no pre-filter data in the
37 // |stream_buffer_|. On the other hand, |*dest_len| can be 0 upon successful
38 // return. For example, decompressor may process some pre-filter data
39 // but not produce output yet.
40 FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len) override {
41 if (!dest_buffer || !dest_len)
42 return Filter::FILTER_ERROR;
43
44 if (decoding_status_ == DECODING_DONE) {
45 *dest_len = 0;
46 return Filter::FILTER_DONE;
47 }
48
49 if (decoding_status_ != DECODING_IN_PROGRESS)
50 return Filter::FILTER_ERROR;
51
52 size_t output_buffer_size = base::checked_cast<size_t>(*dest_len);
53 size_t input_buffer_size = base::checked_cast<size_t>(stream_data_len_);
54
55 size_t available_in = input_buffer_size;
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
64 CHECK(available_in <= input_buffer_size);
65 CHECK(available_out <= output_buffer_size);
66
67 base::CheckedNumeric<size_t> safe_bytes_written(output_buffer_size);
68 safe_bytes_written -= available_out;
69 int bytes_written =
70 base::checked_cast<int>(safe_bytes_written.ValueOrDie());
71
72 switch (result) {
73 case BROTLI_RESULT_NEEDS_MORE_OUTPUT:
74 // Fall through.
75 case BROTLI_RESULT_SUCCESS:
76 *dest_len = bytes_written;
77 stream_data_len_ = base::checked_cast<int>(available_in);
78 next_stream_data_ = bit_cast<char*>(next_in);
79 if (result == BROTLI_RESULT_SUCCESS) {
80 decoding_status_ = DECODING_DONE;
81 return Filter::FILTER_DONE;
82 }
83 return Filter::FILTER_OK;
84
85 case BROTLI_RESULT_NEEDS_MORE_INPUT:
86 *dest_len = bytes_written;
87 stream_data_len_ = 0;
88 next_stream_data_ = nullptr;
89 return Filter::FILTER_NEED_MORE_DATA;
90
91 default:
92 decoding_status_ = DECODING_ERROR;
93 return Filter::FILTER_ERROR;
94 }
95 }
96
97 private:
98 enum DecodingStatus { DECODING_IN_PROGRESS, DECODING_DONE, DECODING_ERROR };
99
100 // Tracks the status of decoding.
101 // This variable is updated only by ReadFilteredData.
102 DecodingStatus decoding_status_;
103
104 BrotliState brotli_state_;
105
106 DISALLOW_COPY_AND_ASSIGN(BrotliFilter);
107 };
108
109 Filter* CreateBrotliFilter(Filter::FilterType type_id) {
110 return new BrotliFilter(type_id);
111 }
112
113 } // 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