Skip to content
This repository
Newer
Older
100644 579 lines (462 sloc) 16.141 kb
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
ff4a9d38 »
2012-03-10 core: use proper #include directives
23 #include "v8.h"
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
24 #include <errno.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28
531eba18 »
2012-03-12 zlib: fix include of zlib.h
29 #include "zlib.h"
ff4a9d38 »
2012-03-10 core: use proper #include directives
30 #include "node.h"
31 #include "node_buffer.h"
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
32
33
34 namespace node {
35 using namespace v8;
36
37
74a8215a »
2012-02-03 Revert support for isolates.
38 static Persistent<String> callback_sym;
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
39 static Persistent<String> onerror_sym;
74a8215a »
2012-02-03 Revert support for isolates.
40
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
41 enum node_zlib_mode {
570e4be9 »
2012-10-30 zlib: reduce memory consumption, release early
42 NONE,
43 DEFLATE,
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
44 INFLATE,
45 GZIP,
46 GUNZIP,
47 DEFLATERAW,
48 INFLATERAW,
49 UNZIP
50 };
51
52
53 void InitZlib(v8::Handle<v8::Object> target);
54
55
56 /**
57 * Deflate/Inflate
58 */
44eb279e »
2012-03-21 zlib: don't use C++ templates
59 class ZCtx : public ObjectWrap {
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
60 public:
61
ef945219 »
2013-02-21 zlib: fix assert on bad input
62 ZCtx(node_zlib_mode mode)
63 : ObjectWrap()
64 , init_done_(false)
65 , level_(0)
66 , windowBits_(0)
67 , memLevel_(0)
68 , strategy_(0)
69 , err_(0)
70 , dictionary_(NULL)
71 , dictionary_len_(0)
72 , flush_(0)
73 , chunk_size_(0)
74 , write_in_progress_(false)
829a9b8c »
2014-02-13 zlib: introduce pending close state
75 , pending_close_(false)
ef945219 »
2013-02-21 zlib: fix assert on bad input
76 , mode_(mode)
77 {
78 }
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
79
570e4be9 »
2012-10-30 zlib: reduce memory consumption, release early
80
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
81 ~ZCtx() {
829a9b8c »
2014-02-13 zlib: introduce pending close state
82 assert(!write_in_progress_ && "write in progress");
07d3b21f »
2012-10-30 zlib: s/clear/close/ and match other close() semantics
83 Close();
570e4be9 »
2012-10-30 zlib: reduce memory consumption, release early
84 }
85
86
07d3b21f »
2012-10-30 zlib: s/clear/close/ and match other close() semantics
87 void Close() {
829a9b8c »
2014-02-13 zlib: introduce pending close state
88 if (write_in_progress_) {
89 pending_close_ = true;
90 return;
91 }
92
93 pending_close_ = false;
07d3b21f »
2012-10-30 zlib: s/clear/close/ and match other close() semantics
94 assert(init_done_ && "close before init");
570e4be9 »
2012-10-30 zlib: reduce memory consumption, release early
95 assert(mode_ <= UNZIP);
96
44eb279e »
2012-03-21 zlib: don't use C++ templates
97 if (mode_ == DEFLATE || mode_ == GZIP || mode_ == DEFLATERAW) {
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
98 (void)deflateEnd(&strm_);
a93424da »
2012-10-30 zlib: pass object size hint to V8
99 V8::AdjustAmountOfExternalAllocatedMemory(-kDeflateContextSize);
bf539f9b »
2012-07-08 zlib: Call inflateEnd for UNZIP. Fixes memory leak.
100 } else if (mode_ == INFLATE || mode_ == GUNZIP || mode_ == INFLATERAW ||
101 mode_ == UNZIP) {
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
102 (void)inflateEnd(&strm_);
a93424da »
2012-10-30 zlib: pass object size hint to V8
103 V8::AdjustAmountOfExternalAllocatedMemory(-kInflateContextSize);
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
104 }
570e4be9 »
2012-10-30 zlib: reduce memory consumption, release early
105 mode_ = NONE;
106
107 if (dictionary_ != NULL) {
108 delete[] dictionary_;
109 dictionary_ = NULL;
110 }
111 }
112
e6091952 »
2011-12-02 [zlib] added dictionary support
113
07d3b21f »
2012-10-30 zlib: s/clear/close/ and match other close() semantics
114 static Handle<Value> Close(const Arguments& args) {
570e4be9 »
2012-10-30 zlib: reduce memory consumption, release early
115 HandleScope scope;
116 ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This());
07d3b21f »
2012-10-30 zlib: s/clear/close/ and match other close() semantics
117 ctx->Close();
51f6e6a9 »
2013-02-25 src, test: downgrade to v8 3.14 api
118 return scope.Close(Undefined());
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
119 }
120
570e4be9 »
2012-10-30 zlib: reduce memory consumption, release early
121
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
122 // write(flush, in, in_off, in_len, out, out_off, out_len)
07701e7c »
2012-01-11 zlib: C++ style fixes
123 static Handle<Value> Write(const Arguments& args) {
d104bfd5 »
2011-09-17 zlib: Fix test so that it's not trivially passing, then pass it.
124 HandleScope scope;
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
125 assert(args.Length() == 7);
126
44eb279e »
2012-03-21 zlib: don't use C++ templates
127 ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This());
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
128 assert(ctx->init_done_ && "write before init");
570e4be9 »
2012-10-30 zlib: reduce memory consumption, release early
129 assert(ctx->mode_ != NONE && "already finalized");
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
130
8cca30f3 »
2012-01-10 zlib binding cleanup
131 assert(!ctx->write_in_progress_ && "write already in progress");
829a9b8c »
2014-02-13 zlib: introduce pending close state
132 assert(!ctx->pending_close_ && "close is pending");
8cca30f3 »
2012-01-10 zlib binding cleanup
133 ctx->write_in_progress_ = true;
ef945219 »
2013-02-21 zlib: fix assert on bad input
134 ctx->Ref();
8cca30f3 »
2012-01-10 zlib binding cleanup
135
0e01d639 »
2012-10-02 zlib: streams2
136 assert(!args[0]->IsUndefined() && "must provide flush value");
137
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
138 unsigned int flush = args[0]->Uint32Value();
0e01d639 »
2012-10-02 zlib: streams2
139
140 if (flush != Z_NO_FLUSH &&
141 flush != Z_PARTIAL_FLUSH &&
142 flush != Z_SYNC_FLUSH &&
143 flush != Z_FULL_FLUSH &&
144 flush != Z_FINISH &&
145 flush != Z_BLOCK) {
146 assert(0 && "Invalid flush value");
147 }
148
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
149 Bytef *in;
150 Bytef *out;
151 size_t in_off, in_len, out_off, out_len;
152
153 if (args[1]->IsNull()) {
154 // just a flush
155 Bytef nada[1] = { 0 };
156 in = nada;
157 in_len = 0;
158 in_off = 0;
159 } else {
160 assert(Buffer::HasInstance(args[1]));
161 Local<Object> in_buf;
162 in_buf = args[1]->ToObject();
07701e7c »
2012-01-11 zlib: C++ style fixes
163 in_off = args[2]->Uint32Value();
164 in_len = args[3]->Uint32Value();
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
165
5c832e44 »
2014-02-05 src: refactor buffer bounds checking
166 assert(Buffer::IsWithinBounds(in_off, in_len, Buffer::Length(in_buf)));
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
167 in = reinterpret_cast<Bytef *>(Buffer::Data(in_buf) + in_off);
168 }
169
170 assert(Buffer::HasInstance(args[4]));
171 Local<Object> out_buf = args[4]->ToObject();
07701e7c »
2012-01-11 zlib: C++ style fixes
172 out_off = args[5]->Uint32Value();
173 out_len = args[6]->Uint32Value();
5c832e44 »
2014-02-05 src: refactor buffer bounds checking
174 assert(Buffer::IsWithinBounds(out_off, out_len, Buffer::Length(out_buf)));
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
175 out = reinterpret_cast<Bytef *>(Buffer::Data(out_buf) + out_off);
176
8cca30f3 »
2012-01-10 zlib binding cleanup
177 // build up the work request
178 uv_work_t* work_req = &(ctx->work_req_);
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
179
180 ctx->strm_.avail_in = in_len;
07701e7c »
2012-01-11 zlib: C++ style fixes
181 ctx->strm_.next_in = in;
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
182 ctx->strm_.avail_out = out_len;
183 ctx->strm_.next_out = out;
184 ctx->flush_ = flush;
185
186 // set this so that later on, I can easily tell how much was written.
187 ctx->chunk_size_ = out_len;
188
74a8215a »
2012-02-03 Revert support for isolates.
189 uv_queue_work(uv_default_loop(),
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
190 work_req,
44eb279e »
2012-03-21 zlib: don't use C++ templates
191 ZCtx::Process,
192 ZCtx::After);
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
193
8cca30f3 »
2012-01-10 zlib binding cleanup
194 return ctx->handle_;
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
195 }
196
197
198 // thread pool!
199 // This function may be called multiple times on the uv_work pool
200 // for a single write() call, until all of the input bytes have
201 // been consumed.
07701e7c »
2012-01-11 zlib: C++ style fixes
202 static void Process(uv_work_t* work_req) {
44eb279e »
2012-03-21 zlib: don't use C++ templates
203 ZCtx *ctx = container_of(work_req, ZCtx, work_req_);
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
204
205 // If the avail_out is left at 0, then it means that it ran out
206 // of room. If there was avail_out left over, then it means
207 // that all of the input was consumed.
44eb279e »
2012-03-21 zlib: don't use C++ templates
208 switch (ctx->mode_) {
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
209 case DEFLATE:
210 case GZIP:
211 case DEFLATERAW:
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
212 ctx->err_ = deflate(&ctx->strm_, ctx->flush_);
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
213 break;
214 case UNZIP:
215 case INFLATE:
216 case GUNZIP:
217 case INFLATERAW:
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
218 ctx->err_ = inflate(&ctx->strm_, ctx->flush_);
e6091952 »
2011-12-02 [zlib] added dictionary support
219
220 // If data was encoded with dictionary
1c32eb4a »
2013-01-22 zlib: don't assert on missing dictionary
221 if (ctx->err_ == Z_NEED_DICT && ctx->dictionary_ != NULL) {
222
223 // Load it
224 ctx->err_ = inflateSetDictionary(&ctx->strm_,
225 ctx->dictionary_,
226 ctx->dictionary_len_);
227 if (ctx->err_ == Z_OK) {
228
229 // And try to decode again
230 ctx->err_ = inflate(&ctx->strm_, ctx->flush_);
d7bf7ed9 »
2013-01-22 zlib: don't assert on malformed dictionary
231 } else if (ctx->err_ == Z_DATA_ERROR) {
232
233 // Both inflateSetDictionary() and inflate() return Z_DATA_ERROR.
234 // Make it possible for After() to tell a bad dictionary from bad
235 // input.
236 ctx->err_ = Z_NEED_DICT;
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
237 }
e6091952 »
2011-12-02 [zlib] added dictionary support
238 }
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
239 break;
240 default:
241 assert(0 && "wtf?");
242 }
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
243
244 // pass any errors back to the main thread to deal with.
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
245
246 // now After will emit the output, and
247 // either schedule another call to Process,
248 // or shift the queue and call Process.
249 }
250
251 // v8 land!
6cf68aea »
2012-12-13 deps: upgrade libuv to e079a99
252 static void After(uv_work_t* work_req, int status) {
253 assert(status == 0);
254
d104bfd5 »
2011-09-17 zlib: Fix test so that it's not trivially passing, then pass it.
255 HandleScope scope;
44eb279e »
2012-03-21 zlib: don't use C++ templates
256 ZCtx *ctx = container_of(work_req, ZCtx, work_req_);
07701e7c »
2012-01-11 zlib: C++ style fixes
257
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
258 // Acceptable error states depend on the type of zlib stream.
259 switch (ctx->err_) {
260 case Z_OK:
261 case Z_STREAM_END:
262 case Z_BUF_ERROR:
263 // normal statuses, not fatal
264 break;
1c32eb4a »
2013-01-22 zlib: don't assert on missing dictionary
265 case Z_NEED_DICT:
d7bf7ed9 »
2013-01-22 zlib: don't assert on malformed dictionary
266 if (ctx->dictionary_ == NULL) {
267 ZCtx::Error(ctx, "Missing dictionary");
268 } else {
269 ZCtx::Error(ctx, "Bad dictionary");
270 }
1c32eb4a »
2013-01-22 zlib: don't assert on missing dictionary
271 return;
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
272 default:
273 // something else.
274 ZCtx::Error(ctx, "Zlib error");
275 return;
276 }
277
51f6e6a9 »
2013-02-25 src, test: downgrade to v8 3.14 api
278 Local<Integer> avail_out = Integer::New(ctx->strm_.avail_out);
279 Local<Integer> avail_in = Integer::New(ctx->strm_.avail_in);
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
280
8cca30f3 »
2012-01-10 zlib binding cleanup
281 ctx->write_in_progress_ = false;
282
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
283 // call the write() cb
8cca30f3 »
2012-01-10 zlib binding cleanup
284 assert(ctx->handle_->Get(callback_sym)->IsFunction() &&
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
285 "Invalid callback");
286 Local<Value> args[2] = { avail_in, avail_out };
a26bee8f »
2012-04-12 MakeCallback: Consistent symbol usage
287 MakeCallback(ctx->handle_, callback_sym, ARRAY_SIZE(args), args);
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
288
0ad2717f »
2012-01-10 Make sure that zlib contexts are not garbage collected when busy
289 ctx->Unref();
829a9b8c »
2014-02-13 zlib: introduce pending close state
290 if (ctx->pending_close_)
291 ctx->Close();
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
292 }
293
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
294 static void Error(ZCtx *ctx, const char *msg_) {
295 const char *msg;
296 if (ctx->strm_.msg != NULL) {
297 msg = ctx->strm_.msg;
298 } else {
299 msg = msg_;
300 }
301
302 assert(ctx->handle_->Get(onerror_sym)->IsFunction() &&
303 "Invalid error handler");
304 HandleScope scope;
305 Local<Value> args[2] = { String::New(msg),
51f6e6a9 »
2013-02-25 src, test: downgrade to v8 3.14 api
306 Local<Value>::New(Number::New(ctx->err_)) };
a26bee8f »
2012-04-12 MakeCallback: Consistent symbol usage
307 MakeCallback(ctx->handle_, onerror_sym, ARRAY_SIZE(args), args);
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
308
309 // no hope of rescue.
ef945219 »
2013-02-21 zlib: fix assert on bad input
310 ctx->write_in_progress_ = false;
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
311 ctx->Unref();
829a9b8c »
2014-02-13 zlib: introduce pending close state
312 if (ctx->pending_close_)
313 ctx->Close();
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
314 }
315
07701e7c »
2012-01-11 zlib: C++ style fixes
316 static Handle<Value> New(const Arguments& args) {
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
317 HandleScope scope;
44eb279e »
2012-03-21 zlib: don't use C++ templates
318 if (args.Length() < 1 || !args[0]->IsInt32()) {
319 return ThrowException(Exception::TypeError(String::New("Bad argument")));
320 }
321 node_zlib_mode mode = (node_zlib_mode) args[0]->Int32Value();
322
323 if (mode < DEFLATE || mode > UNZIP) {
324 return ThrowException(Exception::TypeError(String::New("Bad argument")));
325 }
326
327 ZCtx *ctx = new ZCtx(mode);
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
328 ctx->Wrap(args.This());
329 return args.This();
330 }
331
332 // just pull the ints out of the args and call the other Init
07701e7c »
2012-01-11 zlib: C++ style fixes
333 static Handle<Value> Init(const Arguments& args) {
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
334 HandleScope scope;
335
e6091952 »
2011-12-02 [zlib] added dictionary support
336 assert((args.Length() == 4 || args.Length() == 5) &&
337 "init(windowBits, level, memLevel, strategy, [dictionary])");
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
338
44eb279e »
2012-03-21 zlib: don't use C++ templates
339 ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This());
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
340
341 int windowBits = args[0]->Uint32Value();
342 assert((windowBits >= 8 && windowBits <= 15) && "invalid windowBits");
343
dc3c2d12 »
2013-07-01 zlib: level can be negative
344 int level = args[1]->Int32Value();
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
345 assert((level >= -1 && level <= 9) && "invalid compression level");
346
347 int memLevel = args[2]->Uint32Value();
348 assert((memLevel >= 1 && memLevel <= 9) && "invalid memlevel");
349
350 int strategy = args[3]->Uint32Value();
351 assert((strategy == Z_FILTERED ||
352 strategy == Z_HUFFMAN_ONLY ||
353 strategy == Z_RLE ||
354 strategy == Z_FIXED ||
355 strategy == Z_DEFAULT_STRATEGY) && "invalid strategy");
356
e6091952 »
2011-12-02 [zlib] added dictionary support
357 char* dictionary = NULL;
358 size_t dictionary_len = 0;
359 if (args.Length() >= 5 && Buffer::HasInstance(args[4])) {
360 Local<Object> dictionary_ = args[4]->ToObject();
361
362 dictionary_len = Buffer::Length(dictionary_);
363 dictionary = new char[dictionary_len];
364
365 memcpy(dictionary, Buffer::Data(dictionary_), dictionary_len);
366 }
367
368 Init(ctx, level, windowBits, memLevel, strategy,
369 dictionary, dictionary_len);
71ae1753 »
2012-01-12 zlib: reset() method for deflate/inflate streams
370 SetDictionary(ctx);
51f6e6a9 »
2013-02-25 src, test: downgrade to v8 3.14 api
371 return Undefined();
71ae1753 »
2012-01-12 zlib: reset() method for deflate/inflate streams
372 }
373
374 static Handle<Value> Reset(const Arguments &args) {
375 HandleScope scope;
376
44eb279e »
2012-03-21 zlib: don't use C++ templates
377 ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This());
71ae1753 »
2012-01-12 zlib: reset() method for deflate/inflate streams
378
379 Reset(ctx);
380 SetDictionary(ctx);
51f6e6a9 »
2013-02-25 src, test: downgrade to v8 3.14 api
381 return Undefined();
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
382 }
383
07701e7c »
2012-01-11 zlib: C++ style fixes
384 static void Init(ZCtx *ctx, int level, int windowBits, int memLevel,
9e6957b0 »
2012-01-12 Merge branch 'v0.6'
385 int strategy, char* dictionary, size_t dictionary_len) {
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
386 ctx->level_ = level;
387 ctx->windowBits_ = windowBits;
388 ctx->memLevel_ = memLevel;
389 ctx->strategy_ = strategy;
390
391 ctx->strm_.zalloc = Z_NULL;
392 ctx->strm_.zfree = Z_NULL;
393 ctx->strm_.opaque = Z_NULL;
394
395 ctx->flush_ = Z_NO_FLUSH;
396
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
397 ctx->err_ = Z_OK;
398
44eb279e »
2012-03-21 zlib: don't use C++ templates
399 if (ctx->mode_ == GZIP || ctx->mode_ == GUNZIP) {
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
400 ctx->windowBits_ += 16;
401 }
402
44eb279e »
2012-03-21 zlib: don't use C++ templates
403 if (ctx->mode_ == UNZIP) {
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
404 ctx->windowBits_ += 32;
405 }
406
44eb279e »
2012-03-21 zlib: don't use C++ templates
407 if (ctx->mode_ == DEFLATERAW || ctx->mode_ == INFLATERAW) {
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
408 ctx->windowBits_ *= -1;
409 }
410
44eb279e »
2012-03-21 zlib: don't use C++ templates
411 switch (ctx->mode_) {
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
412 case DEFLATE:
413 case GZIP:
414 case DEFLATERAW:
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
415 ctx->err_ = deflateInit2(&ctx->strm_,
416 ctx->level_,
417 Z_DEFLATED,
418 ctx->windowBits_,
419 ctx->memLevel_,
420 ctx->strategy_);
a93424da »
2012-10-30 zlib: pass object size hint to V8
421 V8::AdjustAmountOfExternalAllocatedMemory(kDeflateContextSize);
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
422 break;
423 case INFLATE:
424 case GUNZIP:
425 case INFLATERAW:
426 case UNZIP:
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
427 ctx->err_ = inflateInit2(&ctx->strm_, ctx->windowBits_);
a93424da »
2012-10-30 zlib: pass object size hint to V8
428 V8::AdjustAmountOfExternalAllocatedMemory(kInflateContextSize);
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
429 break;
430 default:
431 assert(0 && "wtf?");
432 }
433
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
434 if (ctx->err_ != Z_OK) {
435 ZCtx::Error(ctx, "Init error");
436 }
437
e6091952 »
2011-12-02 [zlib] added dictionary support
438
439 ctx->dictionary_ = reinterpret_cast<Bytef *>(dictionary);
440 ctx->dictionary_len_ = dictionary_len;
441
71ae1753 »
2012-01-12 zlib: reset() method for deflate/inflate streams
442 ctx->write_in_progress_ = false;
443 ctx->init_done_ = true;
444 }
445
446 static void SetDictionary(ZCtx* ctx) {
447 if (ctx->dictionary_ == NULL) return;
e6091952 »
2011-12-02 [zlib] added dictionary support
448
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
449 ctx->err_ = Z_OK;
71ae1753 »
2012-01-12 zlib: reset() method for deflate/inflate streams
450
44eb279e »
2012-03-21 zlib: don't use C++ templates
451 switch (ctx->mode_) {
71ae1753 »
2012-01-12 zlib: reset() method for deflate/inflate streams
452 case DEFLATE:
453 case DEFLATERAW:
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
454 ctx->err_ = deflateSetDictionary(&ctx->strm_,
455 ctx->dictionary_,
456 ctx->dictionary_len_);
71ae1753 »
2012-01-12 zlib: reset() method for deflate/inflate streams
457 break;
458 default:
459 break;
e6091952 »
2011-12-02 [zlib] added dictionary support
460 }
461
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
462 if (ctx->err_ != Z_OK) {
463 ZCtx::Error(ctx, "Failed to set dictionary");
464 }
71ae1753 »
2012-01-12 zlib: reset() method for deflate/inflate streams
465 }
466
467 static void Reset(ZCtx* ctx) {
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
468 ctx->err_ = Z_OK;
71ae1753 »
2012-01-12 zlib: reset() method for deflate/inflate streams
469
44eb279e »
2012-03-21 zlib: don't use C++ templates
470 switch (ctx->mode_) {
71ae1753 »
2012-01-12 zlib: reset() method for deflate/inflate streams
471 case DEFLATE:
472 case DEFLATERAW:
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
473 ctx->err_ = deflateReset(&ctx->strm_);
71ae1753 »
2012-01-12 zlib: reset() method for deflate/inflate streams
474 break;
475 case INFLATE:
476 case INFLATERAW:
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
477 ctx->err_ = inflateReset(&ctx->strm_);
71ae1753 »
2012-01-12 zlib: reset() method for deflate/inflate streams
478 break;
479 default:
480 break;
481 }
482
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
483 if (ctx->err_ != Z_OK) {
484 ZCtx::Error(ctx, "Failed to reset stream");
485 }
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
486 }
487
488 private:
a93424da »
2012-10-30 zlib: pass object size hint to V8
489 static const int kDeflateContextSize = 16384; // approximate
490 static const int kInflateContextSize = 10240; // approximate
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
491
492 bool init_done_;
493
494 z_stream strm_;
495 int level_;
496 int windowBits_;
497 int memLevel_;
498 int strategy_;
499
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
500 int err_;
501
e6091952 »
2011-12-02 [zlib] added dictionary support
502 Bytef* dictionary_;
503 size_t dictionary_len_;
504
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
505 int flush_;
506
507 int chunk_size_;
8cca30f3 »
2012-01-10 zlib binding cleanup
508
509 bool write_in_progress_;
829a9b8c »
2014-02-13 zlib: introduce pending close state
510 bool pending_close_;
8cca30f3 »
2012-01-10 zlib binding cleanup
511
512 uv_work_t work_req_;
44eb279e »
2012-03-21 zlib: don't use C++ templates
513 node_zlib_mode mode_;
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
514 };
515
516
517 void InitZlib(Handle<Object> target) {
518 HandleScope scope;
519
44eb279e »
2012-03-21 zlib: don't use C++ templates
520 Local<FunctionTemplate> z = FunctionTemplate::New(ZCtx::New);
521
522 z->InstanceTemplate()->SetInternalFieldCount(1);
523
524 NODE_SET_PROTOTYPE_METHOD(z, "write", ZCtx::Write);
525 NODE_SET_PROTOTYPE_METHOD(z, "init", ZCtx::Init);
07d3b21f »
2012-10-30 zlib: s/clear/close/ and match other close() semantics
526 NODE_SET_PROTOTYPE_METHOD(z, "close", ZCtx::Close);
44eb279e »
2012-03-21 zlib: don't use C++ templates
527 NODE_SET_PROTOTYPE_METHOD(z, "reset", ZCtx::Reset);
528
529 z->SetClassName(String::NewSymbol("Zlib"));
530 target->Set(String::NewSymbol("Zlib"), z->GetFunction());
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
531
532 callback_sym = NODE_PSYMBOL("callback");
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
533 onerror_sym = NODE_PSYMBOL("onerror");
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
534
0e01d639 »
2012-10-02 zlib: streams2
535 // valid flush values.
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
536 NODE_DEFINE_CONSTANT(target, Z_NO_FLUSH);
537 NODE_DEFINE_CONSTANT(target, Z_PARTIAL_FLUSH);
538 NODE_DEFINE_CONSTANT(target, Z_SYNC_FLUSH);
539 NODE_DEFINE_CONSTANT(target, Z_FULL_FLUSH);
540 NODE_DEFINE_CONSTANT(target, Z_FINISH);
541 NODE_DEFINE_CONSTANT(target, Z_BLOCK);
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
542
543 // return/error codes
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
544 NODE_DEFINE_CONSTANT(target, Z_OK);
545 NODE_DEFINE_CONSTANT(target, Z_STREAM_END);
546 NODE_DEFINE_CONSTANT(target, Z_NEED_DICT);
547 NODE_DEFINE_CONSTANT(target, Z_ERRNO);
548 NODE_DEFINE_CONSTANT(target, Z_STREAM_ERROR);
549 NODE_DEFINE_CONSTANT(target, Z_DATA_ERROR);
550 NODE_DEFINE_CONSTANT(target, Z_MEM_ERROR);
551 NODE_DEFINE_CONSTANT(target, Z_BUF_ERROR);
552 NODE_DEFINE_CONSTANT(target, Z_VERSION_ERROR);
01d46f3a »
2012-03-31 Fix #3052 Handle errors properly in zlib
553
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
554 NODE_DEFINE_CONSTANT(target, Z_NO_COMPRESSION);
555 NODE_DEFINE_CONSTANT(target, Z_BEST_SPEED);
556 NODE_DEFINE_CONSTANT(target, Z_BEST_COMPRESSION);
557 NODE_DEFINE_CONSTANT(target, Z_DEFAULT_COMPRESSION);
558 NODE_DEFINE_CONSTANT(target, Z_FILTERED);
559 NODE_DEFINE_CONSTANT(target, Z_HUFFMAN_ONLY);
560 NODE_DEFINE_CONSTANT(target, Z_RLE);
561 NODE_DEFINE_CONSTANT(target, Z_FIXED);
562 NODE_DEFINE_CONSTANT(target, Z_DEFAULT_STRATEGY);
563 NODE_DEFINE_CONSTANT(target, ZLIB_VERNUM);
564
44eb279e »
2012-03-21 zlib: don't use C++ templates
565 NODE_DEFINE_CONSTANT(target, DEFLATE);
566 NODE_DEFINE_CONSTANT(target, INFLATE);
567 NODE_DEFINE_CONSTANT(target, GZIP);
568 NODE_DEFINE_CONSTANT(target, GUNZIP);
569 NODE_DEFINE_CONSTANT(target, DEFLATERAW);
570 NODE_DEFINE_CONSTANT(target, INFLATERAW);
571 NODE_DEFINE_CONSTANT(target, UNZIP);
572
5b8e1dab »
2011-09-06 Initial pass at zlib bindings
573 target->Set(String::NewSymbol("ZLIB_VERSION"), String::New(ZLIB_VERSION));
574 }
575
576 } // namespace node
577
cdcb1118 »
2011-11-09 Remove stray NODE_MODULE() semi-colons.
578 NODE_MODULE(node_zlib, node::InitZlib)
Something went wrong with that request. Please try again.