diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..0d681a8 --- /dev/null +++ b/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. diff --git a/Makefile b/Makefile deleted file mode 100644 index 7f61ca2..0000000 --- a/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -SRC:=$(abspath $(dir $(lastword $(MAKEFILE_LIST)))) -include $(or $(shell make-common),$(error make-common is not on your PATH!)) - -include $(SRC)/module.mk diff --git a/README b/README index 1ca37f5..f1abf8f 100644 --- a/README +++ b/README @@ -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 @@ -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) diff --git a/lua_zlib.c b/lua_zlib.c index c38b06f..53a4035 100644 --- a/lua_zlib.c +++ b/lua_zlib.c @@ -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 ) { @@ -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"); diff --git a/module.mk b/module.mk deleted file mode 100644 index 65c848b..0000000 --- a/module.mk +++ /dev/null @@ -1,30 +0,0 @@ -_pwd := $(pwd) - -include $(make-common.dir)/tool/cc.mk -include $(make-common.dir)/tool/lua.mk -include $(make-common.dir)/layout.mk - -_lib := $(lua.c.lib.dir)/zlib.so -_objs := $(call cc.c.to.o,$(addprefix $(_pwd)/, \ - lua_zlib.c \ -)) - -all: | $(_lib) -$(_lib): cc.libs += lua z -$(_lib): cc.objs := $(_objs) -$(_lib): $(_objs) - $(cc.so.rule) - -# How to run lua_zlib tests: -.PHONY: lua_zlib.test -test: | lua_zlib.test - -lua ?= lua -lua_zlib.test: | $(lua.c.lib.dir)/zlib.so -lua_zlib.test: lua.path += $(_pwd) -lua_zlib.test: $(wildcard $(_pwd)/test*) - @mkdir -p $(tmp.dir) - cd $(tmp.dir); for t in $<; do \ - echo "TESTING: $$t"; \ - env -i $(lua.run) $(lua) $$t; \ - done diff --git a/tap.lua b/tap.lua index fbe49cc..1008cf6 100644 --- a/tap.lua +++ b/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 \ No newline at end of file diff --git a/test.lua b/test.lua index 72b6a72..566c8f7 100644 --- a/test.lua +++ b/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") diff --git a/zlib.def b/zlib.def new file mode 100644 index 0000000..d6c5a91 --- /dev/null +++ b/zlib.def @@ -0,0 +1,2 @@ +EXPORTS +luaopen_zlib