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 ) )
75 ------------ 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 ------------ CRC-64 ecma checksums
90 -- Takes a hex string and calculates a crc64 ecma
92 if s == nil then return nil end
93 if #s == 0 then return nil end
94 if type(s) == 'string' then
95 local utils = require('utils')
96 local asc = utils.ConvertHexToAscii(s)
97 local hash = core.crc64(asc)
104 -- input parameter is a string
105 -- Swaps the endianess and returns a number,
106 -- IE: 'cd7a' -> '7acd' -> 0x7acd
107 SwapEndianness = function(s, len)
108 if s == nil then return nil end
109 if #s == 0 then return '' end
110 if type(s) ~= 'string' then return nil end
114 local t = s:sub(3,4)..s:sub(1,2)
115 retval = tonumber(t,16)
116 elseif len == 24 then
117 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
118 retval = tonumber(t,16)
119 elseif len == 32 then
120 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
121 retval = tonumber(t,16)
126 -- input parameter is a string
127 -- Swaps the endianess and returns a string,
128 -- IE: 'cd7a' -> '7acd' -> 0x7acd
129 SwapEndiannessStr = function(s, len)
130 if s == nil then return nil end
131 if #s == 0 then return '' end
132 if type(s) ~= 'string' then return nil end
136 retval = s:sub(3,4)..s:sub(1,2)
137 elseif len == 24 then
138 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
139 elseif len == 32 then
140 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
144 ------------ CONVERSIONS
147 -- Converts DECIMAL to HEX
148 ConvertDecToHex = function(IN)
149 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
152 IN , D = math.floor(IN/B), math.modf(IN,B)+1
153 OUT = string.sub(K,D,D)..OUT
158 -- Convert Byte array to string of hex
159 ConvertBytesToHex = function(bytes)
164 for i = 1, #(bytes) do
165 s[i] = string.format("%02X",bytes[i])
167 return table.concat(s)
169 -- Convert byte array to string with ascii
170 ConvertBytesToAscii = function(bytes)
175 for i = 1, #(bytes) do
176 s[i] = string.char(bytes[i])
178 return table.concat(s)
180 ConvertHexToBytes = function(s)
182 if s == nil then return t end
183 if #s == 0 then return t end
184 for k in s:gmatch"(%x%x)" do
185 table.insert(t,tonumber(k,16))
189 ConvertAsciiToBytes = function(s, reverse)
191 if s == nil then return t end
192 if #s == 0 then return t end
194 for k in s:gmatch"(.)" do
195 table.insert(t, string.byte(k))
205 table.insert(rev, t[i] )
211 ConvertHexToAscii = function(s)
213 if s == nil then return t end
214 if #s == 0 then return t end
215 for k in s:gmatch"(%x%x)" do
216 table.insert(t, string.char(tonumber(k,16)))
218 return table.concat(t)
221 Chars2num = function(s)
222 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
225 -- use length of string to determine 8,16,32,64 bits
226 bytes_to_int = function(str,endian,signed)
227 local t={str:byte(1,-1)}
228 if endian=="big" then --reverse bytes
240 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.
245 -- function convertStringToBytes(str)
247 -- local strLength = string.len(str)
248 -- for i=1,strLength do
249 -- table.insert(bytes, string.byte(str, i))
255 -- function convertBytesToString(bytes)
256 -- local bytesLength = table.getn(bytes)
258 -- for i=1,bytesLength do
259 -- str = str .. string.char(bytes[i])
265 -- function convertHexStringToBytes(str)
267 -- local strLength = string.len(str)
268 -- for k=2,strLength,2 do
269 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
270 -- table.insert(bytes, hex.to_dec(hexString))
276 -- function convertBytesToHexString(bytes)
278 -- local bytesLength = table.getn(bytes)
279 -- for i=1,bytesLength do
280 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
281 -- if string.len(hexString) == 1 then
282 -- hexString = "0" .. hexString
284 -- str = str .. hexString