[ Team LiB ] |
12.7 Splitting Variables12.7.1 ProblemLarge scalar variables that cannot be merged, or that have large values that cannot easily be manipulated with a constant transform, need to be obfuscated. 12.7.2 SolutionSplitting variables can be effective when the variables holding the split values are in different scopes. The split can also be performed during variable initialization by rewriting the SPLIT_VAR macro presented in Section 12.7.3 to declare and initialize the variables, rather than simply assigning to them. 12.7.3 DiscussionThe value of a scalar variable can be split over a number of equal- or smaller-sized variables. The following code demonstrates how the four bytes of an integer can be stored in four different character variables: #define SPLIT_VAR(in, a, b, c, d) do { \ (a) = (char)((in) >> 24); \ (b) = (char)((in) >> 16); \ (c) = (char)((in) >> 8); \ (d) = (char)((in) & 0xFF); \ } while (0) #define REBUILD_VAR(a, b, c, d) \ ((((a) << 24) & 0xFF000000) | (((b) << 16) & 0x00FF0000) | \ (((c) << 8) & 0x0000FF00) | ((d) & 0xFF)) Each char variable (a, b, c, and d) is filled with a byte of the original four-byte integer variable. This is done by shifting each byte in turn into one of the char variables. Obviously, the four char variables should not be stored contiguously in memory, or splitting the variable will have no effect. #include <stdlib.h> char g1, g2; /* store half of the integer here */ void init_rand(char a, char b) { srand(REBUILD_VAR(a, g1, b, g2)); } int main(int argc, char *argv[ ]) { int seed = 0x81206583; char a, b; SPLIT_VAR(seed, a, g1, b, g2); init_rand(a, b); return 0; } |
[ Team LiB ] |