summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Handle.cc118
-rw-r--r--lib/LuaHandle.cc47
2 files changed, 163 insertions, 2 deletions
diff --git a/lib/Handle.cc b/lib/Handle.cc
index 7313d92..479c055 100644
--- a/lib/Handle.cc
+++ b/lib/Handle.cc
@@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-/* $Id: Handle.cc,v 1.81 2008-01-21 17:19:07 pixel Exp $ */
+/* $Id: Handle.cc,v 1.82 2008-05-13 06:59:10 pixel Exp $ */
#include <stdio.h>
#include <string.h>
@@ -700,3 +700,119 @@ int Handle::nclose() throw (GeneralException) {
int Handle::GetNbHandles() {
return nb_handles;
}
+
+#define CHUNK 10240
+//
+// shamelessly ripped from http://www.zlib.net/zpipe.c
+//
+int Handle::zlib_inflate(Handle * in, Handle * out) throw (GeneralException) {
+ int ret;
+ z_stream s;
+ unsigned char b_in[CHUNK];
+ unsigned char b_out[CHUNK];
+ unsigned int have, total_out;
+
+ s.zalloc = (alloc_func) 0;
+ s.zfree = (free_func) 0;
+ s.opaque = (voidpf) 0;
+ s.next_in = Z_NULL;
+ s.next_out = Z_NULL;
+ s.avail_in = 0;
+ s.avail_out = 0;
+
+ have = total_out = 0;
+
+ ret = inflateInit(&s);
+ if (ret != Z_OK ) {
+ throw GeneralException("zlib: deflateInit() failed: " + String(ret));
+ }
+
+ do {
+ s.avail_in = in->read(b_in, CHUNK);
+
+ if (s.avail_in == 0)
+ break;
+
+ s.next_in = b_in;
+
+ do {
+ s.avail_out = CHUNK;
+ s.next_out = b_out;
+ ret = inflate(&s, Z_NO_FLUSH);
+ if (ret == Z_STREAM_ERROR) {
+ throw GeneralException("inflate returned Z_STREAM_ERROR: " + String(s.msg));
+ }
+ switch (ret) {
+ case Z_NEED_DICT:
+ ret = Z_DATA_ERROR;
+ case Z_DATA_ERROR:
+ case Z_MEM_ERROR:
+ inflateEnd(&s);
+ throw GeneralException("inflate returned an erroneous state: " + String(ret) + " - " + String(s.msg));
+ }
+ have = CHUNK - s.avail_out;
+ if (out->write(b_out, have) != have) {
+ throw GeneralException("Output file not writable.");
+ }
+ total_out += have;
+ } while (s.avail_out == 0);
+ } while (ret != Z_STREAM_END);
+
+ inflateEnd(&s);
+
+ return total_out;
+}
+
+int Handle::zlib_deflate(Handle * in, Handle * out) throw (GeneralException) {
+ int ret, flush;
+ z_stream s;
+ unsigned char b_in[CHUNK];
+ unsigned char b_out[CHUNK];
+ unsigned int have, total_out;
+
+ s.zalloc = (alloc_func) 0;
+ s.zfree = (free_func) 0;
+ s.opaque = (voidpf) 0;
+ s.next_in = Z_NULL;
+ s.next_out = Z_NULL;
+ s.avail_in = 0;
+ s.avail_out = 0;
+
+ have = total_out = 0;
+
+ ret = deflateInit(&s, 9);
+ if (ret != Z_OK ) {
+ throw GeneralException("zlib: deflateInit() failed: " + String(ret));
+ }
+
+ do {
+ s.avail_in = in->read(b_in, CHUNK);
+
+ flush = s.avail_in == 0 ? Z_FINISH : Z_NO_FLUSH;
+ s.next_in = b_in;
+
+ do {
+ s.avail_out = CHUNK;
+ s.next_out = b_out;
+ ret = deflate(&s, flush);
+ if (ret == Z_STREAM_ERROR) {
+ throw GeneralException("inflate returned Z_STREAM_ERROR: " + String(s.msg));
+ }
+ have = CHUNK - s.avail_out;
+ if (out->write(b_out, have) != have) {
+ deflateEnd(&s);
+ throw GeneralException("couldn't write properly to output.");
+ }
+
+ total_out += have;
+ } while (s.avail_out == 0);
+
+ if (s.avail_in != 0) {
+ throw GeneralException("All input not used.");
+ }
+ } while (flush != Z_FINISH);
+
+ deflateEnd(&s);
+
+ return total_out;
+}
diff --git a/lib/LuaHandle.cc b/lib/LuaHandle.cc
index d4a204a..22c5036 100644
--- a/lib/LuaHandle.cc
+++ b/lib/LuaHandle.cc
@@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-/* $Id: LuaHandle.cc,v 1.25 2008-01-24 10:49:59 pixel Exp $ */
+/* $Id: LuaHandle.cc,v 1.26 2008-05-13 06:59:10 pixel Exp $ */
#include "LuaHandle.h"
@@ -67,6 +67,8 @@ class sLuaHandle : public Base {
static int get_nb_input(lua_State * L);
static int get_nb_output(lua_State * L);
static int get_nb_buffer(lua_State * L);
+ static int zlib_inflate(lua_State * L);
+ static int zlib_deflate(lua_State * L);
private:
static int read(lua_State * L, int);
static int write(lua_State * L, int);
@@ -516,6 +518,46 @@ int sLuaHandle::get_nb_buffer(lua_State * __L) {
return 1;
}
+int sLuaHandle::zlib_inflate(lua_State * __L) {
+ Lua * L = Lua::find(__L);
+ int n = L->gettop();
+ Handle * s, * d;
+ int r;
+
+ if (n != 2) {
+ L->error("Incorrect arguments to method `Handle::zlib_inflate'");
+ }
+
+ s = (Handle *) LuaObject::getme(L, 1);
+ d = (Handle *) LuaObject::getme(L, 2);
+
+ r = Handle::zlib_inflate(s, d);
+
+ L->push((lua_Number) r);
+
+ return 1;
+}
+
+int sLuaHandle::zlib_deflate(lua_State * __L) {
+ Lua * L = Lua::find(__L);
+ int n = L->gettop();
+ Handle * s, * d;
+ int r;
+
+ if (n != 2) {
+ L->error("Incorrect arguments to method `Handle::zlib_deflate'");
+ }
+
+ s = (Handle *) LuaObject::getme(L, 1);
+ d = (Handle *) LuaObject::getme(L, 2);
+
+ r = Handle::zlib_deflate(s, d);
+
+ L->push((lua_Number) r);
+
+ return 1;
+}
+
int sLuaHandle::seek(lua_State * __L) {
Lua * L = Lua::find(__L);
int n = L->gettop();
@@ -732,4 +774,7 @@ void LuaHandle::pushconstruct(Lua * L) {
L->declarefunc("get_nb_input", sLuaHandle::get_nb_input);
L->declarefunc("get_nb_output", sLuaHandle::get_nb_output);
L->declarefunc("get_nb_buffer", sLuaHandle::get_nb_buffer);
+
+ L->declarefunc("zlib_inflate", sLuaHandle::zlib_inflate);
+ L->declarefunc("zlib_deflate", sLuaHandle::zlib_deflate);
}