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 if filename == nil then
41 return nil, 'Filename is empty'
43 if #filename == 0 then
44 return nil, 'Filename length is zero'
47 infile = io.open(filename, "rb")
49 return nil, string.format("Could not read file %s",filename)
51 local t = infile:read("*all")
53 local _,hex = bin.unpack(("H%d"):format(len),t)
58 ------------ string split function
59 Split = function( inSplitPattern, outResults )
60 if not outResults then
64 local splitStart, splitEnd = string.find( self, inSplitPattern, start )
66 table.insert( outResults, string.sub( self, start, splitStart-1 ) )
68 splitStart, splitEnd = string.find( self, inSplitPattern, start )
70 table.insert( outResults, string.sub( self, start ) )
74 ------------ CRC-16 ccitt checksums
76 -- Takes a hex string and calculates a crc16
78 if s == nil then return nil end
79 if #s == 0 then return nil end
80 if type(s) == 'string' then
81 local utils = require('utils')
82 local asc = utils.ConvertHexToAscii(s)
83 local hash = core.crc16(asc)
89 -- input parameter is a string
90 -- Swaps the endianess and returns a number,
91 -- IE: 'cd7a' -> '7acd' -> 0x7acd
92 SwapEndianness = function(s, len)
93 if s == nil then return nil end
94 if #s == 0 then return '' end
95 if type(s) ~= 'string' then return nil end
99 local t = s:sub(3,4)..s:sub(1,2)
100 retval = tonumber(t,16)
101 elseif len == 24 then
102 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
103 retval = tonumber(t,16)
104 elseif len == 32 then
105 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
106 retval = tonumber(t,16)
111 ------------ CONVERSIONS
114 -- Converts DECIMAL to HEX
115 ConvertDecToHex = function(IN)
116 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
119 IN,D=math.floor(IN/B),math.mod(IN,B)+1
120 OUT=string.sub(K,D,D)..OUT
125 -- Convert Byte array to string of hex
126 ConvertBytesToHex = function(bytes)
131 for i = 1, #(bytes) do
132 s[i] = string.format("%02X",bytes[i])
134 return table.concat(s)
136 -- Convert byte array to string with ascii
137 ConvertBytesToAscii = function(bytes)
142 for i = 1, #(bytes) do
143 s[i] = string.char(bytes[i])
145 return table.concat(s)
147 ConvertHexToBytes = function(s)
149 if s == nil then return t end
150 if #s == 0 then return t end
151 for k in s:gmatch"(%x%x)" do
152 table.insert(t,tonumber(k,16))
156 ConvertAsciiToBytes = function(s)
158 if s == nil then return t end
159 if #s == 0 then return t end
161 for k in s:gmatch"(.)" do
162 table.insert(t, string.byte(k))
166 ConvertHexToAscii = function(s)
168 if s == nil then return t end
169 if #s == 0 then return t end
170 for k in s:gmatch"(%x%x)" do
171 table.insert(t, string.char(tonumber(k,16)))
173 return table.concat(t)
176 -- function convertStringToBytes(str)
178 -- local strLength = string.len(str)
179 -- for i=1,strLength do
180 -- table.insert(bytes, string.byte(str, i))
186 -- function convertBytesToString(bytes)
187 -- local bytesLength = table.getn(bytes)
189 -- for i=1,bytesLength do
190 -- str = str .. string.char(bytes[i])
196 -- function convertHexStringToBytes(str)
198 -- local strLength = string.len(str)
199 -- for k=2,strLength,2 do
200 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
201 -- table.insert(bytes, hex.to_dec(hexString))
207 -- function convertBytesToHexString(bytes)
209 -- local bytesLength = table.getn(bytes)
210 -- for i=1,bytesLength do
211 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
212 -- if string.len(hexString) == 1 then
213 -- hexString = "0" .. hexString
215 -- str = str .. hexString