OLD | NEW |
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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
6 | 6 |
7 #include "bin/filter.h" | 7 #include "bin/filter.h" |
8 | 8 |
9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
10 #include "bin/io_buffer.h" | 10 #include "bin/io_buffer.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 Dart_Handle err = Dart_ListLength(dictionary_obj, &size); | 45 Dart_Handle err = Dart_ListLength(dictionary_obj, &size); |
46 if (Dart_IsError(err)) { | 46 if (Dart_IsError(err)) { |
47 return err; | 47 return err; |
48 } | 48 } |
49 | 49 |
50 uint8_t* result = new uint8_t[size]; | 50 uint8_t* result = new uint8_t[size]; |
51 if (result == NULL) { | 51 if (result == NULL) { |
52 return Dart_NewApiError("Could not allocate new dictionary"); | 52 return Dart_NewApiError("Could not allocate new dictionary"); |
53 } | 53 } |
54 | 54 |
55 err = Dart_TypedDataAcquireData( | 55 err = Dart_TypedDataAcquireData(dictionary_obj, &type, |
56 dictionary_obj, &type, reinterpret_cast<void**>(&src), &size); | 56 reinterpret_cast<void**>(&src), &size); |
57 if (!Dart_IsError(err)) { | 57 if (!Dart_IsError(err)) { |
58 memmove(result, src, size); | 58 memmove(result, src, size); |
59 Dart_TypedDataReleaseData(dictionary_obj); | 59 Dart_TypedDataReleaseData(dictionary_obj); |
60 } else { | 60 } else { |
61 err = Dart_ListGetAsBytes(dictionary_obj, 0, result, size); | 61 err = Dart_ListGetAsBytes(dictionary_obj, 0, result, size); |
62 if (Dart_IsError(err)) { | 62 if (Dart_IsError(err)) { |
63 delete[] result; | 63 delete[] result; |
64 return err; | 64 return err; |
65 } | 65 } |
66 } | 66 } |
(...skipping 25 matching lines...) Expand all Loading... |
92 if (Dart_IsError(err)) { | 92 if (Dart_IsError(err)) { |
93 delete[] dictionary; | 93 delete[] dictionary; |
94 Dart_PropagateError(err); | 94 Dart_PropagateError(err); |
95 } | 95 } |
96 } | 96 } |
97 | 97 |
98 ZLibInflateFilter* filter = new ZLibInflateFilter( | 98 ZLibInflateFilter* filter = new ZLibInflateFilter( |
99 static_cast<int32_t>(window_bits), dictionary, dictionary_length, raw); | 99 static_cast<int32_t>(window_bits), dictionary, dictionary_length, raw); |
100 if (filter == NULL) { | 100 if (filter == NULL) { |
101 delete[] dictionary; | 101 delete[] dictionary; |
102 Dart_PropagateError(Dart_NewApiError( | 102 Dart_PropagateError( |
103 "Could not allocate ZLibInflateFilter")); | 103 Dart_NewApiError("Could not allocate ZLibInflateFilter")); |
104 } | 104 } |
105 if (!filter->Init()) { | 105 if (!filter->Init()) { |
106 delete filter; | 106 delete filter; |
107 Dart_ThrowException(DartUtils::NewInternalError( | 107 Dart_ThrowException( |
108 "Failed to create ZLibInflateFilter")); | 108 DartUtils::NewInternalError("Failed to create ZLibInflateFilter")); |
109 } | 109 } |
110 err = Filter::SetFilterAndCreateFinalizer( | 110 err = Filter::SetFilterAndCreateFinalizer( |
111 filter_obj, filter, sizeof(*filter) + dictionary_length); | 111 filter_obj, filter, sizeof(*filter) + dictionary_length); |
112 if (Dart_IsError(err)) { | 112 if (Dart_IsError(err)) { |
113 delete filter; | 113 delete filter; |
114 Dart_PropagateError(err); | 114 Dart_PropagateError(err); |
115 } | 115 } |
116 } | 116 } |
117 | 117 |
118 | 118 |
119 void FUNCTION_NAME(Filter_CreateZLibDeflate)(Dart_NativeArguments args) { | 119 void FUNCTION_NAME(Filter_CreateZLibDeflate)(Dart_NativeArguments args) { |
120 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); | 120 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); |
121 Dart_Handle gzip_obj = Dart_GetNativeArgument(args, 1); | 121 Dart_Handle gzip_obj = Dart_GetNativeArgument(args, 1); |
122 bool gzip = DartUtils::GetBooleanValue(gzip_obj); | 122 bool gzip = DartUtils::GetBooleanValue(gzip_obj); |
123 Dart_Handle level_obj = Dart_GetNativeArgument(args, 2); | 123 Dart_Handle level_obj = Dart_GetNativeArgument(args, 2); |
124 int64_t level = DartUtils::GetInt64ValueCheckRange(level_obj, kMinInt32, | 124 int64_t level = |
125 kMaxInt32); | 125 DartUtils::GetInt64ValueCheckRange(level_obj, kMinInt32, kMaxInt32); |
126 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 3); | 126 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 3); |
127 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj); | 127 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj); |
128 Dart_Handle mLevel_obj = Dart_GetNativeArgument(args, 4); | 128 Dart_Handle mLevel_obj = Dart_GetNativeArgument(args, 4); |
129 int64_t mem_level = DartUtils::GetIntegerValue(mLevel_obj); | 129 int64_t mem_level = DartUtils::GetIntegerValue(mLevel_obj); |
130 Dart_Handle strategy_obj = Dart_GetNativeArgument(args, 5); | 130 Dart_Handle strategy_obj = Dart_GetNativeArgument(args, 5); |
131 int64_t strategy = DartUtils::GetIntegerValue(strategy_obj); | 131 int64_t strategy = DartUtils::GetIntegerValue(strategy_obj); |
132 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 6); | 132 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 6); |
133 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 7); | 133 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 7); |
134 bool raw = DartUtils::GetBooleanValue(raw_obj); | 134 bool raw = DartUtils::GetBooleanValue(raw_obj); |
135 | 135 |
136 Dart_Handle err; | 136 Dart_Handle err; |
137 uint8_t* dictionary = NULL; | 137 uint8_t* dictionary = NULL; |
138 intptr_t dictionary_length = 0; | 138 intptr_t dictionary_length = 0; |
139 if (!Dart_IsNull(dict_obj)) { | 139 if (!Dart_IsNull(dict_obj)) { |
140 err = CopyDictionary(dict_obj, &dictionary); | 140 err = CopyDictionary(dict_obj, &dictionary); |
141 if (Dart_IsError(err)) { | 141 if (Dart_IsError(err)) { |
142 Dart_PropagateError(err); | 142 Dart_PropagateError(err); |
143 } | 143 } |
144 ASSERT(dictionary != NULL); | 144 ASSERT(dictionary != NULL); |
145 dictionary_length = 0; | 145 dictionary_length = 0; |
146 err = Dart_ListLength(dict_obj, &dictionary_length); | 146 err = Dart_ListLength(dict_obj, &dictionary_length); |
147 if (Dart_IsError(err)) { | 147 if (Dart_IsError(err)) { |
148 delete[] dictionary; | 148 delete[] dictionary; |
149 Dart_PropagateError(err); | 149 Dart_PropagateError(err); |
150 } | 150 } |
151 } | 151 } |
152 | 152 |
153 ZLibDeflateFilter* filter = new ZLibDeflateFilter( | 153 ZLibDeflateFilter* filter = new ZLibDeflateFilter( |
154 gzip, | 154 gzip, static_cast<int32_t>(level), static_cast<int32_t>(window_bits), |
155 static_cast<int32_t>(level), | 155 static_cast<int32_t>(mem_level), static_cast<int32_t>(strategy), |
156 static_cast<int32_t>(window_bits), | |
157 static_cast<int32_t>(mem_level), | |
158 static_cast<int32_t>(strategy), | |
159 dictionary, dictionary_length, raw); | 156 dictionary, dictionary_length, raw); |
160 if (filter == NULL) { | 157 if (filter == NULL) { |
161 delete[] dictionary; | 158 delete[] dictionary; |
162 Dart_PropagateError(Dart_NewApiError( | 159 Dart_PropagateError( |
163 "Could not allocate ZLibDeflateFilter")); | 160 Dart_NewApiError("Could not allocate ZLibDeflateFilter")); |
164 } | 161 } |
165 if (!filter->Init()) { | 162 if (!filter->Init()) { |
166 delete filter; | 163 delete filter; |
167 Dart_ThrowException(DartUtils::NewInternalError( | 164 Dart_ThrowException( |
168 "Failed to create ZLibDeflateFilter")); | 165 DartUtils::NewInternalError("Failed to create ZLibDeflateFilter")); |
169 } | 166 } |
170 Dart_Handle result = Filter::SetFilterAndCreateFinalizer( | 167 Dart_Handle result = Filter::SetFilterAndCreateFinalizer( |
171 filter_obj, filter, sizeof(*filter) + dictionary_length); | 168 filter_obj, filter, sizeof(*filter) + dictionary_length); |
172 if (Dart_IsError(result)) { | 169 if (Dart_IsError(result)) { |
173 delete filter; | 170 delete filter; |
174 Dart_PropagateError(result); | 171 Dart_PropagateError(result); |
175 } | 172 } |
176 } | 173 } |
177 | 174 |
178 | 175 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 bool flush = DartUtils::GetBooleanValue(flush_obj); | 237 bool flush = DartUtils::GetBooleanValue(flush_obj); |
241 Dart_Handle end_obj = Dart_GetNativeArgument(args, 2); | 238 Dart_Handle end_obj = Dart_GetNativeArgument(args, 2); |
242 bool end = DartUtils::GetBooleanValue(end_obj); | 239 bool end = DartUtils::GetBooleanValue(end_obj); |
243 | 240 |
244 Filter* filter = NULL; | 241 Filter* filter = NULL; |
245 Dart_Handle err = GetFilter(filter_obj, &filter); | 242 Dart_Handle err = GetFilter(filter_obj, &filter); |
246 if (Dart_IsError(err)) { | 243 if (Dart_IsError(err)) { |
247 Dart_PropagateError(err); | 244 Dart_PropagateError(err); |
248 } | 245 } |
249 | 246 |
250 intptr_t read = filter->Processed(filter->processed_buffer(), | 247 intptr_t read = filter->Processed( |
251 filter->processed_buffer_size(), | 248 filter->processed_buffer(), filter->processed_buffer_size(), flush, end); |
252 flush, | |
253 end); | |
254 if (read < 0) { | 249 if (read < 0) { |
255 Dart_ThrowException(DartUtils::NewInternalError( | 250 Dart_ThrowException(DartUtils::NewInternalError("Filter error, bad data")); |
256 "Filter error, bad data")); | |
257 } else if (read == 0) { | 251 } else if (read == 0) { |
258 Dart_SetReturnValue(args, Dart_Null()); | 252 Dart_SetReturnValue(args, Dart_Null()); |
259 } else { | 253 } else { |
260 uint8_t* io_buffer; | 254 uint8_t* io_buffer; |
261 Dart_Handle result = IOBuffer::Allocate(read, &io_buffer); | 255 Dart_Handle result = IOBuffer::Allocate(read, &io_buffer); |
262 memmove(io_buffer, filter->processed_buffer(), read); | 256 memmove(io_buffer, filter->processed_buffer(), read); |
263 Dart_SetReturnValue(args, result); | 257 Dart_SetReturnValue(args, result); |
264 } | 258 } |
265 } | 259 } |
266 | 260 |
267 | 261 |
268 static void DeleteFilter( | 262 static void DeleteFilter(void* isolate_data, |
269 void* isolate_data, | 263 Dart_WeakPersistentHandle handle, |
270 Dart_WeakPersistentHandle handle, | 264 void* filter_pointer) { |
271 void* filter_pointer) { | |
272 Filter* filter = reinterpret_cast<Filter*>(filter_pointer); | 265 Filter* filter = reinterpret_cast<Filter*>(filter_pointer); |
273 delete filter; | 266 delete filter; |
274 } | 267 } |
275 | 268 |
276 | 269 |
277 Dart_Handle Filter::SetFilterAndCreateFinalizer(Dart_Handle filter, | 270 Dart_Handle Filter::SetFilterAndCreateFinalizer(Dart_Handle filter, |
278 Filter* filter_pointer, | 271 Filter* filter_pointer, |
279 intptr_t size) { | 272 intptr_t size) { |
280 Dart_Handle err = Dart_SetNativeInstanceField( | 273 Dart_Handle err = |
281 filter, | 274 Dart_SetNativeInstanceField(filter, kFilterPointerNativeField, |
282 kFilterPointerNativeField, | 275 reinterpret_cast<intptr_t>(filter_pointer)); |
283 reinterpret_cast<intptr_t>(filter_pointer)); | |
284 if (Dart_IsError(err)) { | 276 if (Dart_IsError(err)) { |
285 return err; | 277 return err; |
286 } | 278 } |
287 Dart_NewWeakPersistentHandle(filter, | 279 Dart_NewWeakPersistentHandle(filter, reinterpret_cast<void*>(filter_pointer), |
288 reinterpret_cast<void*>(filter_pointer), | 280 size, DeleteFilter); |
289 size, | |
290 DeleteFilter); | |
291 return err; | 281 return err; |
292 } | 282 } |
293 | 283 |
294 | 284 |
295 Dart_Handle Filter::GetFilterNativeField(Dart_Handle filter, | 285 Dart_Handle Filter::GetFilterNativeField(Dart_Handle filter, |
296 Filter** filter_pointer) { | 286 Filter** filter_pointer) { |
297 return Dart_GetNativeInstanceField( | 287 return Dart_GetNativeInstanceField( |
298 filter, | 288 filter, kFilterPointerNativeField, |
299 kFilterPointerNativeField, | |
300 reinterpret_cast<intptr_t*>(filter_pointer)); | 289 reinterpret_cast<intptr_t*>(filter_pointer)); |
301 } | 290 } |
302 | 291 |
303 | 292 |
304 ZLibDeflateFilter::~ZLibDeflateFilter() { | 293 ZLibDeflateFilter::~ZLibDeflateFilter() { |
305 delete[] dictionary_; | 294 delete[] dictionary_; |
306 delete[] current_buffer_; | 295 delete[] current_buffer_; |
307 if (initialized()) { | 296 if (initialized()) { |
308 deflateEnd(&stream_); | 297 deflateEnd(&stream_); |
309 } | 298 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 return true; | 337 return true; |
349 } | 338 } |
350 | 339 |
351 intptr_t ZLibDeflateFilter::Processed(uint8_t* buffer, | 340 intptr_t ZLibDeflateFilter::Processed(uint8_t* buffer, |
352 intptr_t length, | 341 intptr_t length, |
353 bool flush, | 342 bool flush, |
354 bool end) { | 343 bool end) { |
355 stream_.avail_out = length; | 344 stream_.avail_out = length; |
356 stream_.next_out = buffer; | 345 stream_.next_out = buffer; |
357 bool error = false; | 346 bool error = false; |
358 switch (deflate(&stream_, | 347 switch ( |
359 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { | 348 deflate(&stream_, end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { |
360 case Z_STREAM_END: | 349 case Z_STREAM_END: |
361 case Z_BUF_ERROR: | 350 case Z_BUF_ERROR: |
362 case Z_OK: { | 351 case Z_OK: { |
363 intptr_t processed = length - stream_.avail_out; | 352 intptr_t processed = length - stream_.avail_out; |
364 if (processed == 0) { | 353 if (processed == 0) { |
365 break; | 354 break; |
366 } | 355 } |
367 return processed; | 356 return processed; |
368 } | 357 } |
369 | 358 |
370 default: | 359 default: |
371 case Z_STREAM_ERROR: | 360 case Z_STREAM_ERROR: |
372 error = true; | 361 error = true; |
373 } | 362 } |
374 | 363 |
375 delete[] current_buffer_; | 364 delete[] current_buffer_; |
376 current_buffer_ = NULL; | 365 current_buffer_ = NULL; |
377 // Either 0 Byte processed or error | 366 // Either 0 Byte processed or error |
378 return error ? -1 : 0; | 367 return error ? -1 : 0; |
379 } | 368 } |
380 | 369 |
381 | 370 |
382 ZLibInflateFilter::~ZLibInflateFilter() { | 371 ZLibInflateFilter::~ZLibInflateFilter() { |
383 delete[] dictionary_; | 372 delete[] dictionary_; |
384 delete[] current_buffer_; | 373 delete[] current_buffer_; |
385 if (initialized()) { | 374 if (initialized()) { |
386 inflateEnd(&stream_); | 375 inflateEnd(&stream_); |
387 } | 376 } |
388 } | 377 } |
389 | 378 |
390 | 379 |
391 bool ZLibInflateFilter::Init() { | 380 bool ZLibInflateFilter::Init() { |
392 int window_bits = raw_ ? | 381 int window_bits = |
393 -window_bits_ : | 382 raw_ ? -window_bits_ : window_bits_ | kZLibFlagAcceptAnyHeader; |
394 window_bits_ | kZLibFlagAcceptAnyHeader; | |
395 | 383 |
396 stream_.next_in = Z_NULL; | 384 stream_.next_in = Z_NULL; |
397 stream_.avail_in = 0; | 385 stream_.avail_in = 0; |
398 stream_.zalloc = Z_NULL; | 386 stream_.zalloc = Z_NULL; |
399 stream_.zfree = Z_NULL; | 387 stream_.zfree = Z_NULL; |
400 stream_.opaque = Z_NULL; | 388 stream_.opaque = Z_NULL; |
401 int result = inflateInit2(&stream_, window_bits); | 389 int result = inflateInit2(&stream_, window_bits); |
402 if (result != Z_OK) { | 390 if (result != Z_OK) { |
403 return false; | 391 return false; |
404 } | 392 } |
(...skipping 14 matching lines...) Expand all Loading... |
419 | 407 |
420 intptr_t ZLibInflateFilter::Processed(uint8_t* buffer, | 408 intptr_t ZLibInflateFilter::Processed(uint8_t* buffer, |
421 intptr_t length, | 409 intptr_t length, |
422 bool flush, | 410 bool flush, |
423 bool end) { | 411 bool end) { |
424 stream_.avail_out = length; | 412 stream_.avail_out = length; |
425 stream_.next_out = buffer; | 413 stream_.next_out = buffer; |
426 bool error = false; | 414 bool error = false; |
427 int v; | 415 int v; |
428 switch (v = inflate(&stream_, | 416 switch (v = inflate(&stream_, |
429 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { | 417 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { |
430 case Z_STREAM_END: | 418 case Z_STREAM_END: |
431 case Z_BUF_ERROR: | 419 case Z_BUF_ERROR: |
432 case Z_OK: { | 420 case Z_OK: { |
433 intptr_t processed = length - stream_.avail_out; | 421 intptr_t processed = length - stream_.avail_out; |
434 if (processed == 0) { | 422 if (processed == 0) { |
435 break; | 423 break; |
436 } | 424 } |
437 return processed; | 425 return processed; |
438 } | 426 } |
439 | 427 |
440 case Z_NEED_DICT: | 428 case Z_NEED_DICT: |
441 if (dictionary_ == NULL) { | 429 if (dictionary_ == NULL) { |
442 error = true; | 430 error = true; |
443 } else { | 431 } else { |
444 int result = inflateSetDictionary(&stream_, dictionary_, | 432 int result = |
445 dictionary_length_); | 433 inflateSetDictionary(&stream_, dictionary_, dictionary_length_); |
446 delete[] dictionary_; | 434 delete[] dictionary_; |
447 dictionary_ = NULL; | 435 dictionary_ = NULL; |
448 error = result != Z_OK; | 436 error = result != Z_OK; |
449 } | 437 } |
450 if (error) { | 438 if (error) { |
451 break; | 439 break; |
452 } else { | 440 } else { |
453 return Processed(buffer, length, flush, end); | 441 return Processed(buffer, length, flush, end); |
454 } | 442 } |
455 | 443 |
456 default: | 444 default: |
457 case Z_MEM_ERROR: | 445 case Z_MEM_ERROR: |
458 case Z_DATA_ERROR: | 446 case Z_DATA_ERROR: |
459 case Z_STREAM_ERROR: | 447 case Z_STREAM_ERROR: |
460 error = true; | 448 error = true; |
461 } | 449 } |
462 | 450 |
463 delete[] current_buffer_; | 451 delete[] current_buffer_; |
464 current_buffer_ = NULL; | 452 current_buffer_ = NULL; |
465 // Either 0 Byte processed or error | 453 // Either 0 Byte processed or error |
466 return error ? -1 : 0; | 454 return error ? -1 : 0; |
467 } | 455 } |
468 | 456 |
469 } // namespace bin | 457 } // namespace bin |
470 } // namespace dart | 458 } // namespace dart |
471 | 459 |
472 #endif // !defined(DART_IO_DISABLED) | 460 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |