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 ------------ FILE WRITING (EML)
57 --- Writes an eml-file.
58 -- @param uid - the uid of the tag. Used in filename
59 -- @param blockData. Assumed to be on the format {'\0\1\2\3,'\b\e\e\f' ...,
60 -- that is, blockData[row] contains a string with the actual data, not ascii hex representation
61 -- return filename if all went well,
62 -- @reurn nil, error message if unsuccessfulls
63 WriteDumpFile = function(uid, blockData)
64 local destination = string.format("%s.eml", uid)
65 local file = io.open(destination, "w")
67 return nil, string.format("Could not write to file %s", destination)
69 local rowlen = string.len(blockData[1])
71 for i,block in ipairs(blockData) do
72 if rowlen ~= string.len(block) then
73 prlog(string.format("WARNING: Dumpdata seems corrupted, line %d was not the same length as line 1",i))
76 local formatString = string.format("H%d", string.len(block))
77 local _,hex = bin.unpack(formatString,block)
84 ------------ string split function
85 Split = function( inSplitPattern, outResults )
86 if not outResults then
90 local splitStart, splitEnd = string.find( self, inSplitPattern, start )
92 table.insert( outResults, string.sub( self, start, splitStart-1 ) )
94 splitStart, splitEnd = string.find( self, inSplitPattern, start )
96 table.insert( outResults, string.sub( self, start ) )
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 return utils.ConvertAsciiToHex(
107 core.iso14443b_crc(s)
113 ------------ CRC-8 Legic checksums
114 -- Takes a hex string and calculates a crc8
115 Crc8Legic = function(s)
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.crc8legic(asc)
127 ------------ CRC-16 ccitt checksums
128 -- Takes a hex string and calculates a crc16
130 if s == nil then return nil end
131 if #s == 0 then return nil end
132 if type(s) == 'string' then
133 local utils = require('utils')
134 local asc = utils.ConvertHexToAscii(s)
135 local hash = core.crc16(asc)
142 ------------ CRC-64 ecma checksums
143 -- Takes a hex string and calculates a crc64 ecma
145 if s == nil then return nil end
146 if #s == 0 then return nil end
147 if type(s) == 'string' then
148 local utils = require('utils')
149 local asc = utils.ConvertHexToAscii(s)
150 local hash = core.crc64(asc)
156 ------------ SHA1 hash
157 -- Takes a string and calculates a SHA1 hash
159 if s == nil then return nil end
160 if #s == 0 then return nil end
161 if type(s) == 'string' then
162 local utils = require('utils')
163 --local asc = utils.ConvertHexToAscii(s)
164 local hash = core.sha1(s)
169 -- Takes a hex string and calculates a SHA1 hash
170 Sha1Hex = function(s)
171 if s == nil then return nil end
172 if #s == 0 then return nil end
173 if type(s) == 'string' then
174 local utils = require('utils')
175 local asc = utils.ConvertHexToAscii(s)
176 local hash = core.sha1(asc)
183 -- input parameter is a string
184 -- Swaps the endianess and returns a number,
185 -- IE: 'cd7a' -> '7acd' -> 0x7acd
186 SwapEndianness = function(s, len)
187 if s == nil then return nil end
188 if #s == 0 then return '' end
189 if type(s) ~= 'string' then return nil end
193 local t = s:sub(3,4)..s:sub(1,2)
194 retval = tonumber(t,16)
195 elseif len == 24 then
196 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
197 retval = tonumber(t,16)
198 elseif len == 32 then
199 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
200 retval = tonumber(t,16)
205 -- input parameter is a string
206 -- Swaps the endianess and returns a string,
207 -- IE: 'cd7a' -> '7acd' -> 0x7acd
208 SwapEndiannessStr = function(s, len)
209 if s == nil then return nil end
210 if #s == 0 then return '' end
211 if type(s) ~= 'string' then return nil end
215 retval = s:sub(3,4)..s:sub(1,2)
216 elseif len == 24 then
217 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
218 elseif len == 32 then
219 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
223 ------------ CONVERSIONS
226 -- Converts DECIMAL to HEX
227 ConvertDecToHex = function(IN)
228 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
231 IN , D = math.floor(IN/B), math.modf(IN,B)+1
232 OUT = string.sub(K,D,D)..OUT
237 -- Convert Byte array to string of hex
238 ConvertBytesToHex = function(bytes)
239 if bytes == nil then return '' end
240 if #bytes == 0 then return '' end
243 s[i] = string.format("%02X",bytes[i])
245 return table.concat(s)
247 -- Convert byte array to string with ascii
248 ConvertBytesToAscii = function(bytes)
249 if bytes == nil then return '' end
250 if #bytes == 0 then return '' end
252 for i = 1, #(bytes) do
253 s[i] = string.char(bytes[i])
255 return table.concat(s)
257 ConvertHexToBytes = function(s)
259 if s == nil then return t end
260 if #s == 0 then return t end
261 for k in s:gmatch"(%x%x)" do
262 table.insert(t,tonumber(k,16))
266 ConvertAsciiToBytes = function(s, reverse)
268 if s == nil then return t end
269 if #s == 0 then return t end
271 for k in s:gmatch"(.)" do
272 table.insert(t, string.byte(k))
282 table.insert(rev, t[i] )
288 ConvertHexToAscii = function(s)
289 if s == nil then return '' end
290 if #s == 0 then return '' end
292 for k in s:gmatch"(%x%x)" do
293 local n = tonumber(k,16)
295 if (n < 32) or (n == 127) then
302 return table.concat(t)
305 ConvertAsciiToHex = function(s)
306 if s == nil then return '' end
307 if #s == 0 then return '' end
309 for k in s:gmatch"(.)" do
310 table.insert(t, string.format("%02X", string.byte(k)))
312 return table.concat(t)
315 Chars2num = function(s)
316 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
319 -- use length of string to determine 8,16,32,64 bits
320 bytes_to_int = function(str,endian,signed)
321 local t={str:byte(1,-1)}
322 if endian=="big" then --reverse bytes
334 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.
339 -- a simple implementation of a sleep command. Thanks to Mosci
340 -- takes number of seconds to sleep
342 local clock = os.clock
344 while clock() - t0 <= n do end
348 -- function convertStringToBytes(str)
350 -- local strLength = string.len(str)
351 -- for i=1,strLength do
352 -- table.insert(bytes, string.byte(str, i))
358 -- function convertBytesToString(bytes)
359 -- local bytesLength = table.getn(bytes)
361 -- for i=1,bytesLength do
362 -- str = str .. string.char(bytes[i])
368 -- function convertHexStringToBytes(str)
370 -- local strLength = string.len(str)
371 -- for k=2,strLength,2 do
372 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
373 -- table.insert(bytes, hex.to_dec(hexString))
379 -- function convertBytesToHexString(bytes)
381 -- local bytesLength = table.getn(bytes)
382 -- for i=1,bytesLength do
383 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
384 -- if string.len(hexString) == 1 then
385 -- hexString = "0" .. hexString
387 -- str = str .. hexString