-
Notifications
You must be signed in to change notification settings - Fork 18
/
stream.py
executable file
·165 lines (119 loc) · 3.39 KB
/
stream.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import struct
import array
BIG_ENDIAN, LITTLE_ENDIAN = range(2)
def _create(s):
return (struct.Struct('>'+s), struct.Struct('<'+s))
u64 = _create('Q')
u32 = _create('I')
u16 = _create('H')
u8 = _create('B')
s64 = _create('q')
s32 = _create('i')
s16 = _create('h')
s8 = _create('b')
f32 = _create('f')
f64 = _create('d')
del _create
class DataInputStream:
def __init__(self, data, endian=BIG_ENDIAN, coverage=None):
self.data = data
self.length = len(data)
self.pos = 0
self.endian = endian
if coverage is None:
self.coverage = array.array('b')
self.coverage.extend((0,) * self.length)
else:
self.coverage = coverage
def seek(self, pos):
assert pos >= 0 and pos <= self.length
self.pos = pos
def skip(self, count):
new_pos = self.pos + count
assert new_pos >= 0 and new_pos <= self.length
self.pos = new_pos
def at_end(self):
return self.pos >= self.length
def read_bytes(self, count):
new_pos = self.pos + count
assert new_pos >= 0 and new_pos <= self.length
for i in range(self.pos, new_pos):
self.coverage[i] = 1
data = self.data[self.pos:new_pos]
self.pos = new_pos
return data
def read_u8(self):
return u8[self.endian].unpack(self.read_bytes(1))[0]
def read_u16(self):
return u16[self.endian].unpack(self.read_bytes(2))[0]
def read_u32(self):
return u32[self.endian].unpack(self.read_bytes(4))[0]
def read_u64(self):
return u64[self.endian].unpack(self.read_bytes(8))[0]
def read_s8(self):
return s8[self.endian].unpack(self.read_bytes(1))[0]
def read_s16(self):
return s16[self.endian].unpack(self.read_bytes(2))[0]
def read_s32(self):
return s32[self.endian].unpack(self.read_bytes(4))[0]
def read_s64(self):
return s64[self.endian].unpack(self.read_bytes(8))[0]
def read_float(self):
return f32[self.endian].unpack(self.read_bytes(4))[0]
def read_double(self):
return f64[self.endian].unpack(self.read_bytes(8))[0]
def at(self, pos):
copy = DataInputStream(self.data, self.endian, self.coverage)
copy.seek(pos)
return copy
def copy(self):
return self.at(self.pos)
@property
def covered_bytes(self):
n = 0
for flag in self.coverage:
if flag == 1:
n += 1
return n
class DataOutputStream:
def __init__(self, outfile, endian=BIG_ENDIAN):
self.outfile = outfile
self.endian = endian
def write_bytes(self, data):
self.outfile.write(data)
def write_u8(self, value):
self.write_bytes(u8[self.endian].pack(value))
def write_u16(self, value):
self.write_bytes(u16[self.endian].pack(value))
def write_u32(self, value):
self.write_bytes(u32[self.endian].pack(value))
def write_u64(self, value):
self.write_bytes(u64[self.endian].pack(value))
def write_s8(self, value):
self.write_bytes(s8[self.endian].pack(value))
def write_s16(self, value):
self.write_bytes(s16[self.endian].pack(value))
def write_s32(self, value):
self.write_bytes(s32[self.endian].pack(value))
def write_s64(self, value):
self.write_bytes(s64[self.endian].pack(value))
def write_float(self, value):
self.write_bytes(f32[self.endian].pack(value))
def write_double(self, value):
self.write_bytes(f64[self.endian].pack(value))
def read_bytestring(s):
output = []
while True:
bit = s.read_u8()
if bit == 0:
return bytes(output)
else:
output.append(bit)
def read_u16string(s):
output = []
while True:
bit = s.read_u16()
if bit == 0:
return ''.join(output)
else:
output.append(chr(bit))