forked from mtivadar/qiew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataModel.py
298 lines (212 loc) · 6.69 KB
/
DataModel.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import sys, os
import mmap
class Observer:
def update_geometry(self):
NotImplementedError('method not implemented.')
class DataModel(object, Observer):
def __init__(self, data):
self._dataOffset = 0
self.rows = self.cols = 0
self.data = data
self._lastOffset = 0
self._dirty = False
@property
def dataOffset(self):
return self._dataOffset
@dataOffset.setter
def dataOffset(self, value):
self._lastOffset = self._dataOffset
self._dataOffset = value
def getLastOffset(self):
return self._lastOffset
def inLimits(self, x):
if x >= 0 and x < len(self.data):
return True
return False
def slide(self, off):
if self.inLimits(self.dataOffset + off):
self.dataOffset += off
def goTo(self, off):
if self.inLimits(off):
self.dataOffset = off
def offsetInPage(self, off):
if off >= self.dataOffset and off <= self.dataOffset + self.rows*self.cols:
return True
return False
def update_geometry(self, rows, cols):
self.rows = rows
self.cols = cols
def slideLine(self, factor):
self.slide(factor*self.cols)
def slidePage(self, factor):
self.slide(factor*self.cols*self.rows)
def slideToLastPage(self):
if self.rows*self.cols > len(self.data):
return
self.dataOffset = len(self.data) - self.cols*self.rows
def slideToFirstPage(self):
self.dataOffset = 0
def getXYInPage(self, off):
off -= self.dataOffset
x, y = off/self.cols, off%self.cols
return x, y
def getPageOffset(self, page):
return self.getOffset() + (page)*self.rows*self.cols
def getQWORD(self, offset, asString=False):
if offset + 8 > len(self.data):
return None
b = bytearray(self.data[offset:offset+8])
d = ((b[7] << 56) | (b[6] << 48) | (b[5] << 40) | (b[4] << 32) | (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0])) & 0xFFFFFFFFFFFFFFFF
if not asString:
return d
s = '{0:016X}'.format(d)
return s
def getDWORD(self, offset, asString=False):
if offset + 4 >= len(self.data):
return None
b = bytearray(self.data[offset:offset+4])
d = ((b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0])) & 0xFFFFFFFF
if not asString:
return d
s = '{0:08X}'.format(d)
return s
def getWORD(self, offset, asString=False):
if offset + 2 > len(self.data):
return None
b = bytearray(self.data[offset:offset+2])
d = ((b[1] << 8) | (b[0])) & 0xFFFF
if not asString:
return d
s = '{0:04X}'.format(d)
return s
def getBYTE(self, offset, asString=False):
if offset + 1 > len(self.data):
return None
b = bytearray(self.data[offset:offset+1])
d = (b[0]) & 0xFF
if not asString:
return d
s = '{0:02X}'.format(d)
return s
def getChar(self, offset):
if offset < 0:
return None
if offset >= len(self.data):
return None
return self.data[offset]
def getStream(self, start, end):
return bytearray(self.data[start:end])
def getOffset(self):
return self.dataOffset
def getData(self):
return self.data
def isDirty(self):
return self._dirty
def setData_b(self, offset, b):
if self.inLimits(offset):
self.data[offset] = b
self._dirty = True
return True
return False
def setData_s(self, u, v, s):
self.data[u:v] = s
self._dirty = True
return True
def getDataSize(self):
return len(self.data)
@property
def source(self):
return ''
def flush(self):
raise NotImplementedError('method not implemented.')
def write(self):
raise NotImplementedError('method not implemented.')
def close(self):
pass
def size(self):
pass
class FileDataModel(DataModel):
def __init__(self, filename):
self._filename = filename
self._f = open(filename, "rb")
# memory-map the file, size 0 means whole file
self._mapped = mmap.mmap(self._f.fileno(), 0, access=mmap.ACCESS_COPY)
super(FileDataModel, self).__init__(self._mapped)
@property
def source(self):
return self._filename
def flush(self):
self._f.close()
# open for writing
try:
self._f = open(self._filename, "r+b")
except Exception, e:
# could not open for writing
return False
self._f.write(self._mapped)
return True
def close(self):
self._mapped.close()
self._f.close()
def write(self, offset, stream):
self._mapped.seek(offset)
self._mapped.write(stream)
def size(self):
return os.path.getsize(self._filename)
def f():
return 5
import StringIO
class MyStringIO(StringIO.StringIO, object):
def __init__(self, data):
self.raw = data
super(MyStringIO, self).__init__(data)
def __len__(self):
return len(self.raw)
class MyByte(bytearray):
def __init__(self, data):
self.raw = data
self._pointer = 0
super(MyByte, self).__init__(data)
def __len__(self):
return len(self.raw)
def seek(self, a, b=0):
if b == 0:
self._pointer = a
elif b == 1:
self._pointer += a
elif b == 2:
self._pointer = len(self.raw) - b
else:
return
return
def read(self, size):
if self._pointer + size > len(self.raw):
return ''
data = str(self.raw[self._pointer:self._pointer + size])
self._pointer += size
return data
class BufferDataModel(DataModel):
def __init__(self, data, name):
self._filename = name
self.raw = data
#import StringIO
# self.data = bytearray(data)
# self.data = bytearray(data)
self.data = MyByte(data)
#self.data.__len__ = f
#self.data.__len__ = self.data.len
#print self.data.len
#self.data.__len__ = f
super(BufferDataModel, self).__init__(self.data)
@property
def source(self):
return self._filename
def flush(self):
return False
def close(self):
return
# def write(self, offset, stream):
# self._mapped.seek(offset)
# self._mapped.write(stream)
def size(self):
return len(data)