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

Side by Side Diff: runtime/bin/filter.cc

Issue 130513003: [ZLIB] Add support for windowBits, memLevel, raw (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: integrate feedback Created 6 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « runtime/bin/filter.h ('k') | runtime/bin/filter_patch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "bin/dartutils.h" 5 #include "bin/dartutils.h"
6 #include "bin/filter.h" 6 #include "bin/filter.h"
7 #include "bin/io_buffer.h" 7 #include "bin/io_buffer.h"
8 8
9 #include "include/dart_api.h" 9 #include "include/dart_api.h"
10 10
11
12 namespace dart { 11 namespace dart {
13 namespace bin { 12 namespace bin {
14 13
15 const int kZlibFlagMemUsage = 8;
16 const int kZLibFlagWindowBits = 15;
17 const int kZLibFlagUseGZipHeader = 16; 14 const int kZLibFlagUseGZipHeader = 16;
18 const int kZLibFlagAcceptAnyHeader = 32; 15 const int kZLibFlagAcceptAnyHeader = 32;
19 16
20 static const int kFilterPointerNativeField = 0; 17 static const int kFilterPointerNativeField = 0;
21 18
22 Filter* GetFilter(Dart_Handle filter_obj) { 19 static Filter* GetFilter(Dart_Handle filter_obj) {
23 Filter* filter; 20 Filter* filter;
24 Dart_Handle result = Filter::GetFilterPointerNativeField(filter_obj, &filter); 21 Dart_Handle result = Filter::GetFilterPointerNativeField(filter_obj, &filter);
25 if (Dart_IsError(result)) { 22 if (Dart_IsError(result)) {
26 Dart_PropagateError(result); 23 Dart_PropagateError(result);
27 } 24 }
28 if (filter == NULL) { 25 if (filter == NULL) {
29 Dart_ThrowException(DartUtils::NewInternalError("Filter destroyed")); 26 Dart_ThrowException(DartUtils::NewInternalError("Filter destroyed"));
30 } 27 }
31 return filter; 28 return filter;
32 } 29 }
33 30
34 void EndFilter(Dart_Handle filter_obj, Filter* filter) { 31 static void EndFilter(Dart_Handle filter_obj, Filter* filter) {
35 Filter::SetFilterPointerNativeField(filter_obj, NULL); 32 Filter::SetFilterPointerNativeField(filter_obj, NULL);
36 delete filter; 33 delete filter;
37 } 34 }
38 35
36 static uint8_t* copyDictionary(Dart_Handle dictionary_obj) {
37 uint8_t* src = NULL;
38 intptr_t size;
39 Dart_TypedData_Type type;
40
41 if (Dart_IsError(Dart_ListLength(dictionary_obj, &size))) {
42 Dart_ThrowException(DartUtils::NewInternalError(
43 "Failed to get the zlib dictionary length"));
44 }
45
46 uint8_t* dictionary = new uint8_t[size];
47
48 if (dictionary == NULL) {
49 Dart_ThrowException(DartUtils::NewInternalError(
50 "Failed to allocate buffer for the zlib dictionary"));
51 }
52
53 Dart_Handle result = Dart_TypedDataAcquireData(
54 dictionary_obj, &type, reinterpret_cast<void**>(&src), &size);
55 if (!Dart_IsError(result)) {
56 memmove(dictionary, src, size);
57 Dart_TypedDataReleaseData(dictionary_obj);
58 } else {
59 if (Dart_IsError(Dart_ListGetAsBytes(dictionary_obj, 0, dictionary,
60 size))) {
61 Dart_ThrowException(DartUtils::NewInternalError(
62 "Failed to get the zlib dictionary"));
63 }
64 }
65
66 return dictionary;
67 }
68
39 void FUNCTION_NAME(Filter_CreateZLibInflate)(Dart_NativeArguments args) { 69 void FUNCTION_NAME(Filter_CreateZLibInflate)(Dart_NativeArguments args) {
40 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); 70 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0);
41 Filter* filter = new ZLibInflateFilter(); 71 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 1);
42 if (filter == NULL || !filter->Init()) { 72 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj);
73 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 2);
74 uint8_t* dictionary = NULL;
75 if (!Dart_IsNull(dict_obj)) {
76 dictionary = copyDictionary(dict_obj);
77 }
78 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 3);
79 bool raw;
80 if (Dart_IsError(Dart_BooleanValue(raw_obj, &raw))) {
81 Dart_ThrowException(DartUtils::NewInternalError(
82 "Failed to get 'raw' parameter"));
83 }
84 Filter* filter = new ZLibInflateFilter(static_cast<int32_t>(window_bits),
85 dictionary, raw);
86 if (!filter->Init()) {
43 delete filter; 87 delete filter;
44 Dart_ThrowException(DartUtils::NewInternalError( 88 Dart_ThrowException(DartUtils::NewInternalError(
45 "Failed to create ZLibInflateFilter")); 89 "Failed to create ZLibInflateFilter"));
46 } 90 }
47 Dart_Handle result = Filter::SetFilterPointerNativeField(filter_obj, filter); 91 Dart_Handle result = Filter::SetFilterPointerNativeField(filter_obj, filter);
48 if (Dart_IsError(result)) { 92 if (Dart_IsError(result)) {
49 delete filter; 93 delete filter;
50 Dart_PropagateError(result); 94 Dart_PropagateError(result);
51 } 95 }
52 } 96 }
53 97
54 void FUNCTION_NAME(Filter_CreateZLibDeflate)(Dart_NativeArguments args) { 98 void FUNCTION_NAME(Filter_CreateZLibDeflate)(Dart_NativeArguments args) {
55 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); 99 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0);
56 Dart_Handle gzip_obj = Dart_GetNativeArgument(args, 1); 100 Dart_Handle gzip_obj = Dart_GetNativeArgument(args, 1);
101 bool gzip = DartUtils::GetBooleanValue(gzip_obj);
57 Dart_Handle level_obj = Dart_GetNativeArgument(args, 2); 102 Dart_Handle level_obj = Dart_GetNativeArgument(args, 2);
58 bool gzip; 103 int64_t level = DartUtils::GetInt64ValueCheckRange(level_obj, kMinInt32,
59 if (Dart_IsError(Dart_BooleanValue(gzip_obj, &gzip))) { 104 kMaxInt32);
60 Dart_ThrowException(DartUtils::NewInternalError( 105 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 3);
61 "Failed to get 'gzip' parameter")); 106 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj);
107 Dart_Handle mLevel_obj = Dart_GetNativeArgument(args, 4);
108 int64_t mem_level = DartUtils::GetIntegerValue(mLevel_obj);
109 Dart_Handle strategy_obj = Dart_GetNativeArgument(args, 5);
110 int64_t strategy = DartUtils::GetIntegerValue(strategy_obj);
111 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 6);
112 uint8_t* dictionary = NULL;
113 if (!Dart_IsNull(dict_obj)) {
114 dictionary = copyDictionary(dict_obj);
62 } 115 }
63 int64_t level = 0; 116 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 7);
64 Dart_Handle result = Dart_IntegerToInt64(level_obj, &level); 117 bool raw = DartUtils::GetBooleanValue(raw_obj);
65 if (Dart_IsError(result) || (level < kMinInt32) || (level > kMaxInt32)) { 118 Filter* filter = new ZLibDeflateFilter(gzip, static_cast<int32_t>(level),
66 Dart_ThrowException(DartUtils::NewInternalError( 119 static_cast<int32_t>(window_bits),
67 "Failed to get 'level' parameter")); 120 static_cast<int32_t>(mem_level),
68 } 121 static_cast<int32_t>(strategy),
69 Filter* filter = new ZLibDeflateFilter(gzip, static_cast<int32_t>(level)); 122 dictionary, raw);
70 if (filter == NULL || !filter->Init()) { 123 if (!filter->Init()) {
71 delete filter; 124 delete filter;
72 Dart_ThrowException(DartUtils::NewInternalError( 125 Dart_ThrowException(DartUtils::NewInternalError(
73 "Failed to create ZLibDeflateFilter")); 126 "Failed to create ZLibDeflateFilter"));
74 } 127 }
75 result = Filter::SetFilterPointerNativeField(filter_obj, filter); 128 Dart_Handle result = Filter::SetFilterPointerNativeField(filter_obj, filter);
76 if (Dart_IsError(result)) { 129 if (Dart_IsError(result)) {
77 delete filter; 130 delete filter;
78 Dart_PropagateError(result); 131 Dart_PropagateError(result);
79 } 132 }
80 } 133 }
81 134
82 void FUNCTION_NAME(Filter_Process)(Dart_NativeArguments args) { 135 void FUNCTION_NAME(Filter_Process)(Dart_NativeArguments args) {
83 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); 136 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0);
84 Filter* filter = GetFilter(filter_obj); 137 Filter* filter = GetFilter(filter_obj);
85 Dart_Handle data_obj = Dart_GetNativeArgument(args, 1); 138 Dart_Handle data_obj = Dart_GetNativeArgument(args, 1);
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 Dart_Handle Filter::GetFilterPointerNativeField(Dart_Handle filter, 231 Dart_Handle Filter::GetFilterPointerNativeField(Dart_Handle filter,
179 Filter** filter_pointer) { 232 Filter** filter_pointer) {
180 return Dart_GetNativeInstanceField( 233 return Dart_GetNativeInstanceField(
181 filter, 234 filter,
182 kFilterPointerNativeField, 235 kFilterPointerNativeField,
183 reinterpret_cast<intptr_t*>(filter_pointer)); 236 reinterpret_cast<intptr_t*>(filter_pointer));
184 } 237 }
185 238
186 239
187 ZLibDeflateFilter::~ZLibDeflateFilter() { 240 ZLibDeflateFilter::~ZLibDeflateFilter() {
241 delete[] dictionary_;
188 delete[] current_buffer_; 242 delete[] current_buffer_;
189 if (initialized()) deflateEnd(&stream_); 243 if (initialized()) deflateEnd(&stream_);
190 } 244 }
191 245
192 246
193 bool ZLibDeflateFilter::Init() { 247 bool ZLibDeflateFilter::Init() {
248 int window_bits = window_bits_;
249 if (raw_) {
250 window_bits = -window_bits;
251 } else if (gzip_) {
252 window_bits += kZLibFlagUseGZipHeader;
253 }
254 stream_.next_in = Z_NULL;
194 stream_.zalloc = Z_NULL; 255 stream_.zalloc = Z_NULL;
195 stream_.zfree = Z_NULL; 256 stream_.zfree = Z_NULL;
196 stream_.opaque = Z_NULL; 257 stream_.opaque = Z_NULL;
197 int result = deflateInit2( 258 int result = deflateInit2(&stream_, level_, Z_DEFLATED, window_bits,
198 &stream_, 259 mem_level_, strategy_);
199 level_, 260 if (result != Z_OK) {
200 Z_DEFLATED, 261 return false;
201 kZLibFlagWindowBits | (gzip_ ? kZLibFlagUseGZipHeader : 0),
202 kZlibFlagMemUsage,
203 Z_DEFAULT_STRATEGY);
204 if (result == Z_OK) {
205 set_initialized(true);
206 return true;
207 } 262 }
208 return false; 263 if (dictionary_ != NULL && !gzip_ && !raw_) {
264 result = deflateSetDictionary(&stream_, dictionary_, sizeof(dictionary_));
265 delete[] dictionary_;
266 dictionary_ = NULL;
267 if (result != Z_OK) {
268 return false;
269 }
270 }
271 set_initialized(true);
272 return true;
209 } 273 }
210 274
211 275
212 bool ZLibDeflateFilter::Process(uint8_t* data, intptr_t length) { 276 bool ZLibDeflateFilter::Process(uint8_t* data, intptr_t length) {
213 if (current_buffer_ != NULL) return false; 277 if (current_buffer_ != NULL) return false;
214 stream_.avail_in = length; 278 stream_.avail_in = length;
215 stream_.next_in = current_buffer_ = data; 279 stream_.next_in = current_buffer_ = data;
216 return true; 280 return true;
217 } 281 }
218 282
219 intptr_t ZLibDeflateFilter::Processed(uint8_t* buffer, 283 intptr_t ZLibDeflateFilter::Processed(uint8_t* buffer,
220 intptr_t length, 284 intptr_t length,
221 bool flush, 285 bool flush,
222 bool end) { 286 bool end) {
223 stream_.avail_out = length; 287 stream_.avail_out = length;
224 stream_.next_out = buffer; 288 stream_.next_out = buffer;
289 bool error = false;
225 switch (deflate(&stream_, 290 switch (deflate(&stream_,
226 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { 291 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) {
227 case Z_STREAM_END: 292 case Z_STREAM_END:
228 case Z_BUF_ERROR: 293 case Z_BUF_ERROR:
229 case Z_OK: { 294 case Z_OK: {
230 intptr_t processed = length - stream_.avail_out; 295 intptr_t processed = length - stream_.avail_out;
231 if (processed == 0) { 296 if (processed == 0) {
232 delete[] current_buffer_; 297 break;
233 current_buffer_ = NULL;
234 return 0;
235 } else {
236 // We processed data, should be called again.
237 return processed;
238 } 298 }
299 return processed;
239 } 300 }
240 301
241 default: 302 default:
242 case Z_STREAM_ERROR: 303 case Z_STREAM_ERROR:
243 // An error occoured. 304 error = true;
244 delete[] current_buffer_;
245 current_buffer_ = NULL;
246 return -1;
247 } 305 }
306
307 delete[] current_buffer_;
308 current_buffer_ = NULL;
309 // Either 0 Byte processed or error
310 return error ? -1 : 0;
248 } 311 }
249 312
250 313
251 ZLibInflateFilter::~ZLibInflateFilter() { 314 ZLibInflateFilter::~ZLibInflateFilter() {
315 delete[] dictionary_;
252 delete[] current_buffer_; 316 delete[] current_buffer_;
253 if (initialized()) inflateEnd(&stream_); 317 if (initialized()) inflateEnd(&stream_);
254 } 318 }
255 319
256 320
257 bool ZLibInflateFilter::Init() { 321 bool ZLibInflateFilter::Init() {
322 int window_bits = raw_ ?
323 -window_bits_ :
324 window_bits_ | kZLibFlagAcceptAnyHeader;
325
326 stream_.next_in = Z_NULL;
327 stream_.avail_in = 0;
258 stream_.zalloc = Z_NULL; 328 stream_.zalloc = Z_NULL;
259 stream_.zfree = Z_NULL; 329 stream_.zfree = Z_NULL;
260 stream_.opaque = Z_NULL; 330 stream_.opaque = Z_NULL;
261 int result = inflateInit2(&stream_, 331 int result = inflateInit2(&stream_, window_bits);
262 kZLibFlagWindowBits | kZLibFlagAcceptAnyHeader); 332 if (result != Z_OK) {
263 if (result == Z_OK) { 333 return false;
264 set_initialized(true);
265 return true;
266 } 334 }
267 return false; 335 set_initialized(true);
336 return true;
268 } 337 }
269 338
270 339
271 bool ZLibInflateFilter::Process(uint8_t* data, intptr_t length) { 340 bool ZLibInflateFilter::Process(uint8_t* data, intptr_t length) {
272 if (current_buffer_ != NULL) return false; 341 if (current_buffer_ != NULL) return false;
273 stream_.avail_in = length; 342 stream_.avail_in = length;
274 stream_.next_in = current_buffer_ = data; 343 stream_.next_in = current_buffer_ = data;
275 return true; 344 return true;
276 } 345 }
277 346
278 347
279 intptr_t ZLibInflateFilter::Processed(uint8_t* buffer, 348 intptr_t ZLibInflateFilter::Processed(uint8_t* buffer,
280 intptr_t length, 349 intptr_t length,
281 bool flush, 350 bool flush,
282 bool end) { 351 bool end) {
283 stream_.avail_out = length; 352 stream_.avail_out = length;
284 stream_.next_out = buffer; 353 stream_.next_out = buffer;
354 bool error = false;
285 switch (inflate(&stream_, 355 switch (inflate(&stream_,
286 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { 356 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) {
287 case Z_STREAM_END: 357 case Z_STREAM_END:
288 case Z_BUF_ERROR: 358 case Z_BUF_ERROR:
289 case Z_OK: { 359 case Z_OK: {
290 intptr_t processed = length - stream_.avail_out; 360 intptr_t processed = length - stream_.avail_out;
291 if (processed == 0) { 361 if (processed == 0) {
292 delete[] current_buffer_; 362 break;
293 current_buffer_ = NULL; 363 }
294 return 0; 364 return processed;
365 }
366
367 case Z_NEED_DICT:
368 if (dictionary_ == NULL) {
369 error = true;
295 } else { 370 } else {
296 // We processed data, should be called again. 371 int result = inflateSetDictionary(&stream_, dictionary_,
297 return processed; 372 sizeof(dictionary_));
373 delete[] dictionary_;
374 dictionary_ = NULL;
375 error = result != Z_OK;
298 } 376 }
299 } 377 if (error) {
378 break;
379 } else {
380 return Processed(buffer, length, flush, end);
381 }
300 382
301 default: 383 default:
302 case Z_MEM_ERROR: 384 case Z_MEM_ERROR:
303 case Z_NEED_DICT:
304 case Z_DATA_ERROR: 385 case Z_DATA_ERROR:
305 case Z_STREAM_ERROR: 386 case Z_STREAM_ERROR:
306 // An error occoured. 387 error = true;
307 delete[] current_buffer_;
308 current_buffer_ = NULL;
309 return -1;
310 } 388 }
389
390 delete[] current_buffer_;
391 current_buffer_ = NULL;
392 // Either 0 Byte processed or error
393 return error ? -1 : 0;
311 } 394 }
312 395
313 } // namespace bin 396 } // namespace bin
314 } // namespace dart 397 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/filter.h ('k') | runtime/bin/filter_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698