Skip to content

Instantly share code, notes, and snippets.

@RigoLigoRLC
Created January 27, 2024 03:49
Show Gist options
  • Save RigoLigoRLC/a355669097528e755e59faa2d2fd2a35 to your computer and use it in GitHub Desktop.
Save RigoLigoRLC/a355669097528e755e59faa2d2fd2a35 to your computer and use it in GitHub Desktop.
Synthesizer V R1 - Lua Bytecode Hexpat
struct header {
u8 initial, format, version;
u8 literal[6];
u32 intdata;
float floatdata;
u8 lintsize, insnsize, numbersize, intsize,
ptrsize;
};
struct undump {
header Header;
u8 upvalCount;
};
//undump bytecode @ 0;
#pragma description Lua 5.4 bytecode
#include <std/io.pat>
#include <std/mem.pat>
namespace impl {
// Rigo: This function is as stupid as it can fucking get
// ANYONE WRITES A FUNCTION LIKE THE WHAT IT WAS BEFORE I EDITED IT
// SHOULD BE KILLED AT THE FUCKING SPOT
fn transform_Size_array(auto array) {
u128 res = 0;
for(u8 i = 0, (i <= 3), i+=1) {
res <<= 8;
res |= array[3-i] & 0xff;
std::print(" i={}, byte={:X}, res={:X}", i, array[3-i], res);
}
return res;
};
fn transform_SizeBE_array(auto array) {
u128 res = 0;
for(u8 i = 0, (i <= 3), i+=1) {
res <<= 8;
res |= array[i] & 0x7f;
std::print(" i={}, res={:X}", i, res);
}
return res;
};
fn format_Size(auto leb128) {
u128 res = impl::transform_Size_array(leb128.array);
return std::format("{} ({:#x})", res, res);
};
fn format_SizeBE(auto leb128) {
u128 res = impl::transform_SizeBE_array(leb128.array);
return std::format("{} ({:#x})", res, res);
};
fn transform_Size(auto leb128) {
return impl::transform_Size_array(leb128.array);
};
fn transform_SizeBE(auto leb128) {
return impl::transform_SizeBE_array(leb128.array);
};
fn format_LuaString(auto string) {
if (string.size == 0) {
return "None";
}
return std::format("\"{}\"", string.data);
};
fn format_Constant(auto constant) {
return constant.data;
};
fn format_Version(auto ver) {
return std::format("Ver. {}.{}", ver.major, ver.minor);
};
}
using LuaFunction;
struct Size {
u8 array[4];
} [[sealed, format("impl::format_Size"), transform("impl::transform_Size")]];
struct SizeBE {
u8 array[4];
} [[sealed, format("impl::format_SizeBE"), transform("impl::transform_SizeBE")]];
bitfield Version {
minor : 4;
major : 4;
} [[format("impl::format_Version")]];
struct LuaBinaryHeader {
u32 magic;
Version version_number;
u8 format_version;
u8 error_detection_data[6];
u8 size_of_int;
u8 size_of_size_t;
u8 size_of_number;
u64 luac_int;
double luac_num;
};
struct LuaString {
u8 size;
if (size == 0) {
} else {
if ($ + size - 1 > std::mem::size()) {
std::print("String at {:X} going past end, size {:X}", $, size);
}
if (size > 0) {
char data[size-1];
}
}
}[[format("impl::format_LuaString")]];
struct LuaConstant {
u8 type;
if (type == 0x17) {
bool data;
} else if (type == 0x3b) {
float data;
} else if (type == 0x44) {
u32 data;
} else if (type == 0 || type == 0x2a) {
LuaString data;
}
};//[[format("impl::format_Constant")]]; // FIXME
struct LuaUpvalue {
u8 instack;
u8 idx;
};
struct LuaUpvalueIn {
u8 instack;
u8 idx;
u8 wtf[3];
};
struct Vector<T> {
Size size;
std::print("Size {:X}", size);
T values[size];
};
struct VectorBE<T> {
SizeBE size;
std::print("Size {:X}", size);
T values[size];
};
struct AbsLineInfo {
Size pc;
Size line;
};
struct LocalVar {
LuaString varname;
Size startpc;
Size endpc;
};
struct LuaDebugInfo {
Vector<s8> lineInfo;
Vector<LocalVar> local_vars;
Vector<LuaString> upvalues;
};
struct LuaFunctionIn1 {
LuaString source;
u8 maxstacksize;
u8 number_of_parameters;
u8 is_vararg;
Size last_line_defined;
Size line_defined;
Vector<LuaFunction> protos;
LuaDebugInfo debugInfo;
Vector<LuaUpvalue> upvalues;
Vector<LuaConstant> constants;
Vector<u32> code;
};
struct LuaFunctionIn2 {
LuaString source;
u8 maxstacksize;
u8 number_of_parameters;
u8 is_vararg;
Size last_line_defined;
Size line_defined;
Vector<LuaFunction> protos;
LuaDebugInfo debugInfo;
Vector<LuaUpvalue> upvalues;
Vector<LuaConstant> constants;
Vector<u32> code;
};
struct LuaFunctionOut {
LuaString source;
u8 maxstacksize;
u8 number_of_parameters;
u8 is_vararg;
Size last_line_defined;
Size line_defined;
// Size protoSize;
// LuaFunctionIn1 protos_a[50];
// LuaFunctionIn2 protos_b;
Vector<LuaFunction> protos;
LuaDebugInfo debugInfo;
Vector<LuaUpvalue> upvalues;
Vector<LuaConstant> constants;
Vector<u32> code;
};
struct LuaFunction {
LuaString source;
u8 maxstacksize;
u8 number_of_parameters;
u8 is_vararg;
Size last_line_defined;
Size line_defined;
Vector<LuaFunction> protos;
LuaDebugInfo debugInfo;
Vector<LuaUpvalue> upvalues;
Vector<LuaConstant> constants;
Vector<u32> code;
};
struct LuaFile {
//LuaBinaryHeader header;
header Header;
u8 size_of_upvalues;
LuaFunctionOut func;
};
LuaFile file @ 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment