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

goto prevails...

Name: Anonymous 2007-09-14 18:15 ID:exfP63F/

...teh haterz phail.

int valid_ip(const char *ip_addr) {
    int rem;
    size_t dots;
    char ch, *ip_ptr;

    if (!(ip_ptr = (char *) ip_addr)) {
        return 0;
    }

    dots = 0, rem = 3;    __asm {xor ebx, ebx};
    for (; ch = *ip_ptr; ++ip_ptr) {
        __asm push ecx __asm mov ecx, ebx __asm jecxz ECX_WEAK0 __asm dec ecx
        __asm jecxz ECX_WEAK1 __asm dec ecx __asm jecxz ECX_WEAK2 __asm jmp STRONG
        ECX_WEAK0: goto WEAK0; ECX_WEAK1: goto WEAK1; ECX_WEAK2: goto WEAK2;

        STRONG:
        __asm pop ecx
        switch (ch) {
        case '0': case '1':    case '2': case '3':    case '4': case '5': case '6':    case '7': case '8':    case '9': --rem; goto DIGIT_CHECK;
        case '.': ++dots; goto DOT_CHECK;
        default: return 0;
        }

        WEAK0:
        __asm pop ecx
        switch (ch) {
        case '0': case '1':    --rem; __asm {mov ebx, 3}; goto DIGIT_CHECK;
        case '2': --rem; __asm {inc ebx}; goto DIGIT_CHECK;
        case '3': case '4': case '5': case '6': case '7': case '8': case '9': rem -= 2; __asm {mov ebx, 3}; goto DIGIT_CHECK;
        case '.': ++dots; goto DOT_CHECK;
        default: return 0;
        }

        WEAK1:
        __asm pop ecx
        switch (ch) {
        case '0': case '1':    case '2': case '3':    case '4': --rem; __asm {mov ebx, 3}; goto DIGIT_CHECK;
        case '5': --rem; __asm {inc ebx}; goto DIGIT_CHECK;
        case '6': case '7': case '8': case '9': rem = 0; __asm {mov ebx, 3}; goto DIGIT_CHECK;
        case '.': ++dots; goto DOT_CHECK;
        default: return 0;
        }

        WEAK2:
        __asm pop ecx
        switch (ch) {
        case '0': case '1':    case '2': case '3':    case '4': case '5': --rem; __asm {mov ebx, 3}; goto DIGIT_CHECK;
        case '.': ++dots; goto DOT_CHECK;
        default: return 0;
        }

        DIGIT_CHECK: if (rem < 0) {return 0;} else {continue;}
        DOT_CHECK: if (rem == 3 || dots > 3) {return 0;} else {rem = 3; __asm {xor ebx, ebx}; continue;}
    }

    return (rem != 3 && dots == 3) ? 1 : 0;
}

Name: Anonymous 2007-09-14 18:26 ID:9y/Lpfg0

It's so ugly. It looks like Haskell.

Name: Anonymous 2007-09-14 18:37 ID:QqCrS0aA

use gcc and do case '0' ... '9':, you worthless faggot

Name: Anonymous 2007-09-14 18:43 ID:exfP63F/

>>3
I don't use GCC. I would have used if if I felt like doing that. The compiler has some chance of optimizing the switches which is why they were used.

Name: Anonymous 2007-09-14 19:47 ID:pTtrFNdQ

>>2
So's your face.

Name: Anonymous 2007-09-14 19:54 ID:9y/Lpfg0

>>5
YHBT.

Name: Anonymous 2007-09-14 21:07 ID:PVk2qe+s

It's so ugly. It looks like Perl.

Name: Anonymous 2007-09-14 22:19 ID:Heaven

>>1
Best. Optimization. Ever.

Name: Anonymous 2007-09-14 23:49 ID:V1TF7lhd

Nearly shat myself thinking it was HLA

Name: Anonymous 2007-09-15 4:50 ID:HCXbvvBc

