diff --git a/lua/zapret-lib.lua b/lua/zapret-lib.lua index 5392e5e..d4f73ee 100644 --- a/lua/zapret-lib.lua +++ b/lua/zapret-lib.lua @@ -1436,13 +1436,15 @@ function is_gzip_file(filename) f:close() return hdr and hdr=="\x1F\x8B" end --- ungzips file to raw string -function gunzip_file(filename, read_block_size) +-- ungzip file to raw string +-- expected_ratio = uncompressed_size/compressed_size (default 4) +function gunzip_file(filename, expected_ratio, read_block_size) local f, err = io.open(filename, "r") if not f then error("gunzip_file: "..err) end if not read_block_size then read_block_size=16384 end + if not expected_ratio then expected_ratio=4 end local decompressed="" gz = gunzip_init() @@ -1460,7 +1462,7 @@ function gunzip_file(filename, read_block_size) return nil end end - local decomp, eof = gunzip_inflate(gz, compressed) + local decomp, eof = gunzip_inflate(gz, compressed, #compressed * expected_ratio) if not decomp then f:close() gunzip_end(gz) @@ -1472,15 +1474,17 @@ function gunzip_file(filename, read_block_size) gunzip_end(gz) return decompressed end --- zips file to raw string +-- zip file to raw string +-- expected_ratio = uncompressed_size/compressed_size (default 2) -- level : 1..9 (default 9) -- memlevel : 1..8 (default 8) -function gzip_file(filename, data, level, memlevel, compress_block_size) +function gzip_file(filename, data, expected_ratio, level, memlevel, compress_block_size) local f, err = io.open(filename, "w") if not f then error("gzip_file: "..err) end if not write_block_size then compress_block_size=16384 end + if not expected_ratio then expected_ratio=2 end gz = gzip_init(nil, level, memlevel) if not gz then @@ -1490,7 +1494,7 @@ function gzip_file(filename, data, level, memlevel, compress_block_size) repeat block_size = #data-off+1 if block_size>compress_block_size then block_size=compress_block_size end - local comp, eof = gzip_deflate(gz, string.sub(data,off,off+block_size-1)) + local comp, eof = gzip_deflate(gz, string.sub(data,off,off+block_size-1), block_size / expected_ratio) if not comp then f:close() gzip_end(gz) diff --git a/nfq2/lua.c b/nfq2/lua.c index b1d5835..d540f56 100644 --- a/nfq2/lua.c +++ b/nfq2/lua.c @@ -2667,9 +2667,9 @@ static int luacall_gunzip_end(lua_State *L) LUA_STACK_GUARD_RETURN(L,0) } -#define BUFMIN 128 +#define BUFMIN 64 #define Z_INFL_BUF_INCREMENT 16384 -#define Z_DEFL_BUF_INCREMENT 4096 +#define Z_DEFL_BUF_INCREMENT 8192 static int luacall_gunzip_inflate(lua_State *L) { // gunzip_inflate(zstream, compressed_data, expected_uncompressed_chunk_size) return decompressed_data @@ -2685,8 +2685,7 @@ static int luacall_gunzip_inflate(lua_State *L) struct userdata_zs *uzs = lua_uzs(L, 1, true); uzs->zs.next_in = (z_const Bytef*)luaL_checklstring(L,2,&l); uzs->zs.avail_in = (uInt)l; - size_t bufchunk = argc>=3 ? luaL_checkinteger(L,3) : l*3; - + size_t bufchunk = argc>=3 ? luaL_checkinteger(L,3) : l*4; do { if ((bufsize - size) < BUFMIN) @@ -2797,7 +2796,7 @@ static int luacall_gzip_deflate(lua_State *L) uzs->zs.next_in = (z_const Bytef*)luaL_checklstring(L,2,&l); uzs->zs.avail_in = (uInt)l; } - size_t bufchunk = argc>=3 ? luaL_checkinteger(L,3) : 1+l/3; + size_t bufchunk = BUFMIN + (argc>=3 ? luaL_checkinteger(L,3) : l/2); do {