1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#include <zlib.h>
/* from lsdllib.c */
void* check_const_ptr(lua_State *L, int narg);
void* check_ptr(lua_State *L, int narg);
static int lzlib_compress(lua_State *L) {
uLong destLen = luaL_checkinteger(L, 2);
int status = compress(check_ptr(L, 1), &destLen, check_const_ptr(L, 3), luaL_checkinteger(L, 4));
if(status == Z_MEM_ERROR) luaL_error(L, "out of memory");
if(status == Z_BUF_ERROR) luaL_error(L, "too small output buffer");
lua_pushinteger(L, destLen);
return 1;
}
static int lzlib_uncompress(lua_State *L) {
uLong destLen = luaL_checkinteger(L, 2);
int status = uncompress(check_ptr(L, 1), &destLen, check_const_ptr(L, 3), luaL_checkinteger(L, 4));
if(status == Z_MEM_ERROR) luaL_error(L, "out of memory");
if(status == Z_BUF_ERROR) luaL_error(L, "too small output buffer");
if(status == Z_DATA_ERROR) luaL_error(L, "malformed data");
lua_pushinteger(L, destLen);
return 1;
}
static int lzlib_compressBound(lua_State *L) {
lua_pushinteger(L, compressBound(luaL_checkint(L, 1)));
return 1;
}
static int uncompressZip(Bytef* dest, uLongf* destLen,
const Bytef* source, uLong sourceLen) {
/* Normal uncompress() can't uncompress ZIPs. This is a modified version
* which can. The only difference that it replaces inflateInit(&stream)
* with inflateInit2(&stream, -MAX_WBITS). See also inflateInit2 docs in
* zlib.h (why oh why isn't it on zlib.net?) for an explanation.
* Reference: Ur-Quan Masters 0.6.2, src/sc2code/libs/uio/zip/zip.c
*/
z_stream stream;
int err;
stream.next_in = (Bytef*)source; stream.avail_in = (uInt)sourceLen;
/* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
stream.next_out = dest; stream.avail_out = (uInt)*destLen;
if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
stream.zalloc = (alloc_func)0; stream.zfree = (free_func)0;
err = inflateInit2(&stream, -MAX_WBITS); /* THE MAGIC */
if (err != Z_OK) return err;
err = inflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
inflateEnd(&stream);
if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
return Z_DATA_ERROR;
return err;
}
*destLen = stream.total_out;
return inflateEnd(&stream);
}
static int lzlib_uncompressZip(lua_State *L) {
uLong destLen = luaL_checkinteger(L, 2);
int status = uncompressZip(check_ptr(L, 1), &destLen, check_const_ptr(L, 3), luaL_checkinteger(L, 4));
if(status == Z_MEM_ERROR) luaL_error(L, "out of memory");
if(status == Z_BUF_ERROR) luaL_error(L, "too small output buffer");
if(status == Z_DATA_ERROR) luaL_error(L, "malformed data");
lua_pushinteger(L, destLen);
return 1;
}
int luaopen_zlib(lua_State *L) {
const luaL_Reg reg_zlib[] = {
{ "compress", lzlib_compress },
{ "uncompress", lzlib_uncompress },
{ "compressBound", lzlib_compressBound },
{ "uncompressZip", lzlib_uncompressZip },
{ NULL, NULL }
};
lua_settop(L, 0);
luaL_register(L, "zlib", reg_zlib);
return 1;
}
|