104
|
1 #!/usr/bin/python
|
|
2
|
|
3 from PyQt4.QtCore import *
|
|
4 from PyQt4.QtGui import *
|
109
|
5 from qtpropertyviewer import QtPropertyViewer
|
104
|
6 import sys
|
|
7 import hexfile
|
|
8
|
|
9 class BinViewer(QWidget):
|
|
10 """
|
|
11 The binviewer consists of a hex view, a ascii view
|
|
12 and perhaps others..
|
|
13 """
|
|
14 def __init__(self):
|
|
15 super().__init__()
|
|
16 self.setFont(QFont('Courier'))
|
|
17 self.fontHeight = self.fontMetrics().height()
|
|
18 self.fontWidth = self.fontMetrics().width('x')
|
|
19 self.hexfile = hexfile.HexFile()
|
|
20 def paintEvent(self, event):
|
|
21 painter = QPainter(self)
|
|
22 br = QBrush(Qt.lightGray)
|
|
23 painter.setBrush(br)
|
|
24 h = self.height()
|
|
25 addressWidth = self.fontWidth * 8
|
|
26 painter.drawRect(2, 2, addressWidth, h)
|
|
27 br2 = QBrush(Qt.yellow)
|
|
28 painter.setBrush(br2)
|
|
29 w = self.width()
|
105
|
30 byteWidth = self.fontWidth * (16 * 3 - 1)
|
104
|
31 painter.drawRect(addressWidth + 4, 2, byteWidth, h)
|
|
32 asciiWidth = self.fontWidth * 16
|
|
33 br2 = QBrush(Qt.gray)
|
|
34 painter.setBrush(br2)
|
|
35 painter.drawRect(addressWidth + byteWidth + 4, 2, asciiWidth, h)
|
105
|
36 rows = int(h / self.fontHeight) + 1
|
|
37 offset = 0
|
|
38 for r in range(1, rows):
|
|
39 bts = self.getBytes(offset + r - 1)
|
104
|
40 addr = '{0:08X}'.format(r << 16)
|
|
41 painter.drawText(2, 2 + self.fontHeight * r, addr)
|
|
42 x = addressWidth + 4
|
|
43 for b in bts:
|
|
44 b = '{0:02X}'.format(b)
|
|
45 painter.drawText(x, 2 + self.fontHeight * r, b)
|
|
46 x += 3 * self.fontWidth
|
|
47 x = addressWidth + byteWidth + 4
|
|
48 for b in bts:
|
|
49 b = '{0}'.format(chr(b))
|
|
50 painter.drawText(x, 2 + self.fontHeight * r, b)
|
|
51 x += 1 * self.fontWidth
|
105
|
52 def getBytes(self, offset):
|
|
53 if self.hexfile.regions:
|
|
54 r = self.hexfile.regions[0]
|
|
55 chunks = [r.data[p:p+16] for p in range(0, len(r.data), 16)]
|
|
56 if len(chunks) > offset:
|
|
57 return chunks[offset]
|
|
58 return bytes()
|
104
|
59 def setHexFile(self, hf):
|
105
|
60 self.hexfile = hf
|
104
|
61 self.update()
|
|
62
|
124
|
63 class HexFileModel(QAbstractTableModel):
|
|
64 def __init__(self):
|
|
65 super().__init__()
|
|
66 self.hexFile = None
|
|
67 def setHexFile(self, hf):
|
|
68 self.hexFile = hf
|
|
69 self.modelReset.emit()
|
|
70 def getHexFile(self):
|
|
71 return self.hexFile
|
|
72 HexFile = property(getHexFile, setHexFile)
|
|
73 def rowCount(self, parent):
|
|
74 if self.hexFile:
|
|
75 region = self.hexFile.regions[-1]
|
|
76 r = len(region.data)
|
|
77 s = r >> 4
|
|
78 if r % 16 != 0:
|
|
79 s += 1
|
|
80 return s
|
|
81 return 0
|
|
82 def columnCount(self, parent):
|
|
83 return 16 + 1
|
|
84 def headerData(self, section, orientation, role):
|
|
85 if role == Qt.DisplayRole:
|
|
86 if orientation == Qt.Horizontal:
|
|
87 if section in range(16):
|
|
88 return '{0:X}'.format(section)
|
|
89 elif section == 16:
|
|
90 return 'Ascii'
|
|
91 elif orientation == Qt.Vertical:
|
|
92 region = self.hexFile.regions[-1]
|
|
93 addr = region.address + 16 * section
|
|
94 return '0x{0:X}'.format(addr)
|
|
95 def data(self, index, role):
|
|
96 if index.isValid():
|
|
97 row = index.row()
|
|
98 col = index.column()
|
|
99 region = self.hexFile.regions[-1]
|
|
100 chunk = region.data[row * 16: row * 16 + 16]
|
|
101
|
|
102 if role == Qt.DisplayRole:
|
|
103 if col in range(16):
|
|
104 return '{0:02X}'.format(chunk[col])
|
|
105 else:
|
|
106 s = chunk.decode(encoding='ascii', errors='replace')
|
|
107 return s
|
|
108
|
104
|
109 class BinViewMain(QMainWindow):
|
|
110 def __init__(self):
|
|
111 super().__init__()
|
|
112 self.bv = BinViewer()
|
124
|
113 #self.setCentralWidget(self.bv)
|
|
114 tableView = QTableView()
|
|
115 self.setCentralWidget(tableView)
|
|
116 self.hfm = HexFileModel()
|
|
117 self.hfm.modelReset.connect(tableView.resizeColumnsToContents)
|
|
118 tableView.setModel(self.hfm)
|
104
|
119 mb = self.menuBar()
|
|
120 fileMenu = mb.addMenu("File")
|
|
121
|
|
122 def addMenuEntry(name, menu, callback, shortcut=None):
|
|
123 a = QAction(name, self)
|
|
124 menu.addAction(a)
|
|
125 a.triggered.connect(callback)
|
|
126 if shortcut: a.setShortcut(shortcut)
|
|
127 addMenuEntry("Open", fileMenu, self.openFile, QKeySequence(QKeySequence.Open))
|
|
128 def openFile(self):
|
|
129 filename = QFileDialog.getOpenFileName(self, "Open hex file...", "*.hex", "Intel hexfiles (*.hex)")
|
|
130 if filename:
|
|
131 h = hexfile.HexFile(filename)
|
109
|
132 @pyqtProperty(str)
|
|
133 def leetValue(self):
|
|
134 return '1337'
|
104
|
135
|
|
136 if __name__ == '__main__':
|
|
137 app = QApplication(sys.argv)
|
|
138 bv = BinViewMain()
|
|
139 bv.show()
|
124
|
140 hf = hexfile.HexFile('audio.hex')
|
|
141 #bv.bv.setHexFile(
|
|
142 bv.hfm.HexFile = hf
|
109
|
143 qpv = QtPropertyViewer()
|
|
144 qpv.propertyModel.InspectedWidget = bv
|
124
|
145 #qpv.show()
|
104
|
146 app.exec_()
|
|
147
|