|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <string> |
| 4 | +#include <utility> |
| 5 | + |
| 6 | +namespace |
| 7 | +{ |
| 8 | + constexpr int const_atoi(char c) |
| 9 | + { |
| 10 | + return c - '0'; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +#ifdef _MSC_VER |
| 15 | +#define ALWAYS_INLINE __forceinline |
| 16 | +#else |
| 17 | +#define ALWAYS_INLINE __attribute__((always_inline)) |
| 18 | +#endif |
| 19 | + |
| 20 | +template<typename _string_type, size_t _length> |
| 21 | +class _Basic__xor_ |
| 22 | +{ |
| 23 | + using value_type = typename _string_type::value_type; |
| 24 | + static constexpr auto _length_minus_one = _length - 1; |
| 25 | + |
| 26 | +public: |
| 27 | + constexpr ALWAYS_INLINE _Basic__xor_(value_type const (&str)[_length]) |
| 28 | + : _Basic__xor_(str, std::make_index_sequence<_length_minus_one>()) |
| 29 | + { |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + inline auto c_str() const |
| 34 | + { |
| 35 | + decrypt(); |
| 36 | + |
| 37 | + return data; |
| 38 | + } |
| 39 | + |
| 40 | + inline auto str() const |
| 41 | + { |
| 42 | + decrypt(); |
| 43 | + |
| 44 | + return _string_type(data, data + _length_minus_one); |
| 45 | + } |
| 46 | + |
| 47 | + inline operator _string_type() const |
| 48 | + { |
| 49 | + return str(); |
| 50 | + } |
| 51 | + |
| 52 | +private: |
| 53 | + template<size_t... indices> |
| 54 | + constexpr ALWAYS_INLINE _Basic__xor_(value_type const (&str)[_length], std::index_sequence<indices...>) |
| 55 | + : data{ crypt(str[indices], indices)..., '\0' }, |
| 56 | + encrypted(true) |
| 57 | + { |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + static constexpr auto XOR_KEY = static_cast<value_type>( |
| 62 | + const_atoi(__TIME__[7]) + |
| 63 | + const_atoi(__TIME__[6]) * 10 + |
| 64 | + const_atoi(__TIME__[4]) * 60 + |
| 65 | + const_atoi(__TIME__[3]) * 600 + |
| 66 | + const_atoi(__TIME__[1]) * 3600 + |
| 67 | + const_atoi(__TIME__[0]) * 36000 |
| 68 | + ); |
| 69 | + |
| 70 | + static ALWAYS_INLINE constexpr auto crypt(value_type c, size_t i) |
| 71 | + { |
| 72 | + return static_cast<value_type>(c ^ (XOR_KEY + i)); |
| 73 | + } |
| 74 | + |
| 75 | + inline void decrypt() const |
| 76 | + { |
| 77 | + if (encrypted) |
| 78 | + { |
| 79 | + for (size_t t = 0; t < _length_minus_one; t++) |
| 80 | + { |
| 81 | + data[t] = crypt(data[t], t); |
| 82 | + } |
| 83 | + encrypted = false; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + mutable value_type data[_length]; |
| 88 | + mutable bool encrypted; |
| 89 | +}; |
| 90 | +//--------------------------------------------------------------------------- |
| 91 | +template<size_t _length> |
| 92 | +using _xor_A = _Basic__xor_<std::string, _length>; |
| 93 | +template<size_t _length> |
| 94 | +using _xor_W = _Basic__xor_<std::wstring, _length>; |
| 95 | +template<size_t _length> |
| 96 | +using _xor_U16 = _Basic__xor_<std::u16string, _length>; |
| 97 | +template<size_t _length> |
| 98 | +using _xor_U32 = _Basic__xor_<std::u32string, _length>; |
| 99 | +//--------------------------------------------------------------------------- |
| 100 | +template<typename _string_type, size_t _length, size_t _length2> |
| 101 | +inline auto operator==(const _Basic__xor_<_string_type, _length>& lhs, const _Basic__xor_<_string_type, _length2>& rhs) |
| 102 | +{ |
| 103 | + static_assert(_length == _length2, "_xor_== different length"); |
| 104 | + |
| 105 | + return _length == _length2 && lhs.str() == rhs.str(); |
| 106 | +} |
| 107 | +//--------------------------------------------------------------------------- |
| 108 | +template<typename _string_type, size_t _length> |
| 109 | +inline auto operator==(const _string_type& lhs, const _Basic__xor_<_string_type, _length>& rhs) |
| 110 | +{ |
| 111 | + return lhs.size() == _length && lhs == rhs.str(); |
| 112 | +} |
| 113 | +//--------------------------------------------------------------------------- |
| 114 | +template<typename _stream_type, typename _string_type, size_t _length> |
| 115 | +inline auto& operator<<(_stream_type& lhs, const _Basic__xor_<_string_type, _length>& rhs) |
| 116 | +{ |
| 117 | + lhs << rhs.c_str(); |
| 118 | + |
| 119 | + return lhs; |
| 120 | +} |
| 121 | +//--------------------------------------------------------------------------- |
| 122 | +template<typename _string_type, size_t _length, size_t _length2> |
| 123 | +inline auto operator+(const _Basic__xor_<_string_type, _length>& lhs, const _Basic__xor_<_string_type, _length2>& rhs) |
| 124 | +{ |
| 125 | + return lhs.str() + rhs.str(); |
| 126 | +} |
| 127 | +//--------------------------------------------------------------------------- |
| 128 | +template<typename _string_type, size_t _length> |
| 129 | +inline auto operator+(const _string_type& lhs, const _Basic__xor_<_string_type, _length>& rhs) |
| 130 | +{ |
| 131 | + return lhs + rhs.str(); |
| 132 | +} |
| 133 | +//--------------------------------------------------------------------------- |
| 134 | +template<size_t _length> |
| 135 | +constexpr ALWAYS_INLINE auto _xor_(char const (&str)[_length]) |
| 136 | +{ |
| 137 | + return _xor_A<_length>(str); |
| 138 | +} |
| 139 | +//--------------------------------------------------------------------------- |
| 140 | +template<size_t _length> |
| 141 | +constexpr ALWAYS_INLINE auto _xor_(wchar_t const (&str)[_length]) |
| 142 | +{ |
| 143 | + return _xor_W<_length>(str); |
| 144 | +} |
| 145 | +//--------------------------------------------------------------------------- |
| 146 | +template<size_t _length> |
| 147 | +constexpr ALWAYS_INLINE auto _xor_(char16_t const (&str)[_length]) |
| 148 | +{ |
| 149 | + return _xor_U16<_length>(str); |
| 150 | +} |
| 151 | +//--------------------------------------------------------------------------- |
| 152 | +template<size_t _length> |
| 153 | +constexpr ALWAYS_INLINE auto _xor_(char32_t const (&str)[_length]) |
| 154 | +{ |
| 155 | + return _xor_U32<_length>(str); |
| 156 | +} |
| 157 | +//--------------------------------------------------------------------------- |
0 commit comments