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