]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - tools/findbits.py
Changed size of version info
[proxmark3-svn] / tools / findbits.py
index b753f8f4d0e58969c046a58f87eee9564482de29..1860964b11a144490489c87284d57d3dda1e4f92 100755 (executable)
 
 import sys
 import os
 
 import sys
 import os
-import string
 
 # invert binary string
 def invert(data):
 
 # invert binary string
 def invert(data):
-       i=  0
-       out= ''
-       while(i < len(data)):
-               if data[i] == '0':
-                       out += '1'
-               else:
-                       out += '0'
-               i += 1
-       return out
+       return ''.join('0' if c == '1' else '1' for c in data)
 
 # do the actual search
 
 # do the actual search
-def search(data,target):
-       location= string.find(data,target)
+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):])
        if location >= 0:
                print '*** Match at bit %d:' % location,
                print '%s<%s>%s' % (data[:location],target,data[location+len(target):])
@@ -47,21 +38,14 @@ def search(data,target):
 
 # convert integer to binary string
 def binstring(number):
 
 # convert integer to binary string
 def binstring(number):
-       out= ''
-       while number > 0:
-               out += chr(0x30 + (number & 0x01))
-               number= number >> 1
-       return stringreverse(out)
+       return bin(number)[2:] if number > 0 else ''
 
 # reverse string order
 def stringreverse(data):
 
 # reverse string order
 def stringreverse(data):
-       out= ''
-       for x in range(len(data) -1,-1,-1):
-               out += data[x]
-       return out
+       return data[::-1]
 
 # match forward, backward and inverted
 
 # match forward, backward and inverted
-def domatch(number,binary):
+def domatch(binary,number):
        reversed= stringreverse(number)
        inverted= invert(binary)
 
        reversed= stringreverse(number)
        inverted= invert(binary)
 
@@ -75,39 +59,43 @@ def domatch(number,binary):
        print '    Reverse: (%s)' % reversed,
        search(inverted,reversed)
 
        print '    Reverse: (%s)' % reversed,
        search(inverted,reversed)
 
-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
-       os._exit(True)
+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
+               os._exit(True)
 
 
-bases= { 
-       2:'BINARY',
-       8:'OCTAL',
-       10:'DECIMAL',
-       16:'HEX',
-               }
+       bases=  { 
+               2:'BINARY',
+               8:'OCTAL',
+               10:'DECIMAL',
+               16:'HEX',
+                       }
 
 
-for base in 2,8,10,16:
-       try:
-               number= int(sys.argv[1],base)
-               print
-               print 'Trying', bases[base]
-               # do BINARY as specified to preserve leading zeros
-               if base == 2:
-                       domatch(sys.argv[1],sys.argv[2])
-               else:
-                       domatch(binstring(number),sys.argv[2])
-       except:
-               continue
+       for base, base_name in sorted(bases.iteritems()):
+               try:
+                       number= int(sys.argv[1],base)
+                       print
+                       print 'Trying', base_name
+                       # do BINARY as specified to preserve leading zeros
+                       if base == 2:
+                               domatch(sys.argv[1],sys.argv[2])
+                       else:
+                               domatch(binstring(number),sys.argv[2])
+               except:
+                       continue
+
+if __name__ == '__main__':
+       main()
Impressum, Datenschutz