mirror of
https://github.com/nmap/nmap.git
synced 2025-12-06 04:31:29 +00:00
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.
96 lines
2.1 KiB
Lua
96 lines
2.1 KiB
Lua
---
|
|
-- Bitwise operations on integers.
|
|
--
|
|
-- THIS LIBRARY IS DEPRECATED, Please use native Lua 5.3 bitwise operators.
|
|
--
|
|
-- @copyright BSD License
|
|
-- @see https://www.lua.org/manual/5.3/manual.html#3.4.2
|
|
|
|
local select = select
|
|
|
|
local mininteger = require "math".mininteger
|
|
|
|
local _ENV = {}
|
|
|
|
--- Returns the one's complement of <code>a</code>.
|
|
-- @param a Number.
|
|
-- @return The one's complement of <code>a</code>.
|
|
function bnot(a)
|
|
return ~a
|
|
end
|
|
|
|
--- Returns the bitwise and of all its arguments.
|
|
-- @param ... A variable number of Numbers to and.
|
|
-- @return The anded result.
|
|
function band(a, b, ...)
|
|
a = a & b
|
|
if select("#", ...) > 0 then
|
|
return band(a, ...)
|
|
else
|
|
return a
|
|
end
|
|
end
|
|
|
|
--- Returns the bitwise or of all its arguments.
|
|
-- @param ... A variable number of Numbers to or.
|
|
-- @return The ored result.
|
|
function bor(a, b, ...)
|
|
a = a | b
|
|
if select("#", ...) > 0 then
|
|
return bor(a, ...)
|
|
else
|
|
return a
|
|
end
|
|
end
|
|
|
|
--- Returns the bitwise exclusive or of all its arguments.
|
|
-- @param ... A variable number of Numbers to exclusive or.
|
|
-- @return The exclusive ored result.
|
|
function bxor(a, b, ...)
|
|
a = a ~ b
|
|
if select("#", ...) > 0 then
|
|
return bxor(a, ...)
|
|
else
|
|
return a
|
|
end
|
|
end
|
|
|
|
--- Returns <code>a</code> left-shifted by <code>b</code> places.
|
|
-- @param a Number to perform the shift on.
|
|
-- @param b Number of shifts.
|
|
function lshift(a, b)
|
|
return a << b
|
|
end
|
|
|
|
--- Returns <code>a</code> right-shifted by <code>b</code> places.
|
|
-- @param a Number to perform the shift on.
|
|
-- @param b Number of shifts.
|
|
function rshift(a, b)
|
|
return a >> b
|
|
end
|
|
|
|
--- Returns <code>a</code> arithmetically right-shifted by <code>b</code>
|
|
-- places.
|
|
-- @param a Number to perform the shift on.
|
|
-- @param b Number of shifts.
|
|
function arshift(a, b)
|
|
if a < 0 then
|
|
if a % 2 == 0 then -- even?
|
|
return a // (1<<b)
|
|
else
|
|
return a // (1<<b) + 1
|
|
end
|
|
else
|
|
return a >> b
|
|
end
|
|
end
|
|
|
|
--- Returns the integer remainder of <code>a</code> divided by <code>b</code>.
|
|
-- @param a Dividend.
|
|
-- @param b Divisor.
|
|
function mod(a, b)
|
|
return a % b
|
|
end
|
|
|
|
return _ENV
|