Nmap Development mailing list archives
Re: [NSE] proposal of size_tohuman() in stdnse library
From: Djalal Harouni <tixxdz () gmail com>
Date: Tue, 8 Jun 2010 23:23:02 +0100
On 2010-06-08 11:16:53 -0600, David Fifield wrote:
On Tue, Jun 08, 2010 at 02:09:03AM +0100, Djalal Harouni wrote:I've coded a function which converts the size in bytes to human readable format, I'm using this function in my NFS code to show files size, but perhaps this function would be more useful if it is in the stdnse library, it's a generic function. I've make it simple, the %.1f format will do some round up/down. --- Converts the size in bytes to a human readable format -- -- An optional second argument is the size of the block -- -- @usage -- size_tohuman(1024) --> 1024.0B -- size_tohuman(926548776) --> 883.6M -- size_tohuman(246548, 1024) --> 240.8K -- size_tohuman(246548, 1000) --> 246.5K -- -- @param size in bytes -- @param blocksize represents the number of bytes per block -- Possible values are: 1024 or 1000 -- Default value is: 1024 -- @return String that represent the size in the human readable -- format function size_tohuman(size, blocksize) local idx, bs = 1, 1024 local unit = { "B", "K", "M", "G" } if blocksize and blocksize == 1000 then bs = blocksize end for i=1, #unit do if (size > bs) then size = size / bs idx = idx + 1 end end return string.format("%.1f%s", size, unit[idx]) endThis looks fine to me. In tcpip.cc there is a function called ll2shortascii that is used for the same purpose in Nmap. (ll2shortascii is a bad name; it's going to be changed to format_bytecount when Luis merges his nmap-dedup branches.) How about the name format_bytecount? That goes with the names format_difftime and format_output that are already in stdnse.
Well, it's fine for this name (format_bytecount) or perhaps format_sizeof (found it on the net). the ll2shortascii() functions seems to use only a size block of 1000 (which is used by some hdd manufacturers) but as I'm planning to emulate the tool "ls" in my NFS code which uses 1024 as a size block, so adding a new parameter to the function to choose the block size will be the best bet. For the moment I'm using this code in my nfs-ls but I'll change it and export this function to NSE in the stdnse library, so I'll wait for Luis's merge.
David Fifield
-- tixxdz _______________________________________________ Sent through the nmap-dev mailing list http://cgi.insecure.org/mailman/listinfo/nmap-dev Archived at http://seclists.org/nmap-dev/
Current thread:
- Re: [NSE] proposal of size_tohuman() in stdnse library David Fifield (Jun 08)
- Re: [NSE] proposal of size_tohuman() in stdnse library Djalal Harouni (Jun 08)