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)
103 ------------ SHA1 hash
104 -- Takes a string and calculates a SHA1 hash
106 if s == nil then return nil end
107 if #s == 0 then return nil end
108 if type(s) == 'string' then
109 local utils = require('utils')
110 --local asc = utils.ConvertHexToAscii(s)
111 local hash = core.sha1(s)
116 -- Takes a hex string and calculates a SHA1 hash
117 Sha1Hex = function(s)
118 if s == nil then return nil end
119 if #s == 0 then return nil end
120 if type(s) == 'string' then
121 local utils = require('utils')
122 local asc = utils.ConvertHexToAscii(s)
123 local hash = core.sha1(asc)
130 -- input parameter is a string
131 -- Swaps the endianess and returns a number,
132 -- IE: 'cd7a' -> '7acd' -> 0x7acd
133 SwapEndianness = function(s, len)
134 if s == nil then return nil end
135 if #s == 0 then return '' end
136 if type(s) ~= 'string' then return nil end
140 local t = s:sub(3,4)..s:sub(1,2)
141 retval = tonumber(t,16)
142 elseif len == 24 then
143 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
144 retval = tonumber(t,16)
145 elseif len == 32 then
146 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
147 retval = tonumber(t,16)
152 -- input parameter is a string
153 -- Swaps the endianess and returns a string,
154 -- IE: 'cd7a' -> '7acd' -> 0x7acd
155 SwapEndiannessStr = function(s, len)
156 if s == nil then return nil end
157 if #s == 0 then return '' end
158 if type(s) ~= 'string' then return nil end
162 retval = s:sub(3,4)..s:sub(1,2)
163 elseif len == 24 then
164 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
165 elseif len == 32 then
166 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
170 ------------ CONVERSIONS
173 -- Converts DECIMAL to HEX
174 ConvertDecToHex = function(IN)
175 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
178 IN , D = math.floor(IN/B), math.modf(IN,B)+1
179 OUT = string.sub(K,D,D)..OUT
184 -- Convert Byte array to string of hex
185 ConvertBytesToHex = function(bytes)
190 for i = 1, #(bytes) do
191 s[i] = string.format("%02X",bytes[i])
193 return table.concat(s)
195 -- Convert byte array to string with ascii
196 ConvertBytesToAscii = function(bytes)
201 for i = 1, #(bytes) do
202 s[i] = string.char(bytes[i])
204 return table.concat(s)
206 ConvertHexToBytes = function(s)
208 if s == nil then return t end
209 if #s == 0 then return t end
210 for k in s:gmatch"(%x%x)" do
211 table.insert(t,tonumber(k,16))
215 ConvertAsciiToBytes = function(s, reverse)
217 if s == nil then return t end
218 if #s == 0 then return t end
220 for k in s:gmatch"(.)" do
221 table.insert(t, string.byte(k))
231 table.insert(rev, t[i] )
237 ConvertHexToAscii = function(s)
239 if s == nil then return t end
240 if #s == 0 then return t end
241 for k in s:gmatch"(%x%x)" do
242 table.insert(t, string.char(tonumber(k,16)))
244 return table.concat(t)
247 Chars2num = function(s)
248 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
251 -- use length of string to determine 8,16,32,64 bits
252 bytes_to_int = function(str,endian,signed)
253 local t={str:byte(1,-1)}
254 if endian=="big" then --reverse bytes
266 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.
271 -- function convertStringToBytes(str)
273 -- local strLength = string.len(str)
274 -- for i=1,strLength do
275 -- table.insert(bytes, string.byte(str, i))
281 -- function convertBytesToString(bytes)
282 -- local bytesLength = table.getn(bytes)
284 -- for i=1,bytesLength do
285 -- str = str .. string.char(bytes[i])
291 -- function convertHexStringToBytes(str)
293 -- local strLength = string.len(str)
294 -- for k=2,strLength,2 do
295 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
296 -- table.insert(bytes, hex.to_dec(hexString))
302 -- function convertBytesToHexString(bytes)
304 -- local bytesLength = table.getn(bytes)
305 -- for i=1,bytesLength do
306 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
307 -- if string.len(hexString) == 1 then
308 -- hexString = "0" .. hexString
310 -- str = str .. hexString