]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/lualibs/utils.lua
Minor refactoring
[proxmark3-svn] / client / lualibs / utils.lua
index 3d27d5b68350d1b913536c56c3f3befff15946d3..e84f70ada526e99b36b69f5077f5be8208e2c587 100644 (file)
@@ -33,9 +33,86 @@ local Utils =
 \r
                return answer\r
        end,\r
+       \r
+       ------------ FILE READING\r
+       ReadDumpFile = function (filename)\r
+       \r
+               if filename == nil then \r
+                       return nil, 'Filename is empty'\r
+               end\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
+       ------------ 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
+       ------------ CRC-16 ccitt checksums\r
+       \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
+       -- 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
+       ------------ 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
@@ -46,12 +123,100 @@ local Utils =
        end,\r
        ---\r
        -- Convert Byte array to string of hex\r
-       ConvertBytes2String = function(bytes)\r
-               s = {}\r
+       ConvertBytesToHex = function(bytes)\r
+               if #bytes == 0 then\r
+                       return ''\r
+               end\r
+               local s={}\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
+               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
+       ConvertHexToBytes = function(s)\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,tonumber(k,16))\r
+               end\r
+               return t\r
+       end,\r
+       ConvertAsciiToBytes = function(s)\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
+       end,\r
+       ConvertHexToAscii = function(s)\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
+               end\r
+               return  table.concat(t) \r
+       end,\r
+       \r
+       -- function convertStringToBytes(str)\r
+       -- local bytes = {}\r
+       -- local strLength = string.len(str)\r
+       -- for i=1,strLength do\r
+               -- table.insert(bytes, string.byte(str, i))\r
+       -- end\r
+\r
+       -- return bytes\r
+-- end\r
+\r
+-- function convertBytesToString(bytes)\r
+       -- local bytesLength = table.getn(bytes)\r
+       -- local str = ""\r
+       -- for i=1,bytesLength do\r
+               -- str = str .. string.char(bytes[i])\r
+       -- end\r
+\r
+       -- return str\r
+-- end\r
+\r
+-- function convertHexStringToBytes(str)\r
+       -- local bytes = {}\r
+       -- local strLength = string.len(str)\r
+       -- for k=2,strLength,2 do\r
+               -- local hexString = "0x" .. string.sub(str, (k - 1), k)\r
+               -- table.insert(bytes, hex.to_dec(hexString))\r
+       -- end\r
+\r
+       -- return bytes\r
+-- end\r
+\r
+-- function convertBytesToHexString(bytes)\r
+       -- local str = ""\r
+       -- local bytesLength = table.getn(bytes)\r
+       -- for i=1,bytesLength do\r
+               -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)\r
+               -- if string.len(hexString) == 1 then\r
+                       -- hexString = "0" .. hexString\r
+               -- end\r
+               -- str = str .. hexString\r
+       -- end\r
+\r
+       -- return str\r
+-- end\r
+\r
 }\r
 return Utils
\ No newline at end of file
Impressum, Datenschutz