Monday, March 07, 2005

Obfuscation + Beer don't mix!

Many C programmers will recall the famous "Twelve Days of Christmas" example, seen here:
Wikipedia: Obfuscated Code - Examples
This code is just crazy and indecipherable, but it manages to print out the entire lyrics to the aforementioned song.

I stumbled upon a page which actually reverse-engineers the code and explains how it works:
Reverse Engineering the Twelve Days of Christmas

Taking this knowledge, I spent yesterday coming up with this sleek piece of obfuscated code using similar techniques:
#include<stdio.h>
int main(_,a,A)char*A;{return((!0)==_)?(main(0,99,0)):(_>4)
?((a==0[A])?putchar(1[A]):main(5,a,A+2)):(1>_)?(main(2,a,0)
,main(3,0,0),main(3,17,0),main(2,a,0),main(3,0,0),main(3,31
,0),a--,main(2,a,0),main(3,0,0),main(3,17,0),main(3,29,0),(
a==0?0:main(0,a,0))):(3<_)?((0[A]=='/')?0:(main(5,*A,"eh#e\
^Tds.\n$wk,&o)a(n* @f!d'k{u~l i%bnr,tjp"),main(4,0,A+1))):(
2<_)?(main(4,0,"*%&,,~#d*&@*%##n/*&(*,e#*$)~~./.^)'#*&(#*!\
&$(k*j)dd* ,*)n&{(!./"+a)):(&_<A)?0:(printf("%d",a));}
Now how bloody cool is that? Go ahead and run it! It prints out the lyrics to "99 Bottles of Beer," a standard programming example.
Note: In case you think its a virus or something, if you read through the code you'll notice the only functions I call are main, putchar and printf, therefore it couldn't possibly do anything harmful.

By the way, yes I am aware it prints "1 bottles of beer" - this was a decision to keep the program simpler.

The techniques used are:

  • String encryption

  • Functional coding

  • Expressions instead of statements

  • Merging of functions

  • Removal of whitespace

I've written out a full worked solution as to how I created this code, which I will publish eventually ;)
"Ninety-nine boxes of bottles of beer on the wall,
Ninety-nine boxes of bottles of beer,
You take one down, pass it around,
Ninety-seven boxes of bottles of beer on the wall"
- Principal Skinner and Martin Prince on the way to the box factory

7 Comments:

At 1:20 pm, Blogger Toria/Deb said...

Awesome :) One of my favourite songs to sing on the bus when I was a teenager LOL. Gets boring quickly as I recall.

 
At 7:09 pm, Blogger Tim Cuthbertson said...

Haha stunning job, I sure couldn't decipher it.. Nothing can beat the "loop that never ends" song though ;)

(although its probably easier to code)

 
At 7:42 pm, Blogger Eat_My_Shortz said...

Well no, but neither could I. If you read the article, you'll see they used some sort of profiling tool + a lot of knowledge. It's easier to write than decipher. That's more or less the point ;)

>(although its probably easier to code)
So I'm not sure if you're referring to the 99 bottles being easier to code than decipher, or the loop that never ends being easier to code than 99 bottles obfuscation.
But heh, that was a great Haskell loop. It'd be good to get back into Haskell.

Did u actually compile and run the code? I think thats the funnest part. Btw sorry for the edit, I've been trying to get it to fit on the page all day.
Is this a problem with HTML or Blogger... when I use the C syntax of \ at the end of a line, Blogger is automatically combining it back onto one line. I think I fixed it by writing \ instead.

 
At 7:43 pm, Blogger Eat_My_Shortz said...

OK fine... it just made it into a I mean,
& # 92 ;
without the spaces.

 
At 12:16 am, Blogger Toria/Deb said...

LOL, thank god I'm not supposed to do this. Too much like "work" ha ha ha. Good luck and keep up the good work.

 
At 11:35 pm, Anonymous Anonymous said...

Something you'll probably enjoy - if you haven't already seen it ;)

http://www-2.cs.cmu.edu/~dst/DeCSS/Gallery/

Has some fun "compressed code" and even some CSS-inspired MP3 works!

 
At 2:18 pm, Blogger Toria/Deb said...

hey, the comments are working again! HURRAY! Keep up the programming Matt :)

 

Post a Comment

<< Home