PDA

View Full Version : Obfuscated challenge


Dru Lee Parsec
05-31-2002, 01:08 PM
OK, Even though I still have to finish two of these challenges myself, I just came up with a fun idea for a new challenge.

Print The Alphabet

Sounds easy right?

HOWEVER, Do it in any language you want AND do it in the most obfuscated way you can come up with. [Obfuscated = incomprehensibly confusing]

Dead code is not allowed [Dead Code = code that is never actually executed during the program]

I want to see some code that when you look at it you think WTF is THAT!? But when you run it you get the alphabet as the output.

Now you may be asking yourself "What is the point of writing bad code?" The point is to find some new aspects of your language and to stretch your imagination.

Have fun!

GnuVince
05-31-2002, 01:29 PM
print string.join(map(lambda x: chr(x), range(ord('a'), ord('z')+1)), "")

GnuVince
05-31-2002, 01:43 PM
let _ =
let char_list = Array.make 26 '_' in
for i = 0x7a downto 0x61 do
char_list.(i - 97) <- (Char.chr i);
done;
let alph = Buffer.create (Array.length char_list) in
Array.iter (fun c -> Buffer.add_char alph c) char_list;
Printf.printf "%s\n" (Buffer.contents alph)

Strike
05-31-2002, 01:53 PM
Any rules on preprocessor usage?

GnuVince: wanna label those with languages? (though I know what they are, others might not)

For others: www.iocccc.org for several good C examples

Dru Lee Parsec
05-31-2002, 01:55 PM
I hope 63 lines of code isn't too big

import java.util.*;
public class StateNames {
public static int NUMBER_OF_STATES = 50;
public static void main(String[] args) {
int q = calculateStateNumber();
String[] states = new String[q];
createNames(states,q);
}
private static int calculateStateNumber(){
int foo = NUMBER_OF_STATES/2;
if((NUMBER_OF_STATES%2) == 0){foo++;}return foo;
}
private static void createNames(String[] states, int stateNum){
int fearFactor=NUMBER_OF_STATES/10*3 + NUMBER_OF_STATES;
Random California = new Random();
SortedVector NewYork = new SortedVector();
for (int countyName = fearFactor + (NUMBER_OF_STATES/2+1);countyName >= fearFactor;countyName-- ) {
NewYork.addElement((char)(countyName) + " ");
}NewYork.sort();
for (int stateCapitals = 0; stateCapitals<stateNum; stateCapitals++ ) {
System.out.print((String)NewYork.elementAt(stateCapitals));
}System.out.println(" ");
}
}
class SortedVector extends java.util.Vector {
public void sort() {
if (size() == 0) {return;}
Object[] a = this.toArray();
quickSort(a, 0, a.length - 1);
this.removeAllElements();
for (int i = 0; i < a.length ; i++ ) {this.addElement(a[i]);}
}
private void quickSort(Object a[], int lo0, int hi0)
{
int lo = lo0;int hi = hi0;String mid;
if ( hi0 > lo0)
{
mid = a[ ( lo0 + hi0 ) / 2 ].toString();
while( lo <= hi){
while( ( lo < hi0 ) && ( a[lo].toString().compareTo(mid) < 0 ) ) {
++lo;}
while( ( hi > lo0 ) && ( a[hi].toString().compareTo(mid) > 0 ) ) {--hi;}
if( lo <= hi )
{swap(a, lo, hi);
++lo;
--hi;}
}
if( lo0 < hi )quickSort( a, lo0, hi );
if( lo < hi0 )quickSort( a, lo, hi0 );
}
}
private void swap(Object a[], int i, int j)
{
if (i == j)return;
Object T;T = a[i];
a[i] = a[j];a[j] = T;}
public Object[] toArray() {
Object[] temp = new Object[this.size()];
for (int i = 0; i < this.size() ; i++ ) {
temp[i] = this.elementAt(i);
}
return(temp);
}
}


Output is

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z


Notice that not once in the code is there a single "A", "Z" or the number "26".

PrBacterio
05-31-2002, 02:49 PM
Obfuscated code is fun! :]

const L=__LINE__;
const L_=__LINE__;
#define X_ ){
#define Y printf(
#define Z main
Z(X_ int Y)
#define const ;const
const S=__LINE__*__LINE__
#define Y_ ;int i;for(i=L
#define I i++
#define X Y"%c ",I+S)X_
#define Z i<=L__;
const L__=L_*__LINE__
Y_;Z X}return(L-L);}

Strike
05-31-2002, 02:57 PM
Sans preprocessor abuse:

main(){int i;for(i=97;i<123;++i)putchar(i);putchar(0x0a);}

Strike
05-31-2002, 03:16 PM
With preprocessor abuse:

