Thread: How Obfusicated code works

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64

    How Obfusicated code works

    This code prints the map of India. How does it work?
    Code:
    #include <stdio.h>
    main()
    {
        int a,b,c;
        int count = 1;
        for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\
        TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
        UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
        NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
        HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
        T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
        Hq!WFs XDt!" [b+++21]; )
        for(; a-- > 64 ; )
        putchar ( ++c=='Z' ? c = c/ 9:33^b&1);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The "joy" of obfuscated code is that YOU get to work out why it works. The whole point of obfuscated coding contests is to highlight the benefits of good coding practice, by showing how badly things can be done.

    That code is making use of a couple of obscure and dark corners of C. You'll need to understand those to understand what the code does, otherwise any description people give you will seem meaningless.

    Even worse, there is at least one example of implementation-defined behaviour, so the code is not guaranteed to work with all compilers.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Quote Originally Posted by grumpy View Post
    The "joy" of obfuscated code is that YOU get to work out why it works. The whole point of obfuscated coding contests is to highlight the benefits of good coding practice, by showing how badly things can be done.

    That code is making use of a couple of obscure and dark corners of C. You'll need to understand those to understand what the code does, otherwise any description people give you will seem meaningless.

    Even worse, there is at least one example of implementation-defined behaviour, so the code is not guaranteed to work with all compilers.
    Please can you provide me any link to where i can learn about this dark corners?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I assume you know the conditional operator and that the \ at the ends of the lines building the string cause the newline to be ignored.

    Remember that characters are also numbers. Look at an ascii table. E.g., 'Z'==90, '!'==33, ' '==32

    You need to know operator precedence. 33 ^ b & 1 == 33 ^ (b & 1)

    String constants can be indexed like arrays: "abcdefg"[3] == 'd'

    (b+++21) == (b++ + 21)


    The map is stored in the string by run-length-encoding. Basically, how many spaces, how many bangs, how many spaces, etc. Directing the output to a file is informative. Note the spaces on the right.


    Try rewriting the code as clearly as you can.
    * Properly indent it.
    * Put the string together on one line.
    * Remove the conditional operator.
    * Change letters to numbers where it makes more sense.
    * Simplify anything you can.
    * Always run it after each change to make sure you didn't mess something up.

    Code:
    .                   !!!!!!
                        !!!!!!!!!!
                         !!!!!!!!!!!!!!!
                           !!!!!!!!!!!!!!
                         !!!!!!!!!!!!!!!
                          !!!!!!!!!!!!
                          !!!!!!!!!!!!
                            !!!!!!!!!!!!
                            !!!!!!!!
                            !!!!!!!!!!
                           !!!!!!!!!!!!!!
                         !!!!!!!!!!!!!!!!
                        !!!!!!!!!!!!!!!!                                  !!!!!
                      !!!!!!!!!!!!!!!!!!!                               !!!!!!!!!!
                     !!!!!!!!!!!!!!!!!!!!!!!                 !         !!!!!!!!!!
                !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!              !!     !!!!!!!!!!!!
               !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!        !!      !!!!!!!!
                !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!
                  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  !!!!!!!!!!!!
           !!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!        !!!!!!
          !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!      !!!!!
              !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!        !!!
            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!        !
              !!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
               !!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                      !!!!!!!!!!!!!!!!!!!!!!!!!!!!
                      !!!!!!!!!!!!!!!!!!!!!!!!!!
                      !!!!!!!!!!!!!!!!!!!!!!!!!
                       !!!!!!!!!!!!!!!!!!!!!!!!
                        !!!!!!!!!!!!!!!!!!!!
                        !!!!!!!!!!!!!!!!!!!
                         !!!!!!!!!!!!!!!!
                          !!!!!!!!!!!!!!!!
                          !!!!!!!!!!!!!!!
                           !!!!!!!!!!!!!!
                            !!!!!!!!!!!!
                            !!!!!!!!!!!!
                            !!!!!!!!!!!!
                              !!!!!!!!
                              !!!!!!
                               !!!!
    Last edited by oogabooga; 08-17-2013 at 03:49 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How come my code works? Seriously. :)
    By assiduus in forum C Programming
    Replies: 15
    Last Post: 02-10-2011, 12:54 PM
  2. Not sure how this simple code works
    By metros in forum C Programming
    Replies: 8
    Last Post: 02-09-2010, 06:22 PM
  3. Code works in MS Visual C++, not in Dev-C++
    By Kheila in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2005, 01:15 PM
  4. code only works with 3 digits
    By bejiz in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2005, 05:42 AM
  5. How Do u See if a code works
    By Nos in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2005, 01:34 PM