// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %rbx
movq %rsi, 0x18(%rsp)
movq %rdi, %r14
leaq 0x8(%rdi), %rax
movq %rax, (%rsp)
movq %rbx, %rsi
movl %ecx, %edx
leaq 0x20(%rsp), %rcx
leaq 0xc(%rsp), %r8
callq 0x6570
testl %eax, %eax
jle 0x64b6
cmpl $0x0, (%r14)
je 0x6435
movl %eax, %r15d
xorl %r12d, %r12d
movq %r12, %rbp
incq %r12
cmpq %r15, %rbp
jae 0x63c4
cmpb $0x3a, (%rbx,%rbp)
jne 0x63b3
leal -0x1(%r12), %r13d
pushq $0x28
popq %rdi
callq 0x32f0
leaq 0x8(%rax), %rsi
movq %r12, %rcx
addq %rbx, %rcx
movl %r15d, %edx
subl %r12d, %edx
cmpq %r15, %rbp
movq %rax, 0x10(%rsp)
movq %rbx, (%rax)
movl %r13d, 0x8(%rax)
movq %rcx, 0x10(%rax)
movl $0x0, %ecx
cmovael %ecx, %edx
movl %edx, 0x18(%rax)
movq %rax, %rdi
callq 0x37b6
movq 0x10(%rsp), %rsi
leaq 0x10(%rsi), %rdi
addq $0x18, %rsi
callq 0x37b6
movq (%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x592e
movq 0x20(%rsp), %rbx
movl 0xc(%rsp), %ecx
jmp 0x6387
xorl %ebp, %ebp
cmpl $0xf, %eax
je 0x6490
cmpl $0x13, %eax
je 0x646d
cmpl $0x11, %eax
jne 0x6543
movq %rbx, %rdi
leaq 0x2135(%rip), %rsi      # 0x8589
pushq $0x11
popq %rdx
callq 0x3290
testl %eax, %eax
jne 0x6543
movl $0x1, (%r14)
jmp 0x6427
movq %rbx, %rdi
leaq 0x2124(%rip), %rsi      # 0x859b
pushq $0x13
popq %rdx
callq 0x3290
testl %eax, %eax
jne 0x6543
movl $0x2, (%r14)
jmp 0x6427
movq %rbx, %rdi
leaq 0x2115(%rip), %rsi      # 0x85af
pushq $0xf
popq %rdx
callq 0x3290
testl %eax, %eax
jne 0x6543
movl $0x3, (%r14)
jmp 0x6427
movq 0x20(%r14), %r15
xorl %ebp, %ebp
leaq 0x40(%rsp), %rbx
cmpq 0x28(%r14), %r15
jae 0x6543
movq (%r15), %r13
movq (%r13), %rax
movq %r13, %rdi
callq *0x10(%rax)
cmpl (%r14), %eax
jne 0x653a
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x58e0
movq (%r13), %rax
movq %r13, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movl %eax, %r12d
movq %rbx, %rdi
callq 0x594c
cmpb $0x1, %r12b
jne 0x653a
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq (%rsp), %rsi
callq 0x58e0
movq (%r13), %rax
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movq %r12, %rdx
callq *0x28(%rax)
movl %eax, %r13d
movq %r12, %rdi
callq 0x594c
pushq $0x1
popq %rbp
cmpl $0x1, %r13d
jne 0x6540
addq $0x8, %r15
jmp 0x64c1
movl %r13d, %ebp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq %r12, %rbx
jmp 0x655f
movq %rax, %r14
movq %rbx, %rdi
callq 0x594c
movq %r14, %rdi
callq 0x33d0
nop