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 ) )
74 if s == nil then return nil end
75 if #s == 0 then return nil end
76 if type(s) == 'string' then
77 local utils = require('utils')
78 return utils.ConvertAsciiToHex(
85 ------------ CRC-8 Legic checksums
86 -- Takes a hex string and calculates a crc8
87 Crc8Legic = function(s)
88 if s == nil then return nil end
89 if #s == 0 then return nil end
90 if type(s) == 'string' then
91 local utils = require('utils')
92 local asc = utils.ConvertHexToAscii(s)
93 local hash = core.crc8legic(asc)
99 ------------ CRC-16 ccitt checksums
100 -- Takes a hex string and calculates a crc16
102 if s == nil then return nil end
103 if #s == 0 then return nil end
104 if type(s) == 'string' then
105 local utils = require('utils')
106 local asc = utils.ConvertHexToAscii(s)
107 local hash = core.crc16(asc)
114 ------------ CRC-64 ecma checksums
115 -- Takes a hex string and calculates a crc64 ecma
117 if s == nil then return nil end
118 if #s == 0 then return nil end
119 if type(s) == 'string' then
120 local utils = require('utils')
121 local asc = utils.ConvertHexToAscii(s)
122 local hash = core.crc64(asc)
128 ------------ SHA1 hash
129 -- Takes a string and calculates a SHA1 hash
131 if s == nil then return nil end
132 if #s == 0 then return nil end
133 if type(s) == 'string' then
134 local utils = require('utils')
135 --local asc = utils.ConvertHexToAscii(s)
136 local hash = core.sha1(s)
141 -- Takes a hex string and calculates a SHA1 hash
142 Sha1Hex = function(s)
143 if s == nil then return nil end
144 if #s == 0 then return nil end
145 if type(s) == 'string' then
146 local utils = require('utils')
147 local asc = utils.ConvertHexToAscii(s)
148 local hash = core.sha1(asc)
155 -- input parameter is a string
156 -- Swaps the endianess and returns a number,
157 -- IE: 'cd7a' -> '7acd' -> 0x7acd
158 SwapEndianness = function(s, len)
159 if s == nil then return nil end
160 if #s == 0 then return '' end
161 if type(s) ~= 'string' then return nil end
165 local t = s:sub(3,4)..s:sub(1,2)
166 retval = tonumber(t,16)
167 elseif len == 24 then
168 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
169 retval = tonumber(t,16)
170 elseif len == 32 then
171 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
172 retval = tonumber(t,16)
177 -- input parameter is a string
178 -- Swaps the endianess and returns a string,
179 -- IE: 'cd7a' -> '7acd' -> 0x7acd
180 SwapEndiannessStr = function(s, len)
181 if s == nil then return nil end
182 if #s == 0 then return '' end
183 if type(s) ~= 'string' then return nil end
187 retval = s:sub(3,4)..s:sub(1,2)
188 elseif len == 24 then
189 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
190 elseif len == 32 then
191 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
195 ------------ CONVERSIONS
198 -- Converts DECIMAL to HEX
199 ConvertDecToHex = function(IN)
200 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
203 IN , D = math.floor(IN/B), math.modf(IN,B)+1
204 OUT = string.sub(K,D,D)..OUT
209 -- Convert Byte array to string of hex
210 ConvertBytesToHex = function(bytes)
211 if bytes == nil then return '' end
212 if #bytes == 0 then return '' end
215 s[i] = string.format("%02X",bytes[i])
217 return table.concat(s)
219 -- Convert byte array to string with ascii
220 ConvertBytesToAscii = function(bytes)
221 if bytes == nil then return '' end
222 if #bytes == 0 then return '' end
224 for i = 1, #(bytes) do
225 s[i] = string.char(bytes[i])
227 return table.concat(s)
229 ConvertHexToBytes = function(s)
231 if s == nil then return t end
232 if #s == 0 then return t end
233 for k in s:gmatch"(%x%x)" do
234 table.insert(t,tonumber(k,16))
238 ConvertAsciiToBytes = function(s, reverse)
240 if s == nil then return t end
241 if #s == 0 then return t end
243 for k in s:gmatch"(.)" do
244 table.insert(t, string.byte(k))
254 table.insert(rev, t[i] )
260 ConvertHexToAscii = function(s)
261 if s == nil then return '' end
262 if #s == 0 then return '' end
264 for k in s:gmatch"(%x%x)" do
265 table.insert(t, string.char(tonumber(k,16)))
267 return table.concat(t)
270 ConvertAsciiToHex = function(s)
271 if s == nil then return '' end
272 if #s == 0 then return '' end
274 for k in s:gmatch"(.)" do
275 table.insert(t, string.format("%02X", string.byte(k)))
277 return table.concat(t)
280 Chars2num = function(s)
281 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
284 -- use length of string to determine 8,16,32,64 bits
285 bytes_to_int = function(str,endian,signed)
286 local t={str:byte(1,-1)}
287 if endian=="big" then --reverse bytes
299 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.
304 -- a simple implementation of a sleep command. Thanks to Mosci
305 -- takes number of seconds to sleep
307 local clock = os.clock
309 while clock() - t0 <= n do end
313 -- function convertStringToBytes(str)
315 -- local strLength = string.len(str)
316 -- for i=1,strLength do
317 -- table.insert(bytes, string.byte(str, i))
323 -- function convertBytesToString(bytes)
324 -- local bytesLength = table.getn(bytes)
326 -- for i=1,bytesLength do
327 -- str = str .. string.char(bytes[i])
333 -- function convertHexStringToBytes(str)
335 -- local strLength = string.len(str)
336 -- for k=2,strLength,2 do
337 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
338 -- table.insert(bytes, hex.to_dec(hexString))
344 -- function convertBytesToHexString(bytes)
346 -- local bytesLength = table.getn(bytes)
347 -- for i=1,bytesLength do
348 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
349 -- if string.len(hexString) == 1 then
350 -- hexString = "0" .. hexString
352 -- str = str .. hexString