|
Nmap Development
mailing list archives
Re: [NSE][PATCH] string_ext library
From: Sven Klemm <sven () c3d2 de>
Date: Fri, 03 Oct 2008 12:54:06 +0200
Currently there is already a tohex() function in stdnse.lua which only
works for numbers. It's probably best to rename my function to tohex()
since this is more in line with the lua function names.
Do you think we should put this function into its own module or do you
think it should be added to stdnse?
I would actually recommend extending the tohex() function to handle
strings or numbers. Perhaps the other functions could be extended in
the same way as well.
I've attached a patch which extends stdnse.tohex() to support strings
and numbers and the new formatting options and adjusts the
documentation accordingly.
Cheers,
Sven
--
Sven Klemm
http://cthulhu.c3d2.de/~sven/
Index: docs/scripting.xml
===================================================================
--- docs/scripting.xml (revision 10448)
+++ docs/scripting.xml (working copy)
@@ -2127,12 +2127,17 @@
</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>
Index: nselib/stdnse.lua
===================================================================
--- nselib/stdnse.lua (revision 10448)
+++ nselib/stdnse.lua (working copy)
@@ -143,10 +143,43 @@
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=2}) => "6162:63"
+-- stdnse.tohex(123456) => "1e240"
+-- stdnse.tohex(123456,{separator=":"}) => "1:e2:40"
+-- stdnse.tohex(123456,{separator=":",group=2}) => "1:e240"
+-- () param s string or number to be encoded
+-- () param options table specifiying formatting options
+-- () return hexadecimal encoded string
+tohex = function( s, options )
+ options = options or {}
+ local group = options.group or 1
+ local separator = options.separator or ""
+
+ if type( s ) == 'number' then
+ local hex = ("%x"):format(s)
+ local fmt_table = {}
+ local index
+ group = group * 2
+ for i=#hex,1,-group do
+ -- index must be consecutive otherwise table.concat won't work
+ index = math.ceil(i/group)
+ fmt_table[index] = hex:sub(math.max(i-group+1,1),i)
+ end
+
+ return table.concat( fmt_table, separator )
+ elseif type( s ) == 'string' then
+ local fmt_table = {}
+ for i=1,#s,group do
+ table.insert( fmt_table, string.rep("%02x", math.min(#s-i+1,group)))
+ end
+
+ separator = separator:gsub("%%","%%%%")
+ return table.concat( fmt_table, separator ):format(s:byte(1, #s))
+ else
+ error( "Type not supported in tohex(): " .. type(s), 2 )
+ end
end
+
_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://SecLists.Org
By Date
By Thread
Current thread:
|