]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/lualibs/utils.lua
FIX: introduced a bug in luascripts when adding the "safe ascii chars" to ConvertHex...
[proxmark3-svn] / client / lualibs / utils.lua
index 7db57f333717584cc4b67c8da8419bd05107f9dd..b86b016bdbeec94eb6e4126e600464993d9d000c 100644 (file)
@@ -37,9 +37,7 @@ local Utils =
        ------------ FILE READING\r
        ReadDumpFile = function (filename)\r
        \r
-               if filename == nil then \r
-                       return nil, 'Filename is empty'\r
-               end\r
+               filename = filename or 'dumpdata.bin'\r
                if #filename == 0 then\r
                        return nil, 'Filename length is zero'\r
                end\r
@@ -55,6 +53,34 @@ local Utils =
                return hex\r
        end,\r
        \r
+       ------------ FILE WRITING (EML)\r
+       --- Writes an eml-file.\r
+       -- @param uid - the uid of the tag. Used in filename\r
+       -- @param blockData. Assumed to be on the format {'\0\1\2\3,'\b\e\e\f' ..., \r
+       -- that is, blockData[row] contains a string with the actual data, not ascii hex representation \r
+       -- return filename if all went well, \r
+       -- @reurn nil, error message if unsuccessfulls  \r
+       WriteDumpFile = function(uid, blockData)\r
+               local destination = string.format("%s.eml", uid)\r
+               local file = io.open(destination, "w")\r
+               if file == nil then \r
+                       return nil, string.format("Could not write to file %s", destination)\r
+               end\r
+               local rowlen = string.len(blockData[1])\r
+\r
+               for i,block in ipairs(blockData) do\r
+                       if rowlen ~= string.len(block) then\r
+                               prlog(string.format("WARNING: Dumpdata seems corrupted, line %d was not the same length as line 1",i))\r
+                       end\r
+\r
+                       local formatString = string.format("H%d", string.len(block))\r
+                       local _,hex = bin.unpack(formatString,block)\r
+                       file:write(hex.."\n")\r
+               end\r
+               file:close()    \r
+               return destination\r
+       end,\r
+       \r
        ------------ string split function\r
        Split = function( inSplitPattern, outResults )\r
                if not outResults then\r
@@ -71,6 +97,32 @@ local Utils =
                return outResults\r
        end,\r
        \r
+       ----ISO14443-B CRC\r
+       Crc14b = function(s)\r
+               if s == nil then return nil end\r
+               if #s == 0 then return nil end\r
+               if  type(s) == 'string' then\r
+                       local utils = require('utils')\r
+                       return utils.ConvertAsciiToHex(\r
+                                                       core.iso14443b_crc(s)\r
+                                                       )\r
+               end\r
+               return nil              \r
+       end,\r
+       \r
+       ------------ CRC-8 Legic checksums\r
+       -- Takes a hex string and calculates a crc8\r
+       Crc8Legic = function(s)\r
+               if s == nil then return nil end\r
+               if #s == 0 then return nil end\r
+               if  type(s) == 'string' then\r
+                       local utils = require('utils')\r
+                       local asc = utils.ConvertHexToAscii(s)\r
+                       local hash = core.crc8legic(asc)\r
+                       return hash\r
+               end\r
+               return nil\r
+       end,\r
        \r
        ------------ CRC-16 ccitt checksums\r
        -- Takes a hex string and calculates a crc16\r
@@ -86,6 +138,7 @@ local Utils =
                return nil\r
        end,\r
        \r
+       \r
        ------------ CRC-64 ecma checksums\r
        -- Takes a hex string and calculates a crc64 ecma\r
        Crc64 = function(s)\r
@@ -183,20 +236,18 @@ local Utils =
        ---\r
        -- Convert Byte array to string of hex\r
        ConvertBytesToHex = function(bytes)\r
-               if #bytes == 0 then\r
-                       return ''\r
-               end\r
+               if bytes == nil then return '' end\r
+               if #bytes == 0 then return '' end\r
                local s={}\r
-               for i = 1, #(bytes) do\r
+               for i = 1, #bytes do\r
                        s[i] = string.format("%02X",bytes[i]) \r
                end\r
                return table.concat(s)\r
        end,    \r
        -- Convert byte array to string with ascii\r
     ConvertBytesToAscii = function(bytes)\r
-               if #bytes == 0 then\r
-                       return ''\r
-               end\r
+               if bytes == nil then return '' end\r
+               if #bytes == 0 then return '' end\r
                local s={}\r
                for i = 1, #(bytes) do\r
                        s[i] = string.char(bytes[i]) \r
@@ -234,14 +285,32 @@ local Utils =
                return rev\r
        end,\r
        \r
-       ConvertHexToAscii = function(s)\r
+       ConvertHexToAscii = function(s, useSafechars)\r
+               if s == nil then return '' end\r
+               if #s == 0 then return '' end\r
                local t={}\r
-               if s == nil then return t end\r
-               if #s == 0 then return t end\r
                for k in s:gmatch"(%x%x)" do\r
-                       table.insert(t, string.char(tonumber(k,16)))\r
+\r
+                       local n = tonumber(k,16)                \r
+                       local c \r
+                       if useSafechars and ( (n < 32) or (n == 127) ) then\r
+                               c = '.';\r
+                       else\r
+                               c = string.char(n)\r
+                       end\r
+                       table.insert(t,c)\r
+               end\r
+               return table.concat(t)  \r
+       end,\r
+       \r
+       ConvertAsciiToHex = function(s)         \r
+               if s == nil then return '' end\r
+               if #s == 0 then return '' end\r
+               local t={}\r
+               for k in s:gmatch"(.)" do\r
+                       table.insert(t, string.format("%02X", string.byte(k)))\r
                end\r
-               return  table.concat(t) \r
+               return table.concat(t)\r
        end,\r
        \r
        Chars2num = function(s)\r
@@ -268,6 +337,15 @@ local Utils =
                return n\r
        end,\r
        \r
+       -- a simple implementation of a sleep command. Thanks to Mosci\r
+       -- takes number of seconds to sleep\r
+       Sleep = function(n)\r
+               local clock = os.clock\r
+               local t0 = clock()\r
+               while clock() - t0 <= n do end\r
+               return nil      \r
+       end,\r
+       \r
        -- function convertStringToBytes(str)\r
        -- local bytes = {}\r
        -- local strLength = string.len(str)\r
Impressum, Datenschutz