// File: /workspace/github_cpp_makefile_star2_O3/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 %rsi, 0x20(%rsp)
movq %rdi, %r14
cmpl $0x2, %ecx
jae 0x6b94
movq 0x20(%r14), %r13
xorl %r15d, %r15d
cmpq 0x28(%r14), %r13
jae 0x6d54
leaq 0x8(%r14), %r12
leaq 0x50(%rsp), %rax
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
movq (%r13), %rbx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
cmpl (%r14), %eax
jne 0x6b81
leaq 0x40(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x5eba
movq (%rbx), %rax
movq %rbx, %rdi
movq %rbp, %rsi
callq *0x20(%rax)
movl %eax, %ebp
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x6b32
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
cmpb $0x1, %bpl
jne 0x6b81
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x5eba
movq (%rbx), %rax
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
callq *0x28(%rax)
movl %eax, %ebx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x6b72
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
movl $0x1, %r15d
cmpl $0x1, %ebx
jne 0x6d51
addq $0x8, %r13
cmpq 0x28(%r14), %r13
jb 0x6ae7
jmp 0x6d54
movl %ecx, %ebp
movq %rdx, %r12
leaq 0x8(%r14), %rax
movq %rax, 0x8(%rsp)
movq %r14, 0x18(%rsp)
movq %r12, %r13
movl %ebp, %edx
movq %r12, %rdi
leaq 0x17a9(%rip), %rsi      # 0x835f
callq 0x6dfd
testq %rax, %rax
je 0x6ac5
movq %rax, %r12
addq $0x2, %r12
movl %r13d, %eax
subl %r12d, %eax
addl %eax, %ebp
je 0x6ac5
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
subl %eax, %ecx
testl %ecx, %ecx
jle 0x6ac5
cmpl $0x0, (%r14)
je 0x6c7b
movl %ecx, %r15d
xorl %r14d, %r14d
movq %r14, %rbx
incq %r14
cmpq %r15, %rbx
jae 0x6c0b
cmpb $0x3a, (%r13,%rbx)
jne 0x6bf8
movl $0x28, %edi
callq 0x3310
leaq 0x8(%rax), %rsi
leaq (%r14,%r13), %rcx
movl %r15d, %edx
subl %r14d, %edx
cmpq %r15, %rbx
movq %rax, 0x10(%rsp)
movq %r13, (%rax)
movl %ebx, 0x8(%rax)
movq %rcx, 0x10(%rax)
movl $0x0, %ecx
cmovael %ecx, %edx
movl %edx, 0x18(%rax)
movq %rax, %rdi
callq 0x3871
movq 0x10(%rsp), %rsi
leaq 0x10(%rsi), %rdi
addq $0x18, %rsi
callq 0x3871
movq 0x18(%rsp), %r14
movq 0x10(%r14), %rsi
cmpq 0x18(%r14), %rsi
je 0x6cd0
movq 0x10(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r14)
jmp 0x6d43
xorl %r15d, %r15d
cmpl $-0x15, %eax
je 0x6d11
cmpl $-0x11, %eax
je 0x6ce1
cmpl $-0x13, %eax
jne 0x6d54
movdqu (%r13), %xmm0
pcmpeqb 0x190d(%rip), %xmm0     # 0x85b0
movzbl 0x10(%r13), %eax
movd %eax, %xmm1
pcmpeqb 0x190c(%rip), %xmm1     # 0x85c0
pand %xmm0, %xmm1
pmovmskb %xmm1, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x6d54
movl $0x1, (%r14)
jmp 0x6d43
movq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x5f56
jmp 0x6d43
movq (%r13), %rax
movabsq $0x312e312f50545448, %rcx # imm = 0x312E312F50545448
xorq %rcx, %rax
movq 0x7(%r13), %rcx
movabsq $0x4b4f203030322031, %rdx # imm = 0x4B4F203030322031
xorq %rdx, %rcx
orq %rax, %rcx
jne 0x6d54
movl $0x3, (%r14)
jmp 0x6d43
movdqu (%r13), %xmm0
pcmpeqb 0x1881(%rip), %xmm0     # 0x85a0
movdqu 0x3(%r13), %xmm1
pcmpeqb 0x1863(%rip), %xmm1     # 0x8590
pand %xmm0, %xmm1
pmovmskb %xmm1, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x6d54
movl $0x2, (%r14)
cmpl $0x1, %ebp
jne 0x6ba7
jmp 0x6ac5
movl %ebx, %r15d
movl %r15d, %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, 0x8(%rsp)
jmp 0x6d82
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x6d97
movq 0x8(%rsp), %rax
movq (%rax), %rsi
subq %rdi, %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x3410
nop