Skip to content

Commit

Permalink
Allow for C++20 standard compiling
Browse files Browse the repository at this point in the history
C++20 standard introduces std::remove_cvref<T>, which allows for a reduction of obfuscator code.
C++20 allows for consteval expressions which force the compiler to only accept compile-time
evaluation of functions. This is especially useful for obfuscating purposes.
  • Loading branch information
Maarten van Geijn committed Jun 30, 2024
1 parent e65173d commit 0cbf8c0
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions obfuscate.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ std::cout << obfuscated_string << std::endl;
----------------------------------------------------------------------------- */

#pragma once
#if __cplusplus >= 202002L
#include <type_traits>
#endif

// Workaround for __LINE__ not being constexpr when /ZI (Edit and Continue) is enabled in Visual Studio
// See: https://developercommunity.visualstudio.com/t/-line-cannot-be-used-as-an-argument-for-constexpr/195665
Expand All @@ -51,6 +54,10 @@ namespace ay
using size_type = unsigned long long;
using key_type = unsigned long long;

#if __cplusplus >= 202002L
template <typename T>
using char_type = std::remove_cvref_t<T>;
#else
template <typename T>
struct remove_const_ref {
using type = T;
Expand All @@ -73,9 +80,14 @@ namespace ay

template <typename T>
using char_type = typename remove_const_ref<T>::type;
#endif

// Generate a pseudo-random key that spans all 8 bytes
#if __cplusplus >= 202002L
consteval key_type generate_key(key_type seed)
#else
constexpr key_type generate_key(key_type seed)
#endif
{
// Use the MurmurHash3 64-bit finalizer to hash our seed
key_type key = seed;
Expand Down Expand Up @@ -108,7 +120,11 @@ namespace ay
{
public:
// Obfuscates the string 'data' on construction
#if __cplusplus >= 202002L
consteval obfuscator(const CHAR_TYPE* data)
#else
constexpr obfuscator(const CHAR_TYPE* data)
#endif
{
// Copy data
for (size_type i = 0; i < N; i++)
Expand All @@ -126,12 +142,20 @@ namespace ay
return &m_data[0];
}

#if __cplusplus >= 202002L
consteval size_type size() const
#else
constexpr size_type size() const
#endif
{
return N;
}

#if __cplusplus >= 202002L
consteval key_type key() const
#else
constexpr key_type key() const
#endif
{
return KEY;
}
Expand Down Expand Up @@ -211,7 +235,11 @@ namespace ay
// This function exists purely to extract the number of elements 'N' in the
// array 'data'
template <size_type N, key_type KEY = AY_OBFUSCATE_DEFAULT_KEY, typename CHAR_TYPE = char>
#if __cplusplus >= 202002L
consteval auto make_obfuscator(const CHAR_TYPE(&data)[N])
#else
constexpr auto make_obfuscator(const CHAR_TYPE(&data)[N])
#endif
{
return obfuscator<N, KEY, CHAR_TYPE>(data);
}
Expand Down

0 comments on commit 0cbf8c0

Please sign in to comment.