mirror of
https://github.com/siemens-mobile-hacks/ptr89.git
synced 2026-11-26 02:37:25 +00:00
93 lines
2.6 KiB
C++
93 lines
2.6 KiB
C++
#include "Ptr89Wasm.h"
|
|
|
|
#include <algorithm>
|
|
#include <format>
|
|
#include <stdexcept>
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
namespace Ptr89 {
|
|
|
|
static std::string getBytes(uint32_t offset, size_t size, const Pattern::Memory &memory) {
|
|
std::string bytes;
|
|
size = offset < memory.size ? std::min(size, memory.size - offset) : 0;
|
|
bytes.reserve(size * 2);
|
|
for (size_t i = 0; i < size; i++)
|
|
bytes += std::format("{:02X}", memory.data[offset + i]);
|
|
return bytes;
|
|
}
|
|
|
|
Ptr89Wasm::Ptr89Wasm() {
|
|
spdlog::set_pattern("%v");
|
|
spdlog::set_level(spdlog::level::warn);
|
|
}
|
|
|
|
void Ptr89Wasm::open(uintptr_t ptr, size_t size, uint32_t base, const std::string &archName) {
|
|
if (ptr == 0 && size != 0)
|
|
throw std::invalid_argument("Memory pointer is null.");
|
|
|
|
Architecture arch;
|
|
if (archName == "arm") {
|
|
arch = ARCH_ARM;
|
|
} else if (archName == "c166") {
|
|
arch = ARCH_C166;
|
|
} else {
|
|
throw std::invalid_argument("Invalid architecture '" + archName + "'. Expected arm or c166.");
|
|
}
|
|
|
|
const auto *data = reinterpret_cast<const uint8_t *>(ptr);
|
|
m_data.clear();
|
|
if (size != 0)
|
|
m_data.assign(data, data + size);
|
|
m_base = base;
|
|
m_arch = arch;
|
|
m_open = true;
|
|
}
|
|
|
|
void Ptr89Wasm::close() {
|
|
m_data.clear();
|
|
m_data.shrink_to_fit();
|
|
m_open = false;
|
|
}
|
|
|
|
void Ptr89Wasm::setDebug(bool enabled) {
|
|
spdlog::set_level(enabled ? spdlog::level::debug : spdlog::level::warn);
|
|
}
|
|
|
|
std::vector<Ptr89SearchResult> Ptr89Wasm::find(const std::string &patternText, size_t limit, int align) const {
|
|
auto memory = getMemory(align);
|
|
auto pattern = Pattern::parse(patternText);
|
|
auto matches = Pattern::find(pattern, memory, limit);
|
|
|
|
std::vector<Ptr89SearchResult> results;
|
|
results.reserve(matches.size());
|
|
for (const auto &match: matches)
|
|
results.push_back({ match.address, match.offset, getBytes(match.offset, match.size, memory) });
|
|
return results;
|
|
}
|
|
|
|
std::vector<Ptr89XRef> Ptr89Wasm::findXRefs(uint32_t address, size_t limit) const {
|
|
auto memory = getMemory();
|
|
auto matches = Pattern::finXRefs(address, memory, limit);
|
|
std::vector<Ptr89XRef> results;
|
|
results.reserve(matches.size());
|
|
for (const auto &match: matches) {
|
|
results.push_back({ Pattern::getResultTypeName(match.type), match.address, match.offset,
|
|
getBytes(match.offset, match.size, memory) });
|
|
}
|
|
return results;
|
|
}
|
|
|
|
Pattern::Memory Ptr89Wasm::getMemory(int align) const {
|
|
if (!m_open)
|
|
throw std::runtime_error("Ptr89 is not opened.");
|
|
if (align <= 0)
|
|
throw std::invalid_argument("Search alignment must be greater than zero.");
|
|
return { m_base, m_data.data(), m_data.size(), align, m_arch };
|
|
}
|
|
|
|
std::string prettify(const std::string &pattern) {
|
|
return Pattern::stringify(Pattern::parse(pattern));
|
|
}
|
|
|
|
}; // namespace Ptr89
|