Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 551 lines (439 sloc) 15.416 kb
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
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
ff4a9d3 @bnoordhuis core: use proper #include directives
bnoordhuis authored
23 #include "v8.h"
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
24 #include <errno.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28
531eba1 @bnoordhuis zlib: fix include of zlib.h
bnoordhuis authored
29 #include "zlib.h"
ff4a9d3 @bnoordhuis core: use proper #include directives
bnoordhuis authored
30 #include "node.h"
31 #include "node_buffer.h"
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
32
33
34 namespace node {
35 using namespace v8;
36
37
74a8215 @bnoordhuis Revert support for isolates.
bnoordhuis authored
38 static Persistent<String> callback_sym;
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
39 static Persistent<String> onerror_sym;
74a8215 @bnoordhuis Revert support for isolates.
bnoordhuis authored
40
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
41 enum node_zlib_mode {
8d14668 @bnoordhuis zlib: reduce memory consumption, release early
bnoordhuis authored
42 NONE,
43 DEFLATE,
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
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 */
44eb279 zlib: don't use C++ templates
ssuda authored
59 class ZCtx : public ObjectWrap {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
60 public:
61
ef94521 @bnoordhuis zlib: fix assert on bad input
bnoordhuis authored
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)
75 , mode_(mode)
76 {
77 }
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
78
8d14668 @bnoordhuis zlib: reduce memory consumption, release early
bnoordhuis authored
79
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
80 ~ZCtx() {
8d14668 @bnoordhuis zlib: reduce memory consumption, release early
bnoordhuis authored
81 Clear();
82 }
83
84
85 void Clear() {
86 assert(!write_in_progress_ && "write in progress");
87 assert(init_done_ && "clear before init");
88 assert(mode_ <= UNZIP);
89
44eb279 zlib: don't use C++ templates
ssuda authored
90 if (mode_ == DEFLATE || mode_ == GZIP || mode_ == DEFLATERAW) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
91 (void)deflateEnd(&strm_);
6b99fd2 @bnoordhuis zlib: pass object size hint to V8
bnoordhuis authored
92 V8::AdjustAmountOfExternalAllocatedMemory(-kDeflateContextSize);
bf539f9 @isaacs zlib: Call inflateEnd for UNZIP. Fixes memory leak.
isaacs authored
93 } else if (mode_ == INFLATE || mode_ == GUNZIP || mode_ == INFLATERAW ||
94 mode_ == UNZIP) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
95 (void)inflateEnd(&strm_);
6b99fd2 @bnoordhuis zlib: pass object size hint to V8
bnoordhuis authored
96 V8::AdjustAmountOfExternalAllocatedMemory(-kInflateContextSize);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
97 }
8d14668 @bnoordhuis zlib: reduce memory consumption, release early
bnoordhuis authored
98 mode_ = NONE;
99
100 if (dictionary_ != NULL) {
101 delete[] dictionary_;
102 dictionary_ = NULL;
103 }
104 }
105
e609195 @indutny [zlib] added dictionary support
indutny authored
106
8d14668 @bnoordhuis zlib: reduce memory consumption, release early
bnoordhuis authored
107 static Handle<Value> Clear(const Arguments& args) {
108 HandleScope scope;
109 ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This());
110 ctx->Clear();
111 return scope.Close(Undefined());
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
112 }
113
8d14668 @bnoordhuis zlib: reduce memory consumption, release early
bnoordhuis authored
114
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
115 // write(flush, in, in_off, in_len, out, out_off, out_len)
07701e7 @indutny zlib: C++ style fixes
indutny authored
116 static Handle<Value> Write(const Arguments& args) {
d104bfd @isaacs zlib: Fix test so that it's not trivially passing, then pass it.
isaacs authored
117 HandleScope scope;
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
118 assert(args.Length() == 7);
119
44eb279 zlib: don't use C++ templates
ssuda authored
120 ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This());
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
121 assert(ctx->init_done_ && "write before init");
8d14668 @bnoordhuis zlib: reduce memory consumption, release early
bnoordhuis authored
122 assert(ctx->mode_ != NONE && "already finalized");
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
123
8cca30f @isaacs zlib binding cleanup
isaacs authored
124 assert(!ctx->write_in_progress_ && "write already in progress");
125 ctx->write_in_progress_ = true;
ef94521 @bnoordhuis zlib: fix assert on bad input
bnoordhuis authored
126 ctx->Ref();
8cca30f @isaacs zlib binding cleanup
isaacs authored
127
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
128 unsigned int flush = args[0]->Uint32Value();
129 Bytef *in;
130 Bytef *out;
131 size_t in_off, in_len, out_off, out_len;
132
133 if (args[1]->IsNull()) {
134 // just a flush
135 Bytef nada[1] = { 0 };
136 in = nada;
137 in_len = 0;
138 in_off = 0;
139 } else {
140 assert(Buffer::HasInstance(args[1]));
141 Local<Object> in_buf;
142 in_buf = args[1]->ToObject();
07701e7 @indutny zlib: C++ style fixes
indutny authored
143 in_off = args[2]->Uint32Value();
144 in_len = args[3]->Uint32Value();
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
145
146 assert(in_off + in_len <= Buffer::Length(in_buf));
147 in = reinterpret_cast<Bytef *>(Buffer::Data(in_buf) + in_off);
148 }
149
150 assert(Buffer::HasInstance(args[4]));
151 Local<Object> out_buf = args[4]->ToObject();
07701e7 @indutny zlib: C++ style fixes
indutny authored
152 out_off = args[5]->Uint32Value();
153 out_len = args[6]->Uint32Value();
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
154 assert(out_off + out_len <= Buffer::Length(out_buf));
155 out = reinterpret_cast<Bytef *>(Buffer::Data(out_buf) + out_off);
156
8cca30f @isaacs zlib binding cleanup
isaacs authored
157 // build up the work request
158 uv_work_t* work_req = &(ctx->work_req_);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
159
160 ctx->strm_.avail_in = in_len;
07701e7 @indutny zlib: C++ style fixes
indutny authored
161 ctx->strm_.next_in = in;
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
162 ctx->strm_.avail_out = out_len;
163 ctx->strm_.next_out = out;
164 ctx->flush_ = flush;
165
166 // set this so that later on, I can easily tell how much was written.
167 ctx->chunk_size_ = out_len;
168
74a8215 @bnoordhuis Revert support for isolates.
bnoordhuis authored
169 uv_queue_work(uv_default_loop(),
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
170 work_req,
44eb279 zlib: don't use C++ templates
ssuda authored
171 ZCtx::Process,
172 ZCtx::After);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
173
8cca30f @isaacs zlib binding cleanup
isaacs authored
174 return ctx->handle_;
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
175 }
176
177
178 // thread pool!
179 // This function may be called multiple times on the uv_work pool
180 // for a single write() call, until all of the input bytes have
181 // been consumed.
07701e7 @indutny zlib: C++ style fixes
indutny authored
182 static void Process(uv_work_t* work_req) {
44eb279 zlib: don't use C++ templates
ssuda authored
183 ZCtx *ctx = container_of(work_req, ZCtx, work_req_);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
184
185 // If the avail_out is left at 0, then it means that it ran out
186 // of room. If there was avail_out left over, then it means
187 // that all of the input was consumed.
44eb279 zlib: don't use C++ templates
ssuda authored
188 switch (ctx->mode_) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
189 case DEFLATE:
190 case GZIP:
191 case DEFLATERAW:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
192 ctx->err_ = deflate(&ctx->strm_, ctx->flush_);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
193 break;
194 case UNZIP:
195 case INFLATE:
196 case GUNZIP:
197 case INFLATERAW:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
198 ctx->err_ = inflate(&ctx->strm_, ctx->flush_);
e609195 @indutny [zlib] added dictionary support
indutny authored
199
200 // If data was encoded with dictionary
1c32eb4 @bnoordhuis zlib: don't assert on missing dictionary
bnoordhuis authored
201 if (ctx->err_ == Z_NEED_DICT && ctx->dictionary_ != NULL) {
202
203 // Load it
204 ctx->err_ = inflateSetDictionary(&ctx->strm_,
205 ctx->dictionary_,
206 ctx->dictionary_len_);
207 if (ctx->err_ == Z_OK) {
208
209 // And try to decode again
210 ctx->err_ = inflate(&ctx->strm_, ctx->flush_);
d7bf7ed @bnoordhuis zlib: don't assert on malformed dictionary
bnoordhuis authored
211 } else if (ctx->err_ == Z_DATA_ERROR) {
212
213 // Both inflateSetDictionary() and inflate() return Z_DATA_ERROR.
214 // Make it possible for After() to tell a bad dictionary from bad
215 // input.
216 ctx->err_ = Z_NEED_DICT;
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
217 }
e609195 @indutny [zlib] added dictionary support
indutny authored
218 }
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
219 break;
220 default:
221 assert(0 && "wtf?");
222 }
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
223
224 // pass any errors back to the main thread to deal with.
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
225
226 // now After will emit the output, and
227 // either schedule another call to Process,
228 // or shift the queue and call Process.
229 }
230
231 // v8 land!
07701e7 @indutny zlib: C++ style fixes
indutny authored
232 static void After(uv_work_t* work_req) {
d104bfd @isaacs zlib: Fix test so that it's not trivially passing, then pass it.
isaacs authored
233 HandleScope scope;
44eb279 zlib: don't use C++ templates
ssuda authored
234 ZCtx *ctx = container_of(work_req, ZCtx, work_req_);
07701e7 @indutny zlib: C++ style fixes
indutny authored
235
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
236 // Acceptable error states depend on the type of zlib stream.
237 switch (ctx->err_) {
238 case Z_OK:
239 case Z_STREAM_END:
240 case Z_BUF_ERROR:
241 // normal statuses, not fatal
242 break;
1c32eb4 @bnoordhuis zlib: don't assert on missing dictionary
bnoordhuis authored
243 case Z_NEED_DICT:
d7bf7ed @bnoordhuis zlib: don't assert on malformed dictionary
bnoordhuis authored
244 if (ctx->dictionary_ == NULL) {
245 ZCtx::Error(ctx, "Missing dictionary");
246 } else {
247 ZCtx::Error(ctx, "Bad dictionary");
248 }
1c32eb4 @bnoordhuis zlib: don't assert on missing dictionary
bnoordhuis authored
249 return;
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
250 default:
251 // something else.
252 ZCtx::Error(ctx, "Zlib error");
253 return;
254 }
255
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
256 Local<Integer> avail_out = Integer::New(ctx->strm_.avail_out);
257 Local<Integer> avail_in = Integer::New(ctx->strm_.avail_in);
258
8cca30f @isaacs zlib binding cleanup
isaacs authored
259 ctx->write_in_progress_ = false;
260
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
261 // call the write() cb
8cca30f @isaacs zlib binding cleanup
isaacs authored
262 assert(ctx->handle_->Get(callback_sym)->IsFunction() &&
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
263 "Invalid callback");
264 Local<Value> args[2] = { avail_in, avail_out };
a26bee8 @isaacs MakeCallback: Consistent symbol usage
isaacs authored
265 MakeCallback(ctx->handle_, callback_sym, ARRAY_SIZE(args), args);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
266
0ad2717 @bnoordhuis Make sure that zlib contexts are not garbage collected when busy
bnoordhuis authored
267 ctx->Unref();
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
268 }
269
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
270 static void Error(ZCtx *ctx, const char *msg_) {
271 const char *msg;
272 if (ctx->strm_.msg != NULL) {
273 msg = ctx->strm_.msg;
274 } else {
275 msg = msg_;
276 }
277
278 assert(ctx->handle_->Get(onerror_sym)->IsFunction() &&
279 "Invalid error handler");
280 HandleScope scope;
281 Local<Value> args[2] = { String::New(msg),
282 Local<Value>::New(Number::New(ctx->err_)) };
a26bee8 @isaacs MakeCallback: Consistent symbol usage
isaacs authored
283 MakeCallback(ctx->handle_, onerror_sym, ARRAY_SIZE(args), args);
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
284
285 // no hope of rescue.
ef94521 @bnoordhuis zlib: fix assert on bad input
bnoordhuis authored
286 ctx->write_in_progress_ = false;
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
287 ctx->Unref();
288 }
289
07701e7 @indutny zlib: C++ style fixes
indutny authored
290 static Handle<Value> New(const Arguments& args) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
291 HandleScope scope;
44eb279 zlib: don't use C++ templates
ssuda authored
292 if (args.Length() < 1 || !args[0]->IsInt32()) {
293 return ThrowException(Exception::TypeError(String::New("Bad argument")));
294 }
295 node_zlib_mode mode = (node_zlib_mode) args[0]->Int32Value();
296
297 if (mode < DEFLATE || mode > UNZIP) {
298 return ThrowException(Exception::TypeError(String::New("Bad argument")));
299 }
300
301 ZCtx *ctx = new ZCtx(mode);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
302 ctx->Wrap(args.This());
303 return args.This();
304 }
305
306 // just pull the ints out of the args and call the other Init
07701e7 @indutny zlib: C++ style fixes
indutny authored
307 static Handle<Value> Init(const Arguments& args) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
308 HandleScope scope;
309
e609195 @indutny [zlib] added dictionary support
indutny authored
310 assert((args.Length() == 4 || args.Length() == 5) &&
311 "init(windowBits, level, memLevel, strategy, [dictionary])");
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
312
44eb279 zlib: don't use C++ templates
ssuda authored
313 ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This());
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
314
315 int windowBits = args[0]->Uint32Value();
316 assert((windowBits >= 8 && windowBits <= 15) && "invalid windowBits");
317
318 int level = args[1]->Uint32Value();
319 assert((level >= -1 && level <= 9) && "invalid compression level");
320
321 int memLevel = args[2]->Uint32Value();
322 assert((memLevel >= 1 && memLevel <= 9) && "invalid memlevel");
323
324 int strategy = args[3]->Uint32Value();
325 assert((strategy == Z_FILTERED ||
326 strategy == Z_HUFFMAN_ONLY ||
327 strategy == Z_RLE ||
328 strategy == Z_FIXED ||
329 strategy == Z_DEFAULT_STRATEGY) && "invalid strategy");
330
e609195 @indutny [zlib] added dictionary support
indutny authored
331 char* dictionary = NULL;
332 size_t dictionary_len = 0;
333 if (args.Length() >= 5 && Buffer::HasInstance(args[4])) {
334 Local<Object> dictionary_ = args[4]->ToObject();
335
336 dictionary_len = Buffer::Length(dictionary_);
337 dictionary = new char[dictionary_len];
338
339 memcpy(dictionary, Buffer::Data(dictionary_), dictionary_len);
340 }
341
342 Init(ctx, level, windowBits, memLevel, strategy,
343 dictionary, dictionary_len);
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
344 SetDictionary(ctx);
345 return Undefined();
346 }
347
348 static Handle<Value> Reset(const Arguments &args) {
349 HandleScope scope;
350
44eb279 zlib: don't use C++ templates
ssuda authored
351 ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This());
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
352
353 Reset(ctx);
354 SetDictionary(ctx);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
355 return Undefined();
356 }
357
07701e7 @indutny zlib: C++ style fixes
indutny authored
358 static void Init(ZCtx *ctx, int level, int windowBits, int memLevel,
9e6957b @indutny Merge branch 'v0.6'
indutny authored
359 int strategy, char* dictionary, size_t dictionary_len) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
360 ctx->level_ = level;
361 ctx->windowBits_ = windowBits;
362 ctx->memLevel_ = memLevel;
363 ctx->strategy_ = strategy;
364
365 ctx->strm_.zalloc = Z_NULL;
366 ctx->strm_.zfree = Z_NULL;
367 ctx->strm_.opaque = Z_NULL;
368
369 ctx->flush_ = Z_NO_FLUSH;
370
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
371 ctx->err_ = Z_OK;
372
44eb279 zlib: don't use C++ templates
ssuda authored
373 if (ctx->mode_ == GZIP || ctx->mode_ == GUNZIP) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
374 ctx->windowBits_ += 16;
375 }
376
44eb279 zlib: don't use C++ templates
ssuda authored
377 if (ctx->mode_ == UNZIP) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
378 ctx->windowBits_ += 32;
379 }
380
44eb279 zlib: don't use C++ templates
ssuda authored
381 if (ctx->mode_ == DEFLATERAW || ctx->mode_ == INFLATERAW) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
382 ctx->windowBits_ *= -1;
383 }
384
44eb279 zlib: don't use C++ templates
ssuda authored
385 switch (ctx->mode_) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
386 case DEFLATE:
387 case GZIP:
388 case DEFLATERAW:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
389 ctx->err_ = deflateInit2(&ctx->strm_,
390 ctx->level_,
391 Z_DEFLATED,
392 ctx->windowBits_,
393 ctx->memLevel_,
394 ctx->strategy_);
6b99fd2 @bnoordhuis zlib: pass object size hint to V8
bnoordhuis authored
395 V8::AdjustAmountOfExternalAllocatedMemory(kDeflateContextSize);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
396 break;
397 case INFLATE:
398 case GUNZIP:
399 case INFLATERAW:
400 case UNZIP:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
401 ctx->err_ = inflateInit2(&ctx->strm_, ctx->windowBits_);
6b99fd2 @bnoordhuis zlib: pass object size hint to V8
bnoordhuis authored
402 V8::AdjustAmountOfExternalAllocatedMemory(kInflateContextSize);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
403 break;
404 default:
405 assert(0 && "wtf?");
406 }
407
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
408 if (ctx->err_ != Z_OK) {
409 ZCtx::Error(ctx, "Init error");
410 }
411
e609195 @indutny [zlib] added dictionary support
indutny authored
412
413 ctx->dictionary_ = reinterpret_cast<Bytef *>(dictionary);
414 ctx->dictionary_len_ = dictionary_len;
415
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
416 ctx->write_in_progress_ = false;
417 ctx->init_done_ = true;
418 }
419
420 static void SetDictionary(ZCtx* ctx) {
421 if (ctx->dictionary_ == NULL) return;
e609195 @indutny [zlib] added dictionary support
indutny authored
422
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
423 ctx->err_ = Z_OK;
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
424
44eb279 zlib: don't use C++ templates
ssuda authored
425 switch (ctx->mode_) {
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
426 case DEFLATE:
427 case DEFLATERAW:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
428 ctx->err_ = deflateSetDictionary(&ctx->strm_,
429 ctx->dictionary_,
430 ctx->dictionary_len_);
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
431 break;
432 default:
433 break;
e609195 @indutny [zlib] added dictionary support
indutny authored
434 }
435
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
436 if (ctx->err_ != Z_OK) {
437 ZCtx::Error(ctx, "Failed to set dictionary");
438 }
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
439 }
440
441 static void Reset(ZCtx* ctx) {
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
442 ctx->err_ = Z_OK;
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
443
44eb279 zlib: don't use C++ templates
ssuda authored
444 switch (ctx->mode_) {
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
445 case DEFLATE:
446 case DEFLATERAW:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
447 ctx->err_ = deflateReset(&ctx->strm_);
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
448 break;
449 case INFLATE:
450 case INFLATERAW:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
451 ctx->err_ = inflateReset(&ctx->strm_);
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
452 break;
453 default:
454 break;
455 }
456
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
457 if (ctx->err_ != Z_OK) {
458 ZCtx::Error(ctx, "Failed to reset stream");
459 }
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
460 }
461
462 private:
6b99fd2 @bnoordhuis zlib: pass object size hint to V8
bnoordhuis authored
463 static const int kDeflateContextSize = 16384; // approximate
464 static const int kInflateContextSize = 10240; // approximate
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
465
466 bool init_done_;
467
468 z_stream strm_;
469 int level_;
470 int windowBits_;
471 int memLevel_;
472 int strategy_;
473
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
474 int err_;
475
e609195 @indutny [zlib] added dictionary support
indutny authored
476 Bytef* dictionary_;
477 size_t dictionary_len_;
478
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
479 int flush_;
480
481 int chunk_size_;
8cca30f @isaacs zlib binding cleanup
isaacs authored
482
483 bool write_in_progress_;
484
485 uv_work_t work_req_;
44eb279 zlib: don't use C++ templates
ssuda authored
486 node_zlib_mode mode_;
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
487 };
488
489
490 void InitZlib(Handle<Object> target) {
491 HandleScope scope;
492
44eb279 zlib: don't use C++ templates
ssuda authored
493 Local<FunctionTemplate> z = FunctionTemplate::New(ZCtx::New);
494
495 z->InstanceTemplate()->SetInternalFieldCount(1);
496
497 NODE_SET_PROTOTYPE_METHOD(z, "write", ZCtx::Write);
498 NODE_SET_PROTOTYPE_METHOD(z, "init", ZCtx::Init);
8d14668 @bnoordhuis zlib: reduce memory consumption, release early
bnoordhuis authored
499 NODE_SET_PROTOTYPE_METHOD(z, "clear", ZCtx::Clear);
44eb279 zlib: don't use C++ templates
ssuda authored
500 NODE_SET_PROTOTYPE_METHOD(z, "reset", ZCtx::Reset);
501
502 z->SetClassName(String::NewSymbol("Zlib"));
503 target->Set(String::NewSymbol("Zlib"), z->GetFunction());
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
504
505 callback_sym = NODE_PSYMBOL("callback");
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
506 onerror_sym = NODE_PSYMBOL("onerror");
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
507
508 NODE_DEFINE_CONSTANT(target, Z_NO_FLUSH);
509 NODE_DEFINE_CONSTANT(target, Z_PARTIAL_FLUSH);
510 NODE_DEFINE_CONSTANT(target, Z_SYNC_FLUSH);
511 NODE_DEFINE_CONSTANT(target, Z_FULL_FLUSH);
512 NODE_DEFINE_CONSTANT(target, Z_FINISH);
513 NODE_DEFINE_CONSTANT(target, Z_BLOCK);
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
514
515 // return/error codes
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
516 NODE_DEFINE_CONSTANT(target, Z_OK);
517 NODE_DEFINE_CONSTANT(target, Z_STREAM_END);
518 NODE_DEFINE_CONSTANT(target, Z_NEED_DICT);
519 NODE_DEFINE_CONSTANT(target, Z_ERRNO);
520 NODE_DEFINE_CONSTANT(target, Z_STREAM_ERROR);
521 NODE_DEFINE_CONSTANT(target, Z_DATA_ERROR);
522 NODE_DEFINE_CONSTANT(target, Z_MEM_ERROR);
523 NODE_DEFINE_CONSTANT(target, Z_BUF_ERROR);
524 NODE_DEFINE_CONSTANT(target, Z_VERSION_ERROR);
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
525
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
526 NODE_DEFINE_CONSTANT(target, Z_NO_COMPRESSION);
527 NODE_DEFINE_CONSTANT(target, Z_BEST_SPEED);
528 NODE_DEFINE_CONSTANT(target, Z_BEST_COMPRESSION);
529 NODE_DEFINE_CONSTANT(target, Z_DEFAULT_COMPRESSION);
530 NODE_DEFINE_CONSTANT(target, Z_FILTERED);
531 NODE_DEFINE_CONSTANT(target, Z_HUFFMAN_ONLY);
532 NODE_DEFINE_CONSTANT(target, Z_RLE);
533 NODE_DEFINE_CONSTANT(target, Z_FIXED);
534 NODE_DEFINE_CONSTANT(target, Z_DEFAULT_STRATEGY);
535 NODE_DEFINE_CONSTANT(target, ZLIB_VERNUM);
536
44eb279 zlib: don't use C++ templates
ssuda authored
537 NODE_DEFINE_CONSTANT(target, DEFLATE);
538 NODE_DEFINE_CONSTANT(target, INFLATE);
539 NODE_DEFINE_CONSTANT(target, GZIP);
540 NODE_DEFINE_CONSTANT(target, GUNZIP);
541 NODE_DEFINE_CONSTANT(target, DEFLATERAW);
542 NODE_DEFINE_CONSTANT(target, INFLATERAW);
543 NODE_DEFINE_CONSTANT(target, UNZIP);
544
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
545 target->Set(String::NewSymbol("ZLIB_VERSION"), String::New(ZLIB_VERSION));
546 }
547
548 } // namespace node
549
cdcb111 @bnoordhuis Remove stray NODE_MODULE() semi-colons.
bnoordhuis authored
550 NODE_MODULE(node_zlib, node::InitZlib)
Something went wrong with that request. Please try again.