diff --git a/Makefile.in b/Makefile.in index 4384ca807..109556921 100644 --- a/Makefile.in +++ b/Makefile.in @@ -55,9 +55,9 @@ INSTALLNMAPFE=@INSTALLNMAPFE@ INSTALLNSE=@INSTALLNSE@ ifneq (@LIBLUA_LIBS@,) -NSE_SRC=nse_main.cc nse_auxiliar.cc nse_nsock.cc nse_init.cc nse_nmaplib.cc nse_debug.cc nse_pcrelib.cc nse_string.cc -NSE_HDRS=nse_main.h nse_auxiliar.h nse_nsock.h nse_init.h nse_nmaplib.h nse_debug.h nse_macros.h nse_pcrelib.h nse_string.h -NSE_OBJS=nse_main.o nse_auxiliar.o nse_nsock.o nse_init.o nse_nmaplib.o nse_debug.o nse_pcrelib.o nse_string.o +NSE_SRC=nse_main.cc nse_auxiliar.cc nse_nsock.cc nse_init.cc nse_nmaplib.cc nse_debug.cc nse_pcrelib.cc nse_string.cc nse_bitlib.cc +NSE_HDRS=nse_main.h nse_auxiliar.h nse_nsock.h nse_init.h nse_nmaplib.h nse_debug.h nse_macros.h nse_pcrelib.h nse_string.h nse_bitlib.h +NSE_OBJS=nse_main.o nse_auxiliar.o nse_nsock.o nse_init.o nse_nmaplib.o nse_debug.o nse_pcrelib.o nse_string.o nse_bitlib.o NSESTDLIB=nsestdlib endif diff --git a/nse_bitlib.cc b/nse_bitlib.cc new file mode 100644 index 000000000..96b793c54 --- /dev/null +++ b/nse_bitlib.cc @@ -0,0 +1,75 @@ +/* Bitwise operations library + * by Reuben Thomas (rrt@sc3d.org) + * bitlib is a C library for Lua 5.x that provides bitwise operations + * It is copyright Reuben Thomas 2000-2006, and is released under the + * MIT license, like Lua (see http://www.lua.org/copyright.html for the + * full license; it's basically the same as the BSD license). There is no + * warranty. + * the most recent copy can be found at http://rrt.sc3d.org/Software/Lua/ + **/ + +extern "C" { +#include "lauxlib.h" +#include "lua.h" +} + +#include "nse_bitlib.h" + +typedef long long Integer; +typedef unsigned long long UInteger; + +#define luaL_checkbit(L, n) ((Integer)luaL_checknumber(L, n)) +#define luaL_checkubit(L, n) ((UInteger)luaL_checkbit(L, n)) + +#define TDYADIC(name, op, checkbit1, checkbit2) \ + static int bit_ ## name(lua_State* L) { \ + lua_pushnumber(L, \ + checkbit1(L, 1) op checkbit2(L, 2)); \ + return 1; \ + } + +#define DYADIC(name, op) \ + TDYADIC(name, op, luaL_checkbit, luaL_checkbit) + +#define MONADIC(name, op) \ + static int bit_ ## name(lua_State* L) { \ + lua_pushnumber(L, op luaL_checkbit(L, 1)); \ + return 1; \ + } + +#define VARIADIC(name, op) \ + static int bit_ ## name(lua_State *L) { \ + int n = lua_gettop(L), i; \ + Integer w = luaL_checkbit(L, 1); \ + for (i = 2; i <= n; i++) \ + w op luaL_checkbit(L, i); \ + lua_pushnumber(L, w); \ + return 1; \ + } + +MONADIC(bnot, ~) +VARIADIC(band, &=) +VARIADIC(bor, |=) +VARIADIC(bxor, ^=) +TDYADIC(lshift, <<, luaL_checkbit, luaL_checkubit) +TDYADIC(rshift, >>, luaL_checkubit, luaL_checkubit) +TDYADIC(arshift, >>, luaL_checkbit, luaL_checkubit) +DYADIC(mod, %) + +static const struct luaL_reg bitlib[] = { + {"bnot", bit_bnot}, + {"band", bit_band}, + {"bor", bit_bor}, + {"bxor", bit_bxor}, + {"lshift", bit_lshift}, + {"rshift", bit_rshift}, + {"arshift", bit_arshift}, + {"mod", bit_mod}, + {NULL, NULL} +}; + +LUALIB_API int luaopen_bitlib (lua_State *L) { + luaL_openlib(L, NSE_BITLIBNAME, bitlib, 0); + return 1; +} + diff --git a/nse_bitlib.h b/nse_bitlib.h new file mode 100644 index 000000000..1cc338605 --- /dev/null +++ b/nse_bitlib.h @@ -0,0 +1,9 @@ +#ifndef NSE_BITLIB +#define NSE_BITLIB + +#define NSE_BITLIBNAME "bit" + +LUALIB_API int luaopen_bitlib (lua_State *L); + +#endif + diff --git a/nse_init.cc b/nse_init.cc index 181aa6ee3..0de2873c2 100644 --- a/nse_init.cc +++ b/nse_init.cc @@ -4,6 +4,7 @@ #include "nse_debug.h" // 3rd Party libs +#include "nse_bitlib.h" #include "nse_pcrelib.h" #include "nbase.h" @@ -44,6 +45,7 @@ int init_lua(lua_State* l) { {LUA_STRLIBNAME, luaopen_string}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_DBLIBNAME, luaopen_debug}, + {NSE_BITLIBNAME, luaopen_bitlib}, {NSE_PCRELIBNAME, luaopen_pcrelib}, {NULL, NULL} }; diff --git a/scripts/dns-test-open-recursion.nse b/scripts/dns-test-open-recursion.nse index 20f7176fa..5cbad2446 100644 --- a/scripts/dns-test-open-recursion.nse +++ b/scripts/dns-test-open-recursion.nse @@ -8,7 +8,7 @@ license = "See nmaps COPYING for licence" categories = {"intrusive"} -require "bit" +-- require "bit" portrule = function(host, port) if port.number == 53