Page 1 of 2

obfuscated C code

Posted: 10 Jun 2011, 10:48
by HaWe
hi,
I just found this code but I'm curious how it exactly works...
I mean: I certainly know what it finally calculates, but what is the readable code like?

actually this line

Code: Select all

#define _ F-->00||F-OO--;
is confusing me... :?

Code: Select all

    // original obfuscated code modified for dev C++

    #include <stdio.h>
    #define _ F-->00||F-OO--;

    int F=00,OO=00;
    void F_OO()
    {
                 _-_-_-_
            _-_-_-_-_-_-_-_-_
         _-_-_-_-_-_-_-_-_-_-_-_
       _-_-_-_-_-_-_-_-_-_-_-_-_-_
      _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
      _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
     _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
     _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
     _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
     _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
      _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
      _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
       _-_-_-_-_-_-_-_-_-_-_-_-_-_
         _-_-_-_-_-_-_-_-_-_-_-_
             _-_-_-_-_-_-_-_
                 _-_-_-_
    }
    main()
    {F_OO();printf("%1.3f\n",4.*-F/OO/OO);}


Re: obfuscated C code

Posted: 10 Jun 2011, 16:21
by hergipotter
Well i'm sure you know what a #define does?

The character "_" is replaced with the expression F-->00||F-OO--; or a little more understandable (F--) > 0 || F - (OO--);

First, F is decremented, then it is checked if it's positive. If yes, then the second statement is not processed because of the OR operator. If not, then OO is decremented and is substracted from F. I have to admit that i don't really know what the substraction is for...

Then this gets subracted a lot of times in the function F_OO

At least thats what i think it does...

edit: So this is supposed to compute PI? But only the first two decimals are correct :D If you put "printf("%1.5f\n",4.*-F/OO/OO);" it gives 3.14062

Re: obfuscated C code

Posted: 10 Jun 2011, 16:45
by HaWe
hi,
thx for your reply. Surely I know what #define does.
And yes, it's calculating PI from the area of it's own code-implicated circle area.
I find it really funny...^^

But I still don't quite understand

Code: Select all

(F--) > 0 || F - (OO--);
it's an OR expression, ok, but why will not all expressions be evaluated? I mean: it's no nested if-then-else...
BTW the forum's code font is really bad as you can't distinguish 0 (zero) from OO (double-capital-o)...

From operator precedence || (OR) is less than (), --, -, >
so both sides from OR have to be evaluated as a whole, why should the program skip the left or the right one?

Code: Select all

((F--) > 0)
may be true (F>0) or false (F=0) and

Code: Select all

(F - (OO--))
may be true (≠0) or false (=0)
if one of both expression is true, the whole expression is true. If both are false, all is false. So far, so good.
But there is no "if this expression is true do something, else don't" -

ps:
I think I understand: both expressions left and right of the "OR" are evaluated and if left side is TRUE then F is decremented and/or if right side is TRUE then OO is decremented (side effects of postfix -- I guess ?).

hmmm.
but now how's the calculation working itself, what is

Code: Select all

4.*-F/OO/OO
?

Re: obfuscated C code

Posted: 10 Jun 2011, 23:48
by muntoo
Let's try to make this a bit easier to read:

Code: Select all

#include <stdio.h>

int F = 0;
int OO = 0;

main()
{
    F-- > 0 || F - OO--;
    repeat(3)
        -(F--) > 0 || F - OO--;

    F-- > 0 || F - OO--;
    repeat(7)
        -(F--) > 0 || F - OO--;

    F-- > 0 || F - OO--;
    repeat(11)
        -(F--) > 0 || F - OO--;

    F-- > 0 || F - OO--;
    repeat(13)
        -(F--) > 0 || F - OO--;

    repeat(2)
    {
        F-- > 0 || F - OO--;
        repeat(14)
            -(F--) > 0 || F - OO--;
    }

    repeat(4)
    {
        F-- > 0 || F - OO--;
        repeat(15)
            -(F--) > 0 || F - OO--;
    }

    repeat(2)
    {
        F-- > 0 || F - OO--;
        repeat(14)
            -(F--) > 0 || F - OO--;
    }

    F-- > 0 || F - OO--;
    repeat(13)
        -(F--) > 0 || F - OO--;

    F-- > 0 || F - OO--;
    repeat(11)
        -(F--) > 0 || F - OO--;

    F-- > 0 || F - OO--;
    repeat(7)
        -(F--) > 0 || F - OO--;

    F-- > 0 || F - OO--;
    repeat(3)
        -(F--) > 0 || F - OO--;

    printf("%1.3f\n", 4.0 * -F / OO / OO);
}
Which becomes:

Code: Select all

#include <stdio.h>

int F = 0;
int OO = 0;

void F_OO(size_t i=1)
{
    ((F--) > 0)   ||   (F - (OO--));

    for(size_t i2 = 0; i2 < i; ++i2)
        (-(F--) > 0)   ||   (F - (OO--));
}

main()
{
    F_OO(3);
    F_OO(7);
    F_OO(11);
    F_OO(13);
    F_OO(14); F_OO(14);
    F_OO(15); F_OO(15); F_OO(15); F_OO(15);
    F_OO(14); F_OO(14);
    F_OO(13);
    F_OO(11);
    F_OO(7);
    F_OO(3);

    printf("%1.3f\n", 4.0 * -F / OO / OO);
}
----

