├── test ├── input │ ├── die1.png │ ├── die2.png │ ├── die3.png │ ├── die4.png │ ├── die5.png │ ├── idle.png │ └── stone.png ├── input_advance │ ├── body.png │ ├── legs1.png │ ├── legs2.png │ ├── legs3.png │ ├── legs4.png │ ├── actor.a.lua │ └── actor_anim.a.lua └── test.lua ├── .gitignore ├── 3rd ├── lua │ ├── lua.hpp │ ├── lapi.h │ ├── lundump.h │ ├── lfunc.h │ ├── ldebug.h │ ├── lualib.h │ ├── ltm.h │ ├── lstring.h │ ├── ltable.h │ ├── lvm.h │ ├── ldo.h │ ├── lzio.h │ ├── lzio.c │ ├── linit.c │ ├── lmem.h │ ├── ltm.c │ ├── lctype.h │ ├── llex.h │ ├── lctype.c │ ├── lmem.c │ ├── lcode.h │ ├── lopcodes.c │ ├── lparser.h │ ├── ldump.c │ ├── lcorolib.c │ ├── lfunc.c │ ├── lbitlib.c │ ├── lstring.c │ ├── lgc.h │ ├── lundump.c │ ├── lmathlib.c │ ├── lauxlib.h │ └── lstate.h ├── zlib │ ├── inffast.h │ ├── uncompr.c │ ├── compress.c │ ├── inftrees.h │ ├── adler32.c │ ├── inffixed.h │ ├── inflate.h │ └── zutil.h └── libpng │ ├── pngrio.c │ ├── pngdebug.h │ ├── pngwio.c │ └── pnglibconf.h ├── src ├── img_png.h ├── img_ppm.h ├── main.c ├── scripts │ ├── utils.lua │ ├── binpacking.lua │ ├── ejresource.lua │ ├── ejpackage.lua │ └── main.lua ├── libos.c ├── img_png.c ├── img_ppm.c └── libimg.c ├── README.md └── LICENSE /test/input/die1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/simplepacker/HEAD/test/input/die1.png -------------------------------------------------------------------------------- /test/input/die2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/simplepacker/HEAD/test/input/die2.png -------------------------------------------------------------------------------- /test/input/die3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/simplepacker/HEAD/test/input/die3.png -------------------------------------------------------------------------------- /test/input/die4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/simplepacker/HEAD/test/input/die4.png -------------------------------------------------------------------------------- /test/input/die5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/simplepacker/HEAD/test/input/die5.png -------------------------------------------------------------------------------- /test/input/idle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/simplepacker/HEAD/test/input/idle.png -------------------------------------------------------------------------------- /test/input/stone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/simplepacker/HEAD/test/input/stone.png -------------------------------------------------------------------------------- /test/input_advance/body.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/simplepacker/HEAD/test/input_advance/body.png -------------------------------------------------------------------------------- /test/input_advance/legs1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/simplepacker/HEAD/test/input_advance/legs1.png -------------------------------------------------------------------------------- /test/input_advance/legs2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/simplepacker/HEAD/test/input_advance/legs2.png -------------------------------------------------------------------------------- /test/input_advance/legs3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/simplepacker/HEAD/test/input_advance/legs3.png -------------------------------------------------------------------------------- /test/input_advance/legs4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/simplepacker/HEAD/test/input_advance/legs4.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | bin 4 | test/output 5 | 6 | build/msvc/*.sdf 7 | build/msvc/*.suo 8 | build/msvc/*.opensdf 9 | build/msvc/ipch 10 | build/msvc/_build 11 | 12 | build/osx/_build 13 | -------------------------------------------------------------------------------- /3rd/lua/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /test/input_advance/actor.a.lua: -------------------------------------------------------------------------------- 1 | return { 2 | 3 | -- this animation file is written manually 4 | -- will be generate automacally and is the image name 5 | 6 | { 7 | { 8 | { name="legs" }, -- image contains offset, no need to give a trans here 9 | { name="body" }, 10 | }, -- only one frame 11 | }, 12 | 13 | } -------------------------------------------------------------------------------- /src/img_png.h: -------------------------------------------------------------------------------- 1 | #ifndef __IMG_PNG_H__ 2 | #define __IMG_PNG_H__ 3 | 4 | #define PNG_UNKNOWN 0 5 | #define PNG_RGBA 1 6 | #define PNG_RGB 2 7 | 8 | struct png { 9 | int type; 10 | int channel; 11 | int width; 12 | int height; 13 | uint8_t *buffer; 14 | }; 15 | 16 | int img_loadpng(const char* filename, struct png* png); 17 | int img_savepng(const char* filename, int format, int width, int height, uint8_t* buffer); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /3rd/zlib/inffast.h: -------------------------------------------------------------------------------- 1 | /* inffast.h -- header to use inffast.c 2 | * Copyright (C) 1995-2003, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start)); 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SimplePacker 2 | 3 | simplepacker是一个为[ejoy2d](https://github.com/ejoy/ejoy2d)引擎设计的打包工具,可以把包含PNG图片的文件夹打包成[simplepackage](https://github.com/ejoy/ejoy2d/blob/master/doc/apicn.md#simplepackage)可以读取的格式。 4 | 5 | Build 6 | ===== 7 | windows 8 | * open build\msvc\VS2013.sln 9 | * build release 10 | * run build\msvc\test.bat 11 | 12 | macosx 13 | * cd build/osx 14 | * make 15 | * make test 16 | 17 | Test 18 | ==== 19 | test目录下包含两个测试示例。input演示了简单的图片打包与animation自动生成;input_advance演示了自定义animation。 20 | 打包后生成test/output,配合test/test.lua可在ejoy2d引擎中运行。 -------------------------------------------------------------------------------- /src/img_ppm.h: -------------------------------------------------------------------------------- 1 | #ifndef __IMG_PPM_H__ 2 | #define __IMG_PPM_H__ 3 | 4 | #define PPM_UNKNOWN 0 5 | #define PPM_RGBA8 1 6 | #define PPM_RGB8 2 7 | #define PPM_RGBA4 3 8 | #define PPM_RGB4 4 9 | #define PPM_ALPHA8 5 10 | #define PPM_ALPHA4 6 11 | 12 | struct ppm { 13 | int type; 14 | int depth; 15 | int step; 16 | int width; 17 | int height; 18 | uint8_t *buffer; 19 | }; 20 | 21 | int img_loadppm(const char* filename, struct ppm* ppm); 22 | int img_saveppm(const char* filename, int format, int width, int height, uint8_t* buffer); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /test/input_advance/actor_anim.a.lua: -------------------------------------------------------------------------------- 1 | -- this file demonstrate advanced animation usage 2 | 3 | local function _get_color(alpha) 4 | return alpha * (2^24) + 0xffffff -- ejoy2d use ARGB color format 5 | end 6 | 7 | local blink_action = {action="blink"} 8 | for i=0,255,5 do 9 | local component = {name="actor", color=_get_color(i)} 10 | local frame = {component} 11 | table.insert(blink_action, frame) 12 | end 13 | for i=255,0,-5 do 14 | local component = {name="actor", color=_get_color(i)} 15 | local frame = {component} 16 | table.insert(blink_action, frame) 17 | end 18 | 19 | return { 20 | 21 | blink_action, 22 | 23 | } -------------------------------------------------------------------------------- /3rd/lua/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /3rd/lua/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.39.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | /* load one chunk; from lundump.c */ 14 | LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 15 | 16 | /* make header; from lundump.c */ 17 | LUAI_FUNC void luaU_header (lu_byte* h); 18 | 19 | /* dump one chunk; from ldump.c */ 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 21 | 22 | /* data to catch conversion errors */ 23 | #define LUAC_TAIL "\x19\x93\r\n\x1a\n" 24 | 25 | /* size in bytes of header of binary files */ 26 | #define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char)) 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | #include "lualib.h" 2 | #include "lauxlib.h" 3 | 4 | int register_libimage(lua_State *L); 5 | int register_libos(lua_State *L); 6 | 7 | int main(int argc, char** argv) { 8 | lua_State *L = luaL_newstate(); 9 | 10 | luaL_openlibs(L); 11 | luaL_requiref(L, "libimage", register_libimage, 0); 12 | luaL_requiref(L, "libos", register_libos, 0); 13 | lua_pop(L, 2); 14 | 15 | if (luaL_dofile(L, "main.lua") != LUA_OK) { 16 | printf("====== load script failed ======\n%s\n", lua_tostring(L, -1)); 17 | goto ERR; 18 | } 19 | 20 | lua_getglobal(L, "run"); 21 | if (!lua_isfunction(L, -1)) { 22 | printf("====== cannot find script entry function ======\n"); 23 | goto ERR; 24 | } 25 | 26 | lua_newtable(L); 27 | for (int i=0; icode) - 1) 15 | 16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | /* Active Lua function (given call info) */ 21 | #define ci_func(ci) (clLvalue((ci)->func)) 22 | 23 | 24 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 25 | const char *opname); 26 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2); 27 | LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1, 28 | const TValue *p2); 29 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 30 | const TValue *p2); 31 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 32 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /3rd/lua/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.43.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | 15 | LUAMOD_API int (luaopen_base) (lua_State *L); 16 | 17 | #define LUA_COLIBNAME "coroutine" 18 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 19 | 20 | #define LUA_TABLIBNAME "table" 21 | LUAMOD_API int (luaopen_table) (lua_State *L); 22 | 23 | #define LUA_IOLIBNAME "io" 24 | LUAMOD_API int (luaopen_io) (lua_State *L); 25 | 26 | #define LUA_OSLIBNAME "os" 27 | LUAMOD_API int (luaopen_os) (lua_State *L); 28 | 29 | #define LUA_STRLIBNAME "string" 30 | LUAMOD_API int (luaopen_string) (lua_State *L); 31 | 32 | #define LUA_BITLIBNAME "bit32" 33 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 34 | 35 | #define LUA_MATHLIBNAME "math" 36 | LUAMOD_API int (luaopen_math) (lua_State *L); 37 | 38 | #define LUA_DBLIBNAME "debug" 39 | LUAMOD_API int (luaopen_debug) (lua_State *L); 40 | 41 | #define LUA_LOADLIBNAME "package" 42 | LUAMOD_API int (luaopen_package) (lua_State *L); 43 | 44 | 45 | /* open all previous libraries */ 46 | LUALIB_API void (luaL_openlibs) (lua_State *L); 47 | 48 | 49 | 50 | #if !defined(lua_assert) 51 | #define lua_assert(x) ((void)0) 52 | #endif 53 | 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /3rd/lua/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.11.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with `fast' access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_DIV, 29 | TM_MOD, 30 | TM_POW, 31 | TM_UNM, 32 | TM_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | #define ttypename(x) luaT_typenames_[(x) + 1] 47 | #define objtypename(x) ttypename(ttypenv(x)) 48 | 49 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 50 | 51 | 52 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 53 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 54 | TMS event); 55 | LUAI_FUNC void luaT_init (lua_State *L); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /test/test.lua: -------------------------------------------------------------------------------- 1 | local ej = require "ejoy2d" 2 | local fw = require "ejoy2d.framework" 3 | local pack = require "ejoy2d.simplepackage" 4 | 5 | pack.load { 6 | pattern = fw.WorkDir..[[examples/asset/?]], 7 | "input", 8 | "input_advance", 9 | } 10 | 11 | local idle = ej.sprite("input", "idle") 12 | local die = ej.sprite("input", "die") 13 | local stone = ej.sprite("input", "stone") 14 | idle:ps(100, 300) 15 | die:ps(100, 300) 16 | die.visible = false 17 | 18 | local blink = ej.sprite("input_advance", "actor_anim") 19 | blink:ps(240, 300) 20 | 21 | local y = 100 22 | 23 | local game = {} 24 | 25 | function game.update() 26 | if stone.visible and y < 200 then 27 | y = y + 5 28 | stone:ps(100, y) 29 | if y >= 200 then 30 | stone.visible = false 31 | idle.visible = false 32 | die.visible = true 33 | end 34 | end 35 | 36 | if die.visible and die.frame < 4 then 37 | die.frame = die.frame + 1 38 | end 39 | 40 | blink.frame = blink.frame + 1 41 | end 42 | 43 | function game.drawframe() 44 | ej.clear(0xff808080) 45 | 46 | idle:draw() 47 | die:draw() 48 | stone:draw() 49 | blink:draw() 50 | end 51 | 52 | function game.touch(what, x, y) 53 | end 54 | 55 | function game.message(...) 56 | end 57 | 58 | function game.handle_error(...) 59 | end 60 | 61 | function game.on_resume() 62 | end 63 | 64 | function game.on_pause() 65 | end 66 | 67 | ej.start(game) -------------------------------------------------------------------------------- /3rd/lua/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 16 | 17 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 18 | 19 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 20 | (sizeof(s)/sizeof(char))-1)) 21 | 22 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 23 | 24 | 25 | /* 26 | ** test whether a string is a reserved word 27 | */ 28 | #define isreserved(s) ((s)->tsv.tt == LUA_TSHRSTR && (s)->tsv.extra > 0) 29 | 30 | 31 | /* 32 | ** equality for short strings, which are always internalized 33 | */ 34 | #define eqshrstr(a,b) check_exp((a)->tsv.tt == LUA_TSHRSTR, (a) == (b)) 35 | 36 | 37 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 39 | LUAI_FUNC int luaS_eqstr (TString *a, TString *b); 40 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 41 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 42 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 43 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /3rd/lua/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.16.1.2 2013/08/30 15:49:41 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gkey(n) (&(n)->i_key.tvk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define invalidateTMcache(t) ((t)->flags = 0) 19 | 20 | /* returns the key, given the value of a table entry */ 21 | #define keyfromval(v) \ 22 | (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) 23 | 24 | 25 | LUAI_FUNC const TValue *luaH_getint (Table *t, int key); 26 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value); 27 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 28 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 29 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 30 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 31 | LUAI_FUNC Table *luaH_new (lua_State *L); 32 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); 33 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 34 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 35 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 36 | LUAI_FUNC int luaH_getn (Table *t); 37 | 38 | 39 | #if defined(LUA_DEBUG) 40 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 41 | LUAI_FUNC int luaH_isdummy (Node *n); 42 | #endif 43 | 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /3rd/lua/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.18.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL)) 19 | 20 | #define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2)) 21 | 22 | #define luaV_rawequalobj(o1,o2) equalobj(NULL,o1,o2) 23 | 24 | 25 | /* not to called directly */ 26 | LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2); 27 | 28 | 29 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 30 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 31 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 32 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 33 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 34 | StkId val); 35 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 36 | StkId val); 37 | LUAI_FUNC void luaV_finishOp (lua_State *L); 38 | LUAI_FUNC void luaV_execute (lua_State *L); 39 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 40 | LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb, 41 | const TValue *rc, TMS op); 42 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /3rd/lua/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.20.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \ 17 | luaD_growstack(L, n); else condmovestack(L); 18 | 19 | 20 | #define incr_top(L) {L->top++; luaD_checkstack(L,0);} 21 | 22 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 23 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 24 | 25 | 26 | /* type of protected functions, to be ran by `runprotected' */ 27 | typedef void (*Pfunc) (lua_State *L, void *ud); 28 | 29 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 30 | const char *mode); 31 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 32 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 33 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, 34 | int allowyield); 35 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 36 | ptrdiff_t oldtop, ptrdiff_t ef); 37 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 38 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 39 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 40 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 41 | 42 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 43 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 44 | 45 | #endif 46 | 47 | -------------------------------------------------------------------------------- /3rd/lua/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.26.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 36 | 37 | 38 | #define luaZ_resizebuffer(L, buff, size) \ 39 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 40 | (buff)->buffsize = size) 41 | 42 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 43 | 44 | 45 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 46 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 47 | void *data); 48 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 49 | 50 | 51 | 52 | /* --------- Private Part ------------------ */ 53 | 54 | struct Zio { 55 | size_t n; /* bytes still unread */ 56 | const char *p; /* current position in buffer */ 57 | lua_Reader reader; /* reader function */ 58 | void* data; /* additional data */ 59 | lua_State *L; /* Lua state (for reader) */ 60 | }; 61 | 62 | 63 | LUAI_FUNC int luaZ_fill (ZIO *z); 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /src/scripts/utils.lua: -------------------------------------------------------------------------------- 1 | -- utils module 2 | local M = { 3 | logf = function (...) end 4 | } 5 | 6 | function M:_logf(...) 7 | print("[SCRIPT] "..string.format(...)) 8 | end 9 | 10 | function M:enable_log() 11 | self.logf = self._logf 12 | end 13 | 14 | function M:check_ext(file_name, ext) 15 | return string.find(file_name, ext, 1, true) == #file_name - #ext + 1 16 | end 17 | 18 | function M:trim_slash(path) 19 | if path[#path] == "\\" or path[#path] == "/" then 20 | return string.sub(path, 1, -2) 21 | else 22 | return path 23 | end 24 | end 25 | 26 | function M:_matrix_identity() 27 | return {1, 0, 0, 1, 0, 0} 28 | end 29 | 30 | -- ejoy2d uses a 2x3 row-major matrix (the third column is 0,0,1) 31 | function M:_matrix_multiply(m1, m2) 32 | return { 33 | m1[1]*m2[1] + m1[2]*m2[3], 34 | m1[1]*m2[2] + m1[2]*m2[4], 35 | m1[3]*m2[1] + m1[4]*m2[3], 36 | m1[3]*m2[2] + m1[4]*m2[4], 37 | m1[5]*m2[1] + m1[6]*m2[3] + m2[5], 38 | m1[5]*m2[2] + m1[6]*m2[4] + m2[6], 39 | } 40 | end 41 | 42 | function M:create_matrix(scale, rot, trans) 43 | local s = self:_matrix_identity() 44 | local r = self:_matrix_identity() 45 | local t = self:_matrix_identity() 46 | 47 | if scale then 48 | s[1] = scale[1] 49 | s[4] = scale[2] 50 | end 51 | 52 | if rot then 53 | local rad = math.rad(rot) -- rot is in degree 54 | local cos = math.cos(rad) 55 | local sin = math.sin(rad) 56 | r[1] = cos 57 | r[2] = sin 58 | r[3] = -sin 59 | r[4] = cos 60 | end 61 | 62 | if trans then 63 | t[5] = trans[1] 64 | t[6] = trans[2] 65 | end 66 | 67 | local ret = self:_matrix_multiply(self:_matrix_multiply(s, r), t) 68 | ret[1] = math.floor(ret[1] * 1024) -- ejoy2d uses "fix-point" number 69 | ret[2] = math.floor(ret[2] * 1024) 70 | ret[3] = math.floor(ret[3] * 1024) 71 | ret[4] = math.floor(ret[4] * 1024) 72 | ret[5] = math.floor(ret[5] * 16) 73 | ret[6] = math.floor(ret[6] * 16) 74 | 75 | return ret 76 | end 77 | 78 | return M -------------------------------------------------------------------------------- /3rd/lua/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.35.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lzio_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "llimits.h" 16 | #include "lmem.h" 17 | #include "lstate.h" 18 | #include "lzio.h" 19 | 20 | 21 | int luaZ_fill (ZIO *z) { 22 | size_t size; 23 | lua_State *L = z->L; 24 | const char *buff; 25 | lua_unlock(L); 26 | buff = z->reader(L, z->data, &size); 27 | lua_lock(L); 28 | if (buff == NULL || size == 0) 29 | return EOZ; 30 | z->n = size - 1; /* discount char being returned */ 31 | z->p = buff; 32 | return cast_uchar(*(z->p++)); 33 | } 34 | 35 | 36 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 37 | z->L = L; 38 | z->reader = reader; 39 | z->data = data; 40 | z->n = 0; 41 | z->p = NULL; 42 | } 43 | 44 | 45 | /* --------------------------------------------------------------- read --- */ 46 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 47 | while (n) { 48 | size_t m; 49 | if (z->n == 0) { /* no bytes in buffer? */ 50 | if (luaZ_fill(z) == EOZ) /* try to read more */ 51 | return n; /* no more input; return number of missing bytes */ 52 | else { 53 | z->n++; /* luaZ_fill consumed first byte; put it back */ 54 | z->p--; 55 | } 56 | } 57 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 58 | memcpy(b, z->p, m); 59 | z->n -= m; 60 | z->p += m; 61 | b = (char *)b + m; 62 | n -= m; 63 | } 64 | return 0; 65 | } 66 | 67 | /* ------------------------------------------------------------------------ */ 68 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 69 | if (n > buff->buffsize) { 70 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 71 | luaZ_resizebuffer(L, buff, n); 72 | } 73 | return buff->buffer; 74 | } 75 | 76 | 77 | -------------------------------------------------------------------------------- /3rd/lua/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.32.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | /* 9 | ** If you embed Lua in your program and need to open the standard 10 | ** libraries, call luaL_openlibs in your program. If you need a 11 | ** different set of libraries, copy this file to your project and edit 12 | ** it to suit your needs. 13 | */ 14 | 15 | 16 | #define linit_c 17 | #define LUA_LIB 18 | 19 | #include "lua.h" 20 | 21 | #include "lualib.h" 22 | #include "lauxlib.h" 23 | 24 | 25 | /* 26 | ** these libs are loaded by lua.c and are readily available to any Lua 27 | ** program 28 | */ 29 | static const luaL_Reg loadedlibs[] = { 30 | {"_G", luaopen_base}, 31 | {LUA_LOADLIBNAME, luaopen_package}, 32 | {LUA_COLIBNAME, luaopen_coroutine}, 33 | {LUA_TABLIBNAME, luaopen_table}, 34 | {LUA_IOLIBNAME, luaopen_io}, 35 | {LUA_OSLIBNAME, luaopen_os}, 36 | {LUA_STRLIBNAME, luaopen_string}, 37 | {LUA_BITLIBNAME, luaopen_bit32}, 38 | {LUA_MATHLIBNAME, luaopen_math}, 39 | {LUA_DBLIBNAME, luaopen_debug}, 40 | {NULL, NULL} 41 | }; 42 | 43 | 44 | /* 45 | ** these libs are preloaded and must be required before used 46 | */ 47 | static const luaL_Reg preloadedlibs[] = { 48 | {NULL, NULL} 49 | }; 50 | 51 | 52 | LUALIB_API void luaL_openlibs (lua_State *L) { 53 | const luaL_Reg *lib; 54 | /* call open functions from 'loadedlibs' and set results to global table */ 55 | for (lib = loadedlibs; lib->func; lib++) { 56 | luaL_requiref(L, lib->name, lib->func, 1); 57 | lua_pop(L, 1); /* remove lib */ 58 | } 59 | /* add open functions from 'preloadedlibs' into 'package.preload' table */ 60 | luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 61 | for (lib = preloadedlibs; lib->func; lib++) { 62 | lua_pushcfunction(L, lib->func); 63 | lua_setfield(L, -2, lib->name); 64 | } 65 | lua_pop(L, 1); /* remove _PRELOAD table */ 66 | } 67 | 68 | -------------------------------------------------------------------------------- /3rd/lua/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | /* 18 | ** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is 19 | ** always constant. 20 | ** The macro is somewhat complex to avoid warnings: 21 | ** +1 avoids warnings of "comparison has constant result"; 22 | ** cast to 'void' avoids warnings of "value unused". 23 | */ 24 | #define luaM_reallocv(L,b,on,n,e) \ 25 | (cast(void, \ 26 | (cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \ 27 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 28 | 29 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 30 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 31 | #define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0])) 32 | 33 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 34 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 35 | #define luaM_newvector(L,n,t) \ 36 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 37 | 38 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 39 | 40 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 41 | if ((nelems)+1 > (size)) \ 42 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 43 | 44 | #define luaM_reallocvector(L, v,oldn,n,t) \ 45 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 46 | 47 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 48 | 49 | /* not to be called directly */ 50 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 51 | size_t size); 52 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 53 | size_t size_elem, int limit, 54 | const char *what); 55 | 56 | #endif 57 | 58 | -------------------------------------------------------------------------------- /3rd/lua/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.14.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltm_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lobject.h" 16 | #include "lstate.h" 17 | #include "lstring.h" 18 | #include "ltable.h" 19 | #include "ltm.h" 20 | 21 | 22 | static const char udatatypename[] = "userdata"; 23 | 24 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { 25 | "no value", 26 | "nil", "boolean", udatatypename, "number", 27 | "string", "table", "function", udatatypename, "thread", 28 | "proto", "upval" /* these last two cases are used for tests only */ 29 | }; 30 | 31 | 32 | void luaT_init (lua_State *L) { 33 | static const char *const luaT_eventname[] = { /* ORDER TM */ 34 | "__index", "__newindex", 35 | "__gc", "__mode", "__len", "__eq", 36 | "__add", "__sub", "__mul", "__div", "__mod", 37 | "__pow", "__unm", "__lt", "__le", 38 | "__concat", "__call" 39 | }; 40 | int i; 41 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 43 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 44 | } 45 | } 46 | 47 | 48 | /* 49 | ** function to be used with macro "fasttm": optimized for absence of 50 | ** tag methods 51 | */ 52 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 53 | const TValue *tm = luaH_getstr(events, ename); 54 | lua_assert(event <= TM_EQ); 55 | if (ttisnil(tm)) { /* no tag method? */ 56 | events->flags |= cast_byte(1u<metatable; 68 | break; 69 | case LUA_TUSERDATA: 70 | mt = uvalue(o)->metatable; 71 | break; 72 | default: 73 | mt = G(L)->mt[ttypenv(o)]; 74 | } 75 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 76 | } 77 | 78 | -------------------------------------------------------------------------------- /3rd/zlib/uncompr.c: -------------------------------------------------------------------------------- 1 | /* uncompr.c -- decompress a memory buffer 2 | * Copyright (C) 1995-2003, 2010 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #define ZLIB_INTERNAL 9 | #include "zlib.h" 10 | 11 | /* =========================================================================== 12 | Decompresses the source buffer into the destination buffer. sourceLen is 13 | the byte length of the source buffer. Upon entry, destLen is the total 14 | size of the destination buffer, which must be large enough to hold the 15 | entire uncompressed data. (The size of the uncompressed data must have 16 | been saved previously by the compressor and transmitted to the decompressor 17 | by some mechanism outside the scope of this compression library.) 18 | Upon exit, destLen is the actual size of the compressed buffer. 19 | 20 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not 21 | enough memory, Z_BUF_ERROR if there was not enough room in the output 22 | buffer, or Z_DATA_ERROR if the input data was corrupted. 23 | */ 24 | int ZEXPORT uncompress (dest, destLen, source, sourceLen) 25 | Bytef *dest; 26 | uLongf *destLen; 27 | const Bytef *source; 28 | uLong sourceLen; 29 | { 30 | z_stream stream; 31 | int err; 32 | 33 | stream.next_in = (z_const Bytef *)source; 34 | stream.avail_in = (uInt)sourceLen; 35 | /* Check for source > 64K on 16-bit machine: */ 36 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 37 | 38 | stream.next_out = dest; 39 | stream.avail_out = (uInt)*destLen; 40 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 41 | 42 | stream.zalloc = (alloc_func)0; 43 | stream.zfree = (free_func)0; 44 | 45 | err = inflateInit(&stream); 46 | if (err != Z_OK) return err; 47 | 48 | err = inflate(&stream, Z_FINISH); 49 | if (err != Z_STREAM_END) { 50 | inflateEnd(&stream); 51 | if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) 52 | return Z_DATA_ERROR; 53 | return err; 54 | } 55 | *destLen = stream.total_out; 56 | 57 | err = inflateEnd(&stream); 58 | return err; 59 | } 60 | -------------------------------------------------------------------------------- /3rd/lua/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /3rd/lua/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.72.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | 17 | 18 | /* 19 | * WARNING: if you change the order of this enumeration, 20 | * grep "ORDER RESERVED" 21 | */ 22 | enum RESERVED { 23 | /* terminal symbols denoted by reserved words */ 24 | TK_AND = FIRST_RESERVED, TK_BREAK, 25 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 26 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 27 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 28 | /* other terminal symbols */ 29 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, 30 | TK_NUMBER, TK_NAME, TK_STRING 31 | }; 32 | 33 | /* number of reserved words */ 34 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 35 | 36 | 37 | typedef union { 38 | lua_Number r; 39 | TString *ts; 40 | } SemInfo; /* semantics information */ 41 | 42 | 43 | typedef struct Token { 44 | int token; 45 | SemInfo seminfo; 46 | } Token; 47 | 48 | 49 | /* state of the lexer plus state of the parser when shared by all 50 | functions */ 51 | typedef struct LexState { 52 | int current; /* current character (charint) */ 53 | int linenumber; /* input line counter */ 54 | int lastline; /* line of last token `consumed' */ 55 | Token t; /* current token */ 56 | Token lookahead; /* look ahead token */ 57 | struct FuncState *fs; /* current function (parser) */ 58 | struct lua_State *L; 59 | ZIO *z; /* input stream */ 60 | Mbuffer *buff; /* buffer for tokens */ 61 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 62 | TString *source; /* current source name */ 63 | TString *envn; /* environment variable name */ 64 | char decpoint; /* locale decimal point */ 65 | } LexState; 66 | 67 | 68 | LUAI_FUNC void luaX_init (lua_State *L); 69 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 70 | TString *source, int firstchar); 71 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 72 | LUAI_FUNC void luaX_next (LexState *ls); 73 | LUAI_FUNC int luaX_lookahead (LexState *ls); 74 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 75 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 76 | 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /3rd/lua/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.11.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lctype.h" 11 | 12 | #if !LUA_USE_CTYPE /* { */ 13 | 14 | #include 15 | 16 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 17 | 0x00, /* EOZ */ 18 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 19 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 20 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 22 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 23 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 24 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 25 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 26 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 27 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 28 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 29 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 30 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 32 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 33 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 34 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 35 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 36 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 50 | }; 51 | 52 | #endif /* } */ 53 | -------------------------------------------------------------------------------- /src/libos.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "lualib.h" 6 | #include "lauxlib.h" 7 | 8 | #if defined(WIN32) 9 | #define WIN32_LEAN_AND_MEAN 10 | #include 11 | #include 12 | #elif defined (OSX) 13 | #include 14 | #include 15 | #include 16 | #include 17 | #endif 18 | 19 | static int 20 | lwalkdir(lua_State *L) { 21 | const char* path = luaL_checkstring(L, -1); 22 | lua_newtable(L); 23 | int i = 1; 24 | 25 | #if defined(WIN32) 26 | char szPath[MAX_PATH]; 27 | sprintf(szPath, "%s\\*", path); 28 | 29 | WIN32_FIND_DATA findData; 30 | HANDLE hFind = INVALID_HANDLE_VALUE; 31 | hFind = FindFirstFile(szPath, &findData); 32 | if (hFind != INVALID_HANDLE_VALUE) { 33 | while (1) { 34 | if (findData.cFileName[0] != '.') { 35 | lua_pushnumber(L, i); 36 | lua_pushstring(L, findData.cFileName); 37 | lua_settable(L, -3); 38 | i += 1; 39 | } 40 | 41 | if (!FindNextFile(hFind, &findData)) 42 | break; 43 | } 44 | FindClose(hFind); 45 | } 46 | #elif defined (OSX) 47 | DIR *dp = opendir(path); 48 | struct dirent* dir; 49 | while ((dir = readdir(dp)) != NULL) { 50 | lua_pushnumber(L, i); 51 | lua_pushstring(L, dir->d_name); 52 | lua_settable(L, -3); 53 | i += 1; 54 | } 55 | closedir(dp); 56 | #endif 57 | 58 | return 1; 59 | } 60 | 61 | static int 62 | lmakedir(lua_State *L) { 63 | const char* path = luaL_checkstring(L, -1); 64 | 65 | #if defined(WIN32) 66 | char buf[MAX_PATH]; 67 | GetFullPathName(path, MAX_PATH, buf, NULL); 68 | SHCreateDirectoryEx(NULL, buf, NULL); 69 | #elif defined (OSX) 70 | char* buf = (char*)malloc(sizeof(char) * (strlen(path) + 1)); 71 | strcpy(buf, path); 72 | char* p = buf; 73 | while (*p) { 74 | ++p; 75 | while (*p && *p != '/') ++p; 76 | 77 | char tmp = *p; 78 | *p = '\0'; 79 | if (mkdir(buf, 0755) == -1 && errno != EEXIST) break; 80 | *p = tmp; 81 | } 82 | free(buf); 83 | #endif 84 | 85 | return 0; 86 | } 87 | 88 | static int 89 | lwritefile(lua_State *L) { 90 | const char* path = luaL_checkstring(L, -2); 91 | const char* content = luaL_checkstring(L, -1); 92 | 93 | FILE* fp = fopen(path, "w"); 94 | if (!fp) { 95 | luaL_error(L, "can't write to %s", path); 96 | } 97 | 98 | fputs(content, fp); 99 | fclose(fp); 100 | 101 | return 0; 102 | } 103 | 104 | int 105 | register_libos(lua_State *L) { 106 | luaL_Reg l[] = { 107 | { "walkdir", lwalkdir }, 108 | { "makedir", lmakedir }, 109 | { "writefile", lwritefile }, 110 | { NULL, NULL } 111 | }; 112 | 113 | luaL_newlib(L, l); 114 | return 1; 115 | } -------------------------------------------------------------------------------- /3rd/zlib/compress.c: -------------------------------------------------------------------------------- 1 | /* compress.c -- compress a memory buffer 2 | * Copyright (C) 1995-2005 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #define ZLIB_INTERNAL 9 | #include "zlib.h" 10 | 11 | /* =========================================================================== 12 | Compresses the source buffer into the destination buffer. The level 13 | parameter has the same meaning as in deflateInit. sourceLen is the byte 14 | length of the source buffer. Upon entry, destLen is the total size of the 15 | destination buffer, which must be at least 0.1% larger than sourceLen plus 16 | 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. 17 | 18 | compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 19 | memory, Z_BUF_ERROR if there was not enough room in the output buffer, 20 | Z_STREAM_ERROR if the level parameter is invalid. 21 | */ 22 | int ZEXPORT compress2 (dest, destLen, source, sourceLen, level) 23 | Bytef *dest; 24 | uLongf *destLen; 25 | const Bytef *source; 26 | uLong sourceLen; 27 | int level; 28 | { 29 | z_stream stream; 30 | int err; 31 | 32 | stream.next_in = (z_const Bytef *)source; 33 | stream.avail_in = (uInt)sourceLen; 34 | #ifdef MAXSEG_64K 35 | /* Check for source > 64K on 16-bit machine: */ 36 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 37 | #endif 38 | stream.next_out = dest; 39 | stream.avail_out = (uInt)*destLen; 40 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 41 | 42 | stream.zalloc = (alloc_func)0; 43 | stream.zfree = (free_func)0; 44 | stream.opaque = (voidpf)0; 45 | 46 | err = deflateInit(&stream, level); 47 | if (err != Z_OK) return err; 48 | 49 | err = deflate(&stream, Z_FINISH); 50 | if (err != Z_STREAM_END) { 51 | deflateEnd(&stream); 52 | return err == Z_OK ? Z_BUF_ERROR : err; 53 | } 54 | *destLen = stream.total_out; 55 | 56 | err = deflateEnd(&stream); 57 | return err; 58 | } 59 | 60 | /* =========================================================================== 61 | */ 62 | int ZEXPORT compress (dest, destLen, source, sourceLen) 63 | Bytef *dest; 64 | uLongf *destLen; 65 | const Bytef *source; 66 | uLong sourceLen; 67 | { 68 | return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); 69 | } 70 | 71 | /* =========================================================================== 72 | If the default memLevel or windowBits for deflateInit() is changed, then 73 | this function needs to be updated. 74 | */ 75 | uLong ZEXPORT compressBound (sourceLen) 76 | uLong sourceLen; 77 | { 78 | return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 79 | (sourceLen >> 25) + 13; 80 | } 81 | -------------------------------------------------------------------------------- /3rd/zlib/inftrees.h: -------------------------------------------------------------------------------- 1 | /* inftrees.h -- header to use inftrees.c 2 | * Copyright (C) 1995-2005, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* Structure for decoding tables. Each entry provides either the 12 | information needed to do the operation requested by the code that 13 | indexed that table entry, or it provides a pointer to another 14 | table that indexes more bits of the code. op indicates whether 15 | the entry is a pointer to another table, a literal, a length or 16 | distance, an end-of-block, or an invalid code. For a table 17 | pointer, the low four bits of op is the number of index bits of 18 | that table. For a length or distance, the low four bits of op 19 | is the number of extra bits to get after the code. bits is 20 | the number of bits in this code or part of the code to drop off 21 | of the bit buffer. val is the actual byte to output in the case 22 | of a literal, the base length or distance, or the offset from 23 | the current table to the next table. Each entry is four bytes. */ 24 | typedef struct { 25 | unsigned char op; /* operation, extra bits, table bits */ 26 | unsigned char bits; /* bits in this part of the code */ 27 | unsigned short val; /* offset in table or code value */ 28 | } code; 29 | 30 | /* op values as set by inflate_table(): 31 | 00000000 - literal 32 | 0000tttt - table link, tttt != 0 is the number of table index bits 33 | 0001eeee - length or distance, eeee is the number of extra bits 34 | 01100000 - end of block 35 | 01000000 - invalid code 36 | */ 37 | 38 | /* Maximum size of the dynamic table. The maximum number of code structures is 39 | 1444, which is the sum of 852 for literal/length codes and 592 for distance 40 | codes. These values were found by exhaustive searches using the program 41 | examples/enough.c found in the zlib distribtution. The arguments to that 42 | program are the number of symbols, the initial root table size, and the 43 | maximum bit length of a code. "enough 286 9 15" for literal/length codes 44 | returns returns 852, and "enough 30 6 15" for distance codes returns 592. 45 | The initial root table size (9 or 6) is found in the fifth argument of the 46 | inflate_table() calls in inflate.c and infback.c. If the root table size is 47 | changed, then these maximum sizes would be need to be recalculated and 48 | updated. */ 49 | #define ENOUGH_LENS 852 50 | #define ENOUGH_DISTS 592 51 | #define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS) 52 | 53 | /* Type of code to build for inflate_table() */ 54 | typedef enum { 55 | CODES, 56 | LENS, 57 | DISTS 58 | } codetype; 59 | 60 | int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens, 61 | unsigned codes, code FAR * FAR *table, 62 | unsigned FAR *bits, unsigned short FAR *work)); 63 | -------------------------------------------------------------------------------- /3rd/lua/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.84.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lmem_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lgc.h" 18 | #include "lmem.h" 19 | #include "lobject.h" 20 | #include "lstate.h" 21 | 22 | 23 | 24 | /* 25 | ** About the realloc function: 26 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 27 | ** (`osize' is the old size, `nsize' is the new size) 28 | ** 29 | ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no 30 | ** matter 'x'). 31 | ** 32 | ** * frealloc(ud, p, x, 0) frees the block `p' 33 | ** (in this specific case, frealloc must return NULL); 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 35 | ** (which is equivalent to free(NULL) in ANSI C) 36 | ** 37 | ** frealloc returns NULL if it cannot create or reallocate the area 38 | ** (any reallocation to an equal or smaller size cannot fail!) 39 | */ 40 | 41 | 42 | 43 | #define MINSIZEARRAY 4 44 | 45 | 46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 47 | int limit, const char *what) { 48 | void *newblock; 49 | int newsize; 50 | if (*size >= limit/2) { /* cannot double it? */ 51 | if (*size >= limit) /* cannot grow even a little? */ 52 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 53 | newsize = limit; /* still have at least one free place */ 54 | } 55 | else { 56 | newsize = (*size)*2; 57 | if (newsize < MINSIZEARRAY) 58 | newsize = MINSIZEARRAY; /* minimum size */ 59 | } 60 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 61 | *size = newsize; /* update only when everything else is OK */ 62 | return newblock; 63 | } 64 | 65 | 66 | l_noret luaM_toobig (lua_State *L) { 67 | luaG_runerror(L, "memory allocation error: block too big"); 68 | } 69 | 70 | 71 | 72 | /* 73 | ** generic allocation routine. 74 | */ 75 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 76 | void *newblock; 77 | global_State *g = G(L); 78 | size_t realosize = (block) ? osize : 0; 79 | lua_assert((realosize == 0) == (block == NULL)); 80 | #if defined(HARDMEMTESTS) 81 | if (nsize > realosize && g->gcrunning) 82 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 83 | #endif 84 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 85 | if (newblock == NULL && nsize > 0) { 86 | api_check(L, nsize > realosize, 87 | "realloc cannot fail when shrinking a block"); 88 | if (g->gcrunning) { 89 | luaC_fullgc(L, 1); /* try to free some memory... */ 90 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 91 | } 92 | if (newblock == NULL) 93 | luaD_throw(L, LUA_ERRMEM); 94 | } 95 | lua_assert((nsize == 0) == (newblock == NULL)); 96 | g->GCdebt = (g->GCdebt + nsize) - realosize; 97 | return newblock; 98 | } 99 | 100 | -------------------------------------------------------------------------------- /3rd/lua/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.58.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 28 | OPR_CONCAT, 29 | OPR_EQ, OPR_LT, OPR_LE, 30 | OPR_NE, OPR_GT, OPR_GE, 31 | OPR_AND, OPR_OR, 32 | OPR_NOBINOPR 33 | } BinOpr; 34 | 35 | 36 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 37 | 38 | 39 | #define getcode(fs,e) ((fs)->f->code[(e)->u.info]) 40 | 41 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 42 | 43 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 44 | 45 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 46 | 47 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 48 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 49 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); 50 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 51 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 52 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 53 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 54 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 55 | LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 56 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 57 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 58 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 59 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 60 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 61 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 62 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 63 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 64 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 65 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 66 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 67 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 68 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 69 | LUAI_FUNC int luaK_jump (FuncState *fs); 70 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 71 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 72 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 73 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); 74 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 75 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 76 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 77 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 78 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 79 | expdesc *v2, int line); 80 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 81 | 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /3rd/lua/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define lopcodes_c 9 | #define LUA_CORE 10 | 11 | 12 | #include "lopcodes.h" 13 | 14 | 15 | /* ORDER OP */ 16 | 17 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 18 | "MOVE", 19 | "LOADK", 20 | "LOADKX", 21 | "LOADBOOL", 22 | "LOADNIL", 23 | "GETUPVAL", 24 | "GETTABUP", 25 | "GETTABLE", 26 | "SETTABUP", 27 | "SETUPVAL", 28 | "SETTABLE", 29 | "NEWTABLE", 30 | "SELF", 31 | "ADD", 32 | "SUB", 33 | "MUL", 34 | "DIV", 35 | "MOD", 36 | "POW", 37 | "UNM", 38 | "NOT", 39 | "LEN", 40 | "CONCAT", 41 | "JMP", 42 | "EQ", 43 | "LT", 44 | "LE", 45 | "TEST", 46 | "TESTSET", 47 | "CALL", 48 | "TAILCALL", 49 | "RETURN", 50 | "FORLOOP", 51 | "FORPREP", 52 | "TFORCALL", 53 | "TFORLOOP", 54 | "SETLIST", 55 | "CLOSURE", 56 | "VARARG", 57 | "EXTRAARG", 58 | NULL 59 | }; 60 | 61 | 62 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 63 | 64 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 65 | /* T A B C mode opcode */ 66 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 67 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 68 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 69 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 70 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 71 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 72 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 73 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 74 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 75 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 76 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 77 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 78 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 79 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 80 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 81 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 82 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 83 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 84 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 85 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 86 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 87 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 88 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 89 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 90 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 91 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 92 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 93 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 94 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 95 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 96 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 97 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 98 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 99 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 100 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 101 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 102 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 103 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 104 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 105 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 106 | }; 107 | 108 | -------------------------------------------------------------------------------- /3rd/lua/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.70.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression descriptor 17 | */ 18 | 19 | typedef enum { 20 | VVOID, /* no value */ 21 | VNIL, 22 | VTRUE, 23 | VFALSE, 24 | VK, /* info = index of constant in `k' */ 25 | VKNUM, /* nval = numerical value */ 26 | VNONRELOC, /* info = result register */ 27 | VLOCAL, /* info = local register */ 28 | VUPVAL, /* info = index of upvalue in 'upvalues' */ 29 | VINDEXED, /* t = table register/upvalue; idx = index R/K */ 30 | VJMP, /* info = instruction pc */ 31 | VRELOCABLE, /* info = instruction pc */ 32 | VCALL, /* info = instruction pc */ 33 | VVARARG /* info = instruction pc */ 34 | } expkind; 35 | 36 | 37 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 38 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 39 | 40 | typedef struct expdesc { 41 | expkind k; 42 | union { 43 | struct { /* for indexed variables (VINDEXED) */ 44 | short idx; /* index (R/K) */ 45 | lu_byte t; /* table (register or upvalue) */ 46 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 47 | } ind; 48 | int info; /* for generic use */ 49 | lua_Number nval; /* for VKNUM */ 50 | } u; 51 | int t; /* patch list of `exit when true' */ 52 | int f; /* patch list of `exit when false' */ 53 | } expdesc; 54 | 55 | 56 | /* description of active local variable */ 57 | typedef struct Vardesc { 58 | short idx; /* variable index in stack */ 59 | } Vardesc; 60 | 61 | 62 | /* description of pending goto statements and label statements */ 63 | typedef struct Labeldesc { 64 | TString *name; /* label identifier */ 65 | int pc; /* position in code */ 66 | int line; /* line where it appeared */ 67 | lu_byte nactvar; /* local level where it appears in current block */ 68 | } Labeldesc; 69 | 70 | 71 | /* list of labels or gotos */ 72 | typedef struct Labellist { 73 | Labeldesc *arr; /* array */ 74 | int n; /* number of entries in use */ 75 | int size; /* array size */ 76 | } Labellist; 77 | 78 | 79 | /* dynamic structures used by the parser */ 80 | typedef struct Dyndata { 81 | struct { /* list of active local variables */ 82 | Vardesc *arr; 83 | int n; 84 | int size; 85 | } actvar; 86 | Labellist gt; /* list of pending gotos */ 87 | Labellist label; /* list of active labels */ 88 | } Dyndata; 89 | 90 | 91 | /* control of blocks */ 92 | struct BlockCnt; /* defined in lparser.c */ 93 | 94 | 95 | /* state needed to generate code for a given function */ 96 | typedef struct FuncState { 97 | Proto *f; /* current function header */ 98 | Table *h; /* table to find (and reuse) elements in `k' */ 99 | struct FuncState *prev; /* enclosing function */ 100 | struct LexState *ls; /* lexical state */ 101 | struct BlockCnt *bl; /* chain of current blocks */ 102 | int pc; /* next position to code (equivalent to `ncode') */ 103 | int lasttarget; /* 'label' of last 'jump label' */ 104 | int jpc; /* list of pending jumps to `pc' */ 105 | int nk; /* number of elements in `k' */ 106 | int np; /* number of elements in `p' */ 107 | int firstlocal; /* index of first local var (in Dyndata array) */ 108 | short nlocvars; /* number of elements in 'f->locvars' */ 109 | lu_byte nactvar; /* number of active local variables */ 110 | lu_byte nups; /* number of upvalues */ 111 | lu_byte freereg; /* first free register */ 112 | } FuncState; 113 | 114 | 115 | LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 116 | Dyndata *dyd, const char *name, int firstchar); 117 | 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /3rd/lua/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.17.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define ldump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "lobject.h" 15 | #include "lstate.h" 16 | #include "lundump.h" 17 | 18 | typedef struct { 19 | lua_State* L; 20 | lua_Writer writer; 21 | void* data; 22 | int strip; 23 | int status; 24 | } DumpState; 25 | 26 | #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) 27 | #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) 28 | 29 | static void DumpBlock(const void* b, size_t size, DumpState* D) 30 | { 31 | if (D->status==0) 32 | { 33 | lua_unlock(D->L); 34 | D->status=(*D->writer)(D->L,b,size,D->data); 35 | lua_lock(D->L); 36 | } 37 | } 38 | 39 | static void DumpChar(int y, DumpState* D) 40 | { 41 | char x=(char)y; 42 | DumpVar(x,D); 43 | } 44 | 45 | static void DumpInt(int x, DumpState* D) 46 | { 47 | DumpVar(x,D); 48 | } 49 | 50 | static void DumpNumber(lua_Number x, DumpState* D) 51 | { 52 | DumpVar(x,D); 53 | } 54 | 55 | static void DumpVector(const void* b, int n, size_t size, DumpState* D) 56 | { 57 | DumpInt(n,D); 58 | DumpMem(b,n,size,D); 59 | } 60 | 61 | static void DumpString(const TString* s, DumpState* D) 62 | { 63 | if (s==NULL) 64 | { 65 | size_t size=0; 66 | DumpVar(size,D); 67 | } 68 | else 69 | { 70 | size_t size=s->tsv.len+1; /* include trailing '\0' */ 71 | DumpVar(size,D); 72 | DumpBlock(getstr(s),size*sizeof(char),D); 73 | } 74 | } 75 | 76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) 77 | 78 | static void DumpFunction(const Proto* f, DumpState* D); 79 | 80 | static void DumpConstants(const Proto* f, DumpState* D) 81 | { 82 | int i,n=f->sizek; 83 | DumpInt(n,D); 84 | for (i=0; ik[i]; 87 | DumpChar(ttypenv(o),D); 88 | switch (ttypenv(o)) 89 | { 90 | case LUA_TNIL: 91 | break; 92 | case LUA_TBOOLEAN: 93 | DumpChar(bvalue(o),D); 94 | break; 95 | case LUA_TNUMBER: 96 | DumpNumber(nvalue(o),D); 97 | break; 98 | case LUA_TSTRING: 99 | DumpString(rawtsvalue(o),D); 100 | break; 101 | default: lua_assert(0); 102 | } 103 | } 104 | n=f->sizep; 105 | DumpInt(n,D); 106 | for (i=0; ip[i],D); 107 | } 108 | 109 | static void DumpUpvalues(const Proto* f, DumpState* D) 110 | { 111 | int i,n=f->sizeupvalues; 112 | DumpInt(n,D); 113 | for (i=0; iupvalues[i].instack,D); 116 | DumpChar(f->upvalues[i].idx,D); 117 | } 118 | } 119 | 120 | static void DumpDebug(const Proto* f, DumpState* D) 121 | { 122 | int i,n; 123 | DumpString((D->strip) ? NULL : f->source,D); 124 | n= (D->strip) ? 0 : f->sizelineinfo; 125 | DumpVector(f->lineinfo,n,sizeof(int),D); 126 | n= (D->strip) ? 0 : f->sizelocvars; 127 | DumpInt(n,D); 128 | for (i=0; ilocvars[i].varname,D); 131 | DumpInt(f->locvars[i].startpc,D); 132 | DumpInt(f->locvars[i].endpc,D); 133 | } 134 | n= (D->strip) ? 0 : f->sizeupvalues; 135 | DumpInt(n,D); 136 | for (i=0; iupvalues[i].name,D); 137 | } 138 | 139 | static void DumpFunction(const Proto* f, DumpState* D) 140 | { 141 | DumpInt(f->linedefined,D); 142 | DumpInt(f->lastlinedefined,D); 143 | DumpChar(f->numparams,D); 144 | DumpChar(f->is_vararg,D); 145 | DumpChar(f->maxstacksize,D); 146 | DumpCode(f,D); 147 | DumpConstants(f,D); 148 | DumpUpvalues(f,D); 149 | DumpDebug(f,D); 150 | } 151 | 152 | static void DumpHeader(DumpState* D) 153 | { 154 | lu_byte h[LUAC_HEADERSIZE]; 155 | luaU_header(h); 156 | DumpBlock(h,LUAC_HEADERSIZE,D); 157 | } 158 | 159 | /* 160 | ** dump Lua function as precompiled chunk 161 | */ 162 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 163 | { 164 | DumpState D; 165 | D.L=L; 166 | D.writer=w; 167 | D.data=data; 168 | D.strip=strip; 169 | D.status=0; 170 | DumpHeader(&D); 171 | DumpFunction(f,&D); 172 | return D.status; 173 | } 174 | -------------------------------------------------------------------------------- /3rd/lua/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.5.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | 11 | #define lcorolib_c 12 | #define LUA_LIB 13 | 14 | #include "lua.h" 15 | 16 | #include "lauxlib.h" 17 | #include "lualib.h" 18 | 19 | 20 | static int auxresume (lua_State *L, lua_State *co, int narg) { 21 | int status; 22 | if (!lua_checkstack(co, narg)) { 23 | lua_pushliteral(L, "too many arguments to resume"); 24 | return -1; /* error flag */ 25 | } 26 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { 27 | lua_pushliteral(L, "cannot resume dead coroutine"); 28 | return -1; /* error flag */ 29 | } 30 | lua_xmove(L, co, narg); 31 | status = lua_resume(co, L, narg); 32 | if (status == LUA_OK || status == LUA_YIELD) { 33 | int nres = lua_gettop(co); 34 | if (!lua_checkstack(L, nres + 1)) { 35 | lua_pop(co, nres); /* remove results anyway */ 36 | lua_pushliteral(L, "too many results to resume"); 37 | return -1; /* error flag */ 38 | } 39 | lua_xmove(co, L, nres); /* move yielded values */ 40 | return nres; 41 | } 42 | else { 43 | lua_xmove(co, L, 1); /* move error message */ 44 | return -1; /* error flag */ 45 | } 46 | } 47 | 48 | 49 | static int luaB_coresume (lua_State *L) { 50 | lua_State *co = lua_tothread(L, 1); 51 | int r; 52 | luaL_argcheck(L, co, 1, "coroutine expected"); 53 | r = auxresume(L, co, lua_gettop(L) - 1); 54 | if (r < 0) { 55 | lua_pushboolean(L, 0); 56 | lua_insert(L, -2); 57 | return 2; /* return false + error message */ 58 | } 59 | else { 60 | lua_pushboolean(L, 1); 61 | lua_insert(L, -(r + 1)); 62 | return r + 1; /* return true + `resume' returns */ 63 | } 64 | } 65 | 66 | 67 | static int luaB_auxwrap (lua_State *L) { 68 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 69 | int r = auxresume(L, co, lua_gettop(L)); 70 | if (r < 0) { 71 | if (lua_isstring(L, -1)) { /* error object is a string? */ 72 | luaL_where(L, 1); /* add extra info */ 73 | lua_insert(L, -2); 74 | lua_concat(L, 2); 75 | } 76 | return lua_error(L); /* propagate error */ 77 | } 78 | return r; 79 | } 80 | 81 | 82 | static int luaB_cocreate (lua_State *L) { 83 | lua_State *NL; 84 | luaL_checktype(L, 1, LUA_TFUNCTION); 85 | NL = lua_newthread(L); 86 | lua_pushvalue(L, 1); /* move function to top */ 87 | lua_xmove(L, NL, 1); /* move function from L to NL */ 88 | return 1; 89 | } 90 | 91 | 92 | static int luaB_cowrap (lua_State *L) { 93 | luaB_cocreate(L); 94 | lua_pushcclosure(L, luaB_auxwrap, 1); 95 | return 1; 96 | } 97 | 98 | 99 | static int luaB_yield (lua_State *L) { 100 | return lua_yield(L, lua_gettop(L)); 101 | } 102 | 103 | 104 | static int luaB_costatus (lua_State *L) { 105 | lua_State *co = lua_tothread(L, 1); 106 | luaL_argcheck(L, co, 1, "coroutine expected"); 107 | if (L == co) lua_pushliteral(L, "running"); 108 | else { 109 | switch (lua_status(co)) { 110 | case LUA_YIELD: 111 | lua_pushliteral(L, "suspended"); 112 | break; 113 | case LUA_OK: { 114 | lua_Debug ar; 115 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 116 | lua_pushliteral(L, "normal"); /* it is running */ 117 | else if (lua_gettop(co) == 0) 118 | lua_pushliteral(L, "dead"); 119 | else 120 | lua_pushliteral(L, "suspended"); /* initial state */ 121 | break; 122 | } 123 | default: /* some error occurred */ 124 | lua_pushliteral(L, "dead"); 125 | break; 126 | } 127 | } 128 | return 1; 129 | } 130 | 131 | 132 | static int luaB_corunning (lua_State *L) { 133 | int ismain = lua_pushthread(L); 134 | lua_pushboolean(L, ismain); 135 | return 2; 136 | } 137 | 138 | 139 | static const luaL_Reg co_funcs[] = { 140 | {"create", luaB_cocreate}, 141 | {"resume", luaB_coresume}, 142 | {"running", luaB_corunning}, 143 | {"status", luaB_costatus}, 144 | {"wrap", luaB_cowrap}, 145 | {"yield", luaB_yield}, 146 | {NULL, NULL} 147 | }; 148 | 149 | 150 | 151 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 152 | luaL_newlib(L, co_funcs); 153 | return 1; 154 | } 155 | 156 | -------------------------------------------------------------------------------- /src/img_png.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "png.h" 6 | 7 | #include "img_png.h" 8 | 9 | #define PNG_BYTES_TO_CHECK 4 10 | 11 | int 12 | img_loadpng(const char* filename, struct png* png) { 13 | // open file 14 | FILE* fp = fopen(filename, "rb"); 15 | if (!fp) { return 0; } 16 | 17 | // init libpng 18 | png_structp png_ptr; 19 | png_infop info_ptr; 20 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); 21 | info_ptr = png_create_info_struct(png_ptr); 22 | setjmp(png_jmpbuf(png_ptr)); 23 | 24 | // check png legality 25 | int temp; 26 | char temp2[PNG_BYTES_TO_CHECK]; 27 | 28 | temp = fread(temp2, 1, PNG_BYTES_TO_CHECK, fp); 29 | if (temp < PNG_BYTES_TO_CHECK) { 30 | fclose(fp); 31 | png_destroy_read_struct(&png_ptr, &info_ptr, 0); 32 | return 0; 33 | } 34 | 35 | temp = png_sig_cmp((png_bytep)temp2, (png_size_t)0, PNG_BYTES_TO_CHECK); 36 | if (temp != 0) { 37 | fclose(fp); 38 | png_destroy_read_struct(&png_ptr, &info_ptr, 0); 39 | return 0; 40 | } 41 | 42 | // read png data 43 | rewind(fp); 44 | png_init_io(png_ptr, fp); 45 | png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_EXPAND, 0); 46 | 47 | // get image info 48 | int w = png_get_image_width(png_ptr, info_ptr); 49 | int h = png_get_image_height(png_ptr, info_ptr); 50 | int color_type = png_get_color_type(png_ptr, info_ptr); 51 | 52 | png_bytep* row_pointers = png_get_rows(png_ptr, info_ptr); 53 | 54 | switch (color_type) { 55 | case PNG_COLOR_TYPE_RGB_ALPHA: 56 | png->type = PNG_RGBA; 57 | png->channel = 4; 58 | png->buffer = (uint8_t*)malloc(w*h * 4); 59 | for (int i = 0; i < h; ++i) { 60 | for (int j = 0; j < w; ++j) { 61 | int k = (i*w + j) * 4; 62 | uint8_t alpha = row_pointers[i][j * 4 + 3]; 63 | png->buffer[k + 0] = row_pointers[i][j * 4 + 0] * alpha / 255; // red 64 | png->buffer[k + 1] = row_pointers[i][j * 4 + 1] * alpha / 255; // green 65 | png->buffer[k + 2] = row_pointers[i][j * 4 + 2] * alpha / 255; // blue 66 | png->buffer[k + 3] = alpha; // alpha 67 | } 68 | } 69 | break; 70 | 71 | case PNG_COLOR_TYPE_RGB: 72 | png->type = PNG_RGB; 73 | png->channel = 3; 74 | png->buffer = (uint8_t*)malloc(w*h * 3); 75 | for (int i = 0; i < h; ++i) { 76 | for (int j = 0; j < w; ++j) { 77 | int k = (i*w + j) * 3; 78 | png->buffer[k + 0] = row_pointers[i][j * 3 + 0]; // red 79 | png->buffer[k + 1] = row_pointers[i][j * 3 + 1]; // green 80 | png->buffer[k + 2] = row_pointers[i][j * 3 + 2]; // blue 81 | } 82 | } 83 | break; 84 | 85 | default: 86 | fclose(fp); 87 | png_destroy_read_struct(&png_ptr, &info_ptr, 0); 88 | return 0; 89 | } 90 | 91 | fclose(fp); 92 | png_destroy_read_struct(&png_ptr, &info_ptr, 0); 93 | 94 | png->width = w; 95 | png->height = h; 96 | return 1; 97 | } 98 | 99 | int 100 | img_savepng(const char* filename, int format, int width, int height, uint8_t* buffer) { 101 | // open file 102 | char* tmp = malloc(sizeof(char) * (strlen(filename) + 5)); 103 | sprintf(tmp, "%s.png", filename); 104 | 105 | FILE* fp = fopen(tmp, "wb"); 106 | if (!fp) { return 0; } 107 | 108 | // init libpng 109 | png_structp png_ptr; 110 | png_infop info_ptr; 111 | png_colorp palette; 112 | 113 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); 114 | info_ptr = png_create_info_struct(png_ptr); 115 | setjmp(png_jmpbuf(png_ptr)); 116 | 117 | png_init_io(png_ptr, fp); 118 | 119 | int color_type; 120 | int bytes_per_pixel; 121 | switch (format) { 122 | case PNG_RGBA: 123 | color_type = PNG_COLOR_TYPE_RGB_ALPHA; 124 | bytes_per_pixel = 4; 125 | break; 126 | case PNG_RGB: 127 | color_type = PNG_COLOR_TYPE_RGB; 128 | bytes_per_pixel = 3; 129 | break; 130 | default: 131 | fclose(fp); 132 | png_destroy_write_struct(&png_ptr, &info_ptr); 133 | return 0; 134 | } 135 | 136 | // write png data 137 | png_set_IHDR(png_ptr, info_ptr, width, height, 8, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); 138 | png_write_info(png_ptr, info_ptr); 139 | png_bytep row_pointers[height]; 140 | for (int i = 0; i < height; ++i) 141 | row_pointers[i] = buffer + i * width * bytes_per_pixel; 142 | png_write_image(png_ptr, row_pointers); 143 | png_write_end(png_ptr, info_ptr); 144 | 145 | fclose(fp); 146 | png_destroy_write_struct(&png_ptr, &info_ptr); 147 | 148 | return 1; 149 | } 150 | -------------------------------------------------------------------------------- /src/scripts/binpacking.lua: -------------------------------------------------------------------------------- 1 | -- container for rects 2 | local bin_mt = {} 3 | bin_mt.__index = bin_mt 4 | 5 | local function _debug(...) 6 | -- print("[BPDEBUG] "..string.format(...)) 7 | end 8 | 9 | function bin_mt:_find_node(w, h) 10 | local node = {} -- result 11 | local best_ssf = false 12 | local best_lsf = false 13 | 14 | for _,v in ipairs(self.free) do 15 | if v.w >= w and v.h >= h then 16 | local dw = math.abs(v.w - w) 17 | local dh = math.abs(v.h - h) 18 | local ssf = math.min(dw, dh) -- short side fit 19 | local lsf = math.max(dw, dh) -- long side fit 20 | 21 | if not best_ssf or 22 | ssf < best_ssf or 23 | (ssf == best_ssf and lsf < best_lsf) then 24 | node.x = v.x -- record the best node 25 | node.y = v.y 26 | node.w = w 27 | node.h = h 28 | best_ssf = ssf 29 | best_lsf = lsf 30 | end 31 | end 32 | end 33 | 34 | return node 35 | end 36 | 37 | function bin_mt:_free_contain(node, skip) 38 | for _,v in ipairs(self.free) do 39 | if v ~= skip then -- our node is splited from this freenode, skip it 40 | if v.x <= node.x and 41 | v.x + v.w >= node.x + node.w and 42 | v.y <= node.y and 43 | v.y + v.h >= node.y + node.h then 44 | return true 45 | end 46 | end 47 | end 48 | 49 | return false 50 | end 51 | 52 | function bin_mt:_split_free(freenode, node, splits) 53 | _debug("split free node(%d,%d,%d,%d)", freenode.x, freenode.y, freenode.w, freenode.h) 54 | 55 | if node.x > freenode.x + freenode.w or 56 | node.x + node.w <= freenode.x or 57 | node.y > freenode.y + freenode.h or 58 | node.y + node.h <= freenode.y then 59 | _debug("\tno intersects") 60 | return false 61 | end 62 | 63 | if node.y > freenode.y then -- try top 64 | local n = {} 65 | n.x = freenode.x 66 | n.y = freenode.y 67 | n.w = freenode.w 68 | n.h = node.y - freenode.y 69 | if not self:_free_contain(n, freenode) then 70 | table.insert(splits, n) 71 | _debug("\ttop(%d,%d,%d,%d) splited", n.x, n.y, n.w, n.h) 72 | else 73 | _debug("\ttop area is covered by other freenode") 74 | end 75 | end 76 | 77 | if node.y + node.h < freenode.y + freenode.h then -- try bottom 78 | local n = {} 79 | n.x = freenode.x 80 | n.y = node.y + node.h 81 | n.w = freenode.w 82 | n.h = (freenode.y + freenode.h) - (node.y + node.h) 83 | if not self:_free_contain(n, freenode) then 84 | table.insert(splits, n) 85 | _debug("\tbottom(%d,%d,%d,%d) splited", n.x, n.y, n.w, n.h) 86 | else 87 | _debug("\tbottom area is covered by other freenode") 88 | end 89 | end 90 | 91 | if node.x > freenode.x then -- try left 92 | local n = {} 93 | n.x = freenode.x 94 | n.y = freenode.y 95 | n.w = node.x - freenode.x 96 | n.h = freenode.h 97 | if not self:_free_contain(n, freenode) then 98 | table.insert(splits, n) 99 | _debug("\tleft(%d,%d,%d,%d) splited", n.x, n.y, n.w, n.h) 100 | else 101 | _debug("\tleft area is covered by other freenode") 102 | end 103 | end 104 | 105 | if node.x + node.w < freenode.x + freenode.w then -- try right 106 | local n = {} 107 | n.x = node.x + node.w 108 | n.y = freenode.y 109 | n.w = (freenode.x + freenode.w) - (node.x + node.w) 110 | n.h = freenode.h 111 | if not self:_free_contain(n, freenode) then 112 | table.insert(splits, n) 113 | _debug("\tright(%d,%d,%d,%d) splited", n.x, n.y, n.w, n.h) 114 | else 115 | _debug("\tright area is covered by other freenode") 116 | end 117 | end 118 | 119 | return true 120 | end 121 | 122 | function bin_mt:insert(w, h) 123 | _debug("***** insert new rect(%d,%d) *****", w, h) 124 | 125 | local node = self:_find_node(w, h) 126 | if not node.h then return end -- not found 127 | _debug("found new node(%d,%d,%d,%d)", node.x, node.y, node.w, node.h) 128 | 129 | local splits = {} 130 | local i = 1 131 | while i <= #self.free do -- split free node into small pieces 132 | if self:_split_free(self.free[i], node, splits) then 133 | table.remove(self.free, i) 134 | else 135 | i = i + 1 136 | end 137 | end 138 | 139 | for i,v in ipairs(splits) do 140 | table.insert(self.free, v) 141 | end 142 | 143 | return node 144 | end 145 | 146 | -- binpacking algorithm module 147 | local M = {} 148 | 149 | function M:new_bin(w, h) 150 | local node = {x=0, y=0, w=w, h=h} 151 | local bin = {} 152 | bin.free = {node} -- free node list 153 | return setmetatable(bin, bin_mt) 154 | end 155 | 156 | return M -------------------------------------------------------------------------------- /3rd/libpng/pngrio.c: -------------------------------------------------------------------------------- 1 | 2 | /* pngrio.c - functions for data input 3 | * 4 | * Last changed in libpng 1.6.9 [February 6, 2014] 5 | * Copyright (c) 1998-2014 Glenn Randers-Pehrson 6 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 7 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 8 | * 9 | * This code is released under the libpng license. 10 | * For conditions of distribution and use, see the disclaimer 11 | * and license in png.h 12 | * 13 | * This file provides a location for all input. Users who need 14 | * special handling are expected to write a function that has the same 15 | * arguments as this and performs a similar function, but that possibly 16 | * has a different input method. Note that you shouldn't change this 17 | * function, but rather write a replacement function and then make 18 | * libpng use it at run time with png_set_read_fn(...). 19 | */ 20 | 21 | #include "pngpriv.h" 22 | 23 | #ifdef PNG_READ_SUPPORTED 24 | 25 | /* Read the data from whatever input you are using. The default routine 26 | * reads from a file pointer. Note that this routine sometimes gets called 27 | * with very small lengths, so you should implement some kind of simple 28 | * buffering if you are using unbuffered reads. This should never be asked 29 | * to read more then 64K on a 16 bit machine. 30 | */ 31 | void /* PRIVATE */ 32 | png_read_data(png_structrp png_ptr, png_bytep data, png_size_t length) 33 | { 34 | png_debug1(4, "reading %d bytes", (int)length); 35 | 36 | if (png_ptr->read_data_fn != NULL) 37 | (*(png_ptr->read_data_fn))(png_ptr, data, length); 38 | 39 | else 40 | png_error(png_ptr, "Call to NULL read function"); 41 | } 42 | 43 | #ifdef PNG_STDIO_SUPPORTED 44 | /* This is the function that does the actual reading of data. If you are 45 | * not reading from a standard C stream, you should create a replacement 46 | * read_data function and use it at run time with png_set_read_fn(), rather 47 | * than changing the library. 48 | */ 49 | void PNGCBAPI 50 | png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) 51 | { 52 | png_size_t check; 53 | 54 | if (png_ptr == NULL) 55 | return; 56 | 57 | /* fread() returns 0 on error, so it is OK to store this in a png_size_t 58 | * instead of an int, which is what fread() actually returns. 59 | */ 60 | check = fread(data, 1, length, png_voidcast(png_FILE_p, png_ptr->io_ptr)); 61 | 62 | if (check != length) 63 | png_error(png_ptr, "Read Error"); 64 | } 65 | #endif 66 | 67 | /* This function allows the application to supply a new input function 68 | * for libpng if standard C streams aren't being used. 69 | * 70 | * This function takes as its arguments: 71 | * 72 | * png_ptr - pointer to a png input data structure 73 | * 74 | * io_ptr - pointer to user supplied structure containing info about 75 | * the input functions. May be NULL. 76 | * 77 | * read_data_fn - pointer to a new input function that takes as its 78 | * arguments a pointer to a png_struct, a pointer to 79 | * a location where input data can be stored, and a 32-bit 80 | * unsigned int that is the number of bytes to be read. 81 | * To exit and output any fatal error messages the new write 82 | * function should call png_error(png_ptr, "Error msg"). 83 | * May be NULL, in which case libpng's default function will 84 | * be used. 85 | */ 86 | void PNGAPI 87 | png_set_read_fn(png_structrp png_ptr, png_voidp io_ptr, 88 | png_rw_ptr read_data_fn) 89 | { 90 | if (png_ptr == NULL) 91 | return; 92 | 93 | png_ptr->io_ptr = io_ptr; 94 | 95 | #ifdef PNG_STDIO_SUPPORTED 96 | if (read_data_fn != NULL) 97 | png_ptr->read_data_fn = read_data_fn; 98 | 99 | else 100 | png_ptr->read_data_fn = png_default_read_data; 101 | #else 102 | png_ptr->read_data_fn = read_data_fn; 103 | #endif 104 | 105 | #ifdef PNG_WRITE_SUPPORTED 106 | /* It is an error to write to a read device */ 107 | if (png_ptr->write_data_fn != NULL) 108 | { 109 | png_ptr->write_data_fn = NULL; 110 | png_warning(png_ptr, 111 | "Can't set both read_data_fn and write_data_fn in the" 112 | " same structure"); 113 | } 114 | #endif 115 | 116 | #ifdef PNG_WRITE_FLUSH_SUPPORTED 117 | png_ptr->output_flush_fn = NULL; 118 | #endif 119 | } 120 | #endif /* PNG_READ_SUPPORTED */ 121 | -------------------------------------------------------------------------------- /3rd/lua/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.30.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lfunc_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lfunc.h" 16 | #include "lgc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | Closure *luaF_newCclosure (lua_State *L, int n) { 24 | Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl; 25 | c->c.nupvalues = cast_byte(n); 26 | return c; 27 | } 28 | 29 | 30 | Closure *luaF_newLclosure (lua_State *L, int n) { 31 | Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl; 32 | c->l.p = NULL; 33 | c->l.nupvalues = cast_byte(n); 34 | while (n--) c->l.upvals[n] = NULL; 35 | return c; 36 | } 37 | 38 | 39 | UpVal *luaF_newupval (lua_State *L) { 40 | UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv; 41 | uv->v = &uv->u.value; 42 | setnilvalue(uv->v); 43 | return uv; 44 | } 45 | 46 | 47 | UpVal *luaF_findupval (lua_State *L, StkId level) { 48 | global_State *g = G(L); 49 | GCObject **pp = &L->openupval; 50 | UpVal *p; 51 | UpVal *uv; 52 | while (*pp != NULL && (p = gco2uv(*pp))->v >= level) { 53 | GCObject *o = obj2gco(p); 54 | lua_assert(p->v != &p->u.value); 55 | lua_assert(!isold(o) || isold(obj2gco(L))); 56 | if (p->v == level) { /* found a corresponding upvalue? */ 57 | if (isdead(g, o)) /* is it dead? */ 58 | changewhite(o); /* resurrect it */ 59 | return p; 60 | } 61 | pp = &p->next; 62 | } 63 | /* not found: create a new one */ 64 | uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv; 65 | uv->v = level; /* current value lives in the stack */ 66 | uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 67 | uv->u.l.next = g->uvhead.u.l.next; 68 | uv->u.l.next->u.l.prev = uv; 69 | g->uvhead.u.l.next = uv; 70 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 71 | return uv; 72 | } 73 | 74 | 75 | static void unlinkupval (UpVal *uv) { 76 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 77 | uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ 78 | uv->u.l.prev->u.l.next = uv->u.l.next; 79 | } 80 | 81 | 82 | void luaF_freeupval (lua_State *L, UpVal *uv) { 83 | if (uv->v != &uv->u.value) /* is it open? */ 84 | unlinkupval(uv); /* remove from open list */ 85 | luaM_free(L, uv); /* free upvalue */ 86 | } 87 | 88 | 89 | void luaF_close (lua_State *L, StkId level) { 90 | UpVal *uv; 91 | global_State *g = G(L); 92 | while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) { 93 | GCObject *o = obj2gco(uv); 94 | lua_assert(!isblack(o) && uv->v != &uv->u.value); 95 | L->openupval = uv->next; /* remove from `open' list */ 96 | if (isdead(g, o)) 97 | luaF_freeupval(L, uv); /* free upvalue */ 98 | else { 99 | unlinkupval(uv); /* remove upvalue from 'uvhead' list */ 100 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ 101 | uv->v = &uv->u.value; /* now current value lives here */ 102 | gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */ 103 | g->allgc = o; 104 | luaC_checkupvalcolor(g, uv); 105 | } 106 | } 107 | } 108 | 109 | 110 | Proto *luaF_newproto (lua_State *L) { 111 | Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p; 112 | f->k = NULL; 113 | f->sizek = 0; 114 | f->p = NULL; 115 | f->sizep = 0; 116 | f->code = NULL; 117 | f->cache = NULL; 118 | f->sizecode = 0; 119 | f->lineinfo = NULL; 120 | f->sizelineinfo = 0; 121 | f->upvalues = NULL; 122 | f->sizeupvalues = 0; 123 | f->numparams = 0; 124 | f->is_vararg = 0; 125 | f->maxstacksize = 0; 126 | f->locvars = NULL; 127 | f->sizelocvars = 0; 128 | f->linedefined = 0; 129 | f->lastlinedefined = 0; 130 | f->source = NULL; 131 | return f; 132 | } 133 | 134 | 135 | void luaF_freeproto (lua_State *L, Proto *f) { 136 | luaM_freearray(L, f->code, f->sizecode); 137 | luaM_freearray(L, f->p, f->sizep); 138 | luaM_freearray(L, f->k, f->sizek); 139 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); 140 | luaM_freearray(L, f->locvars, f->sizelocvars); 141 | luaM_freearray(L, f->upvalues, f->sizeupvalues); 142 | luaM_free(L, f); 143 | } 144 | 145 | 146 | /* 147 | ** Look for n-th local variable at line `line' in function `func'. 148 | ** Returns NULL if not found. 149 | */ 150 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 151 | int i; 152 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 153 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 154 | local_number--; 155 | if (local_number == 0) 156 | return getstr(f->locvars[i].varname); 157 | } 158 | } 159 | return NULL; /* not found */ 160 | } 161 | 162 | -------------------------------------------------------------------------------- /3rd/lua/lbitlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lbitlib.c,v 1.18.1.2 2013/07/09 18:01:41 roberto Exp $ 3 | ** Standard library for bitwise operations 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lbitlib_c 8 | #define LUA_LIB 9 | 10 | #include "lua.h" 11 | 12 | #include "lauxlib.h" 13 | #include "lualib.h" 14 | 15 | 16 | /* number of bits to consider in a number */ 17 | #if !defined(LUA_NBITS) 18 | #define LUA_NBITS 32 19 | #endif 20 | 21 | 22 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) 23 | 24 | /* macro to trim extra bits */ 25 | #define trim(x) ((x) & ALLONES) 26 | 27 | 28 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ 29 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) 30 | 31 | 32 | typedef lua_Unsigned b_uint; 33 | 34 | 35 | 36 | static b_uint andaux (lua_State *L) { 37 | int i, n = lua_gettop(L); 38 | b_uint r = ~(b_uint)0; 39 | for (i = 1; i <= n; i++) 40 | r &= luaL_checkunsigned(L, i); 41 | return trim(r); 42 | } 43 | 44 | 45 | static int b_and (lua_State *L) { 46 | b_uint r = andaux(L); 47 | lua_pushunsigned(L, r); 48 | return 1; 49 | } 50 | 51 | 52 | static int b_test (lua_State *L) { 53 | b_uint r = andaux(L); 54 | lua_pushboolean(L, r != 0); 55 | return 1; 56 | } 57 | 58 | 59 | static int b_or (lua_State *L) { 60 | int i, n = lua_gettop(L); 61 | b_uint r = 0; 62 | for (i = 1; i <= n; i++) 63 | r |= luaL_checkunsigned(L, i); 64 | lua_pushunsigned(L, trim(r)); 65 | return 1; 66 | } 67 | 68 | 69 | static int b_xor (lua_State *L) { 70 | int i, n = lua_gettop(L); 71 | b_uint r = 0; 72 | for (i = 1; i <= n; i++) 73 | r ^= luaL_checkunsigned(L, i); 74 | lua_pushunsigned(L, trim(r)); 75 | return 1; 76 | } 77 | 78 | 79 | static int b_not (lua_State *L) { 80 | b_uint r = ~luaL_checkunsigned(L, 1); 81 | lua_pushunsigned(L, trim(r)); 82 | return 1; 83 | } 84 | 85 | 86 | static int b_shift (lua_State *L, b_uint r, int i) { 87 | if (i < 0) { /* shift right? */ 88 | i = -i; 89 | r = trim(r); 90 | if (i >= LUA_NBITS) r = 0; 91 | else r >>= i; 92 | } 93 | else { /* shift left */ 94 | if (i >= LUA_NBITS) r = 0; 95 | else r <<= i; 96 | r = trim(r); 97 | } 98 | lua_pushunsigned(L, r); 99 | return 1; 100 | } 101 | 102 | 103 | static int b_lshift (lua_State *L) { 104 | return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2)); 105 | } 106 | 107 | 108 | static int b_rshift (lua_State *L) { 109 | return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2)); 110 | } 111 | 112 | 113 | static int b_arshift (lua_State *L) { 114 | b_uint r = luaL_checkunsigned(L, 1); 115 | int i = luaL_checkint(L, 2); 116 | if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1)))) 117 | return b_shift(L, r, -i); 118 | else { /* arithmetic shift for 'negative' number */ 119 | if (i >= LUA_NBITS) r = ALLONES; 120 | else 121 | r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */ 122 | lua_pushunsigned(L, r); 123 | return 1; 124 | } 125 | } 126 | 127 | 128 | static int b_rot (lua_State *L, int i) { 129 | b_uint r = luaL_checkunsigned(L, 1); 130 | i &= (LUA_NBITS - 1); /* i = i % NBITS */ 131 | r = trim(r); 132 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ 133 | r = (r << i) | (r >> (LUA_NBITS - i)); 134 | lua_pushunsigned(L, trim(r)); 135 | return 1; 136 | } 137 | 138 | 139 | static int b_lrot (lua_State *L) { 140 | return b_rot(L, luaL_checkint(L, 2)); 141 | } 142 | 143 | 144 | static int b_rrot (lua_State *L) { 145 | return b_rot(L, -luaL_checkint(L, 2)); 146 | } 147 | 148 | 149 | /* 150 | ** get field and width arguments for field-manipulation functions, 151 | ** checking whether they are valid. 152 | ** ('luaL_error' called without 'return' to avoid later warnings about 153 | ** 'width' being used uninitialized.) 154 | */ 155 | static int fieldargs (lua_State *L, int farg, int *width) { 156 | int f = luaL_checkint(L, farg); 157 | int w = luaL_optint(L, farg + 1, 1); 158 | luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); 159 | luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); 160 | if (f + w > LUA_NBITS) 161 | luaL_error(L, "trying to access non-existent bits"); 162 | *width = w; 163 | return f; 164 | } 165 | 166 | 167 | static int b_extract (lua_State *L) { 168 | int w; 169 | b_uint r = luaL_checkunsigned(L, 1); 170 | int f = fieldargs(L, 2, &w); 171 | r = (r >> f) & mask(w); 172 | lua_pushunsigned(L, r); 173 | return 1; 174 | } 175 | 176 | 177 | static int b_replace (lua_State *L) { 178 | int w; 179 | b_uint r = luaL_checkunsigned(L, 1); 180 | b_uint v = luaL_checkunsigned(L, 2); 181 | int f = fieldargs(L, 3, &w); 182 | int m = mask(w); 183 | v &= m; /* erase bits outside given width */ 184 | r = (r & ~(m << f)) | (v << f); 185 | lua_pushunsigned(L, r); 186 | return 1; 187 | } 188 | 189 | 190 | static const luaL_Reg bitlib[] = { 191 | {"arshift", b_arshift}, 192 | {"band", b_and}, 193 | {"bnot", b_not}, 194 | {"bor", b_or}, 195 | {"bxor", b_xor}, 196 | {"btest", b_test}, 197 | {"extract", b_extract}, 198 | {"lrotate", b_lrot}, 199 | {"lshift", b_lshift}, 200 | {"replace", b_replace}, 201 | {"rrotate", b_rrot}, 202 | {"rshift", b_rshift}, 203 | {NULL, NULL} 204 | }; 205 | 206 | 207 | 208 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 209 | luaL_newlib(L, bitlib); 210 | return 1; 211 | } 212 | 213 | -------------------------------------------------------------------------------- /src/scripts/ejresource.lua: -------------------------------------------------------------------------------- 1 | local libimage = require "libimage" 2 | local libos = require "libos" 3 | local binpacking = require "binpacking" 4 | local utils = require "utils" 5 | 6 | -- file templates 7 | local TEMPLATE_IMAGE = [[ 8 | { name="%s", pos={%d,%d}, size={%d,%d}, offset={%d,%d} }, 9 | ]] 10 | 11 | -- image 12 | local img_mt = {} 13 | img_mt.__index = img_mt 14 | 15 | function img_mt:save(path, desc, fmt) -- ensure no file ext in path string 16 | if fmt == "ppm" then 17 | libimage:saveppm(path, self.w, self.h, self.pixfmt, self.buf) 18 | elseif fmt == "png" then 19 | libimage:savepng(path, self.w, self.h, self.pixfmt, self.buf) 20 | end 21 | if desc then --generate a description file as image sheet 22 | local lua_path = path..".p.lua" 23 | local body = "return {\n\n" 24 | body = body..string.format(TEMPLATE_IMAGE, self.name, 0, 0, self.w, self.h, self.ox, self.oy) 25 | body = body.."\n}" 26 | libos:writefile(lua_path, body) 27 | end 28 | 29 | utils:logf("image <%s> saved", self.name) 30 | end 31 | 32 | -- image sheet 33 | local sheet_mt = {} 34 | sheet_mt.__index = sheet_mt 35 | 36 | function sheet_mt:pack_img(img) 37 | assert(self.bin) 38 | 39 | local rect = self.bin:insert(img.w + 2, img.h + 2) -- 2 empty pixel between images 40 | if rect then 41 | local x = rect.x + 1 42 | local y = rect.y + 1 43 | libimage:blitimg(self.buf, self.size, self.pixfmt, img.buf, img.w, img.h, x, y) 44 | local info = {name=img.name, pos={x,y}, size={img.w,img.h}, offset={img.ox,img.oy}} 45 | table.insert(self.imgs, info) 46 | return true 47 | end 48 | 49 | return false 50 | end 51 | 52 | function sheet_mt:save(path, desc, fmt) -- ensure no file ext in path string 53 | if fmt == "ppm" then 54 | libimage:saveppm(path, self.size, self.size, self.pixfmt, self.buf) 55 | elseif fmt == "png" then 56 | libimage:savepng(path, self.size, self.size, self.pixfmt, self.buf) 57 | end 58 | if desc then 59 | local lua_path = path..".p.lua" 60 | local body = "return {\n\n" 61 | for _,v in ipairs(self.imgs) do 62 | body = body..string.format(TEMPLATE_IMAGE, v.name, 63 | v.pos[1], v.pos[2], v.size[1], v.size[2], v.offset[1], v.offset[2]) 64 | end 65 | body = body.."\n}" 66 | libos:writefile(lua_path, body) 67 | end 68 | 69 | utils:logf("image sheet saved") 70 | end 71 | 72 | -- animations 73 | local anim_mt = {} 74 | anim_mt.__index = anim_mt 75 | 76 | function anim_mt:add_action(frames) 77 | table.insert(self.actions, frames) 78 | end 79 | 80 | function anim_mt:save(path) 81 | local body = "return {\n\n" 82 | for _,act in ipairs(self.actions) do 83 | body = body.."{\n" -- start a new action 84 | for _,frm in ipairs(act) do 85 | body = body.."\t{ " -- start a new frame 86 | for _,com in ipairs(frm) do 87 | body = body..string.format("{ name=\"%s\", ", com.name) 88 | if com.scale then 89 | body = body..string.format("scale={%d,%d}, ", com.scale[1], com.scale[2]) 90 | end 91 | if com.rot then 92 | body = body..string.format("rot=%d, ", com.rot) 93 | end 94 | if com.trans then 95 | body = body..string.format("trans={%d,%d}, ", com.trans[1], com.trans[2]) 96 | end 97 | if com.color then 98 | body = body..string.format("color=0x%08x, ", com.color) 99 | end 100 | if com.additive then 101 | body = body..string.format("additive=0x%08x, ", com.additive) 102 | end 103 | body = body.."}, " 104 | end 105 | body = body.."},\n" 106 | end 107 | body = body.."},\n" 108 | end 109 | body = body.."\n}" 110 | libos:writefile(path, body) 111 | 112 | utils:logf("animation <%s> saved", self.name) 113 | end 114 | 115 | -- ejoy2d resource module 116 | local M = {} 117 | 118 | function M:load_img(path, name) 119 | local buf, pixfmt, w, h = libimage:loadpng(path) 120 | if not buf then return end 121 | 122 | local ox = 0 123 | local oy = 0 124 | if pixfmt == "RGBA" then 125 | local buf_trim, l, r, t, b = libimage:trimimg(buf, w, h, pixfmt) 126 | if buf_trim then 127 | buf = buf_trim 128 | w = w - l - r 129 | h = h - t - b 130 | ox = (l - r) / 2 131 | oy = (t - b) / 2 132 | end 133 | end 134 | 135 | local img = {} 136 | img.buf = buf -- raw memory for pixel data 137 | img.pixfmt = pixfmt -- pixel format string 138 | img.w = w 139 | img.h = h 140 | img.ox = ox 141 | img.oy = oy 142 | img.name = name 143 | return setmetatable(img, img_mt) 144 | end 145 | 146 | function M:new_sheet(size, pixfmt) 147 | local sheet = {} 148 | sheet.buf = libimage:newimg(size, pixfmt) 149 | sheet.pixfmt = pixfmt 150 | sheet.size = size 151 | sheet.imgs = {} 152 | sheet.bin = binpacking:new_bin(size, size) 153 | return setmetatable(sheet, sheet_mt) 154 | end 155 | 156 | function M:load_sheet(path) 157 | local buf, pixfmt, w, h = libimage:loadppm(string.sub(path, 1, -7)) 158 | if w ~= h then return end -- image sheet must be a square 159 | if buf then 160 | local sheet = {} 161 | sheet.buf = buf 162 | sheet.pixfmt = pixfmt 163 | sheet.size = w 164 | sheet.imgs = dofile(path) 165 | sheet.bin = false -- loaded sheet cannot pack in new image 166 | return setmetatable(sheet, sheet_mt) 167 | end 168 | end 169 | 170 | function M:new_anim(name) 171 | local anim = {} 172 | anim.name = name 173 | anim.actions = {} 174 | return setmetatable(anim, anim_mt) 175 | end 176 | 177 | function M:load_anim(path, name) 178 | local anim = {} 179 | anim.name = name 180 | anim.actions = dofile(path) 181 | return setmetatable(anim, anim_mt) 182 | end 183 | 184 | return M -------------------------------------------------------------------------------- /3rd/lua/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.26.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lstring_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lmem.h" 16 | #include "lobject.h" 17 | #include "lstate.h" 18 | #include "lstring.h" 19 | 20 | 21 | /* 22 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to 23 | ** compute its hash 24 | */ 25 | #if !defined(LUAI_HASHLIMIT) 26 | #define LUAI_HASHLIMIT 5 27 | #endif 28 | 29 | 30 | /* 31 | ** equality for long strings 32 | */ 33 | int luaS_eqlngstr (TString *a, TString *b) { 34 | size_t len = a->tsv.len; 35 | lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR); 36 | return (a == b) || /* same instance or... */ 37 | ((len == b->tsv.len) && /* equal length and ... */ 38 | (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ 39 | } 40 | 41 | 42 | /* 43 | ** equality for strings 44 | */ 45 | int luaS_eqstr (TString *a, TString *b) { 46 | return (a->tsv.tt == b->tsv.tt) && 47 | (a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b)); 48 | } 49 | 50 | 51 | unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { 52 | unsigned int h = seed ^ cast(unsigned int, l); 53 | size_t l1; 54 | size_t step = (l >> LUAI_HASHLIMIT) + 1; 55 | for (l1 = l; l1 >= step; l1 -= step) 56 | h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1])); 57 | return h; 58 | } 59 | 60 | 61 | /* 62 | ** resizes the string table 63 | */ 64 | void luaS_resize (lua_State *L, int newsize) { 65 | int i; 66 | stringtable *tb = &G(L)->strt; 67 | /* cannot resize while GC is traversing strings */ 68 | luaC_runtilstate(L, ~bitmask(GCSsweepstring)); 69 | if (newsize > tb->size) { 70 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 71 | for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL; 72 | } 73 | /* rehash */ 74 | for (i=0; isize; i++) { 75 | GCObject *p = tb->hash[i]; 76 | tb->hash[i] = NULL; 77 | while (p) { /* for each node in the list */ 78 | GCObject *next = gch(p)->next; /* save next */ 79 | unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */ 80 | gch(p)->next = tb->hash[h]; /* chain it */ 81 | tb->hash[h] = p; 82 | resetoldbit(p); /* see MOVE OLD rule */ 83 | p = next; 84 | } 85 | } 86 | if (newsize < tb->size) { 87 | /* shrinking slice must be empty */ 88 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); 89 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 90 | } 91 | tb->size = newsize; 92 | } 93 | 94 | 95 | /* 96 | ** creates a new string object 97 | */ 98 | static TString *createstrobj (lua_State *L, const char *str, size_t l, 99 | int tag, unsigned int h, GCObject **list) { 100 | TString *ts; 101 | size_t totalsize; /* total size of TString object */ 102 | totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); 103 | ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts; 104 | ts->tsv.len = l; 105 | ts->tsv.hash = h; 106 | ts->tsv.extra = 0; 107 | memcpy(ts+1, str, l*sizeof(char)); 108 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 109 | return ts; 110 | } 111 | 112 | 113 | /* 114 | ** creates a new short string, inserting it into string table 115 | */ 116 | static TString *newshrstr (lua_State *L, const char *str, size_t l, 117 | unsigned int h) { 118 | GCObject **list; /* (pointer to) list where it will be inserted */ 119 | stringtable *tb = &G(L)->strt; 120 | TString *s; 121 | if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 122 | luaS_resize(L, tb->size*2); /* too crowded */ 123 | list = &tb->hash[lmod(h, tb->size)]; 124 | s = createstrobj(L, str, l, LUA_TSHRSTR, h, list); 125 | tb->nuse++; 126 | return s; 127 | } 128 | 129 | 130 | /* 131 | ** checks whether short string exists and reuses it or creates a new one 132 | */ 133 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { 134 | GCObject *o; 135 | global_State *g = G(L); 136 | unsigned int h = luaS_hash(str, l, g->seed); 137 | for (o = g->strt.hash[lmod(h, g->strt.size)]; 138 | o != NULL; 139 | o = gch(o)->next) { 140 | TString *ts = rawgco2ts(o); 141 | if (h == ts->tsv.hash && 142 | l == ts->tsv.len && 143 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { 144 | if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ 145 | changewhite(o); /* resurrect it */ 146 | return ts; 147 | } 148 | } 149 | return newshrstr(L, str, l, h); /* not found; create a new string */ 150 | } 151 | 152 | 153 | /* 154 | ** new string (with explicit length) 155 | */ 156 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 157 | if (l <= LUAI_MAXSHORTLEN) /* short string? */ 158 | return internshrstr(L, str, l); 159 | else { 160 | if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 161 | luaM_toobig(L); 162 | return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL); 163 | } 164 | } 165 | 166 | 167 | /* 168 | ** new zero-terminated string 169 | */ 170 | TString *luaS_new (lua_State *L, const char *str) { 171 | return luaS_newlstr(L, str, strlen(str)); 172 | } 173 | 174 | 175 | Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { 176 | Udata *u; 177 | if (s > MAX_SIZET - sizeof(Udata)) 178 | luaM_toobig(L); 179 | u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u; 180 | u->uv.len = s; 181 | u->uv.metatable = NULL; 182 | u->uv.env = e; 183 | return u; 184 | } 185 | 186 | -------------------------------------------------------------------------------- /3rd/zlib/adler32.c: -------------------------------------------------------------------------------- 1 | /* adler32.c -- compute the Adler-32 checksum of a data stream 2 | * Copyright (C) 1995-2011 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #include "zutil.h" 9 | 10 | #define local static 11 | 12 | local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2)); 13 | 14 | #define BASE 65521 /* largest prime smaller than 65536 */ 15 | #define NMAX 5552 16 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ 17 | 18 | #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} 19 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); 20 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); 21 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); 22 | #define DO16(buf) DO8(buf,0); DO8(buf,8); 23 | 24 | /* use NO_DIVIDE if your processor does not do division in hardware -- 25 | try it both ways to see which is faster */ 26 | #ifdef NO_DIVIDE 27 | /* note that this assumes BASE is 65521, where 65536 % 65521 == 15 28 | (thank you to John Reiser for pointing this out) */ 29 | # define CHOP(a) \ 30 | do { \ 31 | unsigned long tmp = a >> 16; \ 32 | a &= 0xffffUL; \ 33 | a += (tmp << 4) - tmp; \ 34 | } while (0) 35 | # define MOD28(a) \ 36 | do { \ 37 | CHOP(a); \ 38 | if (a >= BASE) a -= BASE; \ 39 | } while (0) 40 | # define MOD(a) \ 41 | do { \ 42 | CHOP(a); \ 43 | MOD28(a); \ 44 | } while (0) 45 | # define MOD63(a) \ 46 | do { /* this assumes a is not negative */ \ 47 | z_off64_t tmp = a >> 32; \ 48 | a &= 0xffffffffL; \ 49 | a += (tmp << 8) - (tmp << 5) + tmp; \ 50 | tmp = a >> 16; \ 51 | a &= 0xffffL; \ 52 | a += (tmp << 4) - tmp; \ 53 | tmp = a >> 16; \ 54 | a &= 0xffffL; \ 55 | a += (tmp << 4) - tmp; \ 56 | if (a >= BASE) a -= BASE; \ 57 | } while (0) 58 | #else 59 | # define MOD(a) a %= BASE 60 | # define MOD28(a) a %= BASE 61 | # define MOD63(a) a %= BASE 62 | #endif 63 | 64 | /* ========================================================================= */ 65 | uLong ZEXPORT adler32(adler, buf, len) 66 | uLong adler; 67 | const Bytef *buf; 68 | uInt len; 69 | { 70 | unsigned long sum2; 71 | unsigned n; 72 | 73 | /* split Adler-32 into component sums */ 74 | sum2 = (adler >> 16) & 0xffff; 75 | adler &= 0xffff; 76 | 77 | /* in case user likes doing a byte at a time, keep it fast */ 78 | if (len == 1) { 79 | adler += buf[0]; 80 | if (adler >= BASE) 81 | adler -= BASE; 82 | sum2 += adler; 83 | if (sum2 >= BASE) 84 | sum2 -= BASE; 85 | return adler | (sum2 << 16); 86 | } 87 | 88 | /* initial Adler-32 value (deferred check for len == 1 speed) */ 89 | if (buf == Z_NULL) 90 | return 1L; 91 | 92 | /* in case short lengths are provided, keep it somewhat fast */ 93 | if (len < 16) { 94 | while (len--) { 95 | adler += *buf++; 96 | sum2 += adler; 97 | } 98 | if (adler >= BASE) 99 | adler -= BASE; 100 | MOD28(sum2); /* only added so many BASE's */ 101 | return adler | (sum2 << 16); 102 | } 103 | 104 | /* do length NMAX blocks -- requires just one modulo operation */ 105 | while (len >= NMAX) { 106 | len -= NMAX; 107 | n = NMAX / 16; /* NMAX is divisible by 16 */ 108 | do { 109 | DO16(buf); /* 16 sums unrolled */ 110 | buf += 16; 111 | } while (--n); 112 | MOD(adler); 113 | MOD(sum2); 114 | } 115 | 116 | /* do remaining bytes (less than NMAX, still just one modulo) */ 117 | if (len) { /* avoid modulos if none remaining */ 118 | while (len >= 16) { 119 | len -= 16; 120 | DO16(buf); 121 | buf += 16; 122 | } 123 | while (len--) { 124 | adler += *buf++; 125 | sum2 += adler; 126 | } 127 | MOD(adler); 128 | MOD(sum2); 129 | } 130 | 131 | /* return recombined sums */ 132 | return adler | (sum2 << 16); 133 | } 134 | 135 | /* ========================================================================= */ 136 | local uLong adler32_combine_(adler1, adler2, len2) 137 | uLong adler1; 138 | uLong adler2; 139 | z_off64_t len2; 140 | { 141 | unsigned long sum1; 142 | unsigned long sum2; 143 | unsigned rem; 144 | 145 | /* for negative len, return invalid adler32 as a clue for debugging */ 146 | if (len2 < 0) 147 | return 0xffffffffUL; 148 | 149 | /* the derivation of this formula is left as an exercise for the reader */ 150 | MOD63(len2); /* assumes len2 >= 0 */ 151 | rem = (unsigned)len2; 152 | sum1 = adler1 & 0xffff; 153 | sum2 = rem * sum1; 154 | MOD(sum2); 155 | sum1 += (adler2 & 0xffff) + BASE - 1; 156 | sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; 157 | if (sum1 >= BASE) sum1 -= BASE; 158 | if (sum1 >= BASE) sum1 -= BASE; 159 | if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1); 160 | if (sum2 >= BASE) sum2 -= BASE; 161 | return sum1 | (sum2 << 16); 162 | } 163 | 164 | /* ========================================================================= */ 165 | uLong ZEXPORT adler32_combine(adler1, adler2, len2) 166 | uLong adler1; 167 | uLong adler2; 168 | z_off_t len2; 169 | { 170 | return adler32_combine_(adler1, adler2, len2); 171 | } 172 | 173 | uLong ZEXPORT adler32_combine64(adler1, adler2, len2) 174 | uLong adler1; 175 | uLong adler2; 176 | z_off64_t len2; 177 | { 178 | return adler32_combine_(adler1, adler2, len2); 179 | } 180 | -------------------------------------------------------------------------------- /src/scripts/ejpackage.lua: -------------------------------------------------------------------------------- 1 | local libos = require "libos" 2 | local utils = require "utils" 3 | 4 | -- file templates 5 | local TEMPLATE_BODY = [[ 6 | return { 7 | 8 | %s 9 | } 10 | ]] 11 | 12 | local TEMPLATE_LABEL = [[ 13 | { 14 | type = "label", 15 | id = %d, 16 | export = "default_label", 17 | width = 200, 18 | height = 100, 19 | font = "", 20 | align = 0, 21 | size = 16, 22 | color = 0xffffffff, 23 | noedge = true, 24 | }, 25 | ]] 26 | 27 | local TEMPLATE_PICTURE = [[ 28 | { 29 | type = "picture", 30 | id = %d, 31 | export = "%s", 32 | { 33 | tex = %d, 34 | src = { %d, %d, %d, %d, %d, %d, %d, %d }, 35 | screen = { %d, %d, %d, %d, %d, %d, %d, %d }, 36 | }, 37 | }, 38 | ]] 39 | 40 | local TEMPLATE_ANIMATION = [[ 41 | { 42 | type = "animation", 43 | id = %d, 44 | export = "%s", 45 | component = { 46 | %s }, 47 | %s 48 | }, 49 | ]] 50 | 51 | -- ejoy2d package 52 | local pkg_mt = {} 53 | pkg_mt.__index = pkg_mt 54 | 55 | function pkg_mt:add_img(img) 56 | table.insert(self.sheets, img) -- treat image as sheet with a single rect 57 | local item = {} 58 | item.type = "picture" 59 | item.id = self:_next_id() 60 | item.data = {#self.sheets, {0,0}, {img.w,img.h}, {img.ox,img.oy}} 61 | self.items[img.name] = item 62 | end 63 | 64 | function pkg_mt:add_sheet(sheet) 65 | table.insert(self.sheets, sheet) 66 | for _,v in ipairs(sheet.imgs) do 67 | local item = {} 68 | item.type = "picture" 69 | item.id = self:_next_id() 70 | item.data = {#self.sheets, v.pos, v.size, v.offset} -- texid, pos, size, offset 71 | self.items[v.name] = item 72 | end 73 | end 74 | 75 | function pkg_mt:add_anim(anim) -- image name sequence 76 | local item = {} 77 | item.type = "animation" 78 | item.id = self:_next_id() 79 | item.data = anim.actions -- frames 80 | self.items[anim.name] = item 81 | end 82 | 83 | function pkg_mt:save(path) 84 | -- save image sheet 85 | for i,v in ipairs(self.sheets) do 86 | local picture_path = string.format("%s/%s.%d", path, self.name, i) 87 | v:save(picture_path, false, self.fmt) 88 | end 89 | 90 | -- save description file 91 | local body = string.format(TEMPLATE_LABEL, self:_next_id()) 92 | for k,v in pairs(self.items) do 93 | if v.type == "picture" then 94 | body = body..self:_serialize_picture(v.id, k, v.data) 95 | end 96 | end 97 | for k,v in pairs(self.items) do 98 | if v.type == "animation" then 99 | body = body..self:_serialize_animation(v.id, k, v.data) 100 | end 101 | end 102 | 103 | local all = string.format(TEMPLATE_BODY, body) 104 | local lua_path = string.format("%s/%s.lua", path, self.name) 105 | libos:writefile(lua_path, all) 106 | 107 | utils:logf("ejoy2d package <%s> saved", self.name) 108 | end 109 | 110 | function pkg_mt:_serialize_picture(id, name, data) 111 | local pos = data[2] 112 | local size = data[3] 113 | local offset = data[4] 114 | 115 | local tex = data[1] 116 | 117 | local sl = pos[1] 118 | local sr = pos[1] + size[1] 119 | local st = pos[2] 120 | local sb = pos[2] + size[2] 121 | 122 | local dl = (-size[1]/2 + offset[1]) * 16 -- left = (-w/2 + ox) * 16 123 | local dr = (size[1]/2 + offset[1]) * 16 124 | local dt = (-size[2]/2 + offset[2]) * 16 125 | local db = (size[2]/2 + offset[2]) * 16 126 | 127 | return string.format(TEMPLATE_PICTURE, id, name, tex, 128 | sl, st, sl, sb, sr, sb, sr, st, 129 | dl, dt, dl, db, dr, db, dr, dt) 130 | end 131 | 132 | function pkg_mt:_serialize_animation(id, name, data) 133 | local com2idx = {} 134 | local idx2com = {} 135 | 136 | local str_a = "" -- action section 137 | 138 | for _,act in ipairs(data) do 139 | local frame_list = {} 140 | for _,frm in ipairs(act) do -- multiple frames in one action 141 | local com_list = {} 142 | for _,com in ipairs(frm) do -- multiple components in one frame 143 | if not com2idx[com.name] then 144 | table.insert(idx2com, com.name) 145 | com2idx[com.name] = #idx2com - 1 -- idx base 0 146 | end 147 | 148 | local idx_only = true 149 | 150 | local str_com = string.format("{ index = %d, ", com2idx[com.name]) 151 | if com.scale or com.rot or com.trans then 152 | idx_only = false 153 | local mat = utils:create_matrix(com.scale, com.rot, com.trans) 154 | str_com = str_com..string.format("mat = { %d, %d, %d, %d, %d, %d }, ", 155 | mat[1], mat[2], mat[3], mat[4], mat[5], mat[6]) 156 | end 157 | if com.color then 158 | idx_only = false 159 | str_com = str_com..string.format("color = 0x%08x", com.color) 160 | end 161 | if com.additive then 162 | idx_only = false 163 | str_com = str_com..string.format("additive = 0x%08x", com.additive) 164 | end 165 | str_com = str_com.."}" 166 | 167 | if idx_only then 168 | table.insert(com_list, tostring(com2idx[com.name])) -- simple component without attributes 169 | else 170 | table.insert(com_list, str_com) 171 | end 172 | end 173 | local str_f = string.format("\t\t{ %s },", table.concat(com_list, ", ")) 174 | table.insert(frame_list, str_f) 175 | end 176 | str_a = string.format("\t{\n%s\n\t},", table.concat(frame_list, "\n")) 177 | end 178 | 179 | local str_c = "" -- component section 180 | for _,com in ipairs(idx2com) do 181 | local item = self.items[com] 182 | if item then 183 | str_c = string.format("%s\t\t{ id = %d, name = \"%s\" },\n", str_c, item.id, com) -- one component one line 184 | else 185 | str_c = string.format("%s\t\t{ name = \"%s\" },\n", str_c, com) -- anchor 186 | end 187 | end 188 | 189 | return string.format(TEMPLATE_ANIMATION, id, name, str_c, str_a) 190 | end 191 | 192 | function pkg_mt:_next_id() 193 | local ret = self._id 194 | self._id = self._id + 1 195 | return ret 196 | end 197 | 198 | -- ejoy2d package module 199 | local M = {} 200 | 201 | function M:new_pkg(name, fmt) 202 | local pkg = {} 203 | pkg._id = 0 204 | pkg.name = name 205 | pkg.fmt = fmt 206 | pkg.sheets = {} 207 | pkg.items = {} -- name:item 208 | return setmetatable(pkg, pkg_mt) 209 | end 210 | 211 | return M -------------------------------------------------------------------------------- /3rd/libpng/pngdebug.h: -------------------------------------------------------------------------------- 1 | 2 | /* pngdebug.h - Debugging macros for libpng, also used in pngtest.c 3 | * 4 | * Copyright (c) 1998-2013 Glenn Randers-Pehrson 5 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 6 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 7 | * 8 | * Last changed in libpng 1.6.8 [December 19, 2013] 9 | * 10 | * This code is released under the libpng license. 11 | * For conditions of distribution and use, see the disclaimer 12 | * and license in png.h 13 | */ 14 | 15 | /* Define PNG_DEBUG at compile time for debugging information. Higher 16 | * numbers for PNG_DEBUG mean more debugging information. This has 17 | * only been added since version 0.95 so it is not implemented throughout 18 | * libpng yet, but more support will be added as needed. 19 | * 20 | * png_debug[1-2]?(level, message ,arg{0-2}) 21 | * Expands to a statement (either a simple expression or a compound 22 | * do..while(0) statement) that outputs a message with parameter 23 | * substitution if PNG_DEBUG is defined to 2 or more. If PNG_DEBUG 24 | * is undefined, 0 or 1 every png_debug expands to a simple expression 25 | * (actually ((void)0)). 26 | * 27 | * level: level of detail of message, starting at 0. A level 'n' 28 | * message is preceded by 'n' 3-space indentations (not implemented 29 | * on Microsoft compilers unless PNG_DEBUG_FILE is also 30 | * defined, to allow debug DLL compilation with no standard IO). 31 | * message: a printf(3) style text string. A trailing '\n' is added 32 | * to the message. 33 | * arg: 0 to 2 arguments for printf(3) style substitution in message. 34 | */ 35 | #ifndef PNGDEBUG_H 36 | #define PNGDEBUG_H 37 | /* These settings control the formatting of messages in png.c and pngerror.c */ 38 | /* Moved to pngdebug.h at 1.5.0 */ 39 | # ifndef PNG_LITERAL_SHARP 40 | # define PNG_LITERAL_SHARP 0x23 41 | # endif 42 | # ifndef PNG_LITERAL_LEFT_SQUARE_BRACKET 43 | # define PNG_LITERAL_LEFT_SQUARE_BRACKET 0x5b 44 | # endif 45 | # ifndef PNG_LITERAL_RIGHT_SQUARE_BRACKET 46 | # define PNG_LITERAL_RIGHT_SQUARE_BRACKET 0x5d 47 | # endif 48 | # ifndef PNG_STRING_NEWLINE 49 | # define PNG_STRING_NEWLINE "\n" 50 | # endif 51 | 52 | #ifdef PNG_DEBUG 53 | # if (PNG_DEBUG > 0) 54 | # if !defined(PNG_DEBUG_FILE) && defined(_MSC_VER) 55 | # include 56 | # if (PNG_DEBUG > 1) 57 | # ifndef _DEBUG 58 | # define _DEBUG 59 | # endif 60 | # ifndef png_debug 61 | # define png_debug(l,m) _RPT0(_CRT_WARN,m PNG_STRING_NEWLINE) 62 | # endif 63 | # ifndef png_debug1 64 | # define png_debug1(l,m,p1) _RPT1(_CRT_WARN,m PNG_STRING_NEWLINE,p1) 65 | # endif 66 | # ifndef png_debug2 67 | # define png_debug2(l,m,p1,p2) \ 68 | _RPT2(_CRT_WARN,m PNG_STRING_NEWLINE,p1,p2) 69 | # endif 70 | # endif 71 | # else /* PNG_DEBUG_FILE || !_MSC_VER */ 72 | # ifndef PNG_STDIO_SUPPORTED 73 | # include /* not included yet */ 74 | # endif 75 | # ifndef PNG_DEBUG_FILE 76 | # define PNG_DEBUG_FILE stderr 77 | # endif /* PNG_DEBUG_FILE */ 78 | 79 | # if (PNG_DEBUG > 1) 80 | # ifdef __STDC__ 81 | # ifndef png_debug 82 | # define png_debug(l,m) \ 83 | do { \ 84 | int num_tabs=l; \ 85 | fprintf(PNG_DEBUG_FILE,"%s" m PNG_STRING_NEWLINE,(num_tabs==1 ? " " : \ 86 | (num_tabs==2 ? " " : (num_tabs>2 ? " " : "")))); \ 87 | } while (0) 88 | # endif 89 | # ifndef png_debug1 90 | # define png_debug1(l,m,p1) \ 91 | do { \ 92 | int num_tabs=l; \ 93 | fprintf(PNG_DEBUG_FILE,"%s" m PNG_STRING_NEWLINE,(num_tabs==1 ? " " : \ 94 | (num_tabs==2 ? " " : (num_tabs>2 ? " " : ""))),p1); \ 95 | } while (0) 96 | # endif 97 | # ifndef png_debug2 98 | # define png_debug2(l,m,p1,p2) \ 99 | do { \ 100 | int num_tabs=l; \ 101 | fprintf(PNG_DEBUG_FILE,"%s" m PNG_STRING_NEWLINE,(num_tabs==1 ? " " : \ 102 | (num_tabs==2 ? " " : (num_tabs>2 ? " " : ""))),p1,p2);\ 103 | } while (0) 104 | # endif 105 | # else /* __STDC __ */ 106 | # ifndef png_debug 107 | # define png_debug(l,m) \ 108 | do { \ 109 | int num_tabs=l; \ 110 | char format[256]; \ 111 | snprintf(format,256,"%s%s%s",(num_tabs==1 ? "\t" : \ 112 | (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))), \ 113 | m,PNG_STRING_NEWLINE); \ 114 | fprintf(PNG_DEBUG_FILE,format); \ 115 | } while (0) 116 | # endif 117 | # ifndef png_debug1 118 | # define png_debug1(l,m,p1) \ 119 | do { \ 120 | int num_tabs=l; \ 121 | char format[256]; \ 122 | snprintf(format,256,"%s%s%s",(num_tabs==1 ? "\t" : \ 123 | (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))), \ 124 | m,PNG_STRING_NEWLINE); \ 125 | fprintf(PNG_DEBUG_FILE,format,p1); \ 126 | } while (0) 127 | # endif 128 | # ifndef png_debug2 129 | # define png_debug2(l,m,p1,p2) \ 130 | do { \ 131 | int num_tabs=l; \ 132 | char format[256]; \ 133 | snprintf(format,256,"%s%s%s",(num_tabs==1 ? "\t" : \ 134 | (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))), \ 135 | m,PNG_STRING_NEWLINE); \ 136 | fprintf(PNG_DEBUG_FILE,format,p1,p2); \ 137 | } while (0) 138 | # endif 139 | # endif /* __STDC __ */ 140 | # endif /* (PNG_DEBUG > 1) */ 141 | 142 | # endif /* _MSC_VER */ 143 | # endif /* (PNG_DEBUG > 0) */ 144 | #endif /* PNG_DEBUG */ 145 | #ifndef png_debug 146 | # define png_debug(l, m) ((void)0) 147 | #endif 148 | #ifndef png_debug1 149 | # define png_debug1(l, m, p1) ((void)0) 150 | #endif 151 | #ifndef png_debug2 152 | # define png_debug2(l, m, p1, p2) ((void)0) 153 | #endif 154 | #endif /* PNGDEBUG_H */ 155 | -------------------------------------------------------------------------------- /3rd/lua/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | /* 15 | ** Collectable objects may have one of three colors: white, which 16 | ** means the object is not marked; gray, which means the 17 | ** object is marked, but its references may be not marked; and 18 | ** black, which means that the object and all its references are marked. 19 | ** The main invariant of the garbage collector, while marking objects, 20 | ** is that a black object can never point to a white one. Moreover, 21 | ** any gray object must be in a "gray list" (gray, grayagain, weak, 22 | ** allweak, ephemeron) so that it can be visited again before finishing 23 | ** the collection cycle. These lists have no meaning when the invariant 24 | ** is not being enforced (e.g., sweep phase). 25 | */ 26 | 27 | 28 | 29 | /* how much to allocate before next GC step */ 30 | #if !defined(GCSTEPSIZE) 31 | /* ~100 small strings */ 32 | #define GCSTEPSIZE (cast_int(100 * sizeof(TString))) 33 | #endif 34 | 35 | 36 | /* 37 | ** Possible states of the Garbage Collector 38 | */ 39 | #define GCSpropagate 0 40 | #define GCSatomic 1 41 | #define GCSsweepstring 2 42 | #define GCSsweepudata 3 43 | #define GCSsweep 4 44 | #define GCSpause 5 45 | 46 | 47 | #define issweepphase(g) \ 48 | (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep) 49 | 50 | #define isgenerational(g) ((g)->gckind == KGC_GEN) 51 | 52 | /* 53 | ** macros to tell when main invariant (white objects cannot point to black 54 | ** ones) must be kept. During a non-generational collection, the sweep 55 | ** phase may break the invariant, as objects turned white may point to 56 | ** still-black objects. The invariant is restored when sweep ends and 57 | ** all objects are white again. During a generational collection, the 58 | ** invariant must be kept all times. 59 | */ 60 | 61 | #define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic) 62 | 63 | 64 | /* 65 | ** Outside the collector, the state in generational mode is kept in 66 | ** 'propagate', so 'keepinvariant' is always true. 67 | */ 68 | #define keepinvariantout(g) \ 69 | check_exp(g->gcstate == GCSpropagate || !isgenerational(g), \ 70 | g->gcstate <= GCSatomic) 71 | 72 | 73 | /* 74 | ** some useful bit tricks 75 | */ 76 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 77 | #define setbits(x,m) ((x) |= (m)) 78 | #define testbits(x,m) ((x) & (m)) 79 | #define bitmask(b) (1<<(b)) 80 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 81 | #define l_setbit(x,b) setbits(x, bitmask(b)) 82 | #define resetbit(x,b) resetbits(x, bitmask(b)) 83 | #define testbit(x,b) testbits(x, bitmask(b)) 84 | 85 | 86 | /* Layout for bit use in `marked' field: */ 87 | #define WHITE0BIT 0 /* object is white (type 0) */ 88 | #define WHITE1BIT 1 /* object is white (type 1) */ 89 | #define BLACKBIT 2 /* object is black */ 90 | #define FINALIZEDBIT 3 /* object has been separated for finalization */ 91 | #define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */ 92 | #define FIXEDBIT 5 /* object is fixed (should not be collected) */ 93 | #define OLDBIT 6 /* object is old (only in generational mode) */ 94 | /* bit 7 is currently used by tests (luaL_checkmemory) */ 95 | 96 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 97 | 98 | 99 | #define iswhite(x) testbits((x)->gch.marked, WHITEBITS) 100 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) 101 | #define isgray(x) /* neither white nor black */ \ 102 | (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT))) 103 | 104 | #define isold(x) testbit((x)->gch.marked, OLDBIT) 105 | 106 | /* MOVE OLD rule: whenever an object is moved to the beginning of 107 | a GC list, its old bit must be cleared */ 108 | #define resetoldbit(o) resetbit((o)->gch.marked, OLDBIT) 109 | 110 | #define otherwhite(g) (g->currentwhite ^ WHITEBITS) 111 | #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) 112 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked) 113 | 114 | #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 115 | #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 116 | 117 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 118 | 119 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 120 | 121 | 122 | #define luaC_condGC(L,c) \ 123 | {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} 124 | #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) 125 | 126 | 127 | #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 128 | luaC_barrier_(L,obj2gco(p),gcvalue(v)); } 129 | 130 | #define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 131 | luaC_barrierback_(L,p); } 132 | 133 | #define luaC_objbarrier(L,p,o) \ 134 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 135 | luaC_barrier_(L,obj2gco(p),obj2gco(o)); } 136 | 137 | #define luaC_objbarrierback(L,p,o) \ 138 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); } 139 | 140 | #define luaC_barrierproto(L,p,c) \ 141 | { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); } 142 | 143 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 144 | LUAI_FUNC void luaC_step (lua_State *L); 145 | LUAI_FUNC void luaC_forcestep (lua_State *L); 146 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 147 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 148 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, 149 | GCObject **list, int offset); 150 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 151 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); 152 | LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c); 153 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 154 | LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv); 155 | LUAI_FUNC void luaC_changemode (lua_State *L, int mode); 156 | 157 | #endif 158 | -------------------------------------------------------------------------------- /3rd/libpng/pngwio.c: -------------------------------------------------------------------------------- 1 | 2 | /* pngwio.c - functions for data output 3 | * 4 | * Last changed in libpng 1.6.9 [February 6, 2014] 5 | * Copyright (c) 1998-2014 Glenn Randers-Pehrson 6 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 7 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 8 | * 9 | * This code is released under the libpng license. 10 | * For conditions of distribution and use, see the disclaimer 11 | * and license in png.h 12 | * 13 | * This file provides a location for all output. Users who need 14 | * special handling are expected to write functions that have the same 15 | * arguments as these and perform similar functions, but that possibly 16 | * use different output methods. Note that you shouldn't change these 17 | * functions, but rather write replacement functions and then change 18 | * them at run time with png_set_write_fn(...). 19 | */ 20 | 21 | #include "pngpriv.h" 22 | 23 | #ifdef PNG_WRITE_SUPPORTED 24 | 25 | /* Write the data to whatever output you are using. The default routine 26 | * writes to a file pointer. Note that this routine sometimes gets called 27 | * with very small lengths, so you should implement some kind of simple 28 | * buffering if you are using unbuffered writes. This should never be asked 29 | * to write more than 64K on a 16 bit machine. 30 | */ 31 | 32 | void /* PRIVATE */ 33 | png_write_data(png_structrp png_ptr, png_const_bytep data, png_size_t length) 34 | { 35 | /* NOTE: write_data_fn must not change the buffer! */ 36 | if (png_ptr->write_data_fn != NULL ) 37 | (*(png_ptr->write_data_fn))(png_ptr, png_constcast(png_bytep,data), 38 | length); 39 | 40 | else 41 | png_error(png_ptr, "Call to NULL write function"); 42 | } 43 | 44 | #ifdef PNG_STDIO_SUPPORTED 45 | /* This is the function that does the actual writing of data. If you are 46 | * not writing to a standard C stream, you should create a replacement 47 | * write_data function and use it at run time with png_set_write_fn(), rather 48 | * than changing the library. 49 | */ 50 | void PNGCBAPI 51 | png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) 52 | { 53 | png_size_t check; 54 | 55 | if (png_ptr == NULL) 56 | return; 57 | 58 | check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr)); 59 | 60 | if (check != length) 61 | png_error(png_ptr, "Write Error"); 62 | } 63 | #endif 64 | 65 | /* This function is called to output any data pending writing (normally 66 | * to disk). After png_flush is called, there should be no data pending 67 | * writing in any buffers. 68 | */ 69 | #ifdef PNG_WRITE_FLUSH_SUPPORTED 70 | void /* PRIVATE */ 71 | png_flush(png_structrp png_ptr) 72 | { 73 | if (png_ptr->output_flush_fn != NULL) 74 | (*(png_ptr->output_flush_fn))(png_ptr); 75 | } 76 | 77 | # ifdef PNG_STDIO_SUPPORTED 78 | void PNGCBAPI 79 | png_default_flush(png_structp png_ptr) 80 | { 81 | png_FILE_p io_ptr; 82 | 83 | if (png_ptr == NULL) 84 | return; 85 | 86 | io_ptr = png_voidcast(png_FILE_p, (png_ptr->io_ptr)); 87 | fflush(io_ptr); 88 | } 89 | # endif 90 | #endif 91 | 92 | /* This function allows the application to supply new output functions for 93 | * libpng if standard C streams aren't being used. 94 | * 95 | * This function takes as its arguments: 96 | * png_ptr - pointer to a png output data structure 97 | * io_ptr - pointer to user supplied structure containing info about 98 | * the output functions. May be NULL. 99 | * write_data_fn - pointer to a new output function that takes as its 100 | * arguments a pointer to a png_struct, a pointer to 101 | * data to be written, and a 32-bit unsigned int that is 102 | * the number of bytes to be written. The new write 103 | * function should call png_error(png_ptr, "Error msg") 104 | * to exit and output any fatal error messages. May be 105 | * NULL, in which case libpng's default function will 106 | * be used. 107 | * flush_data_fn - pointer to a new flush function that takes as its 108 | * arguments a pointer to a png_struct. After a call to 109 | * the flush function, there should be no data in any buffers 110 | * or pending transmission. If the output method doesn't do 111 | * any buffering of output, a function prototype must still be 112 | * supplied although it doesn't have to do anything. If 113 | * PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile 114 | * time, output_flush_fn will be ignored, although it must be 115 | * supplied for compatibility. May be NULL, in which case 116 | * libpng's default function will be used, if 117 | * PNG_WRITE_FLUSH_SUPPORTED is defined. This is not 118 | * a good idea if io_ptr does not point to a standard 119 | * *FILE structure. 120 | */ 121 | void PNGAPI 122 | png_set_write_fn(png_structrp png_ptr, png_voidp io_ptr, 123 | png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn) 124 | { 125 | if (png_ptr == NULL) 126 | return; 127 | 128 | png_ptr->io_ptr = io_ptr; 129 | 130 | #ifdef PNG_STDIO_SUPPORTED 131 | if (write_data_fn != NULL) 132 | png_ptr->write_data_fn = write_data_fn; 133 | 134 | else 135 | png_ptr->write_data_fn = png_default_write_data; 136 | #else 137 | png_ptr->write_data_fn = write_data_fn; 138 | #endif 139 | 140 | #ifdef PNG_WRITE_FLUSH_SUPPORTED 141 | # ifdef PNG_STDIO_SUPPORTED 142 | 143 | if (output_flush_fn != NULL) 144 | png_ptr->output_flush_fn = output_flush_fn; 145 | 146 | else 147 | png_ptr->output_flush_fn = png_default_flush; 148 | 149 | # else 150 | png_ptr->output_flush_fn = output_flush_fn; 151 | # endif 152 | #else 153 | PNG_UNUSED(output_flush_fn) 154 | #endif /* PNG_WRITE_FLUSH_SUPPORTED */ 155 | 156 | #ifdef PNG_READ_SUPPORTED 157 | /* It is an error to read while writing a png file */ 158 | if (png_ptr->read_data_fn != NULL) 159 | { 160 | png_ptr->read_data_fn = NULL; 161 | 162 | png_warning(png_ptr, 163 | "Can't set both read_data_fn and write_data_fn in the" 164 | " same structure"); 165 | } 166 | #endif 167 | } 168 | #endif /* PNG_WRITE_SUPPORTED */ 169 | -------------------------------------------------------------------------------- /3rd/zlib/inffixed.h: -------------------------------------------------------------------------------- 1 | /* inffixed.h -- table for decoding fixed codes 2 | * Generated automatically by makefixed(). 3 | */ 4 | 5 | /* WARNING: this file should *not* be used by applications. 6 | It is part of the implementation of this library and is 7 | subject to change. Applications should only use zlib.h. 8 | */ 9 | 10 | static const code lenfix[512] = { 11 | {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48}, 12 | {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128}, 13 | {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59}, 14 | {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176}, 15 | {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20}, 16 | {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100}, 17 | {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8}, 18 | {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216}, 19 | {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76}, 20 | {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114}, 21 | {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2}, 22 | {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148}, 23 | {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42}, 24 | {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86}, 25 | {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15}, 26 | {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236}, 27 | {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62}, 28 | {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142}, 29 | {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31}, 30 | {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162}, 31 | {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25}, 32 | {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105}, 33 | {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4}, 34 | {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202}, 35 | {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69}, 36 | {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125}, 37 | {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13}, 38 | {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195}, 39 | {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35}, 40 | {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91}, 41 | {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19}, 42 | {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246}, 43 | {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55}, 44 | {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135}, 45 | {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99}, 46 | {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190}, 47 | {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16}, 48 | {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96}, 49 | {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6}, 50 | {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209}, 51 | {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72}, 52 | {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116}, 53 | {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4}, 54 | {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153}, 55 | {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44}, 56 | {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82}, 57 | {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11}, 58 | {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229}, 59 | {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58}, 60 | {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138}, 61 | {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51}, 62 | {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173}, 63 | {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30}, 64 | {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110}, 65 | {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0}, 66 | {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195}, 67 | {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65}, 68 | {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121}, 69 | {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9}, 70 | {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258}, 71 | {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37}, 72 | {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93}, 73 | {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23}, 74 | {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251}, 75 | {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51}, 76 | {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131}, 77 | {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67}, 78 | {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183}, 79 | {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23}, 80 | {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103}, 81 | {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9}, 82 | {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223}, 83 | {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79}, 84 | {0,9,255} 85 | }; 86 | 87 | static const code distfix[32] = { 88 | {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025}, 89 | {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193}, 90 | {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385}, 91 | {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577}, 92 | {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073}, 93 | {22,5,193},{64,5,0} 94 | }; 95 | -------------------------------------------------------------------------------- /3rd/zlib/inflate.h: -------------------------------------------------------------------------------- 1 | /* inflate.h -- internal inflate state definition 2 | * Copyright (C) 1995-2009 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* define NO_GZIP when compiling if you want to disable gzip header and 12 | trailer decoding by inflate(). NO_GZIP would be used to avoid linking in 13 | the crc code when it is not needed. For shared libraries, gzip decoding 14 | should be left enabled. */ 15 | #ifndef NO_GZIP 16 | # define GUNZIP 17 | #endif 18 | 19 | /* Possible inflate modes between inflate() calls */ 20 | typedef enum { 21 | HEAD, /* i: waiting for magic header */ 22 | FLAGS, /* i: waiting for method and flags (gzip) */ 23 | TIME, /* i: waiting for modification time (gzip) */ 24 | OS, /* i: waiting for extra flags and operating system (gzip) */ 25 | EXLEN, /* i: waiting for extra length (gzip) */ 26 | EXTRA, /* i: waiting for extra bytes (gzip) */ 27 | NAME, /* i: waiting for end of file name (gzip) */ 28 | COMMENT, /* i: waiting for end of comment (gzip) */ 29 | HCRC, /* i: waiting for header crc (gzip) */ 30 | DICTID, /* i: waiting for dictionary check value */ 31 | DICT, /* waiting for inflateSetDictionary() call */ 32 | TYPE, /* i: waiting for type bits, including last-flag bit */ 33 | TYPEDO, /* i: same, but skip check to exit inflate on new block */ 34 | STORED, /* i: waiting for stored size (length and complement) */ 35 | COPY_, /* i/o: same as COPY below, but only first time in */ 36 | COPY, /* i/o: waiting for input or output to copy stored block */ 37 | TABLE, /* i: waiting for dynamic block table lengths */ 38 | LENLENS, /* i: waiting for code length code lengths */ 39 | CODELENS, /* i: waiting for length/lit and distance code lengths */ 40 | LEN_, /* i: same as LEN below, but only first time in */ 41 | LEN, /* i: waiting for length/lit/eob code */ 42 | LENEXT, /* i: waiting for length extra bits */ 43 | DIST, /* i: waiting for distance code */ 44 | DISTEXT, /* i: waiting for distance extra bits */ 45 | MATCH, /* o: waiting for output space to copy string */ 46 | LIT, /* o: waiting for output space to write literal */ 47 | CHECK, /* i: waiting for 32-bit check value */ 48 | LENGTH, /* i: waiting for 32-bit length (gzip) */ 49 | DONE, /* finished check, done -- remain here until reset */ 50 | BAD, /* got a data error -- remain here until reset */ 51 | MEM, /* got an inflate() memory error -- remain here until reset */ 52 | SYNC /* looking for synchronization bytes to restart inflate() */ 53 | } inflate_mode; 54 | 55 | /* 56 | State transitions between above modes - 57 | 58 | (most modes can go to BAD or MEM on error -- not shown for clarity) 59 | 60 | Process header: 61 | HEAD -> (gzip) or (zlib) or (raw) 62 | (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT -> 63 | HCRC -> TYPE 64 | (zlib) -> DICTID or TYPE 65 | DICTID -> DICT -> TYPE 66 | (raw) -> TYPEDO 67 | Read deflate blocks: 68 | TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK 69 | STORED -> COPY_ -> COPY -> TYPE 70 | TABLE -> LENLENS -> CODELENS -> LEN_ 71 | LEN_ -> LEN 72 | Read deflate codes in fixed or dynamic block: 73 | LEN -> LENEXT or LIT or TYPE 74 | LENEXT -> DIST -> DISTEXT -> MATCH -> LEN 75 | LIT -> LEN 76 | Process trailer: 77 | CHECK -> LENGTH -> DONE 78 | */ 79 | 80 | /* state maintained between inflate() calls. Approximately 10K bytes. */ 81 | struct inflate_state { 82 | inflate_mode mode; /* current inflate mode */ 83 | int last; /* true if processing last block */ 84 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ 85 | int havedict; /* true if dictionary provided */ 86 | int flags; /* gzip header method and flags (0 if zlib) */ 87 | unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ 88 | unsigned long check; /* protected copy of check value */ 89 | unsigned long total; /* protected copy of output count */ 90 | gz_headerp head; /* where to save gzip header information */ 91 | /* sliding window */ 92 | unsigned wbits; /* log base 2 of requested window size */ 93 | unsigned wsize; /* window size or zero if not using window */ 94 | unsigned whave; /* valid bytes in the window */ 95 | unsigned wnext; /* window write index */ 96 | unsigned char FAR *window; /* allocated sliding window, if needed */ 97 | /* bit accumulator */ 98 | unsigned long hold; /* input bit accumulator */ 99 | unsigned bits; /* number of bits in "in" */ 100 | /* for string and stored block copying */ 101 | unsigned length; /* literal or length of data to copy */ 102 | unsigned offset; /* distance back to copy string from */ 103 | /* for table and code decoding */ 104 | unsigned extra; /* extra bits needed */ 105 | /* fixed and dynamic code tables */ 106 | code const FAR *lencode; /* starting table for length/literal codes */ 107 | code const FAR *distcode; /* starting table for distance codes */ 108 | unsigned lenbits; /* index bits for lencode */ 109 | unsigned distbits; /* index bits for distcode */ 110 | /* dynamic table building */ 111 | unsigned ncode; /* number of code length code lengths */ 112 | unsigned nlen; /* number of length code lengths */ 113 | unsigned ndist; /* number of distance code lengths */ 114 | unsigned have; /* number of code lengths in lens[] */ 115 | code FAR *next; /* next available space in codes[] */ 116 | unsigned short lens[320]; /* temporary storage for code lengths */ 117 | unsigned short work[288]; /* work area for code table building */ 118 | code codes[ENOUGH]; /* space for code tables */ 119 | int sane; /* if false, allow invalid distance too far */ 120 | int back; /* bits back of last unprocessed length/lit */ 121 | unsigned was; /* initial length of match */ 122 | }; 123 | -------------------------------------------------------------------------------- /3rd/lua/lundump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c,v 2.22.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define lundump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "ldebug.h" 15 | #include "ldo.h" 16 | #include "lfunc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstring.h" 20 | #include "lundump.h" 21 | #include "lzio.h" 22 | 23 | typedef struct { 24 | lua_State* L; 25 | ZIO* Z; 26 | Mbuffer* b; 27 | const char* name; 28 | } LoadState; 29 | 30 | static l_noret error(LoadState* S, const char* why) 31 | { 32 | luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why); 33 | luaD_throw(S->L,LUA_ERRSYNTAX); 34 | } 35 | 36 | #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) 37 | #define LoadByte(S) (lu_byte)LoadChar(S) 38 | #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) 39 | #define LoadVector(S,b,n,size) LoadMem(S,b,n,size) 40 | 41 | #if !defined(luai_verifycode) 42 | #define luai_verifycode(L,b,f) /* empty */ 43 | #endif 44 | 45 | static void LoadBlock(LoadState* S, void* b, size_t size) 46 | { 47 | if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated"); 48 | } 49 | 50 | static int LoadChar(LoadState* S) 51 | { 52 | char x; 53 | LoadVar(S,x); 54 | return x; 55 | } 56 | 57 | static int LoadInt(LoadState* S) 58 | { 59 | int x; 60 | LoadVar(S,x); 61 | if (x<0) error(S,"corrupted"); 62 | return x; 63 | } 64 | 65 | static lua_Number LoadNumber(LoadState* S) 66 | { 67 | lua_Number x; 68 | LoadVar(S,x); 69 | return x; 70 | } 71 | 72 | static TString* LoadString(LoadState* S) 73 | { 74 | size_t size; 75 | LoadVar(S,size); 76 | if (size==0) 77 | return NULL; 78 | else 79 | { 80 | char* s=luaZ_openspace(S->L,S->b,size); 81 | LoadBlock(S,s,size*sizeof(char)); 82 | return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ 83 | } 84 | } 85 | 86 | static void LoadCode(LoadState* S, Proto* f) 87 | { 88 | int n=LoadInt(S); 89 | f->code=luaM_newvector(S->L,n,Instruction); 90 | f->sizecode=n; 91 | LoadVector(S,f->code,n,sizeof(Instruction)); 92 | } 93 | 94 | static void LoadFunction(LoadState* S, Proto* f); 95 | 96 | static void LoadConstants(LoadState* S, Proto* f) 97 | { 98 | int i,n; 99 | n=LoadInt(S); 100 | f->k=luaM_newvector(S->L,n,TValue); 101 | f->sizek=n; 102 | for (i=0; ik[i]); 103 | for (i=0; ik[i]; 106 | int t=LoadChar(S); 107 | switch (t) 108 | { 109 | case LUA_TNIL: 110 | setnilvalue(o); 111 | break; 112 | case LUA_TBOOLEAN: 113 | setbvalue(o,LoadChar(S)); 114 | break; 115 | case LUA_TNUMBER: 116 | setnvalue(o,LoadNumber(S)); 117 | break; 118 | case LUA_TSTRING: 119 | setsvalue2n(S->L,o,LoadString(S)); 120 | break; 121 | default: lua_assert(0); 122 | } 123 | } 124 | n=LoadInt(S); 125 | f->p=luaM_newvector(S->L,n,Proto*); 126 | f->sizep=n; 127 | for (i=0; ip[i]=NULL; 128 | for (i=0; ip[i]=luaF_newproto(S->L); 131 | LoadFunction(S,f->p[i]); 132 | } 133 | } 134 | 135 | static void LoadUpvalues(LoadState* S, Proto* f) 136 | { 137 | int i,n; 138 | n=LoadInt(S); 139 | f->upvalues=luaM_newvector(S->L,n,Upvaldesc); 140 | f->sizeupvalues=n; 141 | for (i=0; iupvalues[i].name=NULL; 142 | for (i=0; iupvalues[i].instack=LoadByte(S); 145 | f->upvalues[i].idx=LoadByte(S); 146 | } 147 | } 148 | 149 | static void LoadDebug(LoadState* S, Proto* f) 150 | { 151 | int i,n; 152 | f->source=LoadString(S); 153 | n=LoadInt(S); 154 | f->lineinfo=luaM_newvector(S->L,n,int); 155 | f->sizelineinfo=n; 156 | LoadVector(S,f->lineinfo,n,sizeof(int)); 157 | n=LoadInt(S); 158 | f->locvars=luaM_newvector(S->L,n,LocVar); 159 | f->sizelocvars=n; 160 | for (i=0; ilocvars[i].varname=NULL; 161 | for (i=0; ilocvars[i].varname=LoadString(S); 164 | f->locvars[i].startpc=LoadInt(S); 165 | f->locvars[i].endpc=LoadInt(S); 166 | } 167 | n=LoadInt(S); 168 | for (i=0; iupvalues[i].name=LoadString(S); 169 | } 170 | 171 | static void LoadFunction(LoadState* S, Proto* f) 172 | { 173 | f->linedefined=LoadInt(S); 174 | f->lastlinedefined=LoadInt(S); 175 | f->numparams=LoadByte(S); 176 | f->is_vararg=LoadByte(S); 177 | f->maxstacksize=LoadByte(S); 178 | LoadCode(S,f); 179 | LoadConstants(S,f); 180 | LoadUpvalues(S,f); 181 | LoadDebug(S,f); 182 | } 183 | 184 | /* the code below must be consistent with the code in luaU_header */ 185 | #define N0 LUAC_HEADERSIZE 186 | #define N1 (sizeof(LUA_SIGNATURE)-sizeof(char)) 187 | #define N2 N1+2 188 | #define N3 N2+6 189 | 190 | static void LoadHeader(LoadState* S) 191 | { 192 | lu_byte h[LUAC_HEADERSIZE]; 193 | lu_byte s[LUAC_HEADERSIZE]; 194 | luaU_header(h); 195 | memcpy(s,h,sizeof(char)); /* first char already read */ 196 | LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char)); 197 | if (memcmp(h,s,N0)==0) return; 198 | if (memcmp(h,s,N1)!=0) error(S,"not a"); 199 | if (memcmp(h,s,N2)!=0) error(S,"version mismatch in"); 200 | if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted"); 201 | } 202 | 203 | /* 204 | ** load precompiled chunk 205 | */ 206 | Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 207 | { 208 | LoadState S; 209 | Closure* cl; 210 | if (*name=='@' || *name=='=') 211 | S.name=name+1; 212 | else if (*name==LUA_SIGNATURE[0]) 213 | S.name="binary string"; 214 | else 215 | S.name=name; 216 | S.L=L; 217 | S.Z=Z; 218 | S.b=buff; 219 | LoadHeader(&S); 220 | cl=luaF_newLclosure(L,1); 221 | setclLvalue(L,L->top,cl); incr_top(L); 222 | cl->l.p=luaF_newproto(L); 223 | LoadFunction(&S,cl->l.p); 224 | if (cl->l.p->sizeupvalues != 1) 225 | { 226 | Proto* p=cl->l.p; 227 | cl=luaF_newLclosure(L,cl->l.p->sizeupvalues); 228 | cl->l.p=p; 229 | setclLvalue(L,L->top-1,cl); 230 | } 231 | luai_verifycode(L,buff,cl->l.p); 232 | return cl; 233 | } 234 | 235 | #define MYINT(s) (s[0]-'0') 236 | #define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR) 237 | #define FORMAT 0 /* this is the official format */ 238 | 239 | /* 240 | * make header for precompiled chunks 241 | * if you change the code below be sure to update LoadHeader and FORMAT above 242 | * and LUAC_HEADERSIZE in lundump.h 243 | */ 244 | void luaU_header (lu_byte* h) 245 | { 246 | int x=1; 247 | memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char)); 248 | h+=sizeof(LUA_SIGNATURE)-sizeof(char); 249 | *h++=cast_byte(VERSION); 250 | *h++=cast_byte(FORMAT); 251 | *h++=cast_byte(*(char*)&x); /* endianness */ 252 | *h++=cast_byte(sizeof(int)); 253 | *h++=cast_byte(sizeof(size_t)); 254 | *h++=cast_byte(sizeof(Instruction)); 255 | *h++=cast_byte(sizeof(lua_Number)); 256 | *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */ 257 | memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char)); 258 | } 259 | -------------------------------------------------------------------------------- /src/img_ppm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "img_ppm.h" 7 | 8 | #define LINEMAX 128 9 | 10 | static char* 11 | readline(FILE* f, char* buffer) { 12 | for (;;) { 13 | char* ret = fgets(buffer, LINEMAX, f); 14 | if (ret == NULL) { 15 | return NULL; 16 | } 17 | if (ret[0] != '#') { 18 | return ret; 19 | } 20 | } 21 | } 22 | 23 | static int 24 | ppm_header(FILE* f, struct ppm* ppm) { 25 | char tmp[LINEMAX]; 26 | 27 | char* line = readline(f, tmp); 28 | if (line == NULL) 29 | return 0; 30 | char c = 0; 31 | sscanf(line, "P%c", &c); 32 | ppm->type = c; 33 | 34 | line = readline(f, tmp); 35 | if (line == NULL) 36 | return 0; 37 | sscanf(line, "%d %d", &(ppm->width), &(ppm->height)); 38 | 39 | line = readline(f, tmp); 40 | if (line == NULL) 41 | return 0; 42 | sscanf(line, "%d", &(ppm->depth)); 43 | 44 | return 1; 45 | } 46 | 47 | static int 48 | ppm_data(struct ppm* ppm, FILE* f, int id, int skip) { 49 | int i; 50 | int n = ppm->width * ppm->height; 51 | uint8_t* buffer = ppm->buffer + skip; 52 | uint8_t* tmp; 53 | int step = ppm->step; 54 | switch(id) { 55 | case '3': // RGB text 56 | for (i=0;ibuffer = NULL; 104 | ppm->step = 0; 105 | int rgb_id = 0; 106 | int alpha_id = 0; 107 | if (rgb) { 108 | if (!ppm_header(rgb, ppm)) { 109 | return 0; 110 | } 111 | rgb_id = ppm->type; 112 | ppm->step += 3; 113 | } 114 | if (alpha) { 115 | if (rgb == NULL) { 116 | if (!ppm_header(alpha, ppm)) { 117 | return 0; 118 | } 119 | alpha_id = ppm->type; 120 | } else { 121 | struct ppm pgm; 122 | if (!ppm_header(alpha, &pgm)) { 123 | return 0; 124 | } 125 | if (ppm->depth != pgm.depth || ppm->width != pgm.width || ppm->height != pgm.height) { 126 | return 0; 127 | } 128 | alpha_id = pgm.type; 129 | } 130 | ppm->step += 1; 131 | } 132 | ppm->buffer = (uint8_t*)malloc(ppm->height * ppm->width * ppm->step); 133 | if (rgb) { 134 | if (!ppm_data(ppm, rgb, rgb_id, 0)) { 135 | return 0; 136 | } 137 | } 138 | if (alpha) { 139 | int skip = 0; 140 | if (rgb) { 141 | skip = 3; 142 | } 143 | if (!ppm_data(ppm, alpha, alpha_id, skip)) { 144 | return 0; 145 | } 146 | } 147 | 148 | return 1; 149 | } 150 | 151 | int 152 | img_loadppm(const char* filename, struct ppm* ppm) { 153 | char* tmp = malloc(sizeof(char)* (strlen(filename) + 5)); 154 | sprintf(tmp, "%s.ppm", filename); 155 | FILE *rgb = fopen(tmp, "rb"); 156 | sprintf(tmp, "%s.pgm", filename); 157 | FILE *alpha = fopen(tmp, "rb"); 158 | free(tmp); 159 | 160 | if (rgb == NULL && alpha == NULL) { 161 | return 0; 162 | } 163 | 164 | int ok = loadppm_from_file(rgb, alpha, ppm); 165 | 166 | if (rgb) { 167 | fclose(rgb); 168 | } 169 | if (alpha) { 170 | fclose(alpha); 171 | } 172 | 173 | if (!ok) { 174 | if (ppm->buffer) { 175 | free(ppm->buffer); 176 | } 177 | return 0; 178 | } 179 | 180 | if (ppm->depth == 255) { 181 | if (ppm->step == 4) { 182 | ppm->type = PPM_RGBA8; 183 | } 184 | else if (ppm->step == 3) { 185 | ppm->type = PPM_RGB8; 186 | } 187 | else { 188 | ppm->type = PPM_ALPHA8; 189 | } 190 | } 191 | else { 192 | if (ppm->step == 4) { 193 | ppm->type = PPM_RGBA4; 194 | } 195 | else if (ppm->step == 3) { 196 | ppm->type = PPM_RGB4; 197 | } 198 | else { 199 | ppm->type = PPM_ALPHA4; 200 | } 201 | } 202 | 203 | return 1; 204 | } 205 | 206 | static void 207 | ppm_type(int format, struct ppm* ppm) { 208 | switch (format) { 209 | case PPM_RGBA8: 210 | ppm->type = PPM_RGBA8; 211 | ppm->depth = 255; 212 | ppm->step = 4; 213 | break; 214 | 215 | case PPM_RGB8: 216 | ppm->type = PPM_RGB8; 217 | ppm->depth = 255; 218 | ppm->step = 3; 219 | break; 220 | 221 | case PPM_RGBA4: 222 | ppm->type = PPM_RGBA4; 223 | ppm->depth = 15; 224 | ppm->step = 4; 225 | break; 226 | 227 | case PPM_RGB4: 228 | ppm->type = PPM_RGB4; 229 | ppm->depth = 15; 230 | ppm->step = 3; 231 | break; 232 | 233 | case PPM_ALPHA8: 234 | ppm->type = PPM_ALPHA8; 235 | ppm->depth = 255; 236 | ppm->step = 1; 237 | break; 238 | 239 | case PPM_ALPHA4: 240 | ppm->type = PPM_ALPHA4; 241 | ppm->depth = 15; 242 | ppm->step = 1; 243 | break; 244 | 245 | default: 246 | ppm->type = PPM_UNKNOWN; 247 | ppm->depth = 0; 248 | ppm->step = 0; 249 | break; 250 | } 251 | } 252 | 253 | static int 254 | save_rgb(const char* filename, struct ppm ppm) { 255 | char* tmp = malloc(sizeof(char)* (strlen(filename) + 5)); 256 | sprintf(tmp, "%s.ppm", filename); 257 | FILE *f = fopen(tmp,"wb"); 258 | free(tmp); 259 | if (f == NULL) { 260 | return 0; 261 | } 262 | 263 | fprintf(f, 264 | "P6\n" 265 | "%d %d\n" 266 | "%d\n" 267 | , ppm.width, ppm.height, ppm.depth); 268 | 269 | uint8_t* buffer = (uint8_t*)malloc(ppm.width * ppm.height * 3); 270 | for (int i=0; i 9 | #include 10 | 11 | #define lmathlib_c 12 | #define LUA_LIB 13 | 14 | #include "lua.h" 15 | 16 | #include "lauxlib.h" 17 | #include "lualib.h" 18 | 19 | 20 | #undef PI 21 | #define PI ((lua_Number)(3.1415926535897932384626433832795)) 22 | #define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0)) 23 | 24 | 25 | 26 | static int math_abs (lua_State *L) { 27 | lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); 28 | return 1; 29 | } 30 | 31 | static int math_sin (lua_State *L) { 32 | lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1))); 33 | return 1; 34 | } 35 | 36 | static int math_sinh (lua_State *L) { 37 | lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); 38 | return 1; 39 | } 40 | 41 | static int math_cos (lua_State *L) { 42 | lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); 43 | return 1; 44 | } 45 | 46 | static int math_cosh (lua_State *L) { 47 | lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); 48 | return 1; 49 | } 50 | 51 | static int math_tan (lua_State *L) { 52 | lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); 53 | return 1; 54 | } 55 | 56 | static int math_tanh (lua_State *L) { 57 | lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); 58 | return 1; 59 | } 60 | 61 | static int math_asin (lua_State *L) { 62 | lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); 63 | return 1; 64 | } 65 | 66 | static int math_acos (lua_State *L) { 67 | lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); 68 | return 1; 69 | } 70 | 71 | static int math_atan (lua_State *L) { 72 | lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1))); 73 | return 1; 74 | } 75 | 76 | static int math_atan2 (lua_State *L) { 77 | lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1), 78 | luaL_checknumber(L, 2))); 79 | return 1; 80 | } 81 | 82 | static int math_ceil (lua_State *L) { 83 | lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1))); 84 | return 1; 85 | } 86 | 87 | static int math_floor (lua_State *L) { 88 | lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); 89 | return 1; 90 | } 91 | 92 | static int math_fmod (lua_State *L) { 93 | lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), 94 | luaL_checknumber(L, 2))); 95 | return 1; 96 | } 97 | 98 | static int math_modf (lua_State *L) { 99 | lua_Number ip; 100 | lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip); 101 | lua_pushnumber(L, ip); 102 | lua_pushnumber(L, fp); 103 | return 2; 104 | } 105 | 106 | static int math_sqrt (lua_State *L) { 107 | lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); 108 | return 1; 109 | } 110 | 111 | static int math_pow (lua_State *L) { 112 | lua_Number x = luaL_checknumber(L, 1); 113 | lua_Number y = luaL_checknumber(L, 2); 114 | lua_pushnumber(L, l_mathop(pow)(x, y)); 115 | return 1; 116 | } 117 | 118 | static int math_log (lua_State *L) { 119 | lua_Number x = luaL_checknumber(L, 1); 120 | lua_Number res; 121 | if (lua_isnoneornil(L, 2)) 122 | res = l_mathop(log)(x); 123 | else { 124 | lua_Number base = luaL_checknumber(L, 2); 125 | if (base == (lua_Number)10.0) res = l_mathop(log10)(x); 126 | else res = l_mathop(log)(x)/l_mathop(log)(base); 127 | } 128 | lua_pushnumber(L, res); 129 | return 1; 130 | } 131 | 132 | #if defined(LUA_COMPAT_LOG10) 133 | static int math_log10 (lua_State *L) { 134 | lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); 135 | return 1; 136 | } 137 | #endif 138 | 139 | static int math_exp (lua_State *L) { 140 | lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); 141 | return 1; 142 | } 143 | 144 | static int math_deg (lua_State *L) { 145 | lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); 146 | return 1; 147 | } 148 | 149 | static int math_rad (lua_State *L) { 150 | lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); 151 | return 1; 152 | } 153 | 154 | static int math_frexp (lua_State *L) { 155 | int e; 156 | lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); 157 | lua_pushinteger(L, e); 158 | return 2; 159 | } 160 | 161 | static int math_ldexp (lua_State *L) { 162 | lua_Number x = luaL_checknumber(L, 1); 163 | int ep = luaL_checkint(L, 2); 164 | lua_pushnumber(L, l_mathop(ldexp)(x, ep)); 165 | return 1; 166 | } 167 | 168 | 169 | 170 | static int math_min (lua_State *L) { 171 | int n = lua_gettop(L); /* number of arguments */ 172 | lua_Number dmin = luaL_checknumber(L, 1); 173 | int i; 174 | for (i=2; i<=n; i++) { 175 | lua_Number d = luaL_checknumber(L, i); 176 | if (d < dmin) 177 | dmin = d; 178 | } 179 | lua_pushnumber(L, dmin); 180 | return 1; 181 | } 182 | 183 | 184 | static int math_max (lua_State *L) { 185 | int n = lua_gettop(L); /* number of arguments */ 186 | lua_Number dmax = luaL_checknumber(L, 1); 187 | int i; 188 | for (i=2; i<=n; i++) { 189 | lua_Number d = luaL_checknumber(L, i); 190 | if (d > dmax) 191 | dmax = d; 192 | } 193 | lua_pushnumber(L, dmax); 194 | return 1; 195 | } 196 | 197 | 198 | static int math_random (lua_State *L) { 199 | /* the `%' avoids the (rare) case of r==1, and is needed also because on 200 | some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ 201 | lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; 202 | switch (lua_gettop(L)) { /* check number of arguments */ 203 | case 0: { /* no arguments */ 204 | lua_pushnumber(L, r); /* Number between 0 and 1 */ 205 | break; 206 | } 207 | case 1: { /* only upper limit */ 208 | lua_Number u = luaL_checknumber(L, 1); 209 | luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty"); 210 | lua_pushnumber(L, l_mathop(floor)(r*u) + (lua_Number)(1.0)); /* [1, u] */ 211 | break; 212 | } 213 | case 2: { /* lower and upper limits */ 214 | lua_Number l = luaL_checknumber(L, 1); 215 | lua_Number u = luaL_checknumber(L, 2); 216 | luaL_argcheck(L, l <= u, 2, "interval is empty"); 217 | lua_pushnumber(L, l_mathop(floor)(r*(u-l+1)) + l); /* [l, u] */ 218 | break; 219 | } 220 | default: return luaL_error(L, "wrong number of arguments"); 221 | } 222 | return 1; 223 | } 224 | 225 | 226 | static int math_randomseed (lua_State *L) { 227 | srand(luaL_checkunsigned(L, 1)); 228 | (void)rand(); /* discard first value to avoid undesirable correlations */ 229 | return 0; 230 | } 231 | 232 | 233 | static const luaL_Reg mathlib[] = { 234 | {"abs", math_abs}, 235 | {"acos", math_acos}, 236 | {"asin", math_asin}, 237 | {"atan2", math_atan2}, 238 | {"atan", math_atan}, 239 | {"ceil", math_ceil}, 240 | {"cosh", math_cosh}, 241 | {"cos", math_cos}, 242 | {"deg", math_deg}, 243 | {"exp", math_exp}, 244 | {"floor", math_floor}, 245 | {"fmod", math_fmod}, 246 | {"frexp", math_frexp}, 247 | {"ldexp", math_ldexp}, 248 | #if defined(LUA_COMPAT_LOG10) 249 | {"log10", math_log10}, 250 | #endif 251 | {"log", math_log}, 252 | {"max", math_max}, 253 | {"min", math_min}, 254 | {"modf", math_modf}, 255 | {"pow", math_pow}, 256 | {"rad", math_rad}, 257 | {"random", math_random}, 258 | {"randomseed", math_randomseed}, 259 | {"sinh", math_sinh}, 260 | {"sin", math_sin}, 261 | {"sqrt", math_sqrt}, 262 | {"tanh", math_tanh}, 263 | {"tan", math_tan}, 264 | {NULL, NULL} 265 | }; 266 | 267 | 268 | /* 269 | ** Open math library 270 | */ 271 | LUAMOD_API int luaopen_math (lua_State *L) { 272 | luaL_newlib(L, mathlib); 273 | lua_pushnumber(L, PI); 274 | lua_setfield(L, -2, "pi"); 275 | lua_pushnumber(L, HUGE_VAL); 276 | lua_setfield(L, -2, "huge"); 277 | return 1; 278 | } 279 | 280 | -------------------------------------------------------------------------------- /3rd/zlib/zutil.h: -------------------------------------------------------------------------------- 1 | /* zutil.h -- internal interface and configuration of the compression library 2 | * Copyright (C) 1995-2013 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* @(#) $Id$ */ 12 | 13 | #ifndef ZUTIL_H 14 | #define ZUTIL_H 15 | 16 | #ifdef HAVE_HIDDEN 17 | # define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) 18 | #else 19 | # define ZLIB_INTERNAL 20 | #endif 21 | 22 | #include "zlib.h" 23 | 24 | #if defined(STDC) && !defined(Z_SOLO) 25 | # if !(defined(_WIN32_WCE) && defined(_MSC_VER)) 26 | # include 27 | # endif 28 | # include 29 | # include 30 | #endif 31 | 32 | #ifdef Z_SOLO 33 | typedef long ptrdiff_t; /* guess -- will be caught if guess is wrong */ 34 | #endif 35 | 36 | #ifndef local 37 | # define local static 38 | #endif 39 | /* compile with -Dlocal if your debugger can't find static symbols */ 40 | 41 | typedef unsigned char uch; 42 | typedef uch FAR uchf; 43 | typedef unsigned short ush; 44 | typedef ush FAR ushf; 45 | typedef unsigned long ulg; 46 | 47 | extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ 48 | /* (size given to avoid silly warnings with Visual C++) */ 49 | 50 | #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)] 51 | 52 | #define ERR_RETURN(strm,err) \ 53 | return (strm->msg = ERR_MSG(err), (err)) 54 | /* To be used only when the state is known to be valid */ 55 | 56 | /* common constants */ 57 | 58 | #ifndef DEF_WBITS 59 | # define DEF_WBITS MAX_WBITS 60 | #endif 61 | /* default windowBits for decompression. MAX_WBITS is for compression only */ 62 | 63 | #if MAX_MEM_LEVEL >= 8 64 | # define DEF_MEM_LEVEL 8 65 | #else 66 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 67 | #endif 68 | /* default memLevel */ 69 | 70 | #define STORED_BLOCK 0 71 | #define STATIC_TREES 1 72 | #define DYN_TREES 2 73 | /* The three kinds of block type */ 74 | 75 | #define MIN_MATCH 3 76 | #define MAX_MATCH 258 77 | /* The minimum and maximum match lengths */ 78 | 79 | #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */ 80 | 81 | /* target dependencies */ 82 | 83 | #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32)) 84 | # define OS_CODE 0x00 85 | # ifndef Z_SOLO 86 | # if defined(__TURBOC__) || defined(__BORLANDC__) 87 | # if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) 88 | /* Allow compilation with ANSI keywords only enabled */ 89 | void _Cdecl farfree( void *block ); 90 | void *_Cdecl farmalloc( unsigned long nbytes ); 91 | # else 92 | # include 93 | # endif 94 | # else /* MSC or DJGPP */ 95 | # include 96 | # endif 97 | # endif 98 | #endif 99 | 100 | #ifdef AMIGA 101 | # define OS_CODE 0x01 102 | #endif 103 | 104 | #if defined(VAXC) || defined(VMS) 105 | # define OS_CODE 0x02 106 | # define F_OPEN(name, mode) \ 107 | fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512") 108 | #endif 109 | 110 | #if defined(ATARI) || defined(atarist) 111 | # define OS_CODE 0x05 112 | #endif 113 | 114 | #ifdef OS2 115 | # define OS_CODE 0x06 116 | # if defined(M_I86) && !defined(Z_SOLO) 117 | # include 118 | # endif 119 | #endif 120 | 121 | #if defined(MACOS) || defined(TARGET_OS_MAC) 122 | # define OS_CODE 0x07 123 | # ifndef Z_SOLO 124 | # if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os 125 | # include /* for fdopen */ 126 | # else 127 | # ifndef fdopen 128 | # define fdopen(fd,mode) NULL /* No fdopen() */ 129 | # endif 130 | # endif 131 | # endif 132 | #endif 133 | 134 | #ifdef TOPS20 135 | # define OS_CODE 0x0a 136 | #endif 137 | 138 | #ifdef WIN32 139 | # ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */ 140 | # define OS_CODE 0x0b 141 | # endif 142 | #endif 143 | 144 | #ifdef __50SERIES /* Prime/PRIMOS */ 145 | # define OS_CODE 0x0f 146 | #endif 147 | 148 | #if defined(_BEOS_) || defined(RISCOS) 149 | # define fdopen(fd,mode) NULL /* No fdopen() */ 150 | #endif 151 | 152 | #if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX 153 | # if defined(_WIN32_WCE) 154 | # define fdopen(fd,mode) NULL /* No fdopen() */ 155 | # ifndef _PTRDIFF_T_DEFINED 156 | typedef int ptrdiff_t; 157 | # define _PTRDIFF_T_DEFINED 158 | # endif 159 | # else 160 | # define fdopen(fd,type) _fdopen(fd,type) 161 | # endif 162 | #endif 163 | 164 | #if defined(__BORLANDC__) && !defined(MSDOS) 165 | #pragma warn -8004 166 | #pragma warn -8008 167 | #pragma warn -8066 168 | #endif 169 | 170 | /* provide prototypes for these when building zlib without LFS */ 171 | #if !defined(_WIN32) && \ 172 | (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0) 173 | ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); 174 | ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); 175 | #endif 176 | 177 | /* common defaults */ 178 | 179 | #ifndef OS_CODE 180 | # define OS_CODE 0x03 /* assume Unix */ 181 | #endif 182 | 183 | #ifndef F_OPEN 184 | # define F_OPEN(name, mode) fopen((name), (mode)) 185 | #endif 186 | 187 | /* functions */ 188 | 189 | #if defined(pyr) || defined(Z_SOLO) 190 | # define NO_MEMCPY 191 | #endif 192 | #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__) 193 | /* Use our own functions for small and medium model with MSC <= 5.0. 194 | * You may have to use the same strategy for Borland C (untested). 195 | * The __SC__ check is for Symantec. 196 | */ 197 | # define NO_MEMCPY 198 | #endif 199 | #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY) 200 | # define HAVE_MEMCPY 201 | #endif 202 | #ifdef HAVE_MEMCPY 203 | # ifdef SMALL_MEDIUM /* MSDOS small or medium model */ 204 | # define zmemcpy _fmemcpy 205 | # define zmemcmp _fmemcmp 206 | # define zmemzero(dest, len) _fmemset(dest, 0, len) 207 | # else 208 | # define zmemcpy memcpy 209 | # define zmemcmp memcmp 210 | # define zmemzero(dest, len) memset(dest, 0, len) 211 | # endif 212 | #else 213 | void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len)); 214 | int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len)); 215 | void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len)); 216 | #endif 217 | 218 | /* Diagnostic functions */ 219 | #ifdef DEBUG 220 | # include 221 | extern int ZLIB_INTERNAL z_verbose; 222 | extern void ZLIB_INTERNAL z_error OF((char *m)); 223 | # define Assert(cond,msg) {if(!(cond)) z_error(msg);} 224 | # define Trace(x) {if (z_verbose>=0) fprintf x ;} 225 | # define Tracev(x) {if (z_verbose>0) fprintf x ;} 226 | # define Tracevv(x) {if (z_verbose>1) fprintf x ;} 227 | # define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;} 228 | # define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;} 229 | #else 230 | # define Assert(cond,msg) 231 | # define Trace(x) 232 | # define Tracev(x) 233 | # define Tracevv(x) 234 | # define Tracec(c,x) 235 | # define Tracecv(c,x) 236 | #endif 237 | 238 | #ifndef Z_SOLO 239 | voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items, 240 | unsigned size)); 241 | void ZLIB_INTERNAL zcfree OF((voidpf opaque, voidpf ptr)); 242 | #endif 243 | 244 | #define ZALLOC(strm, items, size) \ 245 | (*((strm)->zalloc))((strm)->opaque, (items), (size)) 246 | #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) 247 | #define TRY_FREE(s, p) {if (p) ZFREE(s, p);} 248 | 249 | /* Reverse the bytes in a 32-bit value */ 250 | #define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ 251 | (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) 252 | 253 | #endif /* ZUTIL_H */ 254 | -------------------------------------------------------------------------------- /3rd/libpng/pnglibconf.h: -------------------------------------------------------------------------------- 1 | /* libpng 1.6.12 STANDARD API DEFINITION */ 2 | 3 | /* pnglibconf.h - library build configuration */ 4 | 5 | /* Libpng version 1.6.12 - June 12, 2014 */ 6 | 7 | /* Copyright (c) 1998-2014 Glenn Randers-Pehrson */ 8 | 9 | /* This code is released under the libpng license. */ 10 | /* For conditions of distribution and use, see the disclaimer */ 11 | /* and license in png.h */ 12 | 13 | /* pnglibconf.h */ 14 | /* Machine generated file: DO NOT EDIT */ 15 | /* Derived from: scripts/pnglibconf.dfa */ 16 | #ifndef PNGLCONF_H 17 | #define PNGLCONF_H 18 | /* options */ 19 | #define PNG_16BIT_SUPPORTED 20 | #define PNG_ALIGNED_MEMORY_SUPPORTED 21 | /*#undef PNG_ARM_NEON_API_SUPPORTED*/ 22 | /*#undef PNG_ARM_NEON_CHECK_SUPPORTED*/ 23 | #define PNG_BENIGN_ERRORS_SUPPORTED 24 | #define PNG_BENIGN_READ_ERRORS_SUPPORTED 25 | /*#undef PNG_BENIGN_WRITE_ERRORS_SUPPORTED*/ 26 | #define PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED 27 | #define PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED 28 | #define PNG_COLORSPACE_SUPPORTED 29 | #define PNG_CONSOLE_IO_SUPPORTED 30 | #define PNG_CONVERT_tIME_SUPPORTED 31 | #define PNG_EASY_ACCESS_SUPPORTED 32 | /*#undef PNG_ERROR_NUMBERS_SUPPORTED*/ 33 | #define PNG_ERROR_TEXT_SUPPORTED 34 | #define PNG_FIXED_POINT_SUPPORTED 35 | #define PNG_FLOATING_ARITHMETIC_SUPPORTED 36 | #define PNG_FLOATING_POINT_SUPPORTED 37 | #define PNG_FORMAT_AFIRST_SUPPORTED 38 | #define PNG_FORMAT_BGR_SUPPORTED 39 | #define PNG_GAMMA_SUPPORTED 40 | #define PNG_GET_PALETTE_MAX_SUPPORTED 41 | #define PNG_HANDLE_AS_UNKNOWN_SUPPORTED 42 | #define PNG_INCH_CONVERSIONS_SUPPORTED 43 | #define PNG_INFO_IMAGE_SUPPORTED 44 | #define PNG_IO_STATE_SUPPORTED 45 | #define PNG_MNG_FEATURES_SUPPORTED 46 | #define PNG_POINTER_INDEXING_SUPPORTED 47 | #define PNG_PROGRESSIVE_READ_SUPPORTED 48 | #define PNG_READ_16BIT_SUPPORTED 49 | #define PNG_READ_ALPHA_MODE_SUPPORTED 50 | #define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED 51 | #define PNG_READ_BACKGROUND_SUPPORTED 52 | #define PNG_READ_BGR_SUPPORTED 53 | #define PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED 54 | #define PNG_READ_COMPOSITE_NODIV_SUPPORTED 55 | #define PNG_READ_COMPRESSED_TEXT_SUPPORTED 56 | #define PNG_READ_EXPAND_16_SUPPORTED 57 | #define PNG_READ_EXPAND_SUPPORTED 58 | #define PNG_READ_FILLER_SUPPORTED 59 | #define PNG_READ_GAMMA_SUPPORTED 60 | #define PNG_READ_GET_PALETTE_MAX_SUPPORTED 61 | #define PNG_READ_GRAY_TO_RGB_SUPPORTED 62 | #define PNG_READ_INTERLACING_SUPPORTED 63 | #define PNG_READ_INT_FUNCTIONS_SUPPORTED 64 | #define PNG_READ_INVERT_ALPHA_SUPPORTED 65 | #define PNG_READ_INVERT_SUPPORTED 66 | #define PNG_READ_OPT_PLTE_SUPPORTED 67 | #define PNG_READ_PACKSWAP_SUPPORTED 68 | #define PNG_READ_PACK_SUPPORTED 69 | #define PNG_READ_QUANTIZE_SUPPORTED 70 | #define PNG_READ_RGB_TO_GRAY_SUPPORTED 71 | #define PNG_READ_SCALE_16_TO_8_SUPPORTED 72 | #define PNG_READ_SHIFT_SUPPORTED 73 | #define PNG_READ_STRIP_16_TO_8_SUPPORTED 74 | #define PNG_READ_STRIP_ALPHA_SUPPORTED 75 | #define PNG_READ_SUPPORTED 76 | #define PNG_READ_SWAP_ALPHA_SUPPORTED 77 | #define PNG_READ_SWAP_SUPPORTED 78 | #define PNG_READ_TEXT_SUPPORTED 79 | #define PNG_READ_TRANSFORMS_SUPPORTED 80 | #define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED 81 | #define PNG_READ_USER_CHUNKS_SUPPORTED 82 | #define PNG_READ_USER_TRANSFORM_SUPPORTED 83 | #define PNG_READ_bKGD_SUPPORTED 84 | #define PNG_READ_cHRM_SUPPORTED 85 | #define PNG_READ_gAMA_SUPPORTED 86 | #define PNG_READ_hIST_SUPPORTED 87 | #define PNG_READ_iCCP_SUPPORTED 88 | #define PNG_READ_iTXt_SUPPORTED 89 | #define PNG_READ_oFFs_SUPPORTED 90 | #define PNG_READ_pCAL_SUPPORTED 91 | #define PNG_READ_pHYs_SUPPORTED 92 | #define PNG_READ_sBIT_SUPPORTED 93 | #define PNG_READ_sCAL_SUPPORTED 94 | #define PNG_READ_sPLT_SUPPORTED 95 | #define PNG_READ_sRGB_SUPPORTED 96 | #define PNG_READ_tEXt_SUPPORTED 97 | #define PNG_READ_tIME_SUPPORTED 98 | #define PNG_READ_tRNS_SUPPORTED 99 | #define PNG_READ_zTXt_SUPPORTED 100 | /*#undef PNG_SAFE_LIMITS_SUPPORTED*/ 101 | #define PNG_SAVE_INT_32_SUPPORTED 102 | #define PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED 103 | #define PNG_SEQUENTIAL_READ_SUPPORTED 104 | #define PNG_SETJMP_SUPPORTED 105 | #define PNG_SET_CHUNK_CACHE_LIMIT_SUPPORTED 106 | #define PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED 107 | #define PNG_SET_OPTION_SUPPORTED 108 | #define PNG_SET_UNKNOWN_CHUNKS_SUPPORTED 109 | #define PNG_SET_USER_LIMITS_SUPPORTED 110 | #define PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED 111 | #define PNG_SIMPLIFIED_READ_BGR_SUPPORTED 112 | #define PNG_SIMPLIFIED_READ_SUPPORTED 113 | #define PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED 114 | #define PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED 115 | #define PNG_SIMPLIFIED_WRITE_SUPPORTED 116 | #define PNG_STDIO_SUPPORTED 117 | #define PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED 118 | #define PNG_TEXT_SUPPORTED 119 | #define PNG_TIME_RFC1123_SUPPORTED 120 | #define PNG_UNKNOWN_CHUNKS_SUPPORTED 121 | #define PNG_USER_CHUNKS_SUPPORTED 122 | #define PNG_USER_LIMITS_SUPPORTED 123 | #define PNG_USER_MEM_SUPPORTED 124 | #define PNG_USER_TRANSFORM_INFO_SUPPORTED 125 | #define PNG_USER_TRANSFORM_PTR_SUPPORTED 126 | #define PNG_WARNINGS_SUPPORTED 127 | #define PNG_WRITE_16BIT_SUPPORTED 128 | #define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED 129 | #define PNG_WRITE_BGR_SUPPORTED 130 | #define PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED 131 | #define PNG_WRITE_COMPRESSED_TEXT_SUPPORTED 132 | #define PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED 133 | #define PNG_WRITE_FILLER_SUPPORTED 134 | #define PNG_WRITE_FILTER_SUPPORTED 135 | #define PNG_WRITE_FLUSH_SUPPORTED 136 | #define PNG_WRITE_GET_PALETTE_MAX_SUPPORTED 137 | #define PNG_WRITE_INTERLACING_SUPPORTED 138 | #define PNG_WRITE_INT_FUNCTIONS_SUPPORTED 139 | #define PNG_WRITE_INVERT_ALPHA_SUPPORTED 140 | #define PNG_WRITE_INVERT_SUPPORTED 141 | #define PNG_WRITE_OPTIMIZE_CMF_SUPPORTED 142 | #define PNG_WRITE_PACKSWAP_SUPPORTED 143 | #define PNG_WRITE_PACK_SUPPORTED 144 | #define PNG_WRITE_SHIFT_SUPPORTED 145 | #define PNG_WRITE_SUPPORTED 146 | #define PNG_WRITE_SWAP_ALPHA_SUPPORTED 147 | #define PNG_WRITE_SWAP_SUPPORTED 148 | #define PNG_WRITE_TEXT_SUPPORTED 149 | #define PNG_WRITE_TRANSFORMS_SUPPORTED 150 | #define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED 151 | #define PNG_WRITE_USER_TRANSFORM_SUPPORTED 152 | #define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED 153 | #define PNG_WRITE_bKGD_SUPPORTED 154 | #define PNG_WRITE_cHRM_SUPPORTED 155 | #define PNG_WRITE_gAMA_SUPPORTED 156 | #define PNG_WRITE_hIST_SUPPORTED 157 | #define PNG_WRITE_iCCP_SUPPORTED 158 | #define PNG_WRITE_iTXt_SUPPORTED 159 | #define PNG_WRITE_oFFs_SUPPORTED 160 | #define PNG_WRITE_pCAL_SUPPORTED 161 | #define PNG_WRITE_pHYs_SUPPORTED 162 | #define PNG_WRITE_sBIT_SUPPORTED 163 | #define PNG_WRITE_sCAL_SUPPORTED 164 | #define PNG_WRITE_sPLT_SUPPORTED 165 | #define PNG_WRITE_sRGB_SUPPORTED 166 | #define PNG_WRITE_tEXt_SUPPORTED 167 | #define PNG_WRITE_tIME_SUPPORTED 168 | #define PNG_WRITE_tRNS_SUPPORTED 169 | #define PNG_WRITE_zTXt_SUPPORTED 170 | #define PNG_bKGD_SUPPORTED 171 | #define PNG_cHRM_SUPPORTED 172 | #define PNG_gAMA_SUPPORTED 173 | #define PNG_hIST_SUPPORTED 174 | #define PNG_iCCP_SUPPORTED 175 | #define PNG_iTXt_SUPPORTED 176 | #define PNG_oFFs_SUPPORTED 177 | #define PNG_pCAL_SUPPORTED 178 | #define PNG_pHYs_SUPPORTED 179 | #define PNG_sBIT_SUPPORTED 180 | #define PNG_sCAL_SUPPORTED 181 | #define PNG_sPLT_SUPPORTED 182 | #define PNG_sRGB_SUPPORTED 183 | #define PNG_tEXt_SUPPORTED 184 | #define PNG_tIME_SUPPORTED 185 | #define PNG_tRNS_SUPPORTED 186 | #define PNG_zTXt_SUPPORTED 187 | /* end of options */ 188 | /* settings */ 189 | #define PNG_API_RULE 0 190 | #define PNG_COST_SHIFT 3 191 | #define PNG_DEFAULT_READ_MACROS 1 192 | #define PNG_GAMMA_THRESHOLD_FIXED 5000 193 | #define PNG_IDAT_READ_SIZE PNG_ZBUF_SIZE 194 | #define PNG_INFLATE_BUF_SIZE 1024 195 | #define PNG_MAX_GAMMA_8 11 196 | #define PNG_QUANTIZE_BLUE_BITS 5 197 | #define PNG_QUANTIZE_GREEN_BITS 5 198 | #define PNG_QUANTIZE_RED_BITS 5 199 | #define PNG_TEXT_Z_DEFAULT_COMPRESSION (-1) 200 | #define PNG_TEXT_Z_DEFAULT_STRATEGY 0 201 | #define PNG_WEIGHT_SHIFT 8 202 | #define PNG_ZBUF_SIZE 8192 203 | #define PNG_ZLIB_VERNUM 0 /* unknown */ 204 | #define PNG_Z_DEFAULT_COMPRESSION (-1) 205 | #define PNG_Z_DEFAULT_NOFILTER_STRATEGY 0 206 | #define PNG_Z_DEFAULT_STRATEGY 1 207 | #define PNG_sCAL_PRECISION 5 208 | #define PNG_sRGB_PROFILE_CHECKS 2 209 | /* end of settings */ 210 | #endif /* PNGLCONF_H */ 211 | -------------------------------------------------------------------------------- /src/scripts/main.lua: -------------------------------------------------------------------------------- 1 | local libos = require "libos" 2 | local ejresource = require "ejresource" 3 | local ejpackage = require "ejpackage" 4 | local utils = require "utils" 5 | 6 | local usage = [[ 7 | Usage: simplepacker inputdir [-o path] [-n name] [-ni] [-fmt ppm|png] [-np] [-ps packsize] [-na] [-raw] [-v] 8 | -o: specify output directory 9 | -n: specify output package name 10 | -ni: no image, ignore raw image files(png) from the input 11 | -fmt: specify image format, ppm and png is available. default value is ppm 12 | -np: no pack, do not pack images to large image sheet 13 | -ps: specify image sheet size, up to 2048. default value is 1024 14 | -na: no animation, ignore animation description files(.a.lua) 15 | -raw: write down all data instead of exporting a ejoy2d package 16 | output folder can used as the input to create ejoy2d package later 17 | -v: show verbose log 18 | ]] 19 | 20 | -- configuration 21 | local config = { 22 | proc_img = true, -- whether to read raw image 23 | proc_anim = true, -- whether to read anim 24 | pack_img = true, 25 | img_fmt = "ppm", 26 | output_path = false, 27 | output_name = false, 28 | pack_size = 1024, 29 | output_raw = false, -- true for write down all data, false for export ejoy2d package 30 | } 31 | 32 | -- functions 33 | local function _parse_args(args) 34 | if not args[2] then -- check input path 35 | print("require input path") 36 | return false 37 | end 38 | 39 | local i = 3 40 | while i <= #args do 41 | local arg = args[i] 42 | if arg == "-o" then 43 | local op = args[i + 1] 44 | if op[1] == "-" then 45 | print("illegal output path") 46 | return false 47 | end 48 | config.output_path = op 49 | i = i + 1 50 | elseif arg == "-n" then 51 | local on = args[i + 1] 52 | if on[1] == "-" then 53 | print("illegal output name") 54 | return false 55 | end 56 | config.output_name = on 57 | i = i + 1 58 | elseif arg == "-ni" then 59 | config.proc_img = false 60 | elseif arg == "-fmt" then 61 | local fmt = args[i + 1] 62 | if fmt ~= "ppm" and fmt ~= "png" then 63 | print("illegal image format") 64 | return false 65 | end 66 | config.img_fmt = fmt 67 | i = i + 1 68 | elseif arg == "-np" then 69 | config.pack_img = false 70 | elseif arg == "-ps" then 71 | local ps = tonumber(args[i + 1]) 72 | if ps <= 0 or ps > 2048 then 73 | print("illegal pack size") 74 | return false 75 | end 76 | config.pack_size = ps 77 | i = i + 1 78 | elseif arg == "-na" then 79 | config.proc_anim = false 80 | elseif arg == "-raw" then 81 | config.output_raw = true 82 | elseif arg == "-v" then 83 | utils:enable_log() 84 | else 85 | print("illegal argument", arg) 86 | return false 87 | end 88 | i = i + 1 89 | end 90 | 91 | return true 92 | end 93 | 94 | local function _check_anims(imgs) 95 | local anims = {} 96 | 97 | -- sort by image name 98 | table.sort(imgs, function (a, b) return a.name < b.name end) 99 | 100 | local i = 1 101 | while i <= #imgs do 102 | -- find image name like xxx1 103 | local _,_,name = string.find(imgs[i].name, "(%a+)1") 104 | if name then 105 | local idx = 2 106 | local found 107 | repeat -- find the whole sequence 108 | found = false 109 | i = i + 1 110 | if i > #imgs then 111 | break 112 | end 113 | if imgs[i].name == name..tostring(idx) then 114 | found = true 115 | idx = idx + 1 116 | end 117 | until not found 118 | 119 | if idx > 2 then 120 | local anim = ejresource:new_anim(name) 121 | local frames = {} 122 | for j=1,idx-1 do 123 | local com = {name=name..tostring(j)} -- name only, no other attributes 124 | table.insert(frames, {com}) 125 | end 126 | anim:add_action(frames) 127 | table.insert(anims, anim) 128 | utils:logf("auto create anim <%s> frames=(%d)", name, idx-1) 129 | end 130 | else 131 | i = i + 1 132 | end 133 | end 134 | 135 | return anims 136 | end 137 | 138 | -- entry point 139 | function run(args) 140 | -- init arguments 141 | if not _parse_args(args) then 142 | print(usage) 143 | return 144 | end 145 | 146 | -- init work path 147 | local input = utils:trim_slash(args[2]) 148 | local output = input.."_out" 149 | if config.output_path then 150 | output = utils:trim_slash(config.output_path) 151 | end 152 | libos:makedir(output) 153 | 154 | -- walk input folder 155 | local file_list = libos.walkdir(input) 156 | if not file_list then 157 | utils:logf("error input path") 158 | print(usage) 159 | return 160 | end 161 | 162 | -- process input files 163 | local all_imgs = {} -- raw images, only png supported 164 | local all_sheets = {} -- iamge sheets 165 | local all_anims = {} -- animation description 166 | 167 | for _,v in ipairs(file_list) do 168 | local full_name = input.."/"..v 169 | local name = string.sub(v, 1, string.find(v, ".", 1, true) - 1) -- name is the filename without ext 170 | 171 | if config.proc_img and utils:check_ext(v, ".png") then 172 | local img = ejresource:load_img(full_name, name) 173 | if img then 174 | utils:logf("load img <%s> success size=(%d,%d) offset=(%d,%d)", name, img.w, img.h, img.ox, img.oy) 175 | table.insert(all_imgs, img) 176 | else 177 | utils:logf("load img <%s> failed", name) 178 | end 179 | end 180 | 181 | if utils:check_ext(v, ".p.lua") then 182 | local sheet = ejresource:load_sheet(full_name) 183 | if sheet then 184 | utils:logf("load sheet <%s> success", name) 185 | table.insert(all_sheets, sheet) 186 | else 187 | utils:logf("load sheet <%s> failed", name) 188 | end 189 | end 190 | 191 | if config.proc_anim and utils:check_ext(v, ".a.lua") then 192 | local anim = ejresource:load_anim(full_name, name) 193 | if anim then 194 | utils:logf("load anim <%s> success", name) 195 | table.insert(all_anims, anim) 196 | else 197 | utils:logf("load anim <%s> failed", name) 198 | end 199 | end 200 | end 201 | 202 | -- guess anim from image filename 203 | if config.proc_anim then 204 | local anims = _check_anims(all_imgs) 205 | for _,v in ipairs(anims) do 206 | table.insert(all_anims, v) 207 | end 208 | end 209 | 210 | -- pack raw images onto image sheet 211 | if config.pack_img then 212 | local sheet_map = {} 213 | local left_imgs = {} 214 | 215 | for _,v in ipairs(all_imgs) do 216 | local pixfmt = v.pixfmt 217 | local sheet = sheet_map[pixfmt] 218 | if not sheet then 219 | sheet = ejresource:new_sheet(config.pack_size, pixfmt) 220 | sheet_map[pixfmt] = sheet 221 | utils:logf("create a new image sheet with format %s", pixfmt) 222 | end 223 | if not sheet:pack_img(v) then 224 | table.insert(left_imgs, v) 225 | utils:logf("pack image <%s> failed", v.name) 226 | else 227 | utils:logf("pack image <%s> success", v.name) 228 | end 229 | end 230 | 231 | for _,v in pairs(sheet_map) do 232 | table.insert(all_sheets, v) 233 | end 234 | 235 | all_imgs = left_imgs -- too big to pack 236 | end 237 | 238 | -- output 239 | if config.output_raw then 240 | utils:logf("export all data to a detailed format") 241 | 242 | for _,v in ipairs(all_imgs) do 243 | local path = string.format("%s/%s", output, v.name) 244 | v:save(path, true, config.img_fmt) -- image save as image sheet 245 | end 246 | 247 | for i,v in ipairs(all_sheets) do 248 | local path = string.format("%s/imagesheet%d", output, i) 249 | v:save(path, true, config.img_fmt) 250 | end 251 | 252 | for _,v in ipairs(all_anims) do 253 | local path = string.format("%s/%s.a.lua", output, v.name) 254 | v:save(path) 255 | end 256 | else 257 | local _,_,name = string.find(input, ".-([^\\/]+)$") 258 | if config.output_name then 259 | name = config.output_name 260 | end 261 | local pkg = ejpackage:new_pkg(name, config.img_fmt) 262 | 263 | utils:logf("export ejoy2d package %s", name) 264 | 265 | -- raw images 266 | for _,v in ipairs(all_imgs) do 267 | pkg:add_img(v) 268 | end 269 | 270 | -- packed images 271 | for _,v in ipairs(all_sheets) do 272 | pkg:add_sheet(v) 273 | end 274 | 275 | -- anims 276 | for _,v in ipairs(all_anims) do 277 | pkg:add_anim(v) 278 | end 279 | 280 | -- save to disk 281 | pkg:save(output) 282 | end 283 | end -------------------------------------------------------------------------------- /3rd/lua/lauxlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lauxlib.h,v 1.120.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions for building Lua libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lauxlib_h 9 | #define lauxlib_h 10 | 11 | 12 | #include 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | 18 | 19 | /* extra error code for `luaL_load' */ 20 | #define LUA_ERRFILE (LUA_ERRERR+1) 21 | 22 | 23 | typedef struct luaL_Reg { 24 | const char *name; 25 | lua_CFunction func; 26 | } luaL_Reg; 27 | 28 | 29 | LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver); 30 | #define luaL_checkversion(L) luaL_checkversion_(L, LUA_VERSION_NUM) 31 | 32 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 33 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 34 | LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 35 | LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); 36 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, 37 | size_t *l); 38 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, 39 | const char *def, size_t *l); 40 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); 41 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); 42 | 43 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); 44 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, 45 | lua_Integer def); 46 | LUALIB_API lua_Unsigned (luaL_checkunsigned) (lua_State *L, int numArg); 47 | LUALIB_API lua_Unsigned (luaL_optunsigned) (lua_State *L, int numArg, 48 | lua_Unsigned def); 49 | 50 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 51 | LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); 52 | LUALIB_API void (luaL_checkany) (lua_State *L, int narg); 53 | 54 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 55 | LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); 56 | LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); 57 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 58 | 59 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); 60 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 61 | 62 | LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, 63 | const char *const lst[]); 64 | 65 | LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 66 | LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 67 | 68 | /* pre-defined references */ 69 | #define LUA_NOREF (-2) 70 | #define LUA_REFNIL (-1) 71 | 72 | LUALIB_API int (luaL_ref) (lua_State *L, int t); 73 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 74 | 75 | LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, 76 | const char *mode); 77 | 78 | #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) 79 | 80 | LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, 81 | const char *name, const char *mode); 82 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 83 | 84 | LUALIB_API lua_State *(luaL_newstate) (void); 85 | 86 | LUALIB_API int (luaL_len) (lua_State *L, int idx); 87 | 88 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 89 | const char *r); 90 | 91 | LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); 92 | 93 | LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); 94 | 95 | LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 96 | const char *msg, int level); 97 | 98 | LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, 99 | lua_CFunction openf, int glb); 100 | 101 | /* 102 | ** =============================================================== 103 | ** some useful macros 104 | ** =============================================================== 105 | */ 106 | 107 | 108 | #define luaL_newlibtable(L,l) \ 109 | lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 110 | 111 | #define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 112 | 113 | #define luaL_argcheck(L, cond,numarg,extramsg) \ 114 | ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) 115 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 116 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 117 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 118 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 119 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 120 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 121 | 122 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 123 | 124 | #define luaL_dofile(L, fn) \ 125 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 126 | 127 | #define luaL_dostring(L, s) \ 128 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 129 | 130 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 131 | 132 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 133 | 134 | #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 135 | 136 | 137 | /* 138 | ** {====================================================== 139 | ** Generic Buffer manipulation 140 | ** ======================================================= 141 | */ 142 | 143 | typedef struct luaL_Buffer { 144 | char *b; /* buffer address */ 145 | size_t size; /* buffer size */ 146 | size_t n; /* number of characters in buffer */ 147 | lua_State *L; 148 | char initb[LUAL_BUFFERSIZE]; /* initial buffer */ 149 | } luaL_Buffer; 150 | 151 | 152 | #define luaL_addchar(B,c) \ 153 | ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 154 | ((B)->b[(B)->n++] = (c))) 155 | 156 | #define luaL_addsize(B,s) ((B)->n += (s)) 157 | 158 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 159 | LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); 160 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 161 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 162 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 163 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 164 | LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); 165 | LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); 166 | 167 | #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) 168 | 169 | /* }====================================================== */ 170 | 171 | 172 | 173 | /* 174 | ** {====================================================== 175 | ** File handles for IO library 176 | ** ======================================================= 177 | */ 178 | 179 | /* 180 | ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 181 | ** initial structure 'luaL_Stream' (it may contain other fields 182 | ** after that initial structure). 183 | */ 184 | 185 | #define LUA_FILEHANDLE "FILE*" 186 | 187 | 188 | typedef struct luaL_Stream { 189 | FILE *f; /* stream (NULL for incompletely created streams) */ 190 | lua_CFunction closef; /* to close stream (NULL for closed streams) */ 191 | } luaL_Stream; 192 | 193 | /* }====================================================== */ 194 | 195 | 196 | 197 | /* compatibility with old module system */ 198 | #if defined(LUA_COMPAT_MODULE) 199 | 200 | LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, 201 | int sizehint); 202 | LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, 203 | const luaL_Reg *l, int nup); 204 | 205 | #define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) 206 | 207 | #endif 208 | 209 | 210 | #endif 211 | 212 | 213 | -------------------------------------------------------------------------------- /3rd/lua/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.82.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstate_h 8 | #define lstate_h 9 | 10 | #include "lua.h" 11 | 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | #include "lzio.h" 15 | 16 | 17 | /* 18 | 19 | ** Some notes about garbage-collected objects: All objects in Lua must 20 | ** be kept somehow accessible until being freed. 21 | ** 22 | ** Lua keeps most objects linked in list g->allgc. The link uses field 23 | ** 'next' of the CommonHeader. 24 | ** 25 | ** Strings are kept in several lists headed by the array g->strt.hash. 26 | ** 27 | ** Open upvalues are not subject to independent garbage collection. They 28 | ** are collected together with their respective threads. Lua keeps a 29 | ** double-linked list with all open upvalues (g->uvhead) so that it can 30 | ** mark objects referred by them. (They are always gray, so they must 31 | ** be remarked in the atomic step. Usually their contents would be marked 32 | ** when traversing the respective threads, but the thread may already be 33 | ** dead, while the upvalue is still accessible through closures.) 34 | ** 35 | ** Objects with finalizers are kept in the list g->finobj. 36 | ** 37 | ** The list g->tobefnz links all objects being finalized. 38 | 39 | */ 40 | 41 | 42 | struct lua_longjmp; /* defined in ldo.c */ 43 | 44 | 45 | 46 | /* extra stack space to handle TM calls and some other extras */ 47 | #define EXTRA_STACK 5 48 | 49 | 50 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 51 | 52 | 53 | /* kinds of Garbage Collection */ 54 | #define KGC_NORMAL 0 55 | #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ 56 | #define KGC_GEN 2 /* generational collection */ 57 | 58 | 59 | typedef struct stringtable { 60 | GCObject **hash; 61 | lu_int32 nuse; /* number of elements */ 62 | int size; 63 | } stringtable; 64 | 65 | 66 | /* 67 | ** information about a call 68 | */ 69 | typedef struct CallInfo { 70 | StkId func; /* function index in the stack */ 71 | StkId top; /* top for this function */ 72 | struct CallInfo *previous, *next; /* dynamic call link */ 73 | short nresults; /* expected number of results from this function */ 74 | lu_byte callstatus; 75 | ptrdiff_t extra; 76 | union { 77 | struct { /* only for Lua functions */ 78 | StkId base; /* base for this function */ 79 | const Instruction *savedpc; 80 | } l; 81 | struct { /* only for C functions */ 82 | int ctx; /* context info. in case of yields */ 83 | lua_CFunction k; /* continuation in case of yields */ 84 | ptrdiff_t old_errfunc; 85 | lu_byte old_allowhook; 86 | lu_byte status; 87 | } c; 88 | } u; 89 | } CallInfo; 90 | 91 | 92 | /* 93 | ** Bits in CallInfo status 94 | */ 95 | #define CIST_LUA (1<<0) /* call is running a Lua function */ 96 | #define CIST_HOOKED (1<<1) /* call is running a debug hook */ 97 | #define CIST_REENTRY (1<<2) /* call is running on same invocation of 98 | luaV_execute of previous call */ 99 | #define CIST_YIELDED (1<<3) /* call reentered after suspension */ 100 | #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ 101 | #define CIST_STAT (1<<5) /* call has an error status (pcall) */ 102 | #define CIST_TAIL (1<<6) /* call was tail called */ 103 | #define CIST_HOOKYIELD (1<<7) /* last hook called yielded */ 104 | 105 | 106 | #define isLua(ci) ((ci)->callstatus & CIST_LUA) 107 | 108 | 109 | /* 110 | ** `global state', shared by all threads of this state 111 | */ 112 | typedef struct global_State { 113 | lua_Alloc frealloc; /* function to reallocate memory */ 114 | void *ud; /* auxiliary data to `frealloc' */ 115 | lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ 116 | l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ 117 | lu_mem GCmemtrav; /* memory traversed by the GC */ 118 | lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ 119 | stringtable strt; /* hash table for strings */ 120 | TValue l_registry; 121 | unsigned int seed; /* randomized seed for hashes */ 122 | lu_byte currentwhite; 123 | lu_byte gcstate; /* state of garbage collector */ 124 | lu_byte gckind; /* kind of GC running */ 125 | lu_byte gcrunning; /* true if GC is running */ 126 | int sweepstrgc; /* position of sweep in `strt' */ 127 | GCObject *allgc; /* list of all collectable objects */ 128 | GCObject *finobj; /* list of collectable objects with finalizers */ 129 | GCObject **sweepgc; /* current position of sweep in list 'allgc' */ 130 | GCObject **sweepfin; /* current position of sweep in list 'finobj' */ 131 | GCObject *gray; /* list of gray objects */ 132 | GCObject *grayagain; /* list of objects to be traversed atomically */ 133 | GCObject *weak; /* list of tables with weak values */ 134 | GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ 135 | GCObject *allweak; /* list of all-weak tables */ 136 | GCObject *tobefnz; /* list of userdata to be GC */ 137 | UpVal uvhead; /* head of double-linked list of all open upvalues */ 138 | Mbuffer buff; /* temporary buffer for string concatenation */ 139 | int gcpause; /* size of pause between successive GCs */ 140 | int gcmajorinc; /* pause between major collections (only in gen. mode) */ 141 | int gcstepmul; /* GC `granularity' */ 142 | lua_CFunction panic; /* to be called in unprotected errors */ 143 | struct lua_State *mainthread; 144 | const lua_Number *version; /* pointer to version number */ 145 | TString *memerrmsg; /* memory-error message */ 146 | TString *tmname[TM_N]; /* array with tag-method names */ 147 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 148 | } global_State; 149 | 150 | 151 | /* 152 | ** `per thread' state 153 | */ 154 | struct lua_State { 155 | CommonHeader; 156 | lu_byte status; 157 | StkId top; /* first free slot in the stack */ 158 | global_State *l_G; 159 | CallInfo *ci; /* call info for current function */ 160 | const Instruction *oldpc; /* last pc traced */ 161 | StkId stack_last; /* last free slot in the stack */ 162 | StkId stack; /* stack base */ 163 | int stacksize; 164 | unsigned short nny; /* number of non-yieldable calls in stack */ 165 | unsigned short nCcalls; /* number of nested C calls */ 166 | lu_byte hookmask; 167 | lu_byte allowhook; 168 | int basehookcount; 169 | int hookcount; 170 | lua_Hook hook; 171 | GCObject *openupval; /* list of open upvalues in this stack */ 172 | GCObject *gclist; 173 | struct lua_longjmp *errorJmp; /* current error recover point */ 174 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 175 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ 176 | }; 177 | 178 | 179 | #define G(L) (L->l_G) 180 | 181 | 182 | /* 183 | ** Union of all collectable objects 184 | */ 185 | union GCObject { 186 | GCheader gch; /* common header */ 187 | union TString ts; 188 | union Udata u; 189 | union Closure cl; 190 | struct Table h; 191 | struct Proto p; 192 | struct UpVal uv; 193 | struct lua_State th; /* thread */ 194 | }; 195 | 196 | 197 | #define gch(o) (&(o)->gch) 198 | 199 | /* macros to convert a GCObject into a specific value */ 200 | #define rawgco2ts(o) \ 201 | check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts)) 202 | #define gco2ts(o) (&rawgco2ts(o)->tsv) 203 | #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 204 | #define gco2u(o) (&rawgco2u(o)->uv) 205 | #define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l)) 206 | #define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c)) 207 | #define gco2cl(o) \ 208 | check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl)) 209 | #define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 210 | #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 211 | #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 212 | #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 213 | 214 | /* macro to convert any Lua object into a GCObject */ 215 | #define obj2gco(v) (cast(GCObject *, (v))) 216 | 217 | 218 | /* actual number of total bytes allocated */ 219 | #define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt) 220 | 221 | LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); 222 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 223 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 224 | LUAI_FUNC void luaE_freeCI (lua_State *L); 225 | 226 | 227 | #endif 228 | 229 | -------------------------------------------------------------------------------- /src/libimg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "lualib.h" 6 | #include "lauxlib.h" 7 | 8 | #include "img_png.h" 9 | #include "img_ppm.h" 10 | 11 | #define PIXEL_FORMAT_UNKNOW 0 12 | #define PIXEL_FORMAT_RGB 1 13 | #define PIXEL_FORMAT_RGBA 2 14 | 15 | static int 16 | _check_pixel_format(const char* fmt) { 17 | if (!fmt) { return PIXEL_FORMAT_UNKNOW; } 18 | 19 | if (strcmp(fmt, "RGB") == 0) { 20 | return PIXEL_FORMAT_RGB; 21 | } else if (strcmp(fmt, "RGBA") == 0) { 22 | return PIXEL_FORMAT_RGBA; 23 | } else { 24 | return PIXEL_FORMAT_UNKNOW; 25 | } 26 | } 27 | 28 | static void 29 | _blit(uint8_t* src, uint8_t* dst, int ks, int kd, int depth) { 30 | for (int i = 0; i < depth; ++i) { 31 | dst[kd + i] = src[ks + i]; 32 | } 33 | } 34 | 35 | static int 36 | lloadpng(lua_State *L) { 37 | const char* fn = luaL_checkstring(L, -1); 38 | 39 | struct png png; 40 | int ok = img_loadpng(fn, &png); 41 | 42 | if (!ok) { 43 | return 0; 44 | } 45 | 46 | int sz = png.width * png.height * png.channel; 47 | uint8_t* buf = lua_newuserdata(L, sz); 48 | memcpy(buf, png.buffer, sz); 49 | free(png.buffer); 50 | 51 | switch (png.type) 52 | { 53 | case PNG_RGBA: 54 | lua_pushstring(L, "RGBA"); 55 | break; 56 | 57 | case PNG_RGB: 58 | lua_pushstring(L, "RGB"); 59 | break; 60 | 61 | default: 62 | break; 63 | } 64 | 65 | lua_pushnumber(L, png.width); 66 | lua_pushnumber(L, png.height); 67 | 68 | return 4; // buf, color_type, w, h 69 | } 70 | 71 | static int 72 | lsavepng(lua_State *L) { 73 | const char* fn = luaL_checkstring(L, -5); // filename without extension 74 | int w = luaL_checkint(L, -4); 75 | int h = luaL_checkint(L, -3); 76 | int pixfmt = _check_pixel_format(luaL_checkstring(L, -2)); 77 | uint8_t* buf = (uint8_t*)lua_touserdata(L, -1); 78 | 79 | int ok = 0; 80 | if (pixfmt == PIXEL_FORMAT_RGB) { 81 | ok = img_savepng(fn, PNG_RGB, w, h, buf); 82 | } else if (pixfmt == PIXEL_FORMAT_RGBA) { 83 | ok = img_savepng(fn, PNG_RGBA, w, h, buf); 84 | } 85 | 86 | if (!ok) { 87 | luaL_error(L, "save png failed"); 88 | } 89 | 90 | return 0; 91 | } 92 | 93 | static int 94 | lloadppm(lua_State *L) { 95 | const char* fn = luaL_checkstring(L, -1); // filename without extension 96 | 97 | struct ppm ppm; 98 | int ok = img_loadppm(fn, &ppm); 99 | 100 | if (!ok) { 101 | return 0; 102 | } 103 | 104 | int sz = ppm.width * ppm.height * ppm.step; 105 | uint8_t* buf = lua_newuserdata(L, sz); 106 | memcpy(buf, ppm.buffer, sz); 107 | free(ppm.buffer); 108 | 109 | switch (ppm.type) 110 | { 111 | case PPM_RGBA8: 112 | lua_pushstring(L, "RGBA"); 113 | break; 114 | 115 | case PPM_RGB8: 116 | lua_pushstring(L, "RGB"); 117 | break; 118 | 119 | default: 120 | lua_pop(L, 1); 121 | return 0; 122 | } 123 | 124 | lua_pushnumber(L, ppm.width); 125 | lua_pushnumber(L, ppm.height); 126 | 127 | return 4; 128 | } 129 | 130 | static int 131 | lsaveppm(lua_State *L) { 132 | const char* fn = luaL_checkstring(L, -5); // filename without extension 133 | int w = luaL_checkint(L, -4); 134 | int h = luaL_checkint(L, -3); 135 | int pixfmt = _check_pixel_format(luaL_checkstring(L, -2)); 136 | uint8_t* buf = (uint8_t*)lua_touserdata(L, -1); 137 | 138 | int ok = 0; 139 | if (pixfmt == PIXEL_FORMAT_RGB) { 140 | ok = img_saveppm(fn, PPM_RGB8, w, h, buf); 141 | } else if (pixfmt == PIXEL_FORMAT_RGBA) { 142 | ok = img_saveppm(fn, PPM_RGBA8, w, h, buf); 143 | } 144 | 145 | if (!ok) { 146 | luaL_error(L, "save ppm failed"); 147 | } 148 | 149 | return 0; 150 | } 151 | 152 | static int 153 | lnewimg(lua_State *L) { 154 | int s = luaL_checkint(L, -2); 155 | int pixfmt = _check_pixel_format(luaL_checkstring(L, -1)); 156 | uint8_t* buf = NULL; 157 | int buf_size = 0; 158 | 159 | switch (pixfmt) { 160 | case PIXEL_FORMAT_RGB: 161 | buf_size = s*s * 3; 162 | buf = lua_newuserdata(L, buf_size); 163 | memset(buf, 0, buf_size); 164 | break; 165 | 166 | case PIXEL_FORMAT_RGBA: 167 | buf_size = s*s * 4; 168 | buf = lua_newuserdata(L, buf_size); 169 | memset(buf, 0, buf_size); 170 | break; 171 | 172 | default: 173 | luaL_error(L, "unknown pixel format"); 174 | } 175 | 176 | return 1; 177 | } 178 | 179 | static int 180 | lblitimg(lua_State *L) { 181 | // parse arguments 182 | int y = luaL_checkint(L, -1); 183 | int x = luaL_checkint(L, -2); 184 | int src_h = luaL_checkint(L, -3); 185 | int src_w = luaL_checkint(L, -4); 186 | uint8_t* src = lua_touserdata(L, -5); 187 | int pixfmt = _check_pixel_format(luaL_checkstring(L, -6)); 188 | int dst_s = luaL_checkint(L, -7); 189 | uint8_t* dst = lua_touserdata(L, -8); 190 | 191 | int ks, kd, depth; 192 | 193 | switch (pixfmt) { // copy pixels 194 | case PIXEL_FORMAT_RGB: 195 | depth = 3; 196 | break; 197 | 198 | case PIXEL_FORMAT_RGBA: 199 | depth = 4; 200 | break; 201 | 202 | default: 203 | luaL_error(L, "unknown pixel format"); 204 | break; 205 | } 206 | 207 | ks = 0; 208 | kd = ((y - 1)*dst_s + x - 1) * depth; 209 | _blit(src, dst, ks, kd, depth); 210 | 211 | ks = (src_w - 1) * depth; 212 | kd = ((y - 1)*dst_s + x + src_w) * depth; 213 | _blit(src, dst, ks, kd, depth); 214 | 215 | ks = (src_w * (src_h - 1)) * depth; 216 | kd = ((y + src_h)*dst_s + x - 1) * depth; 217 | _blit(src, dst, ks, kd, depth); 218 | 219 | ks = (src_w * src_h - 1) * depth; 220 | kd = ((y + src_h)*dst_s + x + src_w) * depth; 221 | _blit(src, dst, ks, kd, depth); 222 | 223 | for (int j = 0; j < src_w; ++j) { 224 | ks = j * depth; 225 | kd = ((y - 1)*dst_s + (x + j)) * depth; 226 | _blit(src, dst, ks, kd, depth); 227 | ks = ((src_h - 1)*src_w + j) * depth; 228 | kd = ((y + src_h)*dst_s + (x + j)) * depth; 229 | _blit(src, dst, ks, kd, depth); 230 | } 231 | 232 | for (int i = 0; i < src_h; ++i) { 233 | ks = (i*src_w) * depth; 234 | kd = ((y + i)*dst_s + (x - 1)) * depth; 235 | _blit(src, dst, ks, kd, depth); 236 | ks = (i*src_w + src_w - 1) * depth; 237 | kd = ((y + i)*dst_s + (x + src_w)) * depth; 238 | _blit(src, dst, ks, kd, depth); 239 | } 240 | 241 | for (int i = 0; i < src_h; ++i) { 242 | for (int j = 0; j < src_w; ++j) { 243 | ks = (i*src_w + j) * depth; 244 | kd = ((y + i)*dst_s + (x + j)) * depth; 245 | _blit(src, dst, ks, kd, depth); 246 | } 247 | } 248 | 249 | return 0; 250 | } 251 | 252 | static int 253 | ltrimimg(lua_State *L) { 254 | int pixfmt = _check_pixel_format(luaL_checkstring(L, -1)); 255 | int h = luaL_checkint(L, -2); 256 | int w = luaL_checkint(L, -3); 257 | uint8_t* src = lua_touserdata(L, -4); 258 | 259 | if (pixfmt != PIXEL_FORMAT_RGBA) { 260 | luaL_error(L, "cannot trim image that do not have alpha channel"); 261 | } 262 | 263 | int l = 0; 264 | int r = 0; 265 | int t = 0; 266 | int b = 0; 267 | 268 | for (int x = 0; x < w; ++x) { 269 | int transparent = 1; 270 | for (int y = 0; y < h; ++y) { 271 | if (src[(y*w + x) * 4 + 3] > 0) { 272 | transparent = 0; 273 | break; 274 | } 275 | } 276 | if (!transparent) break; 277 | ++l; 278 | } 279 | for (int x = w - 1; x >= 0; --x) { 280 | int transparent = 1; 281 | for (int y = 0; y < h; ++y) { 282 | if (src[(y*w + x) * 4 + 3] > 0) { 283 | transparent = 0; 284 | break; 285 | } 286 | } 287 | if (!transparent) break; 288 | ++r; 289 | } 290 | for (int y = 0; y <= h; ++y) { 291 | int transparent = 1; 292 | for (int x = 0; x < w; ++x) { 293 | if (src[(y*w + x) * 4 + 3] > 0) { 294 | transparent = 0; 295 | break; 296 | } 297 | } 298 | if (!transparent) break; 299 | ++t; 300 | } 301 | for (int y = h - 1; y >= 0; --y) { 302 | int transparent = 1; 303 | for (int x = 0; x < w; ++x) { 304 | if (src[(y*w + x) * 4 + 3] > 0) { 305 | transparent = 0; 306 | break; 307 | } 308 | } 309 | if (!transparent) break; 310 | ++b; 311 | } 312 | 313 | if (l == 0 && r == 0 && t == 0 && b == 0) { 314 | return 0; 315 | } 316 | 317 | uint8_t* dst = lua_newuserdata(L, (w - l - r)*(h - t - b) * 4); 318 | for (int i = 0; i < h - t - b; ++i) { 319 | for (int j = 0; j < w - l - r; ++j) { 320 | int k1 = (i*(w - l - r) + j) * 4; 321 | int k2 = ((i + t)*w + j + l) * 4; 322 | dst[k1 + 0] = src[k2 + 0]; 323 | dst[k1 + 1] = src[k2 + 1]; 324 | dst[k1 + 2] = src[k2 + 2]; 325 | dst[k1 + 3] = src[k2 + 3]; 326 | } 327 | } 328 | 329 | lua_pushnumber(L, l); 330 | lua_pushnumber(L, r); 331 | lua_pushnumber(L, t); 332 | lua_pushnumber(L, b); 333 | 334 | return 5; 335 | } 336 | 337 | int 338 | register_libimage(lua_State *L) { 339 | luaL_Reg l[] = { 340 | { "loadpng", lloadpng }, 341 | { "savepng", lsavepng }, 342 | { "loadppm", lloadppm }, 343 | { "saveppm", lsaveppm }, 344 | { "newimg", lnewimg }, 345 | { "blitimg", lblitimg }, 346 | { "trimimg", ltrimimg }, 347 | { NULL, NULL } 348 | }; 349 | 350 | luaL_newlib(L, l); 351 | return 1; 352 | } --------------------------------------------------------------------------------