convert python2 mfd2eml and eml2mfd scripts to python3

This commit is contained in:
Joe Harrison 2020-02-21 15:09:36 +00:00
commit f28404581d
3 changed files with 17 additions and 18 deletions

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
''' '''
# Andrei Costin <zveriu@gmail.com>, 2011 # Andrei Costin <zveriu@gmail.com>, 2011
@ -6,20 +6,20 @@
# Converts PM3 Mifare Classic emulator EML text file to MFD binary dump file # Converts PM3 Mifare Classic emulator EML text file to MFD binary dump file
''' '''
from __future__ import with_statement
import sys import sys
import binascii import binascii
def main(argv): def main(argv):
argc = len(argv) argc = len(argv)
if argc < 3: if argc < 3:
print 'Usage:', argv[0], 'input.eml output.mfd' print('Usage:', argv[0], 'input.eml output.mfd')
sys.exit(1) sys.exit(1)
with file(argv[1], "r") as file_inp, file(argv[2], "wb") as file_out: with open(argv[1], "r") as file_inp, open(argv[2], "wb") as file_out:
for line in file_inp: for line in file_inp:
line = line.rstrip('\n').rstrip('\r') line = line.rstrip('\n').rstrip('\r')
print line print(line)
data = binascii.unhexlify(line) data = binascii.unhexlify(line)
file_out.write(data) file_out.write(data)

View file

@ -1,9 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python3
from __future__ import with_statement
from tempfile import mkdtemp from tempfile import mkdtemp
from shutil import rmtree from shutil import rmtree
from itertools import imap
from string import hexdigits from string import hexdigits
import unittest, os import unittest, os
import pm3_eml2mfd, pm3_mfd2eml import pm3_eml2mfd, pm3_mfd2eml
@ -24,18 +24,18 @@ class TestEmlMfd(unittest.TestCase):
def test_mfd2eml(self): def test_mfd2eml(self):
self.three_argument_test(pm3_mfd2eml.main, self.three_argument_test(pm3_mfd2eml.main,
imap(reversed, self.EML2MFD_TESTCASES), c14n=hex_c14n) map(reversed, self.EML2MFD_TESTCASES), c14n=hex_c14n)
def three_argument_test(self, operation, cases, c14n=str): def three_argument_test(self, operation, cases, c14n=str):
for case_input, case_output in cases: for case_input, case_output in cases:
try: try:
inp_name = os.path.join(self.tmpdir, 'input') inp_name = os.path.join(self.tmpdir, 'input')
out_name = os.path.join(self.tmpdir, 'output') out_name = os.path.join(self.tmpdir, 'output')
with file(inp_name, 'wb') as in_file: with open(inp_name, 'w') as in_file:
in_file.write(case_input) in_file.write(case_input)
operation(['', inp_name, out_name]) operation(['', inp_name, out_name])
with file(out_name, 'rb') as out_file: with open(out_name, 'r') as out_file:
self.assertEquals(c14n(case_output), c14n(out_file.read())) self.assertEqual(c14n(case_output), c14n(out_file.read()))
finally: finally:
for file_name in inp_name, out_name: for file_name in inp_name, out_name:
if os.path.exists(file_name): if os.path.exists(file_name):

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
''' '''
# Andrei Costin <zveriu@gmail.com>, 2011 # Andrei Costin <zveriu@gmail.com>, 2011
@ -6,24 +6,23 @@
# Converts PM3 Mifare Classic MFD binary dump file to emulator EML text file # Converts PM3 Mifare Classic MFD binary dump file to emulator EML text file
''' '''
from __future__ import with_statement
import sys import sys
import binascii
READ_BLOCKSIZE = 16 READ_BLOCKSIZE = 16
def main(argv): def main(argv):
argc = len(argv) argc = len(argv)
if argc < 3: if argc < 3:
print 'Usage:', argv[0], 'input.mfd output.eml' print('Usage:', argv[0], 'input.mfd output.eml')
sys.exit(1) sys.exit(1)
with file(argv[1], "rb") as file_inp, file(argv[2], "w") as file_out: with open(argv[1], "rb") as file_inp, open(argv[2], "w") as file_out:
while True: while True:
byte_s = file_inp.read(READ_BLOCKSIZE) byte_s = file_inp.read(READ_BLOCKSIZE)
if not byte_s: if not byte_s:
break break
hex_char_repr = binascii.hexlify(byte_s) hex_char_repr = byte_s.hex()
file_out.write(hex_char_repr) file_out.write(hex_char_repr)
file_out.write("\n") file_out.write("\n")