Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Code explanation

Name: Anonymous 2012-01-11 12:49

Can somebody explain why this code outputs what it does?

// tested with Core 2 Duo, Core 2 Quad and Xeon
// tested with gcc4.1.2 gcc4.4.3 and gcc4.6.1
// compile with: gcc -O0 -m32
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>

jmp_buf p;
void (*q)();

const char *data =
    "\x8b\x44\x24\x04\x8b\x5c\x24\x08"
    "\x8b\x00\x8b\x1b\x31\xc3\x31\xd8"
    "\x31\xc3\x8b\x4c\x24\x04\x89\x01"
    "\x8b\x4c\x24\x08\x89\x19\xc3\x90"
    "\x55\x89\xe5\x8b\x45\x04\xc9\xc3"
    "\x55\x90\x90\x89\xe5\x90\x90\x90"
    "\x8b\x45\x08\x89\x45\x04\xc9\xc3"
    "\x60\x09\x0e\x13\x14\x01\x0c\x0c"
    "\xc0\x07\x05\x0e\x14\x0f\x0f\x60"
    "\x00\x67\x6f\x74\x6f\x20\x63\x6f"
    "\x6e\x73\x69\x64\x65\x72\x65\x64"
    "\x20\x68\x61\x72\x6d\x66\x75\x6c"
    "\x6c\x00\x90\x90\x1c\x1b\x0a\x20";

int f(int x)
{
    static int b = 0; static int s = 0;
    int a = 0, t;
    if (!s) {
        a = b; b = x;
    } else {
        a = x; t = b;
        do {
            a ^= b;
            b = (a^b) & b;
            b <<= 1;
        } while (b);
        b = t;
    }
    s = (s+1) % 2;
    return a;
}

int g(int i, int *j)
{
    *j = i;
    i = (int) putchar;
    if (*j == (48 << 1)) 
        __asm volatile (
                "movl 8(%ebp),%eax;"
                "leave;"
                "ret"
                );
    return (int) puts;
}

void h(int i)
{
    int b;
    q = (void(*)()) g(i++[data],&b);
    for (f(b);*(data+i)!=b;++i,f(b))
        q(f(i[data])%0xff);
}

void sh(int s)
{
    if (s == 010)
        ((void(*)())g(s,&s))("F");
    longjmp(p,s);
}

int main(void)
{
    int base, addr = 0xffffffff, offs = 16;
    int a = 11, b = 32, i = 25;
    int s = 8, t = 1, u = 4;
    ((void(*)()) data)(&a,&b);
    ((void(*)()) data)(&b,&t);
    ((void(*)()) data)(&t,&s);
    addr ^= a;
       a ^= addr;
    addr ^= a;
    base = ((int(*)())data+addr)();
    if (a == -1)
        goto over;
    puts("A");

    base = (1<<3) | ((f(addr) + f(offs)) & ~0xff);
    h(base+addr+offs);
    exit(0);

over:
    signal(t,sh);signal(s,sh);signal(u,sh);

    if (!(s = setjmp(p))) {
        q = (void(*)()) g(0x30, &a);
        q(data + a + i);
        s = a / (b-1);
        puts("B");
    } else if (s == 0xb) {
        puts("C");
        ((int(*)(int)) data+addr+(offs/2))(base);
    } else {
        puts("D");
        *((int*) base+s) = 0xffffffff;
    }
   
    puts("E");
    return 1;
}

Name: Anonymous 2012-01-11 15:51

``On my machine'' that data string disassembles to this.
   10af3:       46 00 8b        divf2 $0x0 [f-float],(r11)+
   10af6:       44 24 04        mulf2 $0x24 [f-float],$0x4 [f-float]
   10af9:       8b 5c 24 08     bicb3 ap,$0x24,$0x8
   10afd:       8b 00 8b 1b     bicb3 $0x0,(r11)+,$0x1b
   10b01:       31 c3 31        brw 13cc7 <__EH_FRAME_BEGIN__+0x3167>
   10b04:       d8 31 c3 8b     adwc $0x31,0x4c8b(r3)
   10b08:       4c
   10b09:       24 04 89 01     cvtpt $0x4,(r9)+,$0x1,(r11)+,$0x24[ap]
   10b0d:       8b 4c 24
   10b10:       08 89 19 c3     cvtps (r9)+,$0x19,0x5590(r3),(r9)+
   10b14:       90 55 89
   10b17:       e5 8b 45 04     bbcc (r11)+,$0x4[r5],10ae5 <_fini+0x49>
   10b1b:       c9
   10b1c:       c3 55 90 90     subl3 r5,@(r0)+,@(r0)+
   10b20:       89 e5 90 90     bisb3 0x8b909090(r5),$0x8[r5],(r9)+
   10b24:       90 8b 45 08
   10b28:       89
   10b29:       45 04 c9 c3     mulf3 $0x4 [f-float],0x60c3(r9),$0x9 [f-float]
   10b2d:       60 09
   10b2f:       0e 13 14        insque $0x13,$0x14
   10b32:       01              nop
   10b33:       0c 0c c0 07     prober $0xc,0x507(r0),$0xe
   10b37:       05 0e
   10b39:       14 0f           bgtr 10b4a <_fini+0xae>
   10b3b:       0f 60 00        remque (r0),$0x0
   10b3e:       67 6f 74 6f     divd3 (pc),-(r4),(pc)
   10b42:       20 63 6f 6e     addp4 (r3),(pc),(sp),-(r3)
   10b46:       73
   10b47:       69 64 65        cvtdw (r4),(r5)
   10b4a:       72 65 64        mnegd (r5),(r4)
   10b4d:       20 68 61 72     addp4 (r8),(r1),-(r2),(fp)
   10b51:       6d
   10b52:       66 75 6c        divd2 -(r5),(ap)
   10b55:       6c 00 90        cvtbd $0x0,@(r0)+
   10b58:       90 1c 1b        movb $0x1c,$0x1b
   10b5b:       index $0x20,$0x0,Address 0x00010b5e is out of bounds.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List