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

41
luac.py
View File

@ -84,45 +84,37 @@ class LuaCompiler:
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):
@ -130,9 +122,7 @@ class LuaCompiler:
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 = {
@ -262,7 +252,6 @@ class LuaCompiler:
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)
print("int_size: ", self.int_size) print("int_size: ", self.int_size)