Richard said:
Gernot Frisch said:
Hi,
I want to build a encryption macro set, that can crypt:
char secret[] = CRYPT("KungFu");
to anything unreadable, and then have a function:
char* DECRYPT(char* str)
{
...
}
I think it would be suffictient to encrypt a maximum of 16 characters.
Simple XOR would suffice, too. Any ideas of how that can be done?
#define CRYPT(x) my_encryption_routine(x)
I don't think you can do this without calling a function, although I would
be very interested in seeing a counter-example.
My reasoning is as follows: you'd need to do this in a single expression
that doesn't involve a compound statement (because compound statements are
not mere expressions, so they don't yield a value, which wouldn't be so bad
if they played nice with comma operators - but they don't). A function gets
around these problems nicely, but I don't see any other way to do it. In
C99 you could use an inline function, I suppose.
Here is a macro which compute the FNV hash function of the first 32
characters of a litteral string at compile time. The original litteral
string 'str' does not appear in the binary. I let you adapt this macro
to do encryption instead of computing hash code. I have another version
which stops when the tailing '\0' is encountered in the litteral string,
but the compile time is much much longer since the generated AST is
completely evaluated before reductions start (at least in gcc, but I
think in most compilers because it is in a separate compilation phase).
a+, ld.
#define ooc_staticHashStr32(str, initVal) \
OOC_STATICHASHSTR32_(str \
"\000\000\000\000\000\000\000\000" \
"\000\000\000\000\000\000\000\000" \
"\000\000\000\000\000\000\000\000" \
"\000\000\000\000\000\000\000\000", \
(initVal))
#define OOC_STATICHASHSTR32_(str, initVal) \
(((((((((((((((((((((((((((((((( \
(unsigned long)(initVal) \
*16777619ul+(unsigned char)(str)[ 0]) \
*16777619ul+(unsigned char)(str)[ 1]) \
*16777619ul+(unsigned char)(str)[ 2]) \
*16777619ul+(unsigned char)(str)[ 3]) \
*16777619ul+(unsigned char)(str)[ 4]) \
*16777619ul+(unsigned char)(str)[ 5]) \
*16777619ul+(unsigned char)(str)[ 6]) \
*16777619ul+(unsigned char)(str)[ 7]) \
*16777619ul+(unsigned char)(str)[ 8]) \
*16777619ul+(unsigned char)(str)[ 9]) \
*16777619ul+(unsigned char)(str)[10]) \
*16777619ul+(unsigned char)(str)[11]) \
*16777619ul+(unsigned char)(str)[12]) \
*16777619ul+(unsigned char)(str)[13]) \
*16777619ul+(unsigned char)(str)[14]) \
*16777619ul+(unsigned char)(str)[15]) \
*16777619ul+(unsigned char)(str)[16]) \
*16777619ul+(unsigned char)(str)[17]) \
*16777619ul+(unsigned char)(str)[18]) \
*16777619ul+(unsigned char)(str)[19]) \
*16777619ul+(unsigned char)(str)[20]) \
*16777619ul+(unsigned char)(str)[21]) \
*16777619ul+(unsigned char)(str)[22]) \
*16777619ul+(unsigned char)(str)[23]) \
*16777619ul+(unsigned char)(str)[24]) \
*16777619ul+(unsigned char)(str)[25]) \
*16777619ul+(unsigned char)(str)[26]) \
*16777619ul+(unsigned char)(str)[27]) \
*16777619ul+(unsigned char)(str)[28]) \
*16777619ul+(unsigned char)(str)[29]) \
*16777619ul+(unsigned char)(str)[30]) \
*16777619ul+(unsigned char)(str)[31])