C Contest Code

Pages: 1234
Thought anybody would be interested in the extreme complexity and elegance(exact opposite of elegance actually) of a small bit of code, can produce something as amazing as this:

This C code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

main(t,_,a)
char *a;
{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}



Outputs This!:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
On the first day of Christmas my true love gave to me
a partridge in a pear tree.

On the second day of Christmas my true love gave to me
two turtle doves
and a partridge in a pear tree.

On the third day of Christmas my true love gave to me
three french hens, two turtle doves
and a partridge in a pear tree.

On the fourth day of Christmas my true love gave to me
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the fifth day of Christmas my true love gave to me
five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the sixth day of Christmas my true love gave to me
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the seventh day of Christmas my true love gave to me
seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the eighth day of Christmas my true love gave to me
eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the ninth day of Christmas my true love gave to me
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the tenth day of Christmas my true love gave to me
ten lords a-leaping,
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the eleventh day of Christmas my true love gave to me
eleven pipers piping, ten lords a-leaping,
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the twelfth day of Christmas my true love gave to me
twelve drummers drumming, eleven pipers piping, ten lords a-leaping,
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.


How is it possible??? I don't believe this - Can anybody confirm this?

This piece of code actually one an international Obfuscated C Code Contest http://reality.sgi.com/csp/iocc
Last edited on
I wouldn't call that elegance.
ok elegant was the wrong word; but amazing nonetheless. I'll go with ingenious.

Is this code accomplished using regex?
Last edited on
For anybody interested, here is a Microsoft research document explaining the reverse engineering of the "Twelve days of Christmas" C poem:

http://research.microsoft.com/en-us/um/people/tball/papers/xmasgift/

Apparently the author of this code hid the meaning and structure behind this code, by removing white spaces, using conditional and list expressions instead of if else blocks, and encoding the poems strings.

