Nmap Development mailing list archives

Re: [NSE][PATCH] string_ext library


From: Sven Klemm <sven () c3d2 de>
Date: Fri, 03 Oct 2008 13:00:44 +0200

I've attached a patch which extends stdnse.tohex() to support strings and numbers and the new formatting options and adjusts the documentation accordingly.

The same with local function references.

Cheers,
Sven

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

Index: nselib/stdnse.lua
===================================================================
--- nselib/stdnse.lua   (revision 10448)
+++ nselib/stdnse.lua   (working copy)
@@ -4,7 +4,11 @@
 local assert = assert;
 local tonumber = tonumber;
 local concat = table.concat;
+local insert = table.insert;
 local nmap = require"nmap";
+local min = math.min
+local max = math.max
+local ceil = math.ceil
 
 module(... or "stdnse");
 
@@ -143,10 +147,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 = ceil(i/group)
+      fmt_table[index] = hex:sub(max(i-group+1,1),i)
+    end
+
+    return concat( fmt_table, separator )
+  elseif type( s ) == 'string' then
+    local fmt_table = {}
+    for i=1,#s,group do
+      insert( fmt_table, string.rep("%02x", min(#s-i+1,group)))
+    end
+
+    separator = separator:gsub("%%","%%%%")
+    return concat( fmt_table, separator ):format(s:byte(1, #s))
+  else
+    error( "Type not supported in tohex(): " .. type(s), 2 )
+  end
 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>

Attachment: signature.asc
Description: OpenPGP digital signature


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

Current thread: