Wednesday, 19 December 2012

..and now something seasonal

The Christmas holidays are almost here and to celebrate I've mildly obfuscated some C that prints the lyrics to a traditional English carol. The source code can be found here

#include <stdio.h>
#include <stdbool.h>
#include <malloc.h>
#define H malloc (1237) ;
#define c while
#define W >> /* fallen right tree */
#define w << /* fallen left tree */
#define B(wish,times) ((_ &wish) times 2)
#define L {o Q=q;p(3[Z]),P(false[Z],q),\
p(q>2?"th":N),p(Z[2]);c(true+Q)p(q|Q?Q?\
N:4[Z]:"a "),P(1[Z],Q),p(Q>1?", ":N),--\
Q;p(".\n\n"),++q;}
char typedef o;void typedef d

                         ;
                        o*N
                       ="";o
                      q;o*O(o
                       *p){o
                      *P,_;o*
                     f=P=H;c(_
                    =*p)_^=1,*f
                   ++=B(1,w)|B(4
                    ,W)|B(2,w)|
                   B(8,W)|(_&240
                  ),++p,*f=false;
                 return P;}d p(o*f
                ){fputs(f,stdout);}
                 d P(o*s,o o){c( o
                --){c(122-*s)s++; s
               ++;}c(122-*s)putchar(
              *s++);} o main(void){o*
             Z[]={O("hgy}p{}dmnj`{pcg"
              "y`{hny{hgh{}gs{}dxdj{"
             "dglc{jgj{pdj{dbdxdj{p|d"
            "bh{"),O("qeypyg`ld!gj!e!q"
           "dey!pydd{p|n!ptypbd!`nxd}{p"
          "cydd!Hydjmc!Cdj}{hnty!mnbbw!i"
                       "gy`"
                       "}{h"
                       "gxd"
                       "!ln"
                       "b`d"
                 "j!ygjl}{}gs!ldd}"
                 "d&e&bewgjl{}dxdj"
                  "!}|ej}&e&}|gff"
                  "gjl{dglcp!feg`"
"}&e&fgbogjl{jgjd!be`gd}!`ejmgjl{pdj!bny`}&e&bdeqgjl{"
"dbdxdj!qgqdy}&qgqgjl{p|dbxd!`ytffdy}!`ytffgjl{") ,O (
"!`ew!nh!Mcyg}pfe}!fw!pytd!bnxd!lexd!pn!fd!"),O("Nj!p"
"cd!"),O("!ej`!e!")};L L L L L L L L L L L L /*Xmas*/}

6 comments:

  1. That's beautiful. Thank you for sharing this!

    ReplyDelete
  2. Thank you and Merry Christmas!

    Typos: eight maids, not 'eighth'; ninth day, not 'nineth'





    ReplyDelete
    Replies
    1. Thanks for spotting the typos. Now fixed. :-)

      Delete
  3. I keep on wondering how people can do this... and how much time it would take to do it.

    ReplyDelete
    Replies
    1. It depends on how obfuscated one wants the code. I just wrote the program and then mangled the array of text a bit and re-encoded it into something unreadable. I then used some standard tricks like using the ternary operator and macros to mangle the code a little more and then it's a simple case of re-formatting the resulting mess into something pretty. I just used some very elementary code obfuscation and all in all it was a couple of hours from start to finish.

      For a more mangled example, see http://smackerelofopinion.blogspot.co.uk/2011/10/dennis-ritchie-rip.html - this example contains a small stack machine simulator and op-codes to print out a message. This took more effort as the obfuscation is in the actual execution of the code as well as the code layout.

      Delete