#define Obfuscation main
#define using ()
#define the {
#define preprocessor int
#define is i;
#define too for
#define easy (i=97;i<123;++i)
#define but putchar
#define here (i);
#define you but
#define have (0x0a)
#define it ;}

Obfuscation using the preprocessor is too easy but here you have it

Bradmont
05-31-2002, 03:27 PM
Here's another in C


/* Note: This will only work on little-endian machines */
void p(int * i){
int j;
for (j=0; (*i & 1<<j); j++ ) *i ^= 1<<j;
*i |= 1<<j;
}

main(){
int i=0;
while((i ^ (1<<4|1<<3|1<<1))) i |= 1<<6, p(&i), write(1, &i, 1), i ^= 1<<6;
write(1,"\n",1);
}

Strike
05-31-2002, 03:41 PM
And, if you consider the fact that ASM is pretty obfuscated as is:

global main
extern putchar

[section .text]

main:
push ebp
mov ebp, esp
mov esi, 97
alphaloop:
cmp esi, 123
je end
push esi
call putchar
inc esi
add esp, 4
jmp alphaloop
end:
mov eax, 10
push eax
call putchar
add esp, 4
mov eax, 0
mov esp, ebp
pop ebp
ret

Strike
05-31-2002, 03:48 PM
And my hand-written assembly produces an executable that is ... get this ... 5 whopping bytes smaller than the C one above (they do exactly the same thing)

Dru Lee Parsec
05-31-2002, 04:22 PM
I liked the idea of making code that looked like it was counting the state names in the USA and yet it had nothing to do with the variable names at all.

I was thinking of writing code that:

1. had an array of size 26.
2. Then I'd fill it with random letters.
3. Then sort the letters
4. Remove the duplicates
5. Add more random letters
6. repeat steps 3, 4, and 5 until there are no duplicates
7. print the array.

That would be sick and twisted code. Totally unoptimized and completly confusing to try to figure out.

BTW, all I was doing in all that Java code was filling a 26 spot array with the letters from Z to A. Then I sorted them and printed them out. Not too obfuscated from a logic point of view, but still difficult to read with the state names and such.

That's where I'd like to see this competition go. Not only is the code obfuscated, but the logic of how you print the letters should be really unique.

Have Fun :goodjob:


***************

AAACCK! I just Busted myself!

Since I switched from the sick and twisted random letter generation to the easier letter creation I no longer needed a random generator. Therefore, this line

Random California = new Random();

which is left over from the first idea IS DEAD CODE!

I have to disqualify my own code fro breaking my own rule.

inkedmn
05-31-2002, 04:44 PM
that makes it easier for us pythonistas...

we're kinda limited as far as obfuscating on accounta the whitespace thang.

i think i'll give it a go now :)

Bradmont
05-31-2002, 04:51 PM
Originally posted by Dru Lee Parsec
That's where I'd like to see this competition go. Not only is the code obfuscated, but the logic of how you print the letters should be really unique.

Have Fun :goodjob:

What, you weren't impressed with my home-made increment routine using bitwise operators and bit shifts (and had a ++ in there anyway :p)?

GnuVince
05-31-2002, 05:04 PM
I felt WAAAAAAAAAAAAAAAAAAAY lame: http://darkhost.mine.nu:81/~vince/ocaml/alph.ml

Strike
05-31-2002, 05:59 PM
<trumpets blare>
top this:

main() { unsigned int c[] = {0x00457e31, 0x01e8FA3e, 0x00f8420f, 0x01e8c63e,
0x01f87e1f, 0x01f87210, 0x00f85e2f, 0x0118fe31, 0x01f2109f, 0x0010862e, 0x01197251, 0x0108421f,
0x011dd631, 0x011cd671, 0x00e8c62e, 0x01e8fa10, 0x00e8cde1, 0x01c97251, 0x00f8383e, 0x01f21084,
0x0118c62e, 0x0118c544, 0x0118d771, 0x01151151, 0x01151084, 0x01f1111f}; int i,j;
for (i=0; i<26; ++i) { for (j=25; j>=0; --j) { if (!(1<<j & c[i])) putchar(0x20); else
putchar(0x23); if (!(j%5)) putchar(0xa);} putchar(0xa); }}

Strike
05-31-2002, 06:00 PM
Actually, I recommend you all run it to see the unique output as well :)

file13
05-31-2002, 06:03 PM
(setf(symbol-function'x)#'(lambda()(labels((x(y)(cond((= 123 y)(quit)))(format
t"~a "(int-char y))(x (+ 1 y))))(x 97))))(x)


runs on clisp & ecls....

;)

kmj
05-31-2002, 06:05 PM
now, there's a contender

Bradmont
05-31-2002, 06:07 PM
Strike: nice. :)

GnuVince
05-31-2002, 06:12 PM
Fucked up mind.

Strike
05-31-2002, 06:22 PM
Originally posted by kmj
now, there's a contender which one?

GnuVince
05-31-2002, 06:38 PM
Strike makes me hot ;) Do you have a document that explains how bitwise operators work? If not, fire vim and write one!

Bradmont
05-31-2002, 06:40 PM
Originally posted by GnuVince
Strike makes me hot ;) Do you have a document that explains how bitwise operators work? If not, fire vim and write one!
http://bradmont.net/~bradmont/artOfAssembly.pdf

file13
05-31-2002, 06:42 PM
let's make it worse! ;)


(setf(symbol-function'x)#'(lambda()(labels((x(y)(cond((= 123 y)
(quit)))(format t"~a "(int-char y))(x (+ 1 y))))(x 97))))(setf
(symbol-function'y)#'(lambda()(setf(get'y'z)#'(lambda()
(x)))(labels((x()(funcall(get'y'z))(x)))(x))))(y)



output:

[file13@anatta tmp]$ clisp oabc.lsp
a b c d e f g h i j k l m n o p q r s t u v w x y z
[file13@anatta tmp]$

:D

GnuVince
05-31-2002, 06:56 PM
In Perl: feel the power of obfuscation!


print "abcdefghijklmnopqrstuvwxyz\n";


Can you get any more obfuscated that Perl? I don't think so

Bradmont
05-31-2002, 07:11 PM
Originally posted by GnuVince
In Perl: feel the power of obfuscation!


print "abcdefghijklmnopqrstuvwxyz\n";


Can you get any more obfuscated that Perl? I don't think so I nominate this implementation for the grand prize.

Dru Lee Parsec
05-31-2002, 08:13 PM
Bradmont:

No, I like the bitwise stuff. I had to sit here fora long time thinking WTF is he doing here? (which is kind of the whole point) :)

Oh BTW, my left over random statement may or may not be dead code. I mean, it was compiled and it did run that line, it just didn't contribute anything. True dead code is code that is never executed. Hmm, is this dead code or just is it just laying on the floor bleeding?

Dru Lee Parsec
05-31-2002, 08:14 PM
File 13, that is SICK!

GnuVince
05-31-2002, 08:55 PM
I made a new version in O'Caml which is now much more obfuscated (I was thinking of COBOL maybe?)

It's attached.

file13
05-31-2002, 09:10 PM
Originally posted by Dru Lee Parsec
File 13, that is SICK!

:D

ok last one:


(setf(symbol-function'z)#'(lambda(x)(format
t "~a "(int-char x))))(setf(symbol-function
'x)#'(lambda()(labels((x(y)(cond((= 123 y)
(quit)))(funcall 'z y)(x(+ 1 y))))(x 97)))
)(setf(symbol-function'y)#'(lambda()(setf
(get'y'z)#'(lambda()(x)))(labels((x()(funcall
(get'y'z))(x)))(x))))(y)


C:\Code\Lisp>clisp oabc.lsp
a b c d e f g h i j k l m n o p q r s t u v w x y z

off to a-kon!

Bradmont
06-01-2002, 01:49 AM
mine again, with changes (both to style and method)

/* Note: This will only work on little-endian machines */
#include <pthread.h>

int _=0; void p (int*i){int j;for (j=0;
(* i&1<< j);j++ )* i^=1
<<j;*i|= 1<<j;} void*t
( void * foo) { int i
=_;if (i ^(1<<4 |1 <<3
|1 <<1)) p(&_),i |= 1<<
6,p(&i), write(1 ,&i,1)
,pthread_create( (pthread_t *) malloc
(sizeof( pthread_t )),NULL
,t, NULL ); else write(
129>>7, "\n",1), pthread_exit
(NULL);} int main (int argc
,char** argv ) { pthread_create((pthread_t
*)malloc (sizeof( pthread_t)),NULL,t,NULL);

pthread_exit(NULL);}

Bradmont
06-01-2002, 01:52 AM
that's gotta be the world's first multithreaded alphabet printer. :)

GnuVince
06-01-2002, 02:07 AM
Here's another entry. It uses the GPL, but the GPL has no 'z' in it, so you must add one in the file to get the ENTIRE alphabet.


exception Found_char of string

let _ =
let cur_char = ref 97 in
let file = open_in "gpl.txt" in
let abc = Buffer.create 26 in
while !cur_char <= 122 do
try
while true do
let car = (input_char file) in
if (Char.code car) = !cur_char then begin
Buffer.add_char abc car;
cur_char := !cur_char + 1;
raise (Found_char "gotcha")
end;
done
with
Found_char "gotcha" -> ()
| End_of_file -> Printf.printf "%s\n" (Buffer.contents abc); exit 1
done;
Printf.printf "%s\n" (Buffer.contents abc)