From 8401ac212fda7740baf02cb8399084d33985d459 Mon Sep 17 00:00:00 2001 From: revsic Date: Mon, 25 Feb 2019 14:23:27 +0900 Subject: [PATCH] obfs, fsm: Fix template overloading --- obfuscator.hpp | 232 ++++++++++++++++++++-------------------- obfuscator/obfs/fsm.hpp | 9 +- 2 files changed, 118 insertions(+), 123 deletions(-) diff --git a/obfuscator.hpp b/obfuscator.hpp index 67a1924..528bfd7 100644 --- a/obfuscator.hpp +++ b/obfuscator.hpp @@ -4,130 +4,14 @@ #include #include +#define COMPILE_TIME_SEQUENCE #define OBFS_FINITE_STATE_MACHINE #define COMPILE_TIME_RANDOM #define MAKE_RAND_VAL(MOD) RAND_VAL<__LINE__, MOD> -#define COMPILE_TIME_SEQUENCE #define OBFS_STRING #define MAKE_STRING(Var, String, ...) constexpr auto Var = obfs::make_string<__VA_ARGS__>(String); -namespace obfs { - constexpr bool FreeAction() { - return false; - } - - template - struct Next { - using event = Event; - using state = State; - constexpr static bool(*action)() = Action; - }; - - struct Pass {}; - - template - struct First { - using type = std::conditional_t< - std::is_same_v, - typename First::type, - T>; - }; - - template - struct First { - using type = std::conditional_t< - std::is_same_v, - IfAllPass, - T>; - }; - - struct None {}; - - template - struct Stage { - using state = State; - - template - using act = std::conditional_t< - std::is_same_v, Cond, Pass>; - - template - using next = typename First, act...>::type; - }; - - template - struct StateMachine { - template - using find = std::conditional_t< - std::is_same_v, ST, Pass>; - - using stage = typename First...>::type; - - template - using next = typename stage::template next; - - template - using next_s = StateMachine::state, Specs...>; - - template - constexpr static void run() { - if (next::action()) { - return; - } - next_s::template run(); - } - - template - constexpr static void run() { - next::action(); - } - }; -} - - -namespace obfs { - constexpr char TIME[] = __TIME__; - constexpr int digit(char c) { - return c - '0'; - } - constexpr int SEED = digit(TIME[7]) + - digit(TIME[6]) * 10 + - digit(TIME[4]) * 60 + - digit(TIME[3]) * 600 + - digit(TIME[1]) * 3600 + - digit(TIME[0]) * 36000; - - using size_t = decltype(sizeof(void*)); - - template - struct Xorshiftplus { - using prev = Xorshiftplus; - - constexpr static size_t update() { - constexpr size_t x = prev::state0 ^ (prev::state0 << 23); - constexpr size_t y = prev::state1; - return x ^ y ^ (x >> 17) ^ (y >> 26); - } - - constexpr static size_t state0 = prev::state1; - constexpr static size_t state1 = update(); - - constexpr static size_t value = state0 + state1; - }; - - template <> - struct Xorshiftplus<0> { - constexpr static size_t state0 = static_cast(SEED); - constexpr static size_t state1 = static_cast(SEED << 1); - constexpr static size_t value = state0 + state1; - }; - - template - constexpr size_t RAND_VAL = Xorshiftplus::value % Mod; -} - - namespace obfs { template struct TypeVal { @@ -200,6 +84,120 @@ namespace obfs { } +namespace obfs { + constexpr bool FreeAction() { + return false; + } + + template + struct Next { + using event = Event; + using state = State; + constexpr static bool(*action)() = Action; + }; + + struct Pass {}; + + template + struct First { + using type = std::conditional_t< + std::is_same_v, + typename First::type, + T>; + }; + + template + struct First { + using type = std::conditional_t< + std::is_same_v, + IfAllPass, + T>; + }; + + struct None {}; + + template + struct Stage { + using state = State; + + template + using act = std::conditional_t< + std::is_same_v, Cond, Pass>; + + template + using next = typename First, act...>::type; + }; + + template + struct StateMachine { + template + using find = std::conditional_t< + std::is_same_v, ST, Pass>; + + using stage = typename First...>::type; + + template + using next = typename stage::template next; + + template + using next_s = StateMachine::state, Specs...>; + + template + constexpr static void run() { + if (next::action()) { + return; + } + + if constexpr (sizeof...(Others) > 0) { + next_s::template run(); + } + } + }; +} + + +namespace obfs { + constexpr char TIME[] = __TIME__; + constexpr int digit(char c) { + return c - '0'; + } + constexpr int SEED = digit(TIME[7]) + + digit(TIME[6]) * 10 + + digit(TIME[4]) * 60 + + digit(TIME[3]) * 600 + + digit(TIME[1]) * 3600 + + digit(TIME[0]) * 36000; + + using size_t = decltype(sizeof(void*)); + + template + struct Xorshiftplus { + using prev = Xorshiftplus; + + constexpr static size_t update() { + constexpr size_t x = prev::state0 ^ (prev::state0 << 23); + constexpr size_t y = prev::state1; + return x ^ y ^ (x >> 17) ^ (y >> 26); + } + + constexpr static size_t state0 = prev::state1; + constexpr static size_t state1 = update(); + + constexpr static size_t value = state0 + state1; + }; + + template <> + struct Xorshiftplus<0> { + constexpr static size_t state0 = static_cast(SEED); + constexpr static size_t state1 = static_cast(SEED << 1); + constexpr static size_t value = state0 + state1; + }; + + template + constexpr size_t RAND_VAL = Xorshiftplus::value % Mod; +} + + namespace obfs { using Encoder = char(*)(char); using Decoder = char(*)(char); diff --git a/obfuscator/obfs/fsm.hpp b/obfuscator/obfs/fsm.hpp index b7a1a26..beba1cb 100644 --- a/obfuscator/obfs/fsm.hpp +++ b/obfuscator/obfs/fsm.hpp @@ -66,12 +66,9 @@ namespace obfs { if (next::action()) { return; } - next_s::template run(); - } - - template - constexpr static void run() { - next::action(); + if constexpr (sizeof...(Others) > 0) { + next_s::template run(); + } } }; }