1 -- The getopt-functionality is loaded from pm3/getopt.lua
2 -- Have a look there for further details
3 getopt = require('getopt')
6 example = "script run dumptoemul -i dumpdata-foobar.bin"
7 author = "Martin Holst Swende"
8 usage = "script run dumptoemul [-i <file>] [-o <file>]"
10 This script takes a dumpfile from 'hf mf dump' and converts it to a format that can be used
15 -i <file> Specifies the dump-file (input). If omitted, 'dumpdata.bin' is used
16 -o <filename> Specifies the output file. If omitted, <uid>.eml is used.
20 -------------------------------
22 -------------------------------
25 -- A debug printout-function
32 -- This is only meant to be used when errors occur
42 print("Example usage")
46 local function convert_to_ascii(hexdata)
47 if string.len(hexdata) % 8 ~= 0 then
48 return oops(("Bad data, length should be a multiple of 8 (was %d)"):format(string.len(hexdata)))
52 for i = 1, string.len(hexdata),8 do
53 js = js .."'" ..string.sub(hexdata,i,i+7).."',\n"
59 local function readdump(infile)
60 t = infile:read("*all")
61 --print(string.len(t))
63 local len,hex = bin.unpack(("H%d"):format(len),t)
68 local function convert_to_emulform(hexdata)
69 if string.len(hexdata) % 8 ~= 0 then
70 return oops(("Bad data, length should be a multiple of 8 (was %d)"):format(string.len(hexdata)))
73 for i = 1, string.len(hexdata),8 do
74 ascii = ascii ..string.sub(hexdata,i,i+7).."\n"
77 return string.sub(ascii,1,-1)
80 local function main(args)
82 local input = "dumpdata.bin"
85 for o, a in getopt.getopt(args, 'i:o:h') do
86 if o == "h" then return help() end
87 if o == "i" then input = a end
88 if o == "o" then output = a end
90 -- Validate the parameters
92 local infile = io.open(input, "rb")
94 return oops("Could not read file ", input)
96 local dumpdata = readdump(infile)
97 -- The hex-data is now in ascii-format,
99 -- But first, check the uid
100 local uid = string.sub(dumpdata,1,8)
101 output = output or (uid .. ".eml")
103 -- Format some linebreaks
104 dumpdata = convert_to_emulform(dumpdata)
106 local outfile = io.open(output, "w")
107 if outfile == nil then
108 return oops("Could not write to file ", output)
111 outfile:write(dumpdata:lower())
113 print(("Wrote an emulator-dump to the file %s"):format(output))
118 In the future, we may implement so that scripts are invoked directly
119 into a 'main' function, instead of being executed blindly. For future
120 compatibility, I have done so, but I invoke my main from here.