From 559139fce64d685708c283397033bf152afd00a3 Mon Sep 17 00:00:00 2001 From: Oxore Date: Sun, 15 Sep 2024 00:12:25 +0300 Subject: Add initial COFF support (coff2bin, readcoff) --- src/coff_image.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/coff_image.h (limited to 'src/coff_image.h') diff --git a/src/coff_image.h b/src/coff_image.h new file mode 100644 index 0000000..83b10dd --- /dev/null +++ b/src/coff_image.h @@ -0,0 +1,45 @@ +#pragma once + +/* SPDX-License-Identifier: Unlicense + */ + +#include "coff.h" + +#include "data_buffer.h" + +namespace COFF { + +class Image { + const DataBuffer _data; + char *const _error; + const COFF::FileHeader _file_header; + bool _has_optional_header; + const COFF::OptionalHeader _optional_header; +public: + explicit Image(DataBuffer&&); + ~Image(); + constexpr bool IsValid() const { return _error == nullptr; } + constexpr const DataBuffer &Data() const { return _data; }; + constexpr const char *Error() const { return _error; } + constexpr const COFF::FileHeader &FileHeader() const { return _file_header; } + constexpr bool HasOptionalHeader() const { return _has_optional_header; } + constexpr const COFF::OptionalHeader &OptionalHeader() const { return _optional_header; } + constexpr const COFF::SectionHeader GetSectionHeader(unsigned index) const + { + if (index > _file_header.nsections) { + return SectionHeader{}; + } + const size_t offset = COFF::kFileHeaderSize + _file_header.optional_header_nbytes + + COFF::kSectionHeaderSize * index; + if (offset + kSectionHeaderSize > _data.buffer_size) { + return SectionHeader{}; + } + return SectionHeader::FromBytes(_data.buffer + offset); + } + constexpr DataView GetSectionDataView(const COFF::SectionHeader &header) const + { + return DataView{_data.buffer + header.section_offset, header.size}; + } +}; + +} -- cgit v1.2.3