Jim Lawless' Blog


Obfuscated C

Originally published on: Tue, 21 Apr 2009 23:51:27 +0000

Please note! If you're having difficulties compiling the C source code presented below, please see my post: Compiling C from the Command Line with Pelles C

I had written a little C program a while back for the 19th annual International Obfuscated C Coding contest.

Not only did my code not win ... it didn't even warrant an entry in the honorable mentions or anything like that. If you look at the winners and look back at my code, you'll immediately see why; the winners and the runners-up were much more creative than my effort.

Rather than get lost in obscurity, I felt that my entry needed to be published, so it has been given the honor of appearing in my first public blog post.

Everyone, please meet lawless.c ... a ROT-13 encoder/decoder that encodes/decodes one line at a time:


// Copyright (c) 2009 by Jim Lawless
#include <stdio.h>
#include <ctype.h>
 
char *z="DCFEBA";
int zzz;
void zzzzzzzzz(void) { zzz=zzz; }
typedef void (*ZZ)(void);
#define I
void zzzzzzz(void) { z-=6; }
#define r(x) isalpha(x) ? tolower(x)>'m' ? x-'\r' : x+'\r' : x
void zzzzzz(void) { if(!(zzz-10)) { z++; z++; } }
#define o(x) Z[*z-'A']();
#define t(x)
void zzzz(void) { zzz=fgetc(stdin); }
#define a(x) for(;*z;z++)
void zzzzz(void) { zzz=r(zzz); }
#define s(x)
void zzzzzzzz(void) { fputc(zzz,stdout); }
 
ZZ Z[12]={ zzzzzzz, zzzzzzzz, zzzz, zzzzzzzzz, zzzzzz, zzzzz };
 
int main() {
 
  I s(lit) t(he) s(heet) t(he) s(heet) I s(lit) a(nd)
     o(n) t(he) s(litted) s(heet) I s(it)
}
 
 

The rules for the 19th annual competition had noted that C preprocessor tricks might just as well not be used as each entry would be run through the preprocessor for expansion.

Here's what the above code would look like after preprocessing;


char *z="DCFEBA";
int zzz;
void zzzzzzzzz(void) { zzz=zzz; }
typedef void (*ZZ)(void);

void zzzzzzz(void) { z-=6; }

void zzzzzz(void) { if(!(zzz-10)) { z++; z++; } }


void zzzz(void) { zzz=fgetc((&_iob[0])); }

void zzzzz(void) { zzz=(__mb_cur_max > 1 ? _isctype(zzz,(0x0100|0x1|0x2)) : _pctype[zzz] &amp; (0x0100|0x1|0x2)) ? tolower(zzz)>'m' ? zzz-'\r' : zzz+'\r' : zzz; }

void zzzzzzzz(void) { fputc(zzz,(&_iob[1])); }
 
ZZ Z[12]={ zzzzzzz, zzzzzzzz, zzzz, zzzzzzzzz, zzzzzz, zzzzz };
 
int main() {
 
          for(;*z;z++)
     Z[*z-'A']();
}

I tried to use preprocessor abuse to get my cutesy "I slit the sheet..." lines into view ... but the core of the processing was done via indirect invocation of entries in an array of function-pointers.

It was a fun exercise that I hope to revisit with more seriousness in the future.

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Next post:E-mail cleansing


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Charging by the Byte

A Simple Associative Array Library in C

WSH2EXE part 2

A JavaScript REPL for Android Devices

Structuring my Thinking

Play MP3 Files with Python on Windows

An Interview with Brad Templeton

A Quine in Forth

An Interview with Tom Zimmer: Forth System Developer

An Interview with the Creator of the BDS C Compiler


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service