-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
helpers.py
101 lines (71 loc) · 1.67 KB
/
helpers.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
import struct
import binascii
BIG = ">"
LITTLE = "<"
def pd(d, o):
print("%04x: %s" % (o, binascii.hexlify(d[o:o+10])))
def get2l(d, o):
return struct.unpack("<H", d[o:o+2])[0]
def get4l(d, o):
return struct.unpack("<I", d[o:o+4])[0]
def get8l(d, o):
return struct.unpack("<Q", d[o:o+8])[0]
def get2b(d, o):
return struct.unpack(">H", d[o:o+2])[0]
def get4b(d, o):
return struct.unpack(">I", d[o:o+4])[0]
def get8b(d, o):
return struct.unpack(">Q", d[o:o+8])[0]
def get2(d, o, e):
assert e[0] in "<>"
return struct.unpack(e[0] + "H", d[o:o+2])[0]
def get4(d, o, e):
assert e[0] in "<>"
return struct.unpack(e[0] + "I", d[o:o+4])[0]
def inc4l(d, o, delta):
s = 4
v = struct.unpack("<I", d[o:o+s])[0]
v += delta
d = d[:o] + struct.pack("<I", v) + d[o+s:]
return d
def inc4b(d, o, delta):
s = 4
v = struct.unpack(">I", d[o:o+s])[0]
v += delta
d = d[:o] + struct.pack(">I", v) + d[o+s:]
return d
def inc4(d, o, delta, e):
assert e[0] in "<>"
s = 4
v = struct.unpack(e[0] + "I", d[o:o+s])[0]
v += delta
d = d[:o] + struct.pack(e[0] + "I", v) + d[o+s:]
return d
def inc8l(d, o, delta):
s = 8
v = struct.unpack("<Q", d[o:o+s])[0]
v += delta
d = d[:o] + struct.pack("<Q", v) + d[o+s:]
return d
def inc8b(d, o, delta):
s = 8
v = struct.unpack(">Q", d[o:o+s])[0]
v += delta
d = d[:o] + struct.pack(">Q", v) + d[o+s:]
return d
def int2b(i):
return int2(i, BIG)
def int2l(i):
return int2(i, LITTLE)
def int2(i, e):
assert e[0] in "<>"
return struct.pack(e[0] + "H", i)
def int4b(i):
return int4(i, BIG)
def int4l(i):
return int4(i, LITTLE)
def int4(i, e):
assert e[0] in "<>"
return struct.pack(e[0] + "I", i)
def getd(d, o, s):
return d[o:o+s]