From 48c71aeef61c721c7435af68519b4737e1628ecc Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 14 May 2019 11:54:25 +0200 Subject: [PATCH] fix: 'script run legic' - also save as binary. https://github.com/RfidResearchGroup/proxmark3/issues/193 if binary file exists, it will try to increase filename counter to find a non-existing name. --- client/scripts/legic.lua | 47 +++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/client/scripts/legic.lua b/client/scripts/legic.lua index 97fd64f11..0bd7104f7 100644 --- a/client/scripts/legic.lua +++ b/client/scripts/legic.lua @@ -292,13 +292,13 @@ end --- -- check availability of file function file_check(file_name) - local file_found=io.open(file_name, "r") - if file_found==nil then - return false - else - file_found:close() - return true - end + local file_found = io.open(file_name, "r") + if file_found == nil then + return false + else + file_found:close() + return true + end end --- @@ -638,6 +638,30 @@ local function readFile(filename) return tag end +local function save_BIN(data, filename) + local outfile + local counter = 1 + local ext = filename:match("^.+(%..+)$") + local fn = filename + + -- Make sure we don't overwrite a file + while file_check(fn) do + fn = filename:gsub(ext, tostring(counter)..ext) + print(filename, fn) + counter = counter + 1 + end + + outfile = io.open(fn, 'wb') + + local i = 1 + while data[i] do + outfile:write(data[i]) + i = i + 1 + end + + outfile:close() + return fn +end --- -- write bytes to file function writeFile(bytes, filename) @@ -653,7 +677,9 @@ function writeFile(bytes, filename) if err then return oops("OOps ... failed to open output-file ".. filename) end + bytes = xorBytes(bytes, bytes[5]) + for i = 1, #bytes do if (bcnt == 0) then line = bytes[i] @@ -670,7 +696,12 @@ function writeFile(bytes, filename) bcnt = bcnt + 1 end fho:close() - print("\nwrote ".. #bytes .." bytes to " .. filename) + + -- save binary + local fn_bin = save_BIN(bytes, filename) + + print("\nwrote ".. #bytes * 3 .." bytes to " .. filename) + print("\nwrote ".. #bytes .." bytes to " .. fn_bin) return true end