summaryrefslogtreecommitdiff
path: root/src/coff_image.h
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2024-09-15 00:12:25 +0300
committerOxore <oxore@protonmail.com>2024-11-21 00:18:24 +0300
commit559139fce64d685708c283397033bf152afd00a3 (patch)
treee5f2b1d35adef134512bf4e1c2990cf86a95bd1b /src/coff_image.h
parent85614fc367ba53d0d5ca48873337571fc7f4f5b7 (diff)
Add initial COFF support (coff2bin, readcoff)
Diffstat (limited to 'src/coff_image.h')
-rw-r--r--src/coff_image.h45
1 files changed, 45 insertions, 0 deletions
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};
+ }
+};
+
+}