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 local ascii = utils.ConvertHexToAscii(s)
79 local hashed = core.iso14443b_crc(ascii)
80 return utils.ConvertAsciiToHex(hashed)
85 ------------ CRC-16 ccitt checksums
86 -- Takes a hex string and calculates a crc16
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.crc16(asc)
99 ------------ CRC-64 ecma checksums
100 -- Takes a hex string and calculates a crc64 ecma
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.crc64(asc)
113 ------------ SHA1 hash
114 -- Takes a string and calculates a SHA1 hash
116 if s == nil then return nil end
117 if #s == 0 then return nil end
118 if type(s) == 'string' then
119 local utils = require('utils')
120 --local asc = utils.ConvertHexToAscii(s)
121 local hash = core.sha1(s)
126 -- Takes a hex string and calculates a SHA1 hash
127 Sha1Hex = function(s)
128 if s == nil then return nil end
129 if #s == 0 then return nil end
130 if type(s) == 'string' then
131 local utils = require('utils')
132 local asc = utils.ConvertHexToAscii(s)
133 local hash = core.sha1(asc)
140 -- input parameter is a string
141 -- Swaps the endianess and returns a number,
142 -- IE: 'cd7a' -> '7acd' -> 0x7acd
143 SwapEndianness = function(s, len)
144 if s == nil then return nil end
145 if #s == 0 then return '' end
146 if type(s) ~= 'string' then return nil end
150 local t = s:sub(3,4)..s:sub(1,2)
151 retval = tonumber(t,16)
152 elseif len == 24 then
153 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
154 retval = tonumber(t,16)
155 elseif len == 32 then
156 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
157 retval = tonumber(t,16)
162 -- input parameter is a string
163 -- Swaps the endianess and returns a string,
164 -- IE: 'cd7a' -> '7acd' -> 0x7acd
165 SwapEndiannessStr = function(s, len)
166 if s == nil then return nil end
167 if #s == 0 then return '' end
168 if type(s) ~= 'string' then return nil end
172 retval = s:sub(3,4)..s:sub(1,2)
173 elseif len == 24 then
174 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
175 elseif len == 32 then
176 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
180 ------------ CONVERSIONS
183 -- Converts DECIMAL to HEX
184 ConvertDecToHex = function(IN)
185 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
188 IN , D = math.floor(IN/B), math.modf(IN,B)+1
189 OUT = string.sub(K,D,D)..OUT
194 -- Convert Byte array to string of hex
195 ConvertBytesToHex = function(bytes)
196 if bytes == nil then return '' end
197 if #bytes == 0 then return '' end
200 s[i] = string.format("%02X",bytes[i])
202 return table.concat(s)
204 -- Convert byte array to string with ascii
205 ConvertBytesToAscii = function(bytes)
206 if bytes == nil then return '' end
207 if #bytes == 0 then return '' end
209 for i = 1, #(bytes) do
210 s[i] = string.char(bytes[i])
212 return table.concat(s)
214 ConvertHexToBytes = function(s)
216 if s == nil then return t end
217 if #s == 0 then return t end
218 for k in s:gmatch"(%x%x)" do
219 table.insert(t,tonumber(k,16))
223 ConvertAsciiToBytes = function(s, reverse)
225 if s == nil then return t end
226 if #s == 0 then return t end
228 for k in s:gmatch"(.)" do
229 table.insert(t, string.byte(k))
239 table.insert(rev, t[i] )
245 ConvertHexToAscii = function(s)
246 if s == nil then return '' end
247 if #s == 0 then return '' end
249 for k in s:gmatch"(%x%x)" do
250 table.insert(t, string.char(tonumber(k,16)))
252 return table.concat(t)
255 ConvertAsciiToHex = function(s)
256 if s == nil then return '' end
257 if #s == 0 then return '' end
259 for k in s:gmatch"(.)" do
260 table.insert(t, string.format("%02X", string.byte(k)))
262 return table.concat(t)
265 Chars2num = function(s)
266 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
269 -- use length of string to determine 8,16,32,64 bits
270 bytes_to_int = function(str,endian,signed)
271 local t={str:byte(1,-1)}
272 if endian=="big" then --reverse bytes
284 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.
289 -- function convertStringToBytes(str)
291 -- local strLength = string.len(str)
292 -- for i=1,strLength do
293 -- table.insert(bytes, string.byte(str, i))
299 -- function convertBytesToString(bytes)
300 -- local bytesLength = table.getn(bytes)
302 -- for i=1,bytesLength do
303 -- str = str .. string.char(bytes[i])
309 -- function convertHexStringToBytes(str)
311 -- local strLength = string.len(str)
312 -- for k=2,strLength,2 do
313 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
314 -- table.insert(bytes, hex.to_dec(hexString))
320 -- function convertBytesToHexString(bytes)
322 -- local bytesLength = table.getn(bytes)
323 -- for i=1,bytesLength do
324 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
325 -- if string.len(hexString) == 1 then
326 -- hexString = "0" .. hexString
328 -- str = str .. hexString