compile time string manipulation
Thomas Kuehne
thomas-dloop at kuehne.cn
Sat Apr 7 00:19:02 PDT 2007
-----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).
# char[] obfuscate(char[] buf){
# char[] result;
# foreach(c; buf){
# result ~= c ^ 0xCC;
# }
# return result;
# }
#
# template compiletime(char[] m){
# const compiletime = m;
# }
#
# import std.stdio;
#
# void main(){
# writefln("%s", cast(ubyte[]) compiletime!(obfuscate("hello!")));
# }
Thomas
-----BEGIN PGP SIGNATURE-----
iD8DBQFGF1KLLK5blCcjpWoRAlxyAJ45HvncahcYt7wsfxiJtkRw7Kfr3gCfc4bI
6lXl/OqnVHLQPpCJlSNzs0o=
=eHGg
-----END PGP SIGNATURE-----
More information about the Digitalmars-d-learn
mailing list