mirror of
https://github.com/nmap/nmap.git
synced 2026-01-04 13:49:03 +00:00
New stdnse function 'fromhex' does hex decoding like bin.pack('H')
This commit is contained in:
@@ -403,6 +403,30 @@ function tohex( s, options )
|
||||
return hex
|
||||
end
|
||||
|
||||
---Decode a hexadecimal string to raw bytes
|
||||
--
|
||||
-- The string can contain any amount of whitespace and capital or lowercase
|
||||
-- hexadecimal digits. There must be an even number of hex digits, since it
|
||||
-- takes 2 hex digits to make a byte.
|
||||
--
|
||||
-- @param hex A string in hexadecimal representation
|
||||
-- @return A string of bytes or nil if string could not be decoded
|
||||
-- @return Error message if string could not be decoded
|
||||
function fromhex (hex)
|
||||
local len = #hex
|
||||
local out = {}
|
||||
local i = 1
|
||||
while i <= len do
|
||||
local p, q, c1, c2 = find(hex, "^%s*(%x)%s*(%x)%s*", i)
|
||||
if not p then
|
||||
return nil, format("Invalid characters or odd number of hex digits at %d", i)
|
||||
end
|
||||
out[#out+1] = char(tonumber(c1..c2, 16))
|
||||
i = q + 1
|
||||
end
|
||||
return concat(out)
|
||||
end
|
||||
|
||||
---Format a MAC address as colon-separated hex bytes.
|
||||
--@param mac The MAC address in binary, such as <code>host.mac_addr</code>
|
||||
--@return The MAC address in XX:XX:XX:XX:XX:XX format
|
||||
|
||||
Reference in New Issue
Block a user