Chromium Code Reviews| Index: runtime/bin/filter.cc |
| diff --git a/runtime/bin/filter.cc b/runtime/bin/filter.cc |
| index 34c0ee2f1cb62248e7f5a10cb08b207314827151..e6ada5050f2b16d3e08a4ec31263bcb3c9597197 100644 |
| --- a/runtime/bin/filter.cc |
| +++ b/runtime/bin/filter.cc |
| @@ -2,18 +2,17 @@ |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| +#include <stdio.h> |
| + |
| #include "bin/dartutils.h" |
| #include "bin/filter.h" |
| #include "bin/io_buffer.h" |
| #include "include/dart_api.h" |
| - |
| namespace dart { |
| namespace bin { |
| -const int kZlibFlagMemUsage = 8; |
| -const int kZLibFlagWindowBits = 15; |
| const int kZLibFlagUseGZipHeader = 16; |
| const int kZLibFlagAcceptAnyHeader = 32; |
| @@ -36,9 +35,66 @@ void EndFilter(Dart_Handle filter_obj, Filter* filter) { |
| delete filter; |
| } |
| +Dart_Handle AllocateDictionary(Dart_Handle dictionary_obj) { |
|
Anders Johnsen
2014/01/27 12:27:45
static
vicb
2014/01/27 15:14:19
fixed (and updated the 2 above functions)
|
| + uint8_t* dst = NULL; |
| + uint8_t* src = NULL; |
| + intptr_t length; |
| + Dart_TypedData_Type type; |
| + |
| + printf("+allocate"); |
|
Anders Johnsen
2014/01/27 12:27:45
Please remove debug prints in this file.
|
| + |
| + if (Dart_IsError(Dart_ListLength(dictionary_obj, &length))) { |
| + Dart_ThrowException(DartUtils::NewInternalError( |
| + "Failed to get the zlib dictionary length")); |
| + } |
| + |
| + Dart_Handle dictionary = IOBuffer::Allocate(length, &dst); |
|
Anders Johnsen
2014/01/27 12:27:45
Wait with allocation until you have src?
vicb
2014/01/27 15:14:19
I need dst in both if and else branches...
|
| + |
| + if (Dart_IsError(dictionary)) { |
| + Dart_ThrowException(DartUtils::NewInternalError( |
| + "Failed to allocate buffer for the zlib dictionary")); |
| + } |
| + |
| + Dart_Handle result = Dart_TypedDataAcquireData( |
| + dictionary_obj, &type, reinterpret_cast<void**>(&src), &length); |
| + if (!Dart_IsError(result)) { |
| + memmove(dst, src, length); |
| + Dart_TypedDataReleaseData(dictionary_obj); |
| + } else { |
| + if (Dart_IsError(Dart_ListGetAsBytes(dictionary_obj, 0, dst, length))) { |
| + Dart_ThrowException(DartUtils::NewInternalError( |
| + "Failed to get the zlib dictionary bytes")); |
| + } |
| + } |
| + |
| + printf("-allocate"); |
| + |
| + return dictionary; |
| +} |
| + |
| void FUNCTION_NAME(Filter_CreateZLibInflate)(Dart_NativeArguments args) { |
| + setbuf(stdout, NULL); |
| + printf("+Filter_CreateZLibInflate+\n"); |
| Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); |
| - Filter* filter = new ZLibInflateFilter(); |
| + Dart_Handle wBits_obj = Dart_GetNativeArgument(args, 1); |
| + int64_t window_bits; |
| + if (Dart_IsError(Dart_IntegerToInt64(wBits_obj, &window_bits))) { |
| + Dart_ThrowException(DartUtils::NewInternalError( |
| + "Failed to get 'windowBits' parameter")); |
| + } |
| + Dart_Handle dict_obj = Dart_GetNativeArgument(args, 2); |
| + Dart_Handle dictionary = NULL; |
| + if (!Dart_IsNull(dict_obj)) { |
| + dictionary = AllocateDictionary(dict_obj); |
|
Anders Johnsen
2014/01/27 12:27:45
Why are you doing this? :) Would it make sense to
vicb
2014/01/27 15:14:19
I want to get a snapshot of the dict on creation t
|
| + } |
| + Dart_Handle raw_obj = Dart_GetNativeArgument(args, 3); |
| + bool raw; |
| + if (Dart_IsError(Dart_BooleanValue(raw_obj, &raw))) { |
| + Dart_ThrowException(DartUtils::NewInternalError( |
| + "Failed to get 'raw' parameter")); |
| + } |
| + Filter* filter = new ZLibInflateFilter(static_cast<int32_t>(window_bits), |
| + dictionary, raw); |
| if (filter == NULL || !filter->Init()) { |
| delete filter; |
| Dart_ThrowException(DartUtils::NewInternalError( |
| @@ -49,24 +105,58 @@ void FUNCTION_NAME(Filter_CreateZLibInflate)(Dart_NativeArguments args) { |
| delete filter; |
| Dart_PropagateError(result); |
| } |
| + printf("Filter_CreateZLibInflate-----"); |
| } |
| void FUNCTION_NAME(Filter_CreateZLibDeflate)(Dart_NativeArguments args) { |
| Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); |
| Dart_Handle gzip_obj = Dart_GetNativeArgument(args, 1); |
| - Dart_Handle level_obj = Dart_GetNativeArgument(args, 2); |
| bool gzip; |
| if (Dart_IsError(Dart_BooleanValue(gzip_obj, &gzip))) { |
| Dart_ThrowException(DartUtils::NewInternalError( |
| "Failed to get 'gzip' parameter")); |
| } |
| - int64_t level = 0; |
| + Dart_Handle level_obj = Dart_GetNativeArgument(args, 2); |
| + int64_t level; |
| Dart_Handle result = Dart_IntegerToInt64(level_obj, &level); |
| if (Dart_IsError(result) || (level < kMinInt32) || (level > kMaxInt32)) { |
| Dart_ThrowException(DartUtils::NewInternalError( |
| "Failed to get 'level' parameter")); |
| } |
| - Filter* filter = new ZLibDeflateFilter(gzip, static_cast<int32_t>(level)); |
| + Dart_Handle wBits_obj = Dart_GetNativeArgument(args, 3); |
| + int64_t window_bits; |
| + if (Dart_IsError(Dart_IntegerToInt64(wBits_obj, &window_bits))) { |
| + Dart_ThrowException(DartUtils::NewInternalError( |
| + "Failed to get 'windowBits' parameter")); |
| + } |
| + Dart_Handle mLevel_obj = Dart_GetNativeArgument(args, 4); |
| + int64_t mem_level; |
| + if (Dart_IsError(Dart_IntegerToInt64(mLevel_obj, &mem_level))) { |
| + Dart_ThrowException(DartUtils::NewInternalError( |
| + "Failed to get 'memLevel' parameter")); |
| + } |
| + Dart_Handle strategy_obj = Dart_GetNativeArgument(args, 5); |
| + int64_t strategy; |
| + if (Dart_IsError(Dart_IntegerToInt64(strategy_obj, &strategy))) { |
| + Dart_ThrowException(DartUtils::NewInternalError( |
| + "Failed to get 'strategy' parameter")); |
| + } |
| + Dart_Handle dict_obj = Dart_GetNativeArgument(args, 6); |
| + Dart_Handle dictionary = NULL; |
| + if (!Dart_IsNull(dict_obj)) { |
| + dictionary = AllocateDictionary(dict_obj); |
| + } |
| + Dart_Handle raw_obj = Dart_GetNativeArgument(args, 7); |
| + bool raw; |
| + if (Dart_IsError(Dart_BooleanValue(raw_obj, &raw))) { |
| + Dart_ThrowException(DartUtils::NewInternalError( |
| + "Failed to get 'raw' parameter")); |
| + } |
| + Filter* filter = new ZLibDeflateFilter(gzip, static_cast<int32_t>(level), |
| + static_cast<int32_t>(window_bits), |
| + static_cast<int32_t>(mem_level), |
| + static_cast<int32_t>(strategy), |
| + dictionary, raw); |
| if (filter == NULL || !filter->Init()) { |
| delete filter; |
| Dart_ThrowException(DartUtils::NewInternalError( |
| @@ -191,21 +281,38 @@ ZLibDeflateFilter::~ZLibDeflateFilter() { |
| bool ZLibDeflateFilter::Init() { |
| + int window_bits = window_bits_; |
| + if (raw_) { |
| + window_bits *= -1; |
|
Anders Johnsen
2014/01/27 12:27:45
= -window_bits;
vicb
2014/01/27 15:14:19
fixed
|
| + } else if (gzip_) { |
| + window_bits += kZLibFlagUseGZipHeader; |
| + } |
| + stream_.next_in = Z_NULL; |
| stream_.zalloc = Z_NULL; |
| stream_.zfree = Z_NULL; |
| stream_.opaque = Z_NULL; |
| - int result = deflateInit2( |
| - &stream_, |
| - level_, |
| - Z_DEFLATED, |
| - kZLibFlagWindowBits | (gzip_ ? kZLibFlagUseGZipHeader : 0), |
| - kZlibFlagMemUsage, |
| - Z_DEFAULT_STRATEGY); |
| - if (result == Z_OK) { |
| - set_initialized(true); |
| - return true; |
| + int result = deflateInit2(&stream_, level_, Z_DEFLATED, window_bits, |
| + mem_level_, strategy_); |
| + if (result != Z_OK) { |
| + return false; |
| } |
| - return false; |
| + if (dictionary_ != NULL && !gzip_ && !raw_) { |
|
Anders Johnsen
2014/01/27 12:27:45
!Dart_IsNull(directory_)
vicb
2014/01/27 15:14:19
Sure ?
We have "Dart_Handle dictionary = NULL;" in
|
| + uint8_t* buffer = NULL; |
| + intptr_t length; |
| + Dart_TypedData_Type type; |
| + Dart_Handle handle = Dart_TypedDataAcquireData(dictionary_, &type, |
|
Anders Johnsen
2014/01/27 12:27:45
Place all arguments on each line, with 4 indentati
vicb
2014/01/27 15:14:19
fixed
|
| + reinterpret_cast<void**>(&buffer), &length); |
| + if (Dart_IsError(handle)) { |
| + return false; |
| + } |
| + result = deflateSetDictionary(&stream_, buffer, length); |
| + Dart_TypedDataReleaseData(dictionary_); |
| + if (result != Z_OK) { |
| + return false; |
| + } |
| + } |
| + set_initialized(true); |
| + return true; |
| } |
| @@ -222,6 +329,7 @@ intptr_t ZLibDeflateFilter::Processed(uint8_t* buffer, |
| bool end) { |
| stream_.avail_out = length; |
| stream_.next_out = buffer; |
| + bool error = false; |
| switch (deflate(&stream_, |
| end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { |
| case Z_STREAM_END: |
| @@ -229,22 +337,20 @@ intptr_t ZLibDeflateFilter::Processed(uint8_t* buffer, |
| case Z_OK: { |
| intptr_t processed = length - stream_.avail_out; |
| if (processed == 0) { |
| - delete[] current_buffer_; |
| - current_buffer_ = NULL; |
| - return 0; |
| - } else { |
| - // We processed data, should be called again. |
| - return processed; |
| + break; |
| } |
| + return processed; |
| } |
| default: |
| case Z_STREAM_ERROR: |
| - // An error occoured. |
| - delete[] current_buffer_; |
| - current_buffer_ = NULL; |
| - return -1; |
| + error = true; |
| } |
| + |
| + delete[] current_buffer_; |
| + current_buffer_ = NULL; |
| + // Either 0 Byte processed or either |
| + return error ? -1 : 0; |
| } |
| @@ -255,16 +361,21 @@ ZLibInflateFilter::~ZLibInflateFilter() { |
| bool ZLibInflateFilter::Init() { |
| + int window_bits = raw_ ? |
| + -1 * window_bits_ : |
| + window_bits_ | kZLibFlagAcceptAnyHeader; |
| + |
| + stream_.next_in = Z_NULL; |
| + stream_.avail_in = 0; |
| stream_.zalloc = Z_NULL; |
| stream_.zfree = Z_NULL; |
| stream_.opaque = Z_NULL; |
| - int result = inflateInit2(&stream_, |
| - kZLibFlagWindowBits | kZLibFlagAcceptAnyHeader); |
| - if (result == Z_OK) { |
| - set_initialized(true); |
| - return true; |
| + int result = inflateInit2(&stream_, window_bits); |
| + if (result != Z_OK) { |
| + return false; |
| } |
| - return false; |
| + set_initialized(true); |
| + return true; |
| } |
| @@ -282,6 +393,8 @@ intptr_t ZLibInflateFilter::Processed(uint8_t* buffer, |
| bool end) { |
| stream_.avail_out = length; |
| stream_.next_out = buffer; |
| + bool error = false; |
| + printf("Processed"); |
| switch (inflate(&stream_, |
| end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { |
| case Z_STREAM_END: |
| @@ -289,25 +402,52 @@ intptr_t ZLibInflateFilter::Processed(uint8_t* buffer, |
| case Z_OK: { |
| intptr_t processed = length - stream_.avail_out; |
| if (processed == 0) { |
| - delete[] current_buffer_; |
| - current_buffer_ = NULL; |
| - return 0; |
| - } else { |
| - // We processed data, should be called again. |
| - return processed; |
| + break; |
| } |
| + return processed; |
| } |
| + case Z_NEED_DICT: |
| + printf("Z_NEED_DICT\n"); |
| + if (dictionary_ == NULL) { |
| + printf("Z_NEED_DICT no dict\n"); |
| + error = true; |
| + } else { |
| + printf("Z_NEED_DICT dict"); |
| + uint8_t* buffer = NULL; |
| + intptr_t length; |
| + Dart_TypedData_Type type; |
| + Dart_Handle handle = Dart_TypedDataAcquireData(dictionary_, &type, |
|
Anders Johnsen
2014/01/27 12:27:45
Indentation of arguments.
vicb
2014/01/27 15:14:19
fixed
|
| + reinterpret_cast<void**>(&buffer), &length); |
| + printf("never back ?"); |
| + if (Dart_IsError(handle)) { |
| + printf("handle error"); |
| + return false; |
| + } |
| + printf("dict %d", static_cast<int>(length)); |
| + int result = inflateSetDictionary(&stream_, buffer, length); |
| + Dart_TypedDataReleaseData(dictionary_); |
| + |
| + printf("Z_NEED_DICT status %d\n", result); |
| + error = result != Z_OK; |
| + } |
| + if (error) { |
| + break; |
| + } else { |
| + return Processed(buffer, length, flush, end); |
| + } |
| + |
| default: |
| case Z_MEM_ERROR: |
| - case Z_NEED_DICT: |
| case Z_DATA_ERROR: |
| case Z_STREAM_ERROR: |
| - // An error occoured. |
| - delete[] current_buffer_; |
| - current_buffer_ = NULL; |
| - return -1; |
| + error = true; |
| } |
| + |
| + delete[] current_buffer_; |
| + current_buffer_ = NULL; |
| + // Either 0 Byte processed or either |
| + return error ? -1 : 0; |
| } |
| } // namespace bin |