obfuscated code

C

c.lang.myself

I just came across following obfuscated code which prints map of a
asian country India......
Can any body help me in understanding what is happening in this
program.......
***********************************
#include<stdio.h>
main()
{
int a,b,c;
int count = 1;
for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\
TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
Hq!WFs XDt!" [b+++21]; )
for(; a-- > 64 ; )
putchar ( ++c=='Z' ? c = c/ 9:33^b&1);
return 0;
}
 
B

Ben Pfaff

William Pursell said:
for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\
TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
Hq!WFs XDt!" [b+++21]; )

At
first, I thought you'd made a typo
when I saw the 'XDt!" [b+++1]', but
it's just an instance of:

2[g]

where g is an array.

It is? I think that "...Xdt!" [b+++21], in fact, places the
array and the index in the conventional order, not in the
reversed order that you mention.
 
N

Nate Eldredge

I just came across following obfuscated code which prints map of a
asian country India......
Can any body help me in understanding what is happening in this
program.......
***********************************
#include<stdio.h>
main()
{
int a,b,c;
int count = 1;
for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\
TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
Hq!WFs XDt!" [b+++21]; )
for(; a-- > 64 ; )
putchar ( ++c=='Z' ? c = c/ 9:33^b&1);
return 0;
}

I think I've got it.

(spoiler space, scroll way down)














Further...













Further...


















Further...



























Still further...





















Further...












Further...





















Just a little further...
























Nearly there...




















Okay, here we are.

First, let's clean it up by replacing some of the character constants
with integers, and simplify the argument of putchar(). Note that `33^b&1'
simplifies as `(b & 1) ? 32 : 33', which in ASCII translates to

(b & 1) ? ' ' : '!'

Also, note the inner `for' loop just repeats a-64 times.

int main(void) {
int a,b,c;
const char str[] = "- FIGURE ...";
int a;
int b=10, c=10;
while ((a = str[b + 21]) != '\0') {
int i;
for (i = 0; i < a - 64; i++) {
c++;
if (c == 90) {
c = 10;
putchar('\n'); /* '\n' == 10 */
} else {
if (b & 1)
putchar(' ');
else
putchar('!');
}
}
b++;
}
return 0;
}

So `a' is set in sequence to the integer values of the characters from
`arr', starting with number 31. The first 31 characters
"- FIGURE?, UMKC,XYZHello Folks," have no effect; we could just as
easily omit them and change the indexing appropriately. On each
iteration b is incremented.

The inner loop prints a-64 characters. Depending on whether b is even
or odd (which doesn't change during the inner loop), most of the
characters are either ' ' or '!'. The variable c ensures that every
80th character output is '\n', so we get 80 column lines.

The upshot is that we print alternating runs of spaces and of '!', where
the characters in the string specify how long the runs are. The lines
are wrapped at 80 columns to produce a 2-D image.

So the string is just a (strangely) RLE encoded image, and the purpose
of the code is to decode and display the image.

(If by some bizarre eventuality this is homework, I ask that you credit
me in your submitted assignment. I also ask that you tell your
instructor from me that he or she has a perverse mind.)
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top