This commit is contained in:
iceman1001 2019-05-07 23:34:05 +02:00
parent a59f2a1b60
commit 89e25a4d1d
2 changed files with 70 additions and 52 deletions

View file

@ -4,13 +4,17 @@ getopt = require('getopt')
bin = require('bin') bin = require('bin')
copyright = '' copyright = ''
version = ''
example = "script run dumptoemul-mfu -i dumpdata-foobar.bin"
author = "Martin Holst Swende \n @Marshmellow \n @iceman" author = "Martin Holst Swende \n @Marshmellow \n @iceman"
usage = "script run dumptoemul-mfu [-i <file>] [-o <file>]" version = 'v1.0.1'
desc =[[ desc =[[
This script takes a dumpfile from 'hf mfu dump' and converts it to a format that can be used This script takes a dumpfile from 'hf mfu dump' and converts it to a format that can be used
by the emulator by the emulator
]]
example = [[
script run dumptoemul-mfu -i dumpdata-foobar.bin
]]
usage = [[
script run dumptoemul-mfu [-i <file>] [-o <file>]
Arguments: Arguments:
-h This help -h This help
@ -18,13 +22,13 @@ Arguments:
-o <filename> Specifies the output file. If omitted, <uid>.eml is used. -o <filename> Specifies the output file. If omitted, <uid>.eml is used.
]] ]]
local DEBUG = false local DEBUG = false
--- ---
-- A debug printout-function -- A debug printout-function
local function dbg(args) local function dbg(args)
if not DEBUG then return end if not DEBUG then return end
if type(args) == 'table' then if type(args) == 'table' then
local i = 1 local i = 1
while result[i] do while result[i] do
@ -38,16 +42,20 @@ end
--- ---
-- This is only meant to be used when errors occur -- This is only meant to be used when errors occur
local function oops(err) local function oops(err)
print('ERROR: ',err) print('ERROR:', err)
return nil,err core.clearCommandBuffer()
return nil, err
end end
--- ---
-- Usage help -- Usage help
function help() local function help()
print(desc) print(copyright)
print(author) print(author)
print("Example usage") print(version)
print(desc)
print('Example usage')
print(example) print(example)
print(usage)
end end
local function convert_to_ascii(hexdata) local function convert_to_ascii(hexdata)
@ -64,38 +72,38 @@ local function convert_to_ascii(hexdata)
end end
local function readdump(infile) local function readdump(infile)
t = infile:read("*all") t = infile:read('*all')
len = string.len(t) len = string.len(t)
local len,hex = bin.unpack(("H%d"):format(len),t) local len,hex = bin.unpack(('H%d'):format(len),t)
return hex return hex
end end
local function convert_to_emulform(hexdata) local function convert_to_emulform(hexdata)
if string.len(hexdata) % 8 ~= 0 then if string.len(hexdata) % 8 ~= 0 then
return oops(("Bad data, length should be a multiple of 8 (was %d)"):format(string.len(hexdata))) return oops(('Bad data, length should be a multiple of 8 (was %d)'):format(string.len(hexdata)))
end end
local ascii,i = ""; local ascii,i = '';
for i = 1, string.len(hexdata), 8 do for i = 1, string.len(hexdata), 8 do
ascii = ascii..string.sub(hexdata, i, i+7).."\n" ascii = ascii..string.sub(hexdata, i, i+7)..'\n'
end end
return string.sub(ascii, 1, -2) return string.sub(ascii, 1, -2)
end end
local function main(args) local function main(args)
local input = "dumpdata.bin" local input = 'dumpdata.bin'
local output local output
for o, a in getopt.getopt(args, 'i:o:h') do for o, a in getopt.getopt(args, 'i:o:h') do
if o == "h" then return help() end if o == 'h' then return help() end
if o == "i" then input = a end if o == 'i' then input = a end
if o == "o" then output = a end if o == 'o' then output = a end
end end
-- Validate the parameters -- Validate the parameters
local infile = io.open(input, "rb") local infile = io.open(input, 'rb')
if infile == nil then if infile == nil then
return oops("Could not read file ", input) return oops('Could not read file ', input)
end end
local dumpdata = readdump(infile) local dumpdata = readdump(infile)
-- The hex-data is now in ascii-format, -- The hex-data is now in ascii-format,
@ -109,19 +117,19 @@ local function main(args)
-- 119,120 bcc0 -- 119,120 bcc0
-- 121--- UID last four bytes -- 121--- UID last four bytes
local uid = string.sub(dumpdata, 113, 113+5)..string.sub(dumpdata, 113+8, 113+8+7) local uid = string.sub(dumpdata, 113, 113+5)..string.sub(dumpdata, 113+8, 113+8+7)
output = output or (uid .. ".eml") output = output or (uid .. '.eml')
-- Format some linebreaks -- Format some linebreaks
dumpdata = convert_to_emulform(dumpdata) dumpdata = convert_to_emulform(dumpdata)
local outfile = io.open(output, "w") local outfile = io.open(output, 'w')
if outfile == nil then if outfile == nil then
return oops("Could not write to file ", output) return oops('Could not write to file ', output)
end end
outfile:write(dumpdata:lower()) outfile:write(dumpdata:lower())
io.close(outfile) io.close(outfile)
print(("Wrote an emulator-dump to the file %s"):format(output)) print(('Wrote an emulator-dump to the file %s'):format(output))
end end
--[[ --[[

View file

@ -3,12 +3,18 @@
getopt = require('getopt') getopt = require('getopt')
bin = require('bin') bin = require('bin')
example = "script run dumptoemul -i dumpdata-foobar.bin" copyright = ''
author = "Martin Holst Swende" author = 'Martin Holst Swende'
usage = "script run dumptoemul [-i <file>] [-o <file>]" version = 'v1.0.1'
desc =[[ desc = [[
This script takes a dumpfile from 'hf mf dump' and converts it to a format that can be used This script takes a dumpfile from 'hf mf dump' and converts it to a format that can be used
by the emulator by the emulator
]]
example = [[
script run dumptoemul -i dumpdata-foobar.bin
]]
usage = [[
script run dumptoemul [-i <file>] [-o <file>]
Arguments: Arguments:
-h This help -h This help
@ -16,6 +22,7 @@ Arguments:
-o <filename> Specifies the output file. If omitted, <uid>.eml is used. -o <filename> Specifies the output file. If omitted, <uid>.eml is used.
]] ]]
local DEBUG = false local DEBUG = false
------------------------------- -------------------------------
-- Some utilities -- Some utilities
@ -25,7 +32,6 @@ local DEBUG = false
-- A debug printout-function -- A debug printout-function
local function dbg(args) local function dbg(args)
if not DEBUG then return end if not DEBUG then return end
if type(args) == 'table' then if type(args) == 'table' then
local i = 1 local i = 1
while result[i] do while result[i] do
@ -39,83 +45,87 @@ end
--- ---
-- This is only meant to be used when errors occur -- This is only meant to be used when errors occur
local function oops(err) local function oops(err)
print('ERROR: ',err) print('ERROR:', err)
return nil,err core.clearCommandBuffer()
return nil, err
end end
--- ---
-- Usage help -- Usage help
function help() function help()
print(desc) print(copyright)
print(author) print(author)
print("Example usage") print(version)
print(desc)
print('Example usage')
print(example) print(example)
print(usage)
end end
local function convert_to_ascii(hexdata) local function convert_to_ascii(hexdata)
if string.len(hexdata) % 32 ~= 0 then if string.len(hexdata) % 32 ~= 0 then
return oops(("Bad data, length should be a multiple of 32 (was %d)"):format(string.len(hexdata))) return oops(('Bad data, length should be a multiple of 32 (was %d)'):format(string.len(hexdata)))
end end
local js,i = "["; local js,i = '[';
for i = 1, string.len(hexdata),32 do for i = 1, string.len(hexdata),32 do
js = js .."'" ..string.sub(hexdata,i,i+31).."',\n" js = js .."'" ..string.sub(hexdata,i,i+31).."',\n"
end end
js = js .. "]" js = js .. ']'
return js return js
end end
local function readdump(infile) local function readdump(infile)
t = infile:read("*all") t = infile:read('*all')
len = string.len(t) len = string.len(t)
local len,hex = bin.unpack(("H%d"):format(len),t) local len,hex = bin.unpack(('H%d'):format(len),t)
return hex return hex
end end
local function convert_to_emulform(hexdata) local function convert_to_emulform(hexdata)
if string.len(hexdata) % 32 ~= 0 then if string.len(hexdata) % 32 ~= 0 then
return oops(("Bad data, length should be a multiple of 32 (was %d)"):format(string.len(hexdata))) return oops(('Bad data, length should be a multiple of 32 (was %d)'):format(string.len(hexdata)))
end end
local ascii,i = ""; local ascii,i = '';
for i = 1, string.len(hexdata),32 do for i = 1, string.len(hexdata),32 do
ascii = ascii..string.sub(hexdata,i,i+31).."\n" ascii = ascii..string.sub(hexdata,i,i+31)..'\n'
end end
return string.sub(ascii, 1, -2) return string.sub(ascii, 1, -2)
end end
local function main(args) local function main(args)
local input = "dumpdata.bin" local input = 'dumpdata.bin'
local output local output
for o, a in getopt.getopt(args, 'i:o:h') do for o, a in getopt.getopt(args, 'i:o:h') do
if o == "h" then return help() end if o == 'h' then return help() end
if o == "i" then input = a end if o == 'i' then input = a end
if o == "o" then output = a end if o == 'o' then output = a end
end end
-- Validate the parameters -- Validate the parameters
local infile = io.open(input, "rb") local infile = io.open(input, 'rb')
if infile == nil then if infile == nil then
return oops("Could not read file ", input) return oops('Could not read file ', input)
end end
local dumpdata = readdump(infile) local dumpdata = readdump(infile)
-- The hex-data is now in ascii-format, -- The hex-data is now in ascii-format,
-- But first, check the uid -- But first, check the uid
local uid = string.sub(dumpdata,1,8) local uid = string.sub(dumpdata, 1, 8)
output = output or (uid .. ".eml") output = output or (uid .. '.eml')
-- Format some linebreaks -- Format some linebreaks
dumpdata = convert_to_emulform(dumpdata) dumpdata = convert_to_emulform(dumpdata)
local outfile = io.open(output, "w") local outfile = io.open(output, 'w')
if outfile == nil then if outfile == nil then
return oops("Could not write to file ", output) return oops('Could not write to file ', output)
end end
outfile:write(dumpdata:lower()) outfile:write(dumpdata:lower())
io.close(outfile) io.close(outfile)
print(("Wrote an emulator-dump to the file %s"):format(output)) print(('Wrote an emulator-dump to the file %s'):format(output))
end end