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 <stdio.h> | |
6 | |
5 #include "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
6 #include "bin/filter.h" | 8 #include "bin/filter.h" |
7 #include "bin/io_buffer.h" | 9 #include "bin/io_buffer.h" |
8 | 10 |
9 #include "include/dart_api.h" | 11 #include "include/dart_api.h" |
10 | 12 |
11 | |
12 namespace dart { | 13 namespace dart { |
13 namespace bin { | 14 namespace bin { |
14 | 15 |
15 const int kZlibFlagMemUsage = 8; | |
16 const int kZLibFlagWindowBits = 15; | |
17 const int kZLibFlagUseGZipHeader = 16; | 16 const int kZLibFlagUseGZipHeader = 16; |
18 const int kZLibFlagAcceptAnyHeader = 32; | 17 const int kZLibFlagAcceptAnyHeader = 32; |
19 | 18 |
20 static const int kFilterPointerNativeField = 0; | 19 static const int kFilterPointerNativeField = 0; |
21 | 20 |
22 Filter* GetFilter(Dart_Handle filter_obj) { | 21 Filter* GetFilter(Dart_Handle filter_obj) { |
23 Filter* filter; | 22 Filter* filter; |
24 Dart_Handle result = Filter::GetFilterPointerNativeField(filter_obj, &filter); | 23 Dart_Handle result = Filter::GetFilterPointerNativeField(filter_obj, &filter); |
25 if (Dart_IsError(result)) { | 24 if (Dart_IsError(result)) { |
26 Dart_PropagateError(result); | 25 Dart_PropagateError(result); |
27 } | 26 } |
28 if (filter == NULL) { | 27 if (filter == NULL) { |
29 Dart_ThrowException(DartUtils::NewInternalError("Filter destroyed")); | 28 Dart_ThrowException(DartUtils::NewInternalError("Filter destroyed")); |
30 } | 29 } |
31 return filter; | 30 return filter; |
32 } | 31 } |
33 | 32 |
34 void EndFilter(Dart_Handle filter_obj, Filter* filter) { | 33 void EndFilter(Dart_Handle filter_obj, Filter* filter) { |
35 Filter::SetFilterPointerNativeField(filter_obj, NULL); | 34 Filter::SetFilterPointerNativeField(filter_obj, NULL); |
36 delete filter; | 35 delete filter; |
37 } | 36 } |
38 | 37 |
38 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)
| |
39 uint8_t* dst = NULL; | |
40 uint8_t* src = NULL; | |
41 intptr_t length; | |
42 Dart_TypedData_Type type; | |
43 | |
44 printf("+allocate"); | |
Anders Johnsen
2014/01/27 12:27:45
Please remove debug prints in this file.
| |
45 | |
46 if (Dart_IsError(Dart_ListLength(dictionary_obj, &length))) { | |
47 Dart_ThrowException(DartUtils::NewInternalError( | |
48 "Failed to get the zlib dictionary length")); | |
49 } | |
50 | |
51 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...
| |
52 | |
53 if (Dart_IsError(dictionary)) { | |
54 Dart_ThrowException(DartUtils::NewInternalError( | |
55 "Failed to allocate buffer for the zlib dictionary")); | |
56 } | |
57 | |
58 Dart_Handle result = Dart_TypedDataAcquireData( | |
59 dictionary_obj, &type, reinterpret_cast<void**>(&src), &length); | |
60 if (!Dart_IsError(result)) { | |
61 memmove(dst, src, length); | |
62 Dart_TypedDataReleaseData(dictionary_obj); | |
63 } else { | |
64 if (Dart_IsError(Dart_ListGetAsBytes(dictionary_obj, 0, dst, length))) { | |
65 Dart_ThrowException(DartUtils::NewInternalError( | |
66 "Failed to get the zlib dictionary bytes")); | |
67 } | |
68 } | |
69 | |
70 printf("-allocate"); | |
71 | |
72 return dictionary; | |
73 } | |
74 | |
39 void FUNCTION_NAME(Filter_CreateZLibInflate)(Dart_NativeArguments args) { | 75 void FUNCTION_NAME(Filter_CreateZLibInflate)(Dart_NativeArguments args) { |
76 setbuf(stdout, NULL); | |
77 printf("+Filter_CreateZLibInflate+\n"); | |
40 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); | 78 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); |
41 Filter* filter = new ZLibInflateFilter(); | 79 Dart_Handle wBits_obj = Dart_GetNativeArgument(args, 1); |
80 int64_t window_bits; | |
81 if (Dart_IsError(Dart_IntegerToInt64(wBits_obj, &window_bits))) { | |
82 Dart_ThrowException(DartUtils::NewInternalError( | |
83 "Failed to get 'windowBits' parameter")); | |
84 } | |
85 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 2); | |
86 Dart_Handle dictionary = NULL; | |
87 if (!Dart_IsNull(dict_obj)) { | |
88 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
| |
89 } | |
90 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 3); | |
91 bool raw; | |
92 if (Dart_IsError(Dart_BooleanValue(raw_obj, &raw))) { | |
93 Dart_ThrowException(DartUtils::NewInternalError( | |
94 "Failed to get 'raw' parameter")); | |
95 } | |
96 Filter* filter = new ZLibInflateFilter(static_cast<int32_t>(window_bits), | |
97 dictionary, raw); | |
42 if (filter == NULL || !filter->Init()) { | 98 if (filter == NULL || !filter->Init()) { |
43 delete filter; | 99 delete filter; |
44 Dart_ThrowException(DartUtils::NewInternalError( | 100 Dart_ThrowException(DartUtils::NewInternalError( |
45 "Failed to create ZLibInflateFilter")); | 101 "Failed to create ZLibInflateFilter")); |
46 } | 102 } |
47 Dart_Handle result = Filter::SetFilterPointerNativeField(filter_obj, filter); | 103 Dart_Handle result = Filter::SetFilterPointerNativeField(filter_obj, filter); |
48 if (Dart_IsError(result)) { | 104 if (Dart_IsError(result)) { |
49 delete filter; | 105 delete filter; |
50 Dart_PropagateError(result); | 106 Dart_PropagateError(result); |
51 } | 107 } |
108 printf("Filter_CreateZLibInflate-----"); | |
52 } | 109 } |
53 | 110 |
54 void FUNCTION_NAME(Filter_CreateZLibDeflate)(Dart_NativeArguments args) { | 111 void FUNCTION_NAME(Filter_CreateZLibDeflate)(Dart_NativeArguments args) { |
55 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); | 112 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); |
56 Dart_Handle gzip_obj = Dart_GetNativeArgument(args, 1); | 113 Dart_Handle gzip_obj = Dart_GetNativeArgument(args, 1); |
57 Dart_Handle level_obj = Dart_GetNativeArgument(args, 2); | |
58 bool gzip; | 114 bool gzip; |
59 if (Dart_IsError(Dart_BooleanValue(gzip_obj, &gzip))) { | 115 if (Dart_IsError(Dart_BooleanValue(gzip_obj, &gzip))) { |
60 Dart_ThrowException(DartUtils::NewInternalError( | 116 Dart_ThrowException(DartUtils::NewInternalError( |
61 "Failed to get 'gzip' parameter")); | 117 "Failed to get 'gzip' parameter")); |
62 } | 118 } |
63 int64_t level = 0; | 119 Dart_Handle level_obj = Dart_GetNativeArgument(args, 2); |
120 int64_t level; | |
64 Dart_Handle result = Dart_IntegerToInt64(level_obj, &level); | 121 Dart_Handle result = Dart_IntegerToInt64(level_obj, &level); |
65 if (Dart_IsError(result) || (level < kMinInt32) || (level > kMaxInt32)) { | 122 if (Dart_IsError(result) || (level < kMinInt32) || (level > kMaxInt32)) { |
66 Dart_ThrowException(DartUtils::NewInternalError( | 123 Dart_ThrowException(DartUtils::NewInternalError( |
67 "Failed to get 'level' parameter")); | 124 "Failed to get 'level' parameter")); |
68 } | 125 } |
69 Filter* filter = new ZLibDeflateFilter(gzip, static_cast<int32_t>(level)); | 126 Dart_Handle wBits_obj = Dart_GetNativeArgument(args, 3); |
127 int64_t window_bits; | |
128 if (Dart_IsError(Dart_IntegerToInt64(wBits_obj, &window_bits))) { | |
129 Dart_ThrowException(DartUtils::NewInternalError( | |
130 "Failed to get 'windowBits' parameter")); | |
131 } | |
132 Dart_Handle mLevel_obj = Dart_GetNativeArgument(args, 4); | |
133 int64_t mem_level; | |
134 if (Dart_IsError(Dart_IntegerToInt64(mLevel_obj, &mem_level))) { | |
135 Dart_ThrowException(DartUtils::NewInternalError( | |
136 "Failed to get 'memLevel' parameter")); | |
137 } | |
138 Dart_Handle strategy_obj = Dart_GetNativeArgument(args, 5); | |
139 int64_t strategy; | |
140 if (Dart_IsError(Dart_IntegerToInt64(strategy_obj, &strategy))) { | |
141 Dart_ThrowException(DartUtils::NewInternalError( | |
142 "Failed to get 'strategy' parameter")); | |
143 } | |
144 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 6); | |
145 Dart_Handle dictionary = NULL; | |
146 if (!Dart_IsNull(dict_obj)) { | |
147 dictionary = AllocateDictionary(dict_obj); | |
148 } | |
149 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 7); | |
150 bool raw; | |
151 if (Dart_IsError(Dart_BooleanValue(raw_obj, &raw))) { | |
152 Dart_ThrowException(DartUtils::NewInternalError( | |
153 "Failed to get 'raw' parameter")); | |
154 } | |
155 Filter* filter = new ZLibDeflateFilter(gzip, static_cast<int32_t>(level), | |
156 static_cast<int32_t>(window_bits), | |
157 static_cast<int32_t>(mem_level), | |
158 static_cast<int32_t>(strategy), | |
159 dictionary, raw); | |
70 if (filter == NULL || !filter->Init()) { | 160 if (filter == NULL || !filter->Init()) { |
71 delete filter; | 161 delete filter; |
72 Dart_ThrowException(DartUtils::NewInternalError( | 162 Dart_ThrowException(DartUtils::NewInternalError( |
73 "Failed to create ZLibDeflateFilter")); | 163 "Failed to create ZLibDeflateFilter")); |
74 } | 164 } |
75 result = Filter::SetFilterPointerNativeField(filter_obj, filter); | 165 result = Filter::SetFilterPointerNativeField(filter_obj, filter); |
76 if (Dart_IsError(result)) { | 166 if (Dart_IsError(result)) { |
77 delete filter; | 167 delete filter; |
78 Dart_PropagateError(result); | 168 Dart_PropagateError(result); |
79 } | 169 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 } | 274 } |
185 | 275 |
186 | 276 |
187 ZLibDeflateFilter::~ZLibDeflateFilter() { | 277 ZLibDeflateFilter::~ZLibDeflateFilter() { |
188 delete[] current_buffer_; | 278 delete[] current_buffer_; |
189 if (initialized()) deflateEnd(&stream_); | 279 if (initialized()) deflateEnd(&stream_); |
190 } | 280 } |
191 | 281 |
192 | 282 |
193 bool ZLibDeflateFilter::Init() { | 283 bool ZLibDeflateFilter::Init() { |
284 int window_bits = window_bits_; | |
285 if (raw_) { | |
286 window_bits *= -1; | |
Anders Johnsen
2014/01/27 12:27:45
= -window_bits;
vicb
2014/01/27 15:14:19
fixed
| |
287 } else if (gzip_) { | |
288 window_bits += kZLibFlagUseGZipHeader; | |
289 } | |
290 stream_.next_in = Z_NULL; | |
194 stream_.zalloc = Z_NULL; | 291 stream_.zalloc = Z_NULL; |
195 stream_.zfree = Z_NULL; | 292 stream_.zfree = Z_NULL; |
196 stream_.opaque = Z_NULL; | 293 stream_.opaque = Z_NULL; |
197 int result = deflateInit2( | 294 int result = deflateInit2(&stream_, level_, Z_DEFLATED, window_bits, |
198 &stream_, | 295 mem_level_, strategy_); |
199 level_, | 296 if (result != Z_OK) { |
200 Z_DEFLATED, | 297 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 } | 298 } |
208 return false; | 299 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
| |
300 uint8_t* buffer = NULL; | |
301 intptr_t length; | |
302 Dart_TypedData_Type type; | |
303 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
| |
304 reinterpret_cast<void**>(&buffer), &length); | |
305 if (Dart_IsError(handle)) { | |
306 return false; | |
307 } | |
308 result = deflateSetDictionary(&stream_, buffer, length); | |
309 Dart_TypedDataReleaseData(dictionary_); | |
310 if (result != Z_OK) { | |
311 return false; | |
312 } | |
313 } | |
314 set_initialized(true); | |
315 return true; | |
209 } | 316 } |
210 | 317 |
211 | 318 |
212 bool ZLibDeflateFilter::Process(uint8_t* data, intptr_t length) { | 319 bool ZLibDeflateFilter::Process(uint8_t* data, intptr_t length) { |
213 if (current_buffer_ != NULL) return false; | 320 if (current_buffer_ != NULL) return false; |
214 stream_.avail_in = length; | 321 stream_.avail_in = length; |
215 stream_.next_in = current_buffer_ = data; | 322 stream_.next_in = current_buffer_ = data; |
216 return true; | 323 return true; |
217 } | 324 } |
218 | 325 |
219 intptr_t ZLibDeflateFilter::Processed(uint8_t* buffer, | 326 intptr_t ZLibDeflateFilter::Processed(uint8_t* buffer, |
220 intptr_t length, | 327 intptr_t length, |
221 bool flush, | 328 bool flush, |
222 bool end) { | 329 bool end) { |
223 stream_.avail_out = length; | 330 stream_.avail_out = length; |
224 stream_.next_out = buffer; | 331 stream_.next_out = buffer; |
332 bool error = false; | |
225 switch (deflate(&stream_, | 333 switch (deflate(&stream_, |
226 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { | 334 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { |
227 case Z_STREAM_END: | 335 case Z_STREAM_END: |
228 case Z_BUF_ERROR: | 336 case Z_BUF_ERROR: |
229 case Z_OK: { | 337 case Z_OK: { |
230 intptr_t processed = length - stream_.avail_out; | 338 intptr_t processed = length - stream_.avail_out; |
231 if (processed == 0) { | 339 if (processed == 0) { |
232 delete[] current_buffer_; | 340 break; |
233 current_buffer_ = NULL; | |
234 return 0; | |
235 } else { | |
236 // We processed data, should be called again. | |
237 return processed; | |
238 } | 341 } |
342 return processed; | |
239 } | 343 } |
240 | 344 |
241 default: | 345 default: |
242 case Z_STREAM_ERROR: | 346 case Z_STREAM_ERROR: |
243 // An error occoured. | 347 error = true; |
244 delete[] current_buffer_; | |
245 current_buffer_ = NULL; | |
246 return -1; | |
247 } | 348 } |
349 | |
350 delete[] current_buffer_; | |
351 current_buffer_ = NULL; | |
352 // Either 0 Byte processed or either | |
353 return error ? -1 : 0; | |
248 } | 354 } |
249 | 355 |
250 | 356 |
251 ZLibInflateFilter::~ZLibInflateFilter() { | 357 ZLibInflateFilter::~ZLibInflateFilter() { |
252 delete[] current_buffer_; | 358 delete[] current_buffer_; |
253 if (initialized()) inflateEnd(&stream_); | 359 if (initialized()) inflateEnd(&stream_); |
254 } | 360 } |
255 | 361 |
256 | 362 |
257 bool ZLibInflateFilter::Init() { | 363 bool ZLibInflateFilter::Init() { |
364 int window_bits = raw_ ? | |
365 -1 * window_bits_ : | |
366 window_bits_ | kZLibFlagAcceptAnyHeader; | |
367 | |
368 stream_.next_in = Z_NULL; | |
369 stream_.avail_in = 0; | |
258 stream_.zalloc = Z_NULL; | 370 stream_.zalloc = Z_NULL; |
259 stream_.zfree = Z_NULL; | 371 stream_.zfree = Z_NULL; |
260 stream_.opaque = Z_NULL; | 372 stream_.opaque = Z_NULL; |
261 int result = inflateInit2(&stream_, | 373 int result = inflateInit2(&stream_, window_bits); |
262 kZLibFlagWindowBits | kZLibFlagAcceptAnyHeader); | 374 if (result != Z_OK) { |
263 if (result == Z_OK) { | 375 return false; |
264 set_initialized(true); | |
265 return true; | |
266 } | 376 } |
267 return false; | 377 set_initialized(true); |
378 return true; | |
268 } | 379 } |
269 | 380 |
270 | 381 |
271 bool ZLibInflateFilter::Process(uint8_t* data, intptr_t length) { | 382 bool ZLibInflateFilter::Process(uint8_t* data, intptr_t length) { |
272 if (current_buffer_ != NULL) return false; | 383 if (current_buffer_ != NULL) return false; |
273 stream_.avail_in = length; | 384 stream_.avail_in = length; |
274 stream_.next_in = current_buffer_ = data; | 385 stream_.next_in = current_buffer_ = data; |
275 return true; | 386 return true; |
276 } | 387 } |
277 | 388 |
278 | 389 |
279 intptr_t ZLibInflateFilter::Processed(uint8_t* buffer, | 390 intptr_t ZLibInflateFilter::Processed(uint8_t* buffer, |
280 intptr_t length, | 391 intptr_t length, |
281 bool flush, | 392 bool flush, |
282 bool end) { | 393 bool end) { |
283 stream_.avail_out = length; | 394 stream_.avail_out = length; |
284 stream_.next_out = buffer; | 395 stream_.next_out = buffer; |
396 bool error = false; | |
397 printf("Processed"); | |
285 switch (inflate(&stream_, | 398 switch (inflate(&stream_, |
286 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { | 399 end ? Z_FINISH : flush ? Z_SYNC_FLUSH : Z_NO_FLUSH)) { |
287 case Z_STREAM_END: | 400 case Z_STREAM_END: |
288 case Z_BUF_ERROR: | 401 case Z_BUF_ERROR: |
289 case Z_OK: { | 402 case Z_OK: { |
290 intptr_t processed = length - stream_.avail_out; | 403 intptr_t processed = length - stream_.avail_out; |
291 if (processed == 0) { | 404 if (processed == 0) { |
292 delete[] current_buffer_; | 405 break; |
293 current_buffer_ = NULL; | 406 } |
294 return 0; | 407 return processed; |
408 } | |
409 | |
410 case Z_NEED_DICT: | |
411 printf("Z_NEED_DICT\n"); | |
412 if (dictionary_ == NULL) { | |
413 printf("Z_NEED_DICT no dict\n"); | |
414 error = true; | |
295 } else { | 415 } else { |
296 // We processed data, should be called again. | 416 printf("Z_NEED_DICT dict"); |
297 return processed; | 417 uint8_t* buffer = NULL; |
418 intptr_t length; | |
419 Dart_TypedData_Type type; | |
420 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
| |
421 reinterpret_cast<void**>(&buffer), &length); | |
422 printf("never back ?"); | |
423 if (Dart_IsError(handle)) { | |
424 printf("handle error"); | |
425 return false; | |
426 } | |
427 printf("dict %d", static_cast<int>(length)); | |
428 int result = inflateSetDictionary(&stream_, buffer, length); | |
429 Dart_TypedDataReleaseData(dictionary_); | |
430 | |
431 printf("Z_NEED_DICT status %d\n", result); | |
432 error = result != Z_OK; | |
298 } | 433 } |
299 } | 434 if (error) { |
435 break; | |
436 } else { | |
437 return Processed(buffer, length, flush, end); | |
438 } | |
300 | 439 |
301 default: | 440 default: |
302 case Z_MEM_ERROR: | 441 case Z_MEM_ERROR: |
303 case Z_NEED_DICT: | |
304 case Z_DATA_ERROR: | 442 case Z_DATA_ERROR: |
305 case Z_STREAM_ERROR: | 443 case Z_STREAM_ERROR: |
306 // An error occoured. | 444 error = true; |
307 delete[] current_buffer_; | |
308 current_buffer_ = NULL; | |
309 return -1; | |
310 } | 445 } |
446 | |
447 delete[] current_buffer_; | |
448 current_buffer_ = NULL; | |
449 // Either 0 Byte processed or either | |
450 return error ? -1 : 0; | |
311 } | 451 } |
312 | 452 |
313 } // namespace bin | 453 } // namespace bin |
314 } // namespace dart | 454 } // namespace dart |
OLD | NEW |