From f0f8146b662fa7cf00652ff943992cd60747def5 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Fri, 28 Nov 2014 10:48:57 +0400 Subject: [PATCH 1/3] Fix. Build with Lua 5.3.beta --- lakeconfig.lua | 165 +++++++++++++++++++++++++++++++++++++++++++++++++ lakefile | 6 +- lzlib.c | 12 ++++ 3 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 lakeconfig.lua diff --git a/lakeconfig.lua b/lakeconfig.lua new file mode 100644 index 0000000..5ae4396 --- /dev/null +++ b/lakeconfig.lua @@ -0,0 +1,165 @@ +function vc_version() + local VER = lake.compiler_version() + MSVC_VER = ({ + [15] = '9'; + [16] = '10'; + })[VER.MAJOR] or '' + return MSVC_VER +end + +local function arkey(t) + assert(type(t) == 'table') + local keys = {} + for k in pairs(t) do + assert(type(k) == 'number') + table.insert(keys, k) + end + table.sort(keys) + return keys +end + +local function ikeys(t) + local keys = arkey(t) + local i = 0 + return function() + i = i + 1 + local k = keys[i] + if k == nil then return end + return k, t[k] + end +end + +local function expand(arr, t) + if t == nil then return arr end + + if type(t) ~= 'table' then + table.insert(arr, t) + return arr + end + + for _, v in ikeys(t) do + expand(arr, v) + end + + return arr +end + +function L(...) + return expand({}, {...}) +end + +J = J or path.join + +IF = IF or lake.choose or choose + +function prequire(...) + local ok, mod = pcall(require, ...) + if ok then return mod end +end + +function each_join(dir, list) + for i, v in ipairs(list) do + list[i] = path.join(dir, v) + end + return list +end + +function run(file, cwd) + print() + print("run " .. file) + if not TESTING then + if cwd then lake.chdir(cwd) end + local status, code = utils.execute( LUA_RUNNER .. ' ' .. file ) + if cwd then lake.chdir("<") end + print() + return status, code + end + return true, 0 +end + +function run_test(name, params) + local test_dir = J(ROOT, 'test') + local cmd = J(test_dir, name) + if params then cmd = cmd .. ' ' .. params end + local ok = run(cmd, test_dir) + print("TEST " .. cmd .. (ok and ' - pass!' or ' - fail!')) +end + +function spawn(file, cwd) + local winapi = prequire "winapi" + if not winapi then + print(file, ' error: Test needs winapi!') + return false + end + print("spawn " .. file) + if not TESTING then + if cwd then lake.chdir(cwd) end + assert(winapi.shell_exec(nil, LUA_RUNNER, file, cwd)) + if cwd then lake.chdir("<") end + print() + end + return true +end + +function as_bool(v,d) + if v == nil then return not not d end + local n = tonumber(v) + if n == 0 then return false end + if n then return true end + return false +end + +----------------------- +-- needs -- +----------------------- + +lake.define_need('lua53', function() + return { + incdir = J(ENV.LUA_DIR_5_3, 'include'); + libdir = J(ENV.LUA_DIR_5_3, 'lib'); + libs = {'lua53'}; + } +end) + +lake.define_need('lua52', function() + return { + incdir = J(ENV.LUA_DIR_5_2, 'include'); + libdir = J(ENV.LUA_DIR_5_2, 'lib'); + libs = {'lua52'}; + } +end) + +lake.define_need('lua51', function() + return { + incdir = J(ENV.LUA_DIR, 'include'); + libdir = J(ENV.LUA_DIR, 'lib'); + libs = {'lua5.1'}; + } +end) + +local ZLIB_DIR = ZLIB_DIR or ENV.ZLIB_DIR or J(ENV.CPPLIB_DIR, "zlib", "1.2.7") + +lake.define_need('zlib-static-md', function() + local lib + if MSVC then lib = "zlib_vc" .. vc_version() .. "_md" + else lib = "z" end + + return { + incdir = J(ZLIB_DIR, 'include'); + libdir = J(ZLIB_DIR, 'static'); + libs = {lib}; + } +end) + +lake.define_need('zlib-static-mt', function() + local lib + if MSVC then lib = "zlib_vc" .. vc_version() .. "_mt" + else lib = "z" end + + return { + incdir = J(ZLIB_DIR, 'include'); + libdir = J(ZLIB_DIR, 'static'); + libs = {lib}; + } +end) + diff --git a/lakefile b/lakefile index c322f41..7ee6e67 100644 --- a/lakefile +++ b/lakefile @@ -3,7 +3,11 @@ PROJECT = 'zlib' IF=choose J=path.join -if LUA_VER == '5.2' then +if LUA_VER == '5.3' then + LUA_NEED = 'lua53' + LUA_DIR = ENV.LUA_DIR_5_3 or ENV.LUA_DIR + LUA_RUNNER = 'lua53' +elseif LUA_VER == '5.2' then LUA_NEED = 'lua52' LUA_DIR = ENV.LUA_DIR_5_2 or ENV.LUA_DIR LUA_RUNNER = 'lua52' diff --git a/lzlib.c b/lzlib.c index b51dbea..11f670f 100644 --- a/lzlib.c +++ b/lzlib.c @@ -64,6 +64,18 @@ #define LZ_BUFFER_SIZE 8192 #endif +#if LUA_VERSION_NUM >= 503 /* Lua 5.3 */ + +#ifndef luaL_checkint +#define luaL_checkint luaL_checkinteger +#endif + +#ifndef luaL_optint +#define luaL_optint luaL_optinteger +#endif + +#endif + typedef struct { /* zlib structures */ z_stream zstream; From acce149284c3e41ca3edc235311a9acf970b987b Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Tue, 2 Dec 2014 18:33:34 +0400 Subject: [PATCH 2/3] Fix. crc32/adler32 on platform with sizeof(int) == 4. --- lakefile | 1 + lzlib.c | 18 ++++++++++++++---- test_crc32.lua | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 test_crc32.lua diff --git a/lakefile b/lakefile index 7ee6e67..f3e6be8 100644 --- a/lakefile +++ b/lakefile @@ -58,5 +58,6 @@ target('test', install, function() run(J(test_dir,'test_zlib2.lua'), test_dir) run(J(test_dir,'test_prologue.lua'), test_dir) run(J(test_dir,'test_gzip.lua'), test_dir) + run(J(test_dir,'test_crc32.lua'), test_dir) end) diff --git a/lzlib.c b/lzlib.c index 9f26581..ea23a00 100644 --- a/lzlib.c +++ b/lzlib.c @@ -714,6 +714,14 @@ static int lzlib_version(lua_State *L) return 1; } +#if UINT_MAX <= 0xFFFFFFFF +# define check_u32(L, idx) 0xFFFFFFFF & (uLong)luaL_checknumber(L, idx) +# define push_u32(L, v) lua_pushnumber(L, 0xFFFFFFFF & (uLong)v) +#else +# define check_u32(L, idx) 0xFFFFFFFF & (uLong)luaL_checkinteger(L, idx) +# define push_u32(L, v) lua_pushinteger(L, 0xFFFFFFFF & (uLong)v) +#endif + /* ====================================================================== */ static int lzlib_adler32(lua_State *L) { @@ -726,10 +734,11 @@ static int lzlib_adler32(lua_State *L) { /* update adler32 checksum */ size_t len; - int adler = luaL_checkint(L, 1); + uLong adler = check_u32(L, 1); + const unsigned char* buf = (unsigned char*)luaL_checklstring(L, 2, &len); - lua_pushnumber(L, adler32(adler, buf, len)); + push_u32(L, adler32(adler, buf, len)); } return 1; } @@ -746,10 +755,11 @@ static int lzlib_crc32(lua_State *L) { /* update crc32 checksum */ size_t len; - int crc = luaL_checkint(L, 1); + uLong crc = check_u32(L, 1); + const unsigned char* buf = (unsigned char*)luaL_checklstring(L, 2, &len); - lua_pushnumber(L, crc32(crc, buf, len)); + push_u32(L, crc32(crc, buf, len)); } return 1; } diff --git a/test_crc32.lua b/test_crc32.lua new file mode 100644 index 0000000..9b0b291 --- /dev/null +++ b/test_crc32.lua @@ -0,0 +1,20 @@ +local zlib = require 'zlib' +local chunk = ("0"):rep(1024) +local crc = zlib.crc32() + +assert(0 == crc) + +crc = zlib.crc32(crc, chunk) +assert(2900260604 == crc) + +crc = zlib.crc32(crc, chunk) +assert(3309519361 == crc) + +crc = zlib.crc32(crc, chunk) +assert(3284388706 == crc) + +crc = zlib.crc32(crc, chunk) +assert(2038734619 == crc) + +print("Done!") + From 7ea647cdb9e9830653bcefab50257e67c820bbb3 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Wed, 3 Dec 2014 10:29:15 +0400 Subject: [PATCH 3/3] Fix. Use lua_Integer on Lua 5.3 --- lzlib.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/lzlib.c b/lzlib.c index ea23a00..c5efc8f 100644 --- a/lzlib.c +++ b/lzlib.c @@ -714,13 +714,30 @@ static int lzlib_version(lua_State *L) return 1; } -#if UINT_MAX <= 0xFFFFFFFF -# define check_u32(L, idx) 0xFFFFFFFF & (uLong)luaL_checknumber(L, idx) -# define push_u32(L, v) lua_pushnumber(L, 0xFFFFFFFF & (uLong)v) -#else -# define check_u32(L, idx) 0xFFFFFFFF & (uLong)luaL_checkinteger(L, idx) -# define push_u32(L, v) lua_pushinteger(L, 0xFFFFFFFF & (uLong)v) -#endif +#define STATIC_ASSERT(A) {(int(*)[(A)?1:0])0;} + +uLong check_u32(lua_State *L, int idx) +{ + STATIC_ASSERT(sizeof(uLong)>=4); + if(sizeof(lua_Integer) > 4) + { + return 0xFFFFFFFF & luaL_checkinteger(L, idx); + } + return 0xFFFFFFFF & (uLong)luaL_checknumber(L, idx); +} + +void push_u32(lua_State *L, uLong val) +{ + STATIC_ASSERT(sizeof(uLong)>=4); + if(sizeof(lua_Integer) > 4) + { + lua_pushinteger(L, 0xFFFFFFFF & val); + } + else + { + lua_pushnumber(L, 0xFFFFFFFF & (uLong)val); + } +} /* ====================================================================== */ static int lzlib_adler32(lua_State *L) @@ -728,7 +745,7 @@ static int lzlib_adler32(lua_State *L) if (lua_gettop(L) == 0) { /* adler32 initial value */ - lua_pushnumber(L, adler32(0L, Z_NULL, 0)); + push_u32(L, adler32(0L, Z_NULL, 0)); } else { @@ -749,7 +766,7 @@ static int lzlib_crc32(lua_State *L) if (lua_gettop(L) == 0) { /* crc32 initial value */ - lua_pushnumber(L, crc32(0L, Z_NULL, 0)); + push_u32(L, crc32(0L, Z_NULL, 0)); } else {