www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - compile time string manipulation

reply "akcom" <CppCoder gmail.com> writes:
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! 
Apr 06 2007
parent reply Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

akcom schrieb am 2007-04-07:
 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).
Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFGF1KLLK5blCcjpWoRAlxyAJ45HvncahcYt7wsfxiJtkRw7Kfr3gCfc4bI 6lXl/OqnVHLQPpCJlSNzs0o= =eHGg -----END PGP SIGNATURE-----
Apr 07 2007
next sibling parent "akcom" <CppCoder gmail.com> writes:
Unfortunately the string still shows up in plain text in my executable.

Regards,
Alex
"Thomas Kuehne" <thomas-dloop kuehne.cn> wrote in message 
news:slrnf1ekkm.cq2.thomas-dloop birke.kuehne.cn...
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1

 akcom schrieb am 2007-04-07:
 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).
Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFGF1KLLK5blCcjpWoRAlxyAJ45HvncahcYt7wsfxiJtkRw7Kfr3gCfc4bI 6lXl/OqnVHLQPpCJlSNzs0o= =eHGg -----END PGP SIGNATURE-----
Apr 07 2007
prev sibling parent "akcom" <CppCoder gmail.com> writes:
Nevermind, I made a stupid mistake.  Thank you very much for your help :)
"Thomas Kuehne" <thomas-dloop kuehne.cn> wrote in message 
news:slrnf1ekkm.cq2.thomas-dloop birke.kuehne.cn...
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1

 akcom schrieb am 2007-04-07:
 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).
Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFGF1KLLK5blCcjpWoRAlxyAJ45HvncahcYt7wsfxiJtkRw7Kfr3gCfc4bI 6lXl/OqnVHLQPpCJlSNzs0o= =eHGg -----END PGP SIGNATURE-----
Apr 07 2007