Obfuscated c using #include

T

Till Crueger

Hi,
I know you shouldn't do this, but I just thought of a really nasty way to
obfuscate c code. I am however not sure if this is correct c at all. What
I did was this:

file obfuscate.c:
#include <stdio.h>

int main(int argc, char* argv[]){
#include "obfuscate.h"
}

file obfuscate.h:
printf("hello world\n");

I also tried some variations on this scheme, and my compiler accepted all
of them. But does it have to, or is this not valid c?

Till
 
E

E. Robert Tisdale

Till said:
I know you shouldn't do this,
but I just thought of a really nasty way to obfuscate C code.
I am however not sure if this is correct C at all.
What I did was this:

//file obfuscate.c:
#include <stdio.h>

int main(int argc, char* argv[]){
#include "obfuscate.h" return 0;
}

//file obfuscate.h:
printf("hello world\n");

I also tried some variations on this scheme,
and my compiler accepted all of them.
But does it have to?
Yes.

Or is this not valid C?

It *is* valid C.

Note that obfuscate.h is *not* a header file
because it in not included at the *head*
of your obfuscate.c source file.

It is perfectly OK to include code like this
but you should probably use a file extension
different from ".h" to avoid confusing other programmers
who may need to read, understand and maintain your code.
 
A

Alex Monjushko

Till Crueger said:
Hi,
I know you shouldn't do this, but I just thought of a really nasty way to
obfuscate c code. I am however not sure if this is correct c at all. What
I did was this:
file obfuscate.c:
#include <stdio.h>
int main(int argc, char* argv[]){
#include "obfuscate.h"
}
file obfuscate.h:
printf("hello world\n");
I also tried some variations on this scheme, and my compiler accepted all
of them. But does it have to, or is this not valid c?


It is legal and sometimes useful to import source code from
another file. Traditionally, the source include file should still
have a '.c' extension.

i.e.

#ifdef IPV6
#include "ipv6code.c"
#else
#include "ipv4code.c"
#endif
 
A

Arthur J. O'Dwyer

file obfuscate.c:
#include <stdio.h>

int main(int argc, char* argv[]){
#include "obfuscate.h"
}

file obfuscate.h:
printf("hello world\n");

I also tried some variations on this scheme, and my compiler accepted all
of them. But does it have to, or is this not valid c?

It has to. #include just includes some source text; it doesn't
care what it is. As others have pointed out, this is sometimes
even useful.
What is *not* generally useful, but *is* obfuscated a little bit,
is when a file #includes itself one or more times:

file obfuscate.c:
#ifdef FOO
puts("Hello world!");
#undef FOO
#define FOO return 0;}
#else
#include <stdio.h>
#define FOO {
int main(void)
FOO
#include "obfuscate.c"
FOO
#endif

-Arthur,
corrupting
 
C

Case -

Till said:
I know you shouldn't do this, but I just thought of a really nasty way to

Yes, you should do it! Be bold and push C to its limits;
figure out why things are the way they are. This is IMO
what could make the difference between being/becomming a
regular programmer and an expert.
obfuscate c code. I am however not sure if this is correct c at all. What
I did was this:

file obfuscate.c:
#include <stdio.h>

int main(int argc, char* argv[]){
#include "obfuscate.h"
}

file obfuscate.h:
printf("hello world\n");

I also tried some variations on this scheme, and my compiler accepted all
of them. But does it have to, or is this not valid c?

See the C preprocessor as a simple text-processor with a
limited set of #-operations. The #include simply and only
replaces itself by the literal contents of the named
file. Many compilers allow you to see the code that is
actually (i.e., after all text #-operations have been
replaced by code) using a command line option (some compiler
have -E). During preprocessing there is no notion of C
grammar/syntax. (So, you could (mis)use the C preprocessor
for other purposes.)

<OT>
An obfuscated program that does nice in this context is:

#include </dev/tty> /* /dev/tty is the 'command tool' device */

Which at least works on UNIX machines. When running the
compiler on this program, the compiler connects its input
to the command tool from which you just invoked the compiler.
So, you can start typing the actual program. When done, you
can close the input using ^D, and of the compiler goes finishing
it's job.
</OT>

Good luck!

Kees
 
?

=?iso-8859-1?Q?Jos=E9_Mar=EDa?= Mateos

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
It is perfectly OK to include code like this
but you should probably use a file extension
different from ".h" to avoid confusing other programmers
who may need to read, understand and maintain your code.

I thought obfuscating code was made with the purpose of
confusing the next code maintainer.

Regards.

- --
My real e-mail address: chema (AT) chema.homelinux.org
http://EuropeSwPatentFree.hispalinux.es - EuropeSwPatentFree
I don't read HTML posts / No leo mensajes en HTML
Blog Overflow: http://chema.homelinux.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAwNkw9P6GbSlI+hkRAjXuAJ40jnSiF6U5Y19JEQ7tGINAvOGpygCgpn4X
EEtQymUGWtRFXLPa0ozCszs=
=xQ6D
-----END PGP SIGNATURE-----
 
R

RoSsIaCrIiLoIA

file obfuscate.c:
#include <stdio.h>

int main(int argc, char* argv[]){
#include "obfuscate.h"
}

file obfuscate.h:
printf("hello world\n");

I also tried some variations on this scheme, and my compiler accepted all
of them. But does it have to, or is this not valid c?

It has to. #include just includes some source text; it doesn't
care what it is. As others have pointed out, this is sometimes
even useful.
What is *not* generally useful, but *is* obfuscated a little bit,
is when a file #includes itself one or more times:
/* file obfuscate.c: */
#ifdef FOO
puts("Hello world!");
#undef FOO
#define FOO return 0;}
#else
#include <stdio.h>
#define FOO {
int main(void)
FOO
#include "obfuscate.c"
FOO
#endif
 

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,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top