forked from mtivadar/qiew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileFormat.py
150 lines (103 loc) · 3.98 KB
/
FileFormat.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
from yapsy.IPlugin import IPlugin
from PyQt4 import QtGui, QtCore
import PyQt4
import os, sys
#import DisasmViewMode
class Observer:
def changeViewMode(self, viewMode):
self._viewMode = viewMode
self.viewMode = viewMode
#NotImplementedError('method not implemented.')
class IDisasm():
# tells disasm view what to decode
def hintDisasm(self):
return None, None
#return DisasmViewMode.Disasm_x86_32bit
# calculates va for disasm mode
def hintDisasmVA(self, offset):
return offset
# returns ascii string in disasm view (from a va)
def stringFromVA(self, va):
return ''
def disasmVAtoFA(self, va):
return None
def disasmSymbol(self, va):
return None
class FileFormat(IPlugin, IDisasm, Observer):
name = ''
_Shortcuts = []
redbrush = QtGui.QBrush(QtGui.QColor(128, 0, 0))
yellowpen = QtGui.QPen(QtGui.QColor(255, 255, 0))
def isRecognized(self):
return False
def init(self, viewMode, parent):
pass
def getShortcuts(self):
return self._Shortcuts
def registerShortcuts(self, parent):
pass
class DialogGoto(QtGui.QDialog):
def __init__(self, parent, plugin):
super(DialogGoto, self).__init__(parent)
self.parent = parent
self.plugin = plugin
self.oshow = super(DialogGoto, self).show
root = os.path.dirname(sys.argv[0])
self.ui = PyQt4.uic.loadUi(os.path.join(root, 'plugins', 'format', 'goto.ui'), baseinstance=self)
self.konstants = {}
self.GoTos = {'FileAddress' : self.fa}
self.initUI()
def show(self):
# TODO: remember position? resize plugin windows when parent resize?
pwidth = self.parent.parent.size().width()
pheight = self.parent.parent.size().height()
width = self.ui.size().width()
height = self.ui.size().height()
self.setGeometry(pwidth - width - 15, pheight - height, width, height)
self.setFixedSize(width, height)
self.ui.lineEdit.setFocus()
self.oshow()
def initUI(self):
self.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
shortcut = QtGui.QShortcut(QtGui.QKeySequence("Alt+G"), self, self.close, self.close)
self.ui.setWindowTitle('GoTo')
QtCore.QObject.connect(self.ui.lineEdit, QtCore.SIGNAL('returnPressed()'), self.onReturnPressed)
def onReturnPressed(self):
expr = str(self.ui.lineEdit.text())
import ast
import operator as op
# supported operators
operators = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul,
ast.Div: op.div, ast.Pow: op.pow, ast.USub: op.neg}
def eval_expr(expr):
return eval_(ast.parse(expr, mode='eval').body)
def eval_(node):
if isinstance(node, ast.Num):
return node.n
elif isinstance(node, ast.BinOp):
return operators[type(node.op)](eval_(node.left), eval_(node.right))
elif isinstance(node, ast.UnaryOp):
return operators[type(node.op)](eval_(node.operand))
elif isinstance(node, object):
# handle constants
k = str(node.id).upper()
if k in self.konstants:
return self.konstants[k](k)
else:
raise TypeError(node)
else:
raise TypeError(node)
try:
result = eval_expr(expr)
except Exception, e:
self.ui.label.setText('error.')
return
self.ui.label.setText('{0} ({1})'.format(hex(result), result))
self.onResult(result)
def onResult(self, result):
gotoType = str(self.ui.comboBox.currentText())
result = self.GoTos[gotoType](result)
if result is not None:
self.plugin.viewMode.goTo(result)
def fa(self, offset):
return offset