]>
Commit | Line | Data |
---|---|---|
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 | ||
23 | import sys | |
24 | import os | |
25 | import string | |
26 | ||
27 | # invert binary string | |
28 | def invert(data): | |
9f975d42 | 29 | return ''.join('0' if c == '1' else '1' for c in data) |
a93bd81d | 30 | |
31 | # do the actual search | |
9455b51c | 32 | def search(target,data): |
a93bd81d | 33 | location= string.find(data,target) |
34 | if location >= 0: | |
35 | print '*** Match at bit %d:' % location, | |
36 | print '%s<%s>%s' % (data[:location],target,data[location+len(target):]) | |
37 | else: | |
38 | print 'Not found' | |
39 | ||
40 | # convert integer to binary string | |
41 | def binstring(number): | |
42 | out= '' | |
43 | while number > 0: | |
44 | out += chr(0x30 + (number & 0x01)) | |
45 | number= number >> 1 | |
46 | return stringreverse(out) | |
47 | ||
48 | # reverse string order | |
49 | def stringreverse(data): | |
50 | out= '' | |
51 | for x in range(len(data) -1,-1,-1): | |
52 | out += data[x] | |
53 | return out | |
54 | ||
55 | # match forward, backward and inverted | |
56 | def domatch(number,binary): | |
57 | reversed= stringreverse(number) | |
58 | inverted= invert(binary) | |
59 | ||
60 | print ' Forward: (%s)' % number, | |
61 | search(binary,number) | |
62 | print ' Reverse: (%s)' % reversed, | |
63 | search(binary,reversed) | |
64 | print ' Inverse: (%s)' % inverted | |
65 | print ' Forward: (%s)' % number, | |
66 | search(inverted,number) | |
67 | print ' Reverse: (%s)' % reversed, | |
68 | search(inverted,reversed) | |
69 | ||
79c4de2a | 70 | def main(): |
71 | if(len(sys.argv) < 3): | |
72 | ||
73 | print '\t'+sys.argv[0] + ' - Search bitstream for a known number' | |
74 | ||
75 | print 'Usage: ' + sys.argv[0] + ' <NUMBER> <BITSTREAM>' | |
76 | ||
77 | print '\tNUMBER will be converted to it\'s BINARY equivalent for all valid' | |
78 | print '\tinstances of BINARY, OCTAL, DECIMAL and HEX, and the bitstream' | |
79 | print '\tand it\'s inverse will be searched for a pattern match. Note that' | |
80 | print '\tNUMBER must be specified in BINARY to match leading zeros.' | |
81 | ||
82 | print 'Example:' | |
83 | ||
84 | print '\tfindbits.py 73 0110010101110011' | |
85 | ||
86 | os._exit(True) | |
a93bd81d | 87 | |
79c4de2a | 88 | bases= { |
89 | 2:'BINARY', | |
90 | 8:'OCTAL', | |
91 | 10:'DECIMAL', | |
92 | 16:'HEX', | |
93 | } | |
a93bd81d | 94 | |
79c4de2a | 95 | for base in 2,8,10,16: |
96 | try: | |
97 | number= int(sys.argv[1],base) | |
98 | ||
99 | print 'Trying', bases[base] | |
100 | # do BINARY as specified to preserve leading zeros | |
101 | if base == 2: | |
102 | domatch(sys.argv[1],sys.argv[2]) | |
103 | else: | |
104 | domatch(binstring(number),sys.argv[2]) | |
105 | except: | |
106 | continue | |
107 | ||
108 | if __name__ == '__main__': | |
109 | main() |