mirror of
https://github.com/nmap/nmap.git
synced 2025-12-28 18:39:03 +00:00
Merge branch 'nse-lua53'
Lua 5.3 adds several awesome features of particular interest to nmap including bitwise operators and integers, a utf8 library, and standard binary pack/unpack functions. In addition to adding Lua 5.3, this branch changes: o Complete removal of the NSE bit library (in C), It has been replaced with a new Lua library wrapping Lua 5.3's bit-wise operators. o Complete removal of the NSE bin library (in C). It has been replaced with a new Lua library wrapping Lua 5.3's string.pack|unpack functions. o The bin.pack "B" format specifier (which has never worked correctly) is unimplemented. All scripts/libraries which use it have been updated. Most usage of this option was to allow string based bit-wise operations which are no longer necessary now that Lua 5.3 provides integers and bit-wise operators. o The base32/base64 libraries have been reimplemented using Lua 5.3's new bitwise operators. (This library was the main user of the bin.pack "B" format specifier.) o A new "bits" library has been added for common bit hacks. Currently only has a reverse function. Thanks to David Fifield, Daniel Miller, Jacek Wielemborek, and Paulino Calderon for testing this branch.
This commit is contained in:
66
nselib/bits.lua
Normal file
66
nselib/bits.lua
Normal file
@@ -0,0 +1,66 @@
|
||||
---
|
||||
-- Bit manipulation library.
|
||||
--
|
||||
-- @author Patrick Donnelly <batrick@batbytes.com>
|
||||
-- @copyright Same as Nmap -- see https://nmap.org/book/man-legal.html
|
||||
-- @see https://www.lua.org/manual/5.3/manual.html#3.4.2
|
||||
|
||||
local assert = assert
|
||||
local error = error
|
||||
|
||||
local _ENV = {}
|
||||
|
||||
--- Reverses bits in integer.
|
||||
--
|
||||
-- @param n The integer.
|
||||
-- @param size The bit width of the integer (default: 8).
|
||||
-- @return The reversed integer.
|
||||
function reverse (n, size)
|
||||
if not size or size == 8 then
|
||||
n = n & 0xff
|
||||
n = (n & 0xf0) >> 4 | (n & 0x0f) << 4
|
||||
n = (n & 0xcc) >> 2 | (n & 0x33) << 2
|
||||
n = (n & 0xaa) >> 1 | (n & 0x55) << 1
|
||||
n = n & 0xff
|
||||
elseif size == 32 then
|
||||
n = n & 0xffffffff
|
||||
n = ((n >> 1) & 0x55555555) | ((n & 0x55555555) << 1);
|
||||
n = ((n >> 2) & 0x33333333) | ((n & 0x33333333) << 2);
|
||||
n = ((n >> 4) & 0x0F0F0F0F) | ((n & 0x0F0F0F0F) << 4);
|
||||
n = ((n >> 8) & 0x00FF00FF) | ((n & 0x00FF00FF) << 8);
|
||||
n = ( n >> 16 ) | ( n << 16);
|
||||
n = n & 0xffffffff
|
||||
else
|
||||
error("invalid size: "..size)
|
||||
end
|
||||
return n
|
||||
end
|
||||
|
||||
do
|
||||
local function test8 (a, b)
|
||||
local r = reverse(a, 8)
|
||||
if r ~= b then
|
||||
error(("0x%02X: expected 0x%02X, got 0x%02X"):format(a, b, r))
|
||||
end
|
||||
end
|
||||
test8(0x00, 0x00)
|
||||
test8(0x01, 0x80)
|
||||
test8(0x80, 0x01)
|
||||
test8(0xff, 0xff)
|
||||
test8(0x88, 0x11)
|
||||
test8(0x5c, 0x3a)
|
||||
|
||||
local function test32 (a, b)
|
||||
local r = reverse(a, 32)
|
||||
if r ~= b then
|
||||
error(("0x%08X: expected 0x%08X, got 0x%08X"):format(a, b, r))
|
||||
end
|
||||
end
|
||||
test32(0x00000000, 0x00000000)
|
||||
test32(0x00000001, 0x80000000)
|
||||
test32(0x80000000, 0x00000001)
|
||||
test32(0xffffffff, 0xffffffff)
|
||||
test32(0x22221234, 0x2c484444)
|
||||
end
|
||||
|
||||
return _ENV
|
||||
Reference in New Issue
Block a user