Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- /*
- //-------------------------------------------------------------//
- // "Malware related compile-time hacks with C++11" by LeFF //
- // You can use this code however you like, I just don't really //
- // give a shit, but if you feel some respect for me, please //
- // don't cut off this comment when copy-pasting... ;-) //
- //-------------------------------------------------------------//
- ////////////////////////////////////////////////////////////////////
- template <int X> struct EnsureCompileTime {
- enum : int {
- Value = X
- };
- };
- ////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////
- //Use Compile-Time as seed
- #define 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 int LinearCongruentGenerator(int Rounds) {
- return 1013904223 + 1664525 * ((Rounds> 0) ? LinearCongruentGenerator(Rounds - 1) : Seed & 0xFFFFFFFF);
- }
- #define Random() EnsureCompileTime<LinearCongruentGenerator(10)>::Value //10 Rounds
- #define RandomNumber(Min, Max) (Min + (Random() % (Max - Min + 1)))
- ////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////
- template <int... Pack> struct IndexList {};
- ////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////
- template <typename IndexList, int Right> struct Append;
- template <int... Left, int Right> struct Append<IndexList<Left...>, Right> {
- typedef IndexList<Left..., Right> Result;
- };
- ////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////
- template <int N> struct ConstructIndexList {
- typedef typename Append<typename ConstructIndexList<N - 1>::Result, N - 1>::Result Result;
- };
- template <> struct ConstructIndexList<0> {
- typedef IndexList<> Result;
- };
- ////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////
- const char XORKEY = static_cast<char>(RandomNumber(0, 0xFF));
- constexpr char EncryptCharacter(const char Character, int Index) {
- return Character ^ (XORKEY + Index);
- }
- template <typename IndexList> class CXorString;
- template <int... Index> class CXorString<IndexList<Index...> > {
- private:
- char Value[sizeof...(Index) + 1];
- bool Decrypted;
- public:
- #pragma optimize("", off)
- __forceinline
- constexpr CXorString(const char* const String)
- : Value{ EncryptCharacter(String[Index], Index)... }
- , Decrypted (false) {}
- #pragma optimize("", on)
- __forceinline
- char* decrypt() {
- if (Decrypted) {
- return Value;
- }
- for(int t = 0; t < sizeof...(Index); t++) {
- Value[t] = Value[t] ^ (XORKEY + t);
- }
- Value[sizeof...(Index)] = '\0';
- Decrypted = true;
- return Value;
- }
- char* get() {
- return Value;
- }
- };
- #define XorS(X, String) CXorString<ConstructIndexList<sizeof(String)-1>::Result> X(String)
- #ifndef _DEBUG
- #define XS(String) (CXorString<ConstructIndexList<sizeof(String)-1>::Result>(String).decrypt())
- #else
- #define XS(String) (String)
- #endif
- ////////////////////////////////////////////////////////////////////
- */
- #include <string>
- #include <array>
- #include <cstdarg>
- constexpr auto time = __TIME__;
- constexpr auto seed = static_cast< int >( time[ 7 ] ) + static_cast< int >( time[ 6 ] ) * 10 + static_cast< int >( time[ 4 ] ) * 60 + static_cast< int >( time[ 3 ] ) * 600 + static_cast< int >( time[ 1 ] ) * 3600 + static_cast< int >( time[ 0 ] ) * 36000;
- // 1988, Stephen Park and Keith Miller
- // "Random Number Generators: Good Ones Are Hard To Find", considered as "minimal standard"
- // Park-Miller 31 bit pseudo-random number generator, implemented with G. Carta's optimisation:
- // with 32-bit math and without division
- template < int N >
- struct RandomGenerator
- {
- private:
- static constexpr unsigned a = 16807; // 7^5
- static constexpr unsigned m = 2147483647; // 2^31 - 1
- static constexpr unsigned s = RandomGenerator< N - 1 >::value;
- static constexpr unsigned lo = a * ( s & 0xFFFF ); // Multiply lower 16 bits by 16807
- static constexpr unsigned hi = a * ( s >> 16 ); // Multiply higher 16 bits by 16807
- static constexpr unsigned lo2 = lo + ( ( hi & 0x7FFF ) << 16 ); // Combine lower 15 bits of hi with lo's upper bits
- static constexpr unsigned hi2 = hi >> 15; // Discard lower 15 bits of hi
- static constexpr unsigned lo3 = lo2 + hi;
- public:
- static constexpr unsigned max = m;
- static constexpr unsigned value = lo3 > m ? lo3 - m : lo3;
- };
- template <>
- struct RandomGenerator< 0 >
- {
- static constexpr unsigned value = seed;
- };
- template < int N, int M >
- struct RandomInt
- {
- static constexpr auto value = RandomGenerator< N + 1 >::value % M;
- };
- template < int N >
- struct RandomChar
- {
- static const char value = static_cast< char >( 1 + RandomInt< N, 0x7F - 1 >::value );
- };
- template < size_t N, int K >
- struct XorString
- {
- private:
- const char _key;
- std::array< char, N + 1 > _encrypted;
- constexpr char enc( char c ) const
- {
- return c ^ _key;
- }
- char dec( char c ) const
- {
- return c ^ _key;
- }
- public:
- template < size_t... Is >
- constexpr __forceinline XorString( const char* str, std::index_sequence< Is... > ) : _key( RandomChar< K >::value ), _encrypted{ enc( str[ Is ] )... }
- {
- }
- __forceinline decltype( auto ) decrypt( void )
- {
- for( size_t i = 0; i < N; ++i ) {
- _encrypted[ i ] = dec( _encrypted[ i ] );
- }
- _encrypted[ N ] = '\0';
- return _encrypted.data();
- }
- };
- #ifdef _DEBUG
- #define XS(s) (s)
- #else
- #define XS(s) (XorString<sizeof(s)-1,__COUNTER__>(s,std::make_index_sequence<sizeof(s)-1>()).decrypt())
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement