forked from 0xcds4r/build69-gtasa2.0
-
Notifications
You must be signed in to change notification settings - Fork 3
/
str_obfuscator.hpp
65 lines (53 loc) · 1.19 KB
/
str_obfuscator.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
//
// Powered by tapy.me/weikton
//
#ifndef STR_OBFUSCATOR_HPP_
#define STR_OBFUSCATOR_HPP_
namespace detail {
template<std::size_t index>
struct encryptor {
static void encrypt(char *dest, const char *str, char key) {
dest[index] = str[index] ^ key;
encryptor<index - 1>::encrypt(dest, str, key);
}
};
template<>
struct encryptor<0> {
static void encrypt(char *dest, const char *str, char key) {
dest[0] = str[0] ^ key;
}
};
};
class cryptor {
public:
template<std::size_t S>
class string_encryptor {
public:
constexpr string_encryptor(const char str[S], int key) :
_buffer{}, _decrypted{false}, _key{ static_cast<const char>(key % 255) } {
detail::encryptor<S - 1>::encrypt(_buffer, str, _key);
}
#ifdef __GNUC__
__attribute__((noinline))
#endif
const char *decrypt(void) const {
if (_decrypted) {
return _buffer;
}
for (auto &c : _buffer) {
c ^= _key;
}
_decrypted = true;
return _buffer;
}
private:
mutable char _buffer[S];
mutable bool _decrypted;
const char _key;
};
template<std::size_t S>
static constexpr auto create(const char(&str)[S]) {
return string_encryptor<S>{ str, S };
}
};
#endif // STR_OBFUSCATOR_HPP_