forked from eklitzke/lua-bz2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlbz2_stream.c
259 lines (218 loc) · 6.7 KB
/
lbz2_stream.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* This file implements the Lua binding to libbzip2.
*
* Copyright (c) 2008, Evan Klitzke <[email protected]>
* Copyright (c) 2012, Thomas Harning Jr <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <bzlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <assert.h>
#include "lbz2_stream.h"
#include "lbz2_common.h"
#define LBZ2_STREAM_MT "LBZ2_STREAM_MT"
typedef struct {
bz_stream bz_stream;
int isDecompressing;
} lbz2_stream;
static lbz2_stream *lbz2_check_stream(lua_State *L, int index) {
return (lbz2_stream *)luaL_checkudata(L, index, LBZ2_STREAM_MT);
}
static int lbz2_stream_initCompress(lua_State *L) {
lbz2_stream *stream;
int errorCode;
int blockSize100k = luaL_optinteger(L, 1, 9);
int verbosity = luaL_optinteger(L, 2, 0);
int workFactor = luaL_optinteger(L, 3, 0);
stream = lua_newuserdata(L, sizeof(*stream));
memset(stream, 0, sizeof(*stream));
stream->isDecompressing = 0;
luaL_getmetatable(L, LBZ2_STREAM_MT);
lua_setmetatable(L, -2);
errorCode = BZ2_bzCompressInit(&stream->bz_stream, blockSize100k, verbosity, workFactor);
if (BZ_OK != errorCode) {
lua_pushnil(L);
lua_pushstring(L, lbz2_error(errorCode));
return 2;
}
return 1;
}
static int lbz2_stream_initDecompress(lua_State *L) {
lbz2_stream *stream;
int errorCode;
int verbosity = luaL_optinteger(L, 1, 0);
int isSmall = lua_toboolean(L, 2);
stream = lua_newuserdata(L, sizeof(*stream));
memset(stream, 0, sizeof(*stream));
stream->isDecompressing = 1;
luaL_getmetatable(L, LBZ2_STREAM_MT);
lua_setmetatable(L, -2);
errorCode = BZ2_bzDecompressInit(&stream->bz_stream, verbosity, isSmall);
if (BZ_OK != errorCode) {
lua_pushnil(L);
lua_pushstring(L, lbz2_error(errorCode));
return 2;
}
return 1;
}
static int lbz2_stream_close(lua_State *L) {
lbz2_stream *stream = lbz2_check_stream(L, 1);
int errorCode = BZ_OK;
if (stream->bz_stream.state) {
if (stream->isDecompressing) {
errorCode = BZ2_bzDecompressEnd(&stream->bz_stream);
} else {
errorCode = BZ2_bzCompressEnd(&stream->bz_stream);
}
}
lua_pushnil(L);
lua_setmetatable(L, 1);
if (BZ_OK != errorCode) {
lua_pushnil(L);
lua_pushstring(L, lbz2_error(errorCode));
return 2;
}
lua_pushboolean(L, 1);
return 1;
}
static int lbz2_stream_perform_compress(lua_State *L, lbz2_stream *stream, int action) {
int errorCode = BZ_OK;
luaL_Buffer B;
luaL_buffinit(L, &B);
while (1) {
stream->bz_stream.avail_out = LUAL_BUFFERSIZE;
stream->bz_stream.next_out = luaL_prepbuffer(&B);
errorCode = BZ2_bzCompress(&stream->bz_stream, action);
switch (action) {
case BZ_RUN:
if (BZ_RUN_OK != errorCode) {
goto fail;
}
luaL_addsize(&B, LUAL_BUFFERSIZE - stream->bz_stream.avail_out);
if (stream->bz_stream.avail_in == 0 || stream->bz_stream.avail_out == LUAL_BUFFERSIZE) {
goto complete;
}
break;
case BZ_FLUSH:
if (BZ_FLUSH_OK != errorCode && BZ_RUN_OK != errorCode) {
goto fail;
}
luaL_addsize(&B, LUAL_BUFFERSIZE - stream->bz_stream.avail_out);
if (BZ_RUN_OK == errorCode) {
goto complete;
}
break;
case BZ_FINISH:
if (BZ_FINISH_OK != errorCode && BZ_STREAM_END != errorCode) {
goto fail;
}
luaL_addsize(&B, LUAL_BUFFERSIZE - stream->bz_stream.avail_out);
if (BZ_STREAM_END == errorCode) {
goto complete;
}
}
}
complete:
luaL_pushresult(&B);
return 1;
fail:
lua_pushnil(L);
lua_pushstring(L, lbz2_error(errorCode));
return 2;
}
static int lbz2_stream_perform_decompress(lua_State *L, lbz2_stream *stream) {
int errorCode = BZ_OK;
luaL_Buffer B;
luaL_buffinit(L, &B);
while (1) {
stream->bz_stream.avail_out = LUAL_BUFFERSIZE;
stream->bz_stream.next_out = luaL_prepbuffer(&B);
errorCode = BZ2_bzDecompress(&stream->bz_stream);
if (BZ_OK != errorCode && BZ_STREAM_END != errorCode) {
goto fail;
}
luaL_addsize(&B, LUAL_BUFFERSIZE - stream->bz_stream.avail_out);
/* Stream over with */
if (errorCode == BZ_STREAM_END) {
goto completeStream;
}
/* No more bytes left this round */
if (stream->bz_stream.avail_in == 0 && stream->bz_stream.avail_out == LUAL_BUFFERSIZE) {
goto complete;
}
}
complete:
luaL_pushresult(&B);
return 1;
completeStream:
luaL_pushresult(&B);
/* Report in addition to the data collected, the number of trailing bytes
* still available in the input buffer for other use. */
lua_pushinteger(L, stream->bz_stream.avail_in);
return 2;
fail:
lua_pushnil(L);
lua_pushstring(L, lbz2_error(errorCode));
return 2;
}
static int lbz2_stream_update(lua_State *L) {
lbz2_stream *stream = lbz2_check_stream(L, 1);
size_t dataLength;
const char *data = luaL_optlstring(L, 2, NULL, &dataLength);
/* Update the pointers and feed the output buffer while data is available */
stream->bz_stream.avail_in = dataLength;
/* Cast away const-ness since input data is never altered */
stream->bz_stream.next_in = (char *)data;
/* For compression, need to specially flag finishing state */
if (!stream->isDecompressing) {
return lbz2_stream_perform_compress(L, stream, !data ? BZ_FINISH : BZ_RUN);
} else {
return lbz2_stream_perform_decompress(L, stream);
}
}
static int lbz2_stream_flush(lua_State *L) {
lbz2_stream *stream = lbz2_check_stream(L, 1);
if (!stream->isDecompressing) {
return lbz2_stream_perform_compress(L, stream, BZ_FLUSH);
} else {
/* Invalid for decompression */
lua_pushnil(L);
lua_pushstring(L, lbz2_error(BZ_SEQUENCE_ERROR));
return 2;
}
}
static luaL_Reg lbz2_stream_ops[] = {
{ "update", lbz2_stream_update },
{ "flush", lbz2_stream_flush },
{ "close", lbz2_stream_close },
{ NULL, NULL }
};
static luaL_Reg lbz2_stream_global[] = {
{ "initCompress", lbz2_stream_initCompress },
{ "initDecompress", lbz2_stream_initDecompress },
{ NULL, NULL }
};
void register_lbz2_stream(lua_State *L) {
luaL_newmetatable(L, LBZ2_STREAM_MT);
lua_newtable(L);
luaL_register(L, NULL, lbz2_stream_ops);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lbz2_stream_close);
lua_setfield(L, -2, "__gc");
lua_pop(L, 1);
luaL_register(L, NULL, lbz2_stream_global);
}