Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Fix. crc32 on system with 32 bit lua_Integer. #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions lakeconfig.lua
Original file line number Diff line number Diff line change
@@ -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)

7 changes: 6 additions & 1 deletion lakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -54,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)

51 changes: 45 additions & 6 deletions lzlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -702,22 +714,48 @@ static int lzlib_version(lua_State *L)
return 1;
}

#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)
{
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
{
/* 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;
}
Expand All @@ -728,16 +766,17 @@ 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
{
/* 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;
}
Expand Down
20 changes: 20 additions & 0 deletions test_crc32.lua
Original file line number Diff line number Diff line change
@@ -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!")