1
0
mirror of synced 2024-12-05 04:27:56 +01:00
ImHex/plugins/builtin/include/content/providers/view_provider.hpp
iTrooz e3ae169833
impr: Separate the behaviour of being savable and being dumpable for provider (#1183)
### Problem description

Currently, the providers use the method `isSavable()` to determine both
if they can use "Save" or "Save as".
This behaviour is problematic because some providers may need to be
saveable but not saveable as: for example the view provider. The
original provider may not allow to be saved.

### Implementation description
I separate these two behaviour by creating another function:
`isDumpable()`, that return true by default but can be overridden by the
provider to return false, if the provider should not be dumped in any
way.

### Additional things

While I was at it, I also marked "export" operations as needing the
"dumpable" flag. That way, we can't accidentally export the whole
address space of a process as base64.

I also added documentation for these some functions in Provider
2023-07-05 20:49:57 +02:00

132 lines
4.3 KiB
C++

#pragma once
#include <hex/providers/provider.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/api/event.hpp>
namespace hex::plugin::builtin {
class ViewProvider : public hex::prv::Provider {
public:
explicit ViewProvider() {
EventManager::subscribe<EventProviderClosing>(this, [this](prv::Provider *provider, bool*) {
if (this->m_provider == provider)
ImHexApi::Provider::remove(this, true);
});
}
~ViewProvider() override {
EventManager::unsubscribe<EventProviderClosing>(this);
}
[[nodiscard]] bool isAvailable() const override {
if (this->m_provider == nullptr)
return false;
else
return this->m_provider->isAvailable();
}
[[nodiscard]] bool isReadable() const override {
if (this->m_provider == nullptr)
return false;
else
return this->m_provider->isReadable();
}
[[nodiscard]] bool isWritable() const override {
if (this->m_provider == nullptr)
return false;
else
return this->m_provider->isWritable();
}
[[nodiscard]] bool isResizable() const override { return true; }
[[nodiscard]] bool isSavable() const override {
if (this->m_provider == nullptr)
return false;
else
return this->m_provider->isSavable();
}
void save() override {
this->m_provider->save();
}
[[nodiscard]] bool open() override { return true; }
void close() override { }
void resize(size_t newSize) override {
this->m_size = newSize;
}
void insert(u64 offset, size_t size) override {
if (this->m_provider == nullptr)
return;
this->m_size += size;
this->m_provider->insert(offset + this->m_startAddress, size);
}
void remove(u64 offset, size_t size) override {
if (this->m_provider == nullptr)
return;
this->m_size -= size;
this->m_provider->remove(offset + this->m_startAddress, size);
}
void readRaw(u64 offset, void *buffer, size_t size) override {
if (this->m_provider == nullptr)
return;
this->m_provider->read(offset + this->m_startAddress, buffer, size);
}
void writeRaw(u64 offset, const void *buffer, size_t size) override {
if (this->m_provider == nullptr)
return;
this->m_provider->write(offset + this->m_startAddress, buffer, size);
}
[[nodiscard]] size_t getActualSize() const override { return this->m_size; }
[[nodiscard]] std::string getName() const override {
if (this->m_provider == nullptr)
return "View";
else
return hex::format("{} View", this->m_provider->getName());
}
[[nodiscard]] std::vector<Description> getDataDescription() const override {
if (this->m_provider == nullptr)
return { };
return this->m_provider->getDataDescription();
}
void loadSettings(const nlohmann::json &settings) override { hex::unused(settings); }
[[nodiscard]] nlohmann::json storeSettings(nlohmann::json settings) const override { return settings; }
[[nodiscard]] std::string getTypeName() const override {
return "hex.builtin.provider.view";
}
void setProvider(u64 startAddress, size_t size, hex::prv::Provider *provider) {
this->m_startAddress = startAddress;
this->m_size = size;
this->m_provider = provider;
}
[[nodiscard]] std::pair<Region, bool> getRegionValidity(u64 address) const override {
address -= this->getBaseAddress();
if (address < this->getActualSize())
return { Region { this->getBaseAddress() + address, this->getActualSize() - address }, true };
else
return { Region::Invalid(), false };
}
private:
u64 m_startAddress = 0x00;
size_t m_size = 0x00;
prv::Provider *m_provider = nullptr;
};
}