2020-12-22 18:10:01 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <list>
|
2020-12-22 18:10:01 +01:00
|
|
|
#include <map>
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
2023-11-10 21:52:28 +01:00
|
|
|
#include <variant>
|
2020-12-22 18:10:01 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2021-01-30 22:39:06 +01:00
|
|
|
#include <hex/providers/overlay.hpp>
|
2022-03-04 11:36:37 +01:00
|
|
|
#include <hex/helpers/fs.hpp>
|
2022-02-27 23:25:39 +01:00
|
|
|
|
2023-11-30 14:40:07 +01:00
|
|
|
#include <nlohmann/json_fwd.hpp>
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
#include <hex/providers/undo_redo/stack.hpp>
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
namespace hex::prv {
|
|
|
|
|
2023-04-01 11:17:19 +02:00
|
|
|
/**
|
2023-05-05 22:02:18 +02:00
|
|
|
* @brief Represent the data source for a tab in the UI
|
2023-04-01 11:17:19 +02:00
|
|
|
*/
|
2020-12-22 18:10:01 +01:00
|
|
|
class Provider {
|
|
|
|
public:
|
2023-06-04 10:42:11 +02:00
|
|
|
struct Description {
|
|
|
|
std::string name;
|
|
|
|
std::string value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MenuEntry {
|
|
|
|
std::string name;
|
|
|
|
std::function<void()> callback;
|
|
|
|
};
|
|
|
|
|
2023-12-22 16:58:50 +01:00
|
|
|
constexpr static u64 MaxPageSize = 0xFFFF'FFFF'FFFF'FFFF;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
|
|
|
Provider();
|
2021-02-04 01:14:05 +01:00
|
|
|
virtual ~Provider();
|
2024-01-11 20:11:22 +01:00
|
|
|
Provider(const Provider&) = delete;
|
|
|
|
Provider& operator=(const Provider&) = delete;
|
|
|
|
|
|
|
|
Provider(Provider &&provider) noexcept = default;
|
|
|
|
Provider& operator=(Provider &&provider) noexcept = default;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2023-09-08 21:59:27 +02:00
|
|
|
/**
|
|
|
|
* @brief Opens this provider
|
|
|
|
* @note The return value of this function allows to ensure the provider is available,
|
|
|
|
* so calling Provider::isAvailable() just after a call to open() that returned true is redundant.
|
|
|
|
* @note This is not related to the EventProviderOpened event
|
|
|
|
* @return true if the provider was opened successfully, else false
|
|
|
|
*/
|
|
|
|
[[nodiscard]] virtual bool open() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Closes this provider
|
|
|
|
* @note This function is called when the user requests for a provider to be closed, e.g. by closing a tab.
|
|
|
|
* In general, this function should close the underlying data source but leave the provider in a state where
|
|
|
|
* it can be opened again later by calling the open() function again.
|
|
|
|
*/
|
|
|
|
virtual void close() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Checks if this provider is open and can be used to access data
|
|
|
|
* @return Generally, if the open() function succeeded and the data source was successfully opened, this
|
|
|
|
* function should return true
|
|
|
|
*/
|
2021-12-12 00:41:44 +01:00
|
|
|
[[nodiscard]] virtual bool isAvailable() const = 0;
|
2023-09-08 21:59:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Checks if the data in this provider can be read
|
|
|
|
* @return True if the provider is readable, false otherwise
|
|
|
|
*/
|
2022-02-01 22:09:44 +01:00
|
|
|
[[nodiscard]] virtual bool isReadable() const = 0;
|
2023-07-05 20:49:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Controls if the user can write data to this specific provider.
|
|
|
|
* This may be false for e.g. a file opened in read-only
|
|
|
|
*/
|
2022-02-01 22:09:44 +01:00
|
|
|
[[nodiscard]] virtual bool isWritable() const = 0;
|
2023-09-08 21:59:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Controls if the user can resize this provider
|
|
|
|
* @return True if the provider is resizable, false otherwise
|
|
|
|
*/
|
2021-12-12 00:41:44 +01:00
|
|
|
[[nodiscard]] virtual bool isResizable() const = 0;
|
2023-07-05 20:49:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Controls whether the provider can be saved ("saved", not "saved as")
|
|
|
|
* This is mainly used by providers that aren't buffered, and so don't need to be saved
|
|
|
|
* This function will usually return false for providers that aren't writable, but this isn't guaranted
|
|
|
|
*/
|
2023-09-08 21:59:27 +02:00
|
|
|
[[nodiscard]] virtual bool isSavable() const = 0;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2023-07-05 20:49:57 +02:00
|
|
|
/**
|
|
|
|
* @brief Controls whether we can dump data from this provider (e.g. "save as", or "export -> ..").
|
|
|
|
* Typically disabled for process with sparse data, like the Process memory provider
|
|
|
|
* where the virtual address space is several TiBs large.
|
|
|
|
* Default implementation returns true.
|
|
|
|
*/
|
|
|
|
[[nodiscard]] virtual bool isDumpable() const;
|
|
|
|
|
2023-08-25 15:35:15 +02:00
|
|
|
/**
|
|
|
|
* @brief Controls whether this provider can be saved as a recent entry
|
2023-09-08 21:59:27 +02:00
|
|
|
* Typically used for providers that do not retain data, e.g. the memory provider
|
2023-08-25 15:35:15 +02:00
|
|
|
*/
|
|
|
|
[[nodiscard]] virtual bool isSavableAsRecent() const { return true; }
|
|
|
|
|
2023-04-01 11:17:19 +02:00
|
|
|
/**
|
|
|
|
* @brief Read data from this provider, applying overlays and patches
|
|
|
|
* @param offset offset to start reading the data
|
|
|
|
* @param buffer buffer to write read data
|
|
|
|
* @param size number of bytes to read
|
|
|
|
* @param overlays apply overlays and patches is true. Same as readRaw() if false
|
|
|
|
*/
|
2023-12-02 18:42:57 +01:00
|
|
|
void read(u64 offset, void *buffer, size_t size, bool overlays = true);
|
2023-04-01 11:17:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Write data to the patches of this provider. Will not directly modify provider.
|
|
|
|
* @param offset offset to start writing the data
|
|
|
|
* @param buffer buffer to take data to write from
|
|
|
|
* @param size number of bytes to write
|
|
|
|
*/
|
2023-12-02 18:42:57 +01:00
|
|
|
void write(u64 offset, const void *buffer, size_t size);
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2023-04-01 11:17:19 +02:00
|
|
|
/**
|
|
|
|
* @brief Read data from this provider, without applying overlays and patches
|
|
|
|
* @param offset offset to start reading the data
|
|
|
|
* @param buffer buffer to write read data
|
|
|
|
* @param size number of bytes to read
|
|
|
|
*/
|
2023-09-08 21:59:27 +02:00
|
|
|
virtual void readRaw(u64 offset, void *buffer, size_t size) = 0;
|
2023-04-01 11:17:19 +02:00
|
|
|
/**
|
|
|
|
* @brief Write data directly to this provider
|
|
|
|
* @param offset offset to start writing the data
|
|
|
|
* @param buffer buffer to take data to write from
|
|
|
|
* @param size number of bytes to write
|
|
|
|
*/
|
2020-12-22 18:10:01 +01:00
|
|
|
virtual void writeRaw(u64 offset, const void *buffer, size_t size) = 0;
|
2023-09-08 21:59:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the full size of the data in this provider
|
|
|
|
* @return The size of the entire available data of this provider
|
|
|
|
*/
|
2023-12-07 12:06:26 +01:00
|
|
|
[[nodiscard]] virtual u64 getActualSize() const = 0;
|
2023-09-08 21:59:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Gets the type name of this provider
|
|
|
|
* @note This is mainly used to be stored in project files and recents to be able to later on
|
|
|
|
* recreate this exact provider type. This needs to be unique across all providers, this is usually something
|
|
|
|
* like "hex.builtin.provider.memory" or "hex.builtin.provider.file"
|
|
|
|
* @return The provider's type name
|
|
|
|
*/
|
|
|
|
[[nodiscard]] virtual std::string getTypeName() const = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Gets a human readable representation of the current provider
|
|
|
|
* @note This is mainly used to display the provider in the UI. For example, the file provider
|
|
|
|
* will return the file name here
|
|
|
|
* @return The name of the current provider
|
|
|
|
*/
|
|
|
|
[[nodiscard]] virtual std::string getName() const = 0;
|
|
|
|
|
2024-02-26 20:51:08 +01:00
|
|
|
bool resize(u64 newSize);
|
2023-12-08 10:29:44 +01:00
|
|
|
void insert(u64 offset, u64 size);
|
|
|
|
void remove(u64 offset, u64 size);
|
2023-09-08 21:59:27 +02:00
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
virtual void resizeRaw(u64 newSize) { hex::unused(newSize); }
|
2024-03-10 14:31:39 +01:00
|
|
|
virtual void insertRaw(u64 offset, u64 size);
|
|
|
|
virtual void removeRaw(u64 offset, u64 size);
|
2023-09-08 21:59:27 +02:00
|
|
|
|
|
|
|
virtual void save();
|
|
|
|
virtual void saveAs(const std::fs::path &path);
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
[[nodiscard]] Overlay *newOverlay();
|
2021-01-30 22:39:06 +01:00
|
|
|
void deleteOverlay(Overlay *overlay);
|
2023-11-25 12:43:48 +01:00
|
|
|
void applyOverlays(u64 offset, void *buffer, size_t size) const;
|
2023-11-22 08:26:31 +01:00
|
|
|
[[nodiscard]] const std::list<std::unique_ptr<Overlay>> &getOverlays() const;
|
2021-01-30 22:39:06 +01:00
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
[[nodiscard]] u64 getPageSize() const;
|
|
|
|
void setPageSize(u64 pageSize);
|
2023-05-23 11:34:30 +02:00
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
[[nodiscard]] u32 getPageCount() const;
|
|
|
|
[[nodiscard]] u32 getCurrentPage() const;
|
2020-12-22 18:10:01 +01:00
|
|
|
void setCurrentPage(u32 page);
|
|
|
|
|
2021-01-11 13:50:04 +01:00
|
|
|
virtual void setBaseAddress(u64 address);
|
2021-12-12 00:41:44 +01:00
|
|
|
[[nodiscard]] virtual u64 getBaseAddress() const;
|
|
|
|
[[nodiscard]] virtual u64 getCurrentPageAddress() const;
|
2023-12-07 13:02:12 +01:00
|
|
|
[[nodiscard]] virtual u64 getSize() const;
|
2021-12-12 00:41:44 +01:00
|
|
|
[[nodiscard]] virtual std::optional<u32> getPageOfAddress(u64 address) const;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2023-09-08 21:59:27 +02:00
|
|
|
[[nodiscard]] virtual std::vector<Description> getDataDescription() const;
|
2023-01-24 09:07:11 +01:00
|
|
|
[[nodiscard]] virtual std::variant<std::string, i128> queryInformation(const std::string &category, const std::string &argument);
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2021-03-26 21:43:24 +01:00
|
|
|
void undo();
|
|
|
|
void redo();
|
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
[[nodiscard]] bool canUndo() const;
|
|
|
|
[[nodiscard]] bool canRedo() const;
|
|
|
|
|
2022-08-12 15:11:27 +02:00
|
|
|
[[nodiscard]] virtual bool hasFilePicker() const;
|
|
|
|
virtual bool handleFilePicker();
|
|
|
|
|
2023-09-08 21:59:27 +02:00
|
|
|
virtual std::vector<MenuEntry> getMenuEntries() { return { }; }
|
2023-05-21 13:21:53 +02:00
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
[[nodiscard]] virtual bool hasLoadInterface() const;
|
|
|
|
[[nodiscard]] virtual bool hasInterface() const;
|
2023-03-16 16:48:15 +01:00
|
|
|
virtual bool drawLoadInterface();
|
2021-12-12 00:41:44 +01:00
|
|
|
virtual void drawInterface();
|
2021-03-26 21:43:24 +01:00
|
|
|
|
2022-09-22 09:05:09 +02:00
|
|
|
[[nodiscard]] u32 getID() const;
|
|
|
|
void setID(u32 id);
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-11-30 14:40:07 +01:00
|
|
|
[[nodiscard]] virtual nlohmann::json storeSettings(nlohmann::json settings) const;
|
2022-08-08 21:23:52 +02:00
|
|
|
virtual void loadSettings(const nlohmann::json &settings);
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
void markDirty(bool dirty = true) { m_dirty = dirty; }
|
|
|
|
[[nodiscard]] bool isDirty() const { return m_dirty; }
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-10-02 17:30:26 +02:00
|
|
|
[[nodiscard]] virtual std::pair<Region, bool> getRegionValidity(u64 address) const;
|
2022-08-10 09:26:48 +02:00
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
void skipLoadInterface() { m_skipLoadInterface = true; }
|
|
|
|
[[nodiscard]] bool shouldSkipLoadInterface() const { return m_skipLoadInterface; }
|
2022-08-14 10:07:45 +02:00
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
void setErrorMessage(const std::string &errorMessage) { m_errorMessage = errorMessage; }
|
|
|
|
[[nodiscard]] const std::string& getErrorMessage() const { return m_errorMessage; }
|
2023-03-21 10:33:00 +01:00
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
template<std::derived_from<undo::Operation> T>
|
|
|
|
bool addUndoableOperation(auto && ... args) {
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_undoRedoStack.add<T>(std::forward<decltype(args)...>(args)...);
|
2023-11-25 12:43:48 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
[[nodiscard]] undo::Stack& getUndoStack() { return m_undoRedoStack; }
|
2023-11-25 12:43:48 +01:00
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
protected:
|
2022-02-01 22:09:44 +01:00
|
|
|
u32 m_currPage = 0;
|
2021-01-11 13:50:04 +01:00
|
|
|
u64 m_baseAddress = 0;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
undo::Stack m_undoRedoStack;
|
|
|
|
|
2023-08-25 23:54:39 +02:00
|
|
|
std::list<std::unique_ptr<Overlay>> m_overlays;
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
u32 m_id;
|
|
|
|
|
2023-05-05 22:02:18 +02:00
|
|
|
/**
|
|
|
|
* @brief true if there is any data that needs to be saved
|
|
|
|
*/
|
2022-08-08 21:23:52 +02:00
|
|
|
bool m_dirty = false;
|
2023-05-05 22:02:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Control whetever to skip provider initialization
|
|
|
|
* initialization may be asking the user for information related to the provider,
|
|
|
|
* e.g. a process ID for the process memory provider
|
|
|
|
* this is used mainly when restoring a provider with already known initialization information
|
|
|
|
* for example when loading a project or loading a provider from the "recent" lsit
|
|
|
|
*
|
|
|
|
*/
|
2022-08-14 10:07:45 +02:00
|
|
|
bool m_skipLoadInterface = false;
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-03-21 10:33:00 +01:00
|
|
|
std::string m_errorMessage;
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
u64 m_pageSize = MaxPageSize;
|
2020-12-22 18:10:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|