Obfuscated Code Contest: The Results

This is a discussion on Obfuscated Code Contest: The Results within the Contests Board forums, part of the Community Boards category; Hello, It's quite simple indeed. For instance: Code: int main() { int bc; bc = 84; return 0; } Could ...

  1. #16
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    It's quite simple indeed. For instance:
    Code:
    int main() {
        int bc;
    
        bc = 84;
    
        return 0;
    }
    Could be obfuscated as the following:
    Code:
    #define a int
    #define d 0x54
    a main() {a bc=(a)d;return d-d;}
    In this example, a represents and integer. d is a hex value which represents the number 84. We create an integer called bc which equals the type cast of 0x54 which is 84. Lastly, main returns 0, as [84-84 = 0]. That is a sample obfuscated code example.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  2. #17
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I just started messing around, and it doesn't work:
    Code:
    #define hello int
    #define abc int=20*4-7/3*8/.333-504-.584584584584584584584584584585
    #define st if
    #define abb int=80*4-300*40/5-300-20
    #define haha (
    #define laugh cout<<
    
    
    hello main() {st haha abc==abb);laugh"hello world";}
    I just guessed on what to do. So what all rules apply?
    It says syntax error before '=' token. 'cout' undeclared first use this function. In function main 'int main()'.
    Edit: Can you define stuff with more than one letter?
    My computer is awesome.

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Code:
    abc==abb
    Expanding this leads to:
    Code:
    int=20*4-7/3*8/.333-504-.584584584584584584584584584585==int=80*4-300*40/5-300-20
    And the use of int there is invalid.

    When using macros, never forget that they're just dumb text replacement.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #19
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I made those numbers myself they should both equal 80. So what should I do just put the math where the ints are? Why can't I use the ints right there?
    My computer is awesome.

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You can. Just remove the int= from the macro expansions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Alright fixed that. So what do I do about in function 'int main()' and cout undeclared(first use this function)?
    My computer is awesome.

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You still need to include <iostream>, and let laugh expand to std::cout<<.

    BTW, you shouldn't write obfuscated code that way. The way to write obfuscated code is to first write working normal code. Then you take parts out and replace them by macros. This way you'll know that the program works.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #23
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Sorry, I don't know what macros are.
    My fixed code:
    Code:
    #include <iostream>
    #define hello int
    #define abc 20*4-7/3*8/.333-504-.584584584584584584584584584585
    #define st if
    #define abb 80*4-300*40/5-300-20
    #define haha (
    #define laugh std::cout<<
    #define bye std::cin.get()
    #define byeo std::cin.ignore()
    
    
    hello main() {st haha abc==abb);laugh"hello world";
    byeo;
    bye;
    }

    I look at Jessycat's code and it has stuff like he1lo, but in the .c code it is stuff like us2. Why is that?
    My computer is awesome.

  9. #24
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    #define macro_name macro_expansion

    I hope that explains
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #25
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Just look at jessycat's code how can you make a hello world program without "hellworld in there somwhere?
    My computer is awesome.

  11. #26
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by cerin
    I look at Jessycat's code and it has stuff like he1lo, but in the .c code it is stuff like us2. Why is that?
    When I wrote the first version (the normal, readable version) I was putting alot of numbers in for the ASCII art. Instead of typing 32,32,32,32 to represent the ASCII values of 4 spaces, I defined a macro called sp4 so that when i needed 4 spaces I would just have to type sp4, and when the compiler got ahold of my code it would replace every instance of sp4 with the literal values 32,32,32,32. Additionally, when I was looking back over the values, I could see that I had put sp4 and if I had actually wanted 4 underscores, I could easily change it to us4 instead of deleting and replacing a bunch of literal values. In that case, the #defines were being used as a way to make it more easily readable to a human being (the computer doesn't care either way).

    Once my code was complete and worked the way I wanted it to, I then went about replacing EVERYTHING with variations of "hello" and "world". Since the compiler will treat foo, foO, fOo, fOO, Foo, FoO, FOo and FOO as 8 different variables, I just did the same thing. Because I thought I might run out of variations of hello world, I also used 1's (ones) for L's and 0's (zeros) for o's and then kind of randomly defined one of these variations to something in the program. So, for anything that needed to literally be replaced I used a #define to make the compiler replace text, and any variable or function name that I had made up I just changed it, for example from TRANSLATE to WORLD and no replacement by the compiler is necessary since it was a user defined variable to begin with. In this case, the #defines were used to make the code extremely difficult to read (in other words, obfuscate the code!).

    Once the compiler gets ahold of my neat ASCII art, it will literally replace most of it with variable and function names (and various symbols) that make sense to a 'C' compiler. Then it will go through the processed code and compile it just like any other program, looking to make sure syntax is correct, make sure variables are used appropriately, etc. and finally generate machine instructions so that the program can be executed by the computer it was compiled for.

    Quote Originally Posted by cerin
    Just look at jessycat's code how can you make a hello world program without "hellworld in there somwhere?
    If you look at the "readable" code I included and follow it, you'll find that if the user did not type any command line arguments the main() function mallocs space for 12 characters in a char* array and then assigns the characters "H E L L O , W O R L D !" to it. Next, it sends that char*array to the other functions which go about translating those characters to the appropriate ASCII art representations and then displaying the ASCII art.

    Does that help?
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  12. #27
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by cerin
    how can you make a hello world program without "hellworld in there somwhere?
    The same way I did:
    Code:
    #include <stdio.h>
    #define _1(x) putchar((l)=(x));
    #define _2(x) ((l)<<(x))
    #define _3(x) ((l)>>(x))
    #define _4(x,y) ((x)&(1<<(y)))
    #define _5(x) ((unsigned char)(~(x)))
    #define _6(x,y) ((x)&(y))
    #define _7(x,y) ((x)>>(y))
    #define _8(x,y) ((x)<<(y))
    #define _9(x) ((l)+(x))
    #define _10(x) return (x);
    int main(void){unsigned char l=1;_1(_2(6)+8)_1(_4(_2(1),7)-_4(127,4)-11)_1(_9(_4(l,2)+_4(l,2)-1))_1(_5(_5(l)))_1(_9(_6(_5(l),3)))_1(_4(l,5))_1(_2(2)-_3(2)-1)_1(l-_4(_2(1),3))_1(_9(_6(l,3)))_1(l-_3(4)+1)_1(l-(_8(_7(l,_3(4)),3)))_1(_8(_7(l,6),5)+1)_1(_2(3)+2)_10(_2(5))}
    If you understand what you're doing, you're not learning anything.

  13. #28
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Stack Overflow
    That Is Inceridble how much free time you have!
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  14. #29
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Heh,

    Thanks. I didn't expect the Off-Site library to take nearly 3 weeks to assemble, nor 11 running contestants. Though, I wanted it perfect. So I overhauled the design two or three times. Each submission was built and tested on multiple platforms. I was lucky to finish on time. It was fun though. Especially gathering all the build logs and execution output for each submission per compiler and platform.

    Best of all, it turned out to be a success.


    - Stack Overflow
    Last edited by Stack Overflow; 02-18-2005 at 04:49 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  15. #30
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by Stack Overflow
    Best of all, it turned out to be a success.
    I think the Off-Site library you made was wonderful. It took only a few clicks to determine how it was laid out, and the colors and layout are not only easy on the eyes but are also attractive. Thank you for putting your time and effort into it. You made it easy for me to look over the other contestants' entries.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression Evaluator Contest
    By Stack Overflow in forum Contests Board
    Replies: 20
    Last Post: 03-29-2005, 09:34 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21