Search code examples
c++cvisual-c++macrosprebuild

How to make a C macro pre-build pre-processor?


Let's say I have a string that I would like to obfuscate in my code. (This example is just for learning.)

My plan is to wrap the string literal with a macro, e.g.

#define MY_STRING "lol"
const char *get_string() { return _MY_ENCRYPTION_MACRO(MY_STRING); }

and, as a pre-build step, to run my source file through my own preprocessor to look for all usages of _MY_ENCRYPTION_MACRO and obfuscate the strings accordingly.

How would I go about doing this preprocessing with Visual C++?


Solution

  • If you used a recent GCC (i.e. GCC 4.6) on Linux, you could also have a plugin which provides a builtin function to "encrypt" compile time strings, or you could even make it a GCC MELT extension (MELT is a high-level domain specific language to extend GCC).

    If you use some other C++, you might have your own pre-processing scripts finding your macros. You might for instance have some program which scan every occurrence of ENCRYPTSTRING("anyconstantstring") in all your C++ sources, and generate a mycrypt.h file which you #include "mycrypt.h" in your C++ code. Then you might do tricks like

    #define ENCRYPTSTRING(S) ENCRPYTSTRING_AT(S,__LINE__)
    #define ENCRYPTSTRING_AT(S,L) cryptstring_#L
    

    and have your generated "mycrypt.h" contain things like

    const char crypstring_123[]="thecryptedstringatline123";
    

    etc. The "mycrypt.h" generator could be an awk or python or ocaml (etc...) script.