/* zlib.deflate( sink: function | { write: function [, close: function, flush: function] }, compression level, [Z_DEFAILT_COMPRESSION] method, [Z_DEFLATED] windowBits, [15] memLevel, [8] strategy, [Z_DEFAULT_STRATEGY] ) */ static int lzlib_deflate(lua_State *L) { int level, method, windowBits, memLevel, strategy; lz_stream *s; if (lua_istable(L, 1) || lua_isuserdata(L, 1)) { /* is there a :write function? */ lua_getfield(L, 1, "write"); if (!lua_isfunction(L, -1)) { luaL_argerror(L, 1, "output parameter does not provide :write function"); } lua_pop(L, 1); } else if (!lua_isfunction(L, 1)) { luaL_argerror(L, 1, "output parameter must be a function, table or userdata value"); } level = luaL_optint(L, 2, Z_DEFAULT_COMPRESSION); method = luaL_optint(L, 3, Z_DEFLATED); windowBits = luaL_optint(L, 4, 15); memLevel = luaL_optint(L, 5, 8); strategy = luaL_optint(L, 6, Z_DEFAULT_STRATEGY); s = lzstream_new(L, 1); if (deflateInit2(&s->zstream, level, method, windowBits, memLevel, strategy) != Z_OK) { lua_pushliteral(L, "call to deflateInit2 failed"); lua_error(L); } s->state = LZ_DEFLATE; return 1; }
static int lzlib_inflate(lua_State *L) { int windowBits = luaL_optint(L, 1, 15); z_stream *s = lzstream_new(L); if (inflateInit2(s, windowBits) != Z_OK) { lua_pushliteral(L, "failed to start compressing"); lua_error(L); } s->opaque = LZINFLATE; return 1; }
/* zlib.inflate( source: string | function | { read: function, close: function }, windowBits: number, [15] dictionary: [""] ) */ static int lzlib_inflate(lua_State *L) { int windowBits; lz_stream *s; int have_peek = 0; const char *dictionary; size_t dictionary_len; if (lua_istable(L, 1) || lua_isuserdata(L, 1)) { /* is there a :read function? */ lua_getfield(L, 1, "read"); if (!lua_isfunction(L, -1)) { luaL_argerror(L, 1, "input parameter does not provide :read function"); } lua_pop(L, 1); /* check for peek function */ lua_getfield(L, 1, "peek"); have_peek = lua_isfunction(L, -1); lua_pop(L, 1); } else if (!lua_isstring(L, 1) && !lua_isfunction(L, 1)) { luaL_argerror(L, 1, "input parameter must be a string, function, table or userdata value"); } windowBits = luaL_optint(L, 2, 15); dictionary = luaL_optlstring(L, 3, NULL, &dictionary_len); s = lzstream_new(L, 1); if (windowBits > 0 && windowBits < 16) { windowBits |= 32; } if (inflateInit2(&s->zstream, windowBits) != Z_OK) { lua_pushliteral(L, "call to inflateInit2 failed"); lua_error(L); } if (dictionary) { s->dictionary = (const Bytef *) dictionary; s->dictionary_len = dictionary_len; } s->peek = have_peek; s->state = LZ_INFLATE; return 1; }
static int lzlib_deflate(lua_State *L) { int level = luaL_optint(L, 1, Z_DEFAULT_COMPRESSION); int method = luaL_optint(L, 2, Z_DEFLATED); int windowBits = luaL_optint(L, 3, 15); int memLevel = luaL_optint(L, 4, 8); int strategy = luaL_optint(L, 5, Z_DEFAULT_STRATEGY); z_stream *s = lzstream_new(L); if (deflateInit2(s, level, method, windowBits, memLevel, strategy) != Z_OK) { lua_pushliteral(L, "failed to start decompressing"); lua_error(L); } s->opaque = LZDEFLATE; return 1; }
/* zlib.deflate( sink: function | { write: function [, close: function, flush: function] }, compression level, [Z_DEFAILT_COMPRESSION] method, [Z_DEFLATED] windowBits, [15] memLevel, [8] strategy, [Z_DEFAULT_STRATEGY] dictionary: [""] ) */ static int lzlib_deflate(lua_State *L) { int level, method, windowBits, memLevel, strategy; lz_stream *s; const char *dictionary; size_t dictionary_len; if (lua_istable(L, 1) || lua_isuserdata(L, 1)) { /* is there a :write function? */ lua_getfield(L, 1, "write"); if (!lua_isfunction(L, -1)) { luaL_argerror(L, 1, "output parameter does not provide :write function"); } lua_pop(L, 1); } else if (!lua_isfunction(L, 1)) { luaL_argerror(L, 1, "output parameter must be a function, table or userdata value"); } level = (int) luaL_optinteger(L, 2, Z_DEFAULT_COMPRESSION); method = (int) luaL_optinteger(L, 3, Z_DEFLATED); windowBits = (int) luaL_optinteger(L, 4, 15); memLevel = (int) luaL_optinteger(L, 5, 8); strategy = (int) luaL_optinteger(L, 6, Z_DEFAULT_STRATEGY); dictionary = luaL_optlstring(L, 7, NULL, &dictionary_len); s = lzstream_new(L, 1); if (deflateInit2(&s->zstream, level, method, windowBits, memLevel, strategy) != Z_OK) { lua_pushliteral(L, "call to deflateInit2 failed"); lua_error(L); } if (dictionary) { if (deflateSetDictionary(&s->zstream, (const Bytef *) dictionary, dictionary_len) != Z_OK) { lua_pushliteral(L, "call to deflateSetDictionnary failed"); lua_error(L); } } s->state = LZ_DEFLATE; return 1; }