I'm still confused?
Last edited on
closed account (S6k9GNh0)
http://codepad.org/uuGL6FAm
1
2
3
main(t,_,a)
char *a;
{


That's old style C parameter lists. I haven't seen this since the UMoria source.

Is that even legal in modern compilers?

EDIT: also this kind of thing isn't very difficult to do. You just write a program normally, then obfuscate it by removing whitespace, using the ?: operator instead of if/else, and replace variable names with single letters.
Last edited on
closed account (S6k9GNh0)
Disch, if it's so easy, why not make an example?
Open up gmail and view the Javascript source.

@Disch

main(t,_,a)
char *a;
{


That's old style C parameter lists. I haven't seen this since the UMoria source.
Is that even legal in modern compilers?


If do not take into account the omitted return type then this declaration does not satisfy the current C Standard. Of course main can have three parameters. The third parameter for example can specify environment values. Such a declaration is implementation defined. But the problem is that all identifiers in the identifier list shall be declared. According to paragraph #6 of section 6.9.1 Function definitions:

6 If the declarator includes an identifier list, each declaration in the declaration list shall have at least one declarator, those declarators shall declare only identifiers from the identifier list, and every identifier in the identifier list shall be declared.
Last edited on
@Disch

main(t,_,a)
char *a;
{


That's old style C parameter lists. I haven't seen this since the UMoria source.
Is that even legal in modern compilers?


I dealt much with such a code when I took part in the IBM project of the IBM NFS server/client.

There was even more interesting code. For example there was

memcpy( ( char * ) p, ( char * )q, n );

because when the code was being written there was no such a type as void in C.:)

there was no such a type as void in C

I think there was void for "no return type" but there wasn't a void pointer.
As far as I know there was no void in whole. It seems it is Stroustrup that suggested to use the word void.
Last edited on
You're right, there was no void, it defaulted to int. I don't know about your second statement, though, because void was in C before C++ existed.
http://www.stroustrup.com/sibling_rivalry.pdf

has tangential information on the origin of the void keyword and its (later) use for void pointers.
Computerquip, here is an example of the type of obfuscation Disch was talking about:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#define sstr substr
#define instr find
#include <iostream>
#include <string>
void print(std::string);std::string l(std::string);std::string strw(int);int get(char);
int main(int argc, char *argv[]){for(int i=1; i<argc; i++) print(argv[i]);return 0;}
void print(std::string s){std::string t=s, str="", a=strw(0),b;while(t[0]=='0')t.erase(0,1);
while(t.instr(",")!=std::string::npos) t.erase(t.instr(","),1);int d=t.instr("."); 
bool r=(t[0]=='$'), j=((d!=(int)std::string::npos)?true:false); if(r) t=t.sstr(1,t.size()-1);
d=t.instr(".");a=t.sstr(0,((int)t.size()-1<d)?t.size():d);if(d!=(int)std::string::npos) 
b=(t.sstr(d+1)+"0").sstr(0,2);int k=20;for(int i=a.size()-1; i>1; i=i-3){if(a.sstr(i-2,3)!="000") 
str=l(a.sstr(i-2,3))+strw(247)+strw(get((char)(k+48)))+strw(247)+str;k=k+(k<=23?1:0);}
d=(a.size()%3);if(d!=0) str=l(a.sstr(0,d))+strw(247)+strw(get(k+48))+strw(247)+str;if(str.empty()) 
str=strw(get((char)48))+(char)32;if(r) str+=strw(145);while(str.instr(strw('{'*2))!=std::string::npos) 
str.erase(str.instr(strw('{'*2)),1);if(str==strw('h'+'1')) str=str.sstr(0,str.size()-1);if(j){
if(b!="0"&&b!="00") str+=strw('A'+'d')+l(b)+(r?(strw('d'+'G')):"");else if(r) str+=strw('Y'*2);
else str+=strw(165)+strw(0);if(str.instr(strw('a'*2))!=std::string::npos&&str.instr(strw('f'*2))
==std::string::npos) str=str.sstr(0,str.size()-1);}if(str.sstr(str.size()-1,1).compare(strw(247))==0) 
str=str.sstr(0,str.size()-1);while(str.instr(strw('{'*2))!=std::string::npos)str.erase(str.instr(strw('{'*2)),1);
std::cout << (char)toupper(str[0]) << str.sstr(1,str.size()-1) << ".\n";}std::string l(std::string s){
std::string str="00"+s;char a=str[str.size()-1], b=str[str.size()-2], c=str[str.size()-3];
str=std::string(((a-48!=0)?strw(get(a)):""));if(b-48==1){if(((int)a-48)<6) str=strw(get(a+10));
else str=strw(get(a))+strw(219+(((a-48)%8)?0:1));}else if(b-48!= 0){if(((int)b-48)<6) 
str=strw(get(b+14))+std::string(((a-38)%10)?(strw(224)+str):"");else{if(((int)b-48)!=8) 
str=strw(get(b))+strw(230)+std::string(((a-38)%10)?(strw(224)+str):"");else 
str=strw(get(b))+std::string(((a-38)%10)?((strw(236)+strw(224))+str):strw(236));}}
if(c-48!=0) str=strw(get(c))+strw(247)+strw(238)+strw(247)+str;if(str=="")return strw(0);
else return str;}
std::string strw(int n) {
     int nn['S'+'T'+'R'] = { 122,101,114,111,123,111,110,101,123,116,  
     119,111,123,116,104,114,101,101,123,102,111,117,114,123,102,105,
     118,101,123,115,105,      120,123,115,      101,118,101,110,123,
     101,105,103,104,116,      123,110,105,      110,101,123,116,101,
     110,123,101,108,                                101,118,101,110,
     123,116,119,101,                                108,118,101,123,
     116,104,105,114,                                116,101,101,110,
     123,102,111,117,114,      116,101,101,      110,123,102,105,102,
     116,101,101,110,123,      116,119,101,      110,116,121,123,116,
     104,105,114,116,121,      123,102,111,      114,116,121,123,102,
     105,102,116,121,123,      116,104,111,      117,115,197,110,100,
     123,109,105,108,108,      105,111,110,      123,198,105,108,108,
     105,111,110,123,100,      111,108,108,      197,114,115,123,111,
     110,101,132,100,                                111,108,108,197,
     114,115,123,132,                                197,110,100,132,
     123,132,199,101,                                110,116,115,123,
     132,197,110,100,132,      122,101,114,      111,132,199,101,110,
     116,115,123,111,110,      101,132,199,      101,110,116,115,123,
     145,111,110,101,132,199,101,110,116,115,123,101,101,110,123,116,
     101,101,110,123,145,123,116,121,145,123,116,121,123,121,145,123,
     121,123,104,117,110,100,114,101,100,123,132,132,123 };
     if(nn[n]!='N'*2-'!') return (char)((nn[n]>='{')?nn[n]-'d':nn[n])+strw(n+1);
     else     return "";
}
int get(char a) {
     int n=('n'-'V');
     int nn[24] = { 0,5,9,13,19,24,29,33,39,45,50,54,61,68,
                    77,86,94,101,108,114,119,120,129,137 };     
     return nn[a-n*2];
}
computerquip wrote:
Disch, if it's so easy, why not make an example?


I don't normally get goated so easily... and my normal response would be "because it's a huge waste of time."

But I felt like wasting some time.

So here. A small tribute to Jamiroquai:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <Windows.h>
#include <cstdio>
#define _p(_)  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),_)
#define q GetAsyncKeyState(VK_RETURN)
#define v(_) Sleep(_)
int main(){int b[] = 
{24,36,66,66,126,66,66,66,124,66,66,124,66,66,66,124,28,34,64,64,64,64,34,28,120,68,66,66,66,66,68,
120,126,64,64,124,64,64,64,126,126,64,64,124,64,64,64,64,28,34,64,64,78,66,34,28,66,66,66,126,66,66,
66,66,124,16,16,16,16,16,16,124,62,4,4,4,4,36,36,56,66,68,72,112,72,68,66,66,64,64,64,64,64,64,64,126,
108,-110,-110,-110,-126,-126,-126,-126,66,98,82,82,74,74,70,66,60,66,66,66,66,66,66,60,124,66,66,124,
64,64,64,64,60,66,66,66,66,74,68,58,60,66,66,66,124,68,66,66,60,66,64,60,2,2,66,60,124,16,16,16,16,
16,16,16,66,66,66,66,66,66,66,60,66,66,68,68,72,72,80,32,-126,-126,-126,-126,84,84,84,40,66,36,36,
24,24,36,36,66,68,68,68,40,16,16,16,16,126,2,4,8,16,32,64,126};
auto* r = "55555555555LZ@5555^[ZB5555A]\\F555WZZR\\P5555\\F555555SZG5555GPTY55555555555[ZA]\\[R"
"55YPSA55555SZG55555XP555555AZ555555QZ555555W@A5555QT[VP555QT[VP555555555555RZA555VT[[PQ555]PTA"
"55555\\[555555XL55555]PPYF55AZ[\\R]A55WTWL55";COORD z;char _[512];int __=-1,t=1,a[2+2*3];7[a]=t*-2;
while(!q){if((a[7]<-1&&__<0)||(a[7]>=13*5&&__>0)){
for(int ii=0;ii<0x200;++ii){auto __=[&](){return(ii%64)>>3;};_[ii]=(r[t*8+__()] == 41+12)?15+17:
b[((r[t*8+__()]^0x15)-(116^53))*8+(ii>>6)]&(1<<(7^ii&7))?(32+(t!=17)*3):(35-(t!=17)*3);}
t = (t+1) % 27;__=-__;v(300);for(int i=0;i<8;++i)a[i]=__>0?-i:64+i;}for(z.Y=0;
z.Y<8;++z.Y){z.X=a[z.Y]-__;if(z.X>=0&&z.X<2*(17+-3*-5)){_p(z),putchar(_[z.Y*64+z.X]);
}z.X=a[z.Y];if(z.X>=0&&z.X<64){_p(z),putchar(42);}}v(5);for(int i=0;i<8;++i)a[i]+=__;}}



Note: uses C++11 auto and lambdas. Make sure your compiler supports it.


Regarding the output. It animates. You'll have to run it to see.

Also: press Enter to exit the program. Otherwise it'll go forever.

EDIT: removed some questionable behavior
Last edited on
crashing....

and doesn't work for me
Last edited on
Strange. I just updated and removed some things I was doing that were questionable. Try again?
s C++\cplusplus stuff\main.cpp||In function 'int main()':|
s C++\cplusplus stuff\main.cpp|14|warning: 'auto' will change meaning in C++0x; please remove it|
s C++\cplusplus stuff\main.cpp|14|error: ISO C++ forbids declaration of 'r' with no type|
s C++\cplusplus stuff\main.cpp|16|error: cannot convert 'const char*' to 'int*' in initialization|
s C++\cplusplus stuff\main.cpp|18|warning: 'auto' will change meaning in C++0x; please remove it|
s C++\cplusplus stuff\main.cpp|18|error: ISO C++ forbids declaration of '__' with no type|
s C++\cplusplus stuff\main.cpp|18|error: expected primary-expression before '[' token|
s C++\cplusplus stuff\main.cpp|18|error: expected primary-expression before ']' token|
s C++\cplusplus stuff\main.cpp|18|error: expected ',' or ';' before '{' token|
s C++\cplusplus stuff\main.cpp|18|error: '__' cannot be used as a function|
s C++\cplusplus stuff\main.cpp|19|error: '__' cannot be used as a function|
s C++\cplusplus stuff\main.cpp|19|warning: suggest parentheses around arithmetic in operand of '^'|
||=== Build finished: 8 errors, 3 warnings ===|
that's not crashing. That's failing to compile.

Also, see my original post:

Note: uses C++11 auto and lambdas. Make sure your compiler supports it.


Your compiler apparently does not support C++11 or you are not enabling it. Update your compiler and/or enable C++11 support.
Pages: 1234