1 | // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #ifndef RUNTIME_BIN_FILTER_H_ |
6 | #define RUNTIME_BIN_FILTER_H_ |
7 | |
8 | #include "bin/builtin.h" |
9 | #include "bin/utils.h" |
10 | |
11 | #include "zlib/zlib.h" |
12 | |
13 | namespace dart { |
14 | namespace bin { |
15 | |
16 | class Filter { |
17 | public: |
18 | virtual ~Filter() {} |
19 | |
20 | virtual bool Init() = 0; |
21 | |
22 | /** |
23 | * On a successful call to Process, Process will take ownership of data. On |
24 | * successive calls to either Processed or ~Filter, data will be freed with |
25 | * a delete[] call. |
26 | */ |
27 | virtual bool Process(uint8_t* data, intptr_t length) = 0; |
28 | virtual intptr_t Processed(uint8_t* buffer, |
29 | intptr_t length, |
30 | bool finish, |
31 | bool end) = 0; |
32 | |
33 | static Dart_Handle SetFilterAndCreateFinalizer(Dart_Handle filter, |
34 | Filter* filter_pointer, |
35 | intptr_t filter_size); |
36 | static Dart_Handle GetFilterNativeField(Dart_Handle filter, |
37 | Filter** filter_pointer); |
38 | |
39 | bool initialized() const { return initialized_; } |
40 | void set_initialized(bool value) { initialized_ = value; } |
41 | uint8_t* processed_buffer() { return processed_buffer_; } |
42 | intptr_t processed_buffer_size() const { return kFilterBufferSize; } |
43 | |
44 | protected: |
45 | Filter() : initialized_(false) {} |
46 | |
47 | private: |
48 | static const intptr_t kFilterBufferSize = 64 * KB; |
49 | uint8_t processed_buffer_[kFilterBufferSize]; |
50 | bool initialized_; |
51 | |
52 | DISALLOW_COPY_AND_ASSIGN(Filter); |
53 | }; |
54 | |
55 | class ZLibDeflateFilter : public Filter { |
56 | public: |
57 | ZLibDeflateFilter(bool gzip, |
58 | int32_t level, |
59 | int32_t window_bits, |
60 | int32_t mem_level, |
61 | int32_t strategy, |
62 | uint8_t* dictionary, |
63 | intptr_t dictionary_length, |
64 | bool raw) |
65 | : gzip_(gzip), |
66 | level_(level), |
67 | window_bits_(window_bits), |
68 | mem_level_(mem_level), |
69 | strategy_(strategy), |
70 | dictionary_(dictionary), |
71 | dictionary_length_(dictionary_length), |
72 | raw_(raw), |
73 | current_buffer_(NULL) {} |
74 | virtual ~ZLibDeflateFilter(); |
75 | |
76 | virtual bool Init(); |
77 | virtual bool Process(uint8_t* data, intptr_t length); |
78 | virtual intptr_t Processed(uint8_t* buffer, |
79 | intptr_t length, |
80 | bool finish, |
81 | bool end); |
82 | |
83 | private: |
84 | const bool gzip_; |
85 | const int32_t level_; |
86 | const int32_t window_bits_; |
87 | const int32_t mem_level_; |
88 | const int32_t strategy_; |
89 | uint8_t* dictionary_; |
90 | const intptr_t dictionary_length_; |
91 | const bool raw_; |
92 | uint8_t* current_buffer_; |
93 | z_stream stream_; |
94 | |
95 | DISALLOW_COPY_AND_ASSIGN(ZLibDeflateFilter); |
96 | }; |
97 | |
98 | class ZLibInflateFilter : public Filter { |
99 | public: |
100 | ZLibInflateFilter(int32_t window_bits, |
101 | uint8_t* dictionary, |
102 | intptr_t dictionary_length, |
103 | bool raw) |
104 | : window_bits_(window_bits), |
105 | dictionary_(dictionary), |
106 | dictionary_length_(dictionary_length), |
107 | raw_(raw), |
108 | current_buffer_(NULL) {} |
109 | virtual ~ZLibInflateFilter(); |
110 | |
111 | virtual bool Init(); |
112 | virtual bool Process(uint8_t* data, intptr_t length); |
113 | virtual intptr_t Processed(uint8_t* buffer, |
114 | intptr_t length, |
115 | bool finish, |
116 | bool end); |
117 | |
118 | private: |
119 | const int32_t window_bits_; |
120 | uint8_t* dictionary_; |
121 | const intptr_t dictionary_length_; |
122 | const bool raw_; |
123 | uint8_t* current_buffer_; |
124 | z_stream stream_; |
125 | |
126 | DISALLOW_COPY_AND_ASSIGN(ZLibInflateFilter); |
127 | }; |
128 | |
129 | } // namespace bin |
130 | } // namespace dart |
131 | |
132 | #endif // RUNTIME_BIN_FILTER_H_ |
133 | |