>>7

use Data::Validate::IP qw(is_ipv4);

print "Is valid IP" if is_ipv4($ip);

Name: Anonymous 2007-09-15 4:52 ID:HCXbvvBc

>>1
how many thousands of ips are you checking per second to make up for that super-optimization?

If you did it just for fun, then it's awesomely neat.

Name: Anonymous 2007-09-15 5:31 ID:Heaven

itt fools that have no idea about optimizations.
sigh.

Name: Anonymous 2007-09-15 5:43 ID:Heaven

>>12
itt butthurt lispfags who can't optimize their code this well.
sigh.

Name: Anonymous 2007-09-15 6:15 ID:Heaven

>>13
are you trolling you fucking piece of shit?
First of all, using a single `char' instead of `int' causes most compilers to produce more assembly.
He has no idea about base2, (all that case '0': ... case '9': shit is by far WORSE than a ch >= '0' && ch <= '9')
He uses another pointer (ip_ptr) where he could just use ip_addr.
And all that inline assembly makes it so unportable unmaintanable and shitty .. seriously i'm going to puke.

int valid_ipv4(const char *);
/* dont pass NULL faggot. */
int valid_ipv4(const char *ip) {

    size_t dots = 0;
    size_t tmp  = 0;
    const char charset[] = "012345";

    for(; *ip != 0; ip++) {
        if(!strchr(charset, *ip)) {
            if(*ip == '.') {
                if(!tmp || dots > 1) break;
                dots++;
                tmp = 0;
            } else break;
        } else if(tmp > 2) break;
        else tmp++;
    }

    return *ip != 0;
}

Name: Anonymous 2007-09-15 15:31 ID:AZnEOLxm

>>14
So I suppose 6, 7, 8, 9 can't appear in IP addresses. You exponentially phail.

Name: Anonymous 2007-09-15 16:50 ID:fQFnkf8W

In addition to the senseless use of x86 assembly and goto's, and the shitty indentation I would like to point you to the following:

1. Not using constant initializers for rem and dots
2. Using size_t intstead of an (unsigned) int for dots
3. Assignment inside of an if statement
4. Using "!" instead of the far more readable "!= NULL"
5. Casting a const char* to a char*
6. The use of the sometimes slow "jecxz" instead of the more natural "jz", making it worse than what most compilers would generate
7. The use of goto's that could have been omitted if the jumps just went straight their targets
8. Repeated code for the '.' and default cases and the use of gotos because the switches should be the other way around

>(all that case '0': ... case '9': shit is by far WORSE than a ch >= '0' && ch <= '9')
Any compiler worth using will output the same code for both.

Name: Anonymous 2007-09-15 17:03 ID:qPpm2sL3

Why didn't you write it entirely in assembler?

Name: Anonymous 2007-09-15 17:16 ID:PlK+8pzu

ENTERPRISE TURNKEY INTERNET PROTOCOL ADDRESS VERIFICATION SYSTEM

int valid_ipv4( const char * ip )
{
    uint8_t dc = 3;
    uint16_t f = 0xFFFF;

nloop:
    if ( *ip == '.' )
    {
        if ( !dc )
        {
            return 0;
        }

        dc--;

        if ( f != 0xFFFF )
        {
            if ( f > 0xFF )
            {
                return 0;
            }

            f = 0xFFFF;
        }

        goto nloop_cont;
    } 

    if ( *ip >= '0'
      && *ip <= '9' )
    { 
        if ( f == 0xFFFF )
        {
            f = 0;
        }
   
        f *= 10;
        f += *ip - '0';

        if ( f > 0xFF )
        {
            return 0;
        }

        goto nloop_cont;
    } 

    if ( !*ip
      && f <= 0xFF )
    { 
        goto nloop_end;
    } 

    return 0;

nloop_cont:
    ip++;
    goto nloop;

nloop_end:
    return 1;
}

