X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/b69adb794594cc7ea43d3aa70aab091265195abf..41611deef90f78c2ee5d20d78e9bd1c7f64d964b:/client/lualibs/utils.lua diff --git a/client/lualibs/utils.lua b/client/lualibs/utils.lua index da179758..9dcb49c7 100644 --- a/client/lualibs/utils.lua +++ b/client/lualibs/utils.lua @@ -53,6 +53,34 @@ local Utils = return hex end, + ------------ FILE WRITING (EML) + --- Writes an eml-file. + -- @param uid - the uid of the tag. Used in filename + -- @param blockData. Assumed to be on the format {'\0\1\2\3,'\b\e\e\f' ..., + -- that is, blockData[row] contains a string with the actual data, not ascii hex representation + -- return filename if all went well, + -- @reurn nil, error message if unsuccessfulls + WriteDumpFile = function(uid, blockData) + local destination = string.format("%s.eml", uid) + local file = io.open(destination, "w") + if file == nil then + return nil, string.format("Could not write to file %s", destination) + end + local rowlen = string.len(blockData[1]) + + for i,block in ipairs(blockData) do + if rowlen ~= string.len(block) then + prlog(string.format("WARNING: Dumpdata seems corrupted, line %d was not the same length as line 1",i)) + end + + local formatString = string.format("H%d", string.len(block)) + local _,hex = bin.unpack(formatString,block) + file:write(hex.."\n") + end + file:close() + return destination + end, + ------------ string split function Split = function( inSplitPattern, outResults ) if not outResults then @@ -69,6 +97,32 @@ local Utils = return outResults end, + ----ISO14443-B CRC + Crc14b = function(s) + if s == nil then return nil end + if #s == 0 then return nil end + if type(s) == 'string' then + local utils = require('utils') + return utils.ConvertAsciiToHex( + core.iso14443b_crc(s) + ) + end + return nil + end, + + ------------ CRC-8 Legic checksums + -- Takes a hex string and calculates a crc8 + Crc8Legic = function(s) + if s == nil then return nil end + if #s == 0 then return nil end + if type(s) == 'string' then + local utils = require('utils') + local asc = utils.ConvertHexToAscii(s) + local hash = core.crc8legic(asc) + return hash + end + return nil + end, ------------ CRC-16 ccitt checksums -- Takes a hex string and calculates a crc16 @@ -84,6 +138,7 @@ local Utils = return nil end, + ------------ CRC-64 ecma checksums -- Takes a hex string and calculates a crc64 ecma Crc64 = function(s) @@ -181,20 +236,18 @@ local Utils = --- -- Convert Byte array to string of hex ConvertBytesToHex = function(bytes) - if #bytes == 0 then - return '' - end + if bytes == nil then return '' end + if #bytes == 0 then return '' end local s={} - for i = 1, #(bytes) do + for i = 1, #bytes do s[i] = string.format("%02X",bytes[i]) end return table.concat(s) end, -- Convert byte array to string with ascii ConvertBytesToAscii = function(bytes) - if #bytes == 0 then - return '' - end + if bytes == nil then return '' end + if #bytes == 0 then return '' end local s={} for i = 1, #(bytes) do s[i] = string.char(bytes[i]) @@ -233,13 +286,30 @@ local Utils = end, ConvertHexToAscii = function(s) + if s == nil then return '' end + if #s == 0 then return '' end local t={} - if s == nil then return t end - if #s == 0 then return t end for k in s:gmatch"(%x%x)" do - table.insert(t, string.char(tonumber(k,16))) + local n = tonumber(k,16) + local c + if (n < 32) or (n == 127) then + c = '.'; + else + c = string.char(n) + end + table.insert(t,c) end - return table.concat(t) + return table.concat(t) + end, + + ConvertAsciiToHex = function(s) + if s == nil then return '' end + if #s == 0 then return '' end + local t={} + for k in s:gmatch"(.)" do + table.insert(t, string.format("%02X", string.byte(k))) + end + return table.concat(t) end, Chars2num = function(s) @@ -266,6 +336,15 @@ local Utils = return n end, + -- a simple implementation of a sleep command. Thanks to Mosci + -- takes number of seconds to sleep + Sleep = function(n) + local clock = os.clock + local t0 = clock() + while clock() - t0 <= n do end + return nil + end, + -- function convertStringToBytes(str) -- local bytes = {} -- local strLength = string.len(str)