Added loadBlock for abstraction purposes

This was for a personal project where the bytecode was XOR'd with a key. Each call
to lundump.c:LoadBlock() xor'd the data against a static key. This lets me easily
reimplement that behavior.
This commit is contained in:
CPunch 2021-04-08 23:16:14 -05:00
parent 82fea6cf8f
commit 005567fee1

43
luac.py
View File

@ -83,56 +83,46 @@ class LuaCompiler:
print(lua_opcode_names[i['OPCODE']], i['A'], -i['Bx']-1) print(lua_opcode_names[i['OPCODE']], i['A'], -i['Bx']-1)
elif (i['TYPE'] == "AsBx"): elif (i['TYPE'] == "AsBx"):
print("AsBx", lua_opcode_names[i['OPCODE']], i['A'], i['sBx']) print("AsBx", lua_opcode_names[i['OPCODE']], i['A'], i['sBx'])
def loadBlock(self, sz):
temp = bytearray(self.bytecode[self.index:self.index+sz])
self.index = self.index + sz
return temp
def get_byte(self): def get_byte(self):
b = self.bytecode[self.index] return self.loadBlock(1)[0]
self.index = self.index + 1
return b
def get_int32(self): def get_int32(self):
i = 0
if (self.big_endian): if (self.big_endian):
i = int.from_bytes(self.bytecode[self.index:self.index+4], byteorder='big', signed=False) return int.from_bytes(self.loadBlock(4), byteorder='big', signed=False)
else: else:
i = int.from_bytes(self.bytecode[self.index:self.index+4], byteorder='little', signed=False) return int.from_bytes(self.loadBlock(4), byteorder='little', signed=False)
self.index = self.index + self.int_size
return i
def get_int(self): def get_int(self):
i = 0
if (self.big_endian): if (self.big_endian):
i = int.from_bytes(self.bytecode[self.index:self.index+self.int_size], byteorder='big', signed=False) return int.from_bytes(self.loadBlock(self.int_size), byteorder='big', signed=False)
else: else:
i = int.from_bytes(self.bytecode[self.index:self.index+self.int_size], byteorder='little', signed=False) return int.from_bytes(self.loadBlock(self.int_size), byteorder='little', signed=False)
self.index = self.index + self.int_size
return i
def get_size_t(self): def get_size_t(self):
s = ''
if (self.big_endian): if (self.big_endian):
s = int.from_bytes(self.bytecode[self.index:self.index+self.size_t], byteorder='big', signed=False) return int.from_bytes(self.loadBlock(self.size_t), byteorder='big', signed=False)
else: else:
s = int.from_bytes(self.bytecode[self.index:self.index+self.size_t], byteorder='little', signed=False) return int.from_bytes(self.loadBlock(self.size_t), byteorder='little', signed=False)
self.index = self.index + self.size_t
return s
def get_double(self): def get_double(self):
if self.big_endian: if self.big_endian:
f = struct.unpack('>d', bytearray(self.bytecode[self.index:self.index+8])) return struct.unpack('>d', self.loadBlock(8))[0]
else: else:
f = struct.unpack('<d', bytearray(self.bytecode[self.index:self.index+8])) return struct.unpack('<d', self.loadBlock(8))[0]
self.index = self.index + 8
return f[0]
def get_string(self, size): def get_string(self, size):
if (size == None): if (size == None):
size = self.get_size_t() size = self.get_size_t()
if (size == 0): if (size == 0):
return None return None
s = "".join(chr(x) for x in self.bytecode[self.index:self.index+size]) return "".join(chr(x) for x in self.loadBlock(size))
self.index = self.index + size
return s
def decode_chunk(self): def decode_chunk(self):
chunk = { chunk = {
@ -261,7 +251,6 @@ class LuaCompiler:
self.instr_size = self.get_byte() # gets size of instructions self.instr_size = self.get_byte() # gets size of instructions
self.l_number_size = self.get_byte() # size of lua_Number self.l_number_size = self.get_byte() # size of lua_Number
self.integral_flag = self.get_byte() self.integral_flag = self.get_byte()
print("Lua VM version: ", hex(self.vm_version)) print("Lua VM version: ", hex(self.vm_version))
print("Big Endian: ", self.big_endian) print("Big Endian: ", self.big_endian)