// File: brunokeymolen[P]ssdp/src/ssdpparser.cpp
// Function: SSDPParser::Parse(sockaddr*, unsigned char*, unsigned int)
int SSDPParser::Parse(struct sockaddr* sender, u8* buf, u32 len){
    int linelen;
    u8* pos = buf;
    u32 restlen = len;
    u8* newpos;
    u32 newlen;
    int colon;
    int ret = 0;

    SSDPMessage* msg;
    std::vector<SSDPMessage*>::const_iterator itmsg;


    while( (linelen = ReadLine(pos, restlen, &newpos, &newlen)) > 0 ){
        if(mType == SSDP_TYPE_UNKNOWN){
            //Parse the first line and define the type
            if( linelen==strlen(SSDP_START_NOTIFY) && memcmp(pos, SSDP_START_NOTIFY, strlen(SSDP_START_NOTIFY)) == 0){
                mType = SSDP_TYPE_NOTIFY;
            }else if( linelen==strlen(SSDP_START_MSEARCH) && memcmp(pos, SSDP_START_MSEARCH, strlen(SSDP_START_MSEARCH)) == 0){
                mType = SSDP_TYPE_MSEARCH;
            }else if( linelen==strlen(SSDP_START_HTTP) && memcmp(pos, SSDP_START_HTTP, strlen(SSDP_START_HTTP)) == 0){
                mType = SSDP_TYPE_HTTP;
            }else{
                //unknown
                goto EXIT;
            }
        }else{
            //Read the headers
            //Find the first colon, that is the end of the field name, the rest is the field value
            colon = 0;
            while(pos[colon] != ':' && colon < linelen){
                colon++;
            }

            //Add the header to our collection
            SSDP_HTTP_HEADER *thisHeader = (SSDP_HTTP_HEADER*)malloc(sizeof(SSDP_HTTP_HEADER));
            thisHeader->fieldname = pos;
            thisHeader->fieldnamelen = colon;
            thisHeader->fieldvalue = pos+colon+1;
            if (colon<linelen) {
                thisHeader->fieldvaluelen = linelen-colon-1;
            } else {
                thisHeader->fieldvaluelen = 0;
            }
            //Trim spaces
            trimspaces(&(thisHeader->fieldname), &(thisHeader->fieldnamelen));
            trimspaces(&(thisHeader->fieldvalue), &(thisHeader->fieldvaluelen));
            mHeaders.push_back(thisHeader);
        }

        pos = newpos;
        restlen = newlen;
    }

    //We have all headers + type, search the message who can process it
    for(itmsg=mMessages.begin();itmsg<mMessages.end();itmsg++){
        msg = (SSDPMessage*)*itmsg;

        if(msg->GetType() == mType){
            if(msg->CanProcess(mHeaders) == 1){
                ret = msg->Process(sender, mHeaders);
                if(ret != 1){ //0=ok,-1=error,1=not for me, continue...
                    break;
                }
            }
        }
    }

EXIT:
    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x44(%rbp)
leaq -0x58(%rbp), %rdi
callq 0xcfc0
movq -0xc8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x2c(%rbp), %edx
leaq -0x38(%rbp), %rcx
leaq -0x3c(%rbp), %r8
callq 0xcbf0
movl %eax, -0x20(%rbp)
cmpl $0x0, %eax
jle 0xca13
movq -0xc8(%rbp), %rax
cmpl $0x0, (%rax)
jne 0xc921
movslq -0x20(%rbp), %rax
cmpq $0x11, %rax
jne 0xc8ad
movq -0x28(%rbp), %rdi
leaq 0x3d0a(%rip), %rsi      # 0x10599
movl $0x11, %edx
callq 0x3120
cmpl $0x0, %eax
jne 0xc8ad
movq -0xc8(%rbp), %rax
movl $0x1, (%rax)
jmp 0xc91c
movslq -0x20(%rbp), %rax
cmpq $0x13, %rax
jne 0xc8e0
movq -0x28(%rbp), %rdi
leaq 0x3ce9(%rip), %rsi      # 0x105ab
movl $0x13, %edx
callq 0x3120
cmpl $0x0, %eax
jne 0xc8e0
movq -0xc8(%rbp), %rax
movl $0x2, (%rax)
jmp 0xc91a
movslq -0x20(%rbp), %rax
cmpq $0xf, %rax
jne 0xc913
movq -0x28(%rbp), %rdi
leaq 0x3cca(%rip), %rsi      # 0x105bf
movl $0xf, %edx
callq 0x3120
cmpl $0x0, %eax
jne 0xc913
movq -0xc8(%rbp), %rax
movl $0x3, (%rax)
jmp 0xc918
jmp 0xcbd7
jmp 0xc91a
jmp 0xc91c
jmp 0xca00
movl $0x0, -0x40(%rbp)
movq -0x28(%rbp), %rax
movslq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0x3a, %ecx
movb %al, -0xc9(%rbp)
je 0xc950
movl -0x40(%rbp), %eax
cmpl -0x20(%rbp), %eax
setl %al
movb %al, -0xc9(%rbp)
movb -0xc9(%rbp), %al
testb $0x1, %al
jne 0xc95c
jmp 0xc967
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xc928
movl $0x28, %edi
callq 0x33b0
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, (%rax)
movl -0x40(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rcx
movslq -0x40(%rbp), %rax
addq %rax, %rcx
addq $0x1, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x40(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0xc9bb
movl -0x20(%rbp), %ecx
subl -0x40(%rbp), %ecx
subl $0x1, %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0xc9c6
movq -0x60(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x60(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x8, %rsi
callq 0x3b50
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
movq -0x60(%rbp), %rsi
addq $0x18, %rsi
callq 0x3b50
movq -0xc8(%rbp), %rdi
addq $0x8, %rdi
leaq -0x60(%rbp), %rsi
callq 0xaa70
movq -0x38(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0xc843
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
callq 0xce80
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0xcfe0
movq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
callq 0xcef0
movq %rax, -0x78(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xd020
testb $0x1, %al
jne 0xca66
jmp 0xcbd5
leaq -0x58(%rbp), %rdi
callq 0xd060
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq -0xc8(%rbp), %rcx
cmpl (%rcx), %eax
jne 0xcbbc
movq -0xc8(%rbp), %rsi
movq -0x50(%rbp), %rax
movq %rax, -0xe0(%rbp)
addq $0x8, %rsi
leaq -0x90(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0xa860
movq -0xe0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
movb %al, -0xca(%rbp)
jmp 0xcad7
movb -0xca(%rbp), %al
movzbl %al, %eax
movl %eax, -0xe4(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xa800
movl -0xe4(%rbp), %eax
cmpl $0x1, %eax
jne 0xcbba
movq -0xc8(%rbp), %rsi
movq -0x50(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xf8(%rbp)
addq $0x8, %rsi
leaq -0xb8(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0xa860
movq -0x100(%rbp), %rdi
movq -0xf8(%rbp), %rsi
movq -0xf0(%rbp), %rdx
movq (%rdi), %rax
movq 0x28(%rax), %rax
callq *%rax
movl %eax, -0xe8(%rbp)
jmp 0xcb5b
movl -0xe8(%rbp), %eax
movl %eax, -0x44(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0xa800
cmpl $0x1, -0x44(%rbp)
je 0xcbb8
jmp 0xcbd5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xa800
jmp 0xcbe3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0xa800
jmp 0xcbe3
jmp 0xcbba
jmp 0xcbbc
jmp 0xcbbe
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0xd080
movq %rax, -0xc0(%rbp)
jmp 0xca3c
jmp 0xcbd7
movl -0x44(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x98(%rbp), %rdi
callq 0x34b0
nop