Random, half-hearted attempt at pattern recognition:

Code: Select all

#include <stdio.h>

int F = 0;
int OO = 0;

void F_OO(size_t i=1)
{
    F-- > 0 || F - OO--;
    for(size_t i2 = 0; i2 < i; ++i2)
        -(F--) > 0 || F - OO--;
}

main()
{
    for(size_t i = 3; i <= 11; i += 4)
        F_OO(i);

    F_OO(13);

    for(size_t i = 14; i <= 15; ++i)
    {
        F_OO(i);
        F_OO(i);
    }

    for(size_t i = 15; i >= 14; --i)
    {
        F_OO(i);
        F_OO(i);
    }

    F_OO(13);

    for(size_t i = 11; i >= 3; i -= 4)
        F_OO(i);

    printf("%1.3f\n", 4.0 * -F / OO / OO);
}
Random ideas:
  • Code: Select all

    PI = 4 * atan(1)
    Therefore, -F / OO / OO is [approximately] equivalent to atan(1).
  • Also, I'm not sure, but won't the right side of the || operator not be evaluated, if the left side is true? (There's no further need to evaluate the right side, if the first one is already true.)
And, yes, I skipped reading most of your post. :)

Re: obfuscated C code

Posted: 11 Jun 2011, 09:11
by hergipotter
muntoo wrote: Also, I'm not sure, but won't the right side of the || operator not be evaluated, if the left side is true? (There's no further need to evaluate the right side, if the first one is already true.)
yes, that's it. At least it's true for some other languages, i'm not 100% sure for C...

Re: obfuscated C code

Posted: 11 Jun 2011, 09:17
by HaWe
nice, though there were some compiler errors with dec C++ and your code.
"do" is a reserved keyword.
I changed some variable names additionally and this is how your code compiles:

Code: Select all

     #include <stdio.h>

    int F = 0;
    int G = 0;

    void calc(size_t i=1)
    {
        ((F--) > 0)   ||   (F - (G--));

        for(size_t j = 0; j < i; ++j)
            (-(F--) > 0)   ||   (F - (G--));
    }

    main()
    {
                       calc(3);
                 calc(7);   calc(11);
            calc(13);  calc(14); calc(14);
        calc(15); calc(15); calc(15); calc(15);
            calc(14); calc(14); calc(13); 
               calc(11);  calc(7);
                       calc(3);

        printf("%1.3f\n", 4.0 * -F / G / G);
    }

it shows 3.125 as a result.
what's it all about with this
-F / G / G
...?
and what type is size_t? int?

your 2nd code freezes though...
hergipotter wrote:
muntoo wrote: Also, I'm not sure, but won't the right side of the || operator not be evaluated, if the left side is true? (There's no further need to evaluate the right side, if the first one is already true.)
yes, that's it. At least it's true for some other languages, i'm not 100% sure for C...
I won't bet, maybe it's a compiler optimization option....

Re: obfuscated C code

Posted: 11 Jun 2011, 13:56
by hergipotter

Code: Select all

4.0 * -F / G / G)
The negative value of F is multiplied with 4 and then divided by G two times.

Re: obfuscated C code

Posted: 11 Jun 2011, 14:44
by HaWe
yes, thx, THAT part was clear ;)
no, what I meant was:
what has 4.0 * -F / G / G to do with 4*atan(1) or anything else about circle areas?

Re: obfuscated C code

Posted: 11 Jun 2011, 18:02
by muntoo
doc-helmut wrote:yes, thx, THAT part was clear ;)
:lol:
doc-helmut wrote:what has 4.0 * -F / G / G to do with 4*atan(1) or anything else about circle areas?
I think all those calcs could be some kind of series that converges to atan(1). I'll look at simplifying the code more (if possible).
doc-helmut wrote:and what type is size_t? int?
size_t is system dependent - usually analogous to unsigned int or u32, though. It's the proper way to implement any loops.

----

P.S. I knew there was something suspicious about the do I was using, but I couldn't figure out what. (I've only used do-while only about three times in my life. :))

Re: obfuscated C code

Posted: 11 Jun 2011, 19:30
by HaWe
any idea why your code generates 3.125 and the obfuscater's to 3.141?
(surely only any a
-_
somewhere more or less^^)

about F and G:
// result=3.125
// F=-200
// G=-16

maybe it's finding only any a quotient F/G² which is close to 0,785398 because 0,785398*4 = 3.14159 ~ PI
BTW I don't get that code into NXC style...

Code: Select all

         #include <stdio.h>

        int F = 0;
        int G = 0;

        void calc(size_t i=1)
        {
            ((F--) > 0)   ||   (F - (G--));

            for(size_t j = 0; j < i; ++j)
                (-(F--) > 0)   ||   (F - (G--));
        }

        main()
        {
                           calc(3);
                     calc(7);   calc(11);
                calc(13);  calc(14); calc(14);
            calc(15); calc(15); calc(15); calc(15);
                calc(14); calc(14); calc(13);
                   calc(11);  calc(7);
                           calc(3);

            printf("result=%1.3f\nF=%d\nG=%d", 4.0 * -F / G / G, F, G);

            // result=3.125
            // F=-200
            // G=-16
}
and what's the advantage to use size_t instead of simply (and explicitely) int or char or long?