From ec3d2f2942172d79fe74a01f5c980873e01ff5dd Mon Sep 17 00:00:00 2001 From: Dmitry Kozlov Date: Fri, 13 Oct 2017 23:59:50 +0300 Subject: improved lua support Implemented support for lua 5.2/5.3. To build accel-ppp with exact lua version pass it in -DLUA=x.y, for example -DLUA=5.2 (cmake 3.0 is required for this). Old style -DLUA=TRUE supports only 5.1 and does not require cmake 3.0. Also extra lua modules (lua_lpack, lua_bit) took out into separated library luasupp. --- accel-pppd/CMakeLists.txt | 11 ++ accel-pppd/ctrl/ipoe/CMakeLists.txt | 11 +- accel-pppd/ctrl/ipoe/lua.c | 25 ++-- accel-pppd/ctrl/ipoe/lua_bit.c | 189 ------------------------- accel-pppd/ctrl/ipoe/lua_lpack.c | 271 ------------------------------------ accel-pppd/include/luasupp.h | 1 + accel-pppd/lua/CMakeLists.txt | 12 ++ accel-pppd/lua/lua_bit.c | 191 +++++++++++++++++++++++++ accel-pppd/lua/lua_lpack.c | 271 ++++++++++++++++++++++++++++++++++++ accel-pppd/lua/luasupp.h | 9 ++ 10 files changed, 514 insertions(+), 477 deletions(-) delete mode 100644 accel-pppd/ctrl/ipoe/lua_bit.c delete mode 100644 accel-pppd/ctrl/ipoe/lua_lpack.c create mode 120000 accel-pppd/include/luasupp.h create mode 100644 accel-pppd/lua/CMakeLists.txt create mode 100644 accel-pppd/lua/lua_bit.c create mode 100644 accel-pppd/lua/lua_lpack.c create mode 100644 accel-pppd/lua/luasupp.h (limited to 'accel-pppd') diff --git a/accel-pppd/CMakeLists.txt b/accel-pppd/CMakeLists.txt index 8ffcbf4..082c93b 100644 --- a/accel-pppd/CMakeLists.txt +++ b/accel-pppd/CMakeLists.txt @@ -30,6 +30,17 @@ IF (RADIUS) ADD_SUBDIRECTORY(radius) ENDIF (RADIUS) +IF (LUA) + IF (LUA STREQUAL "TRUE") + include(FindLua51) + ELSE () + find_package("Lua" ${LUA}) + ENDIF () + IF (NOT DEFINED LUA_VERSION_STRING) + MESSAGE(FATAL_ERROR "lua not found") + ENDIF () + ADD_SUBDIRECTORY(lua) +ENDIF (LUA) ADD_SUBDIRECTORY(triton) ADD_SUBDIRECTORY(vlan-mon) diff --git a/accel-pppd/ctrl/ipoe/CMakeLists.txt b/accel-pppd/ctrl/ipoe/CMakeLists.txt index bba7463..8ab5975 100644 --- a/accel-pppd/ctrl/ipoe/CMakeLists.txt +++ b/accel-pppd/ctrl/ipoe/CMakeLists.txt @@ -10,20 +10,19 @@ SET(sources ) IF (LUA) - include(FindLua51) - IF (NOT LUA51_FOUND) - MESSAGE(FATAL_ERROR "lua not found") - ENDIF (NOT LUA51_FOUND) INCLUDE_DIRECTORIES(${LUA_INCLUDE_DIR}) ADD_DEFINITIONS(-DUSE_LUA) - SET(sources ${sources} lua.c lua_lpack.c lua_bit.c) + SET(sources ${sources} lua.c) ENDIF (LUA) ADD_LIBRARY(ipoe SHARED ${sources}) + IF (LUA) - TARGET_LINK_LIBRARIES(ipoe ${LUA_LIBRARIES}) + TARGET_LINK_LIBRARIES(ipoe luasupp) ENDIF(LUA) + TARGET_LINK_LIBRARIES(ipoe vlan-mon) + set_property(TARGET ipoe PROPERTY CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) set_property(TARGET ipoe PROPERTY INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/accel-ppp) diff --git a/accel-pppd/ctrl/ipoe/lua.c b/accel-pppd/ctrl/ipoe/lua.c index d5f9e95..88dbeef 100644 --- a/accel-pppd/ctrl/ipoe/lua.c +++ b/accel-pppd/ctrl/ipoe/lua.c @@ -11,6 +11,7 @@ #include "events.h" #include "log.h" #include "utils.h" +#include "luasupp.h" #include "ipoe.h" @@ -34,10 +35,8 @@ static int packet4_agent_circuit_id(lua_State *L); static int packet4_agent_remote_id(lua_State *L); static int packet4_vlan(lua_State *L); -int luaopen_lpack(lua_State *L); -int luaopen_bit(lua_State *L); -static const struct luaL_reg packet4_lib [] = { +static const struct luaL_Reg packet4_lib [] = { {"hdr", packet4_hdr}, {"ifname", packet4_ifname}, {"option", packet4_option}, @@ -52,14 +51,14 @@ static int luaopen_packet4(lua_State *L) { luaL_newmetatable(L, IPOE_PACKET4); - lua_pushstring(L, "__index"); - lua_pushvalue(L, -2); /* pushes the metatable */ - lua_settable(L, -3); /* metatable.__index = metatable */ + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); - - luaI_openlib(L, NULL, packet4_lib, 0); - - luaI_openlib(L, "packet4", packet4_lib, 0); +#if LUA_VERSION_NUM < 502 + luaL_register(L, NULL, packet4_lib); +#else + luaL_setfuncs(L, packet4_lib, 0); +#endif return 1; } @@ -189,12 +188,14 @@ static void init_lua() { __serial = serial; - L = lua_open(); + L = luaL_newstate(); luaL_openlibs(L); luaopen_lpack(L); +#if LUA_VERSION_NUM < 503 luaopen_bit(L); +#endif luaopen_packet4(L); if (luaL_loadfile(L, conf_filename)) @@ -264,6 +265,8 @@ char *ipoe_lua_get_username(struct ipoe_session *ses, const char *func) lua_getglobal(L, func); lua_pushlightuserdata(L, ses); + luaL_getmetatable(L, IPOE_PACKET4); + lua_setmetatable(L, -2); if (lua_pcall(L, 1, 1, 0)) { log_ppp_error("ipoe: lua: %s\n", lua_tostring(L, -1)); diff --git a/accel-pppd/ctrl/ipoe/lua_bit.c b/accel-pppd/ctrl/ipoe/lua_bit.c deleted file mode 100644 index 690df7d..0000000 --- a/accel-pppd/ctrl/ipoe/lua_bit.c +++ /dev/null @@ -1,189 +0,0 @@ -/* -** Lua BitOp -- a bit operations library for Lua 5.1/5.2. -** http://bitop.luajit.org/ -** -** Copyright (C) 2008-2012 Mike Pall. All rights reserved. -** -** Permission is hereby granted, free of charge, to any person obtaining -** a copy of this software and associated documentation files (the -** "Software"), to deal in the Software without restriction, including -** without limitation the rights to use, copy, modify, merge, publish, -** distribute, sublicense, and/or sell copies of the Software, and to -** permit persons to whom the Software is furnished to do so, subject to -** the following conditions: -** -** The above copyright notice and this permission notice shall be -** included in all copies or substantial portions of the Software. -** -** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -** -** [ MIT license: http://www.opensource.org/licenses/mit-license.php ] -*/ - -#define LUA_BITOP_VERSION "1.0.2" - -#define LUA_LIB -#include "lua.h" -#include "lauxlib.h" - -#ifdef _MSC_VER -/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */ -typedef __int32 int32_t; -typedef unsigned __int32 uint32_t; -typedef unsigned __int64 uint64_t; -#else -#include -#endif - -typedef int32_t SBits; -typedef uint32_t UBits; - -typedef union { - lua_Number n; -#ifdef LUA_NUMBER_DOUBLE - uint64_t b; -#else - UBits b; -#endif -} BitNum; - -/* Convert argument to bit type. */ -static UBits barg(lua_State *L, int idx) -{ - BitNum bn; - UBits b; -#if LUA_VERSION_NUM < 502 - bn.n = lua_tonumber(L, idx); -#else - bn.n = luaL_checknumber(L, idx); -#endif -#if defined(LUA_NUMBER_DOUBLE) - bn.n += 6755399441055744.0; /* 2^52+2^51 */ -#ifdef SWAPPED_DOUBLE - b = (UBits)(bn.b >> 32); -#else - b = (UBits)bn.b; -#endif -#elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \ - defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \ - defined(LUA_NUMBER_LLONG) - if (sizeof(UBits) == sizeof(lua_Number)) - b = bn.b; - else - b = (UBits)(SBits)bn.n; -#elif defined(LUA_NUMBER_FLOAT) -#error "A 'float' lua_Number type is incompatible with this library" -#else -#error "Unknown number type, check LUA_NUMBER_* in luaconf.h" -#endif -#if LUA_VERSION_NUM < 502 - if (b == 0 && !lua_isnumber(L, idx)) { - luaL_typerror(L, idx, "number"); - } -#endif - return b; -} - -/* Return bit type. */ -#define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1; - -static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) } -static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) } - -#define BIT_OP(func, opr) \ - static int func(lua_State *L) { int i; UBits b = barg(L, 1); \ - for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) } -BIT_OP(bit_band, &=) -BIT_OP(bit_bor, |=) -BIT_OP(bit_bxor, ^=) - -#define bshl(b, n) (b << n) -#define bshr(b, n) (b >> n) -#define bsar(b, n) ((SBits)b >> n) -#define brol(b, n) ((b << n) | (b >> (32-n))) -#define bror(b, n) ((b << (32-n)) | (b >> n)) -#define BIT_SH(func, fn) \ - static int func(lua_State *L) { \ - UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) } -BIT_SH(bit_lshift, bshl) -BIT_SH(bit_rshift, bshr) -BIT_SH(bit_arshift, bsar) -BIT_SH(bit_rol, brol) -BIT_SH(bit_ror, bror) - -static int bit_bswap(lua_State *L) -{ - UBits b = barg(L, 1); - b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24); - BRET(b) -} - -static int bit_tohex(lua_State *L) -{ - UBits b = barg(L, 1); - SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2); - const char *hexdigits = "0123456789abcdef"; - char buf[8]; - int i; - if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; } - if (n > 8) n = 8; - for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; } - lua_pushlstring(L, buf, (size_t)n); - return 1; -} - -static const struct luaL_Reg bit_funcs[] = { - { "tobit", bit_tobit }, - { "bnot", bit_bnot }, - { "band", bit_band }, - { "bor", bit_bor }, - { "bxor", bit_bxor }, - { "lshift", bit_lshift }, - { "rshift", bit_rshift }, - { "arshift", bit_arshift }, - { "rol", bit_rol }, - { "ror", bit_ror }, - { "bswap", bit_bswap }, - { "tohex", bit_tohex }, - { NULL, NULL } -}; - -/* Signed right-shifts are implementation-defined per C89/C99. -** But the de facto standard are arithmetic right-shifts on two's -** complement CPUs. This behaviour is required here, so test for it. -*/ -#define BAD_SAR (bsar(-8, 2) != (SBits)-2) - -LUALIB_API int luaopen_bit(lua_State *L) -{ - UBits b; - lua_pushnumber(L, (lua_Number)1437217655L); - b = barg(L, -1); - if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */ - const char *msg = "compiled with incompatible luaconf.h"; -#ifdef LUA_NUMBER_DOUBLE -#ifdef _WIN32 - if (b == (UBits)1610612736L) - msg = "use D3DCREATE_FPU_PRESERVE with DirectX"; -#endif - if (b == (UBits)1127743488L) - msg = "not compiled with SWAPPED_DOUBLE"; -#endif - if (BAD_SAR) - msg = "arithmetic right-shift broken"; - luaL_error(L, "bit library self-test failed (%s)", msg); - } -#if LUA_VERSION_NUM < 502 - luaL_register(L, "bit", bit_funcs); -#else - luaL_newlib(L, bit_funcs); -#endif - return 1; -} - diff --git a/accel-pppd/ctrl/ipoe/lua_lpack.c b/accel-pppd/ctrl/ipoe/lua_lpack.c deleted file mode 100644 index feb0a5a..0000000 --- a/accel-pppd/ctrl/ipoe/lua_lpack.c +++ /dev/null @@ -1,271 +0,0 @@ -/* -* lpack.c -* a Lua library for packing and unpacking binary data -* Luiz Henrique de Figueiredo -* 29 Jun 2007 19:27:20 -* This code is hereby placed in the public domain. -* with contributions from Ignacio Castaņo and -* Roberto Ierusalimschy . -*/ - -#define OP_ZSTRING 'z' /* zero-terminated string */ -#define OP_BSTRING 'p' /* string preceded by length byte */ -#define OP_WSTRING 'P' /* string preceded by length word */ -#define OP_SSTRING 'a' /* string preceded by length size_t */ -#define OP_STRING 'A' /* string */ -#define OP_FLOAT 'f' /* float */ -#define OP_DOUBLE 'd' /* double */ -#define OP_NUMBER 'n' /* Lua number */ -#define OP_CHAR 'c' /* char */ -#define OP_BYTE 'b' /* byte = unsigned char */ -#define OP_SHORT 'h' /* short */ -#define OP_USHORT 'H' /* unsigned short */ -#define OP_INT 'i' /* int */ -#define OP_UINT 'I' /* unsigned int */ -#define OP_LONG 'l' /* long */ -#define OP_ULONG 'L' /* unsigned long */ -#define OP_LITTLEENDIAN '<' /* little endian */ -#define OP_BIGENDIAN '>' /* big endian */ -#define OP_NATIVE '=' /* native endian */ - -#include -#include -#include - -#include "lua.h" -#include "lualib.h" -#include "lauxlib.h" - -static void badcode(lua_State *L, int c) -{ - char s[]="bad code `?'"; - s[sizeof(s)-3]=c; - luaL_argerror(L,1,s); -} - -static int doendian(int c) -{ - int x=1; - int e=*(char*)&x; - if (c==OP_LITTLEENDIAN) return !e; - if (c==OP_BIGENDIAN) return e; - if (c==OP_NATIVE) return 0; - return 0; -} - -static void doswap(int swap, void *p, size_t n) -{ - if (swap) - { - char *a=p; - int i,j; - for (i=0, j=n-1, n=n/2; n--; i++, j--) - { - char t=a[i]; a[i]=a[j]; a[j]=t; - } - } -} - -#define UNPACKNUMBER(OP,T) \ - case OP: \ - { \ - T a; \ - int m=sizeof(a); \ - if (i+m>len) goto done; \ - memcpy(&a,s+i,m); \ - i+=m; \ - doswap(swap,&a,m); \ - lua_pushinteger(L,(lua_Integer)a); \ - ++n; \ - break; \ - } - -#define UNPACKSTRING(OP,T) \ - case OP: \ - { \ - T l; \ - int m=sizeof(l); \ - if (i+m>len) goto done; \ - memcpy(&l,s+i,m); \ - doswap(swap,&l,m); \ - if (i+m+l>len) goto done; \ - i+=m; \ - lua_pushlstring(L,s+i,l); \ - i+=l; \ - ++n; \ - break; \ - } - -static int l_unpack(lua_State *L) /** unpack(s,f,[init]) */ -{ - size_t len; - const char *s=luaL_checklstring(L,1,&len); - const char *f=luaL_checkstring(L,2); - int i=luaL_optnumber(L,3,1)-1; - int n=0; - int swap=0; - lua_pushnil(L); - while (*f) - { - int c=*f++; - int N=1; - if (isdigit(*f)) - { - N=0; - while (isdigit(*f)) N=10*N+(*f++)-'0'; - if (N==0 && c==OP_STRING) { lua_pushliteral(L,""); ++n; } - } - while (N--) switch (c) - { - case OP_LITTLEENDIAN: - case OP_BIGENDIAN: - case OP_NATIVE: - { - swap=doendian(c); - N=0; - break; - } - case OP_STRING: - { - ++N; - if (i+N>len) goto done; - lua_pushlstring(L,s+i,N); - i+=N; - ++n; - N=0; - break; - } - case OP_ZSTRING: - { - size_t l; - if (i>=len) goto done; - l=strlen(s+i); - lua_pushlstring(L,s+i,l); - i+=l+1; - ++n; - break; - } - UNPACKSTRING(OP_BSTRING, unsigned char) - UNPACKSTRING(OP_WSTRING, unsigned short) - UNPACKSTRING(OP_SSTRING, size_t) - UNPACKNUMBER(OP_NUMBER, lua_Number) - UNPACKNUMBER(OP_DOUBLE, double) - UNPACKNUMBER(OP_FLOAT, float) - UNPACKNUMBER(OP_CHAR, int8_t) - UNPACKNUMBER(OP_BYTE, uint8_t) - UNPACKNUMBER(OP_SHORT, int16_t) - UNPACKNUMBER(OP_USHORT, uint16_t) - UNPACKNUMBER(OP_INT, int32_t) - UNPACKNUMBER(OP_UINT, uint32_t) - UNPACKNUMBER(OP_LONG, int64_t) - UNPACKNUMBER(OP_ULONG, uint64_t) - case ' ': case ',': - break; - default: - badcode(L,c); - break; - } - } -done: - lua_pushnumber(L,i+1); - lua_replace(L,-n-2); - return n+1; -} - -#define PACKNUMBER(OP,T) \ - case OP: \ - { \ - T a=(T)luaL_checknumber(L,i++); \ - doswap(swap,&a,sizeof(a)); \ - luaL_addlstring(&b,(void*)&a,sizeof(a)); \ - break; \ - } - -#define PACKSTRING(OP,T) \ - case OP: \ - { \ - size_t l; \ - const char *a=luaL_checklstring(L,i++,&l); \ - T ll=(T)l; \ - doswap(swap,&ll,sizeof(ll)); \ - luaL_addlstring(&b,(void*)&ll,sizeof(ll)); \ - luaL_addlstring(&b,a,l); \ - break; \ - } - -static int l_pack(lua_State *L) /** pack(f,...) */ -{ - int i=2; - const char *f=luaL_checkstring(L,1); - int swap=0; - luaL_Buffer b; - luaL_buffinit(L,&b); - while (*f) - { - int c=*f++; - int N=1; - if (isdigit(*f)) - { - N=0; - while (isdigit(*f)) N=10*N+(*f++)-'0'; - } - while (N--) switch (c) - { - case OP_LITTLEENDIAN: - case OP_BIGENDIAN: - case OP_NATIVE: - { - swap=doendian(c); - N=0; - break; - } - case OP_STRING: - case OP_ZSTRING: - { - size_t l; - const char *a=luaL_checklstring(L,i++,&l); - luaL_addlstring(&b,a,l+(c==OP_ZSTRING)); - break; - } - PACKSTRING(OP_BSTRING, unsigned char) - PACKSTRING(OP_WSTRING, unsigned short) - PACKSTRING(OP_SSTRING, size_t) - PACKNUMBER(OP_NUMBER, lua_Number) - PACKNUMBER(OP_DOUBLE, double) - PACKNUMBER(OP_FLOAT, float) - PACKNUMBER(OP_CHAR, int8_t) - PACKNUMBER(OP_BYTE, uint8_t) - PACKNUMBER(OP_SHORT, int16_t) - PACKNUMBER(OP_USHORT, uint16_t) - PACKNUMBER(OP_INT, int32_t) - PACKNUMBER(OP_UINT, uint32_t) - PACKNUMBER(OP_LONG, int64_t) - PACKNUMBER(OP_ULONG, uint64_t) - case ' ': case ',': - break; - default: - badcode(L,c); - break; - } - } - luaL_pushresult(&b); - return 1; -} - -static const luaL_reg R[] = -{ - {"pack", l_pack}, - {"unpack", l_unpack}, - {NULL, NULL} -}; - -int luaopen_lpack(lua_State *L) -{ -#ifdef USE_GLOBALS - lua_register(L,"bpack",l_pack); - lua_register(L,"bunpack",l_unpack); -#else - luaI_openlib(L, LUA_STRLIBNAME, R, 0); -#endif - return 0; -} diff --git a/accel-pppd/include/luasupp.h b/accel-pppd/include/luasupp.h new file mode 120000 index 0000000..2bdd30a --- /dev/null +++ b/accel-pppd/include/luasupp.h @@ -0,0 +1 @@ +../lua/luasupp.h \ No newline at end of file diff --git a/accel-pppd/lua/CMakeLists.txt b/accel-pppd/lua/CMakeLists.txt new file mode 100644 index 0000000..699cfb7 --- /dev/null +++ b/accel-pppd/lua/CMakeLists.txt @@ -0,0 +1,12 @@ +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) +INCLUDE_DIRECTORIES(${LUA_INCLUDE_DIR}) + +SET(sources + lua_lpack.c + lua_bit.c +) + +ADD_LIBRARY(luasupp SHARED ${sources}) +TARGET_LINK_LIBRARIES(luasupp ${LUA_LIBRARIES}) + +INSTALL(TARGETS luasupp LIBRARY DESTINATION lib${LIB_SUFFIX}/accel-ppp) diff --git a/accel-pppd/lua/lua_bit.c b/accel-pppd/lua/lua_bit.c new file mode 100644 index 0000000..be7e768 --- /dev/null +++ b/accel-pppd/lua/lua_bit.c @@ -0,0 +1,191 @@ +/* +** Lua BitOp -- a bit operations library for Lua 5.1/5.2. +** http://bitop.luajit.org/ +** +** Copyright (C) 2008-2012 Mike Pall. All rights reserved. +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +** [ MIT license: http://www.opensource.org/licenses/mit-license.php ] +*/ + +#define LUA_BITOP_VERSION "1.0.2" + +#define LUA_LIB +#include "lua.h" +#include "lauxlib.h" + +#if LUA_VERSION_NUM < 503 + +#ifdef _MSC_VER +/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */ +typedef __int32 int32_t; +typedef unsigned __int32 uint32_t; +typedef unsigned __int64 uint64_t; +#else +#include +#endif + +typedef int32_t SBits; +typedef uint32_t UBits; + +typedef union { + lua_Number n; +#ifdef LUA_NUMBER_DOUBLE + uint64_t b; +#else + UBits b; +#endif +} BitNum; + +/* Convert argument to bit type. */ +static UBits barg(lua_State *L, int idx) +{ + BitNum bn; + UBits b; +#if LUA_VERSION_NUM < 502 + bn.n = lua_tonumber(L, idx); +#else + bn.n = luaL_checknumber(L, idx); +#endif +#if defined(LUA_NUMBER_DOUBLE) + bn.n += 6755399441055744.0; /* 2^52+2^51 */ +#ifdef SWAPPED_DOUBLE + b = (UBits)(bn.b >> 32); +#else + b = (UBits)bn.b; +#endif +#elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \ + defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \ + defined(LUA_NUMBER_LLONG) + if (sizeof(UBits) == sizeof(lua_Number)) + b = bn.b; + else + b = (UBits)(SBits)bn.n; +#elif defined(LUA_NUMBER_FLOAT) +#error "A 'float' lua_Number type is incompatible with this library" +#else +#error "Unknown number type, check LUA_NUMBER_* in luaconf.h" +#endif +#if LUA_VERSION_NUM < 502 + if (b == 0 && !lua_isnumber(L, idx)) { + luaL_typerror(L, idx, "number"); + } +#endif + return b; +} + +/* Return bit type. */ +#define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1; + +static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) } +static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) } + +#define BIT_OP(func, opr) \ + static int func(lua_State *L) { int i; UBits b = barg(L, 1); \ + for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) } +BIT_OP(bit_band, &=) +BIT_OP(bit_bor, |=) +BIT_OP(bit_bxor, ^=) + +#define bshl(b, n) (b << n) +#define bshr(b, n) (b >> n) +#define bsar(b, n) ((SBits)b >> n) +#define brol(b, n) ((b << n) | (b >> (32-n))) +#define bror(b, n) ((b << (32-n)) | (b >> n)) +#define BIT_SH(func, fn) \ + static int func(lua_State *L) { \ + UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) } +BIT_SH(bit_lshift, bshl) +BIT_SH(bit_rshift, bshr) +BIT_SH(bit_arshift, bsar) +BIT_SH(bit_rol, brol) +BIT_SH(bit_ror, bror) + +static int bit_bswap(lua_State *L) +{ + UBits b = barg(L, 1); + b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24); + BRET(b) +} + +static int bit_tohex(lua_State *L) +{ + UBits b = barg(L, 1); + SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2); + const char *hexdigits = "0123456789abcdef"; + char buf[8]; + int i; + if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; } + if (n > 8) n = 8; + for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; } + lua_pushlstring(L, buf, (size_t)n); + return 1; +} + +static const struct luaL_Reg bit_funcs[] = { + { "tobit", bit_tobit }, + { "bnot", bit_bnot }, + { "band", bit_band }, + { "bor", bit_bor }, + { "bxor", bit_bxor }, + { "lshift", bit_lshift }, + { "rshift", bit_rshift }, + { "arshift", bit_arshift }, + { "rol", bit_rol }, + { "ror", bit_ror }, + { "bswap", bit_bswap }, + { "tohex", bit_tohex }, + { NULL, NULL } +}; + +/* Signed right-shifts are implementation-defined per C89/C99. +** But the de facto standard are arithmetic right-shifts on two's +** complement CPUs. This behaviour is required here, so test for it. +*/ +#define BAD_SAR (bsar(-8, 2) != (SBits)-2) + +LUALIB_API __attribute__((visibility("default"))) int luaopen_bit(lua_State *L) +{ + UBits b; + lua_pushnumber(L, (lua_Number)1437217655L); + b = barg(L, -1); + if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */ + const char *msg = "compiled with incompatible luaconf.h"; +#ifdef LUA_NUMBER_DOUBLE +#ifdef _WIN32 + if (b == (UBits)1610612736L) + msg = "use D3DCREATE_FPU_PRESERVE with DirectX"; +#endif + if (b == (UBits)1127743488L) + msg = "not compiled with SWAPPED_DOUBLE"; +#endif + if (BAD_SAR) + msg = "arithmetic right-shift broken"; + luaL_error(L, "bit library self-test failed (%s)", msg); + } +#if LUA_VERSION_NUM < 502 + luaL_register(L, "bit", bit_funcs); +#else + luaL_newlib(L, bit_funcs); +#endif + return 1; +} +#endif diff --git a/accel-pppd/lua/lua_lpack.c b/accel-pppd/lua/lua_lpack.c new file mode 100644 index 0000000..e9ceb85 --- /dev/null +++ b/accel-pppd/lua/lua_lpack.c @@ -0,0 +1,271 @@ +/* +* lpack.c +* a Lua library for packing and unpacking binary data +* Luiz Henrique de Figueiredo +* 29 Jun 2007 19:27:20 +* This code is hereby placed in the public domain. +* with contributions from Ignacio Castaņo and +* Roberto Ierusalimschy . +*/ + +#define OP_ZSTRING 'z' /* zero-terminated string */ +#define OP_BSTRING 'p' /* string preceded by length byte */ +#define OP_WSTRING 'P' /* string preceded by length word */ +#define OP_SSTRING 'a' /* string preceded by length size_t */ +#define OP_STRING 'A' /* string */ +#define OP_FLOAT 'f' /* float */ +#define OP_DOUBLE 'd' /* double */ +#define OP_NUMBER 'n' /* Lua number */ +#define OP_CHAR 'c' /* char */ +#define OP_BYTE 'b' /* byte = unsigned char */ +#define OP_SHORT 'h' /* short */ +#define OP_USHORT 'H' /* unsigned short */ +#define OP_INT 'i' /* int */ +#define OP_UINT 'I' /* unsigned int */ +#define OP_LONG 'l' /* long */ +#define OP_ULONG 'L' /* unsigned long */ +#define OP_LITTLEENDIAN '<' /* little endian */ +#define OP_BIGENDIAN '>' /* big endian */ +#define OP_NATIVE '=' /* native endian */ + +#include +#include +#include + +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" + +static void badcode(lua_State *L, int c) +{ + char s[]="bad code `?'"; + s[sizeof(s)-3]=c; + luaL_argerror(L,1,s); +} + +static int doendian(int c) +{ + int x=1; + int e=*(char*)&x; + if (c==OP_LITTLEENDIAN) return !e; + if (c==OP_BIGENDIAN) return e; + if (c==OP_NATIVE) return 0; + return 0; +} + +static void doswap(int swap, void *p, size_t n) +{ + if (swap) + { + char *a=p; + int i,j; + for (i=0, j=n-1, n=n/2; n--; i++, j--) + { + char t=a[i]; a[i]=a[j]; a[j]=t; + } + } +} + +#define UNPACKNUMBER(OP,T) \ + case OP: \ + { \ + T a; \ + int m=sizeof(a); \ + if (i+m>len) goto done; \ + memcpy(&a,s+i,m); \ + i+=m; \ + doswap(swap,&a,m); \ + lua_pushinteger(L,(lua_Integer)a); \ + ++n; \ + break; \ + } + +#define UNPACKSTRING(OP,T) \ + case OP: \ + { \ + T l; \ + int m=sizeof(l); \ + if (i+m>len) goto done; \ + memcpy(&l,s+i,m); \ + doswap(swap,&l,m); \ + if (i+m+l>len) goto done; \ + i+=m; \ + lua_pushlstring(L,s+i,l); \ + i+=l; \ + ++n; \ + break; \ + } + +static int l_unpack(lua_State *L) /** unpack(s,f,[init]) */ +{ + size_t len; + const char *s=luaL_checklstring(L,1,&len); + const char *f=luaL_checkstring(L,2); + int i=luaL_optnumber(L,3,1)-1; + int n=0; + int swap=0; + lua_pushnil(L); + while (*f) + { + int c=*f++; + int N=1; + if (isdigit(*f)) + { + N=0; + while (isdigit(*f)) N=10*N+(*f++)-'0'; + if (N==0 && c==OP_STRING) { lua_pushliteral(L,""); ++n; } + } + while (N--) switch (c) + { + case OP_LITTLEENDIAN: + case OP_BIGENDIAN: + case OP_NATIVE: + { + swap=doendian(c); + N=0; + break; + } + case OP_STRING: + { + ++N; + if (i+N>len) goto done; + lua_pushlstring(L,s+i,N); + i+=N; + ++n; + N=0; + break; + } + case OP_ZSTRING: + { + size_t l; + if (i>=len) goto done; + l=strlen(s+i); + lua_pushlstring(L,s+i,l); + i+=l+1; + ++n; + break; + } + UNPACKSTRING(OP_BSTRING, unsigned char) + UNPACKSTRING(OP_WSTRING, unsigned short) + UNPACKSTRING(OP_SSTRING, size_t) + UNPACKNUMBER(OP_NUMBER, lua_Number) + UNPACKNUMBER(OP_DOUBLE, double) + UNPACKNUMBER(OP_FLOAT, float) + UNPACKNUMBER(OP_CHAR, int8_t) + UNPACKNUMBER(OP_BYTE, uint8_t) + UNPACKNUMBER(OP_SHORT, int16_t) + UNPACKNUMBER(OP_USHORT, uint16_t) + UNPACKNUMBER(OP_INT, int32_t) + UNPACKNUMBER(OP_UINT, uint32_t) + UNPACKNUMBER(OP_LONG, int64_t) + UNPACKNUMBER(OP_ULONG, uint64_t) + case ' ': case ',': + break; + default: + badcode(L,c); + break; + } + } +done: + lua_pushnumber(L,i+1); + lua_replace(L,-n-2); + return n+1; +} + +#define PACKNUMBER(OP,T) \ + case OP: \ + { \ + T a=(T)luaL_checknumber(L,i++); \ + doswap(swap,&a,sizeof(a)); \ + luaL_addlstring(&b,(void*)&a,sizeof(a)); \ + break; \ + } + +#define PACKSTRING(OP,T) \ + case OP: \ + { \ + size_t l; \ + const char *a=luaL_checklstring(L,i++,&l); \ + T ll=(T)l; \ + doswap(swap,&ll,sizeof(ll)); \ + luaL_addlstring(&b,(void*)&ll,sizeof(ll)); \ + luaL_addlstring(&b,a,l); \ + break; \ + } + +static int l_pack(lua_State *L) /** pack(f,...) */ +{ + int i=2; + const char *f=luaL_checkstring(L,1); + int swap=0; + luaL_Buffer b; + luaL_buffinit(L,&b); + while (*f) + { + int c=*f++; + int N=1; + if (isdigit(*f)) + { + N=0; + while (isdigit(*f)) N=10*N+(*f++)-'0'; + } + while (N--) switch (c) + { + case OP_LITTLEENDIAN: + case OP_BIGENDIAN: + case OP_NATIVE: + { + swap=doendian(c); + N=0; + break; + } + case OP_STRING: + case OP_ZSTRING: + { + size_t l; + const char *a=luaL_checklstring(L,i++,&l); + luaL_addlstring(&b,a,l+(c==OP_ZSTRING)); + break; + } + PACKSTRING(OP_BSTRING, unsigned char) + PACKSTRING(OP_WSTRING, unsigned short) + PACKSTRING(OP_SSTRING, size_t) + PACKNUMBER(OP_NUMBER, lua_Number) + PACKNUMBER(OP_DOUBLE, double) + PACKNUMBER(OP_FLOAT, float) + PACKNUMBER(OP_CHAR, int8_t) + PACKNUMBER(OP_BYTE, uint8_t) + PACKNUMBER(OP_SHORT, int16_t) + PACKNUMBER(OP_USHORT, uint16_t) + PACKNUMBER(OP_INT, int32_t) + PACKNUMBER(OP_UINT, uint32_t) + PACKNUMBER(OP_LONG, int64_t) + PACKNUMBER(OP_ULONG, uint64_t) + case ' ': case ',': + break; + default: + badcode(L,c); + break; + } + } + luaL_pushresult(&b); + return 1; +} + +static const struct luaL_Reg R[] = +{ + {"pack", l_pack}, + {"unpack", l_unpack}, + {NULL, NULL} +}; + +LUALIB_API __attribute__((visibility("default"))) int luaopen_lpack(lua_State *L) +{ +#if LUA_VERSION_NUM < 502 + luaL_register(L, LUA_STRLIBNAME, R); +#else + luaL_newmetatable(L, LUA_STRLIBNAME); + luaL_newlib(L, R); +#endif + return 1; +} diff --git a/accel-pppd/lua/luasupp.h b/accel-pppd/lua/luasupp.h new file mode 100644 index 0000000..fa7a7c3 --- /dev/null +++ b/accel-pppd/lua/luasupp.h @@ -0,0 +1,9 @@ +#ifndef __LUASUPP_H +#define __LUASUPP_H + +#include "lua.h" + +int luaopen_lpack(lua_State *L); +int luaopen_bit(lua_State *L); + +#endif -- cgit v1.2.3