This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
YimMenu/src/memory/pattern.cpp

62 lines
1.6 KiB
C++
Raw Normal View History

2019-03-21 20:18:31 +01:00
#include "pattern.hpp"
#include "../common.hpp"
2019-03-21 20:18:31 +01:00
namespace memory
{
std::optional<uint8_t> to_hex(char const c)
2019-03-21 20:18:31 +01:00
{
2022-10-15 19:50:28 -04:00
switch (c)
2019-03-21 20:18:31 +01:00
{
case '0': return static_cast<uint8_t>(0x0);
case '1': return static_cast<uint8_t>(0x1);
case '2': return static_cast<uint8_t>(0x2);
case '3': return static_cast<uint8_t>(0x3);
case '4': return static_cast<uint8_t>(0x4);
case '5': return static_cast<uint8_t>(0x5);
case '6': return static_cast<uint8_t>(0x6);
case '7': return static_cast<uint8_t>(0x7);
case '8': return static_cast<uint8_t>(0x8);
case '9': return static_cast<uint8_t>(0x9);
case 'a': return static_cast<uint8_t>(0xa);
case 'b': return static_cast<uint8_t>(0xb);
case 'c': return static_cast<uint8_t>(0xc);
case 'd': return static_cast<uint8_t>(0xd);
case 'e': return static_cast<uint8_t>(0xe);
case 'f': return static_cast<uint8_t>(0xf);
case 'A': return static_cast<uint8_t>(0xA);
case 'B': return static_cast<uint8_t>(0xB);
case 'C': return static_cast<uint8_t>(0xC);
case 'D': return static_cast<uint8_t>(0xD);
case 'E': return static_cast<uint8_t>(0xE);
case 'F': return static_cast<uint8_t>(0xF);
default: return std::nullopt;
2022-10-15 19:50:28 -04:00
}
}
2019-03-21 20:18:31 +01:00
2022-10-15 19:50:28 -04:00
pattern::pattern(std::string_view ida_sig)
{
2023-12-21 09:51:23 +01:00
const auto size_minus_one = ida_sig.size() - 1;
m_bytes.reserve(size_minus_one / 2);
for (size_t i = 0; i != size_minus_one; ++i)
2019-03-21 20:18:31 +01:00
{
if (ida_sig[i] == ' ')
continue;
2023-12-21 09:51:23 +01:00
2019-03-21 20:18:31 +01:00
if (ida_sig[i] != '?')
{
2023-12-21 09:51:23 +01:00
auto c1 = to_hex(ida_sig[i]);
auto c2 = to_hex(ida_sig[i + 1]);
if (c1 && c2)
2019-03-21 20:18:31 +01:00
{
2023-12-21 09:51:23 +01:00
m_bytes.emplace_back(static_cast<uint8_t>((*c1 * 0x10) + *c2));
2019-03-21 20:18:31 +01:00
}
}
else
{
2022-10-15 19:50:28 -04:00
m_bytes.push_back({});
2019-03-21 20:18:31 +01:00
}
}
}
}