Last active
April 27, 2024 11:17
-
-
Save yumetodo/238c52d4382db93e1978743cf299ba4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*============================================================================= | |
Copyright (C) 2018 yumetodo <yume-wikijp@live.jp> | |
Distributed under the Boost Software License, Version 1.0. | |
(See https://www.boost.org/LICENSE_1_0.txt) | |
=============================================================================*/ | |
//ref: http://dxlib.o.oo7.jp/cgi/patiobbs/patio.cgi?mode=view&no=4371 | |
#include <string> | |
#if defined(__clang__) | |
# if !__has_feature(cxx_constexpr) | |
# define DXLIB_CUSTOM_NO_CXX11_CONSTEXPR | |
# endif | |
# if !__has_feature(cxx_noexcept) | |
# define DXLIB_CUSTOM_NO_CXX11_NOEXCEPT | |
# endif | |
# if !__has_feature(cxx_relaxed_constexpr) | |
# define DXLIB_CUSTOM_NO_CXX14_CONSTEXPR | |
# endif | |
#elif defined(__GNUC__) | |
# if defined(__GXX_EXPERIMENTAL_CXX0X__) || (__cplusplus >= 201103L) | |
# define DXLIB_CUSTOM_GCC_CXX11 | |
# endif | |
# if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6) || !defined(DXLIB_CUSTOM_GCC_CXX11)) | |
# define DXLIB_CUSTOM_NO_CXX11_CONSTEXPR | |
# define DXLIB_CUSTOM_NO_CXX11_NOEXCEPT | |
# endif | |
# if !defined(__cpp_constexpr) || (__cpp_constexpr < 201304) | |
# define DXLIB_CUSTOM_NO_CXX14_CONSTEXPR | |
# endif | |
#elif defined(_MSC_VER) | |
# if _MSC_FULL_VER < 190023026 | |
# define DXLIB_CUSTOM_NO_CXX11_CONSTEXPR | |
# define DXLIB_CUSTOM_NO_CXX11_NOEXCEPT | |
# endif | |
# if !defined(__cpp_constexpr) || (__cpp_constexpr < 201304) | |
# define DXLIB_CUSTOM_NO_CXX14_CONSTEXPR | |
# endif | |
#endif | |
#ifdef DXLIB_CUSTOM_NO_CXX11_CONSTEXPR | |
# define DXLIB_CUSTOM_CONSTEXPR | |
#else | |
# define DXLIB_CUSTOM_CONSTEXPR constexpr | |
# include <type_traits> | |
#endif | |
#ifdef DXLIB_CUSTOM_NO_CXX11_NOEXCEPT | |
# define DXLIB_CUSTOM_NOEXCEPT | |
#else | |
# define DXLIB_CUSTOM_NOEXCEPT noexcept | |
#endif | |
namespace inferior_encrypted_string { | |
template<typename CharType, std::size_t N> | |
struct inferior_encrypted_string { | |
CharType str[N]; | |
//typedef std::basic_string<CharType> string_t; | |
std::basic_string<CharType> decrypt() const | |
{ | |
std::basic_string<CharType> re = this->str; | |
for(typename std::basic_string<CharType>::iterator it = re.begin(); it != re.end(); ++it) *it += CharType(31); | |
return re; | |
} | |
}; | |
#if !defined(DXLIB_CUSTOM_NO_CXX11_CONSTEXPR) && defined(DXLIB_CUSTOM_NO_CXX14_CONSTEXPR) | |
template <class T> | |
constexpr T&& forward(typename std::remove_reference<T>::type& t) noexcept { return static_cast<T&&>(t); } | |
template <class T> | |
constexpr T&& forward(typename std::remove_reference<T>::type&& t) noexcept { return static_cast<T&&>(t); } | |
namespace detail { | |
template<typename CharType, std::size_t N, std::size_t i, typename std::enable_if<(0 == i), std::nullptr_t>::type, typename ...RestChars> | |
constexpr inferior_encrypted_string<CharType, N>make_inferior_encrypted_string_impl(const CharType (& str)[N], RestChars&& ...rest) noexcept | |
{ | |
return {{ CharType(str[0] - 31), forward<CharType>(rest)... }}; | |
} | |
template<typename CharType, std::size_t N, std::size_t i, typename std::enable_if<(0 != i), std::nullptr_t>::type, typename ...RestChars> | |
constexpr inferior_encrypted_string<CharType, N> make_inferior_encrypted_string_impl(const CharType (& str)[N], RestChars&& ...rest) noexcept | |
{ | |
return make_inferior_encrypted_string_impl<CharType, N, i - 1, nullptr, RestChars...>(str, CharType(str[i] - 31), forward<CharType>(rest)...); | |
} | |
} | |
template<typename CharType, std::size_t N> | |
constexpr inferior_encrypted_string<CharType, N> make_inferior_encrypted_string(const CharType (& str)[N]) noexcept | |
{ | |
return detail::make_inferior_encrypted_string_impl<CharType, N, N - 2, nullptr>(str, CharType('\0')); | |
} | |
#else | |
template<typename CharType, std::size_t N> | |
DXLIB_CUSTOM_CONSTEXPR inferior_encrypted_string<CharType, N> make_inferior_encrypted_string(const CharType (& str)[N]) DXLIB_CUSTOM_NOEXCEPT | |
{ | |
# if defined(DXLIB_CUSTOM_NO_CXX11_CONSTEXPR) && defined(DXLIB_CUSTOM_NO_CXX14_CONSTEXPR) | |
inferior_encrypted_string<CharType, N> re; | |
# else | |
//In constexpr function, uninitialized variable is not parmitted | |
inferior_encrypted_string<CharType, N> re{}; | |
# endif | |
for(std::size_t i = 0; i < N - 1; ++i) re.str[i] = CharType(str[i] - 31); | |
re.str[N - 1] = CharType('\0'); | |
return re; | |
} | |
#endif | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "inferior_encrypted_string.hpp" | |
#include <iostream> | |
#if defined(DXLIB_CUSTOM_NO_CXX11_CONSTEXPR) && defined(DXLIB_CUSTOM_NO_CXX14_CONSTEXPR) | |
const inferior_encrypted_string::inferior_encrypted_string<char, sizeof("arikitari")> s1 = inferior_encrypted_string::make_inferior_encrypted_string("arikitari"); | |
const inferior_encrypted_string::inferior_encrypted_string<char, sizeof("ありきたりな世界")> s2 = inferior_encrypted_string::make_inferior_encrypted_string("ありきたりな世界"); | |
#else | |
constexpr auto s1 = inferior_encrypted_string::make_inferior_encrypted_string("arikitari"); | |
constexpr auto s2 = inferior_encrypted_string::make_inferior_encrypted_string("ありきたりな世界"); | |
#endif | |
int main() | |
{ | |
std::cout | |
<< s1.str << std::endl | |
<< s1.decrypt() << std::endl | |
<< std::endl | |
<< s2.str << std::endl | |
<< s2.decrypt() << std::endl; | |
} |
C++03でもコンパイルが通るようにだけした。ただし簡易暗号化機能はない。暗号化は実行時に行われる
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
動作例: https://wandbox.org/permlink/SOsUuAvhLRbFeWUS