Nmap Development mailing list archives

Re: [Lua snippit] print_hex


From: Ron <ron () skullsecurity net>
Date: Sat, 06 Sep 2008 10:51:56 -0500

Patrick Donnelly wrote:
On Fri, Sep 5, 2008 at 8:19 AM, Ron <ron () skullsecurity net> wrote:

Attached is an improved version that removes unnecessary loops/logic.
It was a fun little exercise :)

Cheers,

You version seems to have an off-by-one error that cuts off the last hex
byte. I added a +1/-1 to compensate.

Maybe now this is perfect? :)
-- 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


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

Current thread: