2         This may be moved to a separate library at some point (Holiman)
 
   6         -- Asks the user for Yes or No
 
   7         confirm = function(message, ...)
 
   9                 message = message .. " [y/n] ?"
 
  14                         if answer == 'Y' or answer == "y" then
 
  16                         elseif answer == 'N' or answer == 'n' then 
 
  22         -- Asks the user for input
 
  23         input = function (message , default)
 
  25                 if default ~= nil then
 
  26                         message = message .. " (default: ".. default.. " )"
 
  28                 message = message .." \n > "
 
  32                 if answer == '' then answer = default end
 
  37         ------------ FILE READING
 
  38         ReadDumpFile = function (filename)
 
  40                 filename = filename or 'dumpdata.bin'
 
  41                 if #filename == 0 then
 
  42                         return nil, 'Filename length is zero'
 
  45                 infile = io.open(filename, "rb")
 
  47                         return nil, string.format("Could not read file %s",filename)
 
  49                 local t = infile:read("*all")
 
  51                 local  _,hex = bin.unpack(("H%d"):format(len),t)
 
  56         ------------ string split function
 
  57         Split = function( inSplitPattern, outResults )
 
  58                 if not outResults then
 
  62                 local splitStart, splitEnd = string.find( self, inSplitPattern, start )
 
  64                         table.insert( outResults, string.sub( self, start, splitStart-1 ) )
 
  66                         splitStart, splitEnd = string.find( self, inSplitPattern, start )
 
  68                 table.insert( outResults, string.sub( self, start ) )
 
  73         ------------ CRC-16 ccitt checksums
 
  74         -- Takes a hex string and calculates a crc16
 
  76                 if s == nil then return nil end
 
  77                 if #s == 0 then return nil end
 
  78                 if  type(s) == 'string' then
 
  79                         local utils = require('utils')
 
  80                         local asc = utils.ConvertHexToAscii(s)
 
  81                         local hash = core.crc16(asc)
 
  87         ------------ CRC-64 ecma checksums
 
  88         -- Takes a hex string and calculates a crc64 ecma
 
  90                 if s == nil then return nil end
 
  91                 if #s == 0 then return nil end
 
  92                 if  type(s) == 'string' then
 
  93                         local utils = require('utils')
 
  94                         local asc = utils.ConvertHexToAscii(s)
 
  95                         local hash = core.crc64(asc)
 
 101         ------------ SHA1 hash
 
 102         -- Takes a string and calculates a SHA1 hash
 
 104                 if s == nil then return nil end
 
 105                 if #s == 0 then return nil end
 
 106                 if  type(s) == 'string' then
 
 107                         local utils = require('utils')
 
 108                         --local asc = utils.ConvertHexToAscii(s)
 
 109                         local hash = core.sha1(s)
 
 114         -- Takes a hex string and calculates a SHA1 hash
 
 115         Sha1Hex = function(s)
 
 116                 if s == nil then return nil end
 
 117                 if #s == 0 then return nil end
 
 118                 if  type(s) == 'string' then
 
 119                         local utils = require('utils')
 
 120                         local asc = utils.ConvertHexToAscii(s)
 
 121                         local hash = core.sha1(asc)
 
 128         -- input parameter is a string
 
 129         -- Swaps the endianess and returns a number,  
 
 130         -- IE:  'cd7a' -> '7acd'  -> 0x7acd
 
 131         SwapEndianness = function(s, len)
 
 132                 if s == nil then return nil end
 
 133                 if #s == 0 then return '' end
 
 134                 if  type(s) ~= 'string' then return nil end
 
 138                         local t = s:sub(3,4)..s:sub(1,2)
 
 139                         retval = tonumber(t,16)
 
 140                 elseif len == 24 then
 
 141                         local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
 
 142                         retval = tonumber(t,16)
 
 143                 elseif len == 32 then
 
 144                         local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
 
 145                         retval = tonumber(t,16)
 
 150         -- input parameter is a string
 
 151         -- Swaps the endianess and returns a string,  
 
 152         -- IE:  'cd7a' -> '7acd'  -> 0x7acd
 
 153         SwapEndiannessStr = function(s, len)
 
 154                 if s == nil then return nil end
 
 155                 if #s == 0 then return '' end
 
 156                 if  type(s) ~= 'string' then return nil end
 
 160                         retval = s:sub(3,4)..s:sub(1,2)
 
 161                 elseif len == 24 then
 
 162                         retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
 
 163                 elseif len == 32 then
 
 164                         retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
 
 168         ------------ CONVERSIONS
 
 171         -- Converts DECIMAL to HEX
 
 172     ConvertDecToHex = function(IN)
 
 173                 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
 
 176                         IN , D = math.floor(IN/B), math.modf(IN,B)+1
 
 177                         OUT = string.sub(K,D,D)..OUT
 
 182         -- Convert Byte array to string of hex
 
 183         ConvertBytesToHex = function(bytes)
 
 188                 for i = 1, #(bytes) do
 
 189                         s[i] = string.format("%02X",bytes[i]) 
 
 191                 return table.concat(s)
 
 193         -- Convert byte array to string with ascii
 
 194     ConvertBytesToAscii = function(bytes)
 
 199                 for i = 1, #(bytes) do
 
 200                         s[i] = string.char(bytes[i]) 
 
 202                 return table.concat(s)          
 
 204         ConvertHexToBytes = function(s)
 
 206                 if s == nil then return t end
 
 207                 if #s == 0 then return t end
 
 208                 for k in s:gmatch"(%x%x)" do
 
 209                         table.insert(t,tonumber(k,16))
 
 213         ConvertAsciiToBytes = function(s, reverse)
 
 215                 if s == nil then return t end
 
 216                 if #s == 0 then return t end
 
 218                 for k in s:gmatch"(.)" do
 
 219                         table.insert(t, string.byte(k))
 
 229                                 table.insert(rev, t[i] )
 
 235         ConvertHexToAscii = function(s)
 
 237                 if s == nil then return t end
 
 238                 if #s == 0 then return t end
 
 239                 for k in s:gmatch"(%x%x)" do
 
 240                         table.insert(t, string.char(tonumber(k,16)))
 
 242                 return  table.concat(t) 
 
 245         Chars2num = function(s)
 
 246         return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
 
 249         -- use length of string to determine 8,16,32,64 bits
 
 250         bytes_to_int = function(str,endian,signed) 
 
 251                 local t={str:byte(1,-1)}
 
 252                 if endian=="big" then --reverse bytes
 
 264                         n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.
 
 269         -- function convertStringToBytes(str)
 
 271         -- local strLength = string.len(str)
 
 272         -- for i=1,strLength do
 
 273                 -- table.insert(bytes, string.byte(str, i))
 
 279 -- function convertBytesToString(bytes)
 
 280         -- local bytesLength = table.getn(bytes)
 
 282         -- for i=1,bytesLength do
 
 283                 -- str = str .. string.char(bytes[i])
 
 289 -- function convertHexStringToBytes(str)
 
 291         -- local strLength = string.len(str)
 
 292         -- for k=2,strLength,2 do
 
 293                 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
 
 294                 -- table.insert(bytes, hex.to_dec(hexString))
 
 300 -- function convertBytesToHexString(bytes)
 
 302         -- local bytesLength = table.getn(bytes)
 
 303         -- for i=1,bytesLength do
 
 304                 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
 
 305                 -- if string.len(hexString) == 1 then
 
 306                         -- hexString = "0" .. hexString
 
 308                 -- str = str .. hexString