-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathstring.h
282 lines (242 loc) · 7.84 KB
/
string.h
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright 2017 - 2018 Justas Masiulis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef JM_XORSTR_HPP
#define JM_XORSTR_HPP
#include <immintrin.h>
#include <type_traits>
#include <cstdint>
#define xorstr(str) ::jm::xor_string<XORSTR_STR(str)>()
#define xors(str) xorstr(str).crypt_get( )
#ifdef _MSC_VER
#define XORSTR_FORCEINLINE __forceinline
#else
#define XORSTR_FORCEINLINE __attribute__((always_inline))
#endif
// MSVC - no volatile
// GCC - volatile almost everywhere
// clang - volatile everywhere
#if defined(__clang__)
#define XORSTR_CLANG_VOLATILE volatile
#define XORSTR_VOLATILE volatile
#elif defined(__GNUC__)
#define XORSTR_CLANG_VOLATILE
#define XORSTR_VOLATILE volatile
#else
#define XORSTR_CLANG_VOLATILE
#define XORSTR_VOLATILE
#endif
// these compile time strings were required for an earlier version.
// might not be necessary for current version
#define XORSTR_STRING_EXPAND_10(n, x) \
jm::detail::c_at<n##0>(x), jm::detail::c_at<n##1>(x), \
jm::detail::c_at<n##2>(x), jm::detail::c_at<n##3>(x), \
jm::detail::c_at<n##4>(x), jm::detail::c_at<n##5>(x), \
jm::detail::c_at<n##6>(x), jm::detail::c_at<n##7>(x), \
jm::detail::c_at<n##8>(x), jm::detail::c_at<n##9>(x)
#define XORSTR_STRING_EXPAND_100(x) \
XORSTR_STRING_EXPAND_10(, x), XORSTR_STRING_EXPAND_10(1, x), \
XORSTR_STRING_EXPAND_10(2, x), XORSTR_STRING_EXPAND_10(3, x), \
XORSTR_STRING_EXPAND_10(4, x), XORSTR_STRING_EXPAND_10(5, x), \
XORSTR_STRING_EXPAND_10(6, x), XORSTR_STRING_EXPAND_10(7, x), \
XORSTR_STRING_EXPAND_10(8, x), XORSTR_STRING_EXPAND_10(9, x)
#define XORSTR_STR(s) \
::jm::detail::string_builder< \
std::decay_t<decltype(*s)>, \
jm::detail::tstring_<std::decay_t<decltype(*s)>>, \
XORSTR_STRING_EXPAND_100(s)>::type
// disable constant overflow warnings
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4309)
#pragma warning(disable : 4307)
#endif
namespace jm
{
namespace detail
{
template<std::size_t I, std::size_t M, class T>
constexpr T c_at( const T( &str )[M] ) noexcept
{
static_assert( M <= 99, "string too large." );
return ( I < M ) ? str[I] : 0;
}
template<class T, class B, T...>
struct string_builder;
template<class T, class B>
struct string_builder<T, B>
{
using type = B;
};
template<class T, template<class, T...> class S, T... Hs, T C, T... Cs>
struct string_builder<T, S<T, Hs...>, C, Cs...>
: std::conditional<C == T( 0 ),
string_builder<T, S<T, Hs...>>,
string_builder<T, S<T, Hs..., C>, Cs...>>::type{
};
template<class T, T... Cs>
struct tstring_
{
using value_type = T;
constexpr static std::size_t size = sizeof...( Cs );
constexpr static value_type str[size + 1] = { Cs..., '\0' };
constexpr static std::size_t size_in_bytes( ) noexcept
{
return ( size + 1 ) * sizeof( value_type );
}
};
constexpr std::uint64_t rng_seed( ) noexcept
{
std::uint64_t shifted = 0ull;
for ( int i = 0; i < 8; ++i )
{
shifted <<= 8;
shifted |= __TIME__[i];
}
return shifted;
}
template<std::uint64_t S>
constexpr std::uint32_t pcg32( ) noexcept
{
constexpr auto seed = rng_seed( );
constexpr auto oldstate = S * 6364136223846793005ull + ( seed | 1 );
std::uint32_t xorshifted = static_cast<std::uint32_t>(
( ( oldstate >> 18u ) ^ oldstate ) >> 27u );
std::uint32_t rot = oldstate >> 59u;
return ( xorshifted >> rot ) | ( xorshifted << ( ( 1u + ~rot ) & 31 ) );
}
template<std::size_t S>
constexpr std::uint64_t key8( )
{
return ( static_cast<std::uint64_t>( detail::pcg32<S>( ) ) << 32 ) |
detail::pcg32<S + 85>( );
}
template<class T>
constexpr std::size_t buffer_size( )
{
constexpr auto x = T::size_in_bytes( ) / 16;
return x * 2 + ( ( T::size_in_bytes( ) - x * 16 ) % 16 != 0 ) * 2;
}
// clang and gcc try really hard to place the constants in data
// sections. to counter that there was a need to create an intermediate
// constexpr string and then copy it into a non constexpr container with
// volatile storage so that the constants would be placed directly into
// code.
template<class T>
struct string_storage
{
std::uint64_t storage[buffer_size<T>( )];
template<std::size_t N = 0>
XORSTR_FORCEINLINE constexpr void _xor( )
{
if constexpr ( N != detail::buffer_size<T>( ) )
{
constexpr auto key = key8<N>( );
storage[N] ^= key;
_xor<N + 1>( );
}
}
XORSTR_FORCEINLINE constexpr string_storage( ) : storage{ 0 }
{
// puts the string into 64 bit integer blocks in a constexpr
// fashion
for ( std::size_t i = 0; i < T::size; ++i )
storage[i / ( 8 / sizeof( typename T::value_type ) )] |=
( std::uint64_t( T::str[i] )
<< ( ( i % ( 8 / sizeof( typename T::value_type ) ) ) * 8 *
sizeof( typename T::value_type ) ) );
// applies the xor encryption
_xor<0>( );
}
};
} // namespace detail
template<class T>
struct xor_string
{
XORSTR_VOLATILE std::uint64_t _storage[detail::buffer_size<T>( )];
template<std::size_t N>
XORSTR_FORCEINLINE void _crypt( ) noexcept
{
if constexpr ( detail::buffer_size<T>( ) > N )
{
#ifndef JM_XORSTR_DISABLE_AVX_INTRINSICS
if constexpr ( ( detail::buffer_size<T>( ) - N ) >= 4 )
{
// assignments are separate on purpose. Do not replace with
// = { ... }
XORSTR_CLANG_VOLATILE std::uint64_t keys[4];
keys[0] = detail::key8<N + 0>( );
keys[1] = detail::key8<N + 1>( );
keys[2] = detail::key8<N + 2>( );
keys[3] = detail::key8<N + 3>( );
*( __m256i* )( &_storage[N] ) = _mm256_xor_si256(
*( __m256i* )( &_storage[N] ), *( const __m256i* )( &keys ) );
_crypt<N + 4>( );
}
else
#endif
{
XORSTR_VOLATILE std::uint64_t keys[2];
keys[0] = detail::key8<N + 0>( );
keys[1] = detail::key8<N + 1>( );
*( __m128i* )( &_storage[N] ) = _mm_xor_si128(
*( __m128i* )( &_storage[N] ), *( const __m128i* )( &keys ) );
_crypt<N + 2>( );
}
}
}
template<std::size_t N>
XORSTR_FORCEINLINE constexpr static std::uint64_t _at( )
{
// forces compile time evaluation of storage for access
return std::integral_constant<
std::uint64_t,
detail::string_storage<T>{}.storage[N]>::value;
}
// loop generates vectorized code which places constants in data dir
template<std::size_t N>
void _copy( ) noexcept
{
if constexpr ( detail::buffer_size<T>( ) > N )
{
_storage[N] = _at<N>( );
_storage[N + 1] = _at<N + 1>( );
_copy<N + 2>( );
}
}
public:
using value_type = typename T::value_type;
using size_type = std::size_t;
using pointer = value_type * ;
using const_pointer = const pointer;
XORSTR_FORCEINLINE xor_string( ) noexcept { _copy<0>( ); }
constexpr size_type size( ) const noexcept { return T::size - 1; }
XORSTR_FORCEINLINE void crypt( ) noexcept { _crypt<0>( ); }
XORSTR_FORCEINLINE const_pointer get( ) const noexcept
{
return _storage;
}
XORSTR_FORCEINLINE const_pointer crypt_get( ) noexcept
{
crypt( );
// C casts are used because buffer may or may not be volatile
return ( const_pointer ) ( _storage );
}
};
} // namespace jm
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif // include guard