Permalink
Browse files

refactored deflator

  • Loading branch information...
1 parent fdd9790 commit 18c15179a8002650eb9543556fda3ee1df47ae48 John Hewson committed Aug 31, 2011
Showing with 56 additions and 102 deletions.
  1. +56 −102 src/deflate.cc
View
@@ -73,86 +73,6 @@ class Deflater : ObjectWrap {
int output_chunk_size;
public:
-
- Deflater() {
- input = NULL;
- output_length = 0;
- output = NULL;
- finished = false;
- }
-
- int Init(int level, int windowBits, int memLevel, int strategy, int chunk_size) {
- strm.zalloc = Z_NULL;
- strm.zfree = Z_NULL;
- strm.opaque = Z_NULL;
- output_chunk_size = chunk_size;
- return deflateInit2(&strm, level, Z_DEFLATED, windowBits, memLevel, strategy);
- }
-
- int SetInput(int in_length) {
- strm.avail_in = in_length;
- strm.next_in = (Bytef*) input;
-
- if (output == NULL || output_length == output_chunk_size) {
- strm.avail_out = output_chunk_size;
- output = (char*) malloc(output_chunk_size);
-
- if (output == NULL) {
- return Z_MEM_ERROR;
- }
-
- strm.next_out = (Bytef*) output;
- }
-
- return Z_OK;
- }
-
- void ResetOutput() {
- strm.avail_out = output_chunk_size;
- strm.next_out = (Bytef*) output;
- }
-
- int Deflate() {
- int ret = deflate(&strm, Z_NO_FLUSH);
- status = ret;
- output_length = output_chunk_size - strm.avail_out;
- return ret;
- }
-
- int Finish() {
- strm.avail_in = 0;
- strm.next_in = NULL;
-
- int ret = deflate(&strm, Z_FINISH);
- status = ret;
-
- output_length = output_chunk_size - strm.avail_out;
-
- if (ret == Z_STREAM_END) {
- deflateEnd(&strm);
- finished = true;
- }
-
- return ret;
- }
-
- bool IsOutputBufferFull() {
- return output_length == output_chunk_size;
- }
-
- bool HasFinished() {
- return finished;
- }
-
- int GetOutputLength() {
- return output_length;
- }
-
- char* GetOutput() {
- return output;
- }
-
- // node.js wrapper //
static void Init(v8::Handle<v8::Object> target) {
Local<FunctionTemplate> t = FunctionTemplate::New(New);
@@ -244,10 +164,19 @@ class Deflater : ObjectWrap {
}
}
- Deflater *deflater = new Deflater();
- deflater->Wrap(args.This());
+ Deflater *self = new Deflater();
+ self->Wrap(args.This());
+
+ self->input = NULL;
+ self->output_length = 0;
+ self->output = NULL;
+ self->finished = false;
- int r = deflater->Init(level, windowBits, memLevel, strategy, output_chunk_size);
+ self->strm.zalloc = Z_NULL;
+ self->strm.zfree = Z_NULL;
+ self->strm.opaque = Z_NULL;
+ self->output_chunk_size = output_chunk_size;
+ int r = deflateInit2(&(self->strm), level, Z_DEFLATED, windowBits, memLevel, strategy);
if (r < 0) {
return GetZError(r);
@@ -257,52 +186,74 @@ class Deflater : ObjectWrap {
}
static Handle<Value> SetInput(const Arguments& args) {
- Deflater *deflater = ObjectWrap::Unwrap<Deflater>(args.This());
+ Deflater *self = ObjectWrap::Unwrap<Deflater>(args.This());
HandleScope scope;
Local<Object> in = args[0]->ToObject();
ssize_t length = Buffer::Length(in);
// copy the input buffer, because it can be kept for several deflate() calls
- deflater->input = (char*)realloc(deflater->input, length);
+ self->input = (char*)realloc(self->input, length);
- if (deflater->input == NULL) {
+ if (self->input == NULL) {
return GetZError(Z_MEM_ERROR);
}
- memcpy(deflater->input, Buffer::Data(in), length);
+ memcpy(self->input, Buffer::Data(in), length);
- int r = deflater->SetInput(length);
+ self->strm.avail_in = length;
+ self->strm.next_in = (Bytef*) self->input;
- if (r < 0) {
- return GetZError(r);
+ if (self->output == NULL || self->output_length == self->output_chunk_size) {
+ self->strm.avail_out = self->output_chunk_size;
+ self->output = (char*) malloc(self->output_chunk_size);
+
+ if (self->output == NULL) {
+ return GetZError(Z_MEM_ERROR);
+ }
+
+ self->strm.next_out = (Bytef*) self->output;
}
return scope.Close(Undefined());
}
static Handle<Value> Deflate(const Arguments& args) {
- Deflater *deflater = ObjectWrap::Unwrap<Deflater>(args.This());
+ Deflater *self = ObjectWrap::Unwrap<Deflater>(args.This());
HandleScope scope;
- int r = deflater->Deflate();
+ int r = deflate(&(self->strm), Z_NO_FLUSH);
+ self->status = r;
+ self->output_length = self->output_chunk_size - self->strm.avail_out;
if (r < 0) {
return GetZError(r);
}
- return scope.Close(Boolean::New(deflater->IsOutputBufferFull()));
+ bool isOutputBufferFull = self->output_length == self->output_chunk_size;
+ return scope.Close(Boolean::New(isOutputBufferFull));
}
static Handle<Value> Finish(const Arguments& args) {
- Deflater *deflater = ObjectWrap::Unwrap<Deflater>(args.This());
+ Deflater *self = ObjectWrap::Unwrap<Deflater>(args.This());
HandleScope scope;
- if (deflater->HasFinished()) {
+ if (self->finished) {
return scope.Close(False());
}
- int r = deflater->Finish();
+ self->strm.avail_in = 0;
+ self->strm.next_in = NULL;
+
+ int r = deflate(&(self->strm), Z_FINISH);
+ self->status = r;
+
+ self->output_length = self->output_chunk_size - self->strm.avail_out;
+
+ if (r == Z_STREAM_END) {
+ deflateEnd(&(self->strm));
+ self->finished = true;
+ }
if (r < 0) {
return GetZError(r);
@@ -312,19 +263,22 @@ class Deflater : ObjectWrap {
}
static Handle<Value> GetOutput(const Arguments& args) {
- Deflater *deflater = ObjectWrap::Unwrap<Deflater>(args.This());
+ Deflater *self = ObjectWrap::Unwrap<Deflater>(args.This());
HandleScope scope;
- Buffer* slowBuffer = Buffer::New(deflater->GetOutput(), deflater->GetOutputLength());
- deflater->ResetOutput();
+ Buffer* slowBuffer = Buffer::New(self->output, self->output_length);
+
+ // reset output
+ self->strm.avail_out = self->output_chunk_size;
+ self->strm.next_out = (Bytef*) self->output;
- if (deflater->HasFinished()) {
- free(deflater->GetOutput());
+ if (self->finished) {
+ free(self->output);
}
Local<Object> globalObj = Context::GetCurrent()->Global();
Local<Function> bufferConstructor = Local<Function>::Cast(globalObj->Get(String::New("Buffer")));
- Handle<Value> constructorArgs[3] = { slowBuffer->handle_, Integer::New(deflater->GetOutputLength()), Integer::New(0) };
+ Handle<Value> constructorArgs[3] = { slowBuffer->handle_, Integer::New(self->output_length), Integer::New(0) };
Local<Object> jsBuffer = bufferConstructor->NewInstance(3, constructorArgs);
return scope.Close(jsBuffer);

0 comments on commit 18c1517

Please sign in to comment.