21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

init_rand(a, b);<br />

return 0;<br />

}<br />

12.8 Disguising Boolean Values<br />

<strong>Problem</strong><br />

Variables representing boolean values are difficult to disguise because they usually<br />

compile to comparisons with 0 or 1.<br />

Solution<br />

Disguising boolean values can be tackled effectively at the assembly-language level<br />

by replacing simple test-and-branch code with more complex branching (see Recipe<br />

12.3). Alternatively, the default boolean test can be replaced with an addition.<br />

Discussion<br />

By replacing the default boolean test—usually a sub or an and instruction—with an<br />

addition, the purpose of the variable becomes unclear. Rather than implying a yes or<br />

no decision, the variable appears to represent two related values:<br />

typedef struct {<br />

char x;<br />

char y;<br />

} spc_bool_t;<br />

#define SPC_TEST_BOOL(b) ((b).x + (b).y)<br />

#define SPC_SET_BOOL_TRUE(b) do { (b).x = 1; (b).y = 0; } while (0)<br />

#define SPC_SET_BOOL_FALSE(b) do { (b).x = -10; (b).y = 10; } while (0)<br />

The SPC_TEST_BOOL macro can be used in conditional expressions:<br />

spc_bool_t b;<br />

SPC_SET_BOOL_TRUE(b);<br />

if (SPC_TEST_BOOL(b)) printf("true!\n");<br />

else printf("false!\n");<br />

See Also<br />

Recipe 12.3<br />

670 | Chapter 12: Anti-Tampering<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!