Skip to content

Instantly share code, notes, and snippets.

@Rhomboid
Last active April 29, 2024 21:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rhomboid/e5998eb392ad285878bb to your computer and use it in GitHub Desktop.
Save Rhomboid/e5998eb392ad285878bb to your computer and use it in GitHub Desktop.
Obfuscated C code (Twelve days of Christmas)
#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);}
#include <stdio.h>
int main(int t, int _, 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;# ){nl]!/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);
}
#include <stdio.h>
int main(int t, int _, char *a)
{
if(t > 1) {
if(t < 3) {
main(-79, -13, a + main(-87, 1 - _, main(-86, 0, a + 1) + a));
}
if(t < _) {
main(t + 1, _, a);
}
if(main(-94, -27 + t, a) && t == 2) {
if(_ < 13) {
return main(2, _ + 1, "%s %d %d\n");
} else {
return 9;
}
} else {
return 16;
}
} else {
if(t < 0) {
if(t < -72) {
return 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;# ){nl]!/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' ')# }'+}##(!!/");
} else {
if(t < -50) {
if(_ == *a) {
return putchar(a[31]);
} else {
return main(-65, _, a + 1);
}
} else {
return main((*a == '/') + t, _, a + 1);
}
}
} else {
if(t > 0) {
return main(2, 2, "%s");
} else {
if(*a != '/') {
return main(0, main(-61, *a, "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"), a + 1);
} else {
return 1;
}
}
}
}
}
#include <stdio.h>
int main(int t, int u, char *a)
{
#ifdef DEBUG
if(t != 1 && t != -65 && t != -61) printf("main(%d, %d, \"%s\")\n", t, u, a);
#endif
if(t < -72) {
// swap t and u and set 'a' to the big blob
// u will be a number of sections to skip before beginning to print
// skipping is handled by the -25 < t <= 0 block (below) and printing by and t == 0 block
return main(u, 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;# ){nl]!/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' ')# }'+}##(!!/");
} else if(t < -50) {
// if we're here, we've been asked to lookup and print a character in the lookup table (t == -61)
// print it if found, otherwise shift one over and recurse (t == -65)
if(u == *a) {
#ifdef DEBUG
printf("putchar('%c')\n", a[31]);
return a[31];
#else
return putchar(a[31]);
#endif
} else {
return main(-65, u, a + 1);
}
} else if(t < 0) {
// -25 <= t < 0: skip over abs(t) many '/'-delimited sections in the main blob
return main((*a == '/') + t, u, a + 1);
} else if(t == 0) {
// t == 0: print the character at *a using a lookup table and then advance one char
// stop when '/' reached
if(*a != '/') {
// load the lookup table, find *a in it, and print the corresponding character 31 to the right
// Example: *a == '@' prints O, *a == 'n' prints n, *a == '\'' prints space
return main(0, main(-61, *a, "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"), a + 1);
} else {
return 1;
}
} else if(t == 1) {
// actualy entry point of the program (argc == 1); only ever called once; replace argv and envp
return main(2, 2, "%s"); // third arg is irrelevant decoy
} else if(t > 1) {
if(t == 2) {
// print "On the <u - 1>th day of Christmas "
// here any t value less than -72 is sufficient, the exact values are irrelevant
// the middle parameters specify how many /-delimited sections to skip over in the long blob before printing
main(-79, -13, a + main(-87, 1 - u, main(-86, 0, a + 1) + a));
// ^ "On the "
// ^^^^^ "first", "second", "third", etc.
// ^^^ " day of Christmas my true love gave to me\n"
}
if(t < u) {
// wind up t until equal to u (== day + 1)
// as the call stack unwinds, this recursion causes t to count down from u to 2 each time, printing the presents in reverse order
main(t + 1, u, a);
}
if(main(-94, -27 + t, a) && t == 2) { // -27 + t corresponds to the text for the present for day (t - 1)
// at the end of each day, either progress on to the next day, or quit if that was the last day
if(u < 13) {
return main(2, u + 1, "%s %d %d\n"); // third arg is irrelevant, just a decoy
} else {
return 9;
}
} else {
return 16;
}
}
}
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.
@dmyersturnbull
Copy link

These no longer compile! :(

gcc xmas.c
xmas.c:3:5: error: second parameter of 'main' (argument array) must be of type 'char **'
int main(int t, int u, char *a)
    ^
xmas.c:3:5: error: third parameter of 'main' (environment) must be of type 'char **'
2 errors generated.

@stevenjoezhang
Copy link

gcc on macOS is actually clang, I tried a real gcc 11 compiler and it's working!

@hedylogos
Copy link

Compiles & runs with Fabrice Bellard's amazing Tiny C Compiler v0.9.27.

On Debian-based distros: apt install tcc

Others & Windows visit https://bellard.org/tcc/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment