-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlua-fastlz.c
118 lines (90 loc) · 2.58 KB
/
lua-fastlz.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "fastlz.h"
#define large_malloc(s) (malloc(((int)(s/4096)+1)*4096))
static char temp_buf[4096];
int lua_f_fastlz_compress ( lua_State *L )
{
if ( !lua_isstring ( L, 1 ) ) {
lua_pushnil ( L );
return 1;
}
size_t vlen = 0;
const char *value = lua_tolstring ( L, 1, &vlen );
if ( vlen < 1 || vlen > 1 * 1024 * 1024 ) { /// Max 1Mb !!!
lua_pushnil ( L );
return 1;
}
char *dst = ( char * ) &temp_buf;
if ( vlen + 20 > 4096 ) {
dst = large_malloc ( vlen + 20 );
}
if ( dst == NULL ) {
lua_pushnil ( L );
lua_pushstring ( L, "not enough memory!" );
return 2;
}
const unsigned int size_header = htonl ( vlen );
memcpy ( dst, &size_header, sizeof ( unsigned int ) );
int dlen = fastlz_compress ( value, vlen, dst + sizeof ( unsigned int ) );
if ( dst ) {
lua_pushlstring ( L, dst, dlen + sizeof ( unsigned int ) );
if ( dst != ( char * ) &temp_buf ) {
free ( dst );
}
return 1;
} else {
lua_pushnil ( L );
return 1;
}
}
int lua_f_fastlz_decompress ( lua_State *L )
{
if ( !lua_isstring ( L, 1 ) ) {
lua_pushnil ( L );
return 1;
}
size_t vlen = 0;
const char *value = lua_tolstring ( L, 1, &vlen );
if ( vlen < 1 ) {
lua_pushnil ( L );
return 1;
}
unsigned int value_len = 0;
memcpy ( &value_len, value, sizeof ( unsigned int ) );
value_len = ntohl ( value_len );
if ( value_len > 1024 * 1024 + 20 ) {
lua_pushnil ( L );
lua_pushstring ( L, "not enough memory!" );
return 2;
}
char *dst = ( char * ) &temp_buf;
if ( value_len + 20 > 4096 ) {
dst = ( unsigned char * ) large_malloc ( value_len + 20 );
}
if ( dst == NULL ) {
lua_pushnil ( L );
lua_pushstring ( L, "not enough memory!" );
return 2;
}
int dlen = fastlz_decompress ( value + sizeof ( unsigned int ),
vlen - sizeof ( unsigned int ), dst, value_len + 20 );
if ( dst ) {
lua_pushlstring ( L, dst, value_len );
if ( dst != ( char * ) &temp_buf ) {
free ( dst );
}
return 1;
} else {
lua_pushnil ( L );
return 1;
}
}
LUALIB_API int luaopen_fastlz ( lua_State *L )
{
lua_register ( L, "fastlz_compress", lua_f_fastlz_compress );
lua_register ( L, "fastlz_decompress", lua_f_fastlz_decompress );
}