This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
pattern.hpp
74 lines (58 loc) · 1.81 KB
/
pattern.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once
namespace pattern {
// wraps a uint8_t and has a bool if the byte should be ignored.
class PatternByte_t {
private:
uint8_t m_byte;
bool m_is_wildcard;
public:
PatternByte_t() : m_byte{ 0 }, m_is_wildcard{ true } { }
PatternByte_t( uint8_t byte, bool is_wildcard ) : m_byte{ byte }, m_is_wildcard{ is_wildcard } { }
uint8_t get_byte() const {
return m_byte;
}
bool is_wildcard() const {
return m_is_wildcard;
}
bool match( const uint8_t other ) const {
return m_byte == other || m_is_wildcard;
}
};
// converts a string into a pattern.
class Pattern {
private:
using pattern_t = std::vector< PatternByte_t >;
using pattern_citer_t = pattern_t::const_iterator;
pattern_t m_pattern;
public:
Pattern() = default;
explicit Pattern( const std::string &str );
const PatternByte_t &operator []( size_t index ) const {
if( index > m_pattern.size() )
return { };
return m_pattern[ index ];
}
bool empty() const {
return m_pattern.empty();
}
pattern_citer_t begin () const {
return m_pattern.cbegin( );
}
pattern_citer_t end( ) const {
return m_pattern.cend( );
}
};
uintptr_t find( uintptr_t start, size_t len, const Pattern &pattern );
uintptr_t find( uintptr_t start, size_t len, const std::string &pattern );
uintptr_t find( const pe::Module &module, const std::string &pattern );
uintptr_t find( hash32_t hash, const std::string &pattern );
uintptr_t find( const std::string &str, const std::string &pattern );
template < typename t = uintptr_t >
static t find( const std::string &str, const std::string &pattern ){
return ( t )( find( str, pattern ) );
}
template < typename t = uintptr_t >
static t find( const pe::Module &module, const std::string &pattern, int offset = 0 ){
return ( t )( find( module, pattern ) + offset );
}
}