compile time string manipulation

akcom CppCoder at gmail.com
Fri Apr 6 22:09:41 PDT 2007


I'm trying to change a string at compile time to avoid static analysis. 
Example:

T[] obfuscate(T)( T[] buf )
{
     return cast(char)(buf[0] ^ 0xCC) ~ obfuscate( buf[1..$] );
}

char []unobfuscate( char []str )
{
    char []result = new char[str.length];
    foreach ( idx, c; str )
    {
        result[idx] = c ^ 0xCC;
    }
    return result;
}

in my code I have something like Stdout.formatln( "hello {0}", 
unobfuscate( obfuscate( "world!" ) ) );
What I'd like to happen, is for obfuscate to get evaluated at compile time 
so that instead of "world!" being visible in my executable's binary data, it 
would be something like 0x77 0x6F 0x72 0x6C 0x64 0x21.  unobfuscate would 
get called at runtime and would transform the string back to it's original 
form.  Any ideas?  (Note: I'm aware that obfuscate will throw an 
ArrayOutOfBounds exception, it's just to explain what I'm trying to do).

Thanks in advance! 




More information about the Digitalmars-d-learn mailing list