]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/lualibs/utils.lua
chg: more keys.
[proxmark3-svn] / client / lualibs / utils.lua
index 15b96ee50776eb5b7a40185b3edaa7f5a029cff3..9dcb49c7b53abbbf35785089d2de71b6a4dadffa 100644 (file)
@@ -33,41 +33,228 @@ local Utils =
 \r
                return answer\r
        end,\r
+       \r
+       ------------ FILE READING\r
+       ReadDumpFile = function (filename)\r
+       \r
+               filename = filename or 'dumpdata.bin'\r
+               if #filename == 0 then\r
+                       return nil, 'Filename length is zero'\r
+               end\r
+\r
+               infile = io.open(filename, "rb")\r
+               if infile == nil then \r
+                       return nil, string.format("Could not read file %s",filename)\r
+               end\r
+               local t = infile:read("*all")\r
+               len = string.len(t)\r
+               local  _,hex = bin.unpack(("H%d"):format(len),t)\r
+               io.close(infile)\r
+               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
+                       outResults = {}\r
+               end\r
+               local start = 1\r
+               local splitStart, splitEnd = string.find( self, inSplitPattern, start )\r
+               while splitStart do\r
+                       table.insert( outResults, string.sub( self, start, splitStart-1 ) )\r
+                       start = splitEnd + 1\r
+                       splitStart, splitEnd = string.find( self, inSplitPattern, start )\r
+               end\r
+               table.insert( outResults, string.sub( self, start ) )\r
+               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
+       Crc16 = 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.crc16(asc)\r
+                       return hash\r
+               end\r
+               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
+               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.crc64(asc)\r
+                       return hash\r
+               end\r
+               return nil\r
+       end,\r
+\r
+       ------------ SHA1 hash\r
+       -- Takes a string and calculates a SHA1 hash\r
+       Sha1 = 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.sha1(s)\r
+                       return hash\r
+               end\r
+               return nil\r
+       end,    \r
+       -- Takes a hex string and calculates a SHA1 hash\r
+       Sha1Hex = 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.sha1(asc)\r
+                       return hash\r
+               end\r
+               return nil\r
+       end,    \r
+\r
+       \r
+       -- input parameter is a string\r
+       -- Swaps the endianess and returns a number,  \r
+       -- IE:  'cd7a' -> '7acd'  -> 0x7acd\r
+       SwapEndianness = function(s, len)\r
+               if s == nil then return nil end\r
+               if #s == 0 then return '' end\r
+               if  type(s) ~= 'string' then return nil end\r
+               \r
+               local retval = 0\r
+               if len == 16 then\r
+                       local t = s:sub(3,4)..s:sub(1,2)\r
+                       retval = tonumber(t,16)\r
+               elseif len == 24 then\r
+                       local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
+                       retval = tonumber(t,16)\r
+               elseif len == 32 then\r
+                       local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
+                       retval = tonumber(t,16)\r
+               end\r
+               return retval\r
+       end,\r
+       \r
+       -- input parameter is a string\r
+       -- Swaps the endianess and returns a string,  \r
+       -- IE:  'cd7a' -> '7acd'  -> 0x7acd\r
+       SwapEndiannessStr = function(s, len)\r
+               if s == nil then return nil end\r
+               if #s == 0 then return '' end\r
+               if  type(s) ~= 'string' then return nil end\r
+               \r
+               local retval\r
+               if len == 16 then\r
+                       retval = s:sub(3,4)..s:sub(1,2)\r
+               elseif len == 24 then\r
+                       retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
+               elseif len == 32 then\r
+                       retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
+               end\r
+               return retval\r
+       end,    \r
+       ------------ CONVERSIONS\r
+       \r
        --\r
        -- Converts DECIMAL to HEX\r
-    ConvertDec2Hex = function(IN)\r
+    ConvertDecToHex = function(IN)\r
                local B,K,OUT,I,D=16,"0123456789ABCDEF","",0\r
                while IN>0 do\r
                        I=I+1\r
-                       IN,D=math.floor(IN/B),math.mod(IN,B)+1\r
-                       OUT=string.sub(K,D,D)..OUT\r
+                       IN , D = math.floor(IN/B), math.modf(IN,B)+1\r
+                       OUT = string.sub(K,D,D)..OUT\r
                end\r
                return OUT\r
        end,\r
        ---\r
        -- Convert Byte array to string of hex\r
-       ConvertBytes2HexString = function(bytes)\r
-               if #bytes == 0 then\r
-                       return ''\r
-               end\r
+       ConvertBytesToHex = function(bytes)\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
-    ConvertBytesToAsciiString = function(bytes)\r
-               if #bytes == 0 then\r
-                       return ''\r
-               end\r
+    ConvertBytesToAscii = function(bytes)\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
                end\r
                return table.concat(s)          \r
        end,     \r
-       ConvertHexStringToBytes = function(s)\r
+       ConvertHexToBytes = function(s)\r
                local t={}\r
                if s == nil then return t end\r
                if #s == 0 then return t end\r
@@ -76,24 +263,86 @@ local Utils =
                end\r
                return t\r
        end,\r
-       ConvertAsciiStringToBytes = function(s)\r
-               local t={}\r
+       ConvertAsciiToBytes = function(s, reverse)\r
+               local t = {}\r
                if s == nil then return t end\r
                if #s == 0 then return t end\r
                \r
                for k in s:gmatch"(.)" do\r
                        table.insert(t, string.byte(k))\r
                end\r
-               return t\r
+               \r
+               if not reverse then\r
+                       return t\r
+               end\r
+       \r
+               local rev = {}\r
+               if reverse then\r
+                       for i = #t, 1,-1 do\r
+                               table.insert(rev, t[i] )\r
+                       end\r
+               end\r
+               return rev\r
        end,\r
+       \r
        ConvertHexToAscii = function(s)\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
+                       local n = tonumber(k,16)\r
+                       local c \r
+                       if  (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
+       end,\r
+       \r
+       Chars2num = function(s)\r
+        return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))\r
+       end,\r
+       \r
+       -- use length of string to determine 8,16,32,64 bits\r
+       bytes_to_int = function(str,endian,signed) \r
+               local t={str:byte(1,-1)}\r
+               if endian=="big" then --reverse bytes\r
+                       local tt={}\r
+                       for k=1,#t do\r
+                               tt[#t-k+1]=t[k]\r
+                       end\r
+                       t=tt\r
                end\r
-               return  table.concat(t) \r
+               local n=0\r
+               for k=1,#t do\r
+                       n=n+t[k]*2^((k-1)*8)\r
+               end\r
+               if signed then\r
+                       n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.\r
+               end\r
+               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
Impressum, Datenschutz