From 6742c089b12e1ab10f61932b3ced3fac6e03ca30 Mon Sep 17 00:00:00 2001
From: "martin.holst@gmail.com"
 <martin.holst@gmail.com@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Date: Sat, 19 Oct 2013 20:18:50 +0000
Subject: [PATCH] Changes to how dumping is performed, now utilises a library.
 This is in preparation for a more generic dumping of different types of
 cards. Also added functionality to html-dump .eml-files

---
 client/lualibs/html_dumplib.lua | 123 ++++++++++++++++++++++++++++++++
 client/scripts/htmldump.lua     |  46 ++----------
 2 files changed, 127 insertions(+), 42 deletions(-)
 create mode 100644 client/lualibs/html_dumplib.lua

diff --git a/client/lualibs/html_dumplib.lua b/client/lualibs/html_dumplib.lua
new file mode 100644
index 000000000..d2aee33ba
--- /dev/null
+++ b/client/lualibs/html_dumplib.lua
@@ -0,0 +1,123 @@
+bin = require('bin')
+
+
+-------------------------------
+-- Some utilities 
+-------------------------------
+
+--- 
+-- A debug printout-function
+local function dbg(args)
+	if DEBUG then
+		print("###", args)
+	end
+end 
+--- 
+-- This is only meant to be used when errors occur
+local function oops(err)
+	print("ERROR: ",err)
+end
+
+local function save_HTML(javascript, filename)
+
+	-- Read the HTML-skel file
+	local skel = require("htmlskel")
+	html = skel.getHTML(javascript);
+
+	-- Open the output file
+	
+	local outfile = io.open(filename, "w")
+	if outfile == nil then 
+		return oops("Could not write to file ", filename)
+	end
+	-- Write the data into it
+	outfile:write(html)
+	io.close(outfile)
+
+	-- Done
+	return filename
+
+end
+
+
+
+local function convert_ascii_dump_to_JS(infile)
+	local t = infile:read("*all")
+	
+	local output = "[";
+	for line in string.gmatch(t, "[^\n]+") do 
+		output = output .. "'"..line.."',\n"
+	end
+	output = output .. "]"
+	return output
+end
+
+
+local function convert_binary_dump_to_JS(infile, blockLen)
+	 local bindata = infile:read("*all")
+	 len = string.len(bindata)
+
+	if len % blockLen ~= 0 then 
+		return oops(("Bad data, length (%d) should be a multiple of blocklen (%d)"):format(len, blockLen))
+	end
+
+	local _,hex = bin.unpack(("H%d"):format(len),bindata)
+
+	-- Now that we've converted binary data into hex, we doubled the size. 
+	-- One byte, like 0xDE is now 
+	-- the characters 'D' and 'E' : one byte each. 
+	-- Thus:
+	blockLen = blockLen * 2
+
+	local js,i = "[";
+	for i = 1, string.len(hex),blockLen do
+		js = js .."'" ..string.sub(hex,i,i+blockLen -1).."',\n"
+	end
+	js = js .. "]"
+	return js
+end
+
+---
+-- Converts a .eml-file into a HTML/Javascript file. 
+-- @param input the file to convert
+-- @param output the file to write to
+-- @return the name of the new file. 
+local function convert_eml_to_html(input, output)
+	input = input or 'dumpdata.eml'
+	output = output or input .. 'html'
+
+	local infile = io.open(input, "r")
+	if infile == nil then 
+		return oops("Could not read file ", input)
+	end
+
+	-- Read file, get JS
+	local javascript = convert_ascii_dump_to_JS(infile)
+	io.close(infile)
+	return save_HTML(javascript, output )
+end
+
+--- Converts a binary dump into HTML/Javascript file
+-- @param input the file containing the dump  (defaults to dumpdata.bin)
+-- @param output the file to write to
+-- @param blockLen, the length of each block. Defaults to 16 bytes
+local function convert_bin_to_html(input, output, blockLen)
+	input = input or 'dumpdata.bin'
+	blockLen = blockLen or 16
+	output = output or input .. 'html'
+
+	local infile = io.open(input, "r")
+	if infile == nil then 
+		return oops("Could not read file ", input)
+	end
+	-- Read file, get JS
+	local javascript = convert_binary_dump_to_JS(infile, blockLen)
+	io.close(infile)
+
+	return save_HTML(javascript, output )
+end
+
+return {
+	convert_bin_to_html = convert_bin_to_html,
+	convert_eml_to_html = convert_eml_to_html,	
+}
diff --git a/client/scripts/htmldump.lua b/client/scripts/htmldump.lua
index e6c1393b0..d14f88195 100644
--- a/client/scripts/htmldump.lua
+++ b/client/scripts/htmldump.lua
@@ -2,6 +2,7 @@
 -- Have a look there for further details
 getopt = require('getopt')
 bin = require('bin')
+dumplib = require('html_dumplib')
 
 example = "script run htmldump -o mifarecard_foo.html"
 author = "Martin Holst Swende"
@@ -43,26 +44,6 @@ function help()
 	print(example)
 end
 
-local function readdump(infile)
-	 t = infile:read("*all")
-	 --print(string.len(t))
-	 len = string.len(t)
-	 local len,hex = bin.unpack(("H%d"):format(len),t)
-	 --print(len,hex)
-	 return hex
-end
-local function convert_to_js(hexdata)
-	if string.len(hexdata) % 32 ~= 0 then 
-		return oops(("Bad data, length should be a multiple of 32 (was %d)"):format(string.len(hexdata)))
-	end
-	local js,i = "[";
-	for i = 1, string.len(hexdata),32 do
-		js = js .."'" ..string.sub(hexdata,i,i+31).."',\n"
-	end
-	js = js .. "]"
-	return js
-end
-
 local function main(args)
 
 	local input = "dumpdata.bin"
@@ -72,31 +53,12 @@ local function main(args)
 		if o == "i" then input = a end
 		if o == "o" then output = a end
 	end
-	-- Validate the parameters
-	
-	local infile = io.open(input, "r")
-	if infile == nil then 
-		return oops("Could not read file ", input)
-	end
-	--lokal skel = require("skel")
-	local dumpdata = readdump(infile)
-	io.close(infile)
+	local filename, err = dumplib.convert_bin_to_html(input,output,16)
+	if err then return oops(err) end
 
-	local js_code = convert_to_js(dumpdata)
-	--print(js_code)
-	local skel = require("htmlskel")
-	html = skel.getHTML(js_code);
-	
-	local outfile = io.open(output, "w")
-	if outfile == nil then 
-		return oops("Could not write to file ", output)
-	end
-	outfile:write(html)
-	io.close(outfile)
-	print(("Wrote a HTML dump to the file %s"):format(output))
+	print(("Wrote a HTML dump to the file %s"):format(filename))
 end
 
-
 --[[
 In the future, we may implement so that scripts are invoked directly 
 into a 'main' function, instead of being executed blindly. For future