-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_lzf.c
69 lines (58 loc) · 1.6 KB
/
lua_lzf.c
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
#define LUA_LIB
#include <core.h>
#include <lzf.h>
static int llzf_compress(lua_State *L) {
size_t tsize;
const char *text = luaL_checklstring(L, 1, &tsize);
if (!text || tsize < 1)
return luaL_error(L, "[lzf_compress error]: Invalid buffer.");
size_t bsize = LZF_MAX_COMPRESSED_SIZE(tsize);
char *buffer = lua_newuserdata(L, bsize);
int rsize;
if ((rsize = lzf_compress(text, tsize, buffer, bsize)) < 1)
return luaL_error(L, "[lzf_compress error]: Can't compressed text.");
lua_pushlstring(L, buffer, rsize);
return 1;
}
static int llzf_uncompress(lua_State *L) {
size_t tsize;
const char *text = luaL_checklstring(L, 1, &tsize);
if (!text || tsize < 1)
return luaL_error(L, "[lzf_compress error]: Invalid buffer.");
size_t bsize = tsize * 2;
for (;;) {
char* buffer = lua_newuserdata(L, bsize);
int ret = lzf_decompress(text, tsize, buffer, bsize);
printf("ret = %d\n", ret);
if (ret == 0) {
if (errno == EINVAL) {
errno = 0;
bsize *= 2;
lua_pop(L, 1);
continue;
}
if (errno == EINVAL) {
lua_pushboolean(L, 0);
lua_pushliteral(L, "[lzf_decompress error]: Invalid text buffer.");
return 2;
}
return luaL_error(L, "[lzf_decompress error]: Unknown error.");
}
if (ret > 0) {
lua_pushlstring(L, buffer, ret);
break;
}
}
return 1;
}
LUAMOD_API int
luaopen_llzf(lua_State *L) {
luaL_checkversion(L);
luaL_Reg lzf_libs[] = {
{"compress", llzf_compress},
{"uncompress", llzf_uncompress},
{NULL, NULL},
};
luaL_newlib(L, lzf_libs);
return 1;
}