]> git.zerfleddert.de Git - proxmark3-svn/blame - tools/findbits.py
added simple test suite for tools/findbits.py
[proxmark3-svn] / tools / findbits.py
CommitLineData
a93bd81d 1#!/usr/bin/python
2
3# findbits.py - find Binary, Octal, Decimal or Hex number in bitstream
4#
5# Adam Laurie <adam@algroup.co.uk>
6# http://rfidiot.org/
7#
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:
11#
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.
16#
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.
21#
22
23import sys
24import os
25import string
26
27# invert binary string
28def invert(data):
29 i= 0
30 out= ''
31 while(i < len(data)):
32 if data[i] == '0':
33 out += '1'
34 else:
35 out += '0'
36 i += 1
37 return out
38
39# do the actual search
9455b51c 40def search(target,data):
a93bd81d 41 location= string.find(data,target)
42 if location >= 0:
43 print '*** Match at bit %d:' % location,
44 print '%s<%s>%s' % (data[:location],target,data[location+len(target):])
45 else:
46 print 'Not found'
47
48# convert integer to binary string
49def binstring(number):
50 out= ''
51 while number > 0:
52 out += chr(0x30 + (number & 0x01))
53 number= number >> 1
54 return stringreverse(out)
55
56# reverse string order
57def stringreverse(data):
58 out= ''
59 for x in range(len(data) -1,-1,-1):
60 out += data[x]
61 return out
62
63# match forward, backward and inverted
64def domatch(number,binary):
65 reversed= stringreverse(number)
66 inverted= invert(binary)
67
68 print ' Forward: (%s)' % number,
69 search(binary,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)
77
79c4de2a 78def main():
79 if(len(sys.argv) < 3):
80 print
81 print '\t'+sys.argv[0] + ' - Search bitstream for a known number'
82 print
83 print 'Usage: ' + sys.argv[0] + ' <NUMBER> <BITSTREAM>'
84 print
85 print '\tNUMBER will be converted to it\'s BINARY equivalent for all valid'
86 print '\tinstances of BINARY, OCTAL, DECIMAL and HEX, and the bitstream'
87 print '\tand it\'s inverse will be searched for a pattern match. Note that'
88 print '\tNUMBER must be specified in BINARY to match leading zeros.'
89 print
90 print 'Example:'
91 print
92 print '\tfindbits.py 73 0110010101110011'
93 print
94 os._exit(True)
a93bd81d 95
79c4de2a 96 bases= {
97 2:'BINARY',
98 8:'OCTAL',
99 10:'DECIMAL',
100 16:'HEX',
101 }
a93bd81d 102
79c4de2a 103 for base in 2,8,10,16:
104 try:
105 number= int(sys.argv[1],base)
106 print
107 print 'Trying', bases[base]
108 # do BINARY as specified to preserve leading zeros
109 if base == 2:
110 domatch(sys.argv[1],sys.argv[2])
111 else:
112 domatch(binstring(number),sys.argv[2])
113 except:
114 continue
115
116if __name__ == '__main__':
117 main()
Impressum, Datenschutz