]>
Commit | Line | Data |
---|---|---|
0de8e387 | 1 | -- The getopt-functionality is loaded from pm3/getopt.lua |
2 | -- Have a look there for further details | |
3 | getopt = require('getopt') | |
4 | bin = require('bin') | |
f3cfe428 | 5 | example = "script run dumptoemul-mfu -i dumpdata-foobar.bin" |
6 | author = "Martin Holst Swende \n @Marshmellow" | |
0de8e387 | 7 | usage = "script run dumptoemul [-i <file>] [-o <file>]" |
8 | desc =[[ | |
9 | This script takes a dumpfile from 'hf mf dump' and converts it to a format that can be used | |
10 | by the emulator | |
11 | ||
12 | Arguments: | |
13 | -h This help | |
14 | -i <file> Specifies the dump-file (input). If omitted, 'dumpdata.bin' is used | |
15 | -o <filename> Specifies the output file. If omitted, <uid>.eml is used. | |
16 | ||
17 | ]] | |
0de8e387 | 18 | --- |
19 | -- A debug printout-function | |
20 | function dbg(args) | |
21 | if DEBUG then | |
22 | print("###", args) | |
23 | end | |
24 | end | |
25 | --- | |
26 | -- This is only meant to be used when errors occur | |
27 | function oops(err) | |
28 | print("ERROR: ",err) | |
29 | end | |
30 | ||
0de8e387 | 31 | --- |
32 | -- Usage help | |
33 | function help() | |
34 | print(desc) | |
f3cfe428 | 35 | print(author) |
0de8e387 | 36 | print("Example usage") |
37 | print(example) | |
38 | end | |
39 | ||
40 | local function convert_to_ascii(hexdata) | |
41 | if string.len(hexdata) % 8 ~= 0 then | |
42 | return oops(("Bad data, length should be a multiple of 8 (was %d)"):format(string.len(hexdata))) | |
43 | end | |
44 | ||
45 | local js,i = "["; | |
46 | for i = 1, string.len(hexdata),8 do | |
47 | js = js .."'" ..string.sub(hexdata,i,i+7).."',\n" | |
48 | end | |
49 | js = js .. "]" | |
50 | return js | |
51 | end | |
52 | ||
53 | local function readdump(infile) | |
54 | t = infile:read("*all") | |
55 | --print(string.len(t)) | |
56 | len = string.len(t) | |
57 | local len,hex = bin.unpack(("H%d"):format(len),t) | |
58 | --print(len,hex) | |
59 | return hex | |
60 | end | |
61 | ||
62 | local function convert_to_emulform(hexdata) | |
63 | if string.len(hexdata) % 8 ~= 0 then | |
64 | return oops(("Bad data, length should be a multiple of 8 (was %d)"):format(string.len(hexdata))) | |
65 | end | |
66 | local ascii,i = ""; | |
67 | for i = 1, string.len(hexdata),8 do | |
68 | ascii = ascii ..string.sub(hexdata,i,i+7).."\n" | |
69 | end | |
70 | ||
71 | return string.sub(ascii,1,-1) | |
72 | end | |
73 | ||
74 | local function main(args) | |
75 | ||
76 | local input = "dumpdata.bin" | |
77 | local output | |
78 | ||
79 | for o, a in getopt.getopt(args, 'i:o:h') do | |
80 | if o == "h" then return help() end | |
81 | if o == "i" then input = a end | |
82 | if o == "o" then output = a end | |
83 | end | |
84 | -- Validate the parameters | |
85 | ||
86 | local infile = io.open(input, "rb") | |
87 | if infile == nil then | |
88 | return oops("Could not read file ", input) | |
89 | end | |
90 | local dumpdata = readdump(infile) | |
91 | -- The hex-data is now in ascii-format, | |
92 | ||
93 | -- But first, check the uid | |
94 | local uid = string.sub(dumpdata,1,8) | |
95 | output = output or (uid .. ".eml") | |
96 | ||
97 | -- Format some linebreaks | |
98 | dumpdata = convert_to_emulform(dumpdata) | |
99 | ||
100 | local outfile = io.open(output, "w") | |
101 | if outfile == nil then | |
102 | return oops("Could not write to file ", output) | |
103 | end | |
104 | ||
105 | outfile:write(dumpdata:lower()) | |
106 | io.close(outfile) | |
107 | print(("Wrote an emulator-dump to the file %s"):format(output)) | |
108 | end | |
109 | ||
110 | ||
111 | --[[ | |
112 | In the future, we may implement so that scripts are invoked directly | |
113 | into a 'main' function, instead of being executed blindly. For future | |
114 | compatibility, I have done so, but I invoke my main from here. | |
115 | --]] | |
116 | main(args) |