aboutsummaryrefslogtreecommitdiff
path: root/vendor/lua-bz2
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/lua-bz2')
-rw-r--r--vendor/lua-bz2/LICENSE13
-rw-r--r--vendor/lua-bz2/README27
-rw-r--r--vendor/lua-bz2/bz2/ltn12.lua93
-rw-r--r--vendor/lua-bz2/compat-5.3.c948
-rw-r--r--vendor/lua-bz2/compat-5.3.h424
-rw-r--r--vendor/lua-bz2/lbz.c52
-rw-r--r--vendor/lua-bz2/lbz2_common.c57
-rw-r--r--vendor/lua-bz2/lbz2_common.h25
-rw-r--r--vendor/lua-bz2/lbz2_file_reader.c164
-rw-r--r--vendor/lua-bz2/lbz2_file_reader.h25
-rw-r--r--vendor/lua-bz2/lbz2_file_writer.c141
-rw-r--r--vendor/lua-bz2/lbz2_file_writer.h25
-rw-r--r--vendor/lua-bz2/lbz2_stream.c263
-rw-r--r--vendor/lua-bz2/lbz2_stream.h25
-rw-r--r--vendor/lua-bz2/lua-bz2-0.2.1-1.rockspec48
15 files changed, 2330 insertions, 0 deletions
diff --git a/vendor/lua-bz2/LICENSE b/vendor/lua-bz2/LICENSE
new file mode 100644
index 00000000..cc1b14dc
--- /dev/null
+++ b/vendor/lua-bz2/LICENSE
@@ -0,0 +1,13 @@
1Copyright (c) 2008, Evan Klitzke <evan@eklitzke.org>
2
3Permission to use, copy, modify, and/or distribute this software for any
4purpose with or without fee is hereby granted, provided that the above
5copyright notice and this permission notice appear in all copies.
6
7THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
diff --git a/vendor/lua-bz2/README b/vendor/lua-bz2/README
new file mode 100644
index 00000000..a5361ad6
--- /dev/null
+++ b/vendor/lua-bz2/README
@@ -0,0 +1,27 @@
1A Lua binding to Julian Seward's libbzip2.
2
3Current maintainer: harningt@gmail.com
4Previous maintainer: evan@yelp.com
5
6This software is released under a BSD-style license (to be exact, the ISC
7license). The full license can be found in the LICENSE file included with this
8source code.
9
10Requirements:
11 * libbzip2
12 * Lua >= 5.1
13
14Tested Operating Systems:
15 * Mac OSX 10.5
16 * Linux
17 * Ubuntu
18 * Gentoo
19
20Patches to make this work fully on Windows and other OSes are welcome.
21
22More information about Lua: http://www.lua.org/
23More information about bzip2: http://www.bzip.org/
24
25Current Repository Details
26 Project : https://github.com/harningt/lua-bz2/tree
27 Public Git : git://github.com/harningt/lua-bz2.git
diff --git a/vendor/lua-bz2/bz2/ltn12.lua b/vendor/lua-bz2/bz2/ltn12.lua
new file mode 100644
index 00000000..d2da675d
--- /dev/null
+++ b/vendor/lua-bz2/bz2/ltn12.lua
@@ -0,0 +1,93 @@
1local _M = {}
2local bz2 = require("bz2")
3
4_M.filter = {}
5_M.source = {}
6_M.sink = {}
7
8local function buildProcessor(filter, err)
9 if not filter then
10 return nil, err
11 end
12 return function(chunk)
13 if not filter then
14 return nil, "closed"
15 end
16 -- Skip empty chunks due to unexpected 'flushing'
17 if chunk and #chunk == 0 then
18 return ""
19 end
20 -- On nil, update closes the stream out as ltn12 expects
21 local ret, err = filter:update(chunk)
22 if not chunk then
23 filter:close()
24 filter = nil
25 end
26 if not ret then
27 return nil, err
28 end
29 return ret
30 end
31
32end
33
34function _M.filter.compress(blockSize100k, verbosity, workFactor)
35 return buildProcessor(bz2.initCompress(blockSize100k, verbosity, workFactor))
36end
37
38function _M.filter.decompress(verbosity, small)
39 return buildProcessor(bz2.initDecompress(verbosity, small))
40end
41
42function _M.sink.file(name, blockSize100k, verbosity, workFactor)
43 local writer, err = bz2.openWrite(name, blockSize100k, verbosity, workFactor)
44 if not writer then
45 return nil, err
46 end
47 return function(data)
48 if not writer then
49 return nil, "closed"
50 end
51 if not data then
52 writer:close()
53 writer = nil
54 return
55 end
56 if #data == 0 then
57 return 1
58 end
59 local ret, err = writer:write(data)
60 if not ret then
61 return nil, err
62 end
63 return 1
64 end
65end
66
67function _M.source.file(name, verbosity, small)
68 local reader, err = bz2.openRead(name, verbosity, small)
69 if not reader then
70 return nil, err
71 end
72 return function()
73 if not reader then
74 return
75 end
76 local ret, err = reader:read(true)
77 if ret and #ret == 0 then
78 reader:close()
79 reader = nil
80 return
81 end
82 if not ret then
83 return nil, err
84 end
85 if err then
86 reader:close()
87 reader = nil
88 end
89 return ret
90 end
91end
92
93return _M
diff --git a/vendor/lua-bz2/compat-5.3.c b/vendor/lua-bz2/compat-5.3.c
new file mode 100644
index 00000000..42b0a4bb
--- /dev/null
+++ b/vendor/lua-bz2/compat-5.3.c
@@ -0,0 +1,948 @@
1#include <stddef.h>
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include <errno.h>
6#include <stdio.h>
7#include "compat-5.3.h"
8
9/* don't compile it again if it already is included via compat53.h */
10#ifndef COMPAT53_C_
11#define COMPAT53_C_
12
13
14
15/* definitions for Lua 5.1 only */
16#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
17
18#ifndef COMPAT53_FOPEN_NO_LOCK
19# if defined(_MSC_VER)
20# define COMPAT53_FOPEN_NO_LOCK 1
21# else /* otherwise */
22# define COMPAT53_FOPEN_NO_LOCK 0
23# endif /* VC++ only so far */
24#endif /* No-lock fopen_s usage if possible */
25
26#if defined(_MSC_VER) && COMPAT53_FOPEN_NO_LOCK
27# include <share.h>
28#endif /* VC++ _fsopen for share-allowed file read */
29
30#ifndef COMPAT53_HAVE_STRERROR_R
31# if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
32 (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) || \
33 defined(__APPLE__)
34# define COMPAT53_HAVE_STRERROR_R 1
35# else /* none of the defines matched: define to 0 */
36# define COMPAT53_HAVE_STRERROR_R 0
37# endif /* have strerror_r of some form */
38#endif /* strerror_r */
39
40#ifndef COMPAT53_HAVE_STRERROR_S
41# if defined(_MSC_VER) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && \
42 defined(__STDC_LIB_EXT1__) && __STDC_LIB_EXT1__)
43# define COMPAT53_HAVE_STRERROR_S 1
44# else /* not VC++ or C11 */
45# define COMPAT53_HAVE_STRERROR_S 0
46# endif /* strerror_s from VC++ or C11 */
47#endif /* strerror_s */
48
49#ifndef COMPAT53_LUA_FILE_BUFFER_SIZE
50# define COMPAT53_LUA_FILE_BUFFER_SIZE 4096
51#endif /* Lua File Buffer Size */
52
53
54static char* compat53_strerror (int en, char* buff, size_t sz) {
55#if COMPAT53_HAVE_STRERROR_R
56 /* use strerror_r here, because it's available on these specific platforms */
57 if (sz > 0) {
58 buff[0] = '\0';
59 /* we don't care whether the GNU version or the XSI version is used: */
60 if (strerror_r(en, buff, sz)) {
61 /* Yes, we really DO want to ignore the return value!
62 * GCC makes that extra hard, not even a (void) cast will do. */
63 }
64 if (buff[0] == '\0') {
65 /* Buffer is unchanged, so we probably have called GNU strerror_r which
66 * returned a static constant string. Chances are that strerror will
67 * return the same static constant string and therefore be thread-safe. */
68 return strerror(en);
69 }
70 }
71 return buff; /* sz is 0 *or* strerror_r wrote into the buffer */
72#elif COMPAT53_HAVE_STRERROR_S
73 /* for MSVC and other C11 implementations, use strerror_s since it's
74 * provided by default by the libraries */
75 strerror_s(buff, sz, en);
76 return buff;
77#else
78 /* fallback, but strerror is not guaranteed to be threadsafe due to modifying
79 * errno itself and some impls not locking a static buffer for it ... but most
80 * known systems have threadsafe errno: this might only change if the locale
81 * is changed out from under someone while this function is being called */
82 (void)buff;
83 (void)sz;
84 return strerror(en);
85#endif
86}
87
88
89COMPAT53_API int lua_absindex (lua_State *L, int i) {
90 if (i < 0 && i > LUA_REGISTRYINDEX)
91 i += lua_gettop(L) + 1;
92 return i;
93}
94
95
96static void compat53_call_lua (lua_State *L, char const code[], size_t len,
97 int nargs, int nret) {
98 lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)code);
99 if (lua_type(L, -1) != LUA_TFUNCTION) {
100 lua_pop(L, 1);
101 if (luaL_loadbuffer(L, code, len, "=none"))
102 lua_error(L);
103 lua_pushvalue(L, -1);
104 lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)code);
105 }
106 lua_insert(L, -nargs-1);
107 lua_call(L, nargs, nret);
108}
109
110
111static const char compat53_arith_code[] =
112 "local op,a,b=...\n"
113 "if op==0 then return a+b\n"
114 "elseif op==1 then return a-b\n"
115 "elseif op==2 then return a*b\n"
116 "elseif op==3 then return a/b\n"
117 "elseif op==4 then return a%b\n"
118 "elseif op==5 then return a^b\n"
119 "elseif op==6 then return -a\n"
120 "end\n";
121
122COMPAT53_API void lua_arith (lua_State *L, int op) {
123 if (op < LUA_OPADD || op > LUA_OPUNM)
124 luaL_error(L, "invalid 'op' argument for lua_arith");
125 luaL_checkstack(L, 5, "not enough stack slots");
126 if (op == LUA_OPUNM)
127 lua_pushvalue(L, -1);
128 lua_pushnumber(L, op);
129 lua_insert(L, -3);
130 compat53_call_lua(L, compat53_arith_code,
131 sizeof(compat53_arith_code)-1, 3, 1);
132}
133
134
135static const char compat53_compare_code[] =
136 "local a,b=...\n"
137 "return a<=b\n";
138
139COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op) {
140 int result = 0;
141 switch (op) {
142 case LUA_OPEQ:
143 return lua_equal(L, idx1, idx2);
144 case LUA_OPLT:
145 return lua_lessthan(L, idx1, idx2);
146 case LUA_OPLE:
147 luaL_checkstack(L, 5, "not enough stack slots");
148 idx1 = lua_absindex(L, idx1);
149 idx2 = lua_absindex(L, idx2);
150 lua_pushvalue(L, idx1);
151 lua_pushvalue(L, idx2);
152 compat53_call_lua(L, compat53_compare_code,
153 sizeof(compat53_compare_code)-1, 2, 1);
154 result = lua_toboolean(L, -1);
155 lua_pop(L, 1);
156 return result;
157 default:
158 luaL_error(L, "invalid 'op' argument for lua_compare");
159 }
160 return 0;
161}
162
163
164COMPAT53_API void lua_copy (lua_State *L, int from, int to) {
165 int abs_to = lua_absindex(L, to);
166 luaL_checkstack(L, 1, "not enough stack slots");
167 lua_pushvalue(L, from);
168 lua_replace(L, abs_to);
169}
170
171
172COMPAT53_API void lua_len (lua_State *L, int i) {
173 switch (lua_type(L, i)) {
174 case LUA_TSTRING:
175 lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
176 break;
177 case LUA_TTABLE:
178 if (!luaL_callmeta(L, i, "__len"))
179 lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
180 break;
181 case LUA_TUSERDATA:
182 if (luaL_callmeta(L, i, "__len"))
183 break;
184 /* FALLTHROUGH */
185 default:
186 luaL_error(L, "attempt to get length of a %s value",
187 lua_typename(L, lua_type(L, i)));
188 }
189}
190
191
192COMPAT53_API int lua_rawgetp (lua_State *L, int i, const void *p) {
193 int abs_i = lua_absindex(L, i);
194 lua_pushlightuserdata(L, (void*)p);
195 lua_rawget(L, abs_i);
196 return lua_type(L, -1);
197}
198
199COMPAT53_API void lua_rawsetp (lua_State *L, int i, const void *p) {
200 int abs_i = lua_absindex(L, i);
201 luaL_checkstack(L, 1, "not enough stack slots");
202 lua_pushlightuserdata(L, (void*)p);
203 lua_insert(L, -2);
204 lua_rawset(L, abs_i);
205}
206
207
208COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum) {
209 lua_Number n = lua_tonumber(L, i);
210 if (isnum != NULL) {
211 *isnum = (n != 0 || lua_isnumber(L, i));
212 }
213 return n;
214}
215
216
217COMPAT53_API void luaL_checkversion (lua_State *L) {
218 (void)L;
219}
220
221
222COMPAT53_API void luaL_checkstack (lua_State *L, int sp, const char *msg) {
223 if (!lua_checkstack(L, sp+LUA_MINSTACK)) {
224 if (msg != NULL)
225 luaL_error(L, "stack overflow (%s)", msg);
226 else {
227 lua_pushliteral(L, "stack overflow");
228 lua_error(L);
229 }
230 }
231}
232
233
234COMPAT53_API int luaL_getsubtable (lua_State *L, int i, const char *name) {
235 int abs_i = lua_absindex(L, i);
236 luaL_checkstack(L, 3, "not enough stack slots");
237 lua_pushstring(L, name);
238 lua_gettable(L, abs_i);
239 if (lua_istable(L, -1))
240 return 1;
241 lua_pop(L, 1);
242 lua_newtable(L);
243 lua_pushstring(L, name);
244 lua_pushvalue(L, -2);
245 lua_settable(L, abs_i);
246 return 0;
247}
248
249
250COMPAT53_API lua_Integer luaL_len (lua_State *L, int i) {
251 lua_Integer res = 0;
252 int isnum = 0;
253 luaL_checkstack(L, 1, "not enough stack slots");
254 lua_len(L, i);
255 res = lua_tointegerx(L, -1, &isnum);
256 lua_pop(L, 1);
257 if (!isnum)
258 luaL_error(L, "object length is not an integer");
259 return res;
260}
261
262
263COMPAT53_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
264 luaL_checkstack(L, nup+1, "too many upvalues");
265 for (; l->name != NULL; l++) { /* fill the table with given functions */
266 int i;
267 lua_pushstring(L, l->name);
268 for (i = 0; i < nup; i++) /* copy upvalues to the top */
269 lua_pushvalue(L, -(nup + 1));
270 lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
271 lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */
272 }
273 lua_pop(L, nup); /* remove upvalues */
274}
275
276
277COMPAT53_API void luaL_setmetatable (lua_State *L, const char *tname) {
278 luaL_checkstack(L, 1, "not enough stack slots");
279 luaL_getmetatable(L, tname);
280 lua_setmetatable(L, -2);
281}
282
283
284COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname) {
285 void *p = lua_touserdata(L, i);
286 luaL_checkstack(L, 2, "not enough stack slots");
287 if (p == NULL || !lua_getmetatable(L, i))
288 return NULL;
289 else {
290 int res = 0;
291 luaL_getmetatable(L, tname);
292 res = lua_rawequal(L, -1, -2);
293 lua_pop(L, 2);
294 if (!res)
295 p = NULL;
296 }
297 return p;
298}
299
300
301static int compat53_countlevels (lua_State *L) {
302 lua_Debug ar;
303 int li = 1, le = 1;
304 /* find an upper bound */
305 while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
306 /* do a binary search */
307 while (li < le) {
308 int m = (li + le)/2;
309 if (lua_getstack(L, m, &ar)) li = m + 1;
310 else le = m;
311 }
312 return le - 1;
313}
314
315static int compat53_findfield (lua_State *L, int objidx, int level) {
316 if (level == 0 || !lua_istable(L, -1))
317 return 0; /* not found */
318 lua_pushnil(L); /* start 'next' loop */
319 while (lua_next(L, -2)) { /* for each pair in table */
320 if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
321 if (lua_rawequal(L, objidx, -1)) { /* found object? */
322 lua_pop(L, 1); /* remove value (but keep name) */
323 return 1;
324 }
325 else if (compat53_findfield(L, objidx, level - 1)) { /* try recursively */
326 lua_remove(L, -2); /* remove table (but keep name) */
327 lua_pushliteral(L, ".");
328 lua_insert(L, -2); /* place '.' between the two names */
329 lua_concat(L, 3);
330 return 1;
331 }
332 }
333 lua_pop(L, 1); /* remove value */
334 }
335 return 0; /* not found */
336}
337
338static int compat53_pushglobalfuncname (lua_State *L, lua_Debug *ar) {
339 int top = lua_gettop(L);
340 lua_getinfo(L, "f", ar); /* push function */
341 lua_pushvalue(L, LUA_GLOBALSINDEX);
342 if (compat53_findfield(L, top + 1, 2)) {
343 lua_copy(L, -1, top + 1); /* move name to proper place */
344 lua_pop(L, 2); /* remove pushed values */
345 return 1;
346 }
347 else {
348 lua_settop(L, top); /* remove function and global table */
349 return 0;
350 }
351}
352
353static void compat53_pushfuncname (lua_State *L, lua_Debug *ar) {
354 if (*ar->namewhat != '\0') /* is there a name? */
355 lua_pushfstring(L, "function " LUA_QS, ar->name);
356 else if (*ar->what == 'm') /* main? */
357 lua_pushliteral(L, "main chunk");
358 else if (*ar->what == 'C') {
359 if (compat53_pushglobalfuncname(L, ar)) {
360 lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
361 lua_remove(L, -2); /* remove name */
362 }
363 else
364 lua_pushliteral(L, "?");
365 }
366 else
367 lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
368}
369
370#define COMPAT53_LEVELS1 12 /* size of the first part of the stack */
371#define COMPAT53_LEVELS2 10 /* size of the second part of the stack */
372
373COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1,
374 const char *msg, int level) {
375 lua_Debug ar;
376 int top = lua_gettop(L);
377 int numlevels = compat53_countlevels(L1);
378 int mark = (numlevels > COMPAT53_LEVELS1 + COMPAT53_LEVELS2) ? COMPAT53_LEVELS1 : 0;
379 if (msg) lua_pushfstring(L, "%s\n", msg);
380 lua_pushliteral(L, "stack traceback:");
381 while (lua_getstack(L1, level++, &ar)) {
382 if (level == mark) { /* too many levels? */
383 lua_pushliteral(L, "\n\t..."); /* add a '...' */
384 level = numlevels - COMPAT53_LEVELS2; /* and skip to last ones */
385 }
386 else {
387 lua_getinfo(L1, "Slnt", &ar);
388 lua_pushfstring(L, "\n\t%s:", ar.short_src);
389 if (ar.currentline > 0)
390 lua_pushfstring(L, "%d:", ar.currentline);
391 lua_pushliteral(L, " in ");
392 compat53_pushfuncname(L, &ar);
393 lua_concat(L, lua_gettop(L) - top);
394 }
395 }
396 lua_concat(L, lua_gettop(L) - top);
397}
398
399
400COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
401 const char *serr = NULL;
402 int en = errno; /* calls to Lua API may change this value */
403 char buf[512] = { 0 };
404 if (stat) {
405 lua_pushboolean(L, 1);
406 return 1;
407 }
408 else {
409 lua_pushnil(L);
410 serr = compat53_strerror(en, buf, sizeof(buf));
411 if (fname)
412 lua_pushfstring(L, "%s: %s", fname, serr);
413 else
414 lua_pushstring(L, serr);
415 lua_pushnumber(L, (lua_Number)en);
416 return 3;
417 }
418}
419
420
421static int compat53_checkmode (lua_State *L, const char *mode, const char *modename, int err) {
422 if (mode && strchr(mode, modename[0]) == NULL) {
423 lua_pushfstring(L, "attempt to load a %s chunk (mode is '%s')", modename, mode);
424 return err;
425 }
426 return LUA_OK;
427}
428
429
430typedef struct {
431 lua_Reader reader;
432 void *ud;
433 int has_peeked_data;
434 const char *peeked_data;
435 size_t peeked_data_size;
436} compat53_reader_data;
437
438
439static const char *compat53_reader (lua_State *L, void *ud, size_t *size) {
440 compat53_reader_data *data = (compat53_reader_data *)ud;
441 if (data->has_peeked_data) {
442 data->has_peeked_data = 0;
443 *size = data->peeked_data_size;
444 return data->peeked_data;
445 } else
446 return data->reader(L, data->ud, size);
447}
448
449
450COMPAT53_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char *source, const char *mode) {
451 int status = LUA_OK;
452 compat53_reader_data compat53_data = { 0, NULL, 1, 0, 0 };
453 compat53_data.reader = reader;
454 compat53_data.ud = data;
455 compat53_data.peeked_data = reader(L, data, &(compat53_data.peeked_data_size));
456 if (compat53_data.peeked_data && compat53_data.peeked_data_size &&
457 compat53_data.peeked_data[0] == LUA_SIGNATURE[0]) /* binary file? */
458 status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX);
459 else
460 status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX);
461 if (status != LUA_OK)
462 return status;
463 /* we need to call the original 5.1 version of lua_load! */
464#undef lua_load
465 return lua_load(L, compat53_reader, &compat53_data, source);
466#define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53)
467}
468
469
470typedef struct {
471 int n; /* number of pre-read characters */
472 FILE *f; /* file being read */
473 char buff[COMPAT53_LUA_FILE_BUFFER_SIZE]; /* area for reading file */
474} compat53_LoadF;
475
476
477static const char *compat53_getF (lua_State *L, void *ud, size_t *size) {
478 compat53_LoadF *lf = (compat53_LoadF *)ud;
479 (void)L; /* not used */
480 if (lf->n > 0) { /* are there pre-read characters to be read? */
481 *size = lf->n; /* return them (chars already in buffer) */
482 lf->n = 0; /* no more pre-read characters */
483 }
484 else { /* read a block from file */
485 /* 'fread' can return > 0 *and* set the EOF flag. If next call to
486 'compat53_getF' called 'fread', it might still wait for user input.
487 The next check avoids this problem. */
488 if (feof(lf->f)) return NULL;
489 *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */
490 }
491 return lf->buff;
492}
493
494
495static int compat53_errfile (lua_State *L, const char *what, int fnameindex) {
496 char buf[512] = {0};
497 const char *serr = compat53_strerror(errno, buf, sizeof(buf));
498 const char *filename = lua_tostring(L, fnameindex) + 1;
499 lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
500 lua_remove(L, fnameindex);
501 return LUA_ERRFILE;
502}
503
504
505static int compat53_skipBOM (compat53_LoadF *lf) {
506 const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */
507 int c;
508 lf->n = 0;
509 do {
510 c = getc(lf->f);
511 if (c == EOF || c != *(const unsigned char *)p++) return c;
512 lf->buff[lf->n++] = (char)c; /* to be read by the parser */
513 } while (*p != '\0');
514 lf->n = 0; /* prefix matched; discard it */
515 return getc(lf->f); /* return next character */
516}
517
518
519/*
520** reads the first character of file 'f' and skips an optional BOM mark
521** in its beginning plus its first line if it starts with '#'. Returns
522** true if it skipped the first line. In any case, '*cp' has the
523** first "valid" character of the file (after the optional BOM and
524** a first-line comment).
525*/
526static int compat53_skipcomment (compat53_LoadF *lf, int *cp) {
527 int c = *cp = compat53_skipBOM(lf);
528 if (c == '#') { /* first line is a comment (Unix exec. file)? */
529 do { /* skip first line */
530 c = getc(lf->f);
531 } while (c != EOF && c != '\n');
532 *cp = getc(lf->f); /* skip end-of-line, if present */
533 return 1; /* there was a comment */
534 }
535 else return 0; /* no comment */
536}
537
538
539COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) {
540 compat53_LoadF lf;
541 int status, readstatus;
542 int c;
543 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
544 if (filename == NULL) {
545 lua_pushliteral(L, "=stdin");
546 lf.f = stdin;
547 }
548 else {
549 lua_pushfstring(L, "@%s", filename);
550#if defined(_MSC_VER)
551 /* This code is here to stop a deprecation error that stops builds
552 * if a certain macro is defined. While normally not caring would
553 * be best, some header-only libraries and builds can't afford to
554 * dictate this to the user. A quick check shows that fopen_s this
555 * goes back to VS 2005, and _fsopen goes back to VS 2003 .NET,
556 * possibly even before that so we don't need to do any version
557 * number checks, since this has been there since forever. */
558
559 /* TO USER: if you want the behavior of typical fopen_s/fopen,
560 * which does lock the file on VC++, define the macro used below to 0 */
561#if COMPAT53_FOPEN_NO_LOCK
562 lf.f = _fsopen(filename, "r", _SH_DENYNO); /* do not lock the file in any way */
563 if (lf.f == NULL)
564 return compat53_errfile(L, "open", fnameindex);
565#else /* use default locking version */
566 if (fopen_s(&lf.f, filename, "r") != 0)
567 return compat53_errfile(L, "open", fnameindex);
568#endif /* Locking vs. No-locking fopen variants */
569#else
570 lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */
571 if (lf.f == NULL) return compat53_errfile(L, "open", fnameindex);
572#endif
573 }
574 if (compat53_skipcomment(&lf, &c)) /* read initial portion */
575 lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
576 if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
577#if defined(_MSC_VER)
578 if (freopen_s(&lf.f, filename, "rb", lf.f) != 0)
579 return compat53_errfile(L, "reopen", fnameindex);
580#else
581 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
582 if (lf.f == NULL) return compat53_errfile(L, "reopen", fnameindex);
583#endif
584 compat53_skipcomment(&lf, &c); /* re-read initial portion */
585 }
586 if (c != EOF)
587 lf.buff[lf.n++] = (char)c; /* 'c' is the first character of the stream */
588 status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1), mode);
589 readstatus = ferror(lf.f);
590 if (filename) fclose(lf.f); /* close file (even in case of errors) */
591 if (readstatus) {
592 lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
593 return compat53_errfile(L, "read", fnameindex);
594 }
595 lua_remove(L, fnameindex);
596 return status;
597}
598
599
600COMPAT53_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode) {
601 int status = LUA_OK;
602 if (sz > 0 && buff[0] == LUA_SIGNATURE[0]) {
603 status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX);
604 }
605 else {
606 status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX);
607 }
608 if (status != LUA_OK)
609 return status;
610 return luaL_loadbuffer(L, buff, sz, name);
611}
612
613
614#if !defined(l_inspectstat) && \
615 (defined(unix) || defined(__unix) || defined(__unix__) || \
616 defined(__TOS_AIX__) || defined(_SYSTYPE_BSD) || \
617 (defined(__APPLE__) && defined(__MACH__)))
618/* some form of unix; check feature macros in unistd.h for details */
619# include <unistd.h>
620/* check posix version; the relevant include files and macros probably
621 * were available before 2001, but I'm not sure */
622# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
623# include <sys/wait.h>
624# define l_inspectstat(stat,what) \
625 if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
626 else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
627# endif
628#endif
629
630/* provide default (no-op) version */
631#if !defined(l_inspectstat)
632# define l_inspectstat(stat,what) ((void)0)
633#endif
634
635
636COMPAT53_API int luaL_execresult (lua_State *L, int stat) {
637 const char *what = "exit";
638 if (stat == -1)
639 return luaL_fileresult(L, 0, NULL);
640 else {
641 l_inspectstat(stat, what);
642 if (*what == 'e' && stat == 0)
643 lua_pushboolean(L, 1);
644 else
645 lua_pushnil(L);
646 lua_pushstring(L, what);
647 lua_pushinteger(L, stat);
648 return 3;
649 }
650}
651
652
653COMPAT53_API void luaL_buffinit (lua_State *L, luaL_Buffer_53 *B) {
654 /* make it crash if used via pointer to a 5.1-style luaL_Buffer */
655 B->b.p = NULL;
656 B->b.L = NULL;
657 B->b.lvl = 0;
658 /* reuse the buffer from the 5.1-style luaL_Buffer though! */
659 B->ptr = B->b.buffer;
660 B->capacity = LUAL_BUFFERSIZE;
661 B->nelems = 0;
662 B->L2 = L;
663}
664
665
666COMPAT53_API char *luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s) {
667 if (B->capacity - B->nelems < s) { /* needs to grow */
668 char* newptr = NULL;
669 size_t newcap = B->capacity * 2;
670 if (newcap - B->nelems < s)
671 newcap = B->nelems + s;
672 if (newcap < B->capacity) /* overflow */
673 luaL_error(B->L2, "buffer too large");
674 newptr = (char*)lua_newuserdata(B->L2, newcap);
675 memcpy(newptr, B->ptr, B->nelems);
676 if (B->ptr != B->b.buffer)
677 lua_replace(B->L2, -2); /* remove old buffer */
678 B->ptr = newptr;
679 B->capacity = newcap;
680 }
681 return B->ptr+B->nelems;
682}
683
684
685COMPAT53_API void luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l) {
686 memcpy(luaL_prepbuffsize(B, l), s, l);
687 luaL_addsize(B, l);
688}
689
690
691COMPAT53_API void luaL_addvalue (luaL_Buffer_53 *B) {
692 size_t len = 0;
693 const char *s = lua_tolstring(B->L2, -1, &len);
694 if (!s)
695 luaL_error(B->L2, "cannot convert value to string");
696 if (B->ptr != B->b.buffer)
697 lua_insert(B->L2, -2); /* userdata buffer must be at stack top */
698 luaL_addlstring(B, s, len);
699 lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1);
700}
701
702
703void luaL_pushresult (luaL_Buffer_53 *B) {
704 lua_pushlstring(B->L2, B->ptr, B->nelems);
705 if (B->ptr != B->b.buffer)
706 lua_replace(B->L2, -2); /* remove userdata buffer */
707}
708
709
710#endif /* Lua 5.1 */
711
712
713
714/* definitions for Lua 5.1 and Lua 5.2 */
715#if defined( LUA_VERSION_NUM ) && LUA_VERSION_NUM <= 502
716
717
718COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i) {
719 index = lua_absindex(L, index);
720 lua_pushinteger(L, i);
721 lua_gettable(L, index);
722 return lua_type(L, -1);
723}
724
725
726#ifndef LUA_EXTRASPACE
727#define LUA_EXTRASPACE (sizeof(void*))
728#endif
729
730COMPAT53_API void *lua_getextraspace (lua_State *L) {
731 int is_main = 0;
732 void *ptr = NULL;
733 luaL_checkstack(L, 4, "not enough stack slots available");
734 lua_pushliteral(L, "__compat53_extraspace");
735 lua_pushvalue(L, -1);
736 lua_rawget(L, LUA_REGISTRYINDEX);
737 if (!lua_istable(L, -1)) {
738 lua_pop(L, 1);
739 lua_createtable(L, 0, 2);
740 lua_createtable(L, 0, 1);
741 lua_pushliteral(L, "k");
742 lua_setfield(L, -2, "__mode");
743 lua_setmetatable(L, -2);
744 lua_pushvalue(L, -2);
745 lua_pushvalue(L, -2);
746 lua_rawset(L, LUA_REGISTRYINDEX);
747 }
748 lua_replace(L, -2);
749 is_main = lua_pushthread(L);
750 lua_rawget(L, -2);
751 ptr = lua_touserdata(L, -1);
752 if (!ptr) {
753 lua_pop(L, 1);
754 ptr = lua_newuserdata(L, LUA_EXTRASPACE);
755 if (is_main) {
756 memset(ptr, '\0', LUA_EXTRASPACE);
757 lua_pushthread(L);
758 lua_pushvalue(L, -2);
759 lua_rawset(L, -4);
760 lua_pushboolean(L, 1);
761 lua_pushvalue(L, -2);
762 lua_rawset(L, -4);
763 } else {
764 void* mptr = NULL;
765 lua_pushboolean(L, 1);
766 lua_rawget(L, -3);
767 mptr = lua_touserdata(L, -1);
768 if (mptr)
769 memcpy(ptr, mptr, LUA_EXTRASPACE);
770 else
771 memset(ptr, '\0', LUA_EXTRASPACE);
772 lua_pop(L, 1);
773 lua_pushthread(L);
774 lua_pushvalue(L, -2);
775 lua_rawset(L, -4);
776 }
777 }
778 lua_pop(L, 2);
779 return ptr;
780}
781
782
783COMPAT53_API int lua_isinteger (lua_State *L, int index) {
784 if (lua_type(L, index) == LUA_TNUMBER) {
785 lua_Number n = lua_tonumber(L, index);
786 lua_Integer i = lua_tointeger(L, index);
787 if (i == n)
788 return 1;
789 }
790 return 0;
791}
792
793
794COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum) {
795 int ok = 0;
796 lua_Number n = lua_tonumberx(L, i, &ok);
797 if (ok) {
798 if (n == (lua_Integer)n) {
799 if (isnum)
800 *isnum = 1;
801 return (lua_Integer)n;
802 }
803 }
804 if (isnum)
805 *isnum = 0;
806 return 0;
807}
808
809
810static void compat53_reverse (lua_State *L, int a, int b) {
811 for (; a < b; ++a, --b) {
812 lua_pushvalue(L, a);
813 lua_pushvalue(L, b);
814 lua_replace(L, a);
815 lua_replace(L, b);
816 }
817}
818
819
820COMPAT53_API void lua_rotate (lua_State *L, int idx, int n) {
821 int n_elems = 0;
822 idx = lua_absindex(L, idx);
823 n_elems = lua_gettop(L)-idx+1;
824 if (n < 0)
825 n += n_elems;
826 if ( n > 0 && n < n_elems) {
827 luaL_checkstack(L, 2, "not enough stack slots available");
828 n = n_elems - n;
829 compat53_reverse(L, idx, idx+n-1);
830 compat53_reverse(L, idx+n, idx+n_elems-1);
831 compat53_reverse(L, idx, idx+n_elems-1);
832 }
833}
834
835
836COMPAT53_API void lua_seti (lua_State *L, int index, lua_Integer i) {
837 luaL_checkstack(L, 1, "not enough stack slots available");
838 index = lua_absindex(L, index);
839 lua_pushinteger(L, i);
840 lua_insert(L, -2);
841 lua_settable(L, index);
842}
843
844
845#if !defined(lua_str2number)
846# define lua_str2number(s, p) strtod((s), (p))
847#endif
848
849COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s) {
850 char* endptr;
851 lua_Number n = lua_str2number(s, &endptr);
852 if (endptr != s) {
853 while (*endptr != '\0' && isspace((unsigned char)*endptr))
854 ++endptr;
855 if (*endptr == '\0') {
856 lua_pushnumber(L, n);
857 return endptr - s + 1;
858 }
859 }
860 return 0;
861}
862
863
864COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
865 if (!luaL_callmeta(L, idx, "__tostring")) {
866 int t = lua_type(L, idx), tt = 0;
867 char const* name = NULL;
868 switch (t) {
869 case LUA_TNIL:
870 lua_pushliteral(L, "nil");
871 break;
872 case LUA_TSTRING:
873 case LUA_TNUMBER:
874 lua_pushvalue(L, idx);
875 break;
876 case LUA_TBOOLEAN:
877 if (lua_toboolean(L, idx))
878 lua_pushliteral(L, "true");
879 else
880 lua_pushliteral(L, "false");
881 break;
882 default:
883 tt = luaL_getmetafield(L, idx, "__name");
884 name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t);
885 lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx));
886 if (tt != LUA_TNIL)
887 lua_replace(L, -2);
888 break;
889 }
890 } else {
891 if (!lua_isstring(L, -1))
892 luaL_error(L, "'__tostring' must return a string");
893 }
894 return lua_tolstring(L, -1, len);
895}
896
897
898COMPAT53_API void luaL_requiref (lua_State *L, const char *modname,
899 lua_CFunction openf, int glb) {
900 luaL_checkstack(L, 3, "not enough stack slots available");
901 luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
902 if (lua_getfield(L, -1, modname) == LUA_TNIL) {
903 lua_pop(L, 1);
904 lua_pushcfunction(L, openf);
905 lua_pushstring(L, modname);
906 lua_call(L, 1, 1);
907 lua_pushvalue(L, -1);
908 lua_setfield(L, -3, modname);
909 }
910 if (glb) {
911 lua_pushvalue(L, -1);
912 lua_setglobal(L, modname);
913 }
914 lua_replace(L, -2);
915}
916
917
918#endif /* Lua 5.1 and 5.2 */
919
920
921#endif /* COMPAT53_C_ */
922
923
924/*********************************************************************
925* This file contains parts of Lua 5.2's and Lua 5.3's source code:
926*
927* Copyright (C) 1994-2014 Lua.org, PUC-Rio.
928*
929* Permission is hereby granted, free of charge, to any person obtaining
930* a copy of this software and associated documentation files (the
931* "Software"), to deal in the Software without restriction, including
932* without limitation the rights to use, copy, modify, merge, publish,
933* distribute, sublicense, and/or sell copies of the Software, and to
934* permit persons to whom the Software is furnished to do so, subject to
935* the following conditions:
936*
937* The above copyright notice and this permission notice shall be
938* included in all copies or substantial portions of the Software.
939*
940* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
941* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
942* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
943* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
944* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
945* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
946* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
947*********************************************************************/
948
diff --git a/vendor/lua-bz2/compat-5.3.h b/vendor/lua-bz2/compat-5.3.h
new file mode 100644
index 00000000..b730a4b8
--- /dev/null
+++ b/vendor/lua-bz2/compat-5.3.h
@@ -0,0 +1,424 @@
1#ifndef COMPAT53_H_
2#define COMPAT53_H_
3
4#include <stddef.h>
5#include <limits.h>
6#include <string.h>
7#if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
8extern "C" {
9#endif
10#include <lua.h>
11#include <lauxlib.h>
12#include <lualib.h>
13#if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
14}
15#endif
16
17
18#undef COMPAT53_INCLUDE_SOURCE
19#if defined(COMPAT53_PREFIX)
20/* - change the symbol names of functions to avoid linker conflicts
21 * - compat-5.3.c needs to be compiled (and linked) separately
22 */
23# if !defined(COMPAT53_API)
24# define COMPAT53_API extern
25# endif
26#else /* COMPAT53_PREFIX */
27/* - make all functions static and include the source.
28 * - compat-5.3.c doesn't need to be compiled (and linked) separately
29 */
30# define COMPAT53_PREFIX compat53
31# undef COMPAT53_API
32# if defined(__GNUC__) || defined(__clang__)
33# define COMPAT53_API __attribute__((__unused__)) static
34# else
35# define COMPAT53_API static
36# endif
37# define COMPAT53_INCLUDE_SOURCE
38#endif /* COMPAT53_PREFIX */
39
40#define COMPAT53_CONCAT_HELPER(a, b) a##b
41#define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b)
42
43
44
45/* declarations for Lua 5.1 */
46#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
47
48/* XXX not implemented:
49 * lua_arith (new operators)
50 * lua_upvalueid
51 * lua_upvaluejoin
52 * lua_version
53 * lua_yieldk
54 */
55
56#ifndef LUA_OK
57# define LUA_OK 0
58#endif
59#ifndef LUA_OPADD
60# define LUA_OPADD 0
61#endif
62#ifndef LUA_OPSUB
63# define LUA_OPSUB 1
64#endif
65#ifndef LUA_OPMUL
66# define LUA_OPMUL 2
67#endif
68#ifndef LUA_OPDIV
69# define LUA_OPDIV 3
70#endif
71#ifndef LUA_OPMOD
72# define LUA_OPMOD 4
73#endif
74#ifndef LUA_OPPOW
75# define LUA_OPPOW 5
76#endif
77#ifndef LUA_OPUNM
78# define LUA_OPUNM 6
79#endif
80#ifndef LUA_OPEQ
81# define LUA_OPEQ 0
82#endif
83#ifndef LUA_OPLT
84# define LUA_OPLT 1
85#endif
86#ifndef LUA_OPLE
87# define LUA_OPLE 2
88#endif
89
90/* LuaJIT/Lua 5.1 does not have the updated
91 * error codes for thread status/function returns (but some patched versions do)
92 * define it only if it's not found
93 */
94#if !defined(LUA_ERRGCMM)
95/* Use + 2 because in some versions of Lua (Lua 5.1)
96 * LUA_ERRFILE is defined as (LUA_ERRERR+1)
97 * so we need to avoid it (LuaJIT might have something at this
98 * integer value too)
99 */
100# define LUA_ERRGCMM (LUA_ERRERR + 2)
101#endif /* LUA_ERRGCMM define */
102
103typedef size_t lua_Unsigned;
104
105typedef struct luaL_Buffer_53 {
106 luaL_Buffer b; /* make incorrect code crash! */
107 char *ptr;
108 size_t nelems;
109 size_t capacity;
110 lua_State *L2;
111} luaL_Buffer_53;
112#define luaL_Buffer luaL_Buffer_53
113
114/* In PUC-Rio 5.1, userdata is a simple FILE*
115 * In LuaJIT, it's a struct where the first member is a FILE*
116 * We can't support the `closef` member
117 */
118typedef struct luaL_Stream {
119 FILE *f;
120} luaL_Stream;
121
122#define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex)
123COMPAT53_API int lua_absindex (lua_State *L, int i);
124
125#define lua_arith COMPAT53_CONCAT(COMPAT53_PREFIX, _arith)
126COMPAT53_API void lua_arith (lua_State *L, int op);
127
128#define lua_compare COMPAT53_CONCAT(COMPAT53_PREFIX, _compare)
129COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op);
130
131#define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy)
132COMPAT53_API void lua_copy (lua_State *L, int from, int to);
133
134#define lua_getuservalue(L, i) \
135 (lua_getfenv((L), (i)), lua_type((L), -1))
136#define lua_setuservalue(L, i) \
137 (luaL_checktype((L), -1, LUA_TTABLE), lua_setfenv((L), (i)))
138
139#define lua_len COMPAT53_CONCAT(COMPAT53_PREFIX, _len)
140COMPAT53_API void lua_len (lua_State *L, int i);
141
142#define lua_pushstring(L, s) \
143 (lua_pushstring((L), (s)), lua_tostring((L), -1))
144
145#define lua_pushlstring(L, s, len) \
146 ((((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))), lua_tostring((L), -1))
147
148#ifndef luaL_newlibtable
149# define luaL_newlibtable(L, l) \
150 (lua_createtable((L), 0, sizeof((l))/sizeof(*(l))-1))
151#endif
152#ifndef luaL_newlib
153# define luaL_newlib(L, l) \
154 (luaL_newlibtable((L), (l)), luaL_register((L), NULL, (l)))
155#endif
156
157#define lua_pushglobaltable(L) \
158 lua_pushvalue((L), LUA_GLOBALSINDEX)
159
160#define lua_rawgetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawgetp)
161COMPAT53_API int lua_rawgetp (lua_State *L, int i, const void *p);
162
163#define lua_rawsetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawsetp)
164COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p);
165
166#define lua_rawlen(L, i) lua_objlen((L), (i))
167
168#define lua_tointeger(L, i) lua_tointegerx((L), (i), NULL)
169
170#define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx)
171COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum);
172
173#define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion)
174COMPAT53_API void luaL_checkversion (lua_State *L);
175
176#define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53)
177COMPAT53_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char* source, const char* mode);
178
179#define luaL_loadfilex COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadfilex)
180COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode);
181
182#define luaL_loadbufferx COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadbufferx)
183COMPAT53_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
184
185#define luaL_checkstack COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkstack_53)
186COMPAT53_API void luaL_checkstack (lua_State *L, int sp, const char *msg);
187
188#define luaL_getsubtable COMPAT53_CONCAT(COMPAT53_PREFIX, L_getsubtable)
189COMPAT53_API int luaL_getsubtable (lua_State* L, int i, const char *name);
190
191#define luaL_len COMPAT53_CONCAT(COMPAT53_PREFIX, L_len)
192COMPAT53_API lua_Integer luaL_len (lua_State *L, int i);
193
194#define luaL_setfuncs COMPAT53_CONCAT(COMPAT53_PREFIX, L_setfuncs)
195COMPAT53_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
196
197#define luaL_setmetatable COMPAT53_CONCAT(COMPAT53_PREFIX, L_setmetatable)
198COMPAT53_API void luaL_setmetatable (lua_State *L, const char *tname);
199
200#define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata)
201COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname);
202
203#define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback)
204COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level);
205
206#define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult)
207COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname);
208
209#define luaL_execresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_execresult)
210COMPAT53_API int luaL_execresult (lua_State *L, int stat);
211
212#define lua_callk(L, na, nr, ctx, cont) \
213 ((void)(ctx), (void)(cont), lua_call((L), (na), (nr)))
214#define lua_pcallk(L, na, nr, err, ctx, cont) \
215 ((void)(ctx), (void)(cont), lua_pcall((L), (na), (nr), (err)))
216
217#define lua_resume(L, from, nargs) \
218 ((void)(from), lua_resume((L), (nargs)))
219
220#define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53)
221COMPAT53_API void luaL_buffinit (lua_State *L, luaL_Buffer_53 *B);
222
223#define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53)
224COMPAT53_API char *luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s);
225
226#define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53)
227COMPAT53_API void luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l);
228
229#define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53)
230COMPAT53_API void luaL_addvalue (luaL_Buffer_53 *B);
231
232#define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53)
233COMPAT53_API void luaL_pushresult (luaL_Buffer_53 *B);
234
235#undef luaL_buffinitsize
236#define luaL_buffinitsize(L, B, s) \
237 (luaL_buffinit((L), (B)), luaL_prepbuffsize((B), (s)))
238
239#undef luaL_prepbuffer
240#define luaL_prepbuffer(B) \
241 luaL_prepbuffsize((B), LUAL_BUFFERSIZE)
242
243#undef luaL_addchar
244#define luaL_addchar(B, c) \
245 ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize((B), 1)), \
246 ((B)->ptr[(B)->nelems++] = (c)))
247
248#undef luaL_addsize
249#define luaL_addsize(B, s) \
250 ((B)->nelems += (s))
251
252#undef luaL_addstring
253#define luaL_addstring(B, s) \
254 luaL_addlstring((B), (s), strlen((s)))
255
256#undef luaL_pushresultsize
257#define luaL_pushresultsize(B, s) \
258 (luaL_addsize((B), (s)), luaL_pushresult((B)))
259
260#if defined(LUA_COMPAT_APIINTCASTS)
261#define lua_pushunsigned(L, n) \
262 lua_pushinteger((L), (lua_Integer)(n))
263#define lua_tounsignedx(L, i, is) \
264 ((lua_Unsigned)lua_tointegerx((L), (i), (is)))
265#define lua_tounsigned(L, i) \
266 lua_tounsignedx((L), (i), NULL)
267#define luaL_checkunsigned(L, a) \
268 ((lua_Unsigned)luaL_checkinteger((L), (a)))
269#define luaL_optunsigned(L, a, d) \
270 ((lua_Unsigned)luaL_optinteger((L), (a), (lua_Integer)(d)))
271#endif
272
273#endif /* Lua 5.1 only */
274
275
276
277/* declarations for Lua 5.1 and 5.2 */
278#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
279
280typedef int lua_KContext;
281
282typedef int (*lua_KFunction)(lua_State *L, int status, lua_KContext ctx);
283
284#define lua_dump(L, w, d, s) \
285 ((void)(s), lua_dump((L), (w), (d)))
286
287#define lua_getfield(L, i, k) \
288 (lua_getfield((L), (i), (k)), lua_type((L), -1))
289
290#define lua_gettable(L, i) \
291 (lua_gettable((L), (i)), lua_type((L), -1))
292
293#define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti)
294COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i);
295
296#define lua_getextraspace COMPAT53_CONCAT(COMPAT53_PREFIX, _getextraspace)
297COMPAT53_API void *lua_getextraspace (lua_State *L);
298
299#define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger)
300COMPAT53_API int lua_isinteger (lua_State *L, int index);
301
302#define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx_53)
303COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum);
304
305#define lua_numbertointeger(n, p) \
306 ((*(p) = (lua_Integer)(n)), 1)
307
308#define lua_rawget(L, i) \
309 (lua_rawget((L), (i)), lua_type((L), -1))
310
311#define lua_rawgeti(L, i, n) \
312 (lua_rawgeti((L), (i), (n)), lua_type((L), -1))
313
314#define lua_rotate COMPAT53_CONCAT(COMPAT53_PREFIX, _rotate)
315COMPAT53_API void lua_rotate (lua_State *L, int idx, int n);
316
317#define lua_seti COMPAT53_CONCAT(COMPAT53_PREFIX, _seti)
318COMPAT53_API void lua_seti (lua_State *L, int index, lua_Integer i);
319
320#define lua_stringtonumber COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber)
321COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s);
322
323#define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring)
324COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len);
325
326#define luaL_getmetafield(L, o, e) \
327 (luaL_getmetafield((L), (o), (e)) ? lua_type((L), -1) : LUA_TNIL)
328
329#define luaL_newmetatable(L, tn) \
330 (luaL_newmetatable((L), (tn)) ? (lua_pushstring((L), (tn)), lua_setfield((L), -2, "__name"), 1) : 0)
331
332#define luaL_requiref COMPAT53_CONCAT(COMPAT53_PREFIX, L_requiref_53)
333COMPAT53_API void luaL_requiref (lua_State *L, const char *modname,
334 lua_CFunction openf, int glb );
335
336#endif /* Lua 5.1 and Lua 5.2 */
337
338
339
340/* declarations for Lua 5.2 */
341#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 502
342
343/* XXX not implemented:
344 * lua_isyieldable
345 * lua_arith (new operators)
346 * lua_pushfstring (new formats)
347 */
348
349#define lua_getglobal(L, n) \
350 (lua_getglobal((L), (n)), lua_type((L), -1))
351
352#define lua_getuservalue(L, i) \
353 (lua_getuservalue((L), (i)), lua_type((L), -1))
354
355#define lua_pushlstring(L, s, len) \
356 (((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len)))
357
358#define lua_rawgetp(L, i, p) \
359 (lua_rawgetp((L), (i), (p)), lua_type((L), -1))
360
361#define LUA_KFUNCTION(_name) \
362 static int (_name)(lua_State *L, int status, lua_KContext ctx); \
363 static int (_name ## _52)(lua_State *L) { \
364 lua_KContext ctx; \
365 int status = lua_getctx(L, &ctx); \
366 return (_name)(L, status, ctx); \
367 } \
368 static int (_name)(lua_State *L, int status, lua_KContext ctx)
369
370#define lua_pcallk(L, na, nr, err, ctx, cont) \
371 lua_pcallk((L), (na), (nr), (err), (ctx), cont ## _52)
372
373#define lua_callk(L, na, nr, ctx, cont) \
374 lua_callk((L), (na), (nr), (ctx), cont ## _52)
375
376#define lua_yieldk(L, nr, ctx, cont) \
377 lua_yieldk((L), (nr), (ctx), cont ## _52)
378
379#ifdef lua_call
380# undef lua_call
381# define lua_call(L, na, nr) \
382 (lua_callk)((L), (na), (nr), 0, NULL)
383#endif
384
385#ifdef lua_pcall
386# undef lua_pcall
387# define lua_pcall(L, na, nr, err) \
388 (lua_pcallk)((L), (na), (nr), (err), 0, NULL)
389#endif
390
391#ifdef lua_yield
392# undef lua_yield
393# define lua_yield(L, nr) \
394 (lua_yieldk)((L), (nr), 0, NULL)
395#endif
396
397#endif /* Lua 5.2 only */
398
399
400
401/* other Lua versions */
402#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 504
403
404# error "unsupported Lua version (i.e. not Lua 5.1, 5.2, 5.3, or 5.4)"
405
406#endif /* other Lua versions except 5.1, 5.2, 5.3, and 5.4 */
407
408
409
410/* helper macro for defining continuation functions (for every version
411 * *except* Lua 5.2) */
412#ifndef LUA_KFUNCTION
413#define LUA_KFUNCTION(_name) \
414 static int (_name)(lua_State *L, int status, lua_KContext ctx)
415#endif
416
417
418#if defined(COMPAT53_INCLUDE_SOURCE)
419# include "compat-5.3.c"
420#endif
421
422
423#endif /* COMPAT53_H_ */
424
diff --git a/vendor/lua-bz2/lbz.c b/vendor/lua-bz2/lbz.c
new file mode 100644
index 00000000..6e0d66e2
--- /dev/null
+++ b/vendor/lua-bz2/lbz.c
@@ -0,0 +1,52 @@
1/* This file implements the Lua binding to libbzip2.
2 *
3 * Copyright (c) 2008, Evan Klitzke <evan@eklitzke.org>
4 * Copyright (c) 2012, Thomas Harning Jr <harningt@gmail.com>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <lua.h>
20#include <lauxlib.h>
21
22/* This explicit define prevents compat-5.3.h from loading compat-5.3.c */
23#define COMPAT53_PREFIX compat53
24#include "compat-5.3.h"
25
26#include "lbz2_file_reader.h"
27#include "lbz2_file_writer.h"
28#include "lbz2_stream.h"
29
30static luaL_Reg lbz2_global[] = {
31 { NULL, NULL }
32};
33
34int luaopen_bz2(lua_State *L) {
35 luaL_newlib(L, lbz2_global);
36
37 lua_pushliteral(L, "bz2");
38 lua_setfield(L, -2, "_NAME");
39 lua_pushliteral(L, "0.1");
40 lua_setfield(L, -2, "_VERSION");
41
42 register_lbz2_file_reader(L);
43 register_lbz2_file_writer(L);
44 register_lbz2_stream(L);
45
46#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
47 lua_pushvalue(L, -1);
48 lua_setglobal(L, "bz2");
49#endif
50
51 return 1;
52}
diff --git a/vendor/lua-bz2/lbz2_common.c b/vendor/lua-bz2/lbz2_common.c
new file mode 100644
index 00000000..2aa76cfb
--- /dev/null
+++ b/vendor/lua-bz2/lbz2_common.c
@@ -0,0 +1,57 @@
1/* This file implements the Lua binding to libbzip2.
2 *
3 * Copyright (c) 2008, Evan Klitzke <evan@eklitzke.org>
4 * Copyright (c) 2012, Thomas Harning Jr <harningt@gmail.com>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19
20#include "lbz2_common.h"
21#include <bzlib.h>
22
23
24const char *lbz2_error(int bzerror) {
25 switch (bzerror) {
26 case BZ_OK:
27 return "OK";
28 case BZ_RUN_OK:
29 return "RUN_OK";
30 case BZ_FLUSH_OK:
31 return "FLUSH_OK";
32 case BZ_FINISH_OK:
33 return "FINISH_OK";
34 case BZ_STREAM_END:
35 return "STREAM_END";
36 case BZ_SEQUENCE_ERROR:
37 return "SEQUENCE_ERROR";
38 case BZ_PARAM_ERROR:
39 return "PARAM_ERROR";
40 case BZ_MEM_ERROR:
41 return "MEM_ERROR";
42 case BZ_DATA_ERROR:
43 return "DATA_ERROR";
44 case BZ_DATA_ERROR_MAGIC:
45 return "DATA_ERROR_MAGIC";
46 case BZ_IO_ERROR:
47 return "IO_ERROR";
48 case BZ_UNEXPECTED_EOF:
49 return "UNEXPECTED_EOF";
50 case BZ_OUTBUFF_FULL:
51 return "OUTBUFF_FULL";
52 case BZ_CONFIG_ERROR:
53 return "CONFIG_ERROR";
54 default:
55 return "UNKNOWN";
56 }
57}
diff --git a/vendor/lua-bz2/lbz2_common.h b/vendor/lua-bz2/lbz2_common.h
new file mode 100644
index 00000000..98e534bf
--- /dev/null
+++ b/vendor/lua-bz2/lbz2_common.h
@@ -0,0 +1,25 @@
1/* This file implements the Lua binding to libbzip2.
2 *
3 * Copyright (c) 2008, Evan Klitzke <evan@eklitzke.org>
4 * Copyright (c) 2012, Thomas Harning Jr <harningt@gmail.com>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19
20#ifndef LBZ2_COMMON_H
21#define LBZ2_COMMON_H
22
23const char *lbz2_error(int bzerror);
24
25#endif
diff --git a/vendor/lua-bz2/lbz2_file_reader.c b/vendor/lua-bz2/lbz2_file_reader.c
new file mode 100644
index 00000000..c101a1ee
--- /dev/null
+++ b/vendor/lua-bz2/lbz2_file_reader.c
@@ -0,0 +1,164 @@
1/* This file implements the Lua binding to libbzip2.
2 *
3 * Copyright (c) 2008, Evan Klitzke <evan@eklitzke.org>
4 * Copyright (c) 2012, Thomas Harning Jr <harningt@gmail.com>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <bzlib.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <lua.h>
24#include <lauxlib.h>
25
26/* This explicit define prevents compat-5.3.h from loading compat-5.3.c */
27#define COMPAT53_PREFIX compat53
28#include "compat-5.3.h"
29
30#include "lbz2_file_reader.h"
31#include "lbz2_common.h"
32
33#define LBZ2_FILE_READER_MT "LBZ2_FILE_READER_MT"
34
35typedef struct {
36 BZFILE *bz_stream;
37 FILE *f;
38} lbz2_file_reader;
39
40static lbz2_file_reader *lbz2_check_file_reader(lua_State *L, int index) {
41 return (lbz2_file_reader *)luaL_checkudata(L, index, LBZ2_FILE_READER_MT);
42}
43
44static int lbz2_file_reader_open(lua_State *L) {
45 lbz2_file_reader *reader;
46 int errorCode;
47 const char *fname = luaL_checkstring(L, 1);
48 int verbosity = luaL_optinteger(L, 3, 0);
49 int small = lua_toboolean(L, 4);
50
51 reader = lua_newuserdata(L, sizeof(*reader));
52 memset(reader, 0, sizeof(*reader));
53
54 luaL_getmetatable(L, LBZ2_FILE_READER_MT);
55 lua_setmetatable(L, -2);
56
57 reader->f = fopen(fname, "rb");
58
59 if (reader->f == NULL) {
60 return luaL_error(L, "Failed to fopen %s", fname);
61 }
62 reader->bz_stream = BZ2_bzReadOpen(&errorCode, reader->f, verbosity, small, NULL, 0);
63
64 if (BZ_OK != errorCode) {
65 fclose(reader->f);
66 reader->f = NULL;
67 lua_pushnil(L);
68 lua_pushstring(L, lbz2_error(errorCode));
69 return 2;
70 }
71 return 1;
72}
73
74static int lbz2_file_reader_close(lua_State *L) {
75 lbz2_file_reader *reader = lbz2_check_file_reader(L, 1);
76 int errorCode = BZ_OK;
77
78 if (reader->bz_stream) {
79 BZ2_bzReadClose(&errorCode, reader->bz_stream);
80 reader->bz_stream = NULL;
81 }
82 if (reader->f) {
83 fclose(reader->f);
84 reader->f = NULL;
85 }
86
87 lua_pushnil(L);
88 lua_setmetatable(L, 1);
89
90 if (BZ_OK != errorCode) {
91 lua_pushnil(L);
92 lua_pushstring(L, lbz2_error(errorCode));
93 return 2;
94 }
95 lua_pushboolean(L, 1);
96 return 1;
97}
98
99static int lbz2_file_reader_read(lua_State *L) {
100 lbz2_file_reader *reader = lbz2_check_file_reader(L, 1);
101 int errorCode = BZ_OK;
102 int dataLength;
103 luaL_Buffer B;
104 /* If passed a boolean, read a single *chunk* */
105 if (lua_isboolean(L, 2)) {
106 dataLength = LUAL_BUFFERSIZE;
107 } else {
108 dataLength = luaL_optinteger(L, 2, -1);
109 }
110
111 luaL_buffinit(L, &B);
112
113 /* Pull in chunks until all data read */
114 while(dataLength > 0 || dataLength == -1) {
115 char *buf = luaL_prepbuffer(&B);
116 int nextRead = (dataLength == -1 || dataLength > LUAL_BUFFERSIZE) ? LUAL_BUFFERSIZE : dataLength;
117 int read = BZ2_bzRead(&errorCode, reader->bz_stream, buf, nextRead);
118 if (read > 0) {
119 luaL_addsize(&B, read);
120 dataLength -= read;
121 }
122 if (BZ_OK != errorCode) {
123 goto handle_error;
124 }
125 }
126 luaL_pushresult(&B);
127 return 1;
128handle_error:
129 if(BZ_STREAM_END == errorCode) {
130 luaL_pushresult(&B);
131 lua_pushboolean(L, 1);
132 return 2;
133 } else {
134 lua_pushnil(L);
135 lua_pushstring(L, lbz2_error(errorCode));
136 return 2;
137 }
138}
139
140static luaL_Reg lbz2_file_reader_ops[] = {
141 { "read", lbz2_file_reader_read },
142 { "close", lbz2_file_reader_close },
143 { NULL, NULL }
144};
145
146static luaL_Reg lbz2_file_reader_global[] = {
147 { "openRead", lbz2_file_reader_open },
148 { NULL, NULL }
149};
150
151
152
153void register_lbz2_file_reader(lua_State *L) {
154 luaL_newmetatable(L, LBZ2_FILE_READER_MT);
155 lua_newtable(L);
156 luaL_setfuncs(L, lbz2_file_reader_ops, 0);
157 lua_setfield(L, -2, "__index");
158
159 lua_pushcfunction(L, lbz2_file_reader_close);
160 lua_setfield(L, -2, "__gc");
161 lua_pop(L, 1);
162
163 luaL_setfuncs(L, lbz2_file_reader_global, 0);
164}
diff --git a/vendor/lua-bz2/lbz2_file_reader.h b/vendor/lua-bz2/lbz2_file_reader.h
new file mode 100644
index 00000000..a183329d
--- /dev/null
+++ b/vendor/lua-bz2/lbz2_file_reader.h
@@ -0,0 +1,25 @@
1/* This file implements the Lua binding to libbzip2.
2 *
3 * Copyright (c) 2008, Evan Klitzke <evan@eklitzke.org>
4 * Copyright (c) 2012, Thomas Harning Jr <harningt@gmail.com>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19
20#ifndef LBZ2_FILE_READER_H
21#define LBZ2_FILE_READER_H
22
23void register_lbz2_file_reader(lua_State *L);
24
25#endif
diff --git a/vendor/lua-bz2/lbz2_file_writer.c b/vendor/lua-bz2/lbz2_file_writer.c
new file mode 100644
index 00000000..f35beca3
--- /dev/null
+++ b/vendor/lua-bz2/lbz2_file_writer.c
@@ -0,0 +1,141 @@
1/* This file implements the Lua binding to libbzip2.
2 *
3 * Copyright (c) 2008, Evan Klitzke <evan@eklitzke.org>
4 * Copyright (c) 2012, Thomas Harning Jr <harningt@gmail.com>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <bzlib.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <lua.h>
24#include <lauxlib.h>
25
26#include <assert.h>
27
28/* This explicit define prevents compat-5.3.h from loading compat-5.3.c */
29#define COMPAT53_PREFIX compat53
30#include "compat-5.3.h"
31
32#include "lbz2_file_writer.h"
33#include "lbz2_common.h"
34
35#define LBZ2_FILE_WRITER_MT "LBZ2_FILE_WRITER_MT"
36
37typedef struct {
38 BZFILE *bz_stream;
39 FILE *f;
40} lbz2_file_writer;
41
42static lbz2_file_writer *lbz2_check_file_writer(lua_State *L, int index) {
43 return (lbz2_file_writer *)luaL_checkudata(L, index, LBZ2_FILE_WRITER_MT);
44}
45
46static int lbz2_file_writer_open(lua_State *L) {
47 lbz2_file_writer *writer;
48 int errorCode;
49 const char *fname = luaL_checkstring(L, 1);
50 int blockSize100k = luaL_optinteger(L, 2, 9);
51 int verbosity = luaL_optinteger(L, 3, 0);
52 int workFactor = luaL_optinteger(L, 4, 0);
53
54 writer = lua_newuserdata(L, sizeof(*writer));
55 memset(writer, 0, sizeof(*writer));
56
57 luaL_getmetatable(L, LBZ2_FILE_WRITER_MT);
58 lua_setmetatable(L, -2);
59
60 writer->f = fopen(fname, "wb");
61
62 if (writer->f == NULL) {
63 return luaL_error(L, "Failed to fopen %s", fname);
64 }
65 writer->bz_stream = BZ2_bzWriteOpen(&errorCode, writer->f, blockSize100k, verbosity, workFactor);
66
67 if (BZ_OK != errorCode) {
68 fclose(writer->f);
69 writer->f = NULL;
70 lua_pushnil(L);
71 lua_pushstring(L, lbz2_error(errorCode));
72 return 2;
73 }
74 return 1;
75}
76
77static int lbz2_file_writer_close(lua_State *L) {
78 lbz2_file_writer *writer = lbz2_check_file_writer(L, 1);
79 int errorCode = BZ_OK;
80
81 if (writer->bz_stream) {
82 BZ2_bzWriteClose(&errorCode, writer->bz_stream, 0, NULL, NULL);
83 writer->bz_stream = NULL;
84 }
85 if (writer->f) {
86 fclose(writer->f);
87 writer->f = NULL;
88 }
89
90 lua_pushnil(L);
91 lua_setmetatable(L, 1);
92
93 if (BZ_OK != errorCode) {
94 lua_pushnil(L);
95 lua_pushstring(L, lbz2_error(errorCode));
96 return 2;
97 }
98 lua_pushboolean(L, 1);
99 return 1;
100}
101
102static int lbz2_file_writer_write(lua_State *L) {
103 lbz2_file_writer *writer = lbz2_check_file_writer(L, 1);
104 int errorCode = BZ_OK;
105 size_t dataLength;
106 const char *data = luaL_checklstring(L, 2, &dataLength);
107
108 BZ2_bzWrite(&errorCode, writer->bz_stream, (void *)data, dataLength);
109
110 if (BZ_OK != errorCode) {
111 lua_pushnil(L);
112 lua_pushstring(L, lbz2_error(errorCode));
113 return 2;
114 }
115 lua_pushboolean(L, 1);
116 return 1;
117}
118
119static luaL_Reg lbz2_file_writer_ops[] = {
120 { "write", lbz2_file_writer_write },
121 { "close", lbz2_file_writer_close },
122 { NULL, NULL }
123};
124
125static luaL_Reg lbz2_file_writer_global[] = {
126 { "openWrite", lbz2_file_writer_open },
127 { NULL, NULL }
128};
129
130void register_lbz2_file_writer(lua_State *L) {
131 luaL_newmetatable(L, LBZ2_FILE_WRITER_MT);
132 lua_newtable(L);
133 luaL_setfuncs(L, lbz2_file_writer_ops, 0);
134 lua_setfield(L, -2, "__index");
135
136 lua_pushcfunction(L, lbz2_file_writer_close);
137 lua_setfield(L, -2, "__gc");
138 lua_pop(L, 1);
139
140 luaL_setfuncs(L, lbz2_file_writer_global, 0);
141}
diff --git a/vendor/lua-bz2/lbz2_file_writer.h b/vendor/lua-bz2/lbz2_file_writer.h
new file mode 100644
index 00000000..4f2e5e09
--- /dev/null
+++ b/vendor/lua-bz2/lbz2_file_writer.h
@@ -0,0 +1,25 @@
1/* This file implements the Lua binding to libbzip2.
2 *
3 * Copyright (c) 2008, Evan Klitzke <evan@eklitzke.org>
4 * Copyright (c) 2012, Thomas Harning Jr <harningt@gmail.com>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19
20#ifndef LBZ2_FILE_WRITER_H
21#define LBZ2_FILE_WRITER_H
22
23void register_lbz2_file_writer(lua_State *L);
24
25#endif
diff --git a/vendor/lua-bz2/lbz2_stream.c b/vendor/lua-bz2/lbz2_stream.c
new file mode 100644
index 00000000..9eb2ab2b
--- /dev/null
+++ b/vendor/lua-bz2/lbz2_stream.c
@@ -0,0 +1,263 @@
1/* This file implements the Lua binding to libbzip2.
2 *
3 * Copyright (c) 2008, Evan Klitzke <evan@eklitzke.org>
4 * Copyright (c) 2012, Thomas Harning Jr <harningt@gmail.com>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <bzlib.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <lua.h>
24#include <lauxlib.h>
25
26#include <assert.h>
27
28/* This explicit define prevents compat-5.3.h from loading compat-5.3.c */
29#define COMPAT53_PREFIX compat53
30#include "compat-5.3.h"
31
32#include "lbz2_stream.h"
33#include "lbz2_common.h"
34
35#define LBZ2_STREAM_MT "LBZ2_STREAM_MT"
36
37typedef struct {
38 bz_stream bz_stream;
39 int isDecompressing;
40} lbz2_stream;
41
42static lbz2_stream *lbz2_check_stream(lua_State *L, int index) {
43 return (lbz2_stream *)luaL_checkudata(L, index, LBZ2_STREAM_MT);
44}
45
46static int lbz2_stream_initCompress(lua_State *L) {
47 lbz2_stream *stream;
48 int errorCode;
49 int blockSize100k = luaL_optinteger(L, 1, 9);
50 int verbosity = luaL_optinteger(L, 2, 0);
51 int workFactor = luaL_optinteger(L, 3, 0);
52
53 stream = lua_newuserdata(L, sizeof(*stream));
54 memset(stream, 0, sizeof(*stream));
55
56 stream->isDecompressing = 0;
57
58 luaL_getmetatable(L, LBZ2_STREAM_MT);
59 lua_setmetatable(L, -2);
60
61 errorCode = BZ2_bzCompressInit(&stream->bz_stream, blockSize100k, verbosity, workFactor);
62
63 if (BZ_OK != errorCode) {
64 lua_pushnil(L);
65 lua_pushstring(L, lbz2_error(errorCode));
66 return 2;
67 }
68 return 1;
69}
70
71static int lbz2_stream_initDecompress(lua_State *L) {
72 lbz2_stream *stream;
73 int errorCode;
74 int verbosity = luaL_optinteger(L, 1, 0);
75 int isSmall = lua_toboolean(L, 2);
76
77 stream = lua_newuserdata(L, sizeof(*stream));
78 memset(stream, 0, sizeof(*stream));
79
80 stream->isDecompressing = 1;
81
82 luaL_getmetatable(L, LBZ2_STREAM_MT);
83 lua_setmetatable(L, -2);
84
85 errorCode = BZ2_bzDecompressInit(&stream->bz_stream, verbosity, isSmall);
86
87 if (BZ_OK != errorCode) {
88 lua_pushnil(L);
89 lua_pushstring(L, lbz2_error(errorCode));
90 return 2;
91 }
92 return 1;
93}
94static int lbz2_stream_close(lua_State *L) {
95 lbz2_stream *stream = lbz2_check_stream(L, 1);
96 int errorCode = BZ_OK;
97
98 if (stream->bz_stream.state) {
99 if (stream->isDecompressing) {
100 errorCode = BZ2_bzDecompressEnd(&stream->bz_stream);
101 } else {
102 errorCode = BZ2_bzCompressEnd(&stream->bz_stream);
103 }
104 }
105
106 lua_pushnil(L);
107 lua_setmetatable(L, 1);
108
109 if (BZ_OK != errorCode) {
110 lua_pushnil(L);
111 lua_pushstring(L, lbz2_error(errorCode));
112 return 2;
113 }
114 lua_pushboolean(L, 1);
115 return 1;
116}
117
118static int lbz2_stream_perform_compress(lua_State *L, lbz2_stream *stream, int action) {
119 int errorCode = BZ_OK;
120 luaL_Buffer B;
121
122 luaL_buffinit(L, &B);
123
124 while (1) {
125 stream->bz_stream.avail_out = LUAL_BUFFERSIZE;
126 stream->bz_stream.next_out = luaL_prepbuffer(&B);
127 errorCode = BZ2_bzCompress(&stream->bz_stream, action);
128
129 switch (action) {
130 case BZ_RUN:
131 if (BZ_RUN_OK != errorCode) {
132 goto fail;
133 }
134 luaL_addsize(&B, LUAL_BUFFERSIZE - stream->bz_stream.avail_out);
135 if (stream->bz_stream.avail_in == 0 || stream->bz_stream.avail_out == LUAL_BUFFERSIZE) {
136 goto complete;
137 }
138 break;
139 case BZ_FLUSH:
140 if (BZ_FLUSH_OK != errorCode && BZ_RUN_OK != errorCode) {
141 goto fail;
142 }
143 luaL_addsize(&B, LUAL_BUFFERSIZE - stream->bz_stream.avail_out);
144 if (BZ_RUN_OK == errorCode) {
145 goto complete;
146 }
147 break;
148 case BZ_FINISH:
149 if (BZ_FINISH_OK != errorCode && BZ_STREAM_END != errorCode) {
150 goto fail;
151 }
152 luaL_addsize(&B, LUAL_BUFFERSIZE - stream->bz_stream.avail_out);
153 if (BZ_STREAM_END == errorCode) {
154 goto complete;
155 }
156 }
157 }
158complete:
159 luaL_pushresult(&B);
160 return 1;
161
162fail:
163 lua_pushnil(L);
164 lua_pushstring(L, lbz2_error(errorCode));
165 return 2;
166}
167
168static int lbz2_stream_perform_decompress(lua_State *L, lbz2_stream *stream) {
169 int errorCode = BZ_OK;
170 luaL_Buffer B;
171
172 luaL_buffinit(L, &B);
173
174 while (1) {
175 stream->bz_stream.avail_out = LUAL_BUFFERSIZE;
176 stream->bz_stream.next_out = luaL_prepbuffer(&B);
177 errorCode = BZ2_bzDecompress(&stream->bz_stream);
178
179 if (BZ_OK != errorCode && BZ_STREAM_END != errorCode) {
180 goto fail;
181 }
182 luaL_addsize(&B, LUAL_BUFFERSIZE - stream->bz_stream.avail_out);
183 /* Stream over with */
184 if (errorCode == BZ_STREAM_END) {
185 goto completeStream;
186 }
187 /* No more bytes left this round */
188 if (stream->bz_stream.avail_in == 0 && stream->bz_stream.avail_out == LUAL_BUFFERSIZE) {
189 goto complete;
190 }
191 }
192complete:
193 luaL_pushresult(&B);
194 return 1;
195
196completeStream:
197 luaL_pushresult(&B);
198 /* Report in addition to the data collected, the number of trailing bytes
199 * still available in the input buffer for other use. */
200 lua_pushinteger(L, stream->bz_stream.avail_in);
201 return 2;
202
203fail:
204 lua_pushnil(L);
205 lua_pushstring(L, lbz2_error(errorCode));
206 return 2;
207}
208static int lbz2_stream_update(lua_State *L) {
209 lbz2_stream *stream = lbz2_check_stream(L, 1);
210 size_t dataLength;
211 const char *data = luaL_optlstring(L, 2, NULL, &dataLength);
212
213 /* Update the pointers and feed the output buffer while data is available */
214 stream->bz_stream.avail_in = dataLength;
215 /* Cast away const-ness since input data is never altered */
216 stream->bz_stream.next_in = (char *)data;
217
218 /* For compression, need to specially flag finishing state */
219 if (!stream->isDecompressing) {
220 return lbz2_stream_perform_compress(L, stream, !data ? BZ_FINISH : BZ_RUN);
221 } else {
222 return lbz2_stream_perform_decompress(L, stream);
223 }
224}
225
226static int lbz2_stream_flush(lua_State *L) {
227 lbz2_stream *stream = lbz2_check_stream(L, 1);
228 if (!stream->isDecompressing) {
229 return lbz2_stream_perform_compress(L, stream, BZ_FLUSH);
230 } else {
231 /* Invalid for decompression */
232 lua_pushnil(L);
233 lua_pushstring(L, lbz2_error(BZ_SEQUENCE_ERROR));
234 return 2;
235 }
236}
237
238static luaL_Reg lbz2_stream_ops[] = {
239 { "update", lbz2_stream_update },
240 { "flush", lbz2_stream_flush },
241 { "close", lbz2_stream_close },
242 { NULL, NULL }
243};
244
245static luaL_Reg lbz2_stream_global[] = {
246 { "initCompress", lbz2_stream_initCompress },
247 { "initDecompress", lbz2_stream_initDecompress },
248 { NULL, NULL }
249};
250
251void register_lbz2_stream(lua_State *L) {
252 luaL_newmetatable(L, LBZ2_STREAM_MT);
253 lua_newtable(L);
254 luaL_setfuncs(L, lbz2_stream_ops, 0);
255 lua_setfield(L, -2, "__index");
256
257 lua_pushcfunction(L, lbz2_stream_close);
258 lua_setfield(L, -2, "__gc");
259 lua_pop(L, 1);
260
261 luaL_setfuncs(L, lbz2_stream_global, 0);
262}
263
diff --git a/vendor/lua-bz2/lbz2_stream.h b/vendor/lua-bz2/lbz2_stream.h
new file mode 100644
index 00000000..a321ea59
--- /dev/null
+++ b/vendor/lua-bz2/lbz2_stream.h
@@ -0,0 +1,25 @@
1/* This file implements the Lua binding to libbzip2.
2 *
3 * Copyright (c) 2008, Evan Klitzke <evan@eklitzke.org>
4 * Copyright (c) 2012, Thomas Harning Jr <harningt@gmail.com>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19
20#ifndef LBZ2_STREAM_H
21#define LBZ2_STREAM_H
22
23void register_lbz2_stream(lua_State *L);
24
25#endif
diff --git a/vendor/lua-bz2/lua-bz2-0.2.1-1.rockspec b/vendor/lua-bz2/lua-bz2-0.2.1-1.rockspec
new file mode 100644
index 00000000..c66770f6
--- /dev/null
+++ b/vendor/lua-bz2/lua-bz2-0.2.1-1.rockspec
@@ -0,0 +1,48 @@
1package = "lua-bz2"
2version = "0.2.1-1"
3source = {
4 url = "git+ssh://git@github.com/hishamhm/lua-bz2.git",
5 tag = "0.2.1"
6}
7description = {
8 summary = "A Lua binding to Julian Seward's libbzip2",
9 detailed = [[
10 Support for reading and writing .bz2 files
11 and handling streams compressed in bzip2 format.
12 ]],
13 homepage = "https://github.com/hishamhm/lua-bz2",
14 license = "ISC"
15}
16external_dependencies = {
17 BZ2 = {
18 library = "bz2"
19 }
20}
21build = {
22 type = "builtin",
23 modules = {
24 bz2 = {
25 defines = {
26 "COMPAT53_PREFIX=compat53"
27 },
28 incdirs = {
29 "$(BZ2_INCDIR)"
30 },
31 libdirs = {
32 "$(BZ2_LIBDIR)"
33 },
34 libraries = {
35 "bz2"
36 },
37 sources = {
38 "lbz.c",
39 "lbz2_common.c",
40 "lbz2_file_reader.c",
41 "lbz2_file_writer.c",
42 "lbz2_stream.c",
43 "compat-5.3.c"
44 }
45 },
46 ["bz2.ltn12"] = "bz2/ltn12.lua"
47 }
48}