Chromium Code Reviews| 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 10 matching lines...) Expand all Loading... | |
| 21 Dart_Handle result = Filter::GetFilterPointerNativeField(filter_obj, &filter); | 21 Dart_Handle result = Filter::GetFilterPointerNativeField(filter_obj, &filter); |
| 22 if (Dart_IsError(result)) { | 22 if (Dart_IsError(result)) { |
| 23 Dart_PropagateError(result); | 23 Dart_PropagateError(result); |
| 24 } | 24 } |
| 25 if (filter == NULL) { | 25 if (filter == NULL) { |
| 26 Dart_ThrowException(DartUtils::NewInternalError("Filter destroyed")); | 26 Dart_ThrowException(DartUtils::NewInternalError("Filter destroyed")); |
| 27 } | 27 } |
| 28 return filter; | 28 return filter; |
| 29 } | 29 } |
| 30 | 30 |
| 31 static void EndFilter(Dart_Handle filter_obj, Filter* filter) { | |
| 32 Filter::SetFilterPointerNativeField(filter_obj, NULL); | |
| 33 delete filter; | |
| 34 } | |
| 35 | 31 |
| 36 static uint8_t* copyDictionary(Dart_Handle dictionary_obj) { | 32 static uint8_t* CopyDictionary(Dart_Handle dictionary_obj) { |
| 37 uint8_t* src = NULL; | 33 uint8_t* src = NULL; |
| 38 intptr_t size; | 34 intptr_t size; |
| 39 Dart_TypedData_Type type; | 35 Dart_TypedData_Type type; |
| 40 | 36 |
| 41 if (Dart_IsError(Dart_ListLength(dictionary_obj, &size))) { | 37 if (Dart_IsError(Dart_ListLength(dictionary_obj, &size))) { |
| 42 Dart_ThrowException(DartUtils::NewInternalError( | 38 Dart_ThrowException(DartUtils::NewInternalError( |
| 43 "Failed to get the zlib dictionary length")); | 39 "Failed to get the zlib dictionary length")); |
| 44 } | 40 } |
| 45 | 41 |
| 46 uint8_t* dictionary = new uint8_t[size]; | 42 uint8_t* dictionary = new uint8_t[size]; |
| 47 | 43 ASSERT(dictionary != NULL); |
|
Cutch
2016/03/10 17:09:59
Are we sure we want to move from throwing an excep
zra
2016/03/10 18:48:09
I don't think we have consistent practices around
| |
| 48 if (dictionary == NULL) { | |
| 49 Dart_ThrowException(DartUtils::NewInternalError( | |
| 50 "Failed to allocate buffer for the zlib dictionary")); | |
| 51 } | |
| 52 | 44 |
| 53 Dart_Handle result = Dart_TypedDataAcquireData( | 45 Dart_Handle result = Dart_TypedDataAcquireData( |
| 54 dictionary_obj, &type, reinterpret_cast<void**>(&src), &size); | 46 dictionary_obj, &type, reinterpret_cast<void**>(&src), &size); |
| 55 if (!Dart_IsError(result)) { | 47 if (!Dart_IsError(result)) { |
| 56 memmove(dictionary, src, size); | 48 memmove(dictionary, src, size); |
| 57 Dart_TypedDataReleaseData(dictionary_obj); | 49 Dart_TypedDataReleaseData(dictionary_obj); |
| 58 } else { | 50 } else { |
| 59 if (Dart_IsError(Dart_ListGetAsBytes(dictionary_obj, 0, dictionary, | 51 if (Dart_IsError(Dart_ListGetAsBytes(dictionary_obj, 0, dictionary, |
| 60 size))) { | 52 size))) { |
| 61 Dart_ThrowException(DartUtils::NewInternalError( | 53 Dart_ThrowException(DartUtils::NewInternalError( |
| 62 "Failed to get the zlib dictionary")); | 54 "Failed to get the zlib dictionary")); |
| 63 } | 55 } |
| 64 } | 56 } |
| 65 | 57 |
| 66 return dictionary; | 58 return dictionary; |
| 67 } | 59 } |
| 68 | 60 |
| 61 | |
| 69 void FUNCTION_NAME(Filter_CreateZLibInflate)(Dart_NativeArguments args) { | 62 void FUNCTION_NAME(Filter_CreateZLibInflate)(Dart_NativeArguments args) { |
| 70 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); | 63 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); |
| 71 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 1); | 64 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 1); |
| 72 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj); | 65 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj); |
| 73 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 2); | 66 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 2); |
| 74 uint8_t* dictionary = NULL; | 67 uint8_t* dictionary = NULL; |
| 75 intptr_t dictionary_length = 0; | 68 intptr_t dictionary_length = 0; |
| 76 if (!Dart_IsNull(dict_obj)) { | 69 if (!Dart_IsNull(dict_obj)) { |
| 77 dictionary = copyDictionary(dict_obj); | 70 dictionary = CopyDictionary(dict_obj); |
| 78 if (dictionary != NULL) { | 71 ASSERT(dictionary != NULL); |
| 79 dictionary_length = 0; | 72 dictionary_length = 0; |
| 80 Dart_ListLength(dict_obj, &dictionary_length); | 73 Dart_ListLength(dict_obj, &dictionary_length); |
| 81 } | |
| 82 } | 74 } |
| 83 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 3); | 75 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 3); |
| 84 bool raw; | 76 bool raw; |
| 85 if (Dart_IsError(Dart_BooleanValue(raw_obj, &raw))) { | 77 if (Dart_IsError(Dart_BooleanValue(raw_obj, &raw))) { |
| 86 Dart_ThrowException(DartUtils::NewInternalError( | 78 Dart_ThrowException(DartUtils::NewInternalError( |
| 87 "Failed to get 'raw' parameter")); | 79 "Failed to get 'raw' parameter")); |
| 88 } | 80 } |
| 89 Filter* filter = new ZLibInflateFilter(static_cast<int32_t>(window_bits), | 81 ZLibInflateFilter* filter = new ZLibInflateFilter( |
| 90 dictionary, dictionary_length, raw); | 82 static_cast<int32_t>(window_bits), dictionary, dictionary_length, raw); |
| 83 ASSERT(filter != NULL); | |
| 91 if (!filter->Init()) { | 84 if (!filter->Init()) { |
| 92 delete filter; | 85 delete filter; |
| 93 Dart_ThrowException(DartUtils::NewInternalError( | 86 Dart_ThrowException(DartUtils::NewInternalError( |
| 94 "Failed to create ZLibInflateFilter")); | 87 "Failed to create ZLibInflateFilter")); |
| 95 } | 88 } |
| 96 Dart_Handle result = Filter::SetFilterPointerNativeField(filter_obj, filter); | 89 Dart_Handle result = Filter::SetFilterPointerNativeField( |
| 90 filter_obj, filter, sizeof(*filter) + dictionary_length); | |
| 97 if (Dart_IsError(result)) { | 91 if (Dart_IsError(result)) { |
| 98 delete filter; | 92 delete filter; |
| 99 Dart_PropagateError(result); | 93 Dart_PropagateError(result); |
| 100 } | 94 } |
| 101 } | 95 } |
| 102 | 96 |
| 97 | |
| 103 void FUNCTION_NAME(Filter_CreateZLibDeflate)(Dart_NativeArguments args) { | 98 void FUNCTION_NAME(Filter_CreateZLibDeflate)(Dart_NativeArguments args) { |
| 104 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); | 99 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); |
| 105 Dart_Handle gzip_obj = Dart_GetNativeArgument(args, 1); | 100 Dart_Handle gzip_obj = Dart_GetNativeArgument(args, 1); |
| 106 bool gzip = DartUtils::GetBooleanValue(gzip_obj); | 101 bool gzip = DartUtils::GetBooleanValue(gzip_obj); |
| 107 Dart_Handle level_obj = Dart_GetNativeArgument(args, 2); | 102 Dart_Handle level_obj = Dart_GetNativeArgument(args, 2); |
| 108 int64_t level = DartUtils::GetInt64ValueCheckRange(level_obj, kMinInt32, | 103 int64_t level = DartUtils::GetInt64ValueCheckRange(level_obj, kMinInt32, |
| 109 kMaxInt32); | 104 kMaxInt32); |
| 110 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 3); | 105 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 3); |
| 111 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj); | 106 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj); |
| 112 Dart_Handle mLevel_obj = Dart_GetNativeArgument(args, 4); | 107 Dart_Handle mLevel_obj = Dart_GetNativeArgument(args, 4); |
| 113 int64_t mem_level = DartUtils::GetIntegerValue(mLevel_obj); | 108 int64_t mem_level = DartUtils::GetIntegerValue(mLevel_obj); |
| 114 Dart_Handle strategy_obj = Dart_GetNativeArgument(args, 5); | 109 Dart_Handle strategy_obj = Dart_GetNativeArgument(args, 5); |
| 115 int64_t strategy = DartUtils::GetIntegerValue(strategy_obj); | 110 int64_t strategy = DartUtils::GetIntegerValue(strategy_obj); |
| 116 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 6); | 111 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 6); |
| 117 uint8_t* dictionary = NULL; | 112 uint8_t* dictionary = NULL; |
| 118 intptr_t dictionary_length = 0; | 113 intptr_t dictionary_length = 0; |
| 119 if (!Dart_IsNull(dict_obj)) { | 114 if (!Dart_IsNull(dict_obj)) { |
| 120 dictionary = copyDictionary(dict_obj); | 115 dictionary = CopyDictionary(dict_obj); |
| 121 if (dictionary != NULL) { | 116 ASSERT(dictionary != NULL); |
| 122 dictionary_length = 0; | 117 dictionary_length = 0; |
| 123 Dart_ListLength(dict_obj, &dictionary_length); | 118 Dart_ListLength(dict_obj, &dictionary_length); |
| 124 } | |
| 125 } | 119 } |
| 126 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 7); | 120 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 7); |
| 127 bool raw = DartUtils::GetBooleanValue(raw_obj); | 121 bool raw = DartUtils::GetBooleanValue(raw_obj); |
| 128 Filter* filter = new ZLibDeflateFilter(gzip, static_cast<int32_t>(level), | 122 ZLibDeflateFilter* filter = new ZLibDeflateFilter( |
| 129 static_cast<int32_t>(window_bits), | 123 gzip, |
| 130 static_cast<int32_t>(mem_level), | 124 static_cast<int32_t>(level), |
| 131 static_cast<int32_t>(strategy), | 125 static_cast<int32_t>(window_bits), |
| 132 dictionary, dictionary_length, raw); | 126 static_cast<int32_t>(mem_level), |
| 127 static_cast<int32_t>(strategy), | |
| 128 dictionary, dictionary_length, raw); | |
| 129 ASSERT(filter != NULL); | |
| 133 if (!filter->Init()) { | 130 if (!filter->Init()) { |
| 134 delete filter; | 131 delete filter; |
| 135 Dart_ThrowException(DartUtils::NewInternalError( | 132 Dart_ThrowException(DartUtils::NewInternalError( |
| 136 "Failed to create ZLibDeflateFilter")); | 133 "Failed to create ZLibDeflateFilter")); |
| 137 } | 134 } |
| 138 Dart_Handle result = Filter::SetFilterPointerNativeField(filter_obj, filter); | 135 Dart_Handle result = Filter::SetFilterPointerNativeField( |
| 136 filter_obj, filter, sizeof(*filter) + dictionary_length); | |
| 139 if (Dart_IsError(result)) { | 137 if (Dart_IsError(result)) { |
| 140 delete filter; | 138 delete filter; |
| 141 Dart_PropagateError(result); | 139 Dart_PropagateError(result); |
| 142 } | 140 } |
| 143 } | 141 } |
| 144 | 142 |
| 143 | |
| 145 void FUNCTION_NAME(Filter_Process)(Dart_NativeArguments args) { | 144 void FUNCTION_NAME(Filter_Process)(Dart_NativeArguments args) { |
| 146 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); | 145 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); |
| 147 Filter* filter = GetFilter(filter_obj); | 146 Filter* filter = GetFilter(filter_obj); |
| 148 Dart_Handle data_obj = Dart_GetNativeArgument(args, 1); | 147 Dart_Handle data_obj = Dart_GetNativeArgument(args, 1); |
| 149 intptr_t start = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | 148 intptr_t start = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); |
| 150 intptr_t end = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | 149 intptr_t end = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); |
| 151 intptr_t chunk_length = end - start; | 150 intptr_t chunk_length = end - start; |
| 152 intptr_t length; | 151 intptr_t length; |
| 153 Dart_TypedData_Type type; | 152 Dart_TypedData_Type type; |
| 154 uint8_t* buffer = NULL; | 153 uint8_t* buffer = NULL; |
| 155 Dart_Handle result = Dart_TypedDataAcquireData( | 154 Dart_Handle result = Dart_TypedDataAcquireData( |
| 156 data_obj, &type, reinterpret_cast<void**>(&buffer), &length); | 155 data_obj, &type, reinterpret_cast<void**>(&buffer), &length); |
| 157 | 156 |
| 158 if (!Dart_IsError(result)) { | 157 if (!Dart_IsError(result)) { |
| 159 ASSERT(type == Dart_TypedData_kUint8 || type == Dart_TypedData_kInt8); | 158 ASSERT(type == Dart_TypedData_kUint8 || type == Dart_TypedData_kInt8); |
| 160 if (type != Dart_TypedData_kUint8 && type != Dart_TypedData_kInt8) { | 159 if (type != Dart_TypedData_kUint8 && type != Dart_TypedData_kInt8) { |
| 161 Dart_TypedDataReleaseData(data_obj); | 160 Dart_TypedDataReleaseData(data_obj); |
| 162 Dart_ThrowException(DartUtils::NewInternalError( | 161 Dart_ThrowException(DartUtils::NewInternalError( |
| 163 "Invalid argument passed to Filter_Process")); | 162 "Invalid argument passed to Filter_Process")); |
| 164 } | 163 } |
| 165 uint8_t* zlib_buffer = new uint8_t[chunk_length]; | 164 uint8_t* zlib_buffer = new uint8_t[chunk_length]; |
| 166 if (zlib_buffer == NULL) { | 165 ASSERT(zlib_buffer != NULL); |
| 167 Dart_TypedDataReleaseData(data_obj); | |
| 168 Dart_ThrowException(DartUtils::NewInternalError( | |
| 169 "Failed to allocate buffer for zlib")); | |
| 170 } | |
| 171 memmove(zlib_buffer, buffer + start, chunk_length); | 166 memmove(zlib_buffer, buffer + start, chunk_length); |
| 172 Dart_TypedDataReleaseData(data_obj); | 167 Dart_TypedDataReleaseData(data_obj); |
| 173 buffer = zlib_buffer; | 168 buffer = zlib_buffer; |
| 174 } else { | 169 } else { |
| 175 if (Dart_IsError(Dart_ListLength(data_obj, &length))) { | 170 if (Dart_IsError(Dart_ListLength(data_obj, &length))) { |
| 176 Dart_ThrowException(DartUtils::NewInternalError( | 171 Dart_ThrowException(DartUtils::NewInternalError( |
| 177 "Failed to get list length")); | 172 "Failed to get list length")); |
| 178 } | 173 } |
| 179 buffer = new uint8_t[chunk_length]; | 174 buffer = new uint8_t[chunk_length]; |
| 175 ASSERT(buffer != NULL); | |
| 180 if (Dart_IsError(Dart_ListGetAsBytes( | 176 if (Dart_IsError(Dart_ListGetAsBytes( |
| 181 data_obj, start, buffer, chunk_length))) { | 177 data_obj, start, buffer, chunk_length))) { |
| 182 delete[] buffer; | 178 delete[] buffer; |
| 183 Dart_ThrowException(DartUtils::NewInternalError( | 179 Dart_ThrowException(DartUtils::NewInternalError( |
| 184 "Failed to get list bytes")); | 180 "Failed to get list bytes")); |
| 185 } | 181 } |
| 186 } | 182 } |
| 187 // Process will take ownership of buffer, if successful. | 183 // Process will take ownership of buffer, if successful. |
| 188 if (!filter->Process(buffer, chunk_length)) { | 184 if (!filter->Process(buffer, chunk_length)) { |
| 189 delete[] buffer; | 185 delete[] buffer; |
| 190 EndFilter(filter_obj, filter); | |
| 191 Dart_ThrowException(DartUtils::NewInternalError( | 186 Dart_ThrowException(DartUtils::NewInternalError( |
| 192 "Call to Process while still processing data")); | 187 "Call to Process while still processing data")); |
| 193 } | 188 } |
| 194 } | 189 } |
| 195 | 190 |
| 196 | 191 |
| 197 void FUNCTION_NAME(Filter_Processed)(Dart_NativeArguments args) { | 192 void FUNCTION_NAME(Filter_Processed)(Dart_NativeArguments args) { |
| 198 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); | 193 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); |
| 199 Filter* filter = GetFilter(filter_obj); | 194 Filter* filter = GetFilter(filter_obj); |
| 200 Dart_Handle flush_obj = Dart_GetNativeArgument(args, 1); | 195 Dart_Handle flush_obj = Dart_GetNativeArgument(args, 1); |
| 201 bool flush; | 196 bool flush; |
| 202 if (Dart_IsError(Dart_BooleanValue(flush_obj, &flush))) { | 197 if (Dart_IsError(Dart_BooleanValue(flush_obj, &flush))) { |
| 203 Dart_ThrowException(DartUtils::NewInternalError( | 198 Dart_ThrowException(DartUtils::NewInternalError( |
| 204 "Failed to get 'flush' parameter")); | 199 "Failed to get 'flush' parameter")); |
| 205 } | 200 } |
| 206 Dart_Handle end_obj = Dart_GetNativeArgument(args, 2); | 201 Dart_Handle end_obj = Dart_GetNativeArgument(args, 2); |
| 207 bool end; | 202 bool end; |
| 208 if (Dart_IsError(Dart_BooleanValue(end_obj, &end))) { | 203 if (Dart_IsError(Dart_BooleanValue(end_obj, &end))) { |
| 209 Dart_ThrowException(DartUtils::NewInternalError( | 204 Dart_ThrowException(DartUtils::NewInternalError( |
| 210 "Failed to get 'end' parameter")); | 205 "Failed to get 'end' parameter")); |
| 211 } | 206 } |
| 212 intptr_t read = filter->Processed(filter->processed_buffer(), | 207 intptr_t read = filter->Processed(filter->processed_buffer(), |
| 213 filter->processed_buffer_size(), | 208 filter->processed_buffer_size(), |
| 214 flush, | 209 flush, |
| 215 end); | 210 end); |
| 216 if (read < 0) { | 211 if (read < 0) { |
| 217 // Error, end filter. | |
| 218 EndFilter(filter_obj, filter); | |
| 219 Dart_ThrowException(DartUtils::NewInternalError( | 212 Dart_ThrowException(DartUtils::NewInternalError( |
| 220 "Filter error, bad data")); | 213 "Filter error, bad data")); |
| 221 } else if (read == 0) { | 214 } else if (read == 0) { |
| 222 Dart_SetReturnValue(args, Dart_Null()); | 215 Dart_SetReturnValue(args, Dart_Null()); |
| 223 } else { | 216 } else { |
| 224 uint8_t* io_buffer; | 217 uint8_t* io_buffer; |
| 225 Dart_Handle result = IOBuffer::Allocate(read, &io_buffer); | 218 Dart_Handle result = IOBuffer::Allocate(read, &io_buffer); |
| 226 memmove(io_buffer, filter->processed_buffer(), read); | 219 memmove(io_buffer, filter->processed_buffer(), read); |
| 227 Dart_SetReturnValue(args, result); | 220 Dart_SetReturnValue(args, result); |
| 228 } | 221 } |
| 229 } | 222 } |
| 230 | 223 |
| 231 | 224 |
| 232 void FUNCTION_NAME(Filter_End)(Dart_NativeArguments args) { | 225 static void DeleteFilter( |
| 233 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); | 226 void* isolate_data, |
| 234 Filter* filter = GetFilter(filter_obj); | 227 Dart_WeakPersistentHandle handle, |
| 235 EndFilter(filter_obj, filter); | 228 void* filter_pointer) { |
| 229 Filter* filter = reinterpret_cast<Filter*>(filter_pointer); | |
| 230 delete filter; | |
| 236 } | 231 } |
| 237 | 232 |
| 238 | 233 |
| 239 Dart_Handle Filter::SetFilterPointerNativeField(Dart_Handle filter, | 234 Dart_Handle Filter::SetFilterPointerNativeField(Dart_Handle filter, |
|
Cutch
2016/03/10 17:09:59
Consider renaming this method as it now does a lot
zra
2016/03/10 18:48:08
Done.
| |
| 240 Filter* filter_pointer) { | 235 Filter* filter_pointer, |
| 241 return Dart_SetNativeInstanceField( | 236 intptr_t size) { |
| 237 Dart_Handle err = Dart_SetNativeInstanceField( | |
| 242 filter, | 238 filter, |
| 243 kFilterPointerNativeField, | 239 kFilterPointerNativeField, |
| 244 reinterpret_cast<intptr_t>(filter_pointer)); | 240 reinterpret_cast<intptr_t>(filter_pointer)); |
| 241 if (Dart_IsError(err)) { | |
| 242 return err; | |
| 243 } | |
| 244 Dart_NewWeakPersistentHandle(filter, | |
| 245 reinterpret_cast<void*>(filter_pointer), | |
| 246 size, | |
| 247 DeleteFilter); | |
| 248 return err; | |
| 245 } | 249 } |
| 246 | 250 |
| 247 | 251 |
| 248 Dart_Handle Filter::GetFilterPointerNativeField(Dart_Handle filter, | 252 Dart_Handle Filter::GetFilterPointerNativeField(Dart_Handle filter, |
| 249 Filter** filter_pointer) { | 253 Filter** filter_pointer) { |
| 250 return Dart_GetNativeInstanceField( | 254 return Dart_GetNativeInstanceField( |
| 251 filter, | 255 filter, |
| 252 kFilterPointerNativeField, | 256 kFilterPointerNativeField, |
| 253 reinterpret_cast<intptr_t*>(filter_pointer)); | 257 reinterpret_cast<intptr_t*>(filter_pointer)); |
| 254 } | 258 } |
| 255 | 259 |
| 256 | 260 |
| 257 ZLibDeflateFilter::~ZLibDeflateFilter() { | 261 ZLibDeflateFilter::~ZLibDeflateFilter() { |
| 258 delete[] dictionary_; | 262 if (dictionary_ != NULL) { |
| 259 delete[] current_buffer_; | 263 delete[] dictionary_; |
|
Cutch
2016/03/10 17:09:59
delete is null safe. Here and elsewhere.
zra
2016/03/10 18:48:08
Done.
| |
| 260 if (initialized()) deflateEnd(&stream_); | 264 } |
| 265 if (current_buffer_ != NULL) { | |
| 266 delete[] current_buffer_; | |
| 267 } | |
| 268 if (initialized()) { | |
| 269 deflateEnd(&stream_); | |
| 270 } | |
| 261 } | 271 } |
| 262 | 272 |
| 263 | 273 |
| 264 bool ZLibDeflateFilter::Init() { | 274 bool ZLibDeflateFilter::Init() { |
| 265 int window_bits = window_bits_; | 275 int window_bits = window_bits_; |
| 266 if (raw_) { | 276 if (raw_) { |
| 267 window_bits = -window_bits; | 277 window_bits = -window_bits; |
| 268 } else if (gzip_) { | 278 } else if (gzip_) { |
| 269 window_bits += kZLibFlagUseGZipHeader; | 279 window_bits += kZLibFlagUseGZipHeader; |
| 270 } | 280 } |
| 271 stream_.next_in = Z_NULL; | 281 stream_.next_in = Z_NULL; |
| 272 stream_.zalloc = Z_NULL; | 282 stream_.zalloc = Z_NULL; |
| 273 stream_.zfree = Z_NULL; | 283 stream_.zfree = Z_NULL; |
| 274 stream_.opaque = Z_NULL; | 284 stream_.opaque = Z_NULL; |
| 275 int result = deflateInit2(&stream_, level_, Z_DEFLATED, window_bits, | 285 int result = deflateInit2(&stream_, level_, Z_DEFLATED, window_bits, |
| 276 mem_level_, strategy_); | 286 mem_level_, strategy_); |
| 277 if (result != Z_OK) { | 287 if (result != Z_OK) { |
| 278 return false; | 288 return false; |
| 279 } | 289 } |
| 280 if (dictionary_ != NULL && !gzip_ && !raw_) { | 290 if ((dictionary_ != NULL) && !gzip_ && !raw_) { |
| 281 result = deflateSetDictionary(&stream_, dictionary_, dictionary_length_); | 291 result = deflateSetDictionary(&stream_, dictionary_, dictionary_length_); |
| 282 delete[] dictionary_; | 292 delete[] dictionary_; |
| 283 dictionary_ = NULL; | 293 dictionary_ = NULL; |
| 284 if (result != Z_OK) { | 294 if (result != Z_OK) { |
| 285 return false; | 295 return false; |
| 286 } | 296 } |
| 287 } | 297 } |
| 288 set_initialized(true); | 298 set_initialized(true); |
| 289 return true; | 299 return true; |
| 290 } | 300 } |
| 291 | 301 |
| 292 | 302 |
| 293 bool ZLibDeflateFilter::Process(uint8_t* data, intptr_t length) { | 303 bool ZLibDeflateFilter::Process(uint8_t* data, intptr_t length) { |
| 294 if (current_buffer_ != NULL) return false; | 304 if (current_buffer_ != NULL) { |
| 305 return false; | |
| 306 } | |
| 295 stream_.avail_in = length; | 307 stream_.avail_in = length; |
| 296 stream_.next_in = current_buffer_ = data; | 308 stream_.next_in = current_buffer_ = data; |
| 297 return true; | 309 return true; |
| 298 } | 310 } |
| 299 | 311 |
| 300 intptr_t ZLibDeflateFilter::Processed(uint8_t* buffer, | 312 intptr_t ZLibDeflateFilter::Processed(uint8_t* buffer, |
| 301 intptr_t length, | 313 intptr_t length, |
| 302 bool flush, | 314 bool flush, |
| 303 bool end) { | 315 bool end) { |
| 304 stream_.avail_out = length; | 316 stream_.avail_out = length; |
| 305 stream_.next_out = buffer; | 317 stream_.next_out = buffer; |
| 306 bool error = false; | 318 bool error = false; |
| 307 switch (deflate(&stream_, | 319 switch (deflate(&stream_, |
| 308 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { | 320 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { |
| 309 case Z_STREAM_END: | 321 case Z_STREAM_END: |
| 310 case Z_BUF_ERROR: | 322 case Z_BUF_ERROR: |
| 311 case Z_OK: { | 323 case Z_OK: { |
| 312 intptr_t processed = length - stream_.avail_out; | 324 intptr_t processed = length - stream_.avail_out; |
| 313 if (processed == 0) { | 325 if (processed == 0) { |
| 314 break; | 326 break; |
| 315 } | 327 } |
| 316 return processed; | 328 return processed; |
| 317 } | 329 } |
| 318 | 330 |
| 319 default: | 331 default: |
| 320 case Z_STREAM_ERROR: | 332 case Z_STREAM_ERROR: |
| 321 error = true; | 333 error = true; |
| 322 } | 334 } |
| 323 | 335 |
| 324 delete[] current_buffer_; | 336 if (current_buffer_ != NULL) { |
| 325 current_buffer_ = NULL; | 337 delete[] current_buffer_; |
| 338 current_buffer_ = NULL; | |
| 339 } | |
| 326 // Either 0 Byte processed or error | 340 // Either 0 Byte processed or error |
| 327 return error ? -1 : 0; | 341 return error ? -1 : 0; |
| 328 } | 342 } |
| 329 | 343 |
| 330 | 344 |
| 331 ZLibInflateFilter::~ZLibInflateFilter() { | 345 ZLibInflateFilter::~ZLibInflateFilter() { |
| 332 delete[] dictionary_; | 346 if (dictionary_ != NULL) { |
| 333 delete[] current_buffer_; | 347 delete[] dictionary_; |
| 334 if (initialized()) inflateEnd(&stream_); | 348 } |
| 349 if (current_buffer_ != NULL) { | |
| 350 delete[] current_buffer_; | |
| 351 } | |
| 352 if (initialized()) { | |
| 353 inflateEnd(&stream_); | |
| 354 } | |
| 335 } | 355 } |
| 336 | 356 |
| 337 | 357 |
| 338 bool ZLibInflateFilter::Init() { | 358 bool ZLibInflateFilter::Init() { |
| 339 int window_bits = raw_ ? | 359 int window_bits = raw_ ? |
| 340 -window_bits_ : | 360 -window_bits_ : |
| 341 window_bits_ | kZLibFlagAcceptAnyHeader; | 361 window_bits_ | kZLibFlagAcceptAnyHeader; |
| 342 | 362 |
| 343 stream_.next_in = Z_NULL; | 363 stream_.next_in = Z_NULL; |
| 344 stream_.avail_in = 0; | 364 stream_.avail_in = 0; |
| 345 stream_.zalloc = Z_NULL; | 365 stream_.zalloc = Z_NULL; |
| 346 stream_.zfree = Z_NULL; | 366 stream_.zfree = Z_NULL; |
| 347 stream_.opaque = Z_NULL; | 367 stream_.opaque = Z_NULL; |
| 348 int result = inflateInit2(&stream_, window_bits); | 368 int result = inflateInit2(&stream_, window_bits); |
| 349 if (result != Z_OK) { | 369 if (result != Z_OK) { |
| 350 return false; | 370 return false; |
| 351 } | 371 } |
| 352 set_initialized(true); | 372 set_initialized(true); |
| 353 return true; | 373 return true; |
| 354 } | 374 } |
| 355 | 375 |
| 356 | 376 |
| 357 bool ZLibInflateFilter::Process(uint8_t* data, intptr_t length) { | 377 bool ZLibInflateFilter::Process(uint8_t* data, intptr_t length) { |
| 358 if (current_buffer_ != NULL) return false; | 378 if (current_buffer_ != NULL) { |
| 379 return false; | |
| 380 } | |
| 359 stream_.avail_in = length; | 381 stream_.avail_in = length; |
| 360 stream_.next_in = current_buffer_ = data; | 382 stream_.next_in = current_buffer_ = data; |
| 361 return true; | 383 return true; |
| 362 } | 384 } |
| 363 | 385 |
| 364 | 386 |
| 365 intptr_t ZLibInflateFilter::Processed(uint8_t* buffer, | 387 intptr_t ZLibInflateFilter::Processed(uint8_t* buffer, |
| 366 intptr_t length, | 388 intptr_t length, |
| 367 bool flush, | 389 bool flush, |
| 368 bool end) { | 390 bool end) { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 398 return Processed(buffer, length, flush, end); | 420 return Processed(buffer, length, flush, end); |
| 399 } | 421 } |
| 400 | 422 |
| 401 default: | 423 default: |
| 402 case Z_MEM_ERROR: | 424 case Z_MEM_ERROR: |
| 403 case Z_DATA_ERROR: | 425 case Z_DATA_ERROR: |
| 404 case Z_STREAM_ERROR: | 426 case Z_STREAM_ERROR: |
| 405 error = true; | 427 error = true; |
| 406 } | 428 } |
| 407 | 429 |
| 408 delete[] current_buffer_; | 430 if (current_buffer_ != NULL) { |
| 409 current_buffer_ = NULL; | 431 delete[] current_buffer_; |
| 432 current_buffer_ = NULL; | |
| 433 } | |
| 410 // Either 0 Byte processed or error | 434 // Either 0 Byte processed or error |
| 411 return error ? -1 : 0; | 435 return error ? -1 : 0; |
| 412 } | 436 } |
| 413 | 437 |
| 414 } // namespace bin | 438 } // namespace bin |
| 415 } // namespace dart | 439 } // namespace dart |
| OLD | NEW |