Skip to content

Commit

Permalink
Use inflateInit2() so that gzip header is read by default, and allow
Browse files Browse the repository at this point in the history
consumers of this library to specify a windowSize.

Also, change the build system to use cmake in the hopes that this
will actually compile on other platforms (only tested OS-X).
  • Loading branch information
Brian Maher committed Jan 29, 2010
1 parent 93c0eb9 commit 5002717
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 43 deletions.
41 changes: 41 additions & 0 deletions CMakeLists.txt
@@ -0,0 +1,41 @@
# Copyright (C) 2007-2009 LuaDist.
# Submitted by David Manura
# Redistribution and use of this file is allowed according to the
# terms of the MIT license.
# For details see the COPYRIGHT file distributed with LuaDist.
# Please note that the package source code is licensed under its own
# license.

PROJECT(lua-zlib C)
CMAKE_MINIMUM_REQUIRED (VERSION 2.6)

# Basic configurations
SET(INSTALL_CMOD share/lua/cmod CACHE PATH "Directory to install Lua binary modules (configure lua via LUA_CPATH)")
# / configs

# Find zlib
FIND_PACKAGE(ZLIB REQUIRED)
# / Find zlib

# Find lua
FIND_PACKAGE(Lua51 REQUIRED)
# / Find lua

# Define how to build zlib.so:
INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS} ${LUA_INCLUDE_DIR})
ADD_LIBRARY(cmod_zlib MODULE
lua_zlib.c zlib.def)
SET_TARGET_PROPERTIES(cmod_zlib PROPERTIES PREFIX "")
SET_TARGET_PROPERTIES(cmod_zlib PROPERTIES OUTPUT_NAME zlib)
TARGET_LINK_LIBRARIES(cmod_zlib ${LUA_LIBRARIES} ${ZLIB_LIBRARIES})
# / build zlib.so

# Define how to test zlib.so:
INCLUDE(CTest)
FIND_PROGRAM(LUA NAMES lua lua.bat)
ADD_TEST(basic ${LUA} ${CMAKE_CURRENT_SOURCE_DIR}/test.lua ${CMAKE_CURRENT_SOURCE_DIR}/ ${CMAKE_CURRENT_BINARY_DIR}/)
# / test zlib.so

# Where to install stuff
INSTALL (TARGETS cmod_zlib DESTINATION ${INSTALL_CMOD})
# / Where to install.
4 changes: 0 additions & 4 deletions Makefile

This file was deleted.

12 changes: 10 additions & 2 deletions README
Expand Up @@ -28,6 +28,9 @@
To use this library, you need zlib, get it here:
http://www.gzip.org/zlib/

To build this library, you need CMake, get it here:
http://www.cmake.org/cmake/resources/software.html

Loading the library:

If you built the library as a loadable package
Expand Down Expand Up @@ -78,10 +81,15 @@ function stream = zlib.deflate([ int compression_level ])
The eof result is true if 'finish' was specified, otherwise
it is false.

stream = zlib.inflate()
stream = zlib.inflate([windowBits])

Returns a "stream" function that decompresses (or inflates) all
strings passed in. Specifically, use it as such:
strings passed in. Optionally specify a windowBits argument
that is passed to inflateInit2(), see zlib.h for details about
this argument. By default, gzip header detection is done, and
the max window size is used.

The "stream" function should be used as such:

string inflated, bool eof = stream(string input)

Expand Down
20 changes: 14 additions & 6 deletions lua_zlib.c
Expand Up @@ -13,9 +13,9 @@ static int lz_inflate(lua_State *L);
//////////////////////////////////////////////////////////////////////
static int lz_version(lua_State *L) {
const char* version = zlibVersion();
int count = strlen(version) + 1;
char* cur = (char*)memcpy(lua_newuserdata(L, count),
version, count);
int count = strlen(version) + 1;
char* cur = (char*)memcpy(lua_newuserdata(L, count),
version, count);

count = 0;
while ( *cur ) {
Expand Down Expand Up @@ -231,9 +231,17 @@ static int lz_inflate_new(lua_State *L) {
// Allocate the stream:
z_stream* stream = (z_stream*)lua_newuserdata(L, sizeof(z_stream));

stream->zalloc = Z_NULL;
stream->zfree = Z_NULL;
lz_assert(L, inflateInit(stream), stream, __FILE__, __LINE__);
// By default, we will do gzip header detection w/ max window
// size:
int window_size = lua_isnumber(L, 1) ? lua_tonumber(L, 1)
: MAX_WBITS + 32;

stream->zalloc = Z_NULL;
stream->zfree = Z_NULL;
stream->next_in = Z_NULL;
stream->avail_in = 0;

lz_assert(L, inflateInit2(stream, window_size), stream, __FILE__, __LINE__);

// Don't allow destructor to execute unless deflateInit was successful:
luaL_getmetatable(L, "lz.inflate.meta");
Expand Down
30 changes: 0 additions & 30 deletions module.mk

This file was deleted.

9 changes: 9 additions & 0 deletions tap.lua
@@ -1,12 +1,21 @@
local os = require("os")
module(..., package.seeall)

local counter = 1
local failed = false

function ok(assert_true, desc)
local msg = ( assert_true and "ok " or "not ok " ) .. counter
if ( not assert_true ) then
failed = true
end
if ( desc ) then
msg = msg .. " - " .. desc
end
print(msg)
counter = counter + 1
end

function exit()
os.exit(failed and 1 or 0)
end
6 changes: 5 additions & 1 deletion test.lua
@@ -1,4 +1,8 @@
print "1..6"
print "1..9"

local src_dir, build_dir = ...
package.path = src_dir .. "?.lua;" .. package.path
package.cpath = build_dir .. "?.so;" .. package.cpath

local tap = require("tap")
local lz = require("zlib")
Expand Down
2 changes: 2 additions & 0 deletions zlib.def
@@ -0,0 +1,2 @@
EXPORTS
luaopen_zlib

0 comments on commit 5002717

Please sign in to comment.