annotate python/hexviewer.py @ 126:bbf4c9b138d4

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