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 -- input parameter is a string
112 -- Swaps the endianess and returns a string,
113 -- IE: 'cd7a' -> '7acd' -> 0x7acd
114 SwapEndiannessStr = function(s, len)
115 if s == nil then return nil end
116 if #s == 0 then return '' end
117 if type(s) ~= 'string' then return nil end
121 retval = s:sub(3,4)..s:sub(1,2)
122 elseif len == 24 then
123 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
124 elseif len == 32 then
125 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
129 ------------ CONVERSIONS
132 -- Converts DECIMAL to HEX
133 ConvertDecToHex = function(IN)
134 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
137 IN , D = math.floor(IN/B), math.modf(IN,B)+1
138 OUT = string.sub(K,D,D)..OUT
143 -- Convert Byte array to string of hex
144 ConvertBytesToHex = function(bytes)
149 for i = 1, #(bytes) do
150 s[i] = string.format("%02X",bytes[i])
152 return table.concat(s)
154 -- Convert byte array to string with ascii
155 ConvertBytesToAscii = function(bytes)
160 for i = 1, #(bytes) do
161 s[i] = string.char(bytes[i])
163 return table.concat(s)
165 ConvertHexToBytes = function(s)
167 if s == nil then return t end
168 if #s == 0 then return t end
169 for k in s:gmatch"(%x%x)" do
170 table.insert(t,tonumber(k,16))
174 ConvertAsciiToBytes = function(s)
176 if s == nil then return t end
177 if #s == 0 then return t end
179 for k in s:gmatch"(.)" do
180 table.insert(t, string.byte(k))
184 ConvertHexToAscii = function(s)
186 if s == nil then return t end
187 if #s == 0 then return t end
188 for k in s:gmatch"(%x%x)" do
189 table.insert(t, string.char(tonumber(k,16)))
191 return table.concat(t)
194 Chars2num = function(s)
195 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
198 -- use length of string to determine 8,16,32,64 bits
199 bytes_to_int = function(str,endian,signed)
200 local t={str:byte(1,-1)}
201 if endian=="big" then --reverse bytes
213 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.
218 -- function convertStringToBytes(str)
220 -- local strLength = string.len(str)
221 -- for i=1,strLength do
222 -- table.insert(bytes, string.byte(str, i))
228 -- function convertBytesToString(bytes)
229 -- local bytesLength = table.getn(bytes)
231 -- for i=1,bytesLength do
232 -- str = str .. string.char(bytes[i])
238 -- function convertHexStringToBytes(str)
240 -- local strLength = string.len(str)
241 -- for k=2,strLength,2 do
242 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
243 -- table.insert(bytes, hex.to_dec(hexString))
249 -- function convertBytesToHexString(bytes)
251 -- local bytesLength = table.getn(bytes)
252 -- for i=1,bytesLength do
253 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
254 -- if string.len(hexString) == 1 then
255 -- hexString = "0" .. hexString
257 -- str = str .. hexString