P. 1


|Views: 458|Likes:
Published by abhijeetnayak

More info:

Published by: abhijeetnayak on Mar 25, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





What does the following C code function do? Is it useful? Why or why not?

void MyFunction(int *arrayA, int *arrayB, cnt)

switch(cnt % 6)

while(cnt ?= 0)

case 0:

arrayA[cnt] = arrayB[cnt--];

case 5:

arrayA[cnt] = arrayB[cnt--];

case 4:

arrayA[cnt] = arrayB[cnt--];

case 3:

arrayA[cnt] = arrayB[cnt--];

case 2:

arrayA[cnt] = arrayB[cnt--];

case 1:

arrayA[cnt] = arrayB[cnt--];




This piece of code is known as a Duff's device or "Duff's machine". It is used to partially unwind a loop

for efficiency. Notice the strange way that the while() is nested inside the switch statement? Two

arrays of integers are passed to the function, and at each iteration of the while loop, 6 consecutive

elements are copied from arrayB to arrayA. The switch statement, since it is outside the while loop,

only occurs at the beginning of the function. The modulo is taken of the variable cnt with respect to 6.

If cnt is not evenly divisible by 6, then the modulo statement is going to start the loop off somewhere in

the middle of the rotation, thus preventing the loop from causing a buffer overflow without having to

test the current count after each iteration.

Duff's Device is considered one of the more efficient general-purpose methods for copying strings,

arrays, or data streams.

Code Obfuscation

Code Obfuscation

Code Obfuscation is the act of making the assembly code or machine code of a program more difficult to

disassemble or decompile. The term "obfuscation" is typically used to suggest a deliberate attempt to add

difficulty, but many other practices will cause code to be obfuscated without that being the intention. Software

vendors may attempt to obfuscate or even encrypt code to prevent reverse engineering efforts. There are many

different types of obfuscations. Notice that many code optimizations (discussed in the previous chapter) have

the side-effect of making code more difficult to read, and therefore optimizations act as obfuscations.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->