Tabula Sonora 0.1.0
A native C++20 implementation of the Roland Sound Canvas VA synth voice
Loading...
Searching...
No Matches
rom_image.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <cstdint>
6#include <memory>
7#include <span>
8#include <stdexcept>
9#include <string>
10#include <string_view>
11#include <vector>
12
13namespace ts {
14
24
26class RomIdentityError : public std::runtime_error {
27public:
28 explicit RomIdentityError(const std::string& message) : std::runtime_error(message) {}
29};
30
39class RomImage {
40public:
45 [[nodiscard]] static RomImage open(const std::string& path,
47 const TableManifest* manifest = nullptr);
48
53 [[nodiscard]] static RomImage from_memory(std::span<const std::uint8_t> image,
55 const TableManifest* manifest = nullptr,
56 std::string name = "<memory>");
57
58 RomImage(RomImage&&) noexcept;
59 RomImage& operator=(RomImage&&) noexcept;
60 RomImage(const RomImage&) = delete;
61 RomImage& operator=(const RomImage&) = delete;
63
65 [[nodiscard]] const std::string& path() const noexcept { return path_; }
66
68 [[nodiscard]] std::int64_t length() const noexcept { return length_; }
69
71 [[nodiscard]] const TableManifest& manifest() const noexcept { return *manifest_; }
72
76 void read(std::int64_t file_offset, std::span<std::uint8_t> destination) const;
77
79 [[nodiscard]] std::vector<std::uint8_t> read(std::int64_t file_offset,
80 std::size_t length) const;
81
83 [[nodiscard]] std::vector<std::uint8_t> read(const TableEntry& entry) const;
84
86 [[nodiscard]] std::uint32_t read_pe_timestamp() const;
87
89 [[nodiscard]] std::string compute_sha256() const;
90
91private:
96 class Source {
97 public:
98 virtual ~Source() = default;
99 [[nodiscard]] virtual std::int64_t length() const noexcept = 0;
101 [[nodiscard]] virtual std::size_t read(std::span<std::uint8_t> destination,
102 std::int64_t offset) const = 0;
103 };
104
105 class FileSource;
106 class MemorySource;
107
108 RomImage(std::string path, std::unique_ptr<Source> source, const TableManifest& manifest);
109
110 void verify(RomVerification verification) const;
111
112 std::string path_;
113 std::unique_ptr<Source> source_;
114 const TableManifest* manifest_;
115 std::int64_t length_ = 0;
116};
117
118} // namespace ts
RomIdentityError(const std::string &message)
Definition rom_image.hpp:28
const std::string & path() const noexcept
Path, or descriptive name, the image was opened from.
Definition rom_image.hpp:65
std::int64_t length() const noexcept
Length of the file in bytes.
Definition rom_image.hpp:68
RomImage(RomImage &&) noexcept
const TableManifest & manifest() const noexcept
The offset map these reads are interpreted through.
Definition rom_image.hpp:71
std::string compute_sha256() const
Computes the SHA-256 of the whole file as lower-case hex.
static RomImage open(const std::string &path, RomVerification verification=RomVerification::full, const TableManifest *manifest=nullptr)
Opens and verifies an SCCore.dll.
static RomImage from_memory(std::span< const std::uint8_t > image, RomVerification verification=RomVerification::full, const TableManifest *manifest=nullptr, std::string name="<memory>")
Wraps an SCCore.dll already held in memory, and verifies it.
std::uint32_t read_pe_timestamp() const
Reads the COFF TimeDateStamp from the PE header, or 0 if the file is not a PE image.
std::vector< std::uint8_t > read(std::int64_t file_offset, std::size_t length) const
Reads length bytes starting at file_offset.
std::vector< std::uint8_t > read(const TableEntry &entry) const
Reads the bytes of one cached table, exactly entry.size long.
void read(std::int64_t file_offset, std::span< std::uint8_t > destination) const
Reads into a caller-supplied buffer, filling it completely.
The machine-readable map of everything the engine needs out of SCCore.dll: the pinned build identity,...
Definition table_manifest.hpp:94
Definition control_decode.hpp:7
@ none
Not XG, or an XG address this engine does nothing with.
Definition control_decode.hpp:94
RomVerification
How strictly RomImage::open checks that the file is the pinned build.
Definition rom_image.hpp:16
@ quick
Verify size and PE timestamp but skip the SHA-256. Faster; use only in tight loops.
Definition rom_image.hpp:20
@ full
Verify size, PE timestamp and the full SHA-256. The default, and the only safe choice.
Definition rom_image.hpp:18
One static table sliced byte-for-byte out of the DLL.
Definition table_manifest.hpp:38