Editorconfig

This commit is contained in:
Sajid 2024-09-07 18:21:08 +06:00
parent 0f9a53f75a
commit 1e24423ea7
8 changed files with 137 additions and 127 deletions

10
.editorconfig Normal file
View File

@ -0,0 +1,10 @@
# editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 4
insert_final_newline = true
end_of_line = lf

View File

@ -5,37 +5,37 @@
int main() int main()
{ {
// TODO: ELFs, symbols, sections, a lot // TODO: ELFs, symbols, sections, a lot
const auto file = LoadFile("default.xex"); const auto file = LoadFile("default.xex");
const auto image = Xex2LoadImage(file.data()); const auto image = Xex2LoadImage(file.data());
auto* headers = (IMAGE_NT_HEADERS32*)(image.get() + ((IMAGE_DOS_HEADER*)image.get())->e_lfanew); auto* headers = (IMAGE_NT_HEADERS32*)(image.get() + ((IMAGE_DOS_HEADER*)image.get())->e_lfanew);
auto numSections = headers->FileHeader.NumberOfSections; auto numSections = headers->FileHeader.NumberOfSections;
auto* sections = (IMAGE_SECTION_HEADER*)(headers + 1); auto* sections = (IMAGE_SECTION_HEADER*)(headers + 1);
auto base = headers->OptionalHeader.ImageBase; auto base = headers->OptionalHeader.ImageBase;
for (size_t i = 0; i < numSections; i++) for (size_t i = 0; i < numSections; i++)
{ {
const auto& section = sections[i]; const auto& section = sections[i];
std::printf("Section %.8s\n", reinterpret_cast<const char*>(section.Name)); std::printf("Section %.8s\n", reinterpret_cast<const char*>(section.Name));
std::printf("\t%X-%X\n", base + section.VirtualAddress, base + section.VirtualAddress + section.Misc.VirtualSize); std::printf("\t%X-%X\n", base + section.VirtualAddress, base + section.VirtualAddress + section.Misc.VirtualSize);
auto* data = image.get() + section.VirtualAddress; // XEX is weird auto* data = image.get() + section.VirtualAddress; // XEX is weird
ppc::SetDetail(true); ppc::SetDetail(true);
if (section.Characteristics & IMAGE_SCN_CNT_CODE) if (section.Characteristics & IMAGE_SCN_CNT_CODE)
{ {
cs_insn* instructions; cs_insn* instructions;
size_t n = ppc::Disassemble(data, section.SizeOfRawData, base + section.VirtualAddress, 0, &instructions); size_t n = ppc::Disassemble(data, section.SizeOfRawData, base + section.VirtualAddress, 0, &instructions);
for(size_t i = 0; i < n; i++) for(size_t i = 0; i < n; i++)
{ {
printf("\t%s\n", instructions[i].mnemonic); printf("\t%s\n", instructions[i].mnemonic);
} }
cs_free(instructions, n); cs_free(instructions, n);
} }
} }
return 0; return 0;
} }

View File

@ -1,4 +1,4 @@
int main() int main()
{ {
return 0; return 0;
} }

View File

