Help with a A Perverse Standard C Program

J

Jerret Johnson

A challenged co-worker of mine challenged me to come up with a perverse
example of a conforming Standard C program. This is by no means a gold
medal winner in a C obfuscation contest, but nevertheless it
demonstrates some arguably serious abuse of the preprocessor. If you can
make it even more perverse, I'd appreciate it.

And for those of you who might ask what my C question is ... is this
Standard C?

#include <ctype.h>
#include <stdio.h>
#include <string.h>

#define UNSIGNED unsigned
#define CHAR char
#define INT int
#define MAIN main
#define VOID void
#define RETURN return
#define STATIC static
#define BEGIN {
#define END }
#define IF if(
#define THEN ){
#define ELSE }else{
#define ELSEIF }else if(
#define ENDIF }
#define FOR for(
#define LOOP ){
#define ENDLOOP }
#define IS ==
/*lint -save -e723 Suspicious use of = */
/*(OK since I know what I am doing)*/
#define ASSIGN =
/*lint -restore*/
#define LT <
#define GTE >=
#define BUMP +=
#define CUT -=

#define PRINTF printf
#define PUTS puts
#define FFLUSH fflush
#define GETS gets
#define STDOUT stdout
#define STRLEN strlen
#define TOUPPER toupper
#define SIZE_T size_t

STATIC CHAR *MAKE_UPPER(CHAR *S);

INT MAIN(VOID)
BEGIN
INT I;
CHAR NAME[4096];

PRINTF("ENTER YOUR NAME: ");
FFLUSH(STDOUT);
PRINTF("HI %s!\n", MAKE_UPPER(GETS(NAME)));/*lint !e421 Caution -- */
/*function */
/*'gets(char *)'*/
/*is considered*/
/*dangerous*/
/*(OK, since no one*/
/*has a name greater*/
/*in length than 4095*/
/*characters).*/
PUTS("LET'S TEST A FOR LOOP WITH BUMP");
FOR I ASSIGN 0; I LT 6; I BUMP 1
LOOP
IF I IS 4
THEN
PRINTF("I IS 4\n");
ELSEIF I IS 5
THEN
PRINTF("I IS 5\n");
ELSE
PRINTF("I IS %d\n", I);
ENDIF
ENDLOOP

PUTS("LET'S TEST A FOR LOOP WITH CUT");
FOR I ASSIGN 5; I GTE 0; I CUT 1
LOOP
IF I IS 4
THEN
PRINTF("I IS 4\n");
ELSEIF I IS 5
THEN
PRINTF("I IS 5\n");
ELSE
PRINTF("I IS %d\n", I);
ENDIF
ENDLOOP

PRINTF("BYE %s!\n", MAKE_UPPER(NAME));

RETURN 0;
END

STATIC CHAR *MAKE_UPPER(CHAR *S)
BEGIN
SIZE_T I;
SIZE_T LEN;

IF S IS NULL
THEN
RETURN NULL;
ENDIF
LEN = STRLEN(S);
FOR I ASSIGN 0; I LT LEN; I BUMP 1
LOOP
S = (CHAR)TOUPPER((UNSIGNED CHAR)(S));
ENDLOOP
RETURN S;
END

Thanks
 
J

jmcgill

Jerret said:
And for those of you who might ask what my C question is ... is this
Standard C?

Yes. Perfectly legal ANSI C. GCC gives a warning about using gets().

This one is my all-time favorite.

#define BeginProgram void main(int argc, char *argv[])
#define CloseBrace }
#define CommandLineArgument -1
#define Declare int i,j,n,Flag=1;
#define EndOfProgram return;
#define False 0;
#define ForLoop ;for
#define GetCommandLineArgument n=atoi(argv[1]);
#define i F1ag
#define If if
#define Increment ++
#define Is ==
#define LessThan *(c&64)*
#define LessThanOrEqualTo !=
#define Modulo %
#define OpenBrace {
#define PossibleFactor j
#define PossiblePrime i
#define Possib1ePrime (c=getchar())
#define PrimeNumber (c^(!i*n%64));
#define Print putchar
#define SetTo =
#define SmallestPrime 2
#define True 1
#define Variables char c;
#define Zero i%j

BeginProgram
OpenBrace
Declare Variables
GetCommandLineArgument

