From 88d669f3c88a1c5505a9242a05261d6fea809d85 Mon Sep 17 00:00:00 2001 From: CPunch Date: Fri, 9 Apr 2021 09:21:47 -0500 Subject: [PATCH] Refactoring, remove compiler support - LuaCompiler has been renamed to LuaUndump - LuaUndump has been striped of the connection to luac, focusing on strictly parsing Lua 5.1 bytecode, not compiling. - Updated README.md to reflect changes --- README.md | 11 ++++++----- luac.py | 16 +++++----------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index ec96bba..21f0dca 100644 --- a/README.md +++ b/README.md @@ -2,25 +2,26 @@ Parses Lua 5.1 bytecode # Example + +loads a raw lua bytecode dump ```python import luac -lc = luac.LuaCompiler() -chunk = lc.compileC("test.lua") +lc = luac.LuaUndump() +chunk = lc.loadFile("test.luac") print("\n===== [[Disassembly]] =====\n") lc.print_dissassembly() ``` -or just parse lua bytecode - +or just parse lua bytecode from an array ```python import luac bytecode = "27\\76\\117\\97\\81\\0\\1\\4\\8\\4\\8\\0\\21\\0\\0\\0\\0\\0\\0\\0\\112\\114\\105\\110\\116\\40\\39\\104\\101\\108\\108\\111\\32\\119\\111\\114\\108\\100\\39\\41\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\2\\2\\4\\0\\0\\0\\5\\0\\0\\0\\65\\64\\0\\0\\28\\64\\0\\1\\30\\0\\128\\0\\2\\0\\0\\0\\4\\6\\0\\0\\0\\0\\0\\0\\0\\112\\114\\105\\110\\116\\0\\4\\12\\0\\0\\0\\0\\0\\0\\0\\104\\101\\108\\108\\111\\32\\119\\111\\114\\108\\100\\0\\0\\0\\0\\0\\4\\0\\0\\0\\1\\0\\0\\0\\1\\0\\0\\0\\1\\0\\0\\0\\1\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0".split('\\') bytecode = list(map(int, bytecode)) -lc = luac.LuaCompiler() +lc = luac.LuaUndump() chunk = lc.decode_bytecode(bytecode) lc.print_dissassembly() diff --git a/luac.py b/luac.py index 9dd05be..014b91f 100644 --- a/luac.py +++ b/luac.py @@ -1,4 +1,3 @@ -import os import struct import array @@ -49,11 +48,8 @@ def get_bits(num, p, k): # convert extracted sub-string into decimal again return (int(kBitSubStr,2)) -class LuaCompiler: +class LuaUndump: def __init__(self): - self.luac = "luac5.1" - self.o_flag = "-o" - self.temp_out = "out.luac" self.chunks = [] self.chunk = {} self.index = 0 @@ -63,7 +59,7 @@ class LuaCompiler: print("==== [[" + str(chunk['NAME']) + "]] ====\n") for z in chunk['PROTOTYPES']: print("** decoding proto\n") - LuaCompiler.dis_chunk(chunk['PROTOTYPES'][z]) + LuaUndump.dis_chunk(chunk['PROTOTYPES'][z]) print("\n==== [[" + str(chunk['NAME']) + "'s constants]] ====\n") for z in chunk['CONSTANTS']: @@ -261,13 +257,11 @@ class LuaCompiler: self.chunk = self.decode_chunk() return self.chunk - def compileC(self, luafile): - os.system(self.luac + " " + self.o_flag + " " + self.temp_out + " " + luafile) - - with open(self.temp_out, 'rb') as luac_file: + def loadFile(self, luaCFile): + with open(luaCFile, 'rb') as luac_file: bytecode = luac_file.read() return self.decode_rawbytecode(bytecode) def print_dissassembly(self): - LuaCompiler.dis_chunk(self.chunk) + LuaUndump.dis_chunk(self.chunk)