@ -4,20 +4,20 @@ DisassemblerEngine ppc::gPPCBigEndianDisassembler{ CS_ARCH_PPC, CS_MODE_BIG_ENDI
DisassemblerEngine::DisassemblerEngine(cs_arch arch, cs_mode mode) DisassemblerEngine::DisassemblerEngine(cs_arch arch, cs_mode mode)
{ {
cs_open(arch, mode, &handle); cs_open(arch, mode, &handle);
} }
size_t DisassemblerEngine::Disassemble(const uint8_t* code, size_t size, uint64_t base, size_t count, cs_insn** instructions) const size_t DisassemblerEngine::Disassemble(const uint8_t* code, size_t size, uint64_t base, size_t count, cs_insn** instructions) const
{ {
return cs_disasm(handle, code, size, base, count, instructions); return cs_disasm(handle, code, size, base, count, instructions);
} }
void DisassemblerEngine::SetOption(cs_opt_type option, size_t value) void DisassemblerEngine::SetOption(cs_opt_type option, size_t value)
{ {
cs_option(handle, option, value); cs_option(handle, option, value);
} }
DisassemblerEngine::~DisassemblerEngine() DisassemblerEngine::~DisassemblerEngine()
{ {
cs_close(&handle); cs_close(&handle);
} }

View File

@ -3,32 +3,32 @@
struct DisassemblerEngine struct DisassemblerEngine
{ {
csh handle{}; csh handle{};
DisassemblerEngine(const DisassemblerEngine&) = default; DisassemblerEngine(const DisassemblerEngine&) = default;
DisassemblerEngine& operator=(const DisassemblerEngine&) = delete; DisassemblerEngine& operator=(const DisassemblerEngine&) = delete;
DisassemblerEngine(cs_arch arch, cs_mode mode); DisassemblerEngine(cs_arch arch, cs_mode mode);
~DisassemblerEngine(); ~DisassemblerEngine();
size_t Disassemble(const uint8_t* code, size_t size, uint64_t base, size_t count, cs_insn** instructions) const; size_t Disassemble(const uint8_t* code, size_t size, uint64_t base, size_t count, cs_insn** instructions) const;
void SetOption(cs_opt_type option, size_t value); void SetOption(cs_opt_type option, size_t value);
void SetDetail(bool value) void SetDetail(bool value)
{ {
SetOption(CS_OPT_DETAIL, value); SetOption(CS_OPT_DETAIL, value);
} }
}; };
namespace ppc namespace ppc
{ {
extern DisassemblerEngine gPPCBigEndianDisassembler; extern DisassemblerEngine gPPCBigEndianDisassembler;
static size_t Disassemble(const uint8_t* code, size_t size, uint64_t base, size_t count, cs_insn** instructions) static size_t Disassemble(const uint8_t* code, size_t size, uint64_t base, size_t count, cs_insn** instructions)
{ {
return gPPCBigEndianDisassembler.Disassemble(code, size, base, count, instructions); return gPPCBigEndianDisassembler.Disassemble(code, size, base, count, instructions);
} }
static void SetDetail(bool value) static void SetDetail(bool value)
{ {
gPPCBigEndianDisassembler.SetDetail(value); gPPCBigEndianDisassembler.SetDetail(value);
} }
} }

View File

@ -3,23 +3,23 @@
inline static std::vector<uint8_t> LoadFile(const char* path) inline static std::vector<uint8_t> LoadFile(const char* path)
{ {
std::vector<uint8_t> data{}; std::vector<uint8_t> data{};
auto* stream = fopen(path, "rb"); auto* stream = fopen(path, "rb");
if (stream == nullptr) if (stream == nullptr)
{ {
return data; return data;
} }
fseek(stream, 0, SEEK_END); fseek(stream, 0, SEEK_END);
const auto size = ftell(stream); const auto size = ftell(stream);
fseek(stream, 0, SEEK_SET); fseek(stream, 0, SEEK_SET);
data.resize(size); data.resize(size);
fread(data.data(), 1, data.size(), stream); fread(data.data(), 1, data.size(), stream);
fclose(stream); fclose(stream);
return data; return data;
} }

View File

@ -4,35 +4,35 @@
#include <bit> #include <bit>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#else #else
#define near #define near
#define far #define far
typedef char CHAR; typedef char CHAR;
typedef wchar_t WCHAR; typedef wchar_t WCHAR;
typedef unsigned long DWORD; typedef unsigned long DWORD;
typedef int BOOL; typedef int BOOL;
typedef unsigned char BYTE; typedef unsigned char BYTE;
typedef unsigned short WORD; typedef unsigned short WORD;
typedef float FLOAT; typedef float FLOAT;
typedef FLOAT* PFLOAT; typedef FLOAT* PFLOAT;
typedef BOOL near* PBOOL; typedef BOOL near* PBOOL;
typedef BOOL far* LPBOOL; typedef BOOL far* LPBOOL;
typedef BYTE near* PBYTE; typedef BYTE near* PBYTE;
typedef BYTE far* LPBYTE; typedef BYTE far* LPBYTE;
typedef int near* PINT; typedef int near* PINT;
typedef int far* LPINT; typedef int far* LPINT;
typedef WORD near* PWORD; typedef WORD near* PWORD;
typedef WORD far* LPWORD; typedef WORD far* LPWORD;
typedef long far* LPLONG; typedef long far* LPLONG;
typedef DWORD near* PDWORD; typedef DWORD near* PDWORD;
typedef DWORD far* LPDWORD; typedef DWORD far* LPDWORD;
typedef void far* LPVOID; typedef void far* LPVOID;
typedef const void far* LPCVOID; typedef const void far* LPCVOID;
typedef unsigned long ULONG; typedef unsigned long ULONG;
typedef ULONG* PULONG; typedef ULONG* PULONG;
typedef signed long LONG; typedef signed long LONG;
typedef LONG* PLONG; typedef LONG* PLONG;
typedef unsigned long long ULONGLONG; typedef unsigned long long ULONGLONG;
typedef ULONGLONG* PULONGLONG; typedef ULONGLONG* PULONGLONG;

View File

@ -3,51 +3,51 @@
std::unique_ptr<uint8_t[]> Xex2LoadImage(const uint8_t* data) std::unique_ptr<uint8_t[]> Xex2LoadImage(const uint8_t* data)
{ {
auto* header = reinterpret_cast<const XEX_HEADER*>(data); auto* header = reinterpret_cast<const XEX_HEADER*>(data);
auto* security = reinterpret_cast<const XEX2_SECURITY_INFO*>(data + header->AddressOfSecurityInfo); auto* security = reinterpret_cast<const XEX2_SECURITY_INFO*>(data + header->AddressOfSecurityInfo);
const auto* compressionInfo = Xex2FindOptionalHeader<XEX_FILE_FORMAT_INFO>(header, XEX_HEADER_FILE_FORMAT_INFO); const auto* compressionInfo = Xex2FindOptionalHeader<XEX_FILE_FORMAT_INFO>(header, XEX_HEADER_FILE_FORMAT_INFO);
std::unique_ptr<uint8_t[]> result{}; std::unique_ptr<uint8_t[]> result{};
size_t imageSize = security->SizeOfImage; size_t imageSize = security->SizeOfImage;
if (compressionInfo != nullptr) if (compressionInfo != nullptr)
{ {
assert(compressionInfo->CompressionType >= XEX_COMPRESSION_BASIC); assert(compressionInfo->CompressionType >= XEX_COMPRESSION_BASIC);
assert(compressionInfo->EncryptionType == XEX_ENCRYPTION_NONE); assert(compressionInfo->EncryptionType == XEX_ENCRYPTION_NONE);
if (compressionInfo->CompressionType == XEX_COMPRESSION_NONE) if (compressionInfo->CompressionType == XEX_COMPRESSION_NONE)
{ {
result = std::make_unique<uint8_t[]>(imageSize); result = std::make_unique<uint8_t[]>(imageSize);
memcpy(result.get(), data + header->SizeOfHeader, imageSize); memcpy(result.get(), data + header->SizeOfHeader, imageSize);
} }
else if (compressionInfo->CompressionType == XEX_COMPRESSION_BASIC) else if (compressionInfo->CompressionType == XEX_COMPRESSION_BASIC)
{ {
auto* blocks = reinterpret_cast<const XEX_BASIC_FILE_COMPRESSION_INFO*>(compressionInfo + 1); auto* blocks = reinterpret_cast<const XEX_BASIC_FILE_COMPRESSION_INFO*>(compressionInfo + 1);
const size_t numBlocks = (compressionInfo->SizeOfHeader / sizeof(XEX_BASIC_FILE_COMPRESSION_INFO)) - 1; const size_t numBlocks = (compressionInfo->SizeOfHeader / sizeof(XEX_BASIC_FILE_COMPRESSION_INFO)) - 1;
imageSize = 0; imageSize = 0;
for (size_t i = 0; i < numBlocks; i++) for (size_t i = 0; i < numBlocks; i++)
{ {
imageSize += blocks[i].SizeOfData + blocks[i].SizeOfPadding; imageSize += blocks[i].SizeOfData + blocks[i].SizeOfPadding;
} }
result = std::make_unique<uint8_t[]>(imageSize); result = std::make_unique<uint8_t[]>(imageSize);
auto* srcData = data + header->SizeOfHeader; auto* srcData = data + header->SizeOfHeader;
auto* destData = result.get(); auto* destData = result.get();
for (size_t i = 0; i < numBlocks; i++) for (size_t i = 0; i < numBlocks; i++)
{ {
memcpy(destData, srcData, blocks[i].SizeOfData); memcpy(destData, srcData, blocks[i].SizeOfData);
srcData += blocks[i].SizeOfData; srcData += blocks[i].SizeOfData;
destData += blocks[i].SizeOfData; destData += blocks[i].SizeOfData;
memset(destData, 0, blocks[i].SizeOfPadding); memset(destData, 0, blocks[i].SizeOfPadding);
destData += blocks[i].SizeOfPadding; destData += blocks[i].SizeOfPadding;
} }
} }
} }
return result; return result;
} }