├── external ├── lpeg │ ├── lpeg.def │ ├── lpeg.h │ ├── lpprint.h │ ├── lpcode.h │ ├── lpcap.h │ ├── lptree.h │ ├── lpvm.h │ ├── lptypes.h │ ├── lpprint.c │ └── lpvm.c ├── lpack │ ├── lpack.h │ └── lpack.c ├── bitop │ ├── bit.h │ └── bit.c ├── sproto │ ├── lsproto.h │ ├── Makefile │ ├── msvcint.h │ ├── LICENSE │ └── sproto.h └── luasocket │ ├── select.h │ ├── unix.h │ ├── luasocket_scripts.h │ ├── udp.h │ ├── wsocket.h │ ├── timeout.h │ ├── tcp.h │ ├── mime.h │ ├── luasocket.h │ ├── io.c │ ├── except.h │ ├── buffer.h │ ├── usocket.h │ ├── inet.h │ ├── auxiliar.h │ ├── io.h │ ├── options.h │ ├── script │ ├── mime.lua │ ├── socket │ │ ├── mbox.lua │ │ ├── headers.lua │ │ ├── tp.lua │ │ ├── smtp.lua │ │ ├── ftp.lua │ │ └── url.lua │ ├── socket.lua │ └── ltn12.lua │ ├── except.c │ ├── socket.h │ ├── luasocket.c │ ├── auxiliar.c │ ├── serial.c │ ├── select.c │ ├── timeout.c │ ├── buffer.c │ └── options.c ├── .gitattributes ├── LICENSE ├── README.md └── Android.mk /external/lpeg/lpeg.def: -------------------------------------------------------------------------------- 1 | EXPORTS 2 | luaopen_lpeg -------------------------------------------------------------------------------- /external/lpack/lpack.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __LUA_LPACK_H_ 3 | #define __LUA_LPACK_H_ 4 | #if __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include "lauxlib.h" 9 | 10 | int luaopen_pack(lua_State *L); 11 | 12 | #if __cplusplus 13 | } 14 | #endif 15 | #endif 16 | -------------------------------------------------------------------------------- /external/bitop/bit.h: -------------------------------------------------------------------------------- 1 | #ifndef __LUA_BITOP_H_ 2 | #define __LUA_BITOP_H_ 3 | 4 | #if __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include "lauxlib.h" 9 | 10 | LUALIB_API int luaopen_bit(lua_State *L); 11 | 12 | #if __cplusplus 13 | } 14 | #endif 15 | 16 | #endif -------------------------------------------------------------------------------- /external/sproto/lsproto.h: -------------------------------------------------------------------------------- 1 | #ifndef __LUA_LSPROTO_H_ 2 | #define __LUA_LSPROTO_H_ 3 | 4 | #if __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include "lauxlib.h" 9 | 10 | int luaopen_sproto_core(lua_State *L); 11 | 12 | #if __cplusplus 13 | } 14 | #endif 15 | 16 | #endif -------------------------------------------------------------------------------- /external/lpeg/lpeg.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __LUA_LPEG_H_ 3 | #define __LUA_LPEG_H_ 4 | 5 | #if __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | #include "lpeg/lptypes.h" 10 | #include "lpeg/lpcap.h" 11 | #include "lpeg/lpcode.h" 12 | #include "lpeg/lpprint.h" 13 | #include "lpeg/lptree.h" 14 | #include "lpeg/lpvm.h" 15 | 16 | int luaopen_lpeg(lua_State *L); 17 | 18 | 19 | #if __cplusplus 20 | } 21 | #endif 22 | 23 | #endif -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | *.c linguist-language=c 19 | *.h linguist-language=c 20 | -------------------------------------------------------------------------------- /external/sproto/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY : all win clean 2 | 3 | all : linux 4 | win : sproto.dll 5 | 6 | # For Linux 7 | linux: 8 | make sproto.so "DLLFLAGS = -shared -fPIC" 9 | # For Mac OS 10 | macosx: 11 | make sproto.so "DLLFLAGS = -bundle -undefined dynamic_lookup" 12 | 13 | sproto.so : sproto.c lsproto.c 14 | env gcc -O2 -Wall $(DLLFLAGS) -o $@ $^ 15 | 16 | sproto.dll : sproto.c lsproto.c 17 | gcc -O2 -Wall --shared -o $@ $^ -I/usr/local/include -L/usr/local/bin -llua53 18 | 19 | clean : 20 | rm -f sproto.so sproto.dll 21 | -------------------------------------------------------------------------------- /external/luasocket/select.h: -------------------------------------------------------------------------------- 1 | #ifndef SELECT_H 2 | #define SELECT_H 3 | /*=========================================================================*\ 4 | * Select implementation 5 | * LuaSocket toolkit 6 | * 7 | * Each object that can be passed to the select function has to export 8 | * method getfd() which returns the descriptor to be passed to the 9 | * underlying select function. Another method, dirty(), should return 10 | * true if there is data ready for reading (required for buffered input). 11 | \*=========================================================================*/ 12 | 13 | int select_open(lua_State *L); 14 | 15 | #endif /* SELECT_H */ 16 | -------------------------------------------------------------------------------- /external/luasocket/unix.h: -------------------------------------------------------------------------------- 1 | #ifndef UNIX_H 2 | #define UNIX_H 3 | /*=========================================================================*\ 4 | * Unix domain object 5 | * LuaSocket toolkit 6 | * 7 | * This module is just an example of how to extend LuaSocket with a new 8 | * domain. 9 | \*=========================================================================*/ 10 | #include "lua.h" 11 | 12 | #include "buffer.h" 13 | #include "timeout.h" 14 | #include "socket.h" 15 | 16 | #ifndef UNIX_API 17 | #define UNIX_API extern 18 | #endif 19 | 20 | typedef struct t_unix_ { 21 | t_socket sock; 22 | t_io io; 23 | t_buffer buf; 24 | t_timeout tm; 25 | } t_unix; 26 | typedef t_unix *p_unix; 27 | 28 | UNIX_API int luaopen_socket_unix(lua_State *L); 29 | 30 | #endif /* UNIX_H */ 31 | -------------------------------------------------------------------------------- /external/lpeg/lpprint.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lpprint.h,v 1.1 2013/03/21 20:25:12 roberto Exp $ 3 | */ 4 | 5 | 6 | #if !defined(lpprint_h) 7 | #define lpprint_h 8 | 9 | 10 | #include "lptree.h" 11 | #include "lpvm.h" 12 | 13 | 14 | #if defined(LPEG_DEBUG) 15 | 16 | void printpatt (Instruction *p, int n); 17 | void printtree (TTree *tree, int ident); 18 | void printktable (lua_State *L, int idx); 19 | void printcharset (const byte *st); 20 | void printcaplist (Capture *cap, Capture *limit); 21 | 22 | #else 23 | 24 | #define printktable(L,idx) \ 25 | luaL_error(L, "function only implemented in debug mode") 26 | #define printtree(tree,i) \ 27 | luaL_error(L, "function only implemented in debug mode") 28 | #define printpatt(p,n) \ 29 | luaL_error(L, "function only implemented in debug mode") 30 | 31 | #endif 32 | 33 | 34 | #endif 35 | 36 | -------------------------------------------------------------------------------- /external/sproto/msvcint.h: -------------------------------------------------------------------------------- 1 | #ifndef msvc_int_h 2 | #define msvc_int_h 3 | 4 | #ifdef _MSC_VER 5 | # define inline __inline 6 | # ifndef _MSC_STDINT_H_ 7 | # if (_MSC_VER < 1300) 8 | typedef signed char int8_t; 9 | typedef signed short int16_t; 10 | typedef signed int int32_t; 11 | typedef unsigned char uint8_t; 12 | typedef unsigned short uint16_t; 13 | typedef unsigned int uint32_t; 14 | # else 15 | typedef signed __int8 int8_t; 16 | typedef signed __int16 int16_t; 17 | typedef signed __int32 int32_t; 18 | typedef unsigned __int8 uint8_t; 19 | typedef unsigned __int16 uint16_t; 20 | typedef unsigned __int32 uint32_t; 21 | # endif 22 | typedef signed __int64 int64_t; 23 | typedef unsigned __int64 uint64_t; 24 | # endif 25 | 26 | #else 27 | 28 | #include 29 | 30 | #endif 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /external/lpeg/lpcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lpcode.h,v 1.5 2013/04/04 21:24:45 roberto Exp $ 3 | */ 4 | 5 | #if !defined(lpcode_h) 6 | #define lpcode_h 7 | 8 | #include "lua.h" 9 | 10 | #include "lptypes.h" 11 | #include "lptree.h" 12 | #include "lpvm.h" 13 | 14 | int tocharset (TTree *tree, Charset *cs); 15 | int checkaux (TTree *tree, int pred); 16 | int fixedlenx (TTree *tree, int count, int len); 17 | int hascaptures (TTree *tree); 18 | int lp_gc (lua_State *L); 19 | Instruction *compile (lua_State *L, Pattern *p); 20 | void reallocprog (lua_State *L, Pattern *p, int nsize); 21 | int sizei (const Instruction *i); 22 | 23 | 24 | #define PEnullable 0 25 | #define PEnofail 1 26 | 27 | #define nofail(t) checkaux(t, PEnofail) 28 | #define nullable(t) checkaux(t, PEnullable) 29 | 30 | #define fixedlen(t) fixedlenx(t, 0, 0) 31 | 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /external/luasocket/luasocket_scripts.h: -------------------------------------------------------------------------------- 1 | 2 | /* luasocket_scripts.h.h */ 3 | 4 | #ifndef __LUA_MODULES_606BA1C00D116CA81B695C141D124DB7_H_ 5 | #define __LUA_MODULES_606BA1C00D116CA81B695C141D124DB7_H_ 6 | 7 | #if __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | #include "lua.h" 12 | 13 | void luaopen_luasocket_scripts(lua_State* L); 14 | 15 | /* 16 | int luaopen_lua_m_ltn12(lua_State* L); 17 | int luaopen_lua_m_mime(lua_State* L); 18 | int luaopen_lua_m_socket_ftp(lua_State* L); 19 | int luaopen_lua_m_socket_headers(lua_State* L); 20 | int luaopen_lua_m_socket_http(lua_State* L); 21 | int luaopen_lua_m_socket_mbox(lua_State* L); 22 | int luaopen_lua_m_socket_smtp(lua_State* L); 23 | int luaopen_lua_m_socket_tp(lua_State* L); 24 | int luaopen_lua_m_socket_url(lua_State* L); 25 | int luaopen_lua_m_socket(lua_State* L); 26 | */ 27 | 28 | #if __cplusplus 29 | } 30 | #endif 31 | 32 | #endif /* __LUA_MODULES_606BA1C00D116CA81B695C141D124DB7_H_ */ 33 | -------------------------------------------------------------------------------- /external/luasocket/udp.h: -------------------------------------------------------------------------------- 1 | #ifndef UDP_H 2 | #define UDP_H 3 | /*=========================================================================*\ 4 | * UDP object 5 | * LuaSocket toolkit 6 | * 7 | * The udp.h module provides LuaSocket with support for UDP protocol 8 | * (AF_INET, SOCK_DGRAM). 9 | * 10 | * Two classes are defined: connected and unconnected. UDP objects are 11 | * originally unconnected. They can be "connected" to a given address 12 | * with a call to the setpeername function. The same function can be used to 13 | * break the connection. 14 | \*=========================================================================*/ 15 | #include "lua.h" 16 | 17 | #include "timeout.h" 18 | #include "socket.h" 19 | 20 | /* can't be larger than wsocket.c MAXCHUNK!!! */ 21 | #define UDP_DATAGRAMSIZE 8192 22 | 23 | typedef struct t_udp_ { 24 | t_socket sock; 25 | t_timeout tm; 26 | int family; 27 | } t_udp; 28 | typedef t_udp *p_udp; 29 | 30 | int udp_open(lua_State *L); 31 | 32 | #endif /* UDP_H */ 33 | -------------------------------------------------------------------------------- /external/luasocket/wsocket.h: -------------------------------------------------------------------------------- 1 | #ifndef WSOCKET_H 2 | #define WSOCKET_H 3 | /*=========================================================================*\ 4 | * Socket compatibilization module for Win32 5 | * LuaSocket toolkit 6 | \*=========================================================================*/ 7 | 8 | /*=========================================================================*\ 9 | * WinSock include files 10 | \*=========================================================================*/ 11 | #include 12 | #include 13 | 14 | typedef int socklen_t; 15 | typedef SOCKADDR_STORAGE t_sockaddr_storage; 16 | typedef SOCKET t_socket; 17 | typedef t_socket *p_socket; 18 | 19 | #ifndef IPV6_V6ONLY 20 | #define IPV6_V6ONLY 27 21 | #endif 22 | 23 | #define SOCKET_INVALID (INVALID_SOCKET) 24 | 25 | #ifndef SO_REUSEPORT 26 | #define SO_REUSEPORT SO_REUSEADDR 27 | #endif 28 | 29 | #ifndef AI_NUMERICSERV 30 | #define AI_NUMERICSERV (0) 31 | #endif 32 | 33 | #endif /* WSOCKET_H */ 34 | -------------------------------------------------------------------------------- /external/luasocket/timeout.h: -------------------------------------------------------------------------------- 1 | #ifndef TIMEOUT_H 2 | #define TIMEOUT_H 3 | /*=========================================================================*\ 4 | * Timeout management functions 5 | * LuaSocket toolkit 6 | \*=========================================================================*/ 7 | #include "lua.h" 8 | 9 | /* timeout control structure */ 10 | typedef struct t_timeout_ { 11 | double block; /* maximum time for blocking calls */ 12 | double total; /* total number of miliseconds for operation */ 13 | double start; /* time of start of operation */ 14 | } t_timeout; 15 | typedef t_timeout *p_timeout; 16 | 17 | int timeout_open(lua_State *L); 18 | void timeout_init(p_timeout tm, double block, double total); 19 | double timeout_get(p_timeout tm); 20 | double timeout_getretry(p_timeout tm); 21 | p_timeout timeout_markstart(p_timeout tm); 22 | double timeout_getstart(p_timeout tm); 23 | double timeout_gettime(void); 24 | int timeout_meth_settimeout(lua_State *L, p_timeout tm); 25 | 26 | #define timeout_iszero(tm) ((tm)->block == 0.0) 27 | 28 | #endif /* TIMEOUT_H */ 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 dot123 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /external/luasocket/tcp.h: -------------------------------------------------------------------------------- 1 | #ifndef TCP_H 2 | #define TCP_H 3 | /*=========================================================================*\ 4 | * TCP object 5 | * LuaSocket toolkit 6 | * 7 | * The tcp.h module is basicly a glue that puts together modules buffer.h, 8 | * timeout.h socket.h and inet.h to provide the LuaSocket TCP (AF_INET, 9 | * SOCK_STREAM) support. 10 | * 11 | * Three classes are defined: master, client and server. The master class is 12 | * a newly created tcp object, that has not been bound or connected. Server 13 | * objects are tcp objects bound to some local address. Client objects are 14 | * tcp objects either connected to some address or returned by the accept 15 | * method of a server object. 16 | \*=========================================================================*/ 17 | #include "lua.h" 18 | 19 | #include "buffer.h" 20 | #include "timeout.h" 21 | #include "socket.h" 22 | 23 | typedef struct t_tcp_ { 24 | t_socket sock; 25 | t_io io; 26 | t_buffer buf; 27 | t_timeout tm; 28 | int family; 29 | } t_tcp; 30 | 31 | typedef t_tcp *p_tcp; 32 | 33 | int tcp_open(lua_State *L); 34 | 35 | #endif /* TCP_H */ 36 | -------------------------------------------------------------------------------- /external/sproto/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 codingnow.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /external/luasocket/mime.h: -------------------------------------------------------------------------------- 1 | #ifndef MIME_H 2 | #define MIME_H 3 | /*=========================================================================*\ 4 | * Core MIME support 5 | * LuaSocket toolkit 6 | * 7 | * This module provides functions to implement transfer content encodings 8 | * and formatting conforming to RFC 2045. It is used by mime.lua, which 9 | * provide a higher level interface to this functionality. 10 | \*=========================================================================*/ 11 | #include "lua.h" 12 | 13 | /*-------------------------------------------------------------------------*\ 14 | * Current MIME library version 15 | \*-------------------------------------------------------------------------*/ 16 | #define MIME_VERSION "MIME 1.0.3" 17 | #define MIME_COPYRIGHT "Copyright (C) 2004-2013 Diego Nehab" 18 | #define MIME_AUTHORS "Diego Nehab" 19 | 20 | /*-------------------------------------------------------------------------*\ 21 | * This macro prefixes all exported API functions 22 | \*-------------------------------------------------------------------------*/ 23 | #ifndef MIME_API 24 | #define MIME_API extern 25 | #endif 26 | 27 | MIME_API int luaopen_mime_core(lua_State *L); 28 | 29 | #endif /* MIME_H */ 30 | -------------------------------------------------------------------------------- /external/lpeg/lpcap.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lpcap.h,v 1.1 2013/03/21 20:25:12 roberto Exp $ 3 | */ 4 | 5 | #if !defined(lpcap_h) 6 | #define lpcap_h 7 | 8 | 9 | #include "lptypes.h" 10 | 11 | 12 | /* kinds of captures */ 13 | typedef enum CapKind { 14 | Cclose, Cposition, Cconst, Cbackref, Carg, Csimple, Ctable, Cfunction, 15 | Cquery, Cstring, Cnum, Csubst, Cfold, Cruntime, Cgroup 16 | } CapKind; 17 | 18 | 19 | typedef struct Capture { 20 | const char *s; /* subject position */ 21 | short idx; /* extra info about capture (group name, arg index, etc.) */ 22 | byte kind; /* kind of capture */ 23 | byte siz; /* size of full capture + 1 (0 = not a full capture) */ 24 | } Capture; 25 | 26 | 27 | typedef struct CapState { 28 | Capture *cap; /* current capture */ 29 | Capture *ocap; /* (original) capture list */ 30 | lua_State *L; 31 | int ptop; /* index of last argument to 'match' */ 32 | const char *s; /* original string */ 33 | int valuecached; /* value stored in cache slot */ 34 | } CapState; 35 | 36 | 37 | int runtimecap (CapState *cs, Capture *close, const char *s, int *rem); 38 | int getcaptures (lua_State *L, const char *s, const char *r, int ptop); 39 | int finddyncap (Capture *cap, Capture *last); 40 | 41 | #endif 42 | 43 | 44 | -------------------------------------------------------------------------------- /external/luasocket/luasocket.h: -------------------------------------------------------------------------------- 1 | #ifndef LUASOCKET_H 2 | #define LUASOCKET_H 3 | /*=========================================================================*\ 4 | * LuaSocket toolkit 5 | * Networking support for the Lua language 6 | * Diego Nehab 7 | * 9/11/1999 8 | \*=========================================================================*/ 9 | #include "lua.h" 10 | 11 | /*-------------------------------------------------------------------------*\ 12 | * Current socket library version 13 | \*-------------------------------------------------------------------------*/ 14 | #define LUASOCKET_VERSION "LuaSocket 3.0-rc1" 15 | #define LUASOCKET_COPYRIGHT "Copyright (C) 1999-2013 Diego Nehab" 16 | 17 | /*-------------------------------------------------------------------------*\ 18 | * This macro prefixes all exported API functions 19 | \*-------------------------------------------------------------------------*/ 20 | #ifndef LUASOCKET_API 21 | #define LUASOCKET_API extern 22 | #endif 23 | 24 | /*-------------------------------------------------------------------------*\ 25 | * Initializes the library. 26 | \*-------------------------------------------------------------------------*/ 27 | LUASOCKET_API int luaopen_socket_core(lua_State *L); 28 | 29 | #endif /* LUASOCKET_H */ 30 | -------------------------------------------------------------------------------- /external/luasocket/io.c: -------------------------------------------------------------------------------- 1 | /*=========================================================================*\ 2 | * Input/Output abstraction 3 | * LuaSocket toolkit 4 | \*=========================================================================*/ 5 | #include "io.h" 6 | 7 | /*=========================================================================*\ 8 | * Exported functions 9 | \*=========================================================================*/ 10 | /*-------------------------------------------------------------------------*\ 11 | * Initializes C structure 12 | \*-------------------------------------------------------------------------*/ 13 | void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx) { 14 | io->send = send; 15 | io->recv = recv; 16 | io->error = error; 17 | io->ctx = ctx; 18 | } 19 | 20 | /*-------------------------------------------------------------------------*\ 21 | * I/O error strings 22 | \*-------------------------------------------------------------------------*/ 23 | const char *io_strerror(int err) { 24 | switch (err) { 25 | case IO_DONE: return NULL; 26 | case IO_CLOSED: return "closed"; 27 | case IO_TIMEOUT: return "timeout"; 28 | default: return "unknown error"; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /external/luasocket/except.h: -------------------------------------------------------------------------------- 1 | #ifndef EXCEPT_H 2 | #define EXCEPT_H 3 | /*=========================================================================*\ 4 | * Exception control 5 | * LuaSocket toolkit (but completely independent from other modules) 6 | * 7 | * This provides support for simple exceptions in Lua. During the 8 | * development of the HTTP/FTP/SMTP support, it became aparent that 9 | * error checking was taking a substantial amount of the coding. These 10 | * function greatly simplify the task of checking errors. 11 | * 12 | * The main idea is that functions should return nil as its first return 13 | * value when it finds an error, and return an error message (or value) 14 | * following nil. In case of success, as long as the first value is not nil, 15 | * the other values don't matter. 16 | * 17 | * The idea is to nest function calls with the "try" function. This function 18 | * checks the first value, and calls "error" on the second if the first is 19 | * nil. Otherwise, it returns all values it received. 20 | * 21 | * The protect function returns a new function that behaves exactly like the 22 | * function it receives, but the new function doesn't throw exceptions: it 23 | * returns nil followed by the error message instead. 24 | * 25 | * With these two function, it's easy to write functions that throw 26 | * exceptions on error, but that don't interrupt the user script. 27 | \*=========================================================================*/ 28 | 29 | #include "lua.h" 30 | 31 | int except_open(lua_State *L); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /external/sproto/sproto.h: -------------------------------------------------------------------------------- 1 | #ifndef sproto_h 2 | #define sproto_h 3 | 4 | #include 5 | 6 | struct sproto; 7 | struct sproto_type; 8 | 9 | #define SPROTO_REQUEST 0 10 | #define SPROTO_RESPONSE 1 11 | 12 | #define SPROTO_TINTEGER 0 13 | #define SPROTO_TBOOLEAN 1 14 | #define SPROTO_TSTRING 2 15 | #define SPROTO_TSTRUCT 3 16 | 17 | #define SPROTO_CB_ERROR -1 18 | #define SPROTO_CB_NIL -2 19 | #define SPROTO_CB_NOARRAY -3 20 | 21 | struct sproto * sproto_create(const void * proto, size_t sz); 22 | void sproto_release(struct sproto *); 23 | 24 | int sproto_prototag(const struct sproto *, const char * name); 25 | const char * sproto_protoname(const struct sproto *, int proto); 26 | // SPROTO_REQUEST(0) : request, SPROTO_RESPONSE(1): response 27 | struct sproto_type * sproto_protoquery(const struct sproto *, int proto, int what); 28 | 29 | struct sproto_type * sproto_type(const struct sproto *, const char * type_name); 30 | 31 | int sproto_pack(const void * src, int srcsz, void * buffer, int bufsz); 32 | int sproto_unpack(const void * src, int srcsz, void * buffer, int bufsz); 33 | 34 | struct sproto_arg { 35 | void *ud; 36 | const char *tagname; 37 | int tagid; 38 | int type; 39 | struct sproto_type *subtype; 40 | void *value; 41 | int length; 42 | int index; // array base 1 43 | int mainindex; // for map 44 | int decimal; // for decimal 45 | }; 46 | 47 | typedef int (*sproto_callback)(const struct sproto_arg *args); 48 | 49 | int sproto_decode(const struct sproto_type *, const void * data, int size, sproto_callback cb, void *ud); 50 | int sproto_encode(const struct sproto_type *, void * buffer, int size, sproto_callback cb, void *ud); 51 | 52 | // for debug use 53 | void sproto_dump(struct sproto *); 54 | const char * sproto_name(struct sproto_type *); 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /external/lpeg/lptree.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lptree.h,v 1.2 2013/03/24 13:51:12 roberto Exp $ 3 | */ 4 | 5 | #if !defined(lptree_h) 6 | #define lptree_h 7 | 8 | 9 | #include "lptypes.h" 10 | 11 | 12 | /* 13 | ** types of trees 14 | */ 15 | typedef enum TTag { 16 | TChar = 0, TSet, TAny, /* standard PEG elements */ 17 | TTrue, TFalse, 18 | TRep, 19 | TSeq, TChoice, 20 | TNot, TAnd, 21 | TCall, 22 | TOpenCall, 23 | TRule, /* sib1 is rule's pattern, sib2 is 'next' rule */ 24 | TGrammar, /* sib1 is initial (and first) rule */ 25 | TBehind, /* match behind */ 26 | TCapture, /* regular capture */ 27 | TRunTime /* run-time capture */ 28 | } TTag; 29 | 30 | /* number of siblings for each tree */ 31 | extern const byte numsiblings[]; 32 | 33 | 34 | /* 35 | ** Tree trees 36 | ** The first sibling of a tree (if there is one) is immediately after 37 | ** the tree. A reference to a second sibling (ps) is its position 38 | ** relative to the position of the tree itself. A key in ktable 39 | ** uses the (unique) address of the original tree that created that 40 | ** entry. NULL means no data. 41 | */ 42 | typedef struct TTree { 43 | byte tag; 44 | byte cap; /* kind of capture (if it is a capture) */ 45 | unsigned short key; /* key in ktable for Lua data (0 if no key) */ 46 | union { 47 | int ps; /* occasional second sibling */ 48 | int n; /* occasional counter */ 49 | } u; 50 | } TTree; 51 | 52 | 53 | /* 54 | ** A complete pattern has its tree plus, if already compiled, 55 | ** its corresponding code 56 | */ 57 | typedef struct Pattern { 58 | union Instruction *code; 59 | int codesize; 60 | TTree tree[1]; 61 | } Pattern; 62 | 63 | 64 | /* number of siblings for each tree */ 65 | extern const byte numsiblings[]; 66 | 67 | /* access to siblings */ 68 | #define sib1(t) ((t) + 1) 69 | #define sib2(t) ((t) + (t)->u.ps) 70 | 71 | 72 | 73 | 74 | 75 | 76 | #endif 77 | 78 | -------------------------------------------------------------------------------- /external/luasocket/buffer.h: -------------------------------------------------------------------------------- 1 | #ifndef BUF_H 2 | #define BUF_H 3 | /*=========================================================================*\ 4 | * Input/Output interface for Lua programs 5 | * LuaSocket toolkit 6 | * 7 | * Line patterns require buffering. Reading one character at a time involves 8 | * too many system calls and is very slow. This module implements the 9 | * LuaSocket interface for input/output on connected objects, as seen by 10 | * Lua programs. 11 | * 12 | * Input is buffered. Output is *not* buffered because there was no simple 13 | * way of making sure the buffered output data would ever be sent. 14 | * 15 | * The module is built on top of the I/O abstraction defined in io.h and the 16 | * timeout management is done with the timeout.h interface. 17 | \*=========================================================================*/ 18 | #include "lua.h" 19 | 20 | #include "io.h" 21 | #include "timeout.h" 22 | 23 | /* buffer size in bytes */ 24 | #define BUF_SIZE 8192 25 | 26 | /* buffer control structure */ 27 | typedef struct t_buffer_ { 28 | double birthday; /* throttle support info: creation time, */ 29 | size_t sent, received; /* bytes sent, and bytes received */ 30 | p_io io; /* IO driver used for this buffer */ 31 | p_timeout tm; /* timeout management for this buffer */ 32 | size_t first, last; /* index of first and last bytes of stored data */ 33 | char data[BUF_SIZE]; /* storage space for buffer data */ 34 | } t_buffer; 35 | typedef t_buffer *p_buffer; 36 | 37 | int buffer_open(lua_State *L); 38 | void buffer_init(p_buffer buf, p_io io, p_timeout tm); 39 | int buffer_meth_send(lua_State *L, p_buffer buf); 40 | int buffer_meth_receive(lua_State *L, p_buffer buf); 41 | int buffer_meth_getstats(lua_State *L, p_buffer buf); 42 | int buffer_meth_setstats(lua_State *L, p_buffer buf); 43 | int buffer_isempty(p_buffer buf); 44 | 45 | #endif /* BUF_H */ 46 | -------------------------------------------------------------------------------- /external/luasocket/usocket.h: -------------------------------------------------------------------------------- 1 | #ifndef USOCKET_H 2 | #define USOCKET_H 3 | /*=========================================================================*\ 4 | * Socket compatibilization module for Unix 5 | * LuaSocket toolkit 6 | \*=========================================================================*/ 7 | 8 | /*=========================================================================*\ 9 | * BSD include files 10 | \*=========================================================================*/ 11 | /* error codes */ 12 | #include 13 | /* close function */ 14 | #include 15 | /* fnctnl function and associated constants */ 16 | #include 17 | /* struct sockaddr */ 18 | #include 19 | /* socket function */ 20 | #include 21 | /* struct timeval */ 22 | #include 23 | /* gethostbyname and gethostbyaddr functions */ 24 | #include 25 | /* sigpipe handling */ 26 | #include 27 | /* IP stuff*/ 28 | #include 29 | #include 30 | /* TCP options (nagle algorithm disable) */ 31 | #include 32 | #include 33 | 34 | #ifndef SO_REUSEPORT 35 | #define SO_REUSEPORT SO_REUSEADDR 36 | #endif 37 | 38 | /* Some platforms use IPV6_JOIN_GROUP instead if 39 | * IPV6_ADD_MEMBERSHIP. The semantics are same, though. */ 40 | #ifndef IPV6_ADD_MEMBERSHIP 41 | #ifdef IPV6_JOIN_GROUP 42 | #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP 43 | #endif /* IPV6_JOIN_GROUP */ 44 | #endif /* !IPV6_ADD_MEMBERSHIP */ 45 | 46 | /* Same with IPV6_DROP_MEMBERSHIP / IPV6_LEAVE_GROUP. */ 47 | #ifndef IPV6_DROP_MEMBERSHIP 48 | #ifdef IPV6_LEAVE_GROUP 49 | #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP 50 | #endif /* IPV6_LEAVE_GROUP */ 51 | #endif /* !IPV6_DROP_MEMBERSHIP */ 52 | 53 | typedef int t_socket; 54 | typedef t_socket *p_socket; 55 | typedef struct sockaddr_storage t_sockaddr_storage; 56 | 57 | #define SOCKET_INVALID (-1) 58 | 59 | #endif /* USOCKET_H */ 60 | -------------------------------------------------------------------------------- /external/luasocket/inet.h: -------------------------------------------------------------------------------- 1 | #ifndef INET_H 2 | #define INET_H 3 | /*=========================================================================*\ 4 | * Internet domain functions 5 | * LuaSocket toolkit 6 | * 7 | * This module implements the creation and connection of internet domain 8 | * sockets, on top of the socket.h interface, and the interface of with the 9 | * resolver. 10 | * 11 | * The function inet_aton is provided for the platforms where it is not 12 | * available. The module also implements the interface of the internet 13 | * getpeername and getsockname functions as seen by Lua programs. 14 | * 15 | * The Lua functions toip and tohostname are also implemented here. 16 | \*=========================================================================*/ 17 | #include "lua.h" 18 | #include "socket.h" 19 | #include "timeout.h" 20 | 21 | #ifdef _WIN32 22 | #define LUASOCKET_INET_ATON 23 | #endif 24 | 25 | int inet_open(lua_State *L); 26 | 27 | const char *inet_trycreate(p_socket ps, int family, int type); 28 | const char *inet_tryconnect(p_socket ps, int *family, const char *address, 29 | const char *serv, p_timeout tm, struct addrinfo *connecthints); 30 | const char *inet_trybind(p_socket ps, const char *address, const char *serv, 31 | struct addrinfo *bindhints); 32 | const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm); 33 | const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm); 34 | 35 | int inet_meth_getpeername(lua_State *L, p_socket ps, int family); 36 | int inet_meth_getsockname(lua_State *L, p_socket ps, int family); 37 | 38 | int inet_optfamily(lua_State* L, int narg, const char* def); 39 | int inet_optsocktype(lua_State* L, int narg, const char* def); 40 | 41 | #ifdef LUASOCKET_INET_ATON 42 | int inet_aton(const char *cp, struct in_addr *inp); 43 | #endif 44 | 45 | #ifdef LUASOCKET_INET_PTON 46 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt); 47 | int inet_pton(int af, const char *src, void *dst); 48 | #endif 49 | 50 | #endif /* INET_H */ 51 | -------------------------------------------------------------------------------- /external/lpeg/lpvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lpvm.h,v 1.2 2013/04/03 20:37:18 roberto Exp $ 3 | */ 4 | 5 | #if !defined(lpvm_h) 6 | #define lpvm_h 7 | 8 | #include "lpcap.h" 9 | 10 | 11 | /* Virtual Machine's instructions */ 12 | typedef enum Opcode { 13 | IAny, /* if no char, fail */ 14 | IChar, /* if char != aux, fail */ 15 | ISet, /* if char not in buff, fail */ 16 | ITestAny, /* in no char, jump to 'offset' */ 17 | ITestChar, /* if char != aux, jump to 'offset' */ 18 | ITestSet, /* if char not in buff, jump to 'offset' */ 19 | ISpan, /* read a span of chars in buff */ 20 | IBehind, /* walk back 'aux' characters (fail if not possible) */ 21 | IRet, /* return from a rule */ 22 | IEnd, /* end of pattern */ 23 | IChoice, /* stack a choice; next fail will jump to 'offset' */ 24 | IJmp, /* jump to 'offset' */ 25 | ICall, /* call rule at 'offset' */ 26 | IOpenCall, /* call rule number 'key' (must be closed to a ICall) */ 27 | ICommit, /* pop choice and jump to 'offset' */ 28 | IPartialCommit, /* update top choice to current position and jump */ 29 | IBackCommit, /* "fails" but jump to its own 'offset' */ 30 | IFailTwice, /* pop one choice and then fail */ 31 | IFail, /* go back to saved state on choice and jump to saved offset */ 32 | IGiveup, /* internal use */ 33 | IFullCapture, /* complete capture of last 'off' chars */ 34 | IOpenCapture, /* start a capture */ 35 | ICloseCapture, 36 | ICloseRunTime 37 | } Opcode; 38 | 39 | 40 | 41 | typedef union Instruction { 42 | struct Inst { 43 | byte code; 44 | byte aux; 45 | short key; 46 | } i; 47 | int offset; 48 | byte buff[1]; 49 | } Instruction; 50 | 51 | 52 | int getposition (lua_State *L, int t, int i); 53 | void printpatt (Instruction *p, int n); 54 | const char *match (lua_State *L, const char *o, const char *s, const char *e, 55 | Instruction *op, Capture *capture, int ptop); 56 | int verify (lua_State *L, Instruction *op, const Instruction *p, 57 | Instruction *e, int postable, int rule); 58 | void checkrule (lua_State *L, Instruction *op, int from, int to, 59 | int postable, int rule); 60 | 61 | 62 | #endif 63 | 64 | -------------------------------------------------------------------------------- /external/luasocket/auxiliar.h: -------------------------------------------------------------------------------- 1 | #ifndef AUXILIAR_H 2 | #define AUXILIAR_H 3 | /*=========================================================================*\ 4 | * Auxiliar routines for class hierarchy manipulation 5 | * LuaSocket toolkit (but completely independent of other LuaSocket modules) 6 | * 7 | * A LuaSocket class is a name associated with Lua metatables. A LuaSocket 8 | * group is a name associated with a class. A class can belong to any number 9 | * of groups. This module provides the functionality to: 10 | * 11 | * - create new classes 12 | * - add classes to groups 13 | * - set the class of objects 14 | * - check if an object belongs to a given class or group 15 | * - get the userdata associated to objects 16 | * - print objects in a pretty way 17 | * 18 | * LuaSocket class names follow the convention {}. Modules 19 | * can define any number of classes and groups. The module tcp.c, for 20 | * example, defines the classes tcp{master}, tcp{client} and tcp{server} and 21 | * the groups tcp{client,server} and tcp{any}. Module functions can then 22 | * perform type-checking on their arguments by either class or group. 23 | * 24 | * LuaSocket metatables define the __index metamethod as being a table. This 25 | * table has one field for each method supported by the class, and a field 26 | * "class" with the class name. 27 | * 28 | * The mapping from class name to the corresponding metatable and the 29 | * reverse mapping are done using lauxlib. 30 | \*=========================================================================*/ 31 | 32 | #include "lua.h" 33 | #include "lauxlib.h" 34 | 35 | int auxiliar_open(lua_State *L); 36 | void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func); 37 | void auxiliar_add2group(lua_State *L, const char *classname, const char *group); 38 | void auxiliar_setclass(lua_State *L, const char *classname, int objidx); 39 | void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx); 40 | void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx); 41 | void *auxiliar_getclassudata(lua_State *L, const char *groupname, int objidx); 42 | void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx); 43 | int auxiliar_checkboolean(lua_State *L, int objidx); 44 | int auxiliar_tostring(lua_State *L); 45 | int auxiliar_typeerror(lua_State *L, int narg, const char *tname); 46 | 47 | #endif /* AUXILIAR_H */ 48 | -------------------------------------------------------------------------------- /external/luasocket/io.h: -------------------------------------------------------------------------------- 1 | #ifndef IO_H 2 | #define IO_H 3 | /*=========================================================================*\ 4 | * Input/Output abstraction 5 | * LuaSocket toolkit 6 | * 7 | * This module defines the interface that LuaSocket expects from the 8 | * transport layer for streamed input/output. The idea is that if any 9 | * transport implements this interface, then the buffer.c functions 10 | * automatically work on it. 11 | * 12 | * The module socket.h implements this interface, and thus the module tcp.h 13 | * is very simple. 14 | \*=========================================================================*/ 15 | #include 16 | #include "lua.h" 17 | 18 | #include "timeout.h" 19 | 20 | /* IO error codes */ 21 | enum { 22 | IO_DONE = 0, /* operation completed successfully */ 23 | IO_TIMEOUT = -1, /* operation timed out */ 24 | IO_CLOSED = -2, /* the connection has been closed */ 25 | IO_UNKNOWN = -3 26 | }; 27 | 28 | /* interface to error message function */ 29 | typedef const char *(*p_error) ( 30 | void *ctx, /* context needed by send */ 31 | int err /* error code */ 32 | ); 33 | 34 | /* interface to send function */ 35 | typedef int (*p_send) ( 36 | void *ctx, /* context needed by send */ 37 | const char *data, /* pointer to buffer with data to send */ 38 | size_t count, /* number of bytes to send from buffer */ 39 | size_t *sent, /* number of bytes sent uppon return */ 40 | p_timeout tm /* timeout control */ 41 | ); 42 | 43 | /* interface to recv function */ 44 | typedef int (*p_recv) ( 45 | void *ctx, /* context needed by recv */ 46 | char *data, /* pointer to buffer where data will be writen */ 47 | size_t count, /* number of bytes to receive into buffer */ 48 | size_t *got, /* number of bytes received uppon return */ 49 | p_timeout tm /* timeout control */ 50 | ); 51 | 52 | /* IO driver definition */ 53 | typedef struct t_io_ { 54 | void *ctx; /* context needed by send/recv */ 55 | p_send send; /* send function pointer */ 56 | p_recv recv; /* receive function pointer */ 57 | p_error error; /* strerror function */ 58 | } t_io; 59 | typedef t_io *p_io; 60 | 61 | void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx); 62 | const char *io_strerror(int err); 63 | 64 | #endif /* IO_H */ 65 | 66 | -------------------------------------------------------------------------------- /external/luasocket/options.h: -------------------------------------------------------------------------------- 1 | #ifndef OPTIONS_H 2 | #define OPTIONS_H 3 | /*=========================================================================*\ 4 | * Common option interface 5 | * LuaSocket toolkit 6 | * 7 | * This module provides a common interface to socket options, used mainly by 8 | * modules UDP and TCP. 9 | \*=========================================================================*/ 10 | 11 | #include "lua.h" 12 | #include "socket.h" 13 | 14 | /* option registry */ 15 | typedef struct t_opt { 16 | const char *name; 17 | int (*func)(lua_State *L, p_socket ps); 18 | } t_opt; 19 | typedef t_opt *p_opt; 20 | 21 | /* supported options for setoption */ 22 | int opt_set_dontroute(lua_State *L, p_socket ps); 23 | int opt_set_broadcast(lua_State *L, p_socket ps); 24 | int opt_set_reuseaddr(lua_State *L, p_socket ps); 25 | int opt_set_tcp_nodelay(lua_State *L, p_socket ps); 26 | int opt_set_keepalive(lua_State *L, p_socket ps); 27 | int opt_set_linger(lua_State *L, p_socket ps); 28 | int opt_set_reuseaddr(lua_State *L, p_socket ps); 29 | int opt_set_reuseport(lua_State *L, p_socket ps); 30 | int opt_set_ip_multicast_if(lua_State *L, p_socket ps); 31 | int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps); 32 | int opt_set_ip_multicast_loop(lua_State *L, p_socket ps); 33 | int opt_set_ip_add_membership(lua_State *L, p_socket ps); 34 | int opt_set_ip_drop_membersip(lua_State *L, p_socket ps); 35 | int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps); 36 | int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps); 37 | int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps); 38 | int opt_set_ip6_add_membership(lua_State *L, p_socket ps); 39 | int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps); 40 | int opt_set_ip6_v6only(lua_State *L, p_socket ps); 41 | 42 | /* supported options for getoption */ 43 | int opt_get_reuseaddr(lua_State *L, p_socket ps); 44 | int opt_get_tcp_nodelay(lua_State *L, p_socket ps); 45 | int opt_get_keepalive(lua_State *L, p_socket ps); 46 | int opt_get_linger(lua_State *L, p_socket ps); 47 | int opt_get_reuseaddr(lua_State *L, p_socket ps); 48 | int opt_get_ip_multicast_loop(lua_State *L, p_socket ps); 49 | int opt_get_ip_multicast_if(lua_State *L, p_socket ps); 50 | int opt_get_error(lua_State *L, p_socket ps); 51 | int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps); 52 | int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps); 53 | int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps); 54 | int opt_get_ip6_v6only(lua_State *L, p_socket ps); 55 | 56 | /* invokes the appropriate option handler */ 57 | int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps); 58 | int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps); 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /external/luasocket/script/mime.lua: -------------------------------------------------------------------------------- 1 | ----------------------------------------------------------------------------- 2 | -- MIME support for the Lua language. 3 | -- Author: Diego Nehab 4 | -- Conforming to RFCs 2045-2049 5 | ----------------------------------------------------------------------------- 6 | 7 | ----------------------------------------------------------------------------- 8 | -- Declare module and import dependencies 9 | ----------------------------------------------------------------------------- 10 | local base = _G 11 | local ltn12 = require("ltn12") 12 | local mime = require("mime.core") 13 | local io = require("io") 14 | local string = require("string") 15 | local _M = mime 16 | 17 | -- encode, decode and wrap algorithm tables 18 | local encodet, decodet, wrapt = {},{},{} 19 | 20 | _M.encodet = encodet 21 | _M.decodet = decodet 22 | _M.wrapt = wrapt 23 | 24 | -- creates a function that chooses a filter by name from a given table 25 | local function choose(table) 26 | return function(name, opt1, opt2) 27 | if base.type(name) ~= "string" then 28 | name, opt1, opt2 = "default", name, opt1 29 | end 30 | local f = table[name or "nil"] 31 | if not f then 32 | base.error("unknown key (" .. base.tostring(name) .. ")", 3) 33 | else return f(opt1, opt2) end 34 | end 35 | end 36 | 37 | -- define the encoding filters 38 | encodet['base64'] = function() 39 | return ltn12.filter.cycle(_M.b64, "") 40 | end 41 | 42 | encodet['quoted-printable'] = function(mode) 43 | return ltn12.filter.cycle(_M.qp, "", 44 | (mode == "binary") and "=0D=0A" or "\r\n") 45 | end 46 | 47 | -- define the decoding filters 48 | decodet['base64'] = function() 49 | return ltn12.filter.cycle(_M.unb64, "") 50 | end 51 | 52 | decodet['quoted-printable'] = function() 53 | return ltn12.filter.cycle(_M.unqp, "") 54 | end 55 | 56 | local function format(chunk) 57 | if chunk then 58 | if chunk == "" then return "''" 59 | else return string.len(chunk) end 60 | else return "nil" end 61 | end 62 | 63 | -- define the line-wrap filters 64 | wrapt['text'] = function(length) 65 | length = length or 76 66 | return ltn12.filter.cycle(_M.wrp, length, length) 67 | end 68 | wrapt['base64'] = wrapt['text'] 69 | wrapt['default'] = wrapt['text'] 70 | 71 | wrapt['quoted-printable'] = function() 72 | return ltn12.filter.cycle(_M.qpwrp, 76, 76) 73 | end 74 | 75 | -- function that choose the encoding, decoding or wrap algorithm 76 | _M.encode = choose(encodet) 77 | _M.decode = choose(decodet) 78 | _M.wrap = choose(wrapt) 79 | 80 | -- define the end-of-line normalization filter 81 | function _M.normalize(marker) 82 | return ltn12.filter.cycle(_M.eol, 0, marker) 83 | end 84 | 85 | -- high level stuffing filter 86 | function _M.stuff() 87 | return ltn12.filter.cycle(_M.dot, 2) 88 | end 89 | 90 | return _M -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 在cocos2d-x/external/lua 目录下新建四个文件夹sproto,bitop,lpeg,lpack。然后将各自的文件放入其中,为了符合cocos2dx的规范,需要在bitop中建立一个bit.h文件,内容如下: 2 | ``` 3 | #ifndef __LUA_BITOP_H_ 4 | #define __LUA_BITOP_H_ 5 | 6 | #if __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | #include "lauxlib.h" 11 | 12 | LUALIB_API int luaopen_bit(lua_State *L); 13 | 14 | #if __cplusplus 15 | } 16 | #endif 17 | 18 | #endif 19 | ``` 20 | 在lpeg中建立一个lpeg.h文件,内容如下: 21 | ``` 22 | #ifndef __LUA_LPEG_H_ 23 | #define __LUA_LPEG_H_ 24 | 25 | #if __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | #include "lpeg/lptypes.h" 30 | #include "lpeg/lpcap.h" 31 | #include "lpeg/lpcode.h" 32 | #include "lpeg/lpprint.h" 33 | #include "lpeg/lptree.h" 34 | #include "lpeg/lpvm.h" 35 | 36 | int luaopen_lpeg(lua_State *L); 37 | 38 | 39 | #if __cplusplus 40 | } 41 | #endif 42 | 43 | #endif 44 | ``` 45 | 在sproto中建立一个lsproto.h文件,内容如下: 46 | ``` 47 | #ifndef __LUA_LSPROTO_H_ 48 | #define __LUA_LSPROTO_H_ 49 | 50 | #if __cplusplus 51 | extern "C" { 52 | #endif 53 | 54 | #include "lauxlib.h" 55 | 56 | int luaopen_sproto_core(lua_State *L); 57 | 58 | #if __cplusplus 59 | } 60 | #endif 61 | 62 | #endif 63 | ``` 64 | 在lpack中建立一个lpack.h文件,内容如下: 65 | ``` 66 | #ifndef __LUA_LPACK_H_ 67 | #define __LUA_LPACK_H_ 68 | #if __cplusplus 69 | extern "C" { 70 | #endif 71 | 72 | #include "lauxlib.h" 73 | 74 | int luaopen_pack(lua_State *L); 75 | 76 | #if __cplusplus 77 | } 78 | #endif 79 | #endif 80 | ``` 81 | 82 | 在cocos2d-x/cocos/scripting/lua-bindings/manual/network目录下,找到 lua_extensions.c 文件。在头部包含所需文件。 83 | ``` 84 | #include "sproto/lsproto.h" 85 | #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) 86 | #include "bitop/bit.h" 87 | #endif 88 | #include "lpeg/lpeg.h" 89 | #include "lpack/lpack.h" 90 | 91 | 在luax_exts内,加入下列几行。 92 | 93 | { "sproto.core", luaopen_sproto_core }, 94 | #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) 95 | { "bit", luaopen_bit }, 96 | #endif 97 | { "lpeg", luaopen_lpeg }, 98 | { "string", luaopen_pack }, 99 | 100 | ``` 101 | Android支持 102 | 103 | 在cocos2d-x/cocos/scripting/lua-bindings/proj.android目录下,打开 Android.mk 文件,在那一长串加载c文件后面,加入我们需要的c文件 104 | ``` 105 | ../../../../external/lua/lpeg/lpcap.c \ 106 | ../../../../external/lua/lpeg/lpcode.c \ 107 | ../../../../external/lua/lpeg/lpprint.c \ 108 | ../../../../external/lua/lpeg/lptree.c \ 109 | ../../../../external/lua/lpeg/lpvm.c \ 110 | ../../../../external/lua/sproto/lsproto.c \ 111 | ../../../../external/lua/sproto/sproto.c \ 112 | ../../../../external/lua/lpack/lpack.c 113 | ``` 114 | Android支持luajit库,里面已经包含了bit库,所以不用加bit.c了,不然编译的时候会出现多重定义的错误。 115 | 116 | 总得来说还是很简单的,只需三步: 117 | 文件放到cocos2d-x/external/lua目录下 118 | 修改lua_extensions.c,包含相关文件 119 | 修改Android.mk做Android支持 120 | 121 | https://github.com/dot123/cocos2d-lua-sproto.git 122 | -------------------------------------------------------------------------------- /external/luasocket/script/socket/mbox.lua: -------------------------------------------------------------------------------- 1 | local _M = {} 2 | 3 | if module then 4 | mbox = _M 5 | end 6 | 7 | function _M.split_message(message_s) 8 | local message = {} 9 | message_s = string.gsub(message_s, "\r\n", "\n") 10 | string.gsub(message_s, "^(.-\n)\n", function (h) message.headers = h end) 11 | string.gsub(message_s, "^.-\n\n(.*)", function (b) message.body = b end) 12 | if not message.body then 13 | string.gsub(message_s, "^\n(.*)", function (b) message.body = b end) 14 | end 15 | if not message.headers and not message.body then 16 | message.headers = message_s 17 | end 18 | return message.headers or "", message.body or "" 19 | end 20 | 21 | function _M.split_headers(headers_s) 22 | local headers = {} 23 | headers_s = string.gsub(headers_s, "\r\n", "\n") 24 | headers_s = string.gsub(headers_s, "\n[ ]+", " ") 25 | string.gsub("\n" .. headers_s, "\n([^\n]+)", function (h) table.insert(headers, h) end) 26 | return headers 27 | end 28 | 29 | function _M.parse_header(header_s) 30 | header_s = string.gsub(header_s, "\n[ ]+", " ") 31 | header_s = string.gsub(header_s, "\n+", "") 32 | local _, __, name, value = string.find(header_s, "([^%s:]-):%s*(.*)") 33 | return name, value 34 | end 35 | 36 | function _M.parse_headers(headers_s) 37 | local headers_t = _M.split_headers(headers_s) 38 | local headers = {} 39 | for i = 1, #headers_t do 40 | local name, value = _M.parse_header(headers_t[i]) 41 | if name then 42 | name = string.lower(name) 43 | if headers[name] then 44 | headers[name] = headers[name] .. ", " .. value 45 | else headers[name] = value end 46 | end 47 | end 48 | return headers 49 | end 50 | 51 | function _M.parse_from(from) 52 | local _, __, name, address = string.find(from, "^%s*(.-)%s*%<(.-)%>") 53 | if not address then 54 | _, __, address = string.find(from, "%s*(.+)%s*") 55 | end 56 | name = name or "" 57 | address = address or "" 58 | if name == "" then name = address end 59 | name = string.gsub(name, '"', "") 60 | return name, address 61 | end 62 | 63 | function _M.split_mbox(mbox_s) 64 | mbox = {} 65 | mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n" 66 | local nj, i, j = 1, 1, 1 67 | while 1 do 68 | i, nj = string.find(mbox_s, "\n\nFrom .-\n", j) 69 | if not i then break end 70 | local message = string.sub(mbox_s, j, i-1) 71 | table.insert(mbox, message) 72 | j = nj+1 73 | end 74 | return mbox 75 | end 76 | 77 | function _M.parse(mbox_s) 78 | local mbox = _M.split_mbox(mbox_s) 79 | for i = 1, #mbox do 80 | mbox[i] = _M.parse_message(mbox[i]) 81 | end 82 | return mbox 83 | end 84 | 85 | function _M.parse_message(message_s) 86 | local message = {} 87 | message.headers, message.body = _M.split_message(message_s) 88 | message.headers = _M.parse_headers(message.headers) 89 | return message 90 | end 91 | 92 | return _M 93 | -------------------------------------------------------------------------------- /external/luasocket/except.c: -------------------------------------------------------------------------------- 1 | /*=========================================================================*\ 2 | * Simple exception support 3 | * LuaSocket toolkit 4 | \*=========================================================================*/ 5 | #include 6 | 7 | #include "lua.h" 8 | #include "lauxlib.h" 9 | 10 | #include "except.h" 11 | 12 | /*=========================================================================*\ 13 | * Internal function prototypes. 14 | \*=========================================================================*/ 15 | static int global_protect(lua_State *L); 16 | static int global_newtry(lua_State *L); 17 | static int protected_(lua_State *L); 18 | static int finalize(lua_State *L); 19 | static int do_nothing(lua_State *L); 20 | 21 | /* except functions */ 22 | static luaL_Reg func[] = { 23 | {"newtry", global_newtry}, 24 | {"protect", global_protect}, 25 | {NULL, NULL} 26 | }; 27 | 28 | /*-------------------------------------------------------------------------*\ 29 | * Try factory 30 | \*-------------------------------------------------------------------------*/ 31 | static void wrap(lua_State *L) { 32 | lua_newtable(L); 33 | lua_pushnumber(L, 1); 34 | lua_pushvalue(L, -3); 35 | lua_settable(L, -3); 36 | lua_insert(L, -2); 37 | lua_pop(L, 1); 38 | } 39 | 40 | static int finalize(lua_State *L) { 41 | if (!lua_toboolean(L, 1)) { 42 | lua_pushvalue(L, lua_upvalueindex(1)); 43 | lua_pcall(L, 0, 0, 0); 44 | lua_settop(L, 2); 45 | wrap(L); 46 | lua_error(L); 47 | return 0; 48 | } else return lua_gettop(L); 49 | } 50 | 51 | static int do_nothing(lua_State *L) { 52 | (void) L; 53 | return 0; 54 | } 55 | 56 | static int global_newtry(lua_State *L) { 57 | lua_settop(L, 1); 58 | if (lua_isnil(L, 1)) lua_pushcfunction(L, do_nothing); 59 | lua_pushcclosure(L, finalize, 1); 60 | return 1; 61 | } 62 | 63 | /*-------------------------------------------------------------------------*\ 64 | * Protect factory 65 | \*-------------------------------------------------------------------------*/ 66 | static int unwrap(lua_State *L) { 67 | if (lua_istable(L, -1)) { 68 | lua_pushnumber(L, 1); 69 | lua_gettable(L, -2); 70 | lua_pushnil(L); 71 | lua_insert(L, -2); 72 | return 1; 73 | } else return 0; 74 | } 75 | 76 | static int protected_(lua_State *L) { 77 | lua_pushvalue(L, lua_upvalueindex(1)); 78 | lua_insert(L, 1); 79 | if (lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0) != 0) { 80 | if (unwrap(L)) return 2; 81 | else lua_error(L); 82 | return 0; 83 | } else return lua_gettop(L); 84 | } 85 | 86 | static int global_protect(lua_State *L) { 87 | lua_pushcclosure(L, protected_, 1); 88 | return 1; 89 | } 90 | 91 | /*-------------------------------------------------------------------------*\ 92 | * Init module 93 | \*-------------------------------------------------------------------------*/ 94 | int except_open(lua_State *L) { 95 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) 96 | luaL_setfuncs(L, func, 0); 97 | #else 98 | luaL_openlib(L, NULL, func, 0); 99 | #endif 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /external/luasocket/socket.h: -------------------------------------------------------------------------------- 1 | #ifndef SOCKET_H 2 | #define SOCKET_H 3 | /*=========================================================================*\ 4 | * Socket compatibilization module 5 | * LuaSocket toolkit 6 | * 7 | * BSD Sockets and WinSock are similar, but there are a few irritating 8 | * differences. Also, not all *nix platforms behave the same. This module 9 | * (and the associated usocket.h and wsocket.h) factor these differences and 10 | * creates a interface compatible with the io.h module. 11 | \*=========================================================================*/ 12 | #include "io.h" 13 | 14 | /*=========================================================================*\ 15 | * Platform specific compatibilization 16 | \*=========================================================================*/ 17 | #ifdef _WIN32 18 | #include "wsocket.h" 19 | #else 20 | #include "usocket.h" 21 | #endif 22 | 23 | /*=========================================================================*\ 24 | * The connect and accept functions accept a timeout and their 25 | * implementations are somewhat complicated. We chose to move 26 | * the timeout control into this module for these functions in 27 | * order to simplify the modules that use them. 28 | \*=========================================================================*/ 29 | #include "timeout.h" 30 | 31 | /* we are lazy... */ 32 | typedef struct sockaddr SA; 33 | 34 | /*=========================================================================*\ 35 | * Functions bellow implement a comfortable platform independent 36 | * interface to sockets 37 | \*=========================================================================*/ 38 | int socket_open(void); 39 | int socket_close(void); 40 | void socket_destroy(p_socket ps); 41 | void socket_shutdown(p_socket ps, int how); 42 | int socket_sendto(p_socket ps, const char *data, size_t count, 43 | size_t *sent, SA *addr, socklen_t addr_len, p_timeout tm); 44 | int socket_recvfrom(p_socket ps, char *data, size_t count, 45 | size_t *got, SA *addr, socklen_t *addr_len, p_timeout tm); 46 | 47 | void socket_setnonblocking(p_socket ps); 48 | void socket_setblocking(p_socket ps); 49 | 50 | int socket_waitfd(p_socket ps, int sw, p_timeout tm); 51 | int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, 52 | p_timeout tm); 53 | 54 | int socket_connect(p_socket ps, SA *addr, socklen_t addr_len, p_timeout tm); 55 | int socket_create(p_socket ps, int domain, int type, int protocol); 56 | int socket_bind(p_socket ps, SA *addr, socklen_t addr_len); 57 | int socket_listen(p_socket ps, int backlog); 58 | int socket_accept(p_socket ps, p_socket pa, SA *addr, 59 | socklen_t *addr_len, p_timeout tm); 60 | 61 | const char *socket_hoststrerror(int err); 62 | const char *socket_gaistrerror(int err); 63 | const char *socket_strerror(int err); 64 | 65 | /* these are perfect to use with the io abstraction module 66 | and the buffered input module */ 67 | int socket_send(p_socket ps, const char *data, size_t count, 68 | size_t *sent, p_timeout tm); 69 | int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm); 70 | int socket_write(p_socket ps, const char *data, size_t count, 71 | size_t *sent, p_timeout tm); 72 | int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm); 73 | const char *socket_ioerror(p_socket ps, int err); 74 | 75 | int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp); 76 | int socket_gethostbyname(const char *addr, struct hostent **hp); 77 | 78 | #endif /* SOCKET_H */ 79 | -------------------------------------------------------------------------------- /external/lpeg/lptypes.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lptypes.h,v 1.8 2013/04/12 16:26:38 roberto Exp $ 3 | ** LPeg - PEG pattern matching for Lua 4 | ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) 5 | ** written by Roberto Ierusalimschy 6 | */ 7 | 8 | #if !defined(lptypes_h) 9 | #define lptypes_h 10 | 11 | 12 | #if !defined(LPEG_DEBUG) 13 | #define NDEBUG 14 | #endif 15 | 16 | #include 17 | #include 18 | 19 | #include "lua.h" 20 | 21 | 22 | #define VERSION "0.12" 23 | 24 | 25 | #define PATTERN_T "lpeg-pattern" 26 | #define MAXSTACKIDX "lpeg-maxstack" 27 | 28 | 29 | /* 30 | ** compatibility with Lua 5.2 31 | */ 32 | #if (LUA_VERSION_NUM == 502) 33 | 34 | #undef lua_equal 35 | #define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) 36 | 37 | #undef lua_getfenv 38 | #define lua_getfenv lua_getuservalue 39 | #undef lua_setfenv 40 | #define lua_setfenv lua_setuservalue 41 | 42 | #undef lua_objlen 43 | #define lua_objlen lua_rawlen 44 | 45 | #undef luaL_register 46 | #define luaL_register(L,n,f) \ 47 | { if ((n) == NULL) luaL_setfuncs(L,f,0); else luaL_newlib(L,f); } 48 | 49 | #endif 50 | 51 | 52 | /* default maximum size for call/backtrack stack */ 53 | #if !defined(MAXBACK) 54 | #define MAXBACK 100 55 | #endif 56 | 57 | 58 | /* maximum number of rules in a grammar */ 59 | #define MAXRULES 200 60 | 61 | 62 | 63 | /* initial size for capture's list */ 64 | #define INITCAPSIZE 32 65 | 66 | 67 | /* index, on Lua stack, for subject */ 68 | #define SUBJIDX 2 69 | 70 | /* number of fixed arguments to 'match' (before capture arguments) */ 71 | #define FIXEDARGS 3 72 | 73 | /* index, on Lua stack, for capture list */ 74 | #define caplistidx(ptop) ((ptop) + 2) 75 | 76 | /* index, on Lua stack, for pattern's ktable */ 77 | #define ktableidx(ptop) ((ptop) + 3) 78 | 79 | /* index, on Lua stack, for backtracking stack */ 80 | #define stackidx(ptop) ((ptop) + 4) 81 | 82 | 83 | 84 | typedef unsigned char byte; 85 | 86 | 87 | #define BITSPERCHAR 8 88 | 89 | #define CHARSETSIZE ((UCHAR_MAX/BITSPERCHAR) + 1) 90 | 91 | 92 | 93 | typedef struct Charset { 94 | byte cs[CHARSETSIZE]; 95 | } Charset; 96 | 97 | 98 | 99 | #define loopset(v,b) { int v; for (v = 0; v < CHARSETSIZE; v++) {b;} } 100 | 101 | /* access to charset */ 102 | #define treebuffer(t) ((byte *)((t) + 1)) 103 | 104 | /* number of slots needed for 'n' bytes */ 105 | #define bytes2slots(n) (((n) - 1) / sizeof(TTree) + 1) 106 | 107 | /* set 'b' bit in charset 'cs' */ 108 | #define setchar(cs,b) ((cs)[(b) >> 3] |= (1 << ((b) & 7))) 109 | 110 | 111 | /* 112 | ** in capture instructions, 'kind' of capture and its offset are 113 | ** packed in field 'aux', 4 bits for each 114 | */ 115 | #define getkind(op) ((op)->i.aux & 0xF) 116 | #define getoff(op) (((op)->i.aux >> 4) & 0xF) 117 | #define joinkindoff(k,o) ((k) | ((o) << 4)) 118 | 119 | #define MAXOFF 0xF 120 | #define MAXAUX 0xFF 121 | 122 | 123 | /* maximum number of bytes to look behind */ 124 | #define MAXBEHIND MAXAUX 125 | 126 | 127 | /* maximum size (in elements) for a pattern */ 128 | #define MAXPATTSIZE (SHRT_MAX - 10) 129 | 130 | 131 | /* size (in elements) for an instruction plus extra l bytes */ 132 | #define instsize(l) (((l) + sizeof(Instruction) - 1)/sizeof(Instruction) + 1) 133 | 134 | 135 | /* size (in elements) for a ISet instruction */ 136 | #define CHARSETINSTSIZE instsize(CHARSETSIZE) 137 | 138 | /* size (in elements) for a IFunc instruction */ 139 | #define funcinstsize(p) ((p)->i.aux + 2) 140 | 141 | 142 | 143 | #define testchar(st,c) (((int)(st)[((c) >> 3)] & (1 << ((c) & 7)))) 144 | 145 | 146 | #endif 147 | 148 | -------------------------------------------------------------------------------- /external/luasocket/script/socket/headers.lua: -------------------------------------------------------------------------------- 1 | ----------------------------------------------------------------------------- 2 | -- Canonic header field capitalization 3 | -- LuaSocket toolkit. 4 | -- Author: Diego Nehab 5 | ----------------------------------------------------------------------------- 6 | local socket = require("socket.socket") 7 | socket.headers = {} 8 | local _M = socket.headers 9 | 10 | _M.canonic = { 11 | ["accept"] = "Accept", 12 | ["accept-charset"] = "Accept-Charset", 13 | ["accept-encoding"] = "Accept-Encoding", 14 | ["accept-language"] = "Accept-Language", 15 | ["accept-ranges"] = "Accept-Ranges", 16 | ["action"] = "Action", 17 | ["alternate-recipient"] = "Alternate-Recipient", 18 | ["age"] = "Age", 19 | ["allow"] = "Allow", 20 | ["arrival-date"] = "Arrival-Date", 21 | ["authorization"] = "Authorization", 22 | ["bcc"] = "Bcc", 23 | ["cache-control"] = "Cache-Control", 24 | ["cc"] = "Cc", 25 | ["comments"] = "Comments", 26 | ["connection"] = "Connection", 27 | ["content-description"] = "Content-Description", 28 | ["content-disposition"] = "Content-Disposition", 29 | ["content-encoding"] = "Content-Encoding", 30 | ["content-id"] = "Content-ID", 31 | ["content-language"] = "Content-Language", 32 | ["content-length"] = "Content-Length", 33 | ["content-location"] = "Content-Location", 34 | ["content-md5"] = "Content-MD5", 35 | ["content-range"] = "Content-Range", 36 | ["content-transfer-encoding"] = "Content-Transfer-Encoding", 37 | ["content-type"] = "Content-Type", 38 | ["cookie"] = "Cookie", 39 | ["date"] = "Date", 40 | ["diagnostic-code"] = "Diagnostic-Code", 41 | ["dsn-gateway"] = "DSN-Gateway", 42 | ["etag"] = "ETag", 43 | ["expect"] = "Expect", 44 | ["expires"] = "Expires", 45 | ["final-log-id"] = "Final-Log-ID", 46 | ["final-recipient"] = "Final-Recipient", 47 | ["from"] = "From", 48 | ["host"] = "Host", 49 | ["if-match"] = "If-Match", 50 | ["if-modified-since"] = "If-Modified-Since", 51 | ["if-none-match"] = "If-None-Match", 52 | ["if-range"] = "If-Range", 53 | ["if-unmodified-since"] = "If-Unmodified-Since", 54 | ["in-reply-to"] = "In-Reply-To", 55 | ["keywords"] = "Keywords", 56 | ["last-attempt-date"] = "Last-Attempt-Date", 57 | ["last-modified"] = "Last-Modified", 58 | ["location"] = "Location", 59 | ["max-forwards"] = "Max-Forwards", 60 | ["message-id"] = "Message-ID", 61 | ["mime-version"] = "MIME-Version", 62 | ["original-envelope-id"] = "Original-Envelope-ID", 63 | ["original-recipient"] = "Original-Recipient", 64 | ["pragma"] = "Pragma", 65 | ["proxy-authenticate"] = "Proxy-Authenticate", 66 | ["proxy-authorization"] = "Proxy-Authorization", 67 | ["range"] = "Range", 68 | ["received"] = "Received", 69 | ["received-from-mta"] = "Received-From-MTA", 70 | ["references"] = "References", 71 | ["referer"] = "Referer", 72 | ["remote-mta"] = "Remote-MTA", 73 | ["reply-to"] = "Reply-To", 74 | ["reporting-mta"] = "Reporting-MTA", 75 | ["resent-bcc"] = "Resent-Bcc", 76 | ["resent-cc"] = "Resent-Cc", 77 | ["resent-date"] = "Resent-Date", 78 | ["resent-from"] = "Resent-From", 79 | ["resent-message-id"] = "Resent-Message-ID", 80 | ["resent-reply-to"] = "Resent-Reply-To", 81 | ["resent-sender"] = "Resent-Sender", 82 | ["resent-to"] = "Resent-To", 83 | ["retry-after"] = "Retry-After", 84 | ["return-path"] = "Return-Path", 85 | ["sender"] = "Sender", 86 | ["server"] = "Server", 87 | ["smtp-remote-recipient"] = "SMTP-Remote-Recipient", 88 | ["status"] = "Status", 89 | ["subject"] = "Subject", 90 | ["te"] = "TE", 91 | ["to"] = "To", 92 | ["trailer"] = "Trailer", 93 | ["transfer-encoding"] = "Transfer-Encoding", 94 | ["upgrade"] = "Upgrade", 95 | ["user-agent"] = "User-Agent", 96 | ["vary"] = "Vary", 97 | ["via"] = "Via", 98 | ["warning"] = "Warning", 99 | ["will-retry-until"] = "Will-Retry-Until", 100 | ["www-authenticate"] = "WWW-Authenticate", 101 | ["x-mailer"] = "X-Mailer", 102 | } 103 | 104 | return _M 105 | -------------------------------------------------------------------------------- /external/luasocket/script/socket/tp.lua: -------------------------------------------------------------------------------- 1 | ----------------------------------------------------------------------------- 2 | -- Unified SMTP/FTP subsystem 3 | -- LuaSocket toolkit. 4 | -- Author: Diego Nehab 5 | ----------------------------------------------------------------------------- 6 | 7 | ----------------------------------------------------------------------------- 8 | -- Declare module and import dependencies 9 | ----------------------------------------------------------------------------- 10 | local base = _G 11 | local string = require("string") 12 | local socket = require("socket.socket") 13 | local ltn12 = require("socket.ltn12") 14 | 15 | socket.tp = {} 16 | local _M = socket.tp 17 | 18 | ----------------------------------------------------------------------------- 19 | -- Program constants 20 | ----------------------------------------------------------------------------- 21 | _M.TIMEOUT = 60 22 | 23 | ----------------------------------------------------------------------------- 24 | -- Implementation 25 | ----------------------------------------------------------------------------- 26 | -- gets server reply (works for SMTP and FTP) 27 | local function get_reply(c) 28 | local code, current, sep 29 | local line, err = c:receive() 30 | local reply = line 31 | if err then return nil, err end 32 | code, sep = socket.skip(2, string.find(line, "^(%d%d%d)(.?)")) 33 | if not code then return nil, "invalid server reply" end 34 | if sep == "-" then -- reply is multiline 35 | repeat 36 | line, err = c:receive() 37 | if err then return nil, err end 38 | current, sep = socket.skip(2, string.find(line, "^(%d%d%d)(.?)")) 39 | reply = reply .. "\n" .. line 40 | -- reply ends with same code 41 | until code == current and sep == " " 42 | end 43 | return code, reply 44 | end 45 | 46 | -- metatable for sock object 47 | local metat = { __index = {} } 48 | 49 | function metat.__index:check(ok) 50 | local code, reply = get_reply(self.c) 51 | if not code then return nil, reply end 52 | if base.type(ok) ~= "function" then 53 | if base.type(ok) == "table" then 54 | for i, v in base.ipairs(ok) do 55 | if string.find(code, v) then 56 | return base.tonumber(code), reply 57 | end 58 | end 59 | return nil, reply 60 | else 61 | if string.find(code, ok) then return base.tonumber(code), reply 62 | else return nil, reply end 63 | end 64 | else return ok(base.tonumber(code), reply) end 65 | end 66 | 67 | function metat.__index:command(cmd, arg) 68 | cmd = string.upper(cmd) 69 | if arg then 70 | return self.c:send(cmd .. " " .. arg.. "\r\n") 71 | else 72 | return self.c:send(cmd .. "\r\n") 73 | end 74 | end 75 | 76 | function metat.__index:sink(snk, pat) 77 | local chunk, err = c:receive(pat) 78 | return snk(chunk, err) 79 | end 80 | 81 | function metat.__index:send(data) 82 | return self.c:send(data) 83 | end 84 | 85 | function metat.__index:receive(pat) 86 | return self.c:receive(pat) 87 | end 88 | 89 | function metat.__index:getfd() 90 | return self.c:getfd() 91 | end 92 | 93 | function metat.__index:dirty() 94 | return self.c:dirty() 95 | end 96 | 97 | function metat.__index:getcontrol() 98 | return self.c 99 | end 100 | 101 | function metat.__index:source(source, step) 102 | local sink = socket.sink("keep-open", self.c) 103 | local ret, err = ltn12.pump.all(source, sink, step or ltn12.pump.step) 104 | return ret, err 105 | end 106 | 107 | -- closes the underlying c 108 | function metat.__index:close() 109 | self.c:close() 110 | return 1 111 | end 112 | 113 | -- connect with server and return c object 114 | function _M.connect(host, port, timeout, create) 115 | local c, e = (create or socket.tcp)() 116 | if not c then return nil, e end 117 | c:settimeout(timeout or _M.TIMEOUT) 118 | local r, e = c:connect(host, port) 119 | if not r then 120 | c:close() 121 | return nil, e 122 | end 123 | return base.setmetatable({c = c}, metat) 124 | end 125 | 126 | return _M 127 | -------------------------------------------------------------------------------- /external/luasocket/luasocket.c: -------------------------------------------------------------------------------- 1 | /*=========================================================================*\ 2 | * LuaSocket toolkit 3 | * Networking support for the Lua language 4 | * Diego Nehab 5 | * 26/11/1999 6 | * 7 | * This library is part of an effort to progressively increase the network 8 | * connectivity of the Lua language. The Lua interface to networking 9 | * functions follows the Sockets API closely, trying to simplify all tasks 10 | * involved in setting up both client and server connections. The provided 11 | * IO routines, however, follow the Lua style, being very similar to the 12 | * standard Lua read and write functions. 13 | \*=========================================================================*/ 14 | 15 | /*=========================================================================*\ 16 | * Standard include files 17 | \*=========================================================================*/ 18 | #include "lua.h" 19 | #include "lauxlib.h" 20 | 21 | 22 | /*=========================================================================*\ 23 | * LuaSocket includes 24 | \*=========================================================================*/ 25 | #include "luasocket.h" 26 | #include "auxiliar.h" 27 | #include "except.h" 28 | #include "timeout.h" 29 | #include "buffer.h" 30 | #include "inet.h" 31 | #include "tcp.h" 32 | #include "udp.h" 33 | #include "select.h" 34 | 35 | /*-------------------------------------------------------------------------*\ 36 | * Internal function prototypes 37 | \*-------------------------------------------------------------------------*/ 38 | static int global_skip(lua_State *L); 39 | static int global_unload(lua_State *L); 40 | static int base_open(lua_State *L); 41 | 42 | /*-------------------------------------------------------------------------*\ 43 | * Modules and functions 44 | \*-------------------------------------------------------------------------*/ 45 | static const luaL_Reg mod[] = { 46 | {"auxiliar", auxiliar_open}, 47 | {"except", except_open}, 48 | {"timeout", timeout_open}, 49 | {"buffer", buffer_open}, 50 | {"inet", inet_open}, 51 | {"tcp", tcp_open}, 52 | {"udp", udp_open}, 53 | {"select", select_open}, 54 | {NULL, NULL} 55 | }; 56 | 57 | static luaL_Reg func[] = { 58 | {"skip", global_skip}, 59 | {"__unload", global_unload}, 60 | {NULL, NULL} 61 | }; 62 | 63 | /*-------------------------------------------------------------------------*\ 64 | * Skip a few arguments 65 | \*-------------------------------------------------------------------------*/ 66 | static int global_skip(lua_State *L) { 67 | int amount = luaL_checkint(L, 1); 68 | int ret = lua_gettop(L) - amount - 1; 69 | return ret >= 0 ? ret : 0; 70 | } 71 | 72 | /*-------------------------------------------------------------------------*\ 73 | * Unloads the library 74 | \*-------------------------------------------------------------------------*/ 75 | static int global_unload(lua_State *L) { 76 | (void) L; 77 | socket_close(); 78 | return 0; 79 | } 80 | 81 | #if LUA_VERSION_NUM > 501 82 | int luaL_typerror (lua_State *L, int narg, const char *tname) { 83 | const char *msg = lua_pushfstring(L, "%s expected, got %s", 84 | tname, luaL_typename(L, narg)); 85 | return luaL_argerror(L, narg, msg); 86 | } 87 | #endif 88 | 89 | /*-------------------------------------------------------------------------*\ 90 | * Setup basic stuff. 91 | \*-------------------------------------------------------------------------*/ 92 | static int base_open(lua_State *L) { 93 | if (socket_open()) { 94 | /* export functions (and leave namespace table on top of stack) */ 95 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) 96 | lua_newtable(L); 97 | luaL_setfuncs(L, func, 0); 98 | #else 99 | luaL_openlib(L, "socket", func, 0); 100 | #endif 101 | #ifdef LUASOCKET_DEBUG 102 | lua_pushstring(L, "_DEBUG"); 103 | lua_pushboolean(L, 1); 104 | lua_rawset(L, -3); 105 | #endif 106 | /* make version string available to scripts */ 107 | lua_pushstring(L, "_VERSION"); 108 | lua_pushstring(L, LUASOCKET_VERSION); 109 | lua_rawset(L, -3); 110 | return 1; 111 | } else { 112 | lua_pushstring(L, "unable to initialize library"); 113 | lua_error(L); 114 | return 0; 115 | } 116 | } 117 | 118 | /*-------------------------------------------------------------------------*\ 119 | * Initializes all library modules. 120 | \*-------------------------------------------------------------------------*/ 121 | LUASOCKET_API int luaopen_socket_core(lua_State *L) { 122 | int i; 123 | base_open(L); 124 | for (i = 0; mod[i].name; i++) mod[i].func(L); 125 | return 1; 126 | } 127 | -------------------------------------------------------------------------------- /external/luasocket/script/socket.lua: -------------------------------------------------------------------------------- 1 | ----------------------------------------------------------------------------- 2 | -- LuaSocket helper module 3 | -- Author: Diego Nehab 4 | ----------------------------------------------------------------------------- 5 | 6 | ----------------------------------------------------------------------------- 7 | -- Declare module and import dependencies 8 | ----------------------------------------------------------------------------- 9 | local base = _G 10 | local string = require("string") 11 | local math = require("math") 12 | local socket = require("socket.core") 13 | 14 | local _M = socket 15 | 16 | ----------------------------------------------------------------------------- 17 | -- Exported auxiliar functions 18 | ----------------------------------------------------------------------------- 19 | function _M.connect4(address, port, laddress, lport) 20 | return socket.connect(address, port, laddress, lport, "inet") 21 | end 22 | 23 | function _M.connect6(address, port, laddress, lport) 24 | return socket.connect(address, port, laddress, lport, "inet6") 25 | end 26 | 27 | function _M.bind(host, port, backlog) 28 | if host == "*" then host = "0.0.0.0" end 29 | local addrinfo, err = socket.dns.getaddrinfo(host); 30 | if not addrinfo then return nil, err end 31 | local sock, res 32 | err = "no info on address" 33 | for i, alt in base.ipairs(addrinfo) do 34 | if alt.family == "inet" then 35 | sock, err = socket.tcp() 36 | else 37 | sock, err = socket.tcp6() 38 | end 39 | if not sock then return nil, err end 40 | sock:setoption("reuseaddr", true) 41 | res, err = sock:bind(alt.addr, port) 42 | if not res then 43 | sock:close() 44 | else 45 | res, err = sock:listen(backlog) 46 | if not res then 47 | sock:close() 48 | else 49 | return sock 50 | end 51 | end 52 | end 53 | return nil, err 54 | end 55 | 56 | _M.try = _M.newtry() 57 | 58 | function _M.choose(table) 59 | return function(name, opt1, opt2) 60 | if base.type(name) ~= "string" then 61 | name, opt1, opt2 = "default", name, opt1 62 | end 63 | local f = table[name or "nil"] 64 | if not f then base.error("unknown key (".. base.tostring(name) ..")", 3) 65 | else return f(opt1, opt2) end 66 | end 67 | end 68 | 69 | ----------------------------------------------------------------------------- 70 | -- Socket sources and sinks, conforming to LTN12 71 | ----------------------------------------------------------------------------- 72 | -- create namespaces inside LuaSocket namespace 73 | local sourcet, sinkt = {}, {} 74 | _M.sourcet = sourcet 75 | _M.sinkt = sinkt 76 | 77 | _M.BLOCKSIZE = 2048 78 | 79 | sinkt["close-when-done"] = function(sock) 80 | return base.setmetatable({ 81 | getfd = function() return sock:getfd() end, 82 | dirty = function() return sock:dirty() end 83 | }, { 84 | __call = function(self, chunk, err) 85 | if not chunk then 86 | sock:close() 87 | return 1 88 | else return sock:send(chunk) end 89 | end 90 | }) 91 | end 92 | 93 | sinkt["keep-open"] = function(sock) 94 | return base.setmetatable({ 95 | getfd = function() return sock:getfd() end, 96 | dirty = function() return sock:dirty() end 97 | }, { 98 | __call = function(self, chunk, err) 99 | if chunk then return sock:send(chunk) 100 | else return 1 end 101 | end 102 | }) 103 | end 104 | 105 | sinkt["default"] = sinkt["keep-open"] 106 | 107 | _M.sink = _M.choose(sinkt) 108 | 109 | sourcet["by-length"] = function(sock, length) 110 | return base.setmetatable({ 111 | getfd = function() return sock:getfd() end, 112 | dirty = function() return sock:dirty() end 113 | }, { 114 | __call = function() 115 | if length <= 0 then return nil end 116 | local size = math.min(socket.BLOCKSIZE, length) 117 | local chunk, err = sock:receive(size) 118 | if err then return nil, err end 119 | length = length - string.len(chunk) 120 | return chunk 121 | end 122 | }) 123 | end 124 | 125 | sourcet["until-closed"] = function(sock) 126 | local done 127 | return base.setmetatable({ 128 | getfd = function() return sock:getfd() end, 129 | dirty = function() return sock:dirty() end 130 | }, { 131 | __call = function() 132 | if done then return nil end 133 | local chunk, err, partial = sock:receive(socket.BLOCKSIZE) 134 | if not err then return chunk 135 | elseif err == "closed" then 136 | sock:close() 137 | done = 1 138 | return partial 139 | else return nil, err end 140 | end 141 | }) 142 | end 143 | 144 | 145 | sourcet["default"] = sourcet["until-closed"] 146 | 147 | _M.source = _M.choose(sourcet) 148 | 149 | return _M 150 | -------------------------------------------------------------------------------- /external/bitop/bit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** Lua BitOp -- a bit operations library for Lua 5.1/5.2. 3 | ** http://bitop.luajit.org/ 4 | ** 5 | ** Copyright (C) 2008-2012 Mike Pall. All rights reserved. 6 | ** 7 | ** Permission is hereby granted, free of charge, to any person obtaining 8 | ** a copy of this software and associated documentation files (the 9 | ** "Software"), to deal in the Software without restriction, including 10 | ** without limitation the rights to use, copy, modify, merge, publish, 11 | ** distribute, sublicense, and/or sell copies of the Software, and to 12 | ** permit persons to whom the Software is furnished to do so, subject to 13 | ** the following conditions: 14 | ** 15 | ** The above copyright notice and this permission notice shall be 16 | ** included in all copies or substantial portions of the Software. 17 | ** 18 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | ** 26 | ** [ MIT license: http://www.opensource.org/licenses/mit-license.php ] 27 | */ 28 | 29 | #define LUA_BITOP_VERSION "1.0.2" 30 | 31 | #define LUA_LIB 32 | #include "lua.h" 33 | #include "lauxlib.h" 34 | 35 | #ifdef _MSC_VER 36 | /* MSVC is stuck in the last century and doesn't have C99's stdint.h. */ 37 | typedef __int32 int32_t; 38 | typedef unsigned __int32 uint32_t; 39 | typedef unsigned __int64 uint64_t; 40 | #else 41 | #include 42 | #endif 43 | 44 | typedef int32_t SBits; 45 | typedef uint32_t UBits; 46 | 47 | typedef union { 48 | lua_Number n; 49 | #ifdef LUA_NUMBER_DOUBLE 50 | uint64_t b; 51 | #else 52 | UBits b; 53 | #endif 54 | } BitNum; 55 | 56 | /* Convert argument to bit type. */ 57 | static UBits barg(lua_State *L, int idx) 58 | { 59 | BitNum bn; 60 | UBits b; 61 | #if LUA_VERSION_NUM < 502 62 | bn.n = lua_tonumber(L, idx); 63 | #else 64 | bn.n = luaL_checknumber(L, idx); 65 | #endif 66 | #if defined(LUA_NUMBER_DOUBLE) 67 | bn.n += 6755399441055744.0; /* 2^52+2^51 */ 68 | #ifdef SWAPPED_DOUBLE 69 | b = (UBits)(bn.b >> 32); 70 | #else 71 | b = (UBits)bn.b; 72 | #endif 73 | #elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \ 74 | defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \ 75 | defined(LUA_NUMBER_LLONG) 76 | if (sizeof(UBits) == sizeof(lua_Number)) 77 | b = bn.b; 78 | else 79 | b = (UBits)(SBits)bn.n; 80 | #elif defined(LUA_NUMBER_FLOAT) 81 | #error "A 'float' lua_Number type is incompatible with this library" 82 | #else 83 | #error "Unknown number type, check LUA_NUMBER_* in luaconf.h" 84 | #endif 85 | #if LUA_VERSION_NUM < 502 86 | if (b == 0 && !lua_isnumber(L, idx)) { 87 | luaL_typerror(L, idx, "number"); 88 | } 89 | #endif 90 | return b; 91 | } 92 | 93 | /* Return bit type. */ 94 | #define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1; 95 | 96 | static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) } 97 | static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) } 98 | 99 | #define BIT_OP(func, opr) \ 100 | static int func(lua_State *L) { int i; UBits b = barg(L, 1); \ 101 | for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) } 102 | BIT_OP(bit_band, &=) 103 | BIT_OP(bit_bor, |=) 104 | BIT_OP(bit_bxor, ^=) 105 | 106 | #define bshl(b, n) (b << n) 107 | #define bshr(b, n) (b >> n) 108 | #define bsar(b, n) ((SBits)b >> n) 109 | #define brol(b, n) ((b << n) | (b >> (32-n))) 110 | #define bror(b, n) ((b << (32-n)) | (b >> n)) 111 | #define BIT_SH(func, fn) \ 112 | static int func(lua_State *L) { \ 113 | UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) } 114 | BIT_SH(bit_lshift, bshl) 115 | BIT_SH(bit_rshift, bshr) 116 | BIT_SH(bit_arshift, bsar) 117 | BIT_SH(bit_rol, brol) 118 | BIT_SH(bit_ror, bror) 119 | 120 | static int bit_bswap(lua_State *L) 121 | { 122 | UBits b = barg(L, 1); 123 | b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24); 124 | BRET(b) 125 | } 126 | 127 | static int bit_tohex(lua_State *L) 128 | { 129 | UBits b = barg(L, 1); 130 | SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2); 131 | const char *hexdigits = "0123456789abcdef"; 132 | char buf[8]; 133 | int i; 134 | if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; } 135 | if (n > 8) n = 8; 136 | for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; } 137 | lua_pushlstring(L, buf, (size_t)n); 138 | return 1; 139 | } 140 | 141 | static const struct luaL_Reg bit_funcs[] = { 142 | { "tobit", bit_tobit }, 143 | { "bnot", bit_bnot }, 144 | { "band", bit_band }, 145 | { "bor", bit_bor }, 146 | { "bxor", bit_bxor }, 147 | { "lshift", bit_lshift }, 148 | { "rshift", bit_rshift }, 149 | { "arshift", bit_arshift }, 150 | { "rol", bit_rol }, 151 | { "ror", bit_ror }, 152 | { "bswap", bit_bswap }, 153 | { "tohex", bit_tohex }, 154 | { NULL, NULL } 155 | }; 156 | 157 | /* Signed right-shifts are implementation-defined per C89/C99. 158 | ** But the de facto standard are arithmetic right-shifts on two's 159 | ** complement CPUs. This behaviour is required here, so test for it. 160 | */ 161 | #define BAD_SAR (bsar(-8, 2) != (SBits)-2) 162 | 163 | LUALIB_API int luaopen_bit(lua_State *L) 164 | { 165 | UBits b; 166 | lua_pushnumber(L, (lua_Number)1437217655L); 167 | b = barg(L, -1); 168 | if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */ 169 | const char *msg = "compiled with incompatible luaconf.h"; 170 | #ifdef LUA_NUMBER_DOUBLE 171 | #ifdef _WIN32 172 | if (b == (UBits)1610612736L) 173 | msg = "use D3DCREATE_FPU_PRESERVE with DirectX"; 174 | #endif 175 | if (b == (UBits)1127743488L) 176 | msg = "not compiled with SWAPPED_DOUBLE"; 177 | #endif 178 | if (BAD_SAR) 179 | msg = "arithmetic right-shift broken"; 180 | luaL_error(L, "bit library self-test failed (%s)", msg); 181 | } 182 | #if LUA_VERSION_NUM < 502 183 | luaL_register(L, "bit", bit_funcs); 184 | #else 185 | luaL_newlib(L, bit_funcs); 186 | #endif 187 | return 1; 188 | } 189 | 190 | -------------------------------------------------------------------------------- /external/lpeg/lpprint.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lpprint.c,v 1.7 2013/04/12 16:29:49 roberto Exp $ 3 | ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | #include "lptypes.h" 12 | #include "lpprint.h" 13 | #include "lpcode.h" 14 | 15 | 16 | #if defined(LPEG_DEBUG) 17 | 18 | /* 19 | ** {====================================================== 20 | ** Printing patterns (for debugging) 21 | ** ======================================================= 22 | */ 23 | 24 | 25 | void printcharset (const byte *st) { 26 | int i; 27 | printf("["); 28 | for (i = 0; i <= UCHAR_MAX; i++) { 29 | int first = i; 30 | while (testchar(st, i) && i <= UCHAR_MAX) i++; 31 | if (i - 1 == first) /* unary range? */ 32 | printf("(%02x)", first); 33 | else if (i - 1 > first) /* non-empty range? */ 34 | printf("(%02x-%02x)", first, i - 1); 35 | } 36 | printf("]"); 37 | } 38 | 39 | 40 | static void printcapkind (int kind) { 41 | const char *const modes[] = { 42 | "close", "position", "constant", "backref", 43 | "argument", "simple", "table", "function", 44 | "query", "string", "num", "substitution", "fold", 45 | "runtime", "group"}; 46 | printf("%s", modes[kind]); 47 | } 48 | 49 | 50 | static void printjmp (const Instruction *op, const Instruction *p) { 51 | printf("-> %d", (int)(p + (p + 1)->offset - op)); 52 | } 53 | 54 | 55 | static void printinst (const Instruction *op, const Instruction *p) { 56 | const char *const names[] = { 57 | "any", "char", "set", 58 | "testany", "testchar", "testset", 59 | "span", "behind", 60 | "ret", "end", 61 | "choice", "jmp", "call", "open_call", 62 | "commit", "partial_commit", "back_commit", "failtwice", "fail", "giveup", 63 | "fullcapture", "opencapture", "closecapture", "closeruntime" 64 | }; 65 | printf("%02ld: %s ", (long)(p - op), names[p->i.code]); 66 | switch ((Opcode)p->i.code) { 67 | case IChar: { 68 | printf("'%c'", p->i.aux); 69 | break; 70 | } 71 | case ITestChar: { 72 | printf("'%c'", p->i.aux); printjmp(op, p); 73 | break; 74 | } 75 | case IFullCapture: { 76 | printcapkind(getkind(p)); 77 | printf(" (size = %d) (idx = %d)", getoff(p), p->i.key); 78 | break; 79 | } 80 | case IOpenCapture: { 81 | printcapkind(getkind(p)); 82 | printf(" (idx = %d)", p->i.key); 83 | break; 84 | } 85 | case ISet: { 86 | printcharset((p+1)->buff); 87 | break; 88 | } 89 | case ITestSet: { 90 | printcharset((p+2)->buff); printjmp(op, p); 91 | break; 92 | } 93 | case ISpan: { 94 | printcharset((p+1)->buff); 95 | break; 96 | } 97 | case IOpenCall: { 98 | printf("-> %d", (p + 1)->offset); 99 | break; 100 | } 101 | case IBehind: { 102 | printf("%d", p->i.aux); 103 | break; 104 | } 105 | case IJmp: case ICall: case ICommit: case IChoice: 106 | case IPartialCommit: case IBackCommit: case ITestAny: { 107 | printjmp(op, p); 108 | break; 109 | } 110 | default: break; 111 | } 112 | printf("\n"); 113 | } 114 | 115 | 116 | void printpatt (Instruction *p, int n) { 117 | Instruction *op = p; 118 | while (p < op + n) { 119 | printinst(op, p); 120 | p += sizei(p); 121 | } 122 | } 123 | 124 | 125 | #if defined(LPEG_DEBUG) 126 | static void printcap (Capture *cap) { 127 | printcapkind(cap->kind); 128 | printf(" (idx: %d - size: %d) -> %p\n", cap->idx, cap->siz, cap->s); 129 | } 130 | 131 | 132 | void printcaplist (Capture *cap, Capture *limit) { 133 | printf(">======\n"); 134 | for (; cap->s && (limit == NULL || cap < limit); cap++) 135 | printcap(cap); 136 | printf("=======\n"); 137 | } 138 | #endif 139 | 140 | /* }====================================================== */ 141 | 142 | 143 | /* 144 | ** {====================================================== 145 | ** Printing trees (for debugging) 146 | ** ======================================================= 147 | */ 148 | 149 | static const char *tagnames[] = { 150 | "char", "set", "any", 151 | "true", "false", 152 | "rep", 153 | "seq", "choice", 154 | "not", "and", 155 | "call", "opencall", "rule", "grammar", 156 | "behind", 157 | "capture", "run-time" 158 | }; 159 | 160 | 161 | void printtree (TTree *tree, int ident) { 162 | int i; 163 | for (i = 0; i < ident; i++) printf(" "); 164 | printf("%s", tagnames[tree->tag]); 165 | switch (tree->tag) { 166 | case TChar: { 167 | int c = tree->u.n; 168 | if (isprint(c)) 169 | printf(" '%c'\n", c); 170 | else 171 | printf(" (%02X)\n", c); 172 | break; 173 | } 174 | case TSet: { 175 | printcharset(treebuffer(tree)); 176 | printf("\n"); 177 | break; 178 | } 179 | case TOpenCall: case TCall: { 180 | printf(" key: %d\n", tree->key); 181 | break; 182 | } 183 | case TBehind: { 184 | printf(" %d\n", tree->u.n); 185 | printtree(sib1(tree), ident + 2); 186 | break; 187 | } 188 | case TCapture: { 189 | printf(" cap: %d key: %d n: %d\n", tree->cap, tree->key, tree->u.n); 190 | printtree(sib1(tree), ident + 2); 191 | break; 192 | } 193 | case TRule: { 194 | printf(" n: %d key: %d\n", tree->cap, tree->key); 195 | printtree(sib1(tree), ident + 2); 196 | break; /* do not print next rule as a sibling */ 197 | } 198 | case TGrammar: { 199 | TTree *rule = sib1(tree); 200 | printf(" %d\n", tree->u.n); /* number of rules */ 201 | for (i = 0; i < tree->u.n; i++) { 202 | printtree(rule, ident + 2); 203 | rule = sib2(rule); 204 | } 205 | assert(rule->tag == TTrue); /* sentinel */ 206 | break; 207 | } 208 | default: { 209 | int sibs = numsiblings[tree->tag]; 210 | printf("\n"); 211 | if (sibs >= 1) { 212 | printtree(sib1(tree), ident + 2); 213 | if (sibs >= 2) 214 | printtree(sib2(tree), ident + 2); 215 | } 216 | break; 217 | } 218 | } 219 | } 220 | 221 | 222 | void printktable (lua_State *L, int idx) { 223 | int n, i; 224 | lua_getfenv(L, idx); 225 | if (lua_isnil(L, -1)) /* no ktable? */ 226 | return; 227 | n = lua_objlen(L, -1); 228 | printf("["); 229 | for (i = 1; i <= n; i++) { 230 | printf("%d = ", i); 231 | lua_rawgeti(L, -1, i); 232 | if (lua_isstring(L, -1)) 233 | printf("%s ", lua_tostring(L, -1)); 234 | else 235 | printf("%s ", lua_typename(L, lua_type(L, -1))); 236 | lua_pop(L, 1); 237 | } 238 | printf("]\n"); 239 | /* leave ktable at the stack */ 240 | } 241 | 242 | /* }====================================================== */ 243 | 244 | #endif 245 | -------------------------------------------------------------------------------- /external/luasocket/auxiliar.c: -------------------------------------------------------------------------------- 1 | /*=========================================================================*\ 2 | * Auxiliar routines for class hierarchy manipulation 3 | * LuaSocket toolkit 4 | \*=========================================================================*/ 5 | #include 6 | #include 7 | 8 | #include "auxiliar.h" 9 | 10 | /*=========================================================================*\ 11 | * Exported functions 12 | \*=========================================================================*/ 13 | /*-------------------------------------------------------------------------*\ 14 | * Initializes the module 15 | \*-------------------------------------------------------------------------*/ 16 | int auxiliar_open(lua_State *L) { 17 | (void) L; 18 | return 0; 19 | } 20 | 21 | /*-------------------------------------------------------------------------*\ 22 | * Creates a new class with given methods 23 | * Methods whose names start with __ are passed directly to the metatable. 24 | \*-------------------------------------------------------------------------*/ 25 | void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func) { 26 | luaL_newmetatable(L, classname); /* mt */ 27 | /* create __index table to place methods */ 28 | lua_pushstring(L, "__index"); /* mt,"__index" */ 29 | lua_newtable(L); /* mt,"__index",it */ 30 | /* put class name into class metatable */ 31 | lua_pushstring(L, "class"); /* mt,"__index",it,"class" */ 32 | lua_pushstring(L, classname); /* mt,"__index",it,"class",classname */ 33 | lua_rawset(L, -3); /* mt,"__index",it */ 34 | /* pass all methods that start with _ to the metatable, and all others 35 | * to the index table */ 36 | for (; func->name; func++) { /* mt,"__index",it */ 37 | lua_pushstring(L, func->name); 38 | lua_pushcfunction(L, func->func); 39 | lua_rawset(L, func->name[0] == '_' ? -5: -3); 40 | } 41 | lua_rawset(L, -3); /* mt */ 42 | lua_pop(L, 1); 43 | } 44 | 45 | /*-------------------------------------------------------------------------*\ 46 | * Prints the value of a class in a nice way 47 | \*-------------------------------------------------------------------------*/ 48 | int auxiliar_tostring(lua_State *L) { 49 | char buf[32]; 50 | if (!lua_getmetatable(L, 1)) goto error; 51 | lua_pushstring(L, "__index"); 52 | lua_gettable(L, -2); 53 | if (!lua_istable(L, -1)) goto error; 54 | lua_pushstring(L, "class"); 55 | lua_gettable(L, -2); 56 | if (!lua_isstring(L, -1)) goto error; 57 | sprintf(buf, "%p", lua_touserdata(L, 1)); 58 | lua_pushfstring(L, "%s: %s", lua_tostring(L, -1), buf); 59 | return 1; 60 | error: 61 | lua_pushstring(L, "invalid object passed to 'auxiliar.c:__tostring'"); 62 | lua_error(L); 63 | return 1; 64 | } 65 | 66 | /*-------------------------------------------------------------------------*\ 67 | * Insert class into group 68 | \*-------------------------------------------------------------------------*/ 69 | void auxiliar_add2group(lua_State *L, const char *classname, const char *groupname) { 70 | luaL_getmetatable(L, classname); 71 | lua_pushstring(L, groupname); 72 | lua_pushboolean(L, 1); 73 | lua_rawset(L, -3); 74 | lua_pop(L, 1); 75 | } 76 | 77 | /*-------------------------------------------------------------------------*\ 78 | * Make sure argument is a boolean 79 | \*-------------------------------------------------------------------------*/ 80 | int auxiliar_checkboolean(lua_State *L, int objidx) { 81 | if (!lua_isboolean(L, objidx)) 82 | auxiliar_typeerror(L, objidx, lua_typename(L, LUA_TBOOLEAN)); 83 | return lua_toboolean(L, objidx); 84 | } 85 | 86 | /*-------------------------------------------------------------------------*\ 87 | * Return userdata pointer if object belongs to a given class, abort with 88 | * error otherwise 89 | \*-------------------------------------------------------------------------*/ 90 | void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) { 91 | void *data = auxiliar_getclassudata(L, classname, objidx); 92 | if (!data) { 93 | char msg[45]; 94 | sprintf(msg, "%.35s expected", classname); 95 | luaL_argerror(L, objidx, msg); 96 | } 97 | return data; 98 | } 99 | 100 | /*-------------------------------------------------------------------------*\ 101 | * Return userdata pointer if object belongs to a given group, abort with 102 | * error otherwise 103 | \*-------------------------------------------------------------------------*/ 104 | void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) { 105 | void *data = auxiliar_getgroupudata(L, groupname, objidx); 106 | if (!data) { 107 | char msg[45]; 108 | sprintf(msg, "%.35s expected", groupname); 109 | luaL_argerror(L, objidx, msg); 110 | } 111 | return data; 112 | } 113 | 114 | /*-------------------------------------------------------------------------*\ 115 | * Set object class 116 | \*-------------------------------------------------------------------------*/ 117 | void auxiliar_setclass(lua_State *L, const char *classname, int objidx) { 118 | luaL_getmetatable(L, classname); 119 | if (objidx < 0) objidx--; 120 | lua_setmetatable(L, objidx); 121 | } 122 | 123 | /*-------------------------------------------------------------------------*\ 124 | * Get a userdata pointer if object belongs to a given group. Return NULL 125 | * otherwise 126 | \*-------------------------------------------------------------------------*/ 127 | void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) { 128 | if (!lua_getmetatable(L, objidx)) 129 | return NULL; 130 | lua_pushstring(L, groupname); 131 | lua_rawget(L, -2); 132 | if (lua_isnil(L, -1)) { 133 | lua_pop(L, 2); 134 | return NULL; 135 | } else { 136 | lua_pop(L, 2); 137 | return lua_touserdata(L, objidx); 138 | } 139 | } 140 | 141 | /*-------------------------------------------------------------------------*\ 142 | * Get a userdata pointer if object belongs to a given class. Return NULL 143 | * otherwise 144 | \*-------------------------------------------------------------------------*/ 145 | void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) { 146 | return luaL_checkudata(L, objidx, classname); 147 | } 148 | 149 | /*-------------------------------------------------------------------------*\ 150 | * Throws error when argument does not have correct type. 151 | * Used to be part of lauxlib in Lua 5.1, was dropped from 5.2. 152 | \*-------------------------------------------------------------------------*/ 153 | int auxiliar_typeerror (lua_State *L, int narg, const char *tname) { 154 | const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, 155 | luaL_typename(L, narg)); 156 | return luaL_argerror(L, narg, msg); 157 | } 158 | 159 | -------------------------------------------------------------------------------- /external/luasocket/serial.c: -------------------------------------------------------------------------------- 1 | /*=========================================================================*\ 2 | * Serial stream 3 | * LuaSocket toolkit 4 | \*=========================================================================*/ 5 | #include 6 | 7 | #include "lua.h" 8 | #include "lauxlib.h" 9 | 10 | #include "auxiliar.h" 11 | #include "socket.h" 12 | #include "options.h" 13 | #include "unix.h" 14 | #include "luasocket.h" 15 | #include 16 | 17 | /* 18 | Reuses userdata definition from unix.h, since it is useful for all 19 | stream-like objects. 20 | 21 | If we stored the serial path for use in error messages or userdata 22 | printing, we might need our own userdata definition. 23 | 24 | Group usage is semi-inherited from unix.c, but unnecessary since we 25 | have only one object type. 26 | */ 27 | 28 | /*=========================================================================*\ 29 | * Internal function prototypes 30 | \*=========================================================================*/ 31 | static int global_create(lua_State *L); 32 | static int meth_send(lua_State *L); 33 | static int meth_receive(lua_State *L); 34 | static int meth_close(lua_State *L); 35 | static int meth_settimeout(lua_State *L); 36 | static int meth_getfd(lua_State *L); 37 | static int meth_setfd(lua_State *L); 38 | static int meth_dirty(lua_State *L); 39 | static int meth_getstats(lua_State *L); 40 | static int meth_setstats(lua_State *L); 41 | 42 | /* serial object methods */ 43 | static luaL_Reg serial_methods[] = { 44 | {"__gc", meth_close}, 45 | {"__tostring", auxiliar_tostring}, 46 | {"close", meth_close}, 47 | {"dirty", meth_dirty}, 48 | {"getfd", meth_getfd}, 49 | {"getstats", meth_getstats}, 50 | {"setstats", meth_setstats}, 51 | {"receive", meth_receive}, 52 | {"send", meth_send}, 53 | {"setfd", meth_setfd}, 54 | {"settimeout", meth_settimeout}, 55 | {NULL, NULL} 56 | }; 57 | 58 | /* our socket creation function */ 59 | /* this is an ad-hoc module that returns a single function 60 | * as such, do not include other functions in this array. */ 61 | static luaL_Reg func[] = { 62 | {"serial", global_create}, 63 | {NULL, NULL} 64 | }; 65 | 66 | 67 | /*-------------------------------------------------------------------------*\ 68 | * Initializes module 69 | \*-------------------------------------------------------------------------*/ 70 | LUASOCKET_API int luaopen_socket_serial(lua_State *L) { 71 | /* create classes */ 72 | auxiliar_newclass(L, "serial{client}", serial_methods); 73 | /* create class groups */ 74 | auxiliar_add2group(L, "serial{client}", "serial{any}"); 75 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) 76 | lua_pushcfunction(L, global_create); 77 | (void) func; 78 | #else 79 | /* set function into socket namespace */ 80 | luaL_openlib(L, "socket", func, 0); 81 | lua_pushcfunction(L, global_create); 82 | #endif 83 | return 1; 84 | } 85 | 86 | /*=========================================================================*\ 87 | * Lua methods 88 | \*=========================================================================*/ 89 | /*-------------------------------------------------------------------------*\ 90 | * Just call buffered IO methods 91 | \*-------------------------------------------------------------------------*/ 92 | static int meth_send(lua_State *L) { 93 | p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1); 94 | return buffer_meth_send(L, &un->buf); 95 | } 96 | 97 | static int meth_receive(lua_State *L) { 98 | p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1); 99 | return buffer_meth_receive(L, &un->buf); 100 | } 101 | 102 | static int meth_getstats(lua_State *L) { 103 | p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1); 104 | return buffer_meth_getstats(L, &un->buf); 105 | } 106 | 107 | static int meth_setstats(lua_State *L) { 108 | p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1); 109 | return buffer_meth_setstats(L, &un->buf); 110 | } 111 | 112 | /*-------------------------------------------------------------------------*\ 113 | * Select support methods 114 | \*-------------------------------------------------------------------------*/ 115 | static int meth_getfd(lua_State *L) { 116 | p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); 117 | lua_pushnumber(L, (int) un->sock); 118 | return 1; 119 | } 120 | 121 | /* this is very dangerous, but can be handy for those that are brave enough */ 122 | static int meth_setfd(lua_State *L) { 123 | p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); 124 | un->sock = (t_socket) luaL_checknumber(L, 2); 125 | return 0; 126 | } 127 | 128 | static int meth_dirty(lua_State *L) { 129 | p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); 130 | lua_pushboolean(L, !buffer_isempty(&un->buf)); 131 | return 1; 132 | } 133 | 134 | /*-------------------------------------------------------------------------*\ 135 | * Closes socket used by object 136 | \*-------------------------------------------------------------------------*/ 137 | static int meth_close(lua_State *L) 138 | { 139 | p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); 140 | socket_destroy(&un->sock); 141 | lua_pushnumber(L, 1); 142 | return 1; 143 | } 144 | 145 | 146 | /*-------------------------------------------------------------------------*\ 147 | * Just call tm methods 148 | \*-------------------------------------------------------------------------*/ 149 | static int meth_settimeout(lua_State *L) { 150 | p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); 151 | return timeout_meth_settimeout(L, &un->tm); 152 | } 153 | 154 | /*=========================================================================*\ 155 | * Library functions 156 | \*=========================================================================*/ 157 | 158 | 159 | /*-------------------------------------------------------------------------*\ 160 | * Creates a serial object 161 | \*-------------------------------------------------------------------------*/ 162 | static int global_create(lua_State *L) { 163 | const char* path = luaL_checkstring(L, 1); 164 | 165 | /* allocate unix object */ 166 | p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); 167 | 168 | /* open serial device */ 169 | t_socket sock = open(path, O_NOCTTY|O_RDWR); 170 | 171 | /*printf("open %s on %d\n", path, sock);*/ 172 | 173 | if (sock < 0) { 174 | lua_pushnil(L); 175 | lua_pushstring(L, socket_strerror(errno)); 176 | lua_pushnumber(L, errno); 177 | return 3; 178 | } 179 | /* set its type as client object */ 180 | auxiliar_setclass(L, "serial{client}", -1); 181 | /* initialize remaining structure fields */ 182 | socket_setnonblocking(&sock); 183 | un->sock = sock; 184 | io_init(&un->io, (p_send) socket_write, (p_recv) socket_read, 185 | (p_error) socket_ioerror, &un->sock); 186 | timeout_init(&un->tm, -1, -1); 187 | buffer_init(&un->buf, &un->io, &un->tm); 188 | return 1; 189 | } 190 | -------------------------------------------------------------------------------- /external/lpack/lpack.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lpack.c 3 | * a Lua library for packing and unpacking binary data 4 | * Luiz Henrique de Figueiredo 5 | * 29 Jun 2007 19:27:20 6 | * This code is hereby placed in the public domain. 7 | * with contributions from Ignacio Castaño and 8 | * Roberto Ierusalimschy . 9 | */ 10 | 11 | #define OP_ZSTRING 'z' /* zero-terminated string */ 12 | #define OP_BSTRING 'p' /* string preceded by length byte */ 13 | #define OP_WSTRING 'P' /* string preceded by length word */ 14 | #define OP_SSTRING 'a' /* string preceded by length size_t */ 15 | #define OP_STRING 'A' /* string */ 16 | #define OP_FLOAT 'f' /* float */ 17 | #define OP_DOUBLE 'd' /* double */ 18 | #define OP_NUMBER 'n' /* Lua number */ 19 | #define OP_CHAR 'c' /* char */ 20 | #define OP_BYTE 'b' /* byte = unsigned char */ 21 | #define OP_SHORT 'h' /* short */ 22 | #define OP_USHORT 'H' /* unsigned short */ 23 | #define OP_INT 'i' /* int */ 24 | #define OP_UINT 'I' /* unsigned int */ 25 | #define OP_LONG 'l' /* long */ 26 | #define OP_ULONG 'L' /* unsigned long */ 27 | #define OP_LITTLEENDIAN '<' /* little endian */ 28 | #define OP_BIGENDIAN '>' /* big endian */ 29 | #define OP_NATIVE '=' /* native endian */ 30 | 31 | #include 32 | #include 33 | 34 | #include "lua.h" 35 | #include "lualib.h" 36 | #include "lauxlib.h" 37 | #include "lpack.h" 38 | static void badcode(lua_State *L, int c) 39 | { 40 | char s[]="bad code `?'"; 41 | s[sizeof(s)-3]=c; 42 | luaL_argerror(L,1,s); 43 | } 44 | 45 | static int doendian(int c) 46 | { 47 | int x=1; 48 | int e=*(char*)&x; 49 | if (c==OP_LITTLEENDIAN) return !e; 50 | if (c==OP_BIGENDIAN) return e; 51 | if (c==OP_NATIVE) return 0; 52 | return 0; 53 | } 54 | 55 | static void doswap(int swap, void *p, size_t n) 56 | { 57 | if (swap) 58 | { 59 | char *a=p; 60 | size_t i,j; 61 | for (i=0, j=n-1, n=n/2; n--; i++, j--) 62 | { 63 | char t=a[i]; a[i]=a[j]; a[j]=t; 64 | } 65 | } 66 | } 67 | 68 | #define UNPACKNUMBER(OP,T) \ 69 | case OP: \ 70 | { \ 71 | T a; \ 72 | int m=sizeof(a); \ 73 | if (i+m>len) goto done; \ 74 | memcpy(&a,s+i,m); \ 75 | i+=m; \ 76 | doswap(swap,&a,m); \ 77 | lua_pushnumber(L,(lua_Number)a); \ 78 | ++n; \ 79 | break; \ 80 | } 81 | 82 | #define UNPACKSTRING(OP,T) \ 83 | case OP: \ 84 | { \ 85 | T l; \ 86 | int m=sizeof(l); \ 87 | if (i+m>len) goto done; \ 88 | memcpy(&l,s+i,m); \ 89 | doswap(swap,&l,m); \ 90 | if (i+m+l>len) goto done; \ 91 | i+=m; \ 92 | lua_pushlstring(L,s+i,l); \ 93 | i+=l; \ 94 | ++n; \ 95 | break; \ 96 | } 97 | 98 | static int l_unpack(lua_State *L) /** unpack(s,f,[init]) */ 99 | { 100 | size_t len; 101 | const char *s=luaL_checklstring(L,1,&len); 102 | const char *f=luaL_checkstring(L,2); 103 | int i=luaL_optnumber(L,3,1)-1; 104 | int n=0; 105 | int swap=0; 106 | lua_pushnil(L); 107 | while (*f) 108 | { 109 | int c=*f++; 110 | int N=1; 111 | if (isdigit(*f)) 112 | { 113 | N=0; 114 | while (isdigit(*f)) N=10*N+(*f++)-'0'; 115 | if (N==0 && c==OP_STRING) { lua_pushliteral(L,""); ++n; } 116 | } 117 | while (N--) switch (c) 118 | { 119 | case OP_LITTLEENDIAN: 120 | case OP_BIGENDIAN: 121 | case OP_NATIVE: 122 | { 123 | swap=doendian(c); 124 | N=0; 125 | break; 126 | } 127 | case OP_STRING: 128 | { 129 | ++N; 130 | if (i+N>len) goto done; 131 | lua_pushlstring(L,s+i,N); 132 | i+=N; 133 | ++n; 134 | N=0; 135 | break; 136 | } 137 | case OP_ZSTRING: 138 | { 139 | size_t l; 140 | if (i>=len) goto done; 141 | l=strlen(s+i); 142 | lua_pushlstring(L,s+i,l); 143 | i+=l+1; 144 | ++n; 145 | break; 146 | } 147 | UNPACKSTRING(OP_BSTRING, unsigned char) 148 | UNPACKSTRING(OP_WSTRING, unsigned short) 149 | UNPACKSTRING(OP_SSTRING, size_t) 150 | UNPACKNUMBER(OP_NUMBER, lua_Number) 151 | UNPACKNUMBER(OP_DOUBLE, double) 152 | UNPACKNUMBER(OP_FLOAT, float) 153 | UNPACKNUMBER(OP_CHAR, char) 154 | UNPACKNUMBER(OP_BYTE, unsigned char) 155 | UNPACKNUMBER(OP_SHORT, short) 156 | UNPACKNUMBER(OP_USHORT, unsigned short) 157 | UNPACKNUMBER(OP_INT, int) 158 | UNPACKNUMBER(OP_UINT, unsigned int) 159 | UNPACKNUMBER(OP_LONG, long) 160 | UNPACKNUMBER(OP_ULONG, unsigned long) 161 | case ' ': case ',': 162 | break; 163 | default: 164 | badcode(L,c); 165 | break; 166 | } 167 | } 168 | done: 169 | lua_pushnumber(L,i+1); 170 | lua_replace(L,-n-2); 171 | return n+1; 172 | } 173 | 174 | #define PACKNUMBER(OP,T) \ 175 | case OP: \ 176 | { \ 177 | T a=(T)luaL_checknumber(L,i++); \ 178 | doswap(swap,&a,sizeof(a)); \ 179 | luaL_addlstring(&b,(void*)&a,sizeof(a)); \ 180 | break; \ 181 | } 182 | 183 | #define PACKSTRING(OP,T) \ 184 | case OP: \ 185 | { \ 186 | size_t l; \ 187 | const char *a=luaL_checklstring(L,i++,&l); \ 188 | T ll=(T)l; \ 189 | doswap(swap,&ll,sizeof(ll)); \ 190 | luaL_addlstring(&b,(void*)&ll,sizeof(ll)); \ 191 | luaL_addlstring(&b,a,l); \ 192 | break; \ 193 | } 194 | 195 | static int l_pack(lua_State *L) /** pack(f,...) */ 196 | { 197 | int i=2; 198 | const char *f=luaL_checkstring(L,1); 199 | int swap=0; 200 | luaL_Buffer b; 201 | luaL_buffinit(L,&b); 202 | while (*f) 203 | { 204 | int c=*f++; 205 | int N=1; 206 | if (isdigit(*f)) 207 | { 208 | N=0; 209 | while (isdigit(*f)) N=10*N+(*f++)-'0'; 210 | } 211 | while (N--) switch (c) 212 | { 213 | case OP_LITTLEENDIAN: 214 | case OP_BIGENDIAN: 215 | case OP_NATIVE: 216 | { 217 | swap=doendian(c); 218 | N=0; 219 | break; 220 | } 221 | case OP_STRING: 222 | case OP_ZSTRING: 223 | { 224 | size_t l; 225 | const char *a=luaL_checklstring(L,i++,&l); 226 | luaL_addlstring(&b,a,l+(c==OP_ZSTRING)); 227 | break; 228 | } 229 | PACKSTRING(OP_BSTRING, unsigned char) 230 | PACKSTRING(OP_WSTRING, unsigned short) 231 | PACKSTRING(OP_SSTRING, size_t) 232 | PACKNUMBER(OP_NUMBER, lua_Number) 233 | PACKNUMBER(OP_DOUBLE, double) 234 | PACKNUMBER(OP_FLOAT, float) 235 | PACKNUMBER(OP_CHAR, char) 236 | PACKNUMBER(OP_BYTE, unsigned char) 237 | PACKNUMBER(OP_SHORT, short) 238 | PACKNUMBER(OP_USHORT, unsigned short) 239 | PACKNUMBER(OP_INT, int) 240 | PACKNUMBER(OP_UINT, unsigned int) 241 | PACKNUMBER(OP_LONG, long) 242 | PACKNUMBER(OP_ULONG, unsigned long) 243 | case ' ': case ',': 244 | break; 245 | default: 246 | badcode(L,c); 247 | break; 248 | } 249 | } 250 | luaL_pushresult(&b); 251 | return 1; 252 | } 253 | 254 | static const luaL_reg R[] = 255 | { 256 | {"pack", l_pack}, 257 | {"unpack", l_unpack}, 258 | {NULL, NULL} 259 | }; 260 | 261 | int luaopen_pack(lua_State *L) 262 | { 263 | #ifdef USE_GLOBALS 264 | lua_register(L,"bpack",l_pack); 265 | lua_register(L,"bunpack",l_unpack); 266 | #else 267 | luaL_openlib(L, LUA_STRLIBNAME, R, 0); 268 | #endif 269 | return 0; 270 | } 271 | -------------------------------------------------------------------------------- /external/luasocket/select.c: -------------------------------------------------------------------------------- 1 | /*=========================================================================*\ 2 | * Select implementation 3 | * LuaSocket toolkit 4 | \*=========================================================================*/ 5 | #include 6 | 7 | #include "lua.h" 8 | #include "lauxlib.h" 9 | 10 | #include "socket.h" 11 | #include "timeout.h" 12 | #include "select.h" 13 | 14 | /*=========================================================================*\ 15 | * Internal function prototypes. 16 | \*=========================================================================*/ 17 | static t_socket getfd(lua_State *L); 18 | static int dirty(lua_State *L); 19 | static void collect_fd(lua_State *L, int tab, int itab, 20 | fd_set *set, t_socket *max_fd); 21 | static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set); 22 | static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, 23 | int itab, int tab, int start); 24 | static void make_assoc(lua_State *L, int tab); 25 | static int global_select(lua_State *L); 26 | 27 | /* functions in library namespace */ 28 | static luaL_Reg func[] = { 29 | {"select", global_select}, 30 | {NULL, NULL} 31 | }; 32 | 33 | /*=========================================================================*\ 34 | * Exported functions 35 | \*=========================================================================*/ 36 | /*-------------------------------------------------------------------------*\ 37 | * Initializes module 38 | \*-------------------------------------------------------------------------*/ 39 | int select_open(lua_State *L) { 40 | lua_pushstring(L, "_SETSIZE"); 41 | lua_pushnumber(L, FD_SETSIZE); 42 | lua_rawset(L, -3); 43 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) 44 | luaL_setfuncs(L, func, 0); 45 | #else 46 | luaL_openlib(L, NULL, func, 0); 47 | #endif 48 | return 0; 49 | } 50 | 51 | /*=========================================================================*\ 52 | * Global Lua functions 53 | \*=========================================================================*/ 54 | /*-------------------------------------------------------------------------*\ 55 | * Waits for a set of sockets until a condition is met or timeout. 56 | \*-------------------------------------------------------------------------*/ 57 | static int global_select(lua_State *L) { 58 | int rtab, wtab, itab, ret, ndirty; 59 | t_socket max_fd = SOCKET_INVALID; 60 | fd_set rset, wset; 61 | t_timeout tm; 62 | double t = luaL_optnumber(L, 3, -1); 63 | FD_ZERO(&rset); FD_ZERO(&wset); 64 | lua_settop(L, 3); 65 | lua_newtable(L); itab = lua_gettop(L); 66 | lua_newtable(L); rtab = lua_gettop(L); 67 | lua_newtable(L); wtab = lua_gettop(L); 68 | collect_fd(L, 1, itab, &rset, &max_fd); 69 | collect_fd(L, 2, itab, &wset, &max_fd); 70 | ndirty = check_dirty(L, 1, rtab, &rset); 71 | t = ndirty > 0? 0.0: t; 72 | timeout_init(&tm, t, -1); 73 | timeout_markstart(&tm); 74 | ret = socket_select(max_fd+1, &rset, &wset, NULL, &tm); 75 | if (ret > 0 || ndirty > 0) { 76 | return_fd(L, &rset, max_fd+1, itab, rtab, ndirty); 77 | return_fd(L, &wset, max_fd+1, itab, wtab, 0); 78 | make_assoc(L, rtab); 79 | make_assoc(L, wtab); 80 | return 2; 81 | } else if (ret == 0) { 82 | lua_pushstring(L, "timeout"); 83 | return 3; 84 | } else { 85 | luaL_error(L, "select failed"); 86 | return 3; 87 | } 88 | } 89 | 90 | /*=========================================================================*\ 91 | * Internal functions 92 | \*=========================================================================*/ 93 | static t_socket getfd(lua_State *L) { 94 | t_socket fd = SOCKET_INVALID; 95 | lua_pushstring(L, "getfd"); 96 | lua_gettable(L, -2); 97 | if (!lua_isnil(L, -1)) { 98 | lua_pushvalue(L, -2); 99 | lua_call(L, 1, 1); 100 | if (lua_isnumber(L, -1)) { 101 | double numfd = lua_tonumber(L, -1); 102 | fd = (numfd >= 0.0)? (t_socket) numfd: SOCKET_INVALID; 103 | } 104 | } 105 | lua_pop(L, 1); 106 | return fd; 107 | } 108 | 109 | static int dirty(lua_State *L) { 110 | int is = 0; 111 | lua_pushstring(L, "dirty"); 112 | lua_gettable(L, -2); 113 | if (!lua_isnil(L, -1)) { 114 | lua_pushvalue(L, -2); 115 | lua_call(L, 1, 1); 116 | is = lua_toboolean(L, -1); 117 | } 118 | lua_pop(L, 1); 119 | return is; 120 | } 121 | 122 | static void collect_fd(lua_State *L, int tab, int itab, 123 | fd_set *set, t_socket *max_fd) { 124 | int i = 1, n = 0; 125 | /* nil is the same as an empty table */ 126 | if (lua_isnil(L, tab)) return; 127 | /* otherwise we need it to be a table */ 128 | luaL_checktype(L, tab, LUA_TTABLE); 129 | for ( ;; ) { 130 | t_socket fd; 131 | lua_pushnumber(L, i); 132 | lua_gettable(L, tab); 133 | if (lua_isnil(L, -1)) { 134 | lua_pop(L, 1); 135 | break; 136 | } 137 | /* getfd figures out if this is a socket */ 138 | fd = getfd(L); 139 | if (fd != SOCKET_INVALID) { 140 | /* make sure we don't overflow the fd_set */ 141 | #ifdef _WIN32 142 | if (n >= FD_SETSIZE) 143 | luaL_argerror(L, tab, "too many sockets"); 144 | #else 145 | if (fd >= FD_SETSIZE) 146 | luaL_argerror(L, tab, "descriptor too large for set size"); 147 | #endif 148 | FD_SET(fd, set); 149 | n++; 150 | /* keep track of the largest descriptor so far */ 151 | if (*max_fd == SOCKET_INVALID || *max_fd < fd) 152 | *max_fd = fd; 153 | /* make sure we can map back from descriptor to the object */ 154 | lua_pushnumber(L, (lua_Number) fd); 155 | lua_pushvalue(L, -2); 156 | lua_settable(L, itab); 157 | } 158 | lua_pop(L, 1); 159 | i = i + 1; 160 | } 161 | } 162 | 163 | static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) { 164 | int ndirty = 0, i = 1; 165 | if (lua_isnil(L, tab)) 166 | return 0; 167 | for ( ;; ) { 168 | t_socket fd; 169 | lua_pushnumber(L, i); 170 | lua_gettable(L, tab); 171 | if (lua_isnil(L, -1)) { 172 | lua_pop(L, 1); 173 | break; 174 | } 175 | fd = getfd(L); 176 | if (fd != SOCKET_INVALID && dirty(L)) { 177 | lua_pushnumber(L, ++ndirty); 178 | lua_pushvalue(L, -2); 179 | lua_settable(L, dtab); 180 | FD_CLR(fd, set); 181 | } 182 | lua_pop(L, 1); 183 | i = i + 1; 184 | } 185 | return ndirty; 186 | } 187 | 188 | static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, 189 | int itab, int tab, int start) { 190 | t_socket fd; 191 | for (fd = 0; fd < max_fd; fd++) { 192 | if (FD_ISSET(fd, set)) { 193 | lua_pushnumber(L, ++start); 194 | lua_pushnumber(L, (lua_Number) fd); 195 | lua_gettable(L, itab); 196 | lua_settable(L, tab); 197 | } 198 | } 199 | } 200 | 201 | static void make_assoc(lua_State *L, int tab) { 202 | int i = 1, atab; 203 | lua_newtable(L); atab = lua_gettop(L); 204 | for ( ;; ) { 205 | lua_pushnumber(L, i); 206 | lua_gettable(L, tab); 207 | if (!lua_isnil(L, -1)) { 208 | lua_pushnumber(L, i); 209 | lua_pushvalue(L, -2); 210 | lua_settable(L, atab); 211 | lua_pushnumber(L, i); 212 | lua_settable(L, atab); 213 | } else { 214 | lua_pop(L, 1); 215 | break; 216 | } 217 | i = i+1; 218 | } 219 | } 220 | 221 | -------------------------------------------------------------------------------- /external/luasocket/timeout.c: -------------------------------------------------------------------------------- 1 | /*=========================================================================*\ 2 | * Timeout management functions 3 | * LuaSocket toolkit 4 | \*=========================================================================*/ 5 | #include 6 | #include 7 | #include 8 | 9 | #include "lua.h" 10 | #include "lauxlib.h" 11 | 12 | #include "auxiliar.h" 13 | #include "timeout.h" 14 | 15 | #ifdef _WIN32 16 | #include 17 | #else 18 | #include 19 | #include 20 | #endif 21 | 22 | /* min and max macros */ 23 | #ifndef MIN 24 | #define MIN(x, y) ((x) < (y) ? x : y) 25 | #endif 26 | #ifndef MAX 27 | #define MAX(x, y) ((x) > (y) ? x : y) 28 | #endif 29 | 30 | /*=========================================================================*\ 31 | * Internal function prototypes 32 | \*=========================================================================*/ 33 | static int timeout_lua_gettime(lua_State *L); 34 | static int timeout_lua_sleep(lua_State *L); 35 | 36 | static luaL_Reg func[] = { 37 | { "gettime", timeout_lua_gettime }, 38 | { "sleep", timeout_lua_sleep }, 39 | { NULL, NULL } 40 | }; 41 | 42 | /*=========================================================================*\ 43 | * Exported functions. 44 | \*=========================================================================*/ 45 | /*-------------------------------------------------------------------------*\ 46 | * Initialize structure 47 | \*-------------------------------------------------------------------------*/ 48 | void timeout_init(p_timeout tm, double block, double total) { 49 | tm->block = block; 50 | tm->total = total; 51 | } 52 | 53 | /*-------------------------------------------------------------------------*\ 54 | * Determines how much time we have left for the next system call, 55 | * if the previous call was successful 56 | * Input 57 | * tm: timeout control structure 58 | * Returns 59 | * the number of ms left or -1 if there is no time limit 60 | \*-------------------------------------------------------------------------*/ 61 | double timeout_get(p_timeout tm) { 62 | if (tm->block < 0.0 && tm->total < 0.0) { 63 | return -1; 64 | } else if (tm->block < 0.0) { 65 | double t = tm->total - timeout_gettime() + tm->start; 66 | return MAX(t, 0.0); 67 | } else if (tm->total < 0.0) { 68 | return tm->block; 69 | } else { 70 | double t = tm->total - timeout_gettime() + tm->start; 71 | return MIN(tm->block, MAX(t, 0.0)); 72 | } 73 | } 74 | 75 | /*-------------------------------------------------------------------------*\ 76 | * Returns time since start of operation 77 | * Input 78 | * tm: timeout control structure 79 | * Returns 80 | * start field of structure 81 | \*-------------------------------------------------------------------------*/ 82 | double timeout_getstart(p_timeout tm) { 83 | return tm->start; 84 | } 85 | 86 | /*-------------------------------------------------------------------------*\ 87 | * Determines how much time we have left for the next system call, 88 | * if the previous call was a failure 89 | * Input 90 | * tm: timeout control structure 91 | * Returns 92 | * the number of ms left or -1 if there is no time limit 93 | \*-------------------------------------------------------------------------*/ 94 | double timeout_getretry(p_timeout tm) { 95 | if (tm->block < 0.0 && tm->total < 0.0) { 96 | return -1; 97 | } else if (tm->block < 0.0) { 98 | double t = tm->total - timeout_gettime() + tm->start; 99 | return MAX(t, 0.0); 100 | } else if (tm->total < 0.0) { 101 | double t = tm->block - timeout_gettime() + tm->start; 102 | return MAX(t, 0.0); 103 | } else { 104 | double t = tm->total - timeout_gettime() + tm->start; 105 | return MIN(tm->block, MAX(t, 0.0)); 106 | } 107 | } 108 | 109 | /*-------------------------------------------------------------------------*\ 110 | * Marks the operation start time in structure 111 | * Input 112 | * tm: timeout control structure 113 | \*-------------------------------------------------------------------------*/ 114 | p_timeout timeout_markstart(p_timeout tm) { 115 | tm->start = timeout_gettime(); 116 | return tm; 117 | } 118 | 119 | /*-------------------------------------------------------------------------*\ 120 | * Gets time in s, relative to January 1, 1970 (UTC) 121 | * Returns 122 | * time in s. 123 | \*-------------------------------------------------------------------------*/ 124 | #ifdef _WIN32 125 | double timeout_gettime(void) { 126 | FILETIME ft; 127 | double t; 128 | GetSystemTimeAsFileTime(&ft); 129 | /* Windows file time (time since January 1, 1601 (UTC)) */ 130 | t = ft.dwLowDateTime/1.0e7 + ft.dwHighDateTime*(4294967296.0/1.0e7); 131 | /* convert to Unix Epoch time (time since January 1, 1970 (UTC)) */ 132 | return (t - 11644473600.0); 133 | } 134 | #else 135 | double timeout_gettime(void) { 136 | struct timeval v; 137 | gettimeofday(&v, (struct timezone *) NULL); 138 | /* Unix Epoch time (time since January 1, 1970 (UTC)) */ 139 | return v.tv_sec + v.tv_usec/1.0e6; 140 | } 141 | #endif 142 | 143 | /*-------------------------------------------------------------------------*\ 144 | * Initializes module 145 | \*-------------------------------------------------------------------------*/ 146 | int timeout_open(lua_State *L) { 147 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) 148 | luaL_setfuncs(L, func, 0); 149 | #else 150 | luaL_openlib(L, NULL, func, 0); 151 | #endif 152 | return 0; 153 | } 154 | 155 | /*-------------------------------------------------------------------------*\ 156 | * Sets timeout values for IO operations 157 | * Lua Input: base, time [, mode] 158 | * time: time out value in seconds 159 | * mode: "b" for block timeout, "t" for total timeout. (default: b) 160 | \*-------------------------------------------------------------------------*/ 161 | int timeout_meth_settimeout(lua_State *L, p_timeout tm) { 162 | double t = luaL_optnumber(L, 2, -1); 163 | const char *mode = luaL_optstring(L, 3, "b"); 164 | switch (*mode) { 165 | case 'b': 166 | tm->block = t; 167 | break; 168 | case 'r': case 't': 169 | tm->total = t; 170 | break; 171 | default: 172 | luaL_argcheck(L, 0, 3, "invalid timeout mode"); 173 | break; 174 | } 175 | lua_pushnumber(L, 1); 176 | return 1; 177 | } 178 | 179 | /*=========================================================================*\ 180 | * Test support functions 181 | \*=========================================================================*/ 182 | /*-------------------------------------------------------------------------*\ 183 | * Returns the time the system has been up, in secconds. 184 | \*-------------------------------------------------------------------------*/ 185 | static int timeout_lua_gettime(lua_State *L) 186 | { 187 | lua_pushnumber(L, timeout_gettime()); 188 | return 1; 189 | } 190 | 191 | /*-------------------------------------------------------------------------*\ 192 | * Sleep for n seconds. 193 | \*-------------------------------------------------------------------------*/ 194 | #ifdef _WIN32 195 | int timeout_lua_sleep(lua_State *L) 196 | { 197 | double n = luaL_checknumber(L, 1); 198 | if (n < 0.0) n = 0.0; 199 | if (n < DBL_MAX/1000.0) n *= 1000.0; 200 | if (n > INT_MAX) n = INT_MAX; 201 | Sleep((int)n); 202 | return 0; 203 | } 204 | #else 205 | int timeout_lua_sleep(lua_State *L) 206 | { 207 | double n = luaL_checknumber(L, 1); 208 | struct timespec t, r; 209 | if (n < 0.0) n = 0.0; 210 | if (n > INT_MAX) n = INT_MAX; 211 | t.tv_sec = (int) n; 212 | n -= t.tv_sec; 213 | t.tv_nsec = (int) (n * 1000000000); 214 | if (t.tv_nsec >= 1000000000) t.tv_nsec = 999999999; 215 | while (nanosleep(&t, &r) != 0) { 216 | t.tv_sec = r.tv_sec; 217 | t.tv_nsec = r.tv_nsec; 218 | } 219 | return 0; 220 | } 221 | #endif 222 | -------------------------------------------------------------------------------- /external/luasocket/script/socket/smtp.lua: -------------------------------------------------------------------------------- 1 | ----------------------------------------------------------------------------- 2 | -- SMTP client support for the Lua language. 3 | -- LuaSocket toolkit. 4 | -- Author: Diego Nehab 5 | ----------------------------------------------------------------------------- 6 | 7 | ----------------------------------------------------------------------------- 8 | -- Declare module and import dependencies 9 | ----------------------------------------------------------------------------- 10 | local base = _G 11 | local coroutine = require("coroutine") 12 | local string = require("string") 13 | local math = require("math") 14 | local os = require("os") 15 | local socket = require("socket.socket") 16 | local tp = require("socket.tp") 17 | local ltn12 = require("socket.ltn12") 18 | local headers = require("socket.headers") 19 | local mime = require("socket.mime") 20 | 21 | socket.smtp = {} 22 | local _M = socket.smtp 23 | 24 | ----------------------------------------------------------------------------- 25 | -- Program constants 26 | ----------------------------------------------------------------------------- 27 | -- timeout for connection 28 | _M.TIMEOUT = 60 29 | -- default server used to send e-mails 30 | _M.SERVER = "localhost" 31 | -- default port 32 | _M.PORT = 25 33 | -- domain used in HELO command and default sendmail 34 | -- If we are under a CGI, try to get from environment 35 | _M.DOMAIN = os.getenv("SERVER_NAME") or "localhost" 36 | -- default time zone (means we don't know) 37 | _M.ZONE = "-0000" 38 | 39 | --------------------------------------------------------------------------- 40 | -- Low level SMTP API 41 | ----------------------------------------------------------------------------- 42 | local metat = { __index = {} } 43 | 44 | function metat.__index:greet(domain) 45 | self.try(self.tp:check("2..")) 46 | self.try(self.tp:command("EHLO", domain or _M.DOMAIN)) 47 | return socket.skip(1, self.try(self.tp:check("2.."))) 48 | end 49 | 50 | function metat.__index:mail(from) 51 | self.try(self.tp:command("MAIL", "FROM:" .. from)) 52 | return self.try(self.tp:check("2..")) 53 | end 54 | 55 | function metat.__index:rcpt(to) 56 | self.try(self.tp:command("RCPT", "TO:" .. to)) 57 | return self.try(self.tp:check("2..")) 58 | end 59 | 60 | function metat.__index:data(src, step) 61 | self.try(self.tp:command("DATA")) 62 | self.try(self.tp:check("3..")) 63 | self.try(self.tp:source(src, step)) 64 | self.try(self.tp:send("\r\n.\r\n")) 65 | return self.try(self.tp:check("2..")) 66 | end 67 | 68 | function metat.__index:quit() 69 | self.try(self.tp:command("QUIT")) 70 | return self.try(self.tp:check("2..")) 71 | end 72 | 73 | function metat.__index:close() 74 | return self.tp:close() 75 | end 76 | 77 | function metat.__index:login(user, password) 78 | self.try(self.tp:command("AUTH", "LOGIN")) 79 | self.try(self.tp:check("3..")) 80 | self.try(self.tp:send(mime.b64(user) .. "\r\n")) 81 | self.try(self.tp:check("3..")) 82 | self.try(self.tp:send(mime.b64(password) .. "\r\n")) 83 | return self.try(self.tp:check("2..")) 84 | end 85 | 86 | function metat.__index:plain(user, password) 87 | local auth = "PLAIN " .. mime.b64("\0" .. user .. "\0" .. password) 88 | self.try(self.tp:command("AUTH", auth)) 89 | return self.try(self.tp:check("2..")) 90 | end 91 | 92 | function metat.__index:auth(user, password, ext) 93 | if not user or not password then return 1 end 94 | if string.find(ext, "AUTH[^\n]+LOGIN") then 95 | return self:login(user, password) 96 | elseif string.find(ext, "AUTH[^\n]+PLAIN") then 97 | return self:plain(user, password) 98 | else 99 | self.try(nil, "authentication not supported") 100 | end 101 | end 102 | 103 | -- send message or throw an exception 104 | function metat.__index:send(mailt) 105 | self:mail(mailt.from) 106 | if base.type(mailt.rcpt) == "table" then 107 | for i,v in base.ipairs(mailt.rcpt) do 108 | self:rcpt(v) 109 | end 110 | else 111 | self:rcpt(mailt.rcpt) 112 | end 113 | self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step) 114 | end 115 | 116 | function _M.open(server, port, create) 117 | local tp = socket.try(tp.connect(server or _M.SERVER, port or _M.PORT, 118 | _M.TIMEOUT, create)) 119 | local s = base.setmetatable({tp = tp}, metat) 120 | -- make sure tp is closed if we get an exception 121 | s.try = socket.newtry(function() 122 | s:close() 123 | end) 124 | return s 125 | end 126 | 127 | -- convert headers to lowercase 128 | local function lower_headers(headers) 129 | local lower = {} 130 | for i,v in base.pairs(headers or lower) do 131 | lower[string.lower(i)] = v 132 | end 133 | return lower 134 | end 135 | 136 | --------------------------------------------------------------------------- 137 | -- Multipart message source 138 | ----------------------------------------------------------------------------- 139 | -- returns a hopefully unique mime boundary 140 | local seqno = 0 141 | local function newboundary() 142 | seqno = seqno + 1 143 | return string.format('%s%05d==%05u', os.date('%d%m%Y%H%M%S'), 144 | math.random(0, 99999), seqno) 145 | end 146 | 147 | -- send_message forward declaration 148 | local send_message 149 | 150 | -- yield the headers all at once, it's faster 151 | local function send_headers(tosend) 152 | local canonic = headers.canonic 153 | local h = "\r\n" 154 | for f,v in base.pairs(tosend) do 155 | h = (canonic[f] or f) .. ': ' .. v .. "\r\n" .. h 156 | end 157 | coroutine.yield(h) 158 | end 159 | 160 | -- yield multipart message body from a multipart message table 161 | local function send_multipart(mesgt) 162 | -- make sure we have our boundary and send headers 163 | local bd = newboundary() 164 | local headers = lower_headers(mesgt.headers or {}) 165 | headers['content-type'] = headers['content-type'] or 'multipart/mixed' 166 | headers['content-type'] = headers['content-type'] .. 167 | '; boundary="' .. bd .. '"' 168 | send_headers(headers) 169 | -- send preamble 170 | if mesgt.body.preamble then 171 | coroutine.yield(mesgt.body.preamble) 172 | coroutine.yield("\r\n") 173 | end 174 | -- send each part separated by a boundary 175 | for i, m in base.ipairs(mesgt.body) do 176 | coroutine.yield("\r\n--" .. bd .. "\r\n") 177 | send_message(m) 178 | end 179 | -- send last boundary 180 | coroutine.yield("\r\n--" .. bd .. "--\r\n\r\n") 181 | -- send epilogue 182 | if mesgt.body.epilogue then 183 | coroutine.yield(mesgt.body.epilogue) 184 | coroutine.yield("\r\n") 185 | end 186 | end 187 | 188 | -- yield message body from a source 189 | local function send_source(mesgt) 190 | -- make sure we have a content-type 191 | local headers = lower_headers(mesgt.headers or {}) 192 | headers['content-type'] = headers['content-type'] or 193 | 'text/plain; charset="iso-8859-1"' 194 | send_headers(headers) 195 | -- send body from source 196 | while true do 197 | local chunk, err = mesgt.body() 198 | if err then coroutine.yield(nil, err) 199 | elseif chunk then coroutine.yield(chunk) 200 | else break end 201 | end 202 | end 203 | 204 | -- yield message body from a string 205 | local function send_string(mesgt) 206 | -- make sure we have a content-type 207 | local headers = lower_headers(mesgt.headers or {}) 208 | headers['content-type'] = headers['content-type'] or 209 | 'text/plain; charset="iso-8859-1"' 210 | send_headers(headers) 211 | -- send body from string 212 | coroutine.yield(mesgt.body) 213 | end 214 | 215 | -- message source 216 | function send_message(mesgt) 217 | if base.type(mesgt.body) == "table" then send_multipart(mesgt) 218 | elseif base.type(mesgt.body) == "function" then send_source(mesgt) 219 | else send_string(mesgt) end 220 | end 221 | 222 | -- set defaul headers 223 | local function adjust_headers(mesgt) 224 | local lower = lower_headers(mesgt.headers) 225 | lower["date"] = lower["date"] or 226 | os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or _M.ZONE) 227 | lower["x-mailer"] = lower["x-mailer"] or socket._VERSION 228 | -- this can't be overriden 229 | lower["mime-version"] = "1.0" 230 | return lower 231 | end 232 | 233 | function _M.message(mesgt) 234 | mesgt.headers = adjust_headers(mesgt) 235 | -- create and return message source 236 | local co = coroutine.create(function() send_message(mesgt) end) 237 | return function() 238 | local ret, a, b = coroutine.resume(co) 239 | if ret then return a, b 240 | else return nil, a end 241 | end 242 | end 243 | 244 | --------------------------------------------------------------------------- 245 | -- High level SMTP API 246 | ----------------------------------------------------------------------------- 247 | _M.send = socket.protect(function(mailt) 248 | local s = _M.open(mailt.server, mailt.port, mailt.create) 249 | local ext = s:greet(mailt.domain) 250 | s:auth(mailt.user, mailt.password, ext) 251 | s:send(mailt) 252 | s:quit() 253 | return s:close() 254 | end) 255 | 256 | return _M 257 | -------------------------------------------------------------------------------- /external/luasocket/script/ltn12.lua: -------------------------------------------------------------------------------- 1 | ----------------------------------------------------------------------------- 2 | -- LTN12 - Filters, sources, sinks and pumps. 3 | -- LuaSocket toolkit. 4 | -- Author: Diego Nehab 5 | ----------------------------------------------------------------------------- 6 | 7 | ----------------------------------------------------------------------------- 8 | -- Declare module 9 | ----------------------------------------------------------------------------- 10 | local string = require("string") 11 | local table = require("table") 12 | local base = _G 13 | local _M = {} 14 | if module then -- heuristic for exporting a global package table 15 | ltn12 = _M 16 | end 17 | local filter,source,sink,pump = {},{},{},{} 18 | 19 | _M.filter = filter 20 | _M.source = source 21 | _M.sink = sink 22 | _M.pump = pump 23 | 24 | -- 2048 seems to be better in windows... 25 | _M.BLOCKSIZE = 2048 26 | _M._VERSION = "LTN12 1.0.3" 27 | 28 | ----------------------------------------------------------------------------- 29 | -- Filter stuff 30 | ----------------------------------------------------------------------------- 31 | -- returns a high level filter that cycles a low-level filter 32 | function filter.cycle(low, ctx, extra) 33 | base.assert(low) 34 | return function(chunk) 35 | local ret 36 | ret, ctx = low(ctx, chunk, extra) 37 | return ret 38 | end 39 | end 40 | 41 | -- chains a bunch of filters together 42 | -- (thanks to Wim Couwenberg) 43 | function filter.chain(...) 44 | local arg = {...} 45 | local n = select('#',...) 46 | local top, index = 1, 1 47 | local retry = "" 48 | return function(chunk) 49 | retry = chunk and retry 50 | while true do 51 | if index == top then 52 | chunk = arg[index](chunk) 53 | if chunk == "" or top == n then return chunk 54 | elseif chunk then index = index + 1 55 | else 56 | top = top+1 57 | index = top 58 | end 59 | else 60 | chunk = arg[index](chunk or "") 61 | if chunk == "" then 62 | index = index - 1 63 | chunk = retry 64 | elseif chunk then 65 | if index == n then return chunk 66 | else index = index + 1 end 67 | else base.error("filter returned inappropriate nil") end 68 | end 69 | end 70 | end 71 | end 72 | 73 | ----------------------------------------------------------------------------- 74 | -- Source stuff 75 | ----------------------------------------------------------------------------- 76 | -- create an empty source 77 | local function empty() 78 | return nil 79 | end 80 | 81 | function source.empty() 82 | return empty 83 | end 84 | 85 | -- returns a source that just outputs an error 86 | function source.error(err) 87 | return function() 88 | return nil, err 89 | end 90 | end 91 | 92 | -- creates a file source 93 | function source.file(handle, io_err) 94 | if handle then 95 | return function() 96 | local chunk = handle:read(_M.BLOCKSIZE) 97 | if not chunk then handle:close() end 98 | return chunk 99 | end 100 | else return source.error(io_err or "unable to open file") end 101 | end 102 | 103 | -- turns a fancy source into a simple source 104 | function source.simplify(src) 105 | base.assert(src) 106 | return function() 107 | local chunk, err_or_new = src() 108 | src = err_or_new or src 109 | if not chunk then return nil, err_or_new 110 | else return chunk end 111 | end 112 | end 113 | 114 | -- creates string source 115 | function source.string(s) 116 | if s then 117 | local i = 1 118 | return function() 119 | local chunk = string.sub(s, i, i+_M.BLOCKSIZE-1) 120 | i = i + _M.BLOCKSIZE 121 | if chunk ~= "" then return chunk 122 | else return nil end 123 | end 124 | else return source.empty() end 125 | end 126 | 127 | -- creates rewindable source 128 | function source.rewind(src) 129 | base.assert(src) 130 | local t = {} 131 | return function(chunk) 132 | if not chunk then 133 | chunk = table.remove(t) 134 | if not chunk then return src() 135 | else return chunk end 136 | else 137 | table.insert(t, chunk) 138 | end 139 | end 140 | end 141 | 142 | function source.chain(src, f) 143 | base.assert(src and f) 144 | local last_in, last_out = "", "" 145 | local state = "feeding" 146 | local err 147 | return function() 148 | if not last_out then 149 | base.error('source is empty!', 2) 150 | end 151 | while true do 152 | if state == "feeding" then 153 | last_in, err = src() 154 | if err then return nil, err end 155 | last_out = f(last_in) 156 | if not last_out then 157 | if last_in then 158 | base.error('filter returned inappropriate nil') 159 | else 160 | return nil 161 | end 162 | elseif last_out ~= "" then 163 | state = "eating" 164 | if last_in then last_in = "" end 165 | return last_out 166 | end 167 | else 168 | last_out = f(last_in) 169 | if last_out == "" then 170 | if last_in == "" then 171 | state = "feeding" 172 | else 173 | base.error('filter returned ""') 174 | end 175 | elseif not last_out then 176 | if last_in then 177 | base.error('filter returned inappropriate nil') 178 | else 179 | return nil 180 | end 181 | else 182 | return last_out 183 | end 184 | end 185 | end 186 | end 187 | end 188 | 189 | -- creates a source that produces contents of several sources, one after the 190 | -- other, as if they were concatenated 191 | -- (thanks to Wim Couwenberg) 192 | function source.cat(...) 193 | local arg = {...} 194 | local src = table.remove(arg, 1) 195 | return function() 196 | while src do 197 | local chunk, err = src() 198 | if chunk then return chunk end 199 | if err then return nil, err end 200 | src = table.remove(arg, 1) 201 | end 202 | end 203 | end 204 | 205 | ----------------------------------------------------------------------------- 206 | -- Sink stuff 207 | ----------------------------------------------------------------------------- 208 | -- creates a sink that stores into a table 209 | function sink.table(t) 210 | t = t or {} 211 | local f = function(chunk, err) 212 | if chunk then table.insert(t, chunk) end 213 | return 1 214 | end 215 | return f, t 216 | end 217 | 218 | -- turns a fancy sink into a simple sink 219 | function sink.simplify(snk) 220 | base.assert(snk) 221 | return function(chunk, err) 222 | local ret, err_or_new = snk(chunk, err) 223 | if not ret then return nil, err_or_new end 224 | snk = err_or_new or snk 225 | return 1 226 | end 227 | end 228 | 229 | -- creates a file sink 230 | function sink.file(handle, io_err) 231 | if handle then 232 | return function(chunk, err) 233 | if not chunk then 234 | handle:close() 235 | return 1 236 | else return handle:write(chunk) end 237 | end 238 | else return sink.error(io_err or "unable to open file") end 239 | end 240 | 241 | -- creates a sink that discards data 242 | local function null() 243 | return 1 244 | end 245 | 246 | function sink.null() 247 | return null 248 | end 249 | 250 | -- creates a sink that just returns an error 251 | function sink.error(err) 252 | return function() 253 | return nil, err 254 | end 255 | end 256 | 257 | -- chains a sink with a filter 258 | function sink.chain(f, snk) 259 | base.assert(f and snk) 260 | return function(chunk, err) 261 | if chunk ~= "" then 262 | local filtered = f(chunk) 263 | local done = chunk and "" 264 | while true do 265 | local ret, snkerr = snk(filtered, err) 266 | if not ret then return nil, snkerr end 267 | if filtered == done then return 1 end 268 | filtered = f(done) 269 | end 270 | else return 1 end 271 | end 272 | end 273 | 274 | ----------------------------------------------------------------------------- 275 | -- Pump stuff 276 | ----------------------------------------------------------------------------- 277 | -- pumps one chunk from the source to the sink 278 | function pump.step(src, snk) 279 | local chunk, src_err = src() 280 | local ret, snk_err = snk(chunk, src_err) 281 | if chunk and ret then return 1 282 | else return nil, src_err or snk_err end 283 | end 284 | 285 | -- pumps all data from a source to a sink, using a step function 286 | function pump.all(src, snk, step) 287 | base.assert(src and snk) 288 | step = step or pump.step 289 | while true do 290 | local ret, err = step(src, snk) 291 | if not ret then 292 | if err then return nil, err 293 | else return 1 end 294 | end 295 | end 296 | end 297 | 298 | return _M 299 | -------------------------------------------------------------------------------- /Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | include $(CLEAR_VARS) 4 | 5 | LOCAL_MODULE := cocos2d_lua_android_static 6 | 7 | LOCAL_MODULE_FILENAME := libluacocos2dandroid 8 | 9 | LOCAL_ARM_MODE := arm 10 | 11 | LOCAL_SRC_FILES := ../manual/platform/android/CCLuaJavaBridge.cpp \ 12 | ../manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp 13 | 14 | LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../.. \ 15 | $(LOCAL_PATH)/../manual \ 16 | $(LOCAL_PATH)/../../../../external/lua/tolua \ 17 | $(LOCAL_PATH)/../manual/platform/android \ 18 | $(LOCAL_PATH)/../manual/platform/android/jni 19 | 20 | LOCAL_EXPORT_LDLIBS := -lGLESv2 \ 21 | -llog \ 22 | -landroid 23 | 24 | ifeq ($(TARGET_ARCH_ABI),arm64-v8a) 25 | LUA_STATIC_LIB := lua_static 26 | LUA_IMPORT_PATH := lua/lua 27 | LUA_INCLUDE_PATH := $(LOCAL_PATH)/../../../../external/lua/lua 28 | else 29 | LUA_STATIC_LIB := luajit_static 30 | LUA_IMPORT_PATH := lua/luajit/prebuilt/android 31 | LUA_INCLUDE_PATH := $(LOCAL_PATH)/../../../../external/lua/luajit/include 32 | endif 33 | 34 | LOCAL_STATIC_LIBRARIES := $(LUA_STATIC_LIB) 35 | 36 | include $(BUILD_STATIC_LIBRARY) 37 | 38 | #============================================================== 39 | 40 | include $(CLEAR_VARS) 41 | 42 | LOCAL_MODULE := cocos2d_lua_static 43 | 44 | LOCAL_MODULE_FILENAME := libluacocos2d 45 | 46 | LOCAL_ARM_MODE := arm 47 | 48 | LOCAL_SRC_FILES := ../manual/CCLuaBridge.cpp \ 49 | ../manual/CCLuaEngine.cpp \ 50 | ../manual/CCLuaStack.cpp \ 51 | ../manual/CCLuaValue.cpp \ 52 | ../manual/Cocos2dxLuaLoader.cpp \ 53 | ../manual/LuaBasicConversions.cpp \ 54 | ../manual/lua_module_register.cpp \ 55 | ../auto/lua_cocos2dx_auto.cpp \ 56 | ../auto/lua_cocos2dx_physics_auto.cpp \ 57 | ../auto/lua_cocos2dx_experimental_auto.cpp \ 58 | ../manual/cocos2d/lua_cocos2dx_deprecated.cpp \ 59 | ../manual/cocos2d/lua_cocos2dx_experimental_manual.cpp \ 60 | ../manual/cocos2d/lua_cocos2dx_manual.cpp \ 61 | ../manual/cocos2d/lua_cocos2dx_physics_manual.cpp \ 62 | ../manual/cocos2d/LuaOpengl.cpp \ 63 | ../manual/cocos2d/LuaScriptHandlerMgr.cpp \ 64 | ../manual/tolua_fix.cpp \ 65 | ../../../../external/lua/tolua/tolua_event.c \ 66 | ../../../../external/lua/tolua/tolua_is.c \ 67 | ../../../../external/lua/tolua/tolua_map.c \ 68 | ../../../../external/lua/tolua/tolua_push.c \ 69 | ../../../../external/lua/tolua/tolua_to.c \ 70 | ../../../../external/xxtea/xxtea.cpp \ 71 | ../auto/lua_cocos2dx_audioengine_auto.cpp \ 72 | ../manual/audioengine/lua_cocos2dx_audioengine_manual.cpp \ 73 | ../../../../external/lua/lpeg/lpcap.c \ 74 | ../../../../external/lua/lpeg/lpcode.c \ 75 | ../../../../external/lua/lpeg/lpprint.c \ 76 | ../../../../external/lua/lpeg/lptree.c \ 77 | ../../../../external/lua/lpeg/lpvm.c \ 78 | ../../../../external/lua/sproto/lsproto.c \ 79 | ../../../../external/lua/sproto/sproto.c \ 80 | ../../../../external/lua/lpack/lpack.c 81 | 82 | #Component 83 | LOCAL_SRC_FILES += ../manual/CCComponentLua.cpp \ 84 | 85 | #3d 86 | LOCAL_SRC_FILES += ../manual/3d/lua_cocos2dx_3d_manual.cpp \ 87 | ../auto/lua_cocos2dx_3d_auto.cpp 88 | 89 | #cocosdenshion 90 | LOCAL_SRC_FILES += ../manual/cocosdenshion/lua_cocos2dx_cocosdenshion_manual.cpp \ 91 | ../auto/lua_cocos2dx_cocosdenshion_auto.cpp 92 | 93 | #network 94 | LOCAL_SRC_FILES += ../manual/network/lua_cocos2dx_network_manual.cpp \ 95 | ../manual/network/lua_extensions.c \ 96 | ../manual/network/Lua_web_socket.cpp \ 97 | ../manual/network/lua_xml_http_request.cpp \ 98 | ../../../../external/lua/luasocket/auxiliar.c \ 99 | ../../../../external/lua/luasocket/buffer.c \ 100 | ../../../../external/lua/luasocket/except.c \ 101 | ../../../../external/lua/luasocket/inet.c \ 102 | ../../../../external/lua/luasocket/io.c \ 103 | ../../../../external/lua/luasocket/luasocket.c \ 104 | ../../../../external/lua/luasocket/luasocket_scripts.c \ 105 | ../../../../external/lua/luasocket/mime.c \ 106 | ../../../../external/lua/luasocket/options.c \ 107 | ../../../../external/lua/luasocket/select.c \ 108 | ../../../../external/lua/luasocket/serial.c \ 109 | ../../../../external/lua/luasocket/tcp.c \ 110 | ../../../../external/lua/luasocket/timeout.c \ 111 | ../../../../external/lua/luasocket/udp.c \ 112 | ../../../../external/lua/luasocket/unix.c \ 113 | ../../../../external/lua/luasocket/usocket.c 114 | 115 | #cocosbuilder 116 | LOCAL_SRC_FILES += ../manual/cocosbuilder/lua_cocos2dx_cocosbuilder_manual.cpp \ 117 | ../manual/cocosbuilder/CCBProxy.cpp \ 118 | ../auto/lua_cocos2dx_cocosbuilder_auto.cpp 119 | 120 | #cocostudio 121 | LOCAL_SRC_FILES += ../manual/cocostudio/lua_cocos2dx_coco_studio_manual.cpp \ 122 | ../manual/cocostudio/CustomGUIReader.cpp \ 123 | ../manual/cocostudio/lua_cocos2dx_csloader_manual.cpp \ 124 | ../auto/lua_cocos2dx_csloader_auto.cpp \ 125 | ../auto/lua_cocos2dx_studio_auto.cpp \ 126 | ../manual/cocostudio/lua-cocos-studio-conversions.cpp 127 | 128 | #spine 129 | LOCAL_SRC_FILES += ../manual/spine/lua_cocos2dx_spine_manual.cpp \ 130 | ../manual/spine/LuaSkeletonAnimation.cpp \ 131 | ../auto/lua_cocos2dx_spine_auto.cpp 132 | 133 | #ui 134 | LOCAL_SRC_FILES += ../manual/ui/lua_cocos2dx_experimental_webview_manual.cpp \ 135 | ../manual/ui/lua_cocos2dx_experimental_video_manual.cpp \ 136 | ../manual/ui/lua_cocos2dx_ui_manual.cpp \ 137 | ../auto/lua_cocos2dx_experimental_video_auto.cpp \ 138 | ../auto/lua_cocos2dx_ui_auto.cpp \ 139 | ../auto/lua_cocos2dx_experimental_webview_auto.cpp 140 | 141 | #extension 142 | LOCAL_SRC_FILES += ../manual/extension/lua_cocos2dx_extension_manual.cpp \ 143 | ../auto/lua_cocos2dx_extension_auto.cpp \ 144 | 145 | #physics3d 146 | LOCAL_SRC_FILES += ../manual/physics3d/lua_cocos2dx_physics3d_manual.cpp \ 147 | ../auto/lua_cocos2dx_physics3d_auto.cpp \ 148 | 149 | #navmesh 150 | LOCAL_SRC_FILES += ../manual/navmesh/lua_cocos2dx_navmesh_conversions.cpp \ 151 | ../manual/navmesh/lua_cocos2dx_navmesh_manual.cpp \ 152 | ../auto/lua_cocos2dx_navmesh_auto.cpp \ 153 | 154 | LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../../external/lua/tolua \ 155 | $(LUA_INCLUDE_PATH) \ 156 | $(LOCAL_PATH)/../../../2d \ 157 | $(LOCAL_PATH)/../../../3d \ 158 | $(LOCAL_PATH)/../../../network \ 159 | $(LOCAL_PATH)/../../../editor-support/cocosbuilder \ 160 | $(LOCAL_PATH)/../../../editor-support/cocostudio \ 161 | $(LOCAL_PATH)/../../../editor-support/cocostudio/ActionTimeline \ 162 | $(LOCAL_PATH)/../../../editor-support/spine \ 163 | $(LOCAL_PATH)/../../../ui \ 164 | $(LOCAL_PATH)/../../../physics3d \ 165 | $(LOCAL_PATH)/../../../navmesh \ 166 | $(LOCAL_PATH)/../../../../extensions \ 167 | $(LOCAL_PATH)/../auto \ 168 | $(LOCAL_PATH)/../manual \ 169 | $(LOCAL_PATH)/../manual/cocos2d \ 170 | $(LOCAL_PATH)/../manual/3d \ 171 | $(LOCAL_PATH)/../manual/cocosdenshion \ 172 | $(LOCAL_PATH)/../manual/audioengine \ 173 | $(LOCAL_PATH)/../manual/network \ 174 | $(LOCAL_PATH)/../manual/extension \ 175 | $(LOCAL_PATH)/../manual/cocostudio \ 176 | $(LOCAL_PATH)/../manual/cocosbuilder \ 177 | $(LOCAL_PATH)/../manual/spine \ 178 | $(LOCAL_PATH)/../manual/ui \ 179 | $(LOCAL_PATH)/../manual/navmesh \ 180 | $(LOCAL_PATH)/../../../../external/xxtea \ 181 | $(LOCAL_PATH)/../../../.. \ 182 | $(LOCAL_PATH)/../../../../external/lua 183 | 184 | LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../../../external/lua/tolua \ 185 | $(LUA_INCLUDE_PATH) \ 186 | $(LOCAL_PATH)/../auto \ 187 | $(LOCAL_PATH)/../manual \ 188 | $(LOCAL_PATH)/../manual/cocos2d \ 189 | $(LOCAL_PATH)/../manual/3d \ 190 | $(LOCAL_PATH)/../manual/cocosdenshion \ 191 | $(LOCAL_PATH)/../manual/audioengine \ 192 | $(LOCAL_PATH)/../manual/network \ 193 | $(LOCAL_PATH)/../manual/cocosbuilder \ 194 | $(LOCAL_PATH)/../manual/cocostudio \ 195 | $(LOCAL_PATH)/../manual/spine \ 196 | $(LOCAL_PATH)/../manual/extension \ 197 | $(LOCAL_PATH)/../manual/ui \ 198 | $(LOCAL_PATH)/../manual/navmesh \ 199 | $(LOCAL_PATH)/../../../.. 200 | 201 | LOCAL_WHOLE_STATIC_LIBRARIES := cocos2d_lua_android_static 202 | 203 | LOCAL_STATIC_LIBRARIES := cocos2dx_static 204 | 205 | include $(BUILD_STATIC_LIBRARY) 206 | 207 | $(call import-module,$(LUA_IMPORT_PATH)) 208 | $(call import-module,.) 209 | -------------------------------------------------------------------------------- /external/luasocket/script/socket/ftp.lua: -------------------------------------------------------------------------------- 1 | ----------------------------------------------------------------------------- 2 | -- FTP support for the Lua language 3 | -- LuaSocket toolkit. 4 | -- Author: Diego Nehab 5 | ----------------------------------------------------------------------------- 6 | 7 | ----------------------------------------------------------------------------- 8 | -- Declare module and import dependencies 9 | ----------------------------------------------------------------------------- 10 | local base = _G 11 | local table = require("table") 12 | local string = require("string") 13 | local math = require("math") 14 | local socket = require("socket.socket") 15 | local url = require("socket.url") 16 | local tp = require("socket.tp") 17 | local ltn12 = require("ltn12") 18 | socket.ftp = {} 19 | local _M = socket.ftp 20 | ----------------------------------------------------------------------------- 21 | -- Program constants 22 | ----------------------------------------------------------------------------- 23 | -- timeout in seconds before the program gives up on a connection 24 | _M.TIMEOUT = 60 25 | -- default port for ftp service 26 | _M.PORT = 21 27 | -- this is the default anonymous password. used when no password is 28 | -- provided in url. should be changed to your e-mail. 29 | _M.USER = "ftp" 30 | _M.PASSWORD = "anonymous@anonymous.org" 31 | 32 | ----------------------------------------------------------------------------- 33 | -- Low level FTP API 34 | ----------------------------------------------------------------------------- 35 | local metat = { __index = {} } 36 | 37 | function _M.open(server, port, create) 38 | local tp = socket.try(tp.connect(server, port or _M.PORT, _M.TIMEOUT, create)) 39 | local f = base.setmetatable({ tp = tp }, metat) 40 | -- make sure everything gets closed in an exception 41 | f.try = socket.newtry(function() f:close() end) 42 | return f 43 | end 44 | 45 | function metat.__index:portconnect() 46 | self.try(self.server:settimeout(_M.TIMEOUT)) 47 | self.data = self.try(self.server:accept()) 48 | self.try(self.data:settimeout(_M.TIMEOUT)) 49 | end 50 | 51 | function metat.__index:pasvconnect() 52 | self.data = self.try(socket.tcp()) 53 | self.try(self.data:settimeout(_M.TIMEOUT)) 54 | self.try(self.data:connect(self.pasvt.ip, self.pasvt.port)) 55 | end 56 | 57 | function metat.__index:login(user, password) 58 | self.try(self.tp:command("user", user or _M.USER)) 59 | local code, reply = self.try(self.tp:check{"2..", 331}) 60 | if code == 331 then 61 | self.try(self.tp:command("pass", password or _M.PASSWORD)) 62 | self.try(self.tp:check("2..")) 63 | end 64 | return 1 65 | end 66 | 67 | function metat.__index:pasv() 68 | self.try(self.tp:command("pasv")) 69 | local code, reply = self.try(self.tp:check("2..")) 70 | local pattern = "(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)" 71 | local a, b, c, d, p1, p2 = socket.skip(2, string.find(reply, pattern)) 72 | self.try(a and b and c and d and p1 and p2, reply) 73 | self.pasvt = { 74 | ip = string.format("%d.%d.%d.%d", a, b, c, d), 75 | port = p1*256 + p2 76 | } 77 | if self.server then 78 | self.server:close() 79 | self.server = nil 80 | end 81 | return self.pasvt.ip, self.pasvt.port 82 | end 83 | 84 | function metat.__index:port(ip, port) 85 | self.pasvt = nil 86 | if not ip then 87 | ip, port = self.try(self.tp:getcontrol():getsockname()) 88 | self.server = self.try(socket.bind(ip, 0)) 89 | ip, port = self.try(self.server:getsockname()) 90 | self.try(self.server:settimeout(_M.TIMEOUT)) 91 | end 92 | local pl = math.mod(port, 256) 93 | local ph = (port - pl)/256 94 | local arg = string.gsub(string.format("%s,%d,%d", ip, ph, pl), "%.", ",") 95 | self.try(self.tp:command("port", arg)) 96 | self.try(self.tp:check("2..")) 97 | return 1 98 | end 99 | 100 | function metat.__index:send(sendt) 101 | self.try(self.pasvt or self.server, "need port or pasv first") 102 | -- if there is a pasvt table, we already sent a PASV command 103 | -- we just get the data connection into self.data 104 | if self.pasvt then self:pasvconnect() end 105 | -- get the transfer argument and command 106 | local argument = sendt.argument or 107 | url.unescape(string.gsub(sendt.path or "", "^[/\\]", "")) 108 | if argument == "" then argument = nil end 109 | local command = sendt.command or "stor" 110 | -- send the transfer command and check the reply 111 | self.try(self.tp:command(command, argument)) 112 | local code, reply = self.try(self.tp:check{"2..", "1.."}) 113 | -- if there is not a a pasvt table, then there is a server 114 | -- and we already sent a PORT command 115 | if not self.pasvt then self:portconnect() end 116 | -- get the sink, source and step for the transfer 117 | local step = sendt.step or ltn12.pump.step 118 | local readt = {self.tp.c} 119 | local checkstep = function(src, snk) 120 | -- check status in control connection while downloading 121 | local readyt = socket.select(readt, nil, 0) 122 | if readyt[tp] then code = self.try(self.tp:check("2..")) end 123 | return step(src, snk) 124 | end 125 | local sink = socket.sink("close-when-done", self.data) 126 | -- transfer all data and check error 127 | self.try(ltn12.pump.all(sendt.source, sink, checkstep)) 128 | if string.find(code, "1..") then self.try(self.tp:check("2..")) end 129 | -- done with data connection 130 | self.data:close() 131 | -- find out how many bytes were sent 132 | local sent = socket.skip(1, self.data:getstats()) 133 | self.data = nil 134 | return sent 135 | end 136 | 137 | function metat.__index:receive(recvt) 138 | self.try(self.pasvt or self.server, "need port or pasv first") 139 | if self.pasvt then self:pasvconnect() end 140 | local argument = recvt.argument or 141 | url.unescape(string.gsub(recvt.path or "", "^[/\\]", "")) 142 | if argument == "" then argument = nil end 143 | local command = recvt.command or "retr" 144 | self.try(self.tp:command(command, argument)) 145 | local code,reply = self.try(self.tp:check{"1..", "2.."}) 146 | if (code >= 200) and (code <= 299) then 147 | recvt.sink(reply) 148 | return 1 149 | end 150 | if not self.pasvt then self:portconnect() end 151 | local source = socket.source("until-closed", self.data) 152 | local step = recvt.step or ltn12.pump.step 153 | self.try(ltn12.pump.all(source, recvt.sink, step)) 154 | if string.find(code, "1..") then self.try(self.tp:check("2..")) end 155 | self.data:close() 156 | self.data = nil 157 | return 1 158 | end 159 | 160 | function metat.__index:cwd(dir) 161 | self.try(self.tp:command("cwd", dir)) 162 | self.try(self.tp:check(250)) 163 | return 1 164 | end 165 | 166 | function metat.__index:type(type) 167 | self.try(self.tp:command("type", type)) 168 | self.try(self.tp:check(200)) 169 | return 1 170 | end 171 | 172 | function metat.__index:greet() 173 | local code = self.try(self.tp:check{"1..", "2.."}) 174 | if string.find(code, "1..") then self.try(self.tp:check("2..")) end 175 | return 1 176 | end 177 | 178 | function metat.__index:quit() 179 | self.try(self.tp:command("quit")) 180 | self.try(self.tp:check("2..")) 181 | return 1 182 | end 183 | 184 | function metat.__index:close() 185 | if self.data then self.data:close() end 186 | if self.server then self.server:close() end 187 | return self.tp:close() 188 | end 189 | 190 | ----------------------------------------------------------------------------- 191 | -- High level FTP API 192 | ----------------------------------------------------------------------------- 193 | local function override(t) 194 | if t.url then 195 | local u = url.parse(t.url) 196 | for i,v in base.pairs(t) do 197 | u[i] = v 198 | end 199 | return u 200 | else return t end 201 | end 202 | 203 | local function tput(putt) 204 | putt = override(putt) 205 | socket.try(putt.host, "missing hostname") 206 | local f = _M.open(putt.host, putt.port, putt.create) 207 | f:greet() 208 | f:login(putt.user, putt.password) 209 | if putt.type then f:type(putt.type) end 210 | f:pasv() 211 | local sent = f:send(putt) 212 | f:quit() 213 | f:close() 214 | return sent 215 | end 216 | 217 | local default = { 218 | path = "/", 219 | scheme = "ftp" 220 | } 221 | 222 | local function parse(u) 223 | local t = socket.try(url.parse(u, default)) 224 | socket.try(t.scheme == "ftp", "wrong scheme '" .. t.scheme .. "'") 225 | socket.try(t.host, "missing hostname") 226 | local pat = "^type=(.)$" 227 | if t.params then 228 | t.type = socket.skip(2, string.find(t.params, pat)) 229 | socket.try(t.type == "a" or t.type == "i", 230 | "invalid type '" .. t.type .. "'") 231 | end 232 | return t 233 | end 234 | 235 | local function sput(u, body) 236 | local putt = parse(u) 237 | putt.source = ltn12.source.string(body) 238 | return tput(putt) 239 | end 240 | 241 | _M.put = socket.protect(function(putt, body) 242 | if base.type(putt) == "string" then return sput(putt, body) 243 | else return tput(putt) end 244 | end) 245 | 246 | local function tget(gett) 247 | gett = override(gett) 248 | socket.try(gett.host, "missing hostname") 249 | local f = _M.open(gett.host, gett.port, gett.create) 250 | f:greet() 251 | f:login(gett.user, gett.password) 252 | if gett.type then f:type(gett.type) end 253 | f:pasv() 254 | f:receive(gett) 255 | f:quit() 256 | return f:close() 257 | end 258 | 259 | local function sget(u) 260 | local gett = parse(u) 261 | local t = {} 262 | gett.sink = ltn12.sink.table(t) 263 | tget(gett) 264 | return table.concat(t) 265 | end 266 | 267 | _M.command = socket.protect(function(cmdt) 268 | cmdt = override(cmdt) 269 | socket.try(cmdt.host, "missing hostname") 270 | socket.try(cmdt.command, "missing command") 271 | local f = open(cmdt.host, cmdt.port, cmdt.create) 272 | f:greet() 273 | f:login(cmdt.user, cmdt.password) 274 | f.try(f.tp:command(cmdt.command, cmdt.argument)) 275 | if cmdt.check then f.try(f.tp:check(cmdt.check)) end 276 | f:quit() 277 | return f:close() 278 | end) 279 | 280 | _M.get = socket.protect(function(gett) 281 | if base.type(gett) == "string" then return sget(gett) 282 | else return tget(gett) end 283 | end) 284 | 285 | return _M 286 | -------------------------------------------------------------------------------- /external/luasocket/buffer.c: -------------------------------------------------------------------------------- 1 | /*=========================================================================*\ 2 | * Input/Output interface for Lua programs 3 | * LuaSocket toolkit 4 | \*=========================================================================*/ 5 | #include "lua.h" 6 | #include "lauxlib.h" 7 | 8 | #include "buffer.h" 9 | 10 | /*=========================================================================*\ 11 | * Internal function prototypes 12 | \*=========================================================================*/ 13 | static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b); 14 | static int recvline(p_buffer buf, luaL_Buffer *b); 15 | static int recvall(p_buffer buf, luaL_Buffer *b); 16 | static int buffer_get(p_buffer buf, const char **data, size_t *count); 17 | static void buffer_skip(p_buffer buf, size_t count); 18 | static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent); 19 | 20 | /* min and max macros */ 21 | #ifndef MIN 22 | #define MIN(x, y) ((x) < (y) ? x : y) 23 | #endif 24 | #ifndef MAX 25 | #define MAX(x, y) ((x) > (y) ? x : y) 26 | #endif 27 | 28 | /*=========================================================================*\ 29 | * Exported functions 30 | \*=========================================================================*/ 31 | /*-------------------------------------------------------------------------*\ 32 | * Initializes module 33 | \*-------------------------------------------------------------------------*/ 34 | int buffer_open(lua_State *L) { 35 | (void) L; 36 | return 0; 37 | } 38 | 39 | /*-------------------------------------------------------------------------*\ 40 | * Initializes C structure 41 | \*-------------------------------------------------------------------------*/ 42 | void buffer_init(p_buffer buf, p_io io, p_timeout tm) { 43 | buf->first = buf->last = 0; 44 | buf->io = io; 45 | buf->tm = tm; 46 | buf->received = buf->sent = 0; 47 | buf->birthday = timeout_gettime(); 48 | } 49 | 50 | /*-------------------------------------------------------------------------*\ 51 | * object:getstats() interface 52 | \*-------------------------------------------------------------------------*/ 53 | int buffer_meth_getstats(lua_State *L, p_buffer buf) { 54 | lua_pushnumber(L, (lua_Number) buf->received); 55 | lua_pushnumber(L, (lua_Number) buf->sent); 56 | lua_pushnumber(L, timeout_gettime() - buf->birthday); 57 | return 3; 58 | } 59 | 60 | /*-------------------------------------------------------------------------*\ 61 | * object:setstats() interface 62 | \*-------------------------------------------------------------------------*/ 63 | int buffer_meth_setstats(lua_State *L, p_buffer buf) { 64 | buf->received = (long) luaL_optnumber(L, 2, (lua_Number) buf->received); 65 | buf->sent = (long) luaL_optnumber(L, 3, (lua_Number) buf->sent); 66 | if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4); 67 | lua_pushnumber(L, 1); 68 | return 1; 69 | } 70 | 71 | /*-------------------------------------------------------------------------*\ 72 | * object:send() interface 73 | \*-------------------------------------------------------------------------*/ 74 | int buffer_meth_send(lua_State *L, p_buffer buf) { 75 | int top = lua_gettop(L); 76 | int err = IO_DONE; 77 | size_t size = 0, sent = 0; 78 | const char *data = luaL_checklstring(L, 2, &size); 79 | long start = (long) luaL_optnumber(L, 3, 1); 80 | long end = (long) luaL_optnumber(L, 4, -1); 81 | #ifdef LUASOCKET_DEBUG 82 | p_timeout tm = timeout_markstart(buf->tm); 83 | #endif 84 | if (start < 0) start = (long) (size+start+1); 85 | if (end < 0) end = (long) (size+end+1); 86 | if (start < 1) start = (long) 1; 87 | if (end > (long) size) end = (long) size; 88 | if (start <= end) err = sendraw(buf, data+start-1, end-start+1, &sent); 89 | /* check if there was an error */ 90 | if (err != IO_DONE) { 91 | lua_pushnil(L); 92 | lua_pushstring(L, buf->io->error(buf->io->ctx, err)); 93 | lua_pushnumber(L, (lua_Number) (sent+start-1)); 94 | } else { 95 | lua_pushnumber(L, (lua_Number) (sent+start-1)); 96 | lua_pushnil(L); 97 | lua_pushnil(L); 98 | } 99 | #ifdef LUASOCKET_DEBUG 100 | /* push time elapsed during operation as the last return value */ 101 | lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm)); 102 | #endif 103 | return lua_gettop(L) - top; 104 | } 105 | 106 | /*-------------------------------------------------------------------------*\ 107 | * object:receive() interface 108 | \*-------------------------------------------------------------------------*/ 109 | int buffer_meth_receive(lua_State *L, p_buffer buf) { 110 | int err = IO_DONE, top = lua_gettop(L); 111 | luaL_Buffer b; 112 | size_t size; 113 | const char *part = luaL_optlstring(L, 3, "", &size); 114 | #ifdef LUASOCKET_DEBUG 115 | p_timeout tm = timeout_markstart(buf->tm); 116 | #endif 117 | /* initialize buffer with optional extra prefix 118 | * (useful for concatenating previous partial results) */ 119 | luaL_buffinit(L, &b); 120 | luaL_addlstring(&b, part, size); 121 | /* receive new patterns */ 122 | if (!lua_isnumber(L, 2)) { 123 | const char *p= luaL_optstring(L, 2, "*l"); 124 | if (p[0] == '*' && p[1] == 'l') err = recvline(buf, &b); 125 | else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b); 126 | else luaL_argcheck(L, 0, 2, "invalid receive pattern"); 127 | /* get a fixed number of bytes (minus what was already partially 128 | * received) */ 129 | } else { 130 | double n = lua_tonumber(L, 2); 131 | size_t wanted = (size_t) n; 132 | luaL_argcheck(L, n >= 0, 2, "invalid receive pattern"); 133 | if (size == 0 || wanted > size) 134 | err = recvraw(buf, wanted-size, &b); 135 | } 136 | /* check if there was an error */ 137 | if (err != IO_DONE) { 138 | /* we can't push anyting in the stack before pushing the 139 | * contents of the buffer. this is the reason for the complication */ 140 | luaL_pushresult(&b); 141 | lua_pushstring(L, buf->io->error(buf->io->ctx, err)); 142 | lua_pushvalue(L, -2); 143 | lua_pushnil(L); 144 | lua_replace(L, -4); 145 | } else { 146 | luaL_pushresult(&b); 147 | lua_pushnil(L); 148 | lua_pushnil(L); 149 | } 150 | #ifdef LUASOCKET_DEBUG 151 | /* push time elapsed during operation as the last return value */ 152 | lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm)); 153 | #endif 154 | return lua_gettop(L) - top; 155 | } 156 | 157 | /*-------------------------------------------------------------------------*\ 158 | * Determines if there is any data in the read buffer 159 | \*-------------------------------------------------------------------------*/ 160 | int buffer_isempty(p_buffer buf) { 161 | return buf->first >= buf->last; 162 | } 163 | 164 | /*=========================================================================*\ 165 | * Internal functions 166 | \*=========================================================================*/ 167 | /*-------------------------------------------------------------------------*\ 168 | * Sends a block of data (unbuffered) 169 | \*-------------------------------------------------------------------------*/ 170 | #define STEPSIZE 8192 171 | static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent) { 172 | p_io io = buf->io; 173 | p_timeout tm = buf->tm; 174 | size_t total = 0; 175 | int err = IO_DONE; 176 | while (total < count && err == IO_DONE) { 177 | size_t done = 0; 178 | size_t step = (count-total <= STEPSIZE)? count-total: STEPSIZE; 179 | err = io->send(io->ctx, data+total, step, &done, tm); 180 | total += done; 181 | } 182 | *sent = total; 183 | buf->sent += total; 184 | return err; 185 | } 186 | 187 | /*-------------------------------------------------------------------------*\ 188 | * Reads a fixed number of bytes (buffered) 189 | \*-------------------------------------------------------------------------*/ 190 | static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b) { 191 | int err = IO_DONE; 192 | size_t total = 0; 193 | while (err == IO_DONE) { 194 | size_t count; const char *data; 195 | err = buffer_get(buf, &data, &count); 196 | count = MIN(count, wanted - total); 197 | luaL_addlstring(b, data, count); 198 | buffer_skip(buf, count); 199 | total += count; 200 | if (total >= wanted) break; 201 | } 202 | return err; 203 | } 204 | 205 | /*-------------------------------------------------------------------------*\ 206 | * Reads everything until the connection is closed (buffered) 207 | \*-------------------------------------------------------------------------*/ 208 | static int recvall(p_buffer buf, luaL_Buffer *b) { 209 | int err = IO_DONE; 210 | size_t total = 0; 211 | while (err == IO_DONE) { 212 | const char *data; size_t count; 213 | err = buffer_get(buf, &data, &count); 214 | total += count; 215 | luaL_addlstring(b, data, count); 216 | buffer_skip(buf, count); 217 | } 218 | if (err == IO_CLOSED) { 219 | if (total > 0) return IO_DONE; 220 | else return IO_CLOSED; 221 | } else return err; 222 | } 223 | 224 | /*-------------------------------------------------------------------------*\ 225 | * Reads a line terminated by a CR LF pair or just by a LF. The CR and LF 226 | * are not returned by the function and are discarded from the buffer 227 | \*-------------------------------------------------------------------------*/ 228 | static int recvline(p_buffer buf, luaL_Buffer *b) { 229 | int err = IO_DONE; 230 | while (err == IO_DONE) { 231 | size_t count, pos; const char *data; 232 | err = buffer_get(buf, &data, &count); 233 | pos = 0; 234 | while (pos < count && data[pos] != '\n') { 235 | /* we ignore all \r's */ 236 | if (data[pos] != '\r') luaL_addchar(b, data[pos]); 237 | pos++; 238 | } 239 | if (pos < count) { /* found '\n' */ 240 | buffer_skip(buf, pos+1); /* skip '\n' too */ 241 | break; /* we are done */ 242 | } else /* reached the end of the buffer */ 243 | buffer_skip(buf, pos); 244 | } 245 | return err; 246 | } 247 | 248 | /*-------------------------------------------------------------------------*\ 249 | * Skips a given number of bytes from read buffer. No data is read from the 250 | * transport layer 251 | \*-------------------------------------------------------------------------*/ 252 | static void buffer_skip(p_buffer buf, size_t count) { 253 | buf->received += count; 254 | buf->first += count; 255 | if (buffer_isempty(buf)) 256 | buf->first = buf->last = 0; 257 | } 258 | 259 | /*-------------------------------------------------------------------------*\ 260 | * Return any data available in buffer, or get more data from transport layer 261 | * if buffer is empty 262 | \*-------------------------------------------------------------------------*/ 263 | static int buffer_get(p_buffer buf, const char **data, size_t *count) { 264 | int err = IO_DONE; 265 | p_io io = buf->io; 266 | p_timeout tm = buf->tm; 267 | if (buffer_isempty(buf)) { 268 | size_t got; 269 | err = io->recv(io->ctx, buf->data, BUF_SIZE, &got, tm); 270 | buf->first = 0; 271 | buf->last = got; 272 | } 273 | *count = buf->last - buf->first; 274 | *data = buf->data + buf->first; 275 | return err; 276 | } 277 | -------------------------------------------------------------------------------- /external/lpeg/lpvm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lpvm.c,v 1.5 2013/04/12 16:29:49 roberto Exp $ 3 | ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | 10 | #include "lua.h" 11 | #include "lauxlib.h" 12 | 13 | #include "lpcap.h" 14 | #include "lptypes.h" 15 | #include "lpvm.h" 16 | #include "lpprint.h" 17 | 18 | 19 | /* initial size for call/backtrack stack */ 20 | #if !defined(INITBACK) 21 | #define INITBACK 100 22 | #endif 23 | 24 | 25 | #define getoffset(p) (((p) + 1)->offset) 26 | 27 | static const Instruction giveup = {{IGiveup, 0, 0}}; 28 | 29 | 30 | /* 31 | ** {====================================================== 32 | ** Virtual Machine 33 | ** ======================================================= 34 | */ 35 | 36 | 37 | typedef struct Stack { 38 | const char *s; /* saved position (or NULL for calls) */ 39 | const Instruction *p; /* next instruction */ 40 | int caplevel; 41 | } Stack; 42 | 43 | 44 | #define getstackbase(L, ptop) ((Stack *)lua_touserdata(L, stackidx(ptop))) 45 | 46 | 47 | /* 48 | ** Double the size of the array of captures 49 | */ 50 | static Capture *doublecap (lua_State *L, Capture *cap, int captop, int ptop) { 51 | Capture *newc; 52 | if (captop >= INT_MAX/((int)sizeof(Capture) * 2)) 53 | luaL_error(L, "too many captures"); 54 | newc = (Capture *)lua_newuserdata(L, captop * 2 * sizeof(Capture)); 55 | memcpy(newc, cap, captop * sizeof(Capture)); 56 | lua_replace(L, caplistidx(ptop)); 57 | return newc; 58 | } 59 | 60 | 61 | /* 62 | ** Double the size of the stack 63 | */ 64 | static Stack *doublestack (lua_State *L, Stack **stacklimit, int ptop) { 65 | Stack *stack = getstackbase(L, ptop); 66 | Stack *newstack; 67 | int n = *stacklimit - stack; /* current stack size */ 68 | int max, newn; 69 | lua_getfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); 70 | max = lua_tointeger(L, -1); /* maximum allowed size */ 71 | lua_pop(L, 1); 72 | if (n >= max) /* already at maximum size? */ 73 | luaL_error(L, "too many pending calls/choices"); 74 | newn = 2 * n; /* new size */ 75 | if (newn > max) newn = max; 76 | newstack = (Stack *)lua_newuserdata(L, newn * sizeof(Stack)); 77 | memcpy(newstack, stack, n * sizeof(Stack)); 78 | lua_replace(L, stackidx(ptop)); 79 | *stacklimit = newstack + newn; 80 | return newstack + n; /* return next position */ 81 | } 82 | 83 | 84 | /* 85 | ** Interpret the result of a dynamic capture: false -> fail; 86 | ** true -> keep current position; number -> next position. 87 | ** Return new subject position. 'fr' is stack index where 88 | ** is the result; 'curr' is current subject position; 'limit' 89 | ** is subject's size. 90 | */ 91 | static int resdyncaptures (lua_State *L, int fr, int curr, int limit) { 92 | lua_Integer res; 93 | if (!lua_toboolean(L, fr)) { /* false value? */ 94 | lua_settop(L, fr - 1); /* remove results */ 95 | return -1; /* and fail */ 96 | } 97 | else if (lua_isboolean(L, fr)) /* true? */ 98 | res = curr; /* keep current position */ 99 | else { 100 | res = lua_tointeger(L, fr) - 1; /* new position */ 101 | if (res < curr || res > limit) 102 | luaL_error(L, "invalid position returned by match-time capture"); 103 | } 104 | lua_remove(L, fr); /* remove first result (offset) */ 105 | return res; 106 | } 107 | 108 | 109 | /* 110 | ** Add capture values returned by a dynamic capture to the capture list 111 | ** 'base', nested inside a group capture. 'fd' indexes the first capture 112 | ** value, 'n' is the number of values (at least 1). 113 | */ 114 | static void adddyncaptures (const char *s, Capture *base, int n, int fd) { 115 | int i; 116 | /* Cgroup capture is already there */ 117 | assert(base[0].kind == Cgroup && base[0].siz == 0); 118 | base[0].idx = 0; /* make it an anonymous group */ 119 | for (i = 1; i <= n; i++) { /* add runtime captures */ 120 | base[i].kind = Cruntime; 121 | base[i].siz = 1; /* mark it as closed */ 122 | base[i].idx = fd + i - 1; /* stack index of capture value */ 123 | base[i].s = s; 124 | } 125 | base[i].kind = Cclose; /* close group */ 126 | base[i].siz = 1; 127 | base[i].s = s; 128 | } 129 | 130 | 131 | /* 132 | ** Remove dynamic captures from the Lua stack (called in case of failure) 133 | */ 134 | static int removedyncap (lua_State *L, Capture *capture, 135 | int level, int last) { 136 | int id = finddyncap(capture + level, capture + last); /* index of 1st cap. */ 137 | int top = lua_gettop(L); 138 | if (id == 0) return 0; /* no dynamic captures? */ 139 | lua_settop(L, id - 1); /* remove captures */ 140 | return top - id + 1; /* number of values removed */ 141 | } 142 | 143 | 144 | /* 145 | ** Opcode interpreter 146 | */ 147 | const char *match (lua_State *L, const char *o, const char *s, const char *e, 148 | Instruction *op, Capture *capture, int ptop) { 149 | Stack stackbase[INITBACK]; 150 | Stack *stacklimit = stackbase + INITBACK; 151 | Stack *stack = stackbase; /* point to first empty slot in stack */ 152 | int capsize = INITCAPSIZE; 153 | int captop = 0; /* point to first empty slot in captures */ 154 | int ndyncap = 0; /* number of dynamic captures (in Lua stack) */ 155 | const Instruction *p = op; /* current instruction */ 156 | stack->p = &giveup; stack->s = s; stack->caplevel = 0; stack++; 157 | lua_pushlightuserdata(L, stackbase); 158 | for (;;) { 159 | #if defined(DEBUG) 160 | printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ", 161 | s, stack - getstackbase(L, ptop), ndyncap, captop); 162 | printinst(op, p); 163 | printcaplist(capture, capture + captop); 164 | #endif 165 | assert(stackidx(ptop) + ndyncap == lua_gettop(L) && ndyncap <= captop); 166 | switch ((Opcode)p->i.code) { 167 | case IEnd: { 168 | assert(stack == getstackbase(L, ptop) + 1); 169 | capture[captop].kind = Cclose; 170 | capture[captop].s = NULL; 171 | return s; 172 | } 173 | case IGiveup: { 174 | assert(stack == getstackbase(L, ptop)); 175 | return NULL; 176 | } 177 | case IRet: { 178 | assert(stack > getstackbase(L, ptop) && (stack - 1)->s == NULL); 179 | p = (--stack)->p; 180 | continue; 181 | } 182 | case IAny: { 183 | if (s < e) { p++; s++; } 184 | else goto fail; 185 | continue; 186 | } 187 | case ITestAny: { 188 | if (s < e) p += 2; 189 | else p += getoffset(p); 190 | continue; 191 | } 192 | case IChar: { 193 | if ((byte)*s == p->i.aux && s < e) { p++; s++; } 194 | else goto fail; 195 | continue; 196 | } 197 | case ITestChar: { 198 | if ((byte)*s == p->i.aux && s < e) p += 2; 199 | else p += getoffset(p); 200 | continue; 201 | } 202 | case ISet: { 203 | int c = (byte)*s; 204 | if (testchar((p+1)->buff, c) && s < e) 205 | { p += CHARSETINSTSIZE; s++; } 206 | else goto fail; 207 | continue; 208 | } 209 | case ITestSet: { 210 | int c = (byte)*s; 211 | if (testchar((p + 2)->buff, c) && s < e) 212 | p += 1 + CHARSETINSTSIZE; 213 | else p += getoffset(p); 214 | continue; 215 | } 216 | case IBehind: { 217 | int n = p->i.aux; 218 | if (n > s - o) goto fail; 219 | s -= n; p++; 220 | continue; 221 | } 222 | case ISpan: { 223 | for (; s < e; s++) { 224 | int c = (byte)*s; 225 | if (!testchar((p+1)->buff, c)) break; 226 | } 227 | p += CHARSETINSTSIZE; 228 | continue; 229 | } 230 | case IJmp: { 231 | p += getoffset(p); 232 | continue; 233 | } 234 | case IChoice: { 235 | if (stack == stacklimit) 236 | stack = doublestack(L, &stacklimit, ptop); 237 | stack->p = p + getoffset(p); 238 | stack->s = s; 239 | stack->caplevel = captop; 240 | stack++; 241 | p += 2; 242 | continue; 243 | } 244 | case ICall: { 245 | if (stack == stacklimit) 246 | stack = doublestack(L, &stacklimit, ptop); 247 | stack->s = NULL; 248 | stack->p = p + 2; /* save return address */ 249 | stack++; 250 | p += getoffset(p); 251 | continue; 252 | } 253 | case ICommit: { 254 | assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); 255 | stack--; 256 | p += getoffset(p); 257 | continue; 258 | } 259 | case IPartialCommit: { 260 | assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); 261 | (stack - 1)->s = s; 262 | (stack - 1)->caplevel = captop; 263 | p += getoffset(p); 264 | continue; 265 | } 266 | case IBackCommit: { 267 | assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); 268 | s = (--stack)->s; 269 | captop = stack->caplevel; 270 | p += getoffset(p); 271 | continue; 272 | } 273 | case IFailTwice: 274 | assert(stack > getstackbase(L, ptop)); 275 | stack--; 276 | /* go through */ 277 | case IFail: 278 | fail: { /* pattern failed: try to backtrack */ 279 | do { /* remove pending calls */ 280 | assert(stack > getstackbase(L, ptop)); 281 | s = (--stack)->s; 282 | } while (s == NULL); 283 | if (ndyncap > 0) /* is there matchtime captures? */ 284 | ndyncap -= removedyncap(L, capture, stack->caplevel, captop); 285 | captop = stack->caplevel; 286 | p = stack->p; 287 | continue; 288 | } 289 | case ICloseRunTime: { 290 | CapState cs; 291 | int rem, res, n; 292 | int fr = lua_gettop(L) + 1; /* stack index of first result */ 293 | cs.s = o; cs.L = L; cs.ocap = capture; cs.ptop = ptop; 294 | n = runtimecap(&cs, capture + captop, s, &rem); /* call function */ 295 | captop -= n; /* remove nested captures */ 296 | fr -= rem; /* 'rem' items were popped from Lua stack */ 297 | res = resdyncaptures(L, fr, s - o, e - o); /* get result */ 298 | if (res == -1) /* fail? */ 299 | goto fail; 300 | s = o + res; /* else update current position */ 301 | n = lua_gettop(L) - fr + 1; /* number of new captures */ 302 | ndyncap += n - rem; /* update number of dynamic captures */ 303 | if (n > 0) { /* any new capture? */ 304 | if ((captop += n + 2) >= capsize) { 305 | capture = doublecap(L, capture, captop, ptop); 306 | capsize = 2 * captop; 307 | } 308 | /* add new captures to 'capture' list */ 309 | adddyncaptures(s, capture + captop - n - 2, n, fr); 310 | } 311 | p++; 312 | continue; 313 | } 314 | case ICloseCapture: { 315 | const char *s1 = s; 316 | assert(captop > 0); 317 | /* if possible, turn capture into a full capture */ 318 | if (capture[captop - 1].siz == 0 && 319 | s1 - capture[captop - 1].s < UCHAR_MAX) { 320 | capture[captop - 1].siz = s1 - capture[captop - 1].s + 1; 321 | p++; 322 | continue; 323 | } 324 | else { 325 | capture[captop].siz = 1; /* mark entry as closed */ 326 | capture[captop].s = s; 327 | goto pushcapture; 328 | } 329 | } 330 | case IOpenCapture: 331 | capture[captop].siz = 0; /* mark entry as open */ 332 | capture[captop].s = s; 333 | goto pushcapture; 334 | case IFullCapture: 335 | capture[captop].siz = getoff(p) + 1; /* save capture size */ 336 | capture[captop].s = s - getoff(p); 337 | /* goto pushcapture; */ 338 | pushcapture: { 339 | capture[captop].idx = p->i.key; 340 | capture[captop].kind = getkind(p); 341 | if (++captop >= capsize) { 342 | capture = doublecap(L, capture, captop, ptop); 343 | capsize = 2 * captop; 344 | } 345 | p++; 346 | continue; 347 | } 348 | default: assert(0); return NULL; 349 | } 350 | } 351 | } 352 | 353 | /* }====================================================== */ 354 | 355 | 356 | -------------------------------------------------------------------------------- /external/luasocket/script/socket/url.lua: -------------------------------------------------------------------------------- 1 | ----------------------------------------------------------------------------- 2 | -- URI parsing, composition and relative URL resolution 3 | -- LuaSocket toolkit. 4 | -- Author: Diego Nehab 5 | ----------------------------------------------------------------------------- 6 | 7 | ----------------------------------------------------------------------------- 8 | -- Declare module 9 | ----------------------------------------------------------------------------- 10 | local string = require("string") 11 | local base = _G 12 | local table = require("table") 13 | local socket = require("socket.socket") 14 | 15 | socket.url = {} 16 | local _M = socket.url 17 | 18 | ----------------------------------------------------------------------------- 19 | -- Module version 20 | ----------------------------------------------------------------------------- 21 | _M._VERSION = "URL 1.0.3" 22 | 23 | ----------------------------------------------------------------------------- 24 | -- Encodes a string into its escaped hexadecimal representation 25 | -- Input 26 | -- s: binary string to be encoded 27 | -- Returns 28 | -- escaped representation of string binary 29 | ----------------------------------------------------------------------------- 30 | function _M.escape(s) 31 | return (string.gsub(s, "([^A-Za-z0-9_])", function(c) 32 | return string.format("%%%02x", string.byte(c)) 33 | end)) 34 | end 35 | 36 | ----------------------------------------------------------------------------- 37 | -- Protects a path segment, to prevent it from interfering with the 38 | -- url parsing. 39 | -- Input 40 | -- s: binary string to be encoded 41 | -- Returns 42 | -- escaped representation of string binary 43 | ----------------------------------------------------------------------------- 44 | local function make_set(t) 45 | local s = {} 46 | for i,v in base.ipairs(t) do 47 | s[t[i]] = 1 48 | end 49 | return s 50 | end 51 | 52 | -- these are allowed withing a path segment, along with alphanum 53 | -- other characters must be escaped 54 | local segment_set = make_set { 55 | "-", "_", ".", "!", "~", "*", "'", "(", 56 | ")", ":", "@", "&", "=", "+", "$", ",", 57 | } 58 | 59 | local function protect_segment(s) 60 | return string.gsub(s, "([^A-Za-z0-9_])", function (c) 61 | if segment_set[c] then return c 62 | else return string.format("%%%02x", string.byte(c)) end 63 | end) 64 | end 65 | 66 | ----------------------------------------------------------------------------- 67 | -- Encodes a string into its escaped hexadecimal representation 68 | -- Input 69 | -- s: binary string to be encoded 70 | -- Returns 71 | -- escaped representation of string binary 72 | ----------------------------------------------------------------------------- 73 | function _M.unescape(s) 74 | return (string.gsub(s, "%%(%x%x)", function(hex) 75 | return string.char(base.tonumber(hex, 16)) 76 | end)) 77 | end 78 | 79 | ----------------------------------------------------------------------------- 80 | -- Builds a path from a base path and a relative path 81 | -- Input 82 | -- base_path 83 | -- relative_path 84 | -- Returns 85 | -- corresponding absolute path 86 | ----------------------------------------------------------------------------- 87 | local function absolute_path(base_path, relative_path) 88 | if string.sub(relative_path, 1, 1) == "/" then return relative_path end 89 | local path = string.gsub(base_path, "[^/]*$", "") 90 | path = path .. relative_path 91 | path = string.gsub(path, "([^/]*%./)", function (s) 92 | if s ~= "./" then return s else return "" end 93 | end) 94 | path = string.gsub(path, "/%.$", "/") 95 | local reduced 96 | while reduced ~= path do 97 | reduced = path 98 | path = string.gsub(reduced, "([^/]*/%.%./)", function (s) 99 | if s ~= "../../" then return "" else return s end 100 | end) 101 | end 102 | path = string.gsub(reduced, "([^/]*/%.%.)$", function (s) 103 | if s ~= "../.." then return "" else return s end 104 | end) 105 | return path 106 | end 107 | 108 | ----------------------------------------------------------------------------- 109 | -- Parses a url and returns a table with all its parts according to RFC 2396 110 | -- The following grammar describes the names given to the URL parts 111 | -- ::= :///;?# 112 | -- ::= @: 113 | -- ::= [:] 114 | -- :: = {/} 115 | -- Input 116 | -- url: uniform resource locator of request 117 | -- default: table with default values for each field 118 | -- Returns 119 | -- table with the following fields, where RFC naming conventions have 120 | -- been preserved: 121 | -- scheme, authority, userinfo, user, password, host, port, 122 | -- path, params, query, fragment 123 | -- Obs: 124 | -- the leading '/' in {/} is considered part of 125 | ----------------------------------------------------------------------------- 126 | function _M.parse(url, default) 127 | -- initialize default parameters 128 | local parsed = {} 129 | for i,v in base.pairs(default or parsed) do parsed[i] = v end 130 | -- empty url is parsed to nil 131 | if not url or url == "" then return nil, "invalid url" end 132 | -- remove whitespace 133 | -- url = string.gsub(url, "%s", "") 134 | -- get fragment 135 | url = string.gsub(url, "#(.*)$", function(f) 136 | parsed.fragment = f 137 | return "" 138 | end) 139 | -- get scheme 140 | url = string.gsub(url, "^([%w][%w%+%-%.]*)%:", 141 | function(s) parsed.scheme = s; return "" end) 142 | -- get authority 143 | url = string.gsub(url, "^//([^/]*)", function(n) 144 | parsed.authority = n 145 | return "" 146 | end) 147 | -- get query string 148 | url = string.gsub(url, "%?(.*)", function(q) 149 | parsed.query = q 150 | return "" 151 | end) 152 | -- get params 153 | url = string.gsub(url, "%;(.*)", function(p) 154 | parsed.params = p 155 | return "" 156 | end) 157 | -- path is whatever was left 158 | if url ~= "" then parsed.path = url end 159 | local authority = parsed.authority 160 | if not authority then return parsed end 161 | authority = string.gsub(authority,"^([^@]*)@", 162 | function(u) parsed.userinfo = u; return "" end) 163 | authority = string.gsub(authority, ":([^:%]]*)$", 164 | function(p) parsed.port = p; return "" end) 165 | if authority ~= "" then 166 | -- IPv6? 167 | parsed.host = string.match(authority, "^%[(.+)%]$") or authority 168 | end 169 | local userinfo = parsed.userinfo 170 | if not userinfo then return parsed end 171 | userinfo = string.gsub(userinfo, ":([^:]*)$", 172 | function(p) parsed.password = p; return "" end) 173 | parsed.user = userinfo 174 | return parsed 175 | end 176 | 177 | ----------------------------------------------------------------------------- 178 | -- Rebuilds a parsed URL from its components. 179 | -- Components are protected if any reserved or unallowed characters are found 180 | -- Input 181 | -- parsed: parsed URL, as returned by parse 182 | -- Returns 183 | -- a stringing with the corresponding URL 184 | ----------------------------------------------------------------------------- 185 | function _M.build(parsed) 186 | local ppath = _M.parse_path(parsed.path or "") 187 | local url = _M.build_path(ppath) 188 | if parsed.params then url = url .. ";" .. parsed.params end 189 | if parsed.query then url = url .. "?" .. parsed.query end 190 | local authority = parsed.authority 191 | if parsed.host then 192 | authority = parsed.host 193 | if string.find(authority, ":") then -- IPv6? 194 | authority = "[" .. authority .. "]" 195 | end 196 | if parsed.port then authority = authority .. ":" .. parsed.port end 197 | local userinfo = parsed.userinfo 198 | if parsed.user then 199 | userinfo = parsed.user 200 | if parsed.password then 201 | userinfo = userinfo .. ":" .. parsed.password 202 | end 203 | end 204 | if userinfo then authority = userinfo .. "@" .. authority end 205 | end 206 | if authority then url = "//" .. authority .. url end 207 | if parsed.scheme then url = parsed.scheme .. ":" .. url end 208 | if parsed.fragment then url = url .. "#" .. parsed.fragment end 209 | -- url = string.gsub(url, "%s", "") 210 | return url 211 | end 212 | 213 | ----------------------------------------------------------------------------- 214 | -- Builds a absolute URL from a base and a relative URL according to RFC 2396 215 | -- Input 216 | -- base_url 217 | -- relative_url 218 | -- Returns 219 | -- corresponding absolute url 220 | ----------------------------------------------------------------------------- 221 | function _M.absolute(base_url, relative_url) 222 | if base.type(base_url) == "table" then 223 | base_parsed = base_url 224 | base_url = _M.build(base_parsed) 225 | else 226 | base_parsed = _M.parse(base_url) 227 | end 228 | local relative_parsed = _M.parse(relative_url) 229 | if not base_parsed then return relative_url 230 | elseif not relative_parsed then return base_url 231 | elseif relative_parsed.scheme then return relative_url 232 | else 233 | relative_parsed.scheme = base_parsed.scheme 234 | if not relative_parsed.authority then 235 | relative_parsed.authority = base_parsed.authority 236 | if not relative_parsed.path then 237 | relative_parsed.path = base_parsed.path 238 | if not relative_parsed.params then 239 | relative_parsed.params = base_parsed.params 240 | if not relative_parsed.query then 241 | relative_parsed.query = base_parsed.query 242 | end 243 | end 244 | else 245 | relative_parsed.path = absolute_path(base_parsed.path or "", 246 | relative_parsed.path) 247 | end 248 | end 249 | return _M.build(relative_parsed) 250 | end 251 | end 252 | 253 | ----------------------------------------------------------------------------- 254 | -- Breaks a path into its segments, unescaping the segments 255 | -- Input 256 | -- path 257 | -- Returns 258 | -- segment: a table with one entry per segment 259 | ----------------------------------------------------------------------------- 260 | function _M.parse_path(path) 261 | local parsed = {} 262 | path = path or "" 263 | --path = string.gsub(path, "%s", "") 264 | string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) 265 | for i = 1, #parsed do 266 | parsed[i] = _M.unescape(parsed[i]) 267 | end 268 | if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end 269 | if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end 270 | return parsed 271 | end 272 | 273 | ----------------------------------------------------------------------------- 274 | -- Builds a path component from its segments, escaping protected characters. 275 | -- Input 276 | -- parsed: path segments 277 | -- unsafe: if true, segments are not protected before path is built 278 | -- Returns 279 | -- path: corresponding path stringing 280 | ----------------------------------------------------------------------------- 281 | function _M.build_path(parsed, unsafe) 282 | local path = "" 283 | local n = #parsed 284 | if unsafe then 285 | for i = 1, n-1 do 286 | path = path .. parsed[i] 287 | path = path .. "/" 288 | end 289 | if n > 0 then 290 | path = path .. parsed[n] 291 | if parsed.is_directory then path = path .. "/" end 292 | end 293 | else 294 | for i = 1, n-1 do 295 | path = path .. protect_segment(parsed[i]) 296 | path = path .. "/" 297 | end 298 | if n > 0 then 299 | path = path .. protect_segment(parsed[n]) 300 | if parsed.is_directory then path = path .. "/" end 301 | end 302 | end 303 | if parsed.is_absolute then path = "/" .. path end 304 | return path 305 | end 306 | 307 | return _M 308 | -------------------------------------------------------------------------------- /external/luasocket/options.c: -------------------------------------------------------------------------------- 1 | /*=========================================================================*\ 2 | * Common option interface 3 | * LuaSocket toolkit 4 | \*=========================================================================*/ 5 | #include 6 | 7 | #include "lauxlib.h" 8 | 9 | #include "auxiliar.h" 10 | #include "options.h" 11 | #include "inet.h" 12 | 13 | 14 | /*=========================================================================*\ 15 | * Internal functions prototypes 16 | \*=========================================================================*/ 17 | static int opt_setmembership(lua_State *L, p_socket ps, int level, int name); 18 | static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name); 19 | static int opt_setboolean(lua_State *L, p_socket ps, int level, int name); 20 | static int opt_getboolean(lua_State *L, p_socket ps, int level, int name); 21 | static int opt_setint(lua_State *L, p_socket ps, int level, int name); 22 | static int opt_getint(lua_State *L, p_socket ps, int level, int name); 23 | static int opt_set(lua_State *L, p_socket ps, int level, int name, 24 | void *val, int len); 25 | static int opt_get(lua_State *L, p_socket ps, int level, int name, 26 | void *val, int* len); 27 | 28 | /*=========================================================================*\ 29 | * Exported functions 30 | \*=========================================================================*/ 31 | /*-------------------------------------------------------------------------*\ 32 | * Calls appropriate option handler 33 | \*-------------------------------------------------------------------------*/ 34 | int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps) 35 | { 36 | const char *name = luaL_checkstring(L, 2); /* obj, name, ... */ 37 | while (opt->name && strcmp(name, opt->name)) 38 | opt++; 39 | if (!opt->func) { 40 | char msg[45]; 41 | sprintf(msg, "unsupported option `%.35s'", name); 42 | luaL_argerror(L, 2, msg); 43 | } 44 | return opt->func(L, ps); 45 | } 46 | 47 | int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps) 48 | { 49 | const char *name = luaL_checkstring(L, 2); /* obj, name, ... */ 50 | while (opt->name && strcmp(name, opt->name)) 51 | opt++; 52 | if (!opt->func) { 53 | char msg[45]; 54 | sprintf(msg, "unsupported option `%.35s'", name); 55 | luaL_argerror(L, 2, msg); 56 | } 57 | return opt->func(L, ps); 58 | } 59 | 60 | /* enables reuse of local address */ 61 | int opt_set_reuseaddr(lua_State *L, p_socket ps) 62 | { 63 | return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); 64 | } 65 | 66 | int opt_get_reuseaddr(lua_State *L, p_socket ps) 67 | { 68 | return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); 69 | } 70 | 71 | /* enables reuse of local port */ 72 | int opt_set_reuseport(lua_State *L, p_socket ps) 73 | { 74 | return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT); 75 | } 76 | 77 | int opt_get_reuseport(lua_State *L, p_socket ps) 78 | { 79 | return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEPORT); 80 | } 81 | 82 | /* disables the Naggle algorithm */ 83 | int opt_set_tcp_nodelay(lua_State *L, p_socket ps) 84 | { 85 | return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); 86 | } 87 | 88 | int opt_get_tcp_nodelay(lua_State *L, p_socket ps) 89 | { 90 | return opt_getboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); 91 | } 92 | 93 | int opt_set_keepalive(lua_State *L, p_socket ps) 94 | { 95 | return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); 96 | } 97 | 98 | int opt_get_keepalive(lua_State *L, p_socket ps) 99 | { 100 | return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); 101 | } 102 | 103 | int opt_set_dontroute(lua_State *L, p_socket ps) 104 | { 105 | return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE); 106 | } 107 | 108 | int opt_set_broadcast(lua_State *L, p_socket ps) 109 | { 110 | return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST); 111 | } 112 | 113 | int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps) 114 | { 115 | return opt_setint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS); 116 | } 117 | 118 | int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps) 119 | { 120 | return opt_getint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS); 121 | } 122 | 123 | int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps) 124 | { 125 | return opt_setint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS); 126 | } 127 | 128 | int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps) 129 | { 130 | return opt_getint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS); 131 | } 132 | 133 | int opt_set_ip_multicast_loop(lua_State *L, p_socket ps) 134 | { 135 | return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); 136 | } 137 | 138 | int opt_get_ip_multicast_loop(lua_State *L, p_socket ps) 139 | { 140 | return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); 141 | } 142 | 143 | int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps) 144 | { 145 | return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP); 146 | } 147 | 148 | int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps) 149 | { 150 | return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP); 151 | } 152 | 153 | int opt_set_linger(lua_State *L, p_socket ps) 154 | { 155 | struct linger li; /* obj, name, table */ 156 | if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE)); 157 | lua_pushstring(L, "on"); 158 | lua_gettable(L, 3); 159 | if (!lua_isboolean(L, -1)) 160 | luaL_argerror(L, 3, "boolean 'on' field expected"); 161 | li.l_onoff = (u_short) lua_toboolean(L, -1); 162 | lua_pushstring(L, "timeout"); 163 | lua_gettable(L, 3); 164 | if (!lua_isnumber(L, -1)) 165 | luaL_argerror(L, 3, "number 'timeout' field expected"); 166 | li.l_linger = (u_short) lua_tonumber(L, -1); 167 | return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li)); 168 | } 169 | 170 | int opt_get_linger(lua_State *L, p_socket ps) 171 | { 172 | struct linger li; /* obj, name */ 173 | int len = sizeof(li); 174 | int err = opt_get(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, &len); 175 | if (err) 176 | return err; 177 | lua_newtable(L); 178 | lua_pushboolean(L, li.l_onoff); 179 | lua_setfield(L, -2, "on"); 180 | lua_pushinteger(L, li.l_linger); 181 | lua_setfield(L, -2, "timeout"); 182 | return 1; 183 | } 184 | 185 | int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps) 186 | { 187 | return opt_setint(L, ps, IPPROTO_IP, IP_MULTICAST_TTL); 188 | } 189 | 190 | int opt_set_ip_multicast_if(lua_State *L, p_socket ps) 191 | { 192 | const char *address = luaL_checkstring(L, 3); /* obj, name, ip */ 193 | struct in_addr val; 194 | val.s_addr = htonl(INADDR_ANY); 195 | if (strcmp(address, "*") && !inet_aton(address, &val)) 196 | luaL_argerror(L, 3, "ip expected"); 197 | return opt_set(L, ps, IPPROTO_IP, IP_MULTICAST_IF, 198 | (char *) &val, sizeof(val)); 199 | } 200 | 201 | int opt_get_ip_multicast_if(lua_State *L, p_socket ps) 202 | { 203 | struct in_addr val; 204 | socklen_t len = sizeof(val); 205 | if (getsockopt(*ps, IPPROTO_IP, IP_MULTICAST_IF, (char *) &val, &len) < 0) { 206 | lua_pushnil(L); 207 | lua_pushstring(L, "getsockopt failed"); 208 | return 2; 209 | } 210 | lua_pushstring(L, inet_ntoa(val)); 211 | return 1; 212 | } 213 | 214 | int opt_set_ip_add_membership(lua_State *L, p_socket ps) 215 | { 216 | return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP); 217 | } 218 | 219 | int opt_set_ip_drop_membersip(lua_State *L, p_socket ps) 220 | { 221 | return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP); 222 | } 223 | 224 | int opt_set_ip6_add_membership(lua_State *L, p_socket ps) 225 | { 226 | return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP); 227 | } 228 | 229 | int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps) 230 | { 231 | return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP); 232 | } 233 | 234 | int opt_get_ip6_v6only(lua_State *L, p_socket ps) 235 | { 236 | return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY); 237 | } 238 | 239 | int opt_set_ip6_v6only(lua_State *L, p_socket ps) 240 | { 241 | return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY); 242 | } 243 | 244 | /*=========================================================================*\ 245 | * Auxiliar functions 246 | \*=========================================================================*/ 247 | static int opt_setmembership(lua_State *L, p_socket ps, int level, int name) 248 | { 249 | struct ip_mreq val; /* obj, name, table */ 250 | if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE)); 251 | lua_pushstring(L, "multiaddr"); 252 | lua_gettable(L, 3); 253 | if (!lua_isstring(L, -1)) 254 | luaL_argerror(L, 3, "string 'multiaddr' field expected"); 255 | if (!inet_aton(lua_tostring(L, -1), &val.imr_multiaddr)) 256 | luaL_argerror(L, 3, "invalid 'multiaddr' ip address"); 257 | lua_pushstring(L, "interface"); 258 | lua_gettable(L, 3); 259 | if (!lua_isstring(L, -1)) 260 | luaL_argerror(L, 3, "string 'interface' field expected"); 261 | val.imr_interface.s_addr = htonl(INADDR_ANY); 262 | if (strcmp(lua_tostring(L, -1), "*") && 263 | !inet_aton(lua_tostring(L, -1), &val.imr_interface)) 264 | luaL_argerror(L, 3, "invalid 'interface' ip address"); 265 | return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); 266 | } 267 | 268 | static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name) 269 | { 270 | struct ipv6_mreq val; /* obj, opt-name, table */ 271 | memset(&val, 0, sizeof(val)); 272 | if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE)); 273 | lua_pushstring(L, "multiaddr"); 274 | lua_gettable(L, 3); 275 | if (!lua_isstring(L, -1)) 276 | luaL_argerror(L, 3, "string 'multiaddr' field expected"); 277 | if (!inet_pton(AF_INET6, lua_tostring(L, -1), &val.ipv6mr_multiaddr)) 278 | luaL_argerror(L, 3, "invalid 'multiaddr' ip address"); 279 | lua_pushstring(L, "interface"); 280 | lua_gettable(L, 3); 281 | /* By default we listen to interface on default route 282 | * (sigh). However, interface= can override it. We should 283 | * support either number, or name for it. Waiting for 284 | * windows port of if_nametoindex */ 285 | if (!lua_isnil(L, -1)) { 286 | if (lua_isnumber(L, -1)) { 287 | val.ipv6mr_interface = (unsigned int) lua_tonumber(L, -1); 288 | } else 289 | luaL_argerror(L, -1, "number 'interface' field expected"); 290 | } 291 | return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); 292 | } 293 | 294 | static 295 | int opt_get(lua_State *L, p_socket ps, int level, int name, void *val, int* len) 296 | { 297 | socklen_t socklen = *len; 298 | if (getsockopt(*ps, level, name, (char *) val, &socklen) < 0) { 299 | lua_pushnil(L); 300 | lua_pushstring(L, "getsockopt failed"); 301 | return 2; 302 | } 303 | *len = socklen; 304 | return 0; 305 | } 306 | 307 | static 308 | int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len) 309 | { 310 | if (setsockopt(*ps, level, name, (char *) val, len) < 0) { 311 | lua_pushnil(L); 312 | lua_pushstring(L, "setsockopt failed"); 313 | return 2; 314 | } 315 | lua_pushnumber(L, 1); 316 | return 1; 317 | } 318 | 319 | static int opt_getboolean(lua_State *L, p_socket ps, int level, int name) 320 | { 321 | int val = 0; 322 | int len = sizeof(val); 323 | int err = opt_get(L, ps, level, name, (char *) &val, &len); 324 | if (err) 325 | return err; 326 | lua_pushboolean(L, val); 327 | return 1; 328 | } 329 | 330 | int opt_get_error(lua_State *L, p_socket ps) 331 | { 332 | int val = 0; 333 | socklen_t len = sizeof(val); 334 | if (getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *) &val, &len) < 0) { 335 | lua_pushnil(L); 336 | lua_pushstring(L, "getsockopt failed"); 337 | return 2; 338 | } 339 | lua_pushstring(L, socket_strerror(val)); 340 | return 1; 341 | } 342 | 343 | static int opt_setboolean(lua_State *L, p_socket ps, int level, int name) 344 | { 345 | int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */ 346 | return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); 347 | } 348 | 349 | static int opt_getint(lua_State *L, p_socket ps, int level, int name) 350 | { 351 | int val = 0; 352 | int len = sizeof(val); 353 | int err = opt_get(L, ps, level, name, (char *) &val, &len); 354 | if (err) 355 | return err; 356 | lua_pushnumber(L, val); 357 | return 1; 358 | } 359 | 360 | static int opt_setint(lua_State *L, p_socket ps, int level, int name) 361 | { 362 | int val = (int) lua_tonumber(L, 3); /* obj, name, int */ 363 | return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); 364 | } 365 | --------------------------------------------------------------------------------