| 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 #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 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 65 |
| 66 return dictionary; | 66 return dictionary; |
| 67 } | 67 } |
| 68 | 68 |
| 69 void FUNCTION_NAME(Filter_CreateZLibInflate)(Dart_NativeArguments args) { | 69 void FUNCTION_NAME(Filter_CreateZLibInflate)(Dart_NativeArguments args) { |
| 70 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); | 70 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); |
| 71 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 1); | 71 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 1); |
| 72 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj); | 72 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj); |
| 73 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 2); | 73 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 2); |
| 74 uint8_t* dictionary = NULL; | 74 uint8_t* dictionary = NULL; |
| 75 intptr_t dictionary_length = 0; |
| 75 if (!Dart_IsNull(dict_obj)) { | 76 if (!Dart_IsNull(dict_obj)) { |
| 76 dictionary = copyDictionary(dict_obj); | 77 dictionary = copyDictionary(dict_obj); |
| 78 if (dictionary != NULL) { |
| 79 dictionary_length = 0; |
| 80 Dart_ListLength(dict_obj, &dictionary_length); |
| 81 } |
| 77 } | 82 } |
| 78 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 3); | 83 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 3); |
| 79 bool raw; | 84 bool raw; |
| 80 if (Dart_IsError(Dart_BooleanValue(raw_obj, &raw))) { | 85 if (Dart_IsError(Dart_BooleanValue(raw_obj, &raw))) { |
| 81 Dart_ThrowException(DartUtils::NewInternalError( | 86 Dart_ThrowException(DartUtils::NewInternalError( |
| 82 "Failed to get 'raw' parameter")); | 87 "Failed to get 'raw' parameter")); |
| 83 } | 88 } |
| 84 Filter* filter = new ZLibInflateFilter(static_cast<int32_t>(window_bits), | 89 Filter* filter = new ZLibInflateFilter(static_cast<int32_t>(window_bits), |
| 85 dictionary, raw); | 90 dictionary, dictionary_length, raw); |
| 86 if (!filter->Init()) { | 91 if (!filter->Init()) { |
| 87 delete filter; | 92 delete filter; |
| 88 Dart_ThrowException(DartUtils::NewInternalError( | 93 Dart_ThrowException(DartUtils::NewInternalError( |
| 89 "Failed to create ZLibInflateFilter")); | 94 "Failed to create ZLibInflateFilter")); |
| 90 } | 95 } |
| 91 Dart_Handle result = Filter::SetFilterPointerNativeField(filter_obj, filter); | 96 Dart_Handle result = Filter::SetFilterPointerNativeField(filter_obj, filter); |
| 92 if (Dart_IsError(result)) { | 97 if (Dart_IsError(result)) { |
| 93 delete filter; | 98 delete filter; |
| 94 Dart_PropagateError(result); | 99 Dart_PropagateError(result); |
| 95 } | 100 } |
| 96 } | 101 } |
| 97 | 102 |
| 98 void FUNCTION_NAME(Filter_CreateZLibDeflate)(Dart_NativeArguments args) { | 103 void FUNCTION_NAME(Filter_CreateZLibDeflate)(Dart_NativeArguments args) { |
| 99 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); | 104 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); |
| 100 Dart_Handle gzip_obj = Dart_GetNativeArgument(args, 1); | 105 Dart_Handle gzip_obj = Dart_GetNativeArgument(args, 1); |
| 101 bool gzip = DartUtils::GetBooleanValue(gzip_obj); | 106 bool gzip = DartUtils::GetBooleanValue(gzip_obj); |
| 102 Dart_Handle level_obj = Dart_GetNativeArgument(args, 2); | 107 Dart_Handle level_obj = Dart_GetNativeArgument(args, 2); |
| 103 int64_t level = DartUtils::GetInt64ValueCheckRange(level_obj, kMinInt32, | 108 int64_t level = DartUtils::GetInt64ValueCheckRange(level_obj, kMinInt32, |
| 104 kMaxInt32); | 109 kMaxInt32); |
| 105 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 3); | 110 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 3); |
| 106 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj); | 111 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj); |
| 107 Dart_Handle mLevel_obj = Dart_GetNativeArgument(args, 4); | 112 Dart_Handle mLevel_obj = Dart_GetNativeArgument(args, 4); |
| 108 int64_t mem_level = DartUtils::GetIntegerValue(mLevel_obj); | 113 int64_t mem_level = DartUtils::GetIntegerValue(mLevel_obj); |
| 109 Dart_Handle strategy_obj = Dart_GetNativeArgument(args, 5); | 114 Dart_Handle strategy_obj = Dart_GetNativeArgument(args, 5); |
| 110 int64_t strategy = DartUtils::GetIntegerValue(strategy_obj); | 115 int64_t strategy = DartUtils::GetIntegerValue(strategy_obj); |
| 111 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 6); | 116 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 6); |
| 112 uint8_t* dictionary = NULL; | 117 uint8_t* dictionary = NULL; |
| 118 intptr_t dictionary_length = 0; |
| 113 if (!Dart_IsNull(dict_obj)) { | 119 if (!Dart_IsNull(dict_obj)) { |
| 114 dictionary = copyDictionary(dict_obj); | 120 dictionary = copyDictionary(dict_obj); |
| 121 if (dictionary != NULL) { |
| 122 dictionary_length = 0; |
| 123 Dart_ListLength(dict_obj, &dictionary_length); |
| 124 } |
| 115 } | 125 } |
| 116 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 7); | 126 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 7); |
| 117 bool raw = DartUtils::GetBooleanValue(raw_obj); | 127 bool raw = DartUtils::GetBooleanValue(raw_obj); |
| 118 Filter* filter = new ZLibDeflateFilter(gzip, static_cast<int32_t>(level), | 128 Filter* filter = new ZLibDeflateFilter(gzip, static_cast<int32_t>(level), |
| 119 static_cast<int32_t>(window_bits), | 129 static_cast<int32_t>(window_bits), |
| 120 static_cast<int32_t>(mem_level), | 130 static_cast<int32_t>(mem_level), |
| 121 static_cast<int32_t>(strategy), | 131 static_cast<int32_t>(strategy), |
| 122 dictionary, raw); | 132 dictionary, dictionary_length, raw); |
| 123 if (!filter->Init()) { | 133 if (!filter->Init()) { |
| 124 delete filter; | 134 delete filter; |
| 125 Dart_ThrowException(DartUtils::NewInternalError( | 135 Dart_ThrowException(DartUtils::NewInternalError( |
| 126 "Failed to create ZLibDeflateFilter")); | 136 "Failed to create ZLibDeflateFilter")); |
| 127 } | 137 } |
| 128 Dart_Handle result = Filter::SetFilterPointerNativeField(filter_obj, filter); | 138 Dart_Handle result = Filter::SetFilterPointerNativeField(filter_obj, filter); |
| 129 if (Dart_IsError(result)) { | 139 if (Dart_IsError(result)) { |
| 130 delete filter; | 140 delete filter; |
| 131 Dart_PropagateError(result); | 141 Dart_PropagateError(result); |
| 132 } | 142 } |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 stream_.next_in = Z_NULL; | 264 stream_.next_in = Z_NULL; |
| 255 stream_.zalloc = Z_NULL; | 265 stream_.zalloc = Z_NULL; |
| 256 stream_.zfree = Z_NULL; | 266 stream_.zfree = Z_NULL; |
| 257 stream_.opaque = Z_NULL; | 267 stream_.opaque = Z_NULL; |
| 258 int result = deflateInit2(&stream_, level_, Z_DEFLATED, window_bits, | 268 int result = deflateInit2(&stream_, level_, Z_DEFLATED, window_bits, |
| 259 mem_level_, strategy_); | 269 mem_level_, strategy_); |
| 260 if (result != Z_OK) { | 270 if (result != Z_OK) { |
| 261 return false; | 271 return false; |
| 262 } | 272 } |
| 263 if (dictionary_ != NULL && !gzip_ && !raw_) { | 273 if (dictionary_ != NULL && !gzip_ && !raw_) { |
| 264 result = deflateSetDictionary(&stream_, dictionary_, sizeof(dictionary_)); | 274 result = deflateSetDictionary(&stream_, dictionary_, dictionary_length_); |
| 265 delete[] dictionary_; | 275 delete[] dictionary_; |
| 266 dictionary_ = NULL; | 276 dictionary_ = NULL; |
| 267 if (result != Z_OK) { | 277 if (result != Z_OK) { |
| 268 return false; | 278 return false; |
| 269 } | 279 } |
| 270 } | 280 } |
| 271 set_initialized(true); | 281 set_initialized(true); |
| 272 return true; | 282 return true; |
| 273 } | 283 } |
| 274 | 284 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 } | 355 } |
| 346 | 356 |
| 347 | 357 |
| 348 intptr_t ZLibInflateFilter::Processed(uint8_t* buffer, | 358 intptr_t ZLibInflateFilter::Processed(uint8_t* buffer, |
| 349 intptr_t length, | 359 intptr_t length, |
| 350 bool flush, | 360 bool flush, |
| 351 bool end) { | 361 bool end) { |
| 352 stream_.avail_out = length; | 362 stream_.avail_out = length; |
| 353 stream_.next_out = buffer; | 363 stream_.next_out = buffer; |
| 354 bool error = false; | 364 bool error = false; |
| 355 switch (inflate(&stream_, | 365 int v; |
| 366 switch (v = inflate(&stream_, |
| 356 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { | 367 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { |
| 357 case Z_STREAM_END: | 368 case Z_STREAM_END: |
| 358 case Z_BUF_ERROR: | 369 case Z_BUF_ERROR: |
| 359 case Z_OK: { | 370 case Z_OK: { |
| 360 intptr_t processed = length - stream_.avail_out; | 371 intptr_t processed = length - stream_.avail_out; |
| 361 if (processed == 0) { | 372 if (processed == 0) { |
| 362 break; | 373 break; |
| 363 } | 374 } |
| 364 return processed; | 375 return processed; |
| 365 } | 376 } |
| 366 | 377 |
| 367 case Z_NEED_DICT: | 378 case Z_NEED_DICT: |
| 368 if (dictionary_ == NULL) { | 379 if (dictionary_ == NULL) { |
| 369 error = true; | 380 error = true; |
| 370 } else { | 381 } else { |
| 371 int result = inflateSetDictionary(&stream_, dictionary_, | 382 int result = inflateSetDictionary(&stream_, dictionary_, |
| 372 sizeof(dictionary_)); | 383 dictionary_length_); |
| 373 delete[] dictionary_; | 384 delete[] dictionary_; |
| 374 dictionary_ = NULL; | 385 dictionary_ = NULL; |
| 375 error = result != Z_OK; | 386 error = result != Z_OK; |
| 376 } | 387 } |
| 377 if (error) { | 388 if (error) { |
| 378 break; | 389 break; |
| 379 } else { | 390 } else { |
| 380 return Processed(buffer, length, flush, end); | 391 return Processed(buffer, length, flush, end); |
| 381 } | 392 } |
| 382 | 393 |
| 383 default: | 394 default: |
| 384 case Z_MEM_ERROR: | 395 case Z_MEM_ERROR: |
| 385 case Z_DATA_ERROR: | 396 case Z_DATA_ERROR: |
| 386 case Z_STREAM_ERROR: | 397 case Z_STREAM_ERROR: |
| 387 error = true; | 398 error = true; |
| 388 } | 399 } |
| 389 | 400 |
| 390 delete[] current_buffer_; | 401 delete[] current_buffer_; |
| 391 current_buffer_ = NULL; | 402 current_buffer_ = NULL; |
| 392 // Either 0 Byte processed or error | 403 // Either 0 Byte processed or error |
| 393 return error ? -1 : 0; | 404 return error ? -1 : 0; |
| 394 } | 405 } |
| 395 | 406 |
| 396 } // namespace bin | 407 } // namespace bin |
| 397 } // namespace dart | 408 } // namespace dart |
| OLD | NEW |