-
Notifications
You must be signed in to change notification settings - Fork 0
/
filebuffer.py
237 lines (176 loc) · 4.9 KB
/
filebuffer.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from __future__ import division
import os
import sys
import struct
def fmtslice(slice):
width = slice.stop - slice.start
start = slice.start or ''
stop = slice.stop # if (slice.stop != width) else ''
step = slice.step or None
res = '%s:%s' % (start, stop)
if step: res += ':%s' % step
return res
def FileBuffer(fname, mode='rb'):
fp = open(fname, mode)
return Buffer(fp, slice(0, os.path.getsize(fname)))
class BufferReader(object):
def __init__(self, bufobj):
self.bufobj = bufobj
self.fptr = 0
def read(self, n=None):
if n is None:
n = len(self.bufobj) - self.fptr
else:
assert n >= 0
data = self.bufobj[self.fptr : self.fptr+n]
self.fptr += len(data)
return data.str()
class Buffer(object):
def __init__(self, fp, range):
self.fp = fp
self.pos = 0
start, stop, step = range.start, range.stop, range.step
start = (start or 0)
assert stop is not None
assert step in (None, 1)
assert start <= stop
self.slice = slice(start, stop, None)
def __len__(self):
res = self.slice.stop - self.slice.start
assert res >= 0
return res
def length(self):
return len(self)
len = property(lambda self: len(self))
start = property(lambda self: self.slice.start)
stop = property(lambda self: self.slice.start + len(self))
def reset(self):
self.pos = 0
def read(self, nbytes=None):
if nbytes is None:
nbytes = len(self) - self.pos
else:
if self.pos + nbytes > len(self):
nbytes = len(self) - self.pos
assert nbytes >= 0
assert self.pos + nbytes <= len(self)
result = self[self.pos : self.pos + nbytes].str()
self.pos += nbytes
return result
def write(self, data):
nbytes = len(data)
assert self.pos + nbytes <= len(self)
self.fp.seek(self.start + self.pos)
self.fp.write(data)
self.pos += nbytes
def str(self):
self.fp.seek(self.slice.start)
width = self.slice.stop - self.slice.start
assert(width >= 0)
res = self.fp.read(width)
return res
def szstr(self):
result = self.str()
try:
return result[:result.index('\x00')]
except ValueError:
return result
def __str__(self):
return "Buffer[%s]" % fmtslice(self.slice)
def __repr__(self):
additional = 16
additional = ["%02x" % ord(self[i]) for i in xrange(min(additional, self.length()))] + (['...'] * (self.length() > additional))
additional = " [%s]" % (' '.join(additional))
#return '<Buffer %s, len %d [%s]%s>' % (repr(os.path.basename(self.fp.name)), self.length(), fmtslice(self.slice), additional)
return '<Buffer, len %d [%s]%s>' % (self.length(), fmtslice(self.slice), additional)
def __iter__(self):
i = 0
len = self.len
while i < len:
yield self[i]
i += 1
def __rshift__(self, fmt):
fmtlen = struct.calcsize(fmt)
assert self.pos + fmtlen <= len(self)
res = self[self.pos:][fmt]
self.pos += fmtlen
return res
def copy(self):
return self[:]
def __getitem__(self, key):
width = self.slice.stop - self.slice.start
if isinstance(key, str):
fmtlen = struct.calcsize(key)
assert fmtlen <= len(self)
rv = struct.unpack(key, self[:fmtlen].str())
return rv[0] if len(rv) == 1 else rv
elif isinstance(key, slice):
assert (not key.step) or (key.step == 1)
kstart = key.start
kstop = key.stop
if kstart is None:
start = 0
else:
if kstart < 0: kstart += width
start = min(width, kstart)
if kstop is None:
stop = width
else:
if kstop < 0: kstop += width
stop = min(width, kstop)
if start > stop:
stop = start
assert 0 <= start <= width
assert 0 <= stop <= width
return Buffer(self.fp, slice(
self.slice.start + start,
self.slice.start + stop,
None
))
else:
# wrap-around behavior
if key < 0:
key += self.length()
# bounds check
assert 0 <= key < self.length()
self.fp.seek(self.slice.start + key)
return self.fp.read(1)
def __setitem__(self, key, newval):
width = self.slice.stop - self.slice.start
if isinstance(key, str):
if not isinstance(newval, tuple):
newval = (newval,)
formatted = struct.pack(key, *newval)
assert len(formatted) <= len(self)
self.fp.seek(self.start)
self.fp.write(formatted)
elif isinstance(key, slice):
kstart = key.start
kstop = key.stop
if kstart is None:
start = 0
else:
if kstart < 0: kstart += width
start = min(width, kstart)
if kstop is None:
stop = width
else:
if kstop < 0: kstop += width
stop = min(width, kstop)
if start > stop:
stop = start
assert 0 <= start <= width
assert 0 <= stop <= width
if not isinstance(newval, str):
newval = str(newval)
assert len(newval) == stop-start
self.fp.seek(self.slice.start + start)
self.fp.write(newval)
else:
assert isinstance(newval, str)
assert len(newval) == 1
if key < 0:
k += self.length()
assert 0 <= key < self.length()
self.fp.seek(self.slice.start + key)
self.fp.write(newval)