Source
pub fn init(data: []const u8, is_loaded: bool) !Coff {
const pe_pointer_offset = 0x3C;
const pe_magic = "PE\x00\x00";
var reader: std.Io.Reader = .fixed(data);
reader.seek = pe_pointer_offset;
const coff_header_offset = try reader.takeInt(u32, .little);
reader.seek = coff_header_offset;
const is_image = mem.eql(u8, pe_magic, try reader.takeArray(4));
var coff = @This(){
.data = data,
.is_image = is_image,
.is_loaded = is_loaded,
.coff_header_offset = coff_header_offset,
};
// Do some basic validation upfront
if (is_image) {
coff.coff_header_offset = coff.coff_header_offset + 4;
const coff_header = coff.getCoffHeader();
if (coff_header.size_of_optional_header == 0) return error.MissingPEHeader;
}
// JK: we used to check for architecture here and throw an error if not x86 or derivative.
// However I am willing to take a leap of faith and let aarch64 have a shot also.
return coff;
}