Obfuscating a Function – How not to write Code

Some time ago, I had to write a pretty simple function that converts an integer value into a new “format.” The function ended up being easy-to-read code. For reasons I do not remember, I decided to obfuscate the purpose of the function slightly, and I ended up with the following:

#include <stdio.h>

char* whatDoIdo(unsigned int x, char *t) {
    int i=7,j=0,v=5000,p,z[7]={0x49,0x56,0x58,0x4c,0x43,0x44,0x4d};
    while(!(t[j]='\0')&&x&&(v/=5/((i--+1)%2+1))+1) {
        while(x>=v&&(x-=v)+1&&(t[j++]=z[i])+1);
        while(i&&x>=(p=v-v*(i%2+1)/10)&&(x-=p)+1&&(t[j++]=z[i-2+(i%2)])+1
          &&(t[j++]=z[i])+1);
    }
    return t;
}

int main() {
    unsigned int x = 123;
    char t[200] = {0};
    printf("%d : %s", x, whatDoIdo(x,t));
    return 0;
}

If you run the code for different values of x in the function main(), you will quickly discover the function’s purpose, whatDoIdo(). If you are more dedicated, you can decrypt the code and guess its functionality without running it once. Indeed, you should prevent writing code like this at work or university since your colleagues/classmates or others who have to read your code most likely won’t appreciate this kind of programming style.

If you do not have a compiler at hand, you can take a look at some sample outputs that should appear familiar.

Click here to see some examples
  1  : I
  5  : V
  50 : L
  


Any guess? If not, then take a look at some more outputs:

Click here to see more examples
  87   : LXXXVII
  123  : CXXIII
  1846 : MDCCCXLVI
  


There is even an annual contest for obfuscated C code on https://www.ioccc.org/. IIf you are interested in submitting an entry for the next contest, the degree of obfuscation should be significantly higher than in this trivial example. The winning entries of the past years are really worth looking at.




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Solving Peg Solitaire with efficient Bit-Board Representations
  • Choosing a Voltage Divider Resistor for a Light Dependent Resistor
  • Deriving a Closed-Form Solution of the Fibonacci Sequence
  • a distill-style blog post
  • a post with code