Nmap Development mailing list archives
Re: [Lua snippit] print_hex
From: "Patrick Donnelly" <batrick.donnelly () gmail com>
Date: Sun, 7 Sep 2008 18:49:26 -0600
On Sat, Sep 6, 2008 at 9:51 AM, Ron <ron () skullsecurity net> wrote:
You version seems to have an off-by-one error that cuts off the last hex byte. I added a +1/-1 to compensate.
Good catch!
Maybe now this is perfect? :)
That looks fine. I put the "out" local out of the function definition to prevent the string from repeatedly being remade (numbers are constant folded but not strings). I did this mainly to expose people on the list to some good practices (among other things I did), even though it probably won't make a huge difference in a debugging function. If you moved it inside because you didn't want to clutter the surrounding function (the file) with variables, you can surround the print_hex function with do ... end to force scoping. e.g.: do local out = "..." function print_hex(str) ... end end
-- Print out a string in hex, for debugging. function print_hex(str) local out = "%08x"..(" %02x"):rep(16).." "; local len, a = #str, 1; repeat if a + 16 > len then -- partial line? io.write( -- 00000000 AB CD EF GH JK ("%08x"..(" %02x"):rep(len-a+1)..(" "):rep(17+a-len-1)):format( a-1, str:byte(a, a+16)), -- abcdefg\n str:sub(a, a+16):gsub("%c", "."), "\n"); else -- full line io.write( -- 00000000 AB CD EF GH JK LM NO PQ out:format(a-1, str:byte(a, a+16)), -- abcdefgh\n str:sub(a, a+16):gsub("%c", "."), "\n"); end a = a + 16; until a > len; -- Print out the length io.write((" Length: %d [0x%x]\n"):format(len, len)) end
Cheers, -- -Patrick Donnelly "One of the lessons of history is that nothing is often a good thing to do and always a clever thing to say." -Will Durant _______________________________________________ Sent through the nmap-dev mailing list http://cgi.insecure.org/mailman/listinfo/nmap-dev Archived at http://SecLists.Org
Current thread:
- [Lua snippit] print_hex Ron (Sep 05)
- Re: [Lua snippit] print_hex Patrick Donnelly (Sep 05)
- Re: [Lua snippit] print_hex Ron (Sep 05)
- Re: [Lua snippit] print_hex Ron (Sep 06)
- Re: [Lua snippit] print_hex Patrick Donnelly (Sep 07)
- Re: [Lua snippit] print_hex Kris Katterjohn (Sep 07)
- Re: [Lua snippit] print_hex Ron (Sep 07)
- Re: [Lua snippit] print_hex Patrick Donnelly (Sep 07)
- Re: [Lua snippit] print_hex Patrick Donnelly (Sep 07)
- Re: [Lua snippit] print_hex Sven Klemm (Sep 08)
- Re: [Lua snippit] print_hex Patrick Donnelly (Sep 07)
- Re: [Lua snippit] print_hex Patrick Donnelly (Sep 05)
- Re: [Lua snippit] print_hex Ron (Sep 07)
- Re: [Lua snippit] print_hex Patrick Donnelly (Sep 07)