import os import struct import array lua_opcode_types = [ "ABC", "ABx", "ABC", "ABC", "ABC", "ABx", "ABC", "ABx", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "AsBx", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "AsBx", "AsBx", "ABC", "ABC", "ABC", "ABx", "ABC" ] lua_opcode_names = [ "MOVE", "LOADK", "LOADBOOL", "LOADNIL", "GETUPVAL", "GETGLOBAL", "GETTABLE", "SETGLOBAL", "SETUPVAL", "SETTABLE", "NEWTABLE", "SELF", "ADD", "SUB", "MUL", "DIV", "MOD", "POW", "UNM", "NOT", "LEN", "CONCAT", "JMP", "EQ", "LT", "LE", "TEST", "TESTSET", "CALL", "TAILCALL", "RETURN", "FORLOOP", "FORPREP", "TFORLOOP", "SETLIST", "CLOSE", "CLOSURE", "VARARG" ] # at [p]osition size of k def get_bits(num, p, k): # convert number into binary first binary = bin(num) # remove first two characters binary = binary[2:] # fill in missing bits for i in range(32 - len(binary)): binary = '0' + binary end = len(binary) - p + 1 start = len(binary) - k + 1 # extract k bit sub-string kBitSubStr = binary[start : end] # convert extracted sub-string into decimal again return (int(kBitSubStr,2) % 256) class LuaCompiler: def __init__(self): self.luac = "luac5.1" self.o_flag = "-o" self.temp_out = "out.luac" self.chunks = [] self.index = 0 def get_byte(self): b = self.bytecode[self.index] self.index = self.index + 1 return b 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) 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 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) 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 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) 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 def get_float(self): f = struct.unpack('