Nmap Development mailing list archives

Re: [NSE][PATCH] string_ext library


From: Sven Klemm <sven () c3d2 de>
Date: Fri, 03 Oct 2008 19:20:17 +0200

David Fifield wrote:
On Fri, Oct 03, 2008 at 12:54:06PM +0200, Sven Klemm wrote:
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.

I think this is a great idea. I was surprised at this difference in
behavior:

stdnse.tohex("abc",{separator=":",group=2}) => "6162:63"
stdnse.tohex(123456,{separator=":",group=2}) => "1:e240"

When the argument is a string, grouping is done left to right, and when
it is a number grouping is done right to left. I expected it to go right
to left in both cases, but maybe there's a good reason to do it
otherwise?

You are right it should be done the same for both cases.
I've attached a new version which does it right to left in both cases. I've also changed group to mean grouping by hex characters not bytes but I'm not sure about this one.


Cheers,
Sven


--
Sven Klemm
http://cthulhu.c3d2.de/~sven/

Index: nselib/stdnse.lua
===================================================================
--- nselib/stdnse.lua   (revision 10448)
+++ nselib/stdnse.lua   (working copy)
@@ -5,6 +5,9 @@
 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 @@
   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
+
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>

_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://SecLists.Org

Current thread: