diff --git a/CHANGELOG b/CHANGELOG index b55bd4828..90c5aa191 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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] diff --git a/docs/scripting.xml b/docs/scripting.xml index ea91314cf..1db13e280 100644 --- a/docs/scripting.xml +++ b/docs/scripting.xml @@ -1910,12 +1910,17 @@ if(s) code_to_be_done_on_match end - + - Converts the given number, n, to a string - in a hexadecimal number format (e.g. 10 becomes "a"). + Converts the given number or string, s, to a + string in a hexadecimal number format (e.g. 10 becomes "a"). + options is a table containing parameters to + control the formatting. You may specify options.separator + which will be used as separator for groups of consecutive bytes. + With options.group you can control the group + length to be used with options.separator. diff --git a/nselib/stdnse.lua b/nselib/stdnse.lua index 99ee00fea..9e32a8ee6 100644 --- a/nselib/stdnse.lua +++ b/nselib/stdnse.lua @@ -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 +