-
Notifications
You must be signed in to change notification settings - Fork 26
/
Banners.py
392 lines (281 loc) · 12 KB
/
Banners.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
from cemu import *
from PyQt5 import QtGui, QtCore, QtWidgets
import os, sys, inspect
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0], "pefile")))
yapsy_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0], "yapsy")))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
sys.path.insert(0, yapsy_subfolder)
import pefile
from TextDecorators import *
import yapsy
class Banner(object):
def getOrientation(self):
NotImplementedError('method not implemented.')
def getDesiredGeometry(self):
NotImplementedError('method not implemented.')
def changeDisplay(self):
return
def setViewMode(self, viewMode):
self.viewMode = viewMode
def getPixmap(self):
return self.qpix
def _getNewPixmap(self, width, height):
return QtGui.QPixmap(width, height)
Orientation = enum(Left=0, Bottom=1, Top=2)
class Observer(object):
def changeViewMode(self, viewMode):
self.setViewMode(viewMode)
class Banners(Observer):
BOTTOM_SEPARATOR = 5
def __init__(self):
self._Banners = []
self.separatorBottom = 5
self.separatorLeft = 5
self.separatorTop = 5
def add(self, banner):
self._Banners.append(banner)
def banners(self):
return self._Banners
def getLeftOffset(self):
offset = 0
for banner in self._Banners:
if banner.getOrientation() == Orientation.Left:
offset += banner.getDesiredGeometry()
offset += self.separatorLeft
return offset
def getBottomOffset(self):
offset = 0
for banner in self._Banners:
if banner.getOrientation() == Orientation.Bottom:
offset += banner.getDesiredGeometry()
offset += self.separatorBottom
return offset
def getTopOffset(self):
offset = 0
for banner in self._Banners:
if banner.getOrientation() == Orientation.Top:
offset += banner.getDesiredGeometry()
offset += self.separatorTop
return offset
def resize(self, width, height):
limit = self.getBottomOffset() + self.getTopOffset()
for banner in self._Banners:
# banners are not resizeable actually
if banner.getOrientation() == Orientation.Left:
banner.resize(banner.getDesiredGeometry(), height - limit + 4) # +4 , make to look nice
if banner.getOrientation() == Orientation.Bottom:
banner.resize(width, banner.getDesiredGeometry())
if banner.getOrientation() == Orientation.Top:
banner.resize(width, banner.getDesiredGeometry())
def setViewMode(self, viewMode):
for banner in self._Banners:
banner.setViewMode(viewMode)
def draw(self, qp, offsetLeft, offsetBottom, maxY):
for banner in self._Banners:
if banner.getOrientation() == Orientation.Top:
banner.draw()
qp.drawPixmap(offsetLeft-4, offsetBottom, banner.getPixmap())
offsetBottom += banner.getDesiredGeometry() + self.separatorLeft
for banner in self._Banners:
if banner.getOrientation() == Orientation.Left:
banner.draw()
qp.drawPixmap(offsetLeft, offsetBottom, banner.getPixmap())
offsetLeft += banner.getDesiredGeometry() + self.separatorLeft
# initial offset + all offsets from all banners. We are doing this because Y growns down
offsetBottom = maxY - self.getBottomOffset() + self.BOTTOM_SEPARATOR
for banner in self._Banners:
if banner.getOrientation() == Orientation.Bottom:
banner.draw()
qp.drawPixmap(offsetLeft, offsetBottom, banner.getPixmap())
offsetBottom += banner.getDesiredGeometry() + self.separatorBottom
class FileAddrBanner(Banner):
def __init__(self, dataModel, viewMode):
self.width = 0
self.height = 0
self.dataModel = dataModel
self.viewMode = viewMode
self.qpix = self._getNewPixmap(self.width, self.height)
self.backgroundBrush = QtGui.QBrush(QtGui.QColor(0, 0, 128))
# text font
self.font = QtGui.QFont('Terminus', 11, QtGui.QFont.Light)
# font metrics. assume font is monospaced
self.font.setKerning(False)
self.font.setFixedPitch(True)
fm = QtGui.QFontMetrics(self.font)
self.fontWidth = fm.width('a')
self.fontHeight = fm.height()
self.textPen = QtGui.QPen(QtGui.QColor(192, 192, 192), 0, QtCore.Qt.SolidLine)
def getOrientation(self):
return Orientation.Left
def getDesiredGeometry(self):
return 10 * self.fontWidth
def setViewMode(self, viewMode):
self.viewMode = viewMode
def getPixmap(self):
return self.qpix
def _getNewPixmap(self, width, height):
return QtGui.QPixmap(width, height)
def draw(self):
qp = QtGui.QPainter()
offset = self.viewMode.getPageOffset()
columns, rows = self.viewMode.getGeometry()
qp.begin(self.qpix)
qp.fillRect(0, 0, self.width, self.height, self.backgroundBrush)
qp.setPen(self.textPen)
qp.setFont(self.font)
for i in range(rows):
s = '{0:08x}'.format(offset)
qp.drawText(0+5, (i+1) * self.fontHeight, s)
columns = self.viewMode.getColumnsbyRow(i)
offset += columns
qp.end()
def resize(self, width, height):
self.width = width
self.height = height
self.qpix = self._getNewPixmap(self.width, self.height)
class BottomBanner(Banner):
def __init__(self, dataModel, viewMode):
self.width = 0
self.height = 0
self.dataModel = dataModel
self.viewMode = viewMode
self.qpix = self._getNewPixmap(self.width, self.height)
self.backgroundBrush = QtGui.QBrush(QtGui.QColor(0, 0, 128))
# text font
self.font = QtGui.QFont('Consolas', 11, QtGui.QFont.Light)
# font metrics. assume font is monospaced
self.font.setKerning(False)
self.font.setFixedPitch(True)
fm = QtGui.QFontMetrics(self.font)
self.fontWidth = fm.width('a')
self.fontHeight = fm.height()
self.textPen = QtGui.QPen(QtGui.QColor(255, 255, 0), 0, QtCore.Qt.SolidLine)
def getOrientation(self):
return Orientation.Bottom
def getDesiredGeometry(self):
return 60
def setViewMode(self, viewMode):
self.viewMode = viewMode
def draw(self):
qp = QtGui.QPainter()
qp.begin(self.qpix)
qp.fillRect(0, 0, self.width, self.height, self.backgroundBrush)
qp.setPen(self.textPen)
qp.setFont(self.font)
cemu = ConsoleEmulator(qp, self.height//self.fontHeight, self.width//self.fontWidth)
dword = self.dataModel.getDWORD(self.viewMode.getCursorAbsolutePosition(), asString=True)
if dword is None:
dword = '----'
sd = 'DWORD: {0}'.format(dword)
pos = 'POS: {0:08x}'.format(self.viewMode.getCursorAbsolutePosition())
qword = self.dataModel.getQWORD(self.viewMode.getCursorAbsolutePosition(), asString=True)
if qword is None:
qword = '----'
sq = 'QWORD: {0}'.format(qword)
byte = self.dataModel.getBYTE(self.viewMode.getCursorAbsolutePosition(), asString=True)
if byte is None:
byte = '-'
sb = 'BYTE: {0}'.format(byte)
cemu.writeAt(1, 0, pos)
cemu.writeAt(17, 0, sd)
cemu.writeAt(35, 0, sq)
cemu.writeAt(62, 0, sb)
qp.drawLine(15 * self.fontWidth + 5, 0, 15 * self.fontWidth + 5, 50)
qp.drawLine(33 * self.fontWidth + 5, 0, 33 * self.fontWidth + 5, 50)
qp.drawLine(59 * self.fontWidth + 5, 0, 59 * self.fontWidth + 5, 50)
qp.drawLine(71 * self.fontWidth + 5, 0, 71 * self.fontWidth + 5, 50)
if self.viewMode.selector.getCurrentSelection():
u, v = self.viewMode.selector.getCurrentSelection()
if u != v:
pen = QtGui.QPen(QtGui.QColor(51, 153, 255), 0, QtCore.Qt.SolidLine)
qp.setPen(pen)
cemu.writeAt(73, 0, 'Selection: ')
cemu.write('{0:x}:{1}'.format(u, v-u))
else:
pen = QtGui.QPen(QtGui.QColor(128, 128, 128), 0, QtCore.Qt.SolidLine)
qp.setPen(pen)
cemu.writeAt(73, 0, '<no selection>')
"""
qp.drawLine(self.fontWidth*(len(pos) + 1) + 15, 0, self.fontWidth*(len(pos) + 1) + 15, 50)
qp.drawLine(self.fontWidth*(len(pos + sd) + 1) + 3*15, 0, self.fontWidth*(len(pos + sd) + 1) + 3*15, 50)
qp.drawLine(self.fontWidth*(len(pos + sd + sq) + 1) + 5*15, 0, self.fontWidth*(len(pos + sd + sq) + 1) + 5*15, 50)
qp.drawLine(self.fontWidth*(len(pos + sd + sq + sb) + 1) + 8*15, 0, self.fontWidth*(len(pos + sd + sq + sb) + 1) + 8*15, 50)
"""
#qp.drawLine(270, 0, 270, 50)
#qp.drawLine(480, 0, 480, 50)
#qp.drawLine(570, 0, 570, 50)
"""
# position
qp.drawText(0 + 5, self.fontHeight, pos)
# separator
qp.drawLine(120, 0, 120, 50)
# dword
qp.drawText(130 + 5, self.fontHeight, sd)
# separator
qp.drawLine(270, 0, 270, 50)
# qword
qp.drawText(280 + 5, self.fontHeight, sq)
# separator
qp.drawLine(480, 0, 480, 50)
# byte
qp.drawText(490 + 5, self.fontHeight, sb)
# separator
qp.drawLine(570, 0, 570, 50)
"""
qp.end()
pass
def getPixmap(self):
return self.qpix
def _getNewPixmap(self, width, height):
return QtGui.QPixmap(width, height)
def resize(self, width, height):
self.width = width
self.height = height
self.qpix = self._getNewPixmap(self.width, self.height)
class TopBanner(Banner):
def __init__(self, dataModel, viewMode):
self.width = 0
self.height = 0
self.dataModel = dataModel
self.viewMode = viewMode
self.qpix = self._getNewPixmap(self.width, self.height)
self.backgroundBrush = QtGui.QBrush(QtGui.QColor(0, 0, 128))
# text font
self.font = QtGui.QFont('Terminus', 11, QtGui.QFont.Bold)
# font metrics. assume font is monospaced
self.font.setKerning(False)
self.font.setFixedPitch(True)
fm = QtGui.QFontMetrics(self.font)
self.fontWidth = fm.width('a')
self.fontHeight = fm.height()
self.textPen = QtGui.QPen(QtGui.QColor(255, 255, 0), 0, QtCore.Qt.SolidLine)
def getOrientation(self):
return Orientation.Top
def getDesiredGeometry(self):
return 26#22
def setViewMode(self, viewMode):
self.viewMode = viewMode
def draw(self):
# i don't really like this in terms of arhitecture. We have
# artificially introduced getHeaderInfo() in Views. Then we had one top
# banner implemented per plugin. I will think to a better solution
qp = QtGui.QPainter()
qp.begin(self.qpix)
qp.fillRect(0, 0, self.width, self.height, self.backgroundBrush)
qp.setPen(self.textPen)
qp.setFont(self.font)
cemu = ConsoleEmulator(qp, self.height//self.fontHeight, self.width//self.fontWidth)
cemu.writeAt(1, 0, 'FileAddr')
offset = 11
text = self.viewMode.getHeaderInfo()
cemu.writeAt(offset, 0, text)
qp.end()
def getPixmap(self):
return self.qpix
def _getNewPixmap(self, width, height):
return QtGui.QPixmap(width, height)
def resize(self, width, height):
self.width = width
self.height = height
self.qpix = self._getNewPixmap(self.width, self.height)