ForLoop (PossiblePrime SetTo SmallestPrime ;
Possib1ePrime LessThanOrEqualTo CommandLineArgument ;
Increment PossiblePrime)
OpenBrace
F1ag SetTo True
ForLoop (PossibleFactor SetTo SmallestPrime ;
PossibleFactor LessThan PossiblePrime ;
Increment PossibleFactor)
If (PossiblePrime Modulo PossibleFactor Is Zero)
F1ag SetTo False

If (Flag Is True)
Print PrimeNumber
CloseBrace

EndOfProgram
CloseBrace
 
R

Roland Pibinger

A challenged co-worker of mine challenged me to come up with a perverse
example of a conforming Standard C program. This is by no means a gold
medal winner in a C obfuscation contest, but nevertheless it
demonstrates some arguably serious abuse of the preprocessor.

Fortunately you can also use the proprocessor to effortlessly
reconstruct the C program from your 'perversity'.

Best wishes,
Roland Pibinger
 
R

Richard Heathfield

Jerret Johnson said:

And for those of you who might ask what my C question is ... is this
Standard C?

PRINTF("HI %s!\n", MAKE_UPPER(GETS(NAME)));/*lint !e421 Caution -- */
/*function */
/*'gets(char *)'*/
/*is considered*/
/*dangerous*/
/*(OK, since no one*/
/*has a name greater*/
/*in length than 4095*/
/*characters).*/

Not okay, because it can be attacked by a malicious user (cf Internet Worm).
It also fails to take into account the possibility that gets() returns
NULL.

S = (CHAR)TOUPPER((UNSIGNED CHAR)(S));


The first cast is unnecessary. The second is sensible.
 
R

Rod Pemberton

Richard Heathfield said:
Jerret Johnson said:



Not okay, because it can be attacked by a malicious user (cf Internet Worm).
It also fails to take into account the possibility that gets() returns
NULL.

?

Then why does MAKE_UPPER begin with this?

IF S IS NULL
THEN
RETURN NULL;
ENDIF

It appears to me that it takes the possibility into account, and then passes
the NULL along to printf()...

;)


Rod Pemberton
 
R

Rod Pemberton

Jerret Johnson said:
A challenged co-worker of mine challenged me to come up with a perverse
example of a conforming Standard C program.

Why? (whiny)...
This is by no means a gold
medal winner in a C obfuscation contest, but nevertheless it
demonstrates some arguably serious abuse of the preprocessor. If you can
make it even more perverse, I'd appreciate it.

Someone a while back tried to turn C into Pascal or BASIC using the
preprocessor. So, there are two directions you could go. It might look
really interesting if you chose BASIC and managed to add line numbers...
You could then hide all the defines in a local include for your prank.



Rod Pemberton
 
A

Ancient_Hacker

Jerret said:
A challenged co-worker of mine challenged me to come up with a perverse
example of a conforming Standard C program.

Actually I've done something similar to "help" C code look a little
less like a mangy porcupine in a blender.

#define If if(
#define Then ) {
#define Else } else {
#define Equals ==
#define Is ==
#define Isnt !=
#define Procedure void
#define Function
#define NoArgs ( void )
#define For(var,lo,hi) for(var=(lo);var <=(hi);var++)
#define var {
#define begin {
#define end }
#define code
#define EndFor }


so you can write the arguably prettier and clearer:

Procedure CrapOnMem( char Buf[], int Len )
var
int i;
code
For( i, 0, Len - 1 )
If i Isnt 22 Then
Buf = '%';
EndIf
EndFor
end


Of course this causes most C aficionados to raise an eyebrow, or sneer,
or feint barfing.
 
J

jmcgill

Richard said:
jmcgill said:
This one is my all-time favorite.

#define BeginProgram void main(int argc, char *argv[])

So much for Standard C.

Fair enough, but I still get a kick out of this program. It does
something really interesting, and not at all obvious.
 
R

Richard Heathfield

Rod Pemberton said:

!
Then why does MAKE_UPPER begin with this?

IF S IS NULL
THEN
RETURN NULL;
ENDIF

It appears to me that it takes the possibility into account, and then
passes the NULL along to printf()...

I was talking about the whole statement. Passing NULL to printf as a match
for %s invokes undefined behaviour.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top