Obfuscate C tricks

J

J.L.Cooper

I have been looking at the winning entries for the International Obfuscated
C Contest (Shame I missed it till it had closed, well next time I will get
an entry in) and I was wondering if anyone has any good suggestions for
tricks that can be used to Obfuscate C code. *

Some trick that I like to use include the following.

a^=a;
instead of
a=0;

5;
instead of
b[5];

and of course
a=(!(a^a))?1:0;
instead of
if (a) {
a=0;
} else {
a=1;
}

* Before people take great delight in flaming me for posting this I would
like to say that I am not actively stating that C should ever be coded like
this except when entering an Obfuscated competition.
 
R

Robert Harris

J.L.Cooper said:
I have been looking at the winning entries for the International Obfuscated
C Contest (Shame I missed it till it had closed, well next time I will get
an entry in) and I was wondering if anyone has any good suggestions for
tricks that can be used to Obfuscate C code. *

Some trick that I like to use include the following.

a^=a;
instead of
a=0;

5;
instead of
b[5];

and of course
a=(!(a^a))?1:0;

Surely (a^a) is always 0 so a becomes 1
 
J

Jason Cooper

Sorry should have been
a=(!(a^b))?1:0;
instead of
if (a==b) {
a=0;
} else {
a=1;
}[/QUOTE]
 
C

Christian Staudenmayer

Sorry should have been
a=(!(a^b))?1:0;
instead of
if (a==b) {
a=0;
} else {
a=1;
}

That would do the same as a = (!(a^b)) ? 0 : 1; not a = (!(a^b)) ? 1 : 0;

Greetings, Chris.
 
P

pete

J.L.Cooper said:
I have been looking at the winning entries for the International Obfuscated
C Contest (Shame I missed it till it had closed, well next time I will get
an entry in) and I was wondering if anyone has any good suggestions for
tricks that can be used to Obfuscate C code. *

Some trick that I like to use include the following.

a^=a;
instead of
a=0;

a = '-'-'-';
 
C

Christian Staudenmayer

There was an entry by David Korn in 1987 that used the fact that on most
(all?) Unix platforms you automatically have:

#define unix 1

even if you didn't include anything. I think he combined it with the
fact that a = i[a] which resulted in something roughly in the form of

unix["unix"]

in order to yield 'n'.

Greetings, Chris
 
O

Old Wolf

J.L.Cooper said:
Opps. You are correct the one and zero should be swapped round.

Both of these are more obfuscated than:
a = (a == b);

PS. When posting in comp.lang.c please write your comments
AFTER the ones you're replying to.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

J.L.Cooper said:
I have been looking at the winning entries for the International Obfuscated
C Contest (Shame I missed it till it had closed, well next time I will get
an entry in) and I was wondering if anyone has any good suggestions for
tricks that can be used to Obfuscate C code. *

Some trick that I like to use include the following.

a^=a;
instead of
a=0;

5;
instead of
b[5];

and of course
a=(!(a^a))?1:0;
instead of
if (a) {
a=0;
} else {
a=1;
}

Of course the preprocessor is fun as well.
#define ZAP(x) (x = 0)
#define EQ ==
#define NEQ !=
#define when if
#define end }
#define test
#define val_a a=
#define then {
#define unless(x) if(!x)

when(a)
ZAP(a);

if(x EQ 40) then
test val_a 20;
end

More fun is also to encode strings, and do hell of a job
decoding it.



#define a ;
#define a_o_ exit
#define _ 100
#define _n_ putchar
#define a_o(_l,l_)_l##l_
#define _a_(_l,l_)a_o(l_,_l)
#define ___ _n_(_-68)
int o__[_-93]={_-27,_+8,_+18,1+_,_+21,_+17,_-67} a int o=_-_ a
_a_(a_o(i,n),a_o(m,a))(__o,O,__){if(++__==_-26||__--==_+2)(___) a
if(__==_+8||__==_+((O=26)-5)) a_o((_n_(_+11)),a
if)((o)>7)a_o_(_n_(o+O-24))a _a_(a_o(i,n),a_o(m,a))
(__,O,_n_(a_o(o,__)[o++]))a}
 
M

Mark F. Haigh

J.L.Cooper said:
I have been looking at the winning entries for the International Obfuscated
C Contest (Shame I missed it till it had closed, well next time I will get
an entry in) and I was wondering if anyone has any good suggestions for
tricks that can be used to Obfuscate C code. *
<snip>

#include <stdio.h>

int main(void)
{
int a, b, c;
a = b = c = 0;

a = 42; /*
b = 43; * Perform calculation
c = 44; */

printf("a = %d, b = %d, c = %d\n",
a, b, c);

return 0;
}

$ gcc -O2 -Wall -ansi -pedantic -o tricky tricky.c
$ ./tricky
a = 42, b = 0, c = 0


Credit for this is due to the author of the "How to Write
Unmaintainable Code" document floating around.


Mark F. Haigh
(e-mail address removed)
 
K

Kenny McCormack

<snip>

#include <stdio.h>

int main(void)
{
int a, b, c;
a = b = c = 0;

a = 42; /*
b = 43; * Perform calculation
c = 44; */

printf("a = %d, b = %d, c = %d\n",
a, b, c);

return 0;
}

$ gcc -O2 -Wall -ansi -pedantic -o tricky tricky.c
$ ./tricky
a = 42, b = 0, c = 0

Oh, very nice. Very tricky, indeed!
Credit for this is due to the author of the "How to Write
Unmaintainable Code" document floating around.

Is that subtitled:
"And keep yourself employed: Job skills for programmers"
?

It sure works around here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top