-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathminilz4.c
More file actions
54 lines (47 loc) · 1.41 KB
/
minilz4.c
File metadata and controls
54 lines (47 loc) · 1.41 KB
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
// This code is part of lua2exe
// Very simple LZ4 for lua
#include<lua/lua.h>
#include<lua/lualib.h>
#include<lua/lauxlib.h>
#include<stdlib.h>
#include<stdio.h>
#include<stdint.h>
#include<lz4.h>
#ifdef __WIN32
#define LIBEXPORT __declspec(dllexport)
#else
#define LIBEXPORT
#endif
static int lz4c (lua_State* L) {
static char* out=NULL; static size_t outlen=0;
size_t inlen=0;
const char* in = luaL_checklstring(L, 1, &inlen);
int level = luaL_optinteger(L, 2, 1);
if (!out || outlen<inlen+10) out = realloc(out, outlen = inlen+10);
if (!out) return luaL_error(L, "memory allocation fail");
int relen = LZ4_compress_fast(in, out, inlen, outlen, level);
if (relen<0) return luaL_error(L, "Compression error %d", relen);
lua_pushlstring(L, out, relen);
return 1;
}
static int lz4d (lua_State* L) {
static char* out=NULL; static size_t outlen=0;
size_t inlen=0;
const char* in = luaL_checklstring(L, 1, &inlen);
size_t neededlen = luaL_optinteger(L, 2, inlen*10+10);
if (!out || outlen<neededlen) out = realloc(out, outlen=neededlen);
if (!out) return luaL_error(L, "memory allocation fail");
int relen = LZ4_decompress_safe(in, out, inlen, outlen);
if (relen<0) luaL_error(L, "decompression error %d", relen);
lua_pushlstring(L, out, relen);
return 1;
}
static const luaL_Reg lz4f[] = {
{ "compress", lz4c },
{ "decompress", lz4d },
{ NULL, NULL }
};
int LIBEXPORT luaopen_minilz4 (lua_State* L) {
luaL_newlib(L, lz4f);
return 1;
}