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"):
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):
b = self.bytecode[self.index]
self.index = self.index + 1
return b
return self.loadBlock(1)[0]
def get_int32(self):
i = 0
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:
i = int.from_bytes(self.bytecode[self.index:self.index+4], byteorder='little', signed=False)
self.index = self.index + self.int_size
return i
return int.from_bytes(self.loadBlock(4), byteorder='little', signed=False)
def get_int(self):
i = 0
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:
i = int.from_bytes(self.bytecode[self.index:self.index+self.int_size], byteorder='little', signed=False)
self.index = self.index + self.int_size
return i
return int.from_bytes(self.loadBlock(self.int_size), byteorder='little', signed=False)
def get_size_t(self):
s = ''
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:
s = int.from_bytes(self.bytecode[self.index:self.index+self.size_t], byteorder='little', signed=False)
self.index = self.index + self.size_t
return s
return int.from_bytes(self.loadBlock(self.size_t), byteorder='little', signed=False)
def get_double(self):
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:
f = struct.unpack('<d', bytearray(self.bytecode[self.index:self.index+8]))
self.index = self.index + 8
return f[0]
return struct.unpack('<d', self.loadBlock(8))[0]
def get_string(self, size):
if (size == None):
@ -130,9 +122,7 @@ class LuaCompiler:
if (size == 0):
return None
s = "".join(chr(x) for x in self.bytecode[self.index:self.index+size])
self.index = self.index + size
return s
return "".join(chr(x) for x in self.loadBlock(size))
def decode_chunk(self):
chunk = {
@ -262,7 +252,6 @@ class LuaCompiler:
self.l_number_size = self.get_byte() # size of lua_Number
self.integral_flag = self.get_byte()
print("Lua VM version: ", hex(self.vm_version))
print("Big Endian: ", self.big_endian)
print("int_size: ", self.int_size)