]>
git.zerfleddert.de Git - proxmark3-svn/blob - tools/findbits.py
f5d2dbd6b465c8afb859dac876ed23614525c421
3 # findbits.py - find Binary, Octal, Decimal or Hex number in bitstream
5 # Adam Laurie <adam@algroup.co.uk>
8 # This code is copyright (c) Adam Laurie, 2009, All rights reserved.
9 # For non-commercial use only, the following terms apply - for all other
10 # uses, please contact the author:
12 # This code is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # This code is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
27 # invert binary string
39 # do the actual search
40 def search(target
,data
):
41 location
= string
.find(data
,target
)
43 print '*** Match at bit %d:' % location
,
44 print '%s<%s>%s' % (data
[:location
],target
,data
[location
+len(target
):])
48 # convert integer to binary string
49 def binstring(number
):
52 out
+= chr(0x30 + (number
& 0x01))
54 return stringreverse(out
)
56 # reverse string order
57 def stringreverse(data
):
59 for x
in range(len(data
) -1,-1,-1):
63 # match forward, backward and inverted
64 def domatch(number
,binary
):
65 reversed= stringreverse(number
)
66 inverted
= invert(binary
)
68 print ' Forward: (%s)' % number
,
70 print ' Reverse: (%s)' % reversed,
71 search(binary
,reversed)
72 print ' Inverse: (%s)' % inverted
73 print ' Forward: (%s)' % number
,
74 search(inverted
,number
)
75 print ' Reverse: (%s)' % reversed,
76 search(inverted
,reversed)
78 if(len(sys
.argv
) < 3):
80 print '\t'+sys
.argv
[0] + ' - Search bitstream for a known number'
82 print 'Usage: ' + sys
.argv
[0] + ' <NUMBER> <BITSTREAM>'
84 print '\tNUMBER will be converted to it\'s BINARY equivalent for all valid'
85 print '\tinstances of BINARY, OCTAL, DECIMAL and HEX, and the bitstream'
86 print '\tand it\'s inverse will be searched for a pattern match. Note that'
87 print '\tNUMBER must be specified in BINARY to match leading zeros.'
91 print '\tfindbits.py 73 0110010101110011'
102 for base
in 2,8,10,16:
104 number
= int(sys
.argv
[1],base
)
106 print 'Trying', bases
[base
]
107 # do BINARY as specified to preserve leading zeros
109 domatch(sys
.argv
[1],sys
.argv
[2])
111 domatch(binstring(number
),sys
.argv
[2])