mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-14 02:27:26 -07:00
CHG - some lua functions in utils.lua
This commit is contained in:
parent
47cbb2d418
commit
f91f0ebb35
3 changed files with 101 additions and 64 deletions
|
@ -33,9 +33,86 @@ local Utils =
|
|||
|
||||
return answer
|
||||
end,
|
||||
|
||||
------------ FILE READING
|
||||
ReadDumpFile = function (filename)
|
||||
|
||||
if filename == nil then
|
||||
return nil, 'Filename is empty'
|
||||
end
|
||||
if #filename == 0 then
|
||||
return nil, 'Filename length is zero'
|
||||
end
|
||||
|
||||
infile = io.open(filename, "rb")
|
||||
if infile == nil then
|
||||
return nil, string.format("Could not read file %s",filename)
|
||||
end
|
||||
local t = infile:read("*all")
|
||||
len = string.len(t)
|
||||
local _,hex = bin.unpack(("H%d"):format(len),t)
|
||||
io.close(infile)
|
||||
return hex
|
||||
end,
|
||||
|
||||
------------ string split function
|
||||
Split = function( inSplitPattern, outResults )
|
||||
if not outResults then
|
||||
outResults = {}
|
||||
end
|
||||
local start = 1
|
||||
local splitStart, splitEnd = string.find( self, inSplitPattern, start )
|
||||
while splitStart do
|
||||
table.insert( outResults, string.sub( self, start, splitStart-1 ) )
|
||||
start = splitEnd + 1
|
||||
splitStart, splitEnd = string.find( self, inSplitPattern, start )
|
||||
end
|
||||
table.insert( outResults, string.sub( self, start ) )
|
||||
return outResults
|
||||
end,
|
||||
|
||||
------------ CRC-16 ccitt checksums
|
||||
|
||||
-- Takes a hex string and calculates a crc16
|
||||
Crc16 = 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.crc16(asc)
|
||||
return hash
|
||||
end
|
||||
return nil
|
||||
end,
|
||||
|
||||
-- input parameter is a string
|
||||
-- Swaps the endianess and returns a number,
|
||||
-- IE: 'cd7a' -> '7acd' -> 0x7acd
|
||||
SwapEndianness = function(s, len)
|
||||
if s == nil then return nil end
|
||||
if #s == 0 then return '' end
|
||||
if type(s) ~= 'string' then return nil end
|
||||
|
||||
local retval = 0
|
||||
if len == 16 then
|
||||
local t = s:sub(3,4)..s:sub(1,2)
|
||||
retval = tonumber(t,16)
|
||||
elseif len == 24 then
|
||||
local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
|
||||
retval = tonumber(t,16)
|
||||
elseif len == 32 then
|
||||
local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
|
||||
retval = tonumber(t,16)
|
||||
end
|
||||
return retval
|
||||
end,
|
||||
|
||||
------------ CONVERSIONS
|
||||
|
||||
--
|
||||
-- Converts DECIMAL to HEX
|
||||
ConvertDec2Hex = function(IN)
|
||||
ConvertDecToHex = function(IN)
|
||||
local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
|
||||
while IN>0 do
|
||||
I=I+1
|
||||
|
@ -46,7 +123,7 @@ local Utils =
|
|||
end,
|
||||
---
|
||||
-- Convert Byte array to string of hex
|
||||
ConvertBytes2HexString = function(bytes)
|
||||
ConvertBytesToHex = function(bytes)
|
||||
if #bytes == 0 then
|
||||
return ''
|
||||
end
|
||||
|
@ -57,7 +134,7 @@ local Utils =
|
|||
return table.concat(s)
|
||||
end,
|
||||
-- Convert byte array to string with ascii
|
||||
ConvertBytesToAsciiString = function(bytes)
|
||||
ConvertBytesToAscii = function(bytes)
|
||||
if #bytes == 0 then
|
||||
return ''
|
||||
end
|
||||
|
@ -67,7 +144,7 @@ local Utils =
|
|||
end
|
||||
return table.concat(s)
|
||||
end,
|
||||
ConvertHexStringToBytes = function(s)
|
||||
ConvertHexToBytes = function(s)
|
||||
local t={}
|
||||
if s == nil then return t end
|
||||
if #s == 0 then return t end
|
||||
|
@ -76,7 +153,7 @@ local Utils =
|
|||
end
|
||||
return t
|
||||
end,
|
||||
ConvertAsciiStringToBytes = function(s)
|
||||
ConvertAsciiToBytes = function(s)
|
||||
local t={}
|
||||
if s == nil then return t end
|
||||
if #s == 0 then return t end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue