From 8abf31744c41eacee7ea835aca7866735a30a4f0 Mon Sep 17 00:00:00 2001
From: Pixel <pixel@nobis-crew.org>
Date: Sun, 20 Jun 2010 20:51:57 -0700
Subject: Adding levels and ability to raw-compress in zlib_deflate.

---
 lib/Handle.cc    | 10 +++++-----
 lib/LuaHandle.cc | 10 ++++++++--
 2 files changed, 13 insertions(+), 7 deletions(-)

(limited to 'lib')

diff --git a/lib/Handle.cc b/lib/Handle.cc
index 457f2c9..e88663a 100644
--- a/lib/Handle.cc
+++ b/lib/Handle.cc
@@ -887,8 +887,8 @@ int Handle::zlib_inflate(Handle * in, Handle * out) throw (GeneralException) {
     have = total_out = 0;
 
     ret = inflateInit(&s);
-    if (ret != Z_OK ) {
-	throw GeneralException("zlib: deflateInit() failed: " + String(ret));
+    if (ret != Z_OK) {
+	throw GeneralException("zlib: inflateInit() failed: " + String(ret));
     }
 
     do {
@@ -927,7 +927,7 @@ int Handle::zlib_inflate(Handle * in, Handle * out) throw (GeneralException) {
     return total_out;
 }
 
-int Handle::zlib_deflate(Handle * in, Handle * out) throw (GeneralException) {
+int Handle::zlib_deflate(Handle * in, Handle * out, int level, bool raw) throw (GeneralException) {
     int ret, flush;
     z_stream s;
     unsigned char b_in[CHUNK];
@@ -944,8 +944,8 @@ int Handle::zlib_deflate(Handle * in, Handle * out) throw (GeneralException) {
 
     have = total_out = 0;
 
-    ret = deflateInit(&s, Z_DEFAULT_COMPRESSION);
-    if (ret != Z_OK ) {
+    ret = deflateInit2(&s, level, Z_DEFLATED, raw ? -15 : 15, 8, Z_DEFAULT_STRATEGY);
+    if (ret != Z_OK) {
         throw GeneralException("zlib: deflateInit() failed: " + String(ret));
     }
 
diff --git a/lib/LuaHandle.cc b/lib/LuaHandle.cc
index 207c4e7..789876e 100644
--- a/lib/LuaHandle.cc
+++ b/lib/LuaHandle.cc
@@ -829,15 +829,21 @@ int sLuaHandle::zlib_deflate(lua_State * __L) {
     int n = L->gettop();
     Handle * s, * d;
     int r;
+    int level = Z_DEFAULT_COMPRESSION;
+    bool raw = false;
 
-    if (n != 2) {
+    if ((n < 2) || (n > 4) || ((n >= 3) && !L->isnumber()) || ((n == 4) && !L->isboolean())) {
         L->error("Incorrect arguments to method `Handle::zlib_deflate'");
     }
 
     s = L->recast<Handle>(1);
     d = L->recast<Handle>(2);
+    if (n >= 3)
+        level = L->tonumber(3);
+    if (n == 4)
+        raw = L->toboolean(4);
 
-    r = Handle::zlib_deflate(s, d);
+    r = Handle::zlib_deflate(s, d, level, raw);
 
     L->push((lua_Number) r);
 
-- 
cgit v1.2.3