-
Notifications
You must be signed in to change notification settings - Fork 0
/
filetools.py
104 lines (80 loc) · 1.76 KB
/
filetools.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from __future__ import with_statement
import pprint
import os
import struct
from filebuffer import *
pp = pprint.pprint
def nsplit(s, n=1):
for i in xrange(0, len(s), n):
yield s[i:i+n]
def hexdump(s, offset=0, width=16, sep=4):
def lineout(position, hex, clear):
hexparts = []
for i,c in enumerate(hex):
if i % sep == 0:
hexparts.append([])
hexparts[-1].append("%02s" % c)
hexpart = ' '.join(
' '.join(part)
for part in hexparts
)
asciiparts = []
for i,c in enumerate(clear):
if i % sep == 0:
asciiparts.append('')
asciiparts[-1] += ("%1s" % c if (32 <= ord(c) < 128) else '.')
asciipart = ' '.join(asciiparts)
print "%08x : %*s | %*s |" % (
position,
-(3*width-1 + ((width-1) // sep)),
hexpart,
-(1*width + ((width-1) // sep)),
asciipart
)
# position in input
p = 0
# line number
q = offset - (offset % width)
# first line
munch = width - (offset % width)
line = s[:munch]
lineout(
q,
[' ']*(width-munch) + ["%02x" % ord(c) for c in line],
[' ']*(width-munch) + list(line),
)
p += munch
q += width
# remaining lines
while p < len(s):
munch = min(len(s) - p, width)
assert 0 <= munch <= width
line = s[p:p+munch]
lineout(
q,
["%02x" % ord(c) for c in line],
list(line),
)
p += munch
q += width
def int2bin(x, pad=0):
res = []
while x:
res.append(x & 1)
x >>= 1
if len(res) < pad:
res += [0] * (pad - len(res))
return res
def bindump(s):
print " ".join(
''.join(str(b) for b in reversed(int2bin(ord(c), pad=8)))
for c in s
)
class Seekguard(object):
def __init__(self, fp):
self.fp = fp
self.position = fp.tell()
def __enter__(self):
return self.fp
def __exit__(self, exc_type, exc_value, traceback):
self.fp.seek(self.position)