Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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