-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhexdump-1
More file actions
executable file
·67 lines (54 loc) · 1.69 KB
/
hexdump-1
File metadata and controls
executable file
·67 lines (54 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
from __future__ import print_function
import logging
import re
import subprocess
import sys
import tempfile
log = logging.getLogger('bindiff')
logging.basicConfig(level=logging.INFO, stream=sys.stderr)
hex_pattern = '([0-9a-fA-F]+) +' * 16
ascii_pattern = '(.)' * 16
line_pattern = re.compile(
'^[0-9a-fA-F]+ +{hex}\|{ascii}\|'.format(
hex=hex_pattern, ascii=ascii_pattern))
class NoHexdumpFile(Exception):
pass
def format_lines(lines):
no_match = True
for line in lines:
match = line_pattern.match(line)
if not match:
log.debug('no match for: %s', line)
else:
no_match = False
half = len(match.groups()) / 2
for n in xrange(1, half + 1):
print(match.group(n)
+ ' ' + match.group(n + half))
if no_match:
raise NoHexdumpFile()
def format_file(src_path):
with open(src_path) as src:
if not line_pattern.match(src.readline()):
raise NoHexdumpFile()
src.seek(0)
format_lines(iter(src))
def hexdump_and_format_file(src_path):
with tempfile.NamedTemporaryFile() as tmp:
retc = subprocess.call(
('hexdump', '-C', src_path),
stdout=tmp)
if retc != 0:
raise RuntimeError('Failed to get hexdump of file %s'
% src_path)
tmp.seek(0)
format_lines(iter(tmp))
if __name__ == '__main__':
src_path = sys.argv[1]
try:
format_file(src_path)
except NoHexdumpFile:
log.info('%s does not looks like hexdump file, hexdumping ...',
src_path)
hexdump_and_format_file(src_path)