是C preprocessor能够处理由字符的字符串烧焦? [英] Is the C preprocessor able to process strings char by char?

查看:153
本文介绍了是C preprocessor能够处理由字符的字符串烧焦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在编译时掩盖的字符串。我知道它可以在其他preprocessors做,但我还没有找到一种方法用在 C preprocessor做到这一点。

I'd like to obscure strings at compile time. I know it can be done in other preprocessors but I haven't found a way to do this with the C preprocessor.

推荐答案

好了,你可以做到这一点,但它是丑陋的。

Well, you can do it, but it's ugly.

#define ENCODE_STRING_14(str) {\
			str[0] ^ 0x020,\
			str[1] ^ 0x020,\
			str[2] ^ 0x020,\
			str[3] ^ 0x020,\
			str[4] ^ 0x020,\
			str[5] ^ 0x020,\
			str[6] ^ 0x020,\
			str[7] ^ 0x020,\
			str[8] ^ 0x020,\
			str[9] ^ 0x020,\
			str[10] ^ 0x020,\
			str[11] ^ 0x020,\
			str[12] ^ 0x020,\
			'\0'\
			}

void Decode( char *str, int length )
{
    for( int i = 0; i < length - 1; ++i )
    {
    	str[i] ^= 0x20;
    }
}

char hello[] = ENCODE_STRING_14("Hello, World!");

int main()
{
    printf("%s\n", hello);
    Decode( hello, 14 );
    printf("%s\n", hello);
    return 0;
}

编纂的在VS2005优化的,该字符串存储在可执行文件你好\\ X0C \\ 0wORLD \\ X01。现在,很明显,与异或0x20的是不是一个很好的功能,以隐藏自己的字符串。而且,当然,你必须#定义每个字符串长度的宏。

Compiled with optimizations in VS2005, the string is stored in the executable as "hELLO\x0C\0wORLD\x01". Now, obviously, xor with 0x20 isn't a good function to hide your string. And, of course, you'd have to #define a macro for each string length.

显然,这不是最好的解决方案。 C ++模板元编程将是一个更好的选择。你也可以写您的所有字符串在一个单独的机器可读的文件,并单独写一个程序,即解析,掩盖了弦的任何方式您认为合适的,并输出到所有.H / .C。 。这都是比这更好的解决方案。

Obviously, this isn't the best solution. C++ template metaprogramming would be a better fit. You could also write all of your strings in a separate machine readable file, and write a separate program that parses that, obscures the strings in whatever way you see fit and outputs it all into .h/.c. Both of those are better solutions than this.

这篇关于是C preprocessor能够处理由字符的字符串烧焦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