findbits converted to python3 + tests

This commit is contained in:
Philippe Teuwen 2020-02-21 16:45:00 +01:00
parent aa6fc60a22
commit ba6f58cc05
3 changed files with 28 additions and 29 deletions

View file

@ -117,6 +117,8 @@ while true; do
if ! CheckExecute "mfkey64 long trace test" "tools/mfkey/./mfkey64 14579f69 ce844261 f8049ccb 0525c84f 9431cc40 7093df99 9972428ce2e8523f456b99c831e769dced09 8ca6827b ab797fd369e8b93a86776b40dae3ef686efd c3c381ba 49e2c9def4868d1777670e584c27230286f4 fbdcd7c1 4abd964b07d3563aa066ed0a2eac7f6312bf 9f9149ea" "Found Key: \[091e639cb715\]"; then break; fi
if ! CheckExecute "nonce2key test" "tools/nonce2key/nonce2key e9cadd9c a8bf4a12 a020a8285858b090 050f010607060e07 5693be6c00000000" "key recovered: fc00018778f7"; then break; fi
if ! CheckExecute "xorcheck test" "tools/xorcheck.py 04 00 80 64 ba" "final LRC XOR byte value: 5A"; then break; fi
if ! CheckExecute "findbits test" "tools/findbits.py 73 0110010101110011" "Match at bit 9: 011001010"; then break; fi
if ! CheckExecute "findbits_test test" "tools/findbits_test.py 2>&1" "OK"; then break; fi
printf "\n${C_GREEN}Tests [OK]${C_NC}\n\n"
exit 0
done

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# findbits.py - find Binary, Octal, Decimal or Hex number in bitstream
#
@ -31,10 +31,9 @@ def invert(data):
def search(target,data):
location = data.find(target)
if location >= 0:
print '*** Match at bit %d:' % location,
print '%s<%s>%s' % (data[:location],target,data[location+len(target):])
print('*** Match at bit {:d}: {}<{}>{}'.format(location, data[:location],target,data[location+len(target):]))
else:
print 'Not found'
print('Not found')
# convert integer to binary string
def binstring(number):
@ -49,32 +48,32 @@ def domatch(binary,number):
reversed= stringreverse(number)
inverted= invert(binary)
print ' Forward: (%s)' % number,
print(' Forward: (%s) ' % number, end = '')
search(binary,number)
print ' Reverse: (%s)' % reversed,
print(' Reverse: (%s) ' % reversed, end = '')
search(binary,reversed)
print ' Inverse: (%s)' % inverted
print ' Forward: (%s)' % number,
print(' Inverse: (%s) ' % inverted)
print(' Forward: (%s) ' % number, end = '')
search(inverted,number)
print ' Reverse: (%s)' % reversed,
print(' Reverse: (%s) ' % reversed, end = '')
search(inverted,reversed)
def main():
if(len(sys.argv) < 3):
print
print '\t'+sys.argv[0] + ' - Search bitstream for a known number'
print
print 'Usage: ' + sys.argv[0] + ' <NUMBER> <BITSTREAM>'
print
print '\tNUMBER will be converted to it\'s BINARY equivalent for all valid'
print '\tinstances of BINARY, OCTAL, DECIMAL and HEX, and the bitstream'
print '\tand it\'s inverse will be searched for a pattern match. Note that'
print '\tNUMBER must be specified in BINARY to match leading zeros.'
print
print 'Example:'
print
print '\tfindbits.py 73 0110010101110011'
print
print("""
\t{0} - Search bitstream for a known number
Usage: {0} <NUMBER> <BITSTREAM>
\tNUMBER will be converted to it\'s BINARY equivalent for all valid
\tinstances of BINARY, OCTAL, DECIMAL and HEX, and the bitstream
\tand it\'s inverse will be searched for a pattern match. Note that
\tNUMBER must be specified in BINARY to match leading zeros.
Example:
\t{0} 73 0110010101110011
""".format(sys.argv[0]))
os._exit(True)
bases= {
@ -84,11 +83,10 @@ def main():
16:'HEX',
}
for base, base_name in sorted(bases.iteritems()):
for base, base_name in sorted(bases.items()):
try:
number= int(sys.argv[1],base)
print
print 'Trying', base_name
print('\nTrying ' + base_name)
# do BINARY as specified to preserve leading zeros
if base == 2:
domatch(sys.argv[1],sys.argv[2])

5
tools/findbits_test.py Normal file → Executable file
View file

@ -1,6 +1,5 @@
#!/usr/bin/env python
#!/usr/bin/env python3
from itertools import imap
import unittest, sys, findbits
class TestFindBits(unittest.TestCase):
@ -46,7 +45,7 @@ class TestFindBits(unittest.TestCase):
def commutative_test(self, operation, cases):
self.unary_operation_test(operation, cases)
self.unary_operation_test(operation, imap(reversed, cases))
self.unary_operation_test(operation, map(reversed, cases))
def unary_operation_test(self, operation, cases):
for case_in, case_out in cases: