From 065eadca9d88826111e90d06666c3a8ca232cb86 Mon Sep 17 00:00:00 2001 From: revsic Date: Sun, 24 Feb 2019 04:36:47 +0900 Subject: [PATCH] obfs, fsm: Add finite state machine impl --- obfuscator.hpp | 78 +++++++++++++++++++++------------------ obfuscator/obfs/fsm.hpp | 80 ++++++++++++++++++++++------------------ sample/state_machine.cpp | 29 +++++++++++++++ 3 files changed, 116 insertions(+), 71 deletions(-) diff --git a/obfuscator.hpp b/obfuscator.hpp index 67bbfb0..67a1924 100644 --- a/obfuscator.hpp +++ b/obfuscator.hpp @@ -5,8 +5,6 @@ #include #define OBFS_FINITE_STATE_MACHINE -#define STATE(Name) struct Name {}; -#define EVENT(Name) struct Name {}; #define COMPILE_TIME_RANDOM #define MAKE_RAND_VAL(MOD) RAND_VAL<__LINE__, MOD> #define COMPILE_TIME_SEQUENCE @@ -15,66 +13,76 @@ namespace obfs { - template + constexpr bool FreeAction() { + return false; + } + + template struct Next { using event = Event; using state = State; + constexpr static bool(*action)() = Action; }; - struct FalseType {}; + struct Pass {}; - template + template struct First { using type = std::conditional_t< - std::is_same_v, - typename First::type, + std::is_same_v, + typename First::type, T>; }; - template - struct First { + template + struct First { using type = std::conditional_t< - std::is_same_v, - IfFalse, + std::is_same_v, + IfAllPass, T>; }; + struct None {}; + template struct Stage { + using state = State; template using act = std::conditional_t< - std::is_same_v, - typename Cond::state, - FalseType>; + std::is_same_v, Cond, Pass>; template - using next = typename First...>::type; + using next = typename First, act...>::type; }; - template + 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(); + } }; - - - STATE(Final); - - EVENT(none); - EVENT(Trigger); - - STATE(state1); STATE(state2); STATE(state3); STATE(state4); STATE(state5); - EVENT(event1); EVENT(event2); EVENT(event3); EVENT(event4); EVENT(event5); - - using machine = StateMachine< - Stage, - Next>, - Stage>, - Stage>, - Stage, - Next>, - Stage>>; - } diff --git a/obfuscator/obfs/fsm.hpp b/obfuscator/obfs/fsm.hpp index 2990a02..b7a1a26 100644 --- a/obfuscator/obfs/fsm.hpp +++ b/obfuscator/obfs/fsm.hpp @@ -4,68 +4,76 @@ #include namespace obfs { - template + constexpr bool FreeAction() { + return false; + } + + template struct Next { using event = Event; using state = State; + constexpr static bool(*action)() = Action; }; - struct FalseType {}; + struct Pass {}; - template + template struct First { using type = std::conditional_t< - std::is_same_v, - typename First::type, + std::is_same_v, + typename First::type, T>; }; - template - struct First { + template + struct First { using type = std::conditional_t< - std::is_same_v, - IfFalse, + std::is_same_v, + IfAllPass, T>; }; + struct None {}; + template struct Stage { + using state = State; template using act = std::conditional_t< - std::is_same_v, - typename Cond::state, - FalseType>; + std::is_same_v, Cond, Pass>; template - using next = typename First...>::type; + using next = typename First, act...>::type; }; - template + 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(); + } }; - -#define STATE(Name) struct Name {}; -#define EVENT(Name) struct Name {}; - - STATE(Final); - - EVENT(none); - EVENT(Trigger); - - STATE(state1); STATE(state2); STATE(state3); STATE(state4); STATE(state5); - EVENT(event1); EVENT(event2); EVENT(event3); EVENT(event4); EVENT(event5); - - using machine = StateMachine< - Stage, - Next>, - Stage>, - Stage>, - Stage, - Next>, - Stage>>; - } #endif \ No newline at end of file diff --git a/sample/state_machine.cpp b/sample/state_machine.cpp index c2abdb8..d16a8d0 100644 --- a/sample/state_machine.cpp +++ b/sample/state_machine.cpp @@ -1,5 +1,34 @@ #include "../obfuscator.hpp" +#include + +#define STATE(Name) struct Name {}; +#define EVENT(Name) struct Name {}; + +STATE(Final); +EVENT(Trigger); + +STATE(state1); STATE(state2); STATE(state3); STATE(state4); STATE(state5); +EVENT(event1); EVENT(event2); EVENT(event3); EVENT(event4); EVENT(event5); + +bool action() { + std::cout << "Trigged" << std::endl; + return true; +} + int main() { + using namespace obfs; + using machine = StateMachine< + state1, + Stage, + Next>, + Stage>, + Stage>, + Stage, + Next>, + Stage>>; + + // machine::run(); + return 0; } \ No newline at end of file