Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lua_Integer instead of lua_Number for checksum calculation #53

Open
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 50 additions & 23 deletions lua_zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@
#define luaL_optint(L,n,d) ((int)luaL_optinteger(L,(n),(d)))
#endif


/*
** Memory Allocation: zlib uses lua-Allocator
*/

static voidpf lua_zalloc( voidpf opaque, uInt items, uInt size )
{
void* ud;
lua_Alloc allocf = lua_getallocf( (lua_State*)opaque, &ud );

return allocf( ud, NULL, 0, items * size );
}

static void lua_zfree(voidpf opaque, voidpf address)
{
void* ud;
lua_Alloc allocf = lua_getallocf( (lua_State*)opaque, &ud );

allocf( ud, address, 0, 0 );
}


#ifdef LZLIB_COMPAT
/**************** lzlib compatibility **********************************/
/* Taken from https://raw.githubusercontent.com/LuaDist/lzlib/93b88e931ffa7cd0a52a972b6b26d37628f479f3/lzlib.c */
Expand Down Expand Up @@ -132,8 +154,9 @@ static lz_stream *lzstream_new(lua_State *L, int src) {
s->o_buffer_len = 0;
s->o_buffer_max = sizeof(s->o_buffer) / sizeof(s->o_buffer[0]);

s->zstream.zalloc = Z_NULL;
s->zstream.zfree = Z_NULL;
s->zstream.zalloc = lua_zalloc;
s->zstream.zfree = lua_zfree;
s->zstream.opaque = L;

/* prepare source */
if (lua_isstring(L, src)) {
Expand Down Expand Up @@ -726,8 +749,9 @@ static int lzlib_compress(lua_State *L) {

luaL_buffinit(L, &b);

zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.zalloc = lua_zalloc;
zs.zfree = lua_zfree;
zs.opaque = L;

zs.next_out = Z_NULL;
zs.avail_out = 0;
Expand Down Expand Up @@ -788,8 +812,9 @@ static int lzlib_decompress(lua_State *L)

luaL_buffinit(L, &b);

zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.zalloc = lua_zalloc;
zs.zfree = lua_zfree;
zs.opaque = L;

zs.next_out = Z_NULL;
zs.avail_out = 0;
Expand Down Expand Up @@ -1053,8 +1078,9 @@ static int lz_deflate_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;
stream->zalloc = lua_zalloc;
stream->zfree = lua_zfree;
stream->opaque = L;

result = deflateInit2(stream, level, Z_DEFLATED, window_size,
DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
Expand Down Expand Up @@ -1109,8 +1135,9 @@ static int lz_inflate_new(lua_State *L) {
/* By default, we will do gzip header detection w/ max window size */
int window_size = lua_isnumber(L, 1) ? lua_tointeger(L, 1) : MAX_WBITS + 32;

stream->zalloc = Z_NULL;
stream->zfree = Z_NULL;
stream->zalloc = lua_zalloc;
stream->zfree = lua_zfree;
stream->opaque = L;
stream->next_in = Z_NULL;
stream->avail_in = 0;

Expand Down Expand Up @@ -1148,21 +1175,21 @@ static int lz_checksum(lua_State *L) {

lua_pushvalue(L, 1);
lua_call(L, 0, 2);
if ( ! lua_isnumber(L, -2) || ! lua_isnumber(L, -1) ) {
if ( ! lua_isinteger(L, -2) || ! lua_isinteger(L, -1) ) {
luaL_argerror(L, 1, "expected function to return two numbers");
}

/* Calculate and replace the checksum */
lua_pushnumber(L,
combine((uLong)lua_tonumber(L, lua_upvalueindex(3)),
(uLong)lua_tonumber(L, -2),
(z_off_t)lua_tonumber(L, -1)));
lua_pushinteger(L,
combine((uLong)lua_tointeger(L, lua_upvalueindex(3)),
(uLong)lua_tointeger(L, -2),
(z_off_t)lua_tointeger(L, -1)));
lua_pushvalue(L, -1);
lua_replace(L, lua_upvalueindex(3));

/* Calculate and replace the length */
lua_pushnumber(L,
lua_tonumber(L, lua_upvalueindex(4)) + lua_tonumber(L, -2));
lua_pushinteger(L,
lua_tointeger(L, lua_upvalueindex(4)) + lua_tointeger(L, -2));
lua_pushvalue(L, -1);
lua_replace(L, lua_upvalueindex(4));
} else {
Expand All @@ -1174,16 +1201,16 @@ static int lz_checksum(lua_State *L) {
str = (const Bytef*)luaL_checklstring(L, 1, &len);

/* Calculate and replace the checksum */
lua_pushnumber(L,
checksum((uLong)lua_tonumber(L, lua_upvalueindex(3)),
lua_pushinteger(L,
checksum((uLong)lua_tointeger(L, lua_upvalueindex(3)),
str,
len));
lua_pushvalue(L, -1);
lua_replace(L, lua_upvalueindex(3));

/* Calculate and replace the length */
lua_pushnumber(L,
lua_tonumber(L, lua_upvalueindex(4)) + len);
lua_pushinteger(L,
lua_tointeger(L, lua_upvalueindex(4)) + len);
lua_pushvalue(L, -1);
lua_replace(L, lua_upvalueindex(4));
}
Expand All @@ -1193,8 +1220,8 @@ static int lz_checksum(lua_State *L) {
static int lz_checksum_new(lua_State *L, checksum_t checksum, checksum_combine_t combine) {
lua_pushlightuserdata(L, (void *)checksum);
lua_pushlightuserdata(L, (void *)combine);
lua_pushnumber(L, checksum(0L, Z_NULL, 0));
lua_pushnumber(L, 0);
lua_pushinteger(L, checksum(0L, Z_NULL, 0));
lua_pushinteger(L, 0);
lua_pushcclosure(L, lz_checksum, 4);
return 1;
}
Expand Down
2 changes: 2 additions & 0 deletions test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ local function test_illegal_state()
end

local function test_checksum()
ok(lz.crc32()("123456789") == 0xCBF43926, "crc32()(\"123456789\") == 0xCBF43926")
ok(lz.adler32()("123456789") == 0x091E01DE, "adler()(\"123456789\") == 0x091E01DE")
for _, factory in pairs{lz.crc32, lz.adler32} do
local csum = factory()("one two")

Expand Down