mirror of
https://github.com/nmap/nmap.git
synced 2026-01-22 22:29:03 +00:00
o Enhanced the tohex() function in the NSE stdnse library to support strings
and added options to control the formatting. [Sven]
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
# Nmap Changelog ($Id$); -*-text-*-
|
||||
|
||||
o Enhanced the tohex() function in the NSE stdnse library to support strings
|
||||
and added options to control the formatting. [Sven]
|
||||
|
||||
o The http NSE module tries to deal with non-standards-compliant HTTP
|
||||
traffic, particularly responses in which the header fields are
|
||||
separated by plain LF rather than CRLF. [Jah, Sven]
|
||||
|
||||
@@ -1910,12 +1910,17 @@ if(s) code_to_be_done_on_match end
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term><option>string = stdnse.tohex(n)</option>
|
||||
<term><option>string = stdnse.tohex(s, options)</option>
|
||||
</term>
|
||||
<listitem>
|
||||
<para>
|
||||
Converts the given number, <literal>n</literal>, to a string
|
||||
in a hexadecimal number format (e.g. 10 becomes "a").
|
||||
Converts the given number or string, <literal>s</literal>, to a
|
||||
string in a hexadecimal number format (e.g. 10 becomes "a").
|
||||
<literal>options</literal> is a table containing parameters to
|
||||
control the formatting. You may specify <literal>options.separator</literal>
|
||||
which will be used as separator for groups of consecutive bytes.
|
||||
With <literal>options.group</literal> you can control the group
|
||||
length to be used with <literal>options.separator</literal>.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
@@ -5,6 +5,9 @@ local assert = assert;
|
||||
local tonumber = tonumber;
|
||||
local concat = table.concat;
|
||||
local nmap = require"nmap";
|
||||
local max = math.max
|
||||
local ceil = math.ceil
|
||||
local type = type
|
||||
|
||||
module(... or "stdnse");
|
||||
|
||||
@@ -143,10 +146,36 @@ function tooctal(n)
|
||||
return ("%o"):format(n)
|
||||
end
|
||||
|
||||
--- Converts the given number, n, to a string in a hexidecimal number format.
|
||||
--@param n Number to convert.
|
||||
--@return String in hexidecimal format.
|
||||
function tohex(n)
|
||||
assert(tonumber(n), "number expected");
|
||||
return ("%x"):format(n);
|
||||
--- encode string or number to hexadecimal
|
||||
-- example: stdnse.tohex("abc") => "616263"
|
||||
-- stdnse.tohex("abc",{separator=":"}) => "61:62:63"
|
||||
-- stdnse.tohex("abc",{separator=":",group=4}) => "61:6263"
|
||||
-- stdnse.tohex(123456) => "1e240"
|
||||
-- stdnse.tohex(123456,{separator=":"}) => "1:e2:40"
|
||||
-- stdnse.tohex(123456,{separator=":",group=4}) => "1:e240"
|
||||
--@param s string or number to be encoded
|
||||
--@param options table specifiying formatting options
|
||||
--@return hexadecimal encoded string
|
||||
function tohex( s, options )
|
||||
options = options or {}
|
||||
local group = options.group or 2
|
||||
local separator = options.separator or ""
|
||||
local hex
|
||||
|
||||
if type( s ) == 'number' then
|
||||
hex = ("%x"):format(s)
|
||||
elseif type( s ) == 'string' then
|
||||
hex = ("%02x"):rep(#s):format(s:byte(1,#s))
|
||||
else
|
||||
error( "Type not supported in tohex(): " .. type(s), 2 )
|
||||
end
|
||||
|
||||
local fmt_table = {}
|
||||
for i=#hex,1,-group do
|
||||
-- index must be consecutive otherwise table.concat won't work
|
||||
fmt_table[ceil(i/group)] = hex:sub(max(i-group+1,1),i)
|
||||
end
|
||||
|
||||
return concat( fmt_table, separator )
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user