-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlbz.c
325 lines (277 loc) · 8.76 KB
/
lbz.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* This file implements the Lua binding to libbzip2.
*
* Copyright (c) 2008, Evan Klitzke <evan@eklitzke.org>
*
* 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>
#define LBZ_STATE_META "LuaBook.bz2"
typedef struct {
BZFILE *bz_stream;
FILE *f;
/* getline related stuff */
char *buf;
size_t buf_size; /* max == LUAL_BUFFERSIZE */
} lbz_state;
/* Forward declarations */
static lbz_state *lbz_check_state(lua_State *L, int index);
static int lbz_read_open(lua_State *L);
static int lbz_read(lua_State *L);
static void lbz_perform_close(lbz_state *state, int keep_extra_buf);
static int lbz_read_close(lua_State *L);
static int lbz_getline(lua_State *L);
static int lbz_getline_read(lua_State *L, luaL_Buffer *b, lbz_state *state, int keep_eol);
static int lbz_lines(lua_State *L);
static void lbz_buffer_init(lbz_state *state);
static void lbz_buffer_free(lbz_state *state);
static void lbz_buffer_append(lbz_state *state, const char *data, size_t data_len);
static void lbz_buffer_drain(lbz_state *state, size_t amount);
static void lbz_buffer_drain_all(lbz_state *state);
static const struct luaL_reg bzlib_f [] = {
{"open", lbz_read_open},
{NULL, NULL} /* Sentinel */
};
static const struct luaL_reg bzlib_m [] = {
{"read", lbz_read},
{"getline", lbz_getline},
{"close", lbz_read_close},
{"lines", lbz_lines},
{NULL, NULL} /* Sentinel */
};
lbz_state *lbz_check_state(lua_State *L, int index) {
return (lbz_state *)luaL_checkudata(L, index, LBZ_STATE_META);
}
/* Binding to libbzip2's BZ2_bzReadOpen method */
int lbz_read_open(lua_State *L) {
size_t len;
const char *fname = lua_tolstring(L, 1, &len);
FILE *f = fopen(fname, "rb");
if (f == NULL)
return luaL_error(L, "Failed to fopen %s", fname);
int bzerror;
lbz_state *state = (lbz_state *) lua_newuserdata(L, sizeof(lbz_state));
state->bz_stream = BZ2_bzReadOpen(&bzerror, f, 0, 0, NULL, 0);
state->f = f;
lbz_buffer_init(state);
luaL_getmetatable(L, LBZ_STATE_META);
lua_setmetatable(L, -2);
if (bzerror != BZ_OK)
lua_pushnil(L);
return 1;
}
void lbz_buffer_init(lbz_state *state) {
state->buf = malloc(LUAL_BUFFERSIZE);
state->buf_size = 0;
}
void lbz_buffer_free(lbz_state *state) {
if(!state->buf) return;
state->buf_size = 0;
free(state->buf);
state->buf = NULL;
}
void lbz_buffer_append(lbz_state *state, const char *data, size_t data_size) {
assert(state->buf_size + data_size < LUAL_BUFFERSIZE);
memmove(state->buf + state->buf_size, data, data_size);
state->buf_size += data_size;
}
void lbz_buffer_drain(lbz_state *state, size_t amount) {
memmove(state->buf, state->buf + amount, state->buf_size - amount);
state->buf_size -= amount;
}
void lbz_buffer_drain_all(lbz_state *state) {
state->buf_size = 0;
}
/* Binding to libbzip2's BZ2_bzReadOpen method */
static int lbz_read(lua_State *L) {
int bzerror = BZ_OK;
int len;
luaL_Buffer b;
lbz_state *state = lbz_check_state(L, 1);
len = luaL_checkint(L, 2);
if (!state->bz_stream && !state->buf) {
/* The logical end of file has been reached -- there's no more data to
* return, and the user should call the read_close method. */
lua_pushnil(L);
lua_pushstring(L, "CLOSED");
return 2;
}
luaL_buffinit(L, &b);
/* In case this function is being used alongsize the getline method, we
* should use the buffers that getline is using */
if (state->buf_size) {
int used_len = (state->buf_size < len) ? state->buf_size : len;
luaL_addlstring(&b, state->buf, used_len);
lbz_buffer_drain(state, used_len);
len -= used_len;
}
/* Pull in chunks until all data read */
while(len > 0) {
char *buf = luaL_prepbuffer(&b);
int nextRead = len > LUAL_BUFFERSIZE ? LUAL_BUFFERSIZE : len;
int read = BZ2_bzRead(&bzerror, state->bz_stream, buf, nextRead);
if (read > 0) {
luaL_addsize(&b, read);
len -= read;
}
if (bzerror != BZ_OK)
goto handle_error;
}
luaL_pushresult(&b);
return 1;
handle_error:
if(BZ_STREAM_END == bzerror) {
/* Push the data read already and mark the stream done */
luaL_pushresult(&b);
lbz_perform_close(state, 0);
return 1;
} else {
lua_pushnil(L);
lua_pushstring(L, BZ2_bzerror(state->bz_stream, &bzerror));
return 2;
}
}
void lbz_perform_close(lbz_state *state, int keep_extra_buf) {
int bzerror;
if(!keep_extra_buf)
lbz_buffer_free(state);
if(!state->bz_stream)
return;
BZ2_bzReadClose(&bzerror, state->bz_stream);
fclose(state->f);
state->bz_stream = NULL;
state->f = NULL;
}
/* Binding to libbzip2's BZ2_bzReadClose method */
static int lbz_read_close(lua_State *L) {
lbz_state *state = lbz_check_state(L, 1);
lbz_perform_close(state, 0);
return 0;
}
/*
* GETLINE STUFF
* This code is considerably more complicated... if you know of a simpler way
* to do it that doesn't sacrifice speed, please let me know.
*/
static int lbz_handle_eol(luaL_Buffer *b, char *buf, size_t buf_len, lbz_state *state, int in_buffer, int keep_eol) {
char *eol = memchr(buf, '\n', buf_len);
size_t chars_to_return;
/* If a newline hasn't been found, keep iterating while building up
* the buffer */
if(eol == NULL) {
if(in_buffer)
luaL_addsize(b, buf_len);
else
luaL_addlstring(b, buf, buf_len);
return 0;
}
chars_to_return = eol - buf;
eol++;
if(keep_eol)
chars_to_return++;
if(in_buffer)
luaL_addsize(b, chars_to_return);
else
luaL_addlstring(b, buf, chars_to_return);
/* Save the remaining data end of data - position of beginning */
lbz_buffer_append(state, eol, buf_len - (eol - buf));
luaL_pushresult(b);
return 1;
}
/* This is an auxilliary function that lbz_getline calls when it needs to
* actually use the BZ2_bzRead method to read more data from the bzipped file.
**/
static int lbz_getline_read(lua_State *L, luaL_Buffer *b, lbz_state *state, int keep_eol) {
int bzerror;
/* The entire 'extra_buf' buffer is needed */
luaL_addlstring(b, state->buf, state->buf_size);
lbz_buffer_drain_all(state);
if (!state->bz_stream) { /* No more data left at all - return data is 'success' */
lbz_perform_close(state, 0); // Completely close it out now
luaL_pushresult(b);
return 1;
}
while(1) {
char *buf = luaL_prepbuffer(b);
int len = BZ2_bzRead(&bzerror, state->bz_stream, buf, LUAL_BUFFERSIZE);
if ((bzerror != BZ_OK) && (bzerror != BZ_STREAM_END)) {
/* Error happened, data thrown */
lua_pushnil(L);
lua_pushstring(L, BZ2_bzerror(state->bz_stream, &bzerror));
return 2;
}
if (!lbz_handle_eol(b, buf, len, state, 1, keep_eol))
continue;
/* Kill the stream, keep the remaining buffer */
if (bzerror == BZ_STREAM_END)
lbz_perform_close(state, state->buf_size ? 1 : 0);
return 1;
}
return 0;
}
static int lbz_getline(lua_State *L) {
lbz_state *state = lbz_check_state(L, 1);
int skip_eol = lua_toboolean(L, 2);
luaL_Buffer b;
if (!state->bz_stream && !state->buf) {
lua_pushnil(L);
lua_pushstring(L, "CLOSED");
return 2;
}
luaL_buffinit(L, &b);
if (state->buf_size) {
size_t data_size = state->buf_size;
lbz_buffer_drain_all(state);
/* Drain entire buffer so that remaining data can be appropriately added */
if (!lbz_handle_eol(&b, state->buf, data_size, state, 0, !skip_eol))
return lbz_getline_read(L, &b, state, !skip_eol);
return 1;
}
/* If there was no extra data from the last pass then we need to call
* lbz_getline_read directly to get more data and find the newline. */
return lbz_getline_read(L, &b, state, !skip_eol);
}
static int lbz_line_iter(lua_State *L) {
lua_settop(L, 0);
lua_pushvalue(L, lua_upvalueindex(1));
lua_pushvalue(L, lua_upvalueindex(2));
return lbz_getline(L);
}
/* (bz):lines(keep_eol) */
int lbz_lines(lua_State *L) {
int skip_eol = !lua_toboolean(L, 2);
lua_pushvalue(L, 1);
lua_pushboolean(L, skip_eol);
lua_pushcclosure(L, lbz_line_iter, 2);
return 1;
}
static int lbz_gc(lua_State *L) {
lbz_read_close(L);
return 0;
}
int luaopen_bz2(lua_State *L) {
luaL_newmetatable(L, LBZ_STATE_META);
lua_newtable(L);
luaL_register(L, NULL, bzlib_m);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lbz_gc);
lua_setfield(L, -2, "__gc");
lua_pop(L, 1);
luaL_register(L, "bz2", bzlib_f);
return 1;
}