collabdbg/collabdbg/collabdbg_PortableExecutable.cpp
2025-12-01 11:48:53 -05:00

45 lines
1.2 KiB
C++

#include "collabdbg_PortableExecutable.h"
#include <fstream>
#include <sstream>
#include <string>
collabdbg::Microsoft::PortableExecutable::PortableExecutable()
{
}
collabdbg::Microsoft::PortableExecutable::PortableExecutable(std::string path)
{
LoadFrom(path);
}
void collabdbg::Microsoft::PortableExecutable::LoadFrom(std::string path)
{
std::ifstream fileHandle(path);
std::stringstream fileContentHandle; fileContentHandle << fileHandle.rdbuf();
std::string fileContent = fileContentHandle.str();
fileHandle.close(); // close file asap
if (fileContent.size() <= 256) {
throw std::logic_error("File is too small to possibly be an executable of this type.");
}
std::memcpy(&dosHeader, fileContent.data(), sizeof(DOSHeader));
if (dosHeader.magic[0] != 'M' || dosHeader.magic[1] != 'Z') {
throw std::logic_error("This executable lacks a DOS header.");
}
if (fileContent.size() - sizeof(COFFHeader) < dosHeader.coffHeaderPointer) {
throw std::out_of_range("This executable's COFF header cannot possibly exist in the file.");
}
std::memcpy(&coffHeader, fileContent.data() + dosHeader.coffHeaderPointer - 1, sizeof(COFFHeader));
}
collabdbg::Microsoft::PortableExecutable::~PortableExecutable()
{
}