-
Notifications
You must be signed in to change notification settings - Fork 2
/
malstring.hh
223 lines (182 loc) · 5.3 KB
/
malstring.hh
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#pragma once
#include <type_traits>
#include <ranges>
#include <algorithm>
#include <string_view>
#include <charconv>
#include <cstdint>
#include <cstddef>
#if (defined(_MSC_VER))
#define NOINLINE __declspec(noinline)
#define FORCEINLINE __forceinline
#else
#define NOINLINE __attribute__((noinline))
#define FORCEINLINE __attribute__((always_inline))
#endif
#define TIME_SEED ((__TIME__[7] - '0') * 1 + (__TIME__[6] - '0') * 10 + \
(__TIME__[4] - '0') * 60 + (__TIME__[3] - '0') * 600 + \
(__TIME__[1] - '0') * 3600 + (__TIME__[0] - '0') * 36000)
constexpr std::uint32_t Modulus()
{
return 0x7fffffff;
}
template <std::size_t N>
consteval std::uint32_t Seed(const char(&Entropy)[N], const std::uint32_t Iv = 0)
{
auto Value{ Iv };
for (std::size_t i{ 0 }; i < N; i++)
{
Value = (Value & ((~0) << 8)) | ((Value & 0xFF) ^ Entropy[i]);
Value = Value << 8 | Value >> ((sizeof(Value) * 8) - 8);
}
while (Value > Modulus())
Value = Value >> 1;
return Value << 1 | 1;
}
constexpr std::uint32_t prng(const std::uint32_t Input)
{
return (Input * 48271) % Modulus();
}
#define RAND() (Seed(__FILE__, __COUNTER__ + __LINE__ * TIME_SEED))
template <std::size_t Size, typename CharType>
struct FixedString
{
consteval FixedString(const CharType(&Str)[Size]) noexcept
{
for (std::size_t i = 0; i < Size; ++i)
{
this->Buf[i] = Str[i];
}
}
static constexpr std::size_t Len = Size;
CharType Buf[Size];
constexpr operator std::string_view() const noexcept
{
return { Buf, Len - 1 };
}
};
template <FixedString Fixed, std::uint32_t Rand>
class StackString
{
public:
consteval StackString()
{
static_assert(
std::is_same_v<std::remove_cv_t<std::remove_reference_t<decltype(Fixed.Buf[0])>>, char> ||
std::is_same_v<std::remove_cv_t<std::remove_reference_t<decltype(Fixed.Buf[0])>>, wchar_t> ||
std::is_same_v<std::remove_cv_t<std::remove_reference_t<decltype(Fixed.Buf[0])>>, char16_t> ||
std::is_same_v<std::remove_cv_t<std::remove_reference_t<decltype(Fixed.Buf[0])>>, char32_t>
);
for (std::size_t i = 0; i < Fixed.Len; ++i)
{
Buf[i] = Fixed.Buf[i] ^ (Rand + static_cast<std::uint32_t>(i));
}
}
FORCEINLINE void EncryptDecrypt()
{
for (std::size_t i = 0; i < Fixed.Len; ++i)
{
Buf[i] = Buf[i] ^ (Rand + static_cast<std::uint32_t>(i));
}
}
consteval std::size_t Length()
{
return Fixed.Len;
}
std::remove_cv_t<std::remove_reference_t<decltype(Fixed.Buf[0])>> Buf[Fixed.Len];
};
template <auto C>
auto NOINLINE CallNum()
{
return C;
}
template <FixedString Fixed, std::uint32_t Rand, bool IsTerm = true, std::size_t N = Fixed.Len - (IsTerm ? 1 : 2)>
FORCEINLINE void MakeCallString(std::remove_cv_t<std::remove_reference_t<decltype(Fixed.Buf[0])>>* Buf)
{
Buf[N] = static_cast<std::uint8_t>(CallNum<Fixed.Buf[N] ^ (Rand + static_cast<std::uint32_t>(N))>());
if constexpr (N)
MakeCallString<Fixed, Rand, IsTerm, N - 1>(Buf);
}
template <FixedString Fixed, std::uint32_t Rand, bool IsTerm = true>
class CallString
{
public:
FORCEINLINE CallString()
{
MakeCallString<Fixed, Rand, IsTerm>(Buf);
}
FORCEINLINE void EncryptDecrypt()
{
for (std::size_t i = 0; i < Fixed.Len - (IsTerm ? 0 : 1); ++i)
{
Buf[i] = Buf[i] ^ static_cast<std::uint8_t>(Rand + static_cast<std::uint32_t>(i));
}
}
std::remove_cv_t<std::remove_reference_t<decltype(Fixed.Buf[0])>> Buf[Fixed.Len - (IsTerm ? 0 : 1)];
};
constexpr auto to_int = [](std::string_view sv) -> std::uint8_t
{
std::uint8_t value{};
const auto res = std::from_chars(sv.data(), sv.data() + sv.size(), value, 16);
return value;
};
constexpr auto to_sv = [](auto&& rng) { return std::string_view(rng); };
template <FixedString Pattern>
constexpr auto hex2array() -> std::array<std::uint8_t, Pattern.Len / 3>
{
const std::size_t ArrSize = Pattern.Len / 3;
constexpr auto pat = std::string_view{ Pattern };
std::array<std::uint8_t, ArrSize> res{};
static_assert(std::ranges::distance(std::views::split(pat, ' ')) == ArrSize);
for (std::size_t i{};
const auto byte : std::views::split(pat, ' ')
| std::views::transform(to_sv)
| std::views::transform(to_int))
{
res.at(i++) = byte;
}
return res;
}
template <std::size_t ArraySize, std::array<std::uint8_t, ArraySize> Array, std::uint32_t Rand, std::size_t N =
ArraySize - 1>
FORCEINLINE void MakeCallBuffer(std::uint8_t* Buf)
{
Buf[N] = static_cast<std::uint8_t>(CallNum<Array[N] ^ (Rand + static_cast<std::uint32_t>(N))>());
if constexpr (N)
MakeCallBuffer<ArraySize, Array, Rand, N - 1>(Buf);
}
template <std::size_t ArraySize, std::array<std::uint8_t, ArraySize> Array, std::uint32_t Rand>
class CallArray
{
public:
FORCEINLINE CallArray()
{
MakeCallBuffer<ArraySize, Array, Rand>(Buf);
}
FORCEINLINE void EncryptDecrypt()
{
for (std::size_t i = 0; i < ArraySize; ++i)
{
Buf[i] = Buf[i] ^ static_cast<std::uint8_t>(Rand + static_cast<std::uint32_t>(i));
}
}
std::uint8_t Buf[ArraySize];
};
template <FixedString Fixed, std::uint32_t Rand>
class CallArrayFromHex
{
public:
FORCEINLINE CallArrayFromHex()
{
constexpr std::array<std::uint8_t, Fixed.Len / 3> Mask = hex2array<Fixed>();
MakeCallBuffer<Fixed.Len / 3, Mask, Rand>(Buf);
}
FORCEINLINE void EncryptDecrypt()
{
for (std::size_t i = 0; i < (Fixed.Len - 1) / 3; ++i)
{
Buf[i] = Buf[i] ^ (Rand + static_cast<std::uint32_t>(i));
}
}
std::uint8_t Buf[(Fixed.Len - 1) / 3];
};