-
Notifications
You must be signed in to change notification settings - Fork 38
/
decoder.py
31 lines (21 loc) · 802 Bytes
/
decoder.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
import dis
from instruction import Instruction
class Decoder:
"""
Class to decode raw bytes into instruction.
"""
def __init__(self, insBytes):
self.insBytes = insBytes
def decode_at(self, offset):
assert offset < len(self.insBytes)
opcode = self.insBytes[offset]
if opcode == dis.opmap['EXTENDED_ARG']:
raise Exception('EXTENDED_ARG not yet implemented')
# Invalid instruction
if opcode not in dis.opmap.values():
return Instruction(-1, None, 1)
if opcode < dis.HAVE_ARGUMENT:
return Instruction(opcode, None, 1)
if opcode >= dis.HAVE_ARGUMENT:
arg = (self.insBytes[offset + 2] << 8) | self.insBytes[offset + 1]
return Instruction(opcode, arg, 3)