// File: /workspace/github_cpp_makefile_star2_O1/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
movl %ecx, %r14d
movq %rdx, %rbx
movq %rsi, 0x20(%rsp)
movq %rdi, %r13
leaq 0x8(%rdi), %rax
movq %rax, (%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
cmpl $0x2, %r14d
jb 0x6aec
movl %r14d, %edx
movq %rbx, %rdi
leaq 0x189d(%rip), %rsi      # 0x835f
callq 0x6dfb
testq %rax, %rax
je 0x6aec
addq $0x2, %rax
movl %ebx, %edx
subl %eax, %edx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
subl %edx, %ecx
addl %edx, %r14d
cmovneq %rax, %r15
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovel %eax, %ecx
jmp 0x6af1
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testl %ecx, %ecx
jle 0x6c8b
cmpl $0x0, (%r13)
je 0x6b94
movq %r15, %rbp
movl %ecx, %r15d
xorl %r13d, %r13d
movq %r13, %r12
incq %r13
cmpq %r15, %r12
jae 0x6b1f
cmpb $0x3a, (%rbx,%r12)
jne 0x6b0d
movl $0x28, %edi
callq 0x3310
leaq 0x8(%rax), %rsi
leaq (%rbx,%r13), %rcx
movl %r15d, %edx
subl %r13d, %edx
cmpq %r15, %r12
movq %rax, 0x8(%rsp)
movq %rbx, (%rax)
movl %r12d, 0x8(%rax)
movq %rcx, 0x10(%rax)
movl $0x0, %ecx
cmovael %ecx, %edx
movl %edx, 0x18(%rax)
movq %rax, %rdi
callq 0x3899
movq 0x8(%rsp), %rsi
leaq 0x10(%rsi), %rdi
addq $0x18, %rsi
callq 0x3899
movq 0x18(%rsp), %r13
movq 0x10(%r13), %rsi
cmpq 0x18(%r13), %rsi
je 0x6c52
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r13)
jmp 0x6c60
xorl %r12d, %r12d
cmpl $0xf, %ecx
je 0x6c18
cmpl $0x13, %ecx
je 0x6be7
cmpl $0x11, %ecx
jne 0x6d53
movdqu (%rbx), %xmm0
pcmpeqb 0x19da(%rip), %xmm0     # 0x8590
movzbl 0x10(%rbx), %eax
movd %eax, %xmm1
pcmpeqb 0x19da(%rip), %xmm1     # 0x85a0
pand %xmm0, %xmm1
pmovmskb %xmm1, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x6c7b
cmpl $0xf, %ecx
je 0x6c18
cmpl $0x13, %ecx
jne 0x6d53
movdqu (%rbx), %xmm0
pcmpeqb 0x19cd(%rip), %xmm0     # 0x85c0
movdqu 0x3(%rbx), %xmm1
pcmpeqb 0x19b0(%rip), %xmm1     # 0x85b0
pand %xmm0, %xmm1
pmovmskb %xmm1, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x6c6b
cmpl $0xf, %ecx
jne 0x6d53
movq (%rbx), %rax
movabsq $0x312e312f50545448, %rcx # imm = 0x312E312F50545448
xorq %rcx, %rax
movq 0x7(%rbx), %rcx
movabsq $0x4b4f203030322031, %rdx # imm = 0x4B4F203030322031
xorq %rdx, %rcx
orq %rax, %rcx
jne 0x6d53
movl $0x3, (%r13)
movq %r15, %rbx
jmp 0x6aaf
movq (%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x5f40
movq %rbp, %r15
movq %rbp, %rbx
jmp 0x6aaf
movl $0x2, (%r13)
movq %r15, %rbx
jmp 0x6aaf
movl $0x1, (%r13)
movq %r15, %rbx
jmp 0x6aaf
movq 0x20(%r13), %r15
xorl %r12d, %r12d
cmpq 0x28(%r13), %r15
jae 0x6d53
xorl %r12d, %r12d
leaq 0x40(%rsp), %rbx
leaq 0x28(%rsp), %r14
movq (%r15), %rbp
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x10(%rax)
cmpl (%r13), %eax
jne 0x6d40
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x5ea4
movq (%rbp), %rax
movq %rbp, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movl %eax, %r13d
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x6cf3
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
cmpb $0x1, %r13b
movq 0x18(%rsp), %r13
jne 0x6d40
movq %r14, %rdi
movq (%rsp), %rsi
callq 0x5ea4
movq (%rbp), %rax
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
callq *0x28(%rax)
movl %eax, %ebp
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x6d35
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
movl $0x1, %r12d
cmpl $0x1, %ebp
jne 0x6d50
addq $0x8, %r15
cmpq 0x28(%r13), %r15
jb 0x6ca9
jmp 0x6d53
movl %ebp, %r12d
movl %r12d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x6d81
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x6d96
movq 0x10(%rsp), %rax
movq (%rax), %rsi
subq %rdi, %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x3410