-
Notifications
You must be signed in to change notification settings - Fork 9
/
dwlib.py
91 lines (81 loc) · 1.97 KB
/
dwlib.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
def canonicalize(instr):
outt = []
npos = pos = 0
while pos <= len(instr):
npos += 8
s = instr[pos:npos]
if not s:
break
hs = ''
cs = ''
for c in s:
n = ord(c)
hs += "%02x" % n
cs += c if 32 < n < 127 else '.'
outt.append((pos, hs, cs))
pos = npos
# print outt
outh = outc = ''
ostr = ["len: %s" % len(instr)]
eight = sixteen = ppos = 0
state = 0
for pos, h, c in outt:
outh += "%-16s" % h
outc += "%-8s" % c
eight = (pos % 8) == 0
sixteen = (pos % 16) == 0
if state == 0:
ppos = pos
outh += ' '
outc += ' '
state = 1
elif state == 1:
ostr.append("%04x: |%s| |%s|" % (ppos, outh, outc))
outh = outc = ''
state = 0
# print pos, eight, sixteen
# if eight and not sixteen:
# outh += ' '
# outc += ' '
# if sixteen:
# ppos = pos
# if pos>0 and sixteen:
# ostr.append("%04x: |%s| |%s|" % (ppos, outh, outc))
# outh = outc = ''
# print pos, outc, outh
# if pos==0 or (eight and not sixteen):
# if len(outc)<0:
if len(instr) == 0:
ostr.append("%04x: |%s| |%s|" % (ppos, ' ' * 33, ' ' * 17))
elif state == 1:
ostr.append(
"%04x: |%s | |%s |" %
(ppos, outh, outc))
outh = outc = ''
return '\n'.join(ostr)
def hexscii(s):
d = ''
sl = len(s)
p = 0
while p < sl:
e = p + 2
d += chr(int(s[p:e], 16))
p = e
return d
if __name__ == '__main__':
strs = [
'',
'a',
'a' * 8,
'a' * 9,
'a' * 16,
'a' * 17,
'a' * 23,
'a' * 24,
'a' * 31,
'a' * 32,
]
for s in strs:
print "==="
print canonicalize(s)
# vim: ts=4 sw=4 sts=4 expandtab