From ed89332b465d6a4663482e5f86eca424bbaf9d81 Mon Sep 17 00:00:00 2001 From: micio Date: Wed, 2 Feb 2011 23:02:40 +0000 Subject: [PATCH] Bazaar/Protect : fixed compiling in optimal mode for GCC git-svn-id: svn://ultimatepp.org/upp/trunk@3150 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- bazaar/Protect/Protect.h | 12 ++++++++++-- bazaar/Protect/srcdoc.tpp/HowItWorks$en-us.tpp | 6 ++++-- bazaar/Protect/srcdoc.tpp/Obfuscation$en-us.tpp | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/bazaar/Protect/Protect.h b/bazaar/Protect/Protect.h index f422535ea..dc99853dd 100644 --- a/bazaar/Protect/Protect.h +++ b/bazaar/Protect/Protect.h @@ -16,6 +16,9 @@ using namespace Upp; #ifdef PLATFORM_POSIX +// no optimize with -O2 or more an encrypted function +#define PROTECT_NO_OPTIMIZE __attribute__((optimize(0))) + #define PROTECT_START_FUNC(decrFunc) \ static bool __decrypted = false; \ if(!__decrypted) \ @@ -81,6 +84,10 @@ using namespace Upp; #else +// no optimize strongly an encrypted function +// @@ TO DO -- check if needed on MSC... +#define PROTECT_NO_OPTIMIZE + #define _PROTECT_START_MARKER __asm _emit 0xba __asm _emit 0xad __asm _emit 0xde __asm _emit 0xad __asm _emit 0xfa __asm _emit 0xce #define _PROTECT_END_MARKER __asm _emit 0xc0 __asm _emit 0xde __asm _emit 0xde __asm _emit 0xad __asm _emit 0xfe __asm _emit 0xed @@ -202,8 +209,9 @@ using namespace Upp; const int len = strlen("abracadabra"); \ Bufferbuf(len); \ Buffernonce(6); \ - memcpy(buf, crypted + 6 /* sizeof(PROTECT_START_MARKER)*/, len); \ - memcpy(nonce, crypted, 6); \ + /* can't use memcpy, it gets optimized out */ \ + for(int i = 0; i < len; i++) buf[i] = crypted[i + 6 /* sizeof(PROTECT_START_MARKER)*/]; \ + for(int i = 0; i < 6 /* sizeof(PROTECT_START_MARKER)*/; i++) nonce[i] = crypted[i]; \ decrFunc(buf, len, nonce, 6); \ __keyOk = !memcmp(buf, "abracadabra", len); \ } \ diff --git a/bazaar/Protect/srcdoc.tpp/HowItWorks$en-us.tpp b/bazaar/Protect/srcdoc.tpp/HowItWorks$en-us.tpp index fbba71ac5..f58f6d07a 100644 --- a/bazaar/Protect/srcdoc.tpp/HowItWorks$en-us.tpp +++ b/bazaar/Protect/srcdoc.tpp/HowItWorks$en-us.tpp @@ -896,9 +896,11 @@ string, every 2 chars represent 1 hex byte.&] [s0; &] [s0;i150;O0; In your application, enclose parts of code you want to be encrypted by mean of PROTECT`_START`_FUNC(decrFunc) and -PROTECT`_END`_FUNC macros :&] +PROTECT`_END`_FUNC macros, and mark function as [* non`-optimizable], +otherwise the optimizing compiler will rearrange the code and +make the system crash : :&] [s0; &] -[s0;l160; [/ void MyEncryptedFunction(void)]&] +[s0;l160; [*/ PROTECT`_NO`_OPTIMIZE][/ void MyEncryptedFunction(void)]&] [s0;l160; [/ `{]&] [s0;l160; [/ -|][*/ PROTECT`_START`_FUNC(Decrypt);]&] [s0;l160;/ &] diff --git a/bazaar/Protect/srcdoc.tpp/Obfuscation$en-us.tpp b/bazaar/Protect/srcdoc.tpp/Obfuscation$en-us.tpp index bdd7a2571..c8f89385b 100644 --- a/bazaar/Protect/srcdoc.tpp/Obfuscation$en-us.tpp +++ b/bazaar/Protect/srcdoc.tpp/Obfuscation$en-us.tpp @@ -28,7 +28,7 @@ we can make cracker`'s live a bit more difficult if we encrypt somehow also sensitive parts of code with a random key.&] [s0; This is done by another couple of macros :&] [s0; &] -[s0;l160; [/ void MyObfuscatedFunction(void)]&] +[s0;l160; [*/ PROTECT`_NO`_OPTIMIZE][/ void MyObfuscatedFunction(void)]&] [s0;l160; [/ `{]&] [s0;l160; [/ -|][*/ OBFUSCATE`_START`_FUNC;]&] [s0;l160;/ &]