Name: Anonymous 2007-09-15 18:11 ID:kz5jAM3T

>>16
I agree on everything, except:

3. Assignment inside of an if statement
This being bad is arguable.

4. Using "!" instead of the far more readable "!= NULL"
This is bullshite. Why not then clarify: a != NULL != NULL? Or a != NULL != NULL != NULL, which is even more readable.

Name: Anonymous 2007-09-15 20:15 ID:d2asGXBU

>>19
The proper, agreed-upon way to write this is !(a != NULL) == 0.

Name: Anonymous 2007-09-15 20:19 ID:ZrRYnvOP

This being bad is arguable.
No it's not. It's begging for a bug.

Why not then clarify: a != NULL != NULL? Or a != NULL != NULL != NULL, which is even more readable.
That has to be the stupidest thing I've read today on 4chan. I regret to inform you that he has a point, your ridiculous example to the contrary.

I think it's pretty obvious which of you two has written more C.

Name: Anonymous 2007-09-15 20:40 ID:fQFnkf8W

>>19
Funny how you nitpick on that while you didn't notice on the quite big mistake I made in that statement.

I meant:
4. Using "!" instead of the far more readable "== NULL"

Of course this only applies to checks on pointers.

Name: Anonymous 2007-09-15 20:54 ID:TwomJCBz

Try using the [ code ] tags next time faggot.

Name: Anonymous 2007-09-15 20:57 ID:BAIOjQWS


          ∧_∧   / ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
          ( ´∀`) < DID SOMEBODY SAYS GATO DE MESA SEPTEMBER 15 VIVA MEOOWXICO!
        /    |    \________
       /       .|     
       / "⌒ヽ |.イ |
   __ |   .ノ | || |__
  .    ノく__つ∪∪   \
   _((_________\
    ̄ ̄ヽつ ̄ ̄ ̄ ̄ ̄ ̄ | | ̄
   ___________| |
    ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| |

Name: Anonymous 2007-09-15 23:20 ID:AZnEOLxm

The ULTIMATE Optimization

int valid_ip(const char *ip_addr) {
    size_t dots;
    char ch, *ip_ptr;

    if (!(ip_ptr = (char *) ip_addr)) {
        return 0;
    }

    dots = 0; __asm {xor ebx, ebx};
    for (; ch = *ip_ptr; ++ip_ptr) {
        if (ch == '.') {
            __asm {cmp ebx, 0};
            __asm {je INVALID};
            if (dots > 3) {
                return 0;
            }

            __asm {xor ebx, ebx};
            ++dots; continue;
        }

        __asm {mov eax, ebx};
        __asm {and eax, 15};
        __asm {jz WEAK0};
        __asm {dec eax};
        __asm {jz STRONG};
        __asm {dec eax};
        __asm {jz INVALID};
        __asm {dec eax};
        __asm {jz WEAK1};
        __asm {jmp WEAK2};

        WEAK0:
        if (ch >= '3' && ch <= '9') {
            __asm {mov ebx, 21h};
            continue;
        } else if (ch == '1' || ch == '0') {
            __asm {mov ebx, 211h};
            continue;
        } else if (ch == '2') {
            __asm {mov ebx, 3h};
            continue;
        } else {
            return 0;
        }

        WEAK1:
        if (ch >= '0' && ch <= '4') {
            __asm {mov ebx, 21h};
            continue;
        } else if (ch >= '6' && ch <= '9') {
            __asm {mov ebx, 2h};
            continue;
        } else if (ch == '5') {
            __asm {mov ebx, 4h};
            continue;
        } else {
            return 0;
        }

        WEAK2:
        if (ch >= '0' && ch <= '5') {
            __asm {mov ebx, 2h};
            continue;
        } else {
            return 0;
        }

        STRONG:
        if (ch >= '0' && ch <= '9') {
            __asm {shr ebx, 4};
            continue;
        } else {
            return 0;
        }

    }

    __asm {cmp dots, 3};
    __asm {jne INVALID};
    __asm {cmp ebx, 0};
    __asm {je INVALID};

    return 1;

    INVALID:
    return 0;
}

Name: Anonymous 2007-09-16 1:05 ID:6RKOVcG/

Try using the [ code ] tags next time faggot.

Name: Anonymous 2007-09-16 1:18 ID:D5CsidDR

>>26
Try beating this optimization, nitwit.

Name: Anonymous 2007-09-16 1:26 ID:Heaven

ID:AZnEOLxm
ASIAN END-OF-LINE, XM SATELLITE RADIO

Name: Anonymous 2007-09-16 2:24 ID:D5CsidDR

>>28
Very creative. Did you think of that yourself?

Name: Anonymous 2007-09-16 4:10 ID:hkExkvvi

>>29
no i helped him

Name: Anonymous 2007-09-16 22:17 ID:D5CsidDR

>>17
It's getting closer and closer.

This one converts the string to an array as it is checking (split because 4chan is ghey).

int conv_ip4_to_array(const char *ip_addr, unsigned int *ip_array) {
    size_t count;
    size_t dots;
    unsigned int num;
    unsigned int ich;
    unsigned char ch;
    unsigned char *ip_str_ptr;
    unsigned int *ip_array_ptr;

    if (!(ip_str_ptr = (unsigned char *) ip_addr) || !(ip_array_ptr = ip_array)) {
        __asm {jmp INVALID};
    }

    count = dots = 0;
    __asm {xor ebx, ebx};
    for (num = 0; ch = *ip_str_ptr; ++ip_str_ptr) {
        if (ch == '.') {
            //DOT_CHECK:
            __asm {cmp ebx, 0};
            __asm {je INVALID};
            __asm {cmp dots, 3};
            __asm {jg INVALID};

            //DOT_BRANCH:
            __asm {mov eax, count};
            __asm {test eax, eax};
            __asm {je INVALID};
            __asm {dec eax};
            __asm {je ONE_DIGIT};
            __asm {dec eax};
            __asm {je TWO_DIGITS};
            __asm {jmp THREE_DIGITS};

            ONE_DIGIT:
            *ip_array_ptr++ = num;
            __asm {jmp DOT_CONTINUE};

            TWO_DIGITS:
            __asm {mov eax, num};
            __asm {shr eax, 8};
            __asm {mov ecx, 10};
            __asm {mul ecx};
            __asm {mov ebx, eax};
            __asm {mov eax, num};
            __asm {and eax, 255};
            __asm {add eax, ebx};
            __asm {mov num, eax};
            *ip_array_ptr++ = num;
            __asm {jmp DOT_CONTINUE};

            THREE_DIGITS:
            __asm {mov eax, num};
            __asm {shr eax, 16};
            __asm {mov ecx, 100};
            __asm {mul ecx};
            __asm {mov ebx, eax};
            __asm {mov eax, num};
            __asm {shr eax, 8};
            __asm {and eax, 255};
            __asm {mov ecx, 10};
            __asm {mul ecx};
            __asm {add ebx, eax};
            __asm {mov eax, num};
            __asm {and eax, 255};
            __asm {add eax, ebx};
            __asm {mov num, eax};
            *ip_array_ptr++ = num;
            __asm {jmp DOT_CONTINUE};

            DOT_CONTINUE:
            count = 0, ++dots, num = 0;
            __asm {xor ebx, ebx};
            continue;
        }

Name: Anonymous 2007-09-16 22:18 ID:D5CsidDR

        //DIGIT_BRANCH:
        __asm {mov eax, ebx};
        __asm {and eax, 15};
        __asm {je WEAK0};
        __asm {dec eax};
        __asm {je STRONG};
        __asm {dec eax};
        __asm {je INVALID};
        __asm {dec eax};
        __asm {je WEAK1};
        __asm {jmp WEAK2};

        WEAK0:
        if (ch >= '3' && ch <= '9') {
            ich = (unsigned int) ch;
            __asm {mov eax, ich};
            __asm {sub eax, 30h};
            __asm {mov edx, num};
            __asm {or edx, eax};
            __asm {mov num, edx};
            __asm {mov ebx, 21h};
            ++count;
            continue;
        } else if (ch == '1' || ch == '0') {
            ich = (unsigned int) ch;
            __asm {mov eax, ich};
            __asm {sub eax, 30h};
            __asm {mov edx, num};
            __asm {or edx, eax};
            __asm {mov num, edx};
            __asm {mov ebx, 211h};
            ++count;
            continue;
        } else if (ch == '2') {
            __asm {mov eax, 2};
            __asm {mov edx, num};
            __asm {or edx, eax};
            __asm {mov num, edx};
            __asm {mov ebx, 3h};
            ++count;
            continue;
        } else {
            __asm {jmp INVALID};
        }

        WEAK1:
        if (ch >= '0' && ch <= '4') {
            ich = (unsigned int) ch;
            __asm {mov eax, ich};
            __asm {sub eax, 30h};
            __asm {mov edx, num};
            __asm {shl edx, 8};
            __asm {or edx, eax};
            __asm {mov num, edx};
            __asm {mov ebx, 21h};
            ++count;
            continue;
        } else if (ch >= '6' && ch <= '9') {
            ich = (unsigned int) ch;
            __asm {mov eax, ich};
            __asm {sub eax, 30h};
            __asm {mov edx, num};
            __asm {shl edx, 8};
            __asm {or edx, eax};
            __asm {mov num, edx};
            __asm {mov ebx, 2h};
            ++count;
            continue;
        } else if (ch == '5') {
            __asm {mov eax, 5};
            __asm {mov edx, num};
            __asm {shl edx, 8};
            __asm {or edx, eax};
            __asm {mov num, edx};
            __asm {mov ebx, 4h};
            ++count;
            continue;
        } else {
            __asm {jmp INVALID};
        }

        WEAK2:
        if (ch >= '0' && ch <= '5') {
            ich = (unsigned int) ch;
            __asm {mov eax, ich};
            __asm {sub eax, 30h};
            __asm {mov edx, num};
            __asm {shl edx, 8};
            __asm {or edx, eax};
            __asm {mov num, edx};
            __asm {mov ebx, 2h};
            ++count;
            continue;
        } else {
            __asm {jmp INVALID};
        }

        STRONG:
        if (ch >= '0' && ch <= '9') {
            ich = (unsigned int) ch;
            __asm {mov eax, ich};
            __asm {sub eax, 30h};
            __asm {mov edx, num};
            __asm {shl edx, 8};
            __asm {or edx, eax};
            __asm {mov num, edx};
            __asm {shr ebx, 4};
            ++count;
            continue;
        } else {
            __asm {jmp INVALID};
        }
    }

Name: Anonymous 2007-09-16 22:19 ID:D5CsidDR

//FINAL_CHECK:
    __asm {cmp dots, 3};
    __asm {jne INVALID};
    __asm {cmp ebx, 0};
    __asm {je INVALID};

    //FINAL_BRANCH:
    __asm {mov eax, count};
    __asm {test eax, eax};
    __asm {je INVALID};
    __asm {dec eax};
    __asm {je FINAL_ONE_DIGIT};
    __asm {dec eax};
    __asm {je FINAL_TWO_DIGITS};
    __asm {jmp FINAL_THREE_DIGITS};

    FINAL_ONE_DIGIT:
    *ip_array_ptr++ = num;
    __asm {jmp VALID};

    FINAL_TWO_DIGITS:
    __asm {mov edx, num};
    __asm {mov eax, edx};
    __asm {shr eax, 8};
    __asm {mov ecx, 10};
    __asm {mul ecx};
    __asm {mov ebx, eax};
    __asm {mov eax, num};
    __asm {and eax, 255};
    __asm {add eax, ebx};
    __asm {mov num, eax};
    *ip_array_ptr++ = num;
    __asm {jmp VALID};

    FINAL_THREE_DIGITS:
    __asm {mov edx, num};
    __asm {mov eax, edx};
    __asm {shr eax, 16};
    __asm {mov ecx, 100};
    __asm {mul ecx};
    __asm {mov ebx, eax};
    __asm {mov eax, num};
    __asm {shr eax, 8};
    __asm {and eax, 255};
    __asm {mov ecx, 10};
    __asm {mul ecx};
    __asm {add ebx, eax};
    __asm {mov eax, num};
    __asm {and eax, 255};
    __asm {add eax, ebx};
    __asm {mov num, eax};
    *ip_array_ptr++ = num;
    __asm {jmp VALID};

    VALID:
    return 1;

    INVALID:
    return 0;
}

Name: Anonymous 2007-09-16 22:19 ID:D5CsidDR

F:\Dev\C++\Win32\Com\IPAuth\debug>IPAuth 255.255.255.255
It's valid (mult time: 3421, state time: 2110, array time: 2422).
Array generated by conv_ip4_to_array: {255, 255, 255, 255}.
Authorization key: A4D57348757A2C2D

Name: Anonymous 2007-09-16 22:20 ID:D5CsidDR

Above is time for 10 million calls.

Name: Anonymous 2007-09-17 0:58 ID:UjmRZLF+

Good answers and all, but what about flexibility? Extensibility? Portability? Well-defined APIs?

package org.fourchan.dis.prog.faggotry;

/**
 * IPv4Checker.java
 *
 * @author Anonymous
 */
public class IPv4Checker implements IIPChecker {
    private static final String DELIMITER_REGEXP = "\\.";
    private static final int NUM_BLOCKS = 4;
   
    /**
     * The string to be checked
     */
    private String s;
   
    /**
     * Helper class to check a number block for validity.
     */
    private class BlockChecker {
        private String block;
       
        public BlockChecker(String b) {
            this.block = b;
        }
       
        private boolean isValidBlock() {
            try {
                int n = Integer.parseInt(this.block);
                return (0 <= n && n <= 255);
            } catch (NumberFormatException e) {
                return false;
            }
        }
    }

    /**
     * Constructor
     * @param s String to be checked for IPv4 validity.
     */
    public IPv4Checker(String s) {
        this.s = s;
    }
   
    /**
     * @return <code>true</code> if and only if the String with which this
     * IPv4Checker was instantiated is a valid IPv4 IP address.
     */
    public boolean isValidIP() {
        String[] blocks = this.s.split(DELIMITER_REGEXP);
       
        if (blocks.length != NUM_BLOCKS)
            return false;
       
        for (int i = 0; i < NUM_BLOCKS; i++) {
            if (!new BlockChecker(blocks[i]).isValidBlock())
                return false;
        }
        return true;
    }
   
    public static void main(String[] args) {
        /*
         * Perform unit tests
         */
        System.out.println(new IPv4Checker("192.168.0.1").isValidIP());
        System.out.println(new IPv4Checker("255.255.255.255").isValidIP());
    }
   
}


You'll notice I haven't written IIPChecker. I'll leave that as an exercise to the reader.

Name: Anonymous 2007-09-17 2:47 ID:6JABiyR0

I'd hate to see the runtime on that. :roll:

Name: Anonymous 2007-09-17 8:03 ID:UjmRZLF+

>>37

32.437s


Above time is for 10 million calls

Name: Anonymous 2007-09-17 12:55 ID:hkZs2Ksd

--==P.R.E.G TO THE RESCUE

/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/

Name: Anonymous 2007-09-17 13:28 ID:Ya5DeVsO

>>39
Pregnancy?

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