// File: /workspace/github_c_makefile_star2_O0/sszllzss[P]webSocket_Libevent_ThreadPool/ev_websocket.c
// Function: webSocket_dePackage(unsigned char*, unsigned int, unsigned char*, unsigned int, unsigned int*)
int webSocket_dePackage(unsigned char *data, unsigned int dataLen, unsigned char *package, unsigned int packageMaxLen, unsigned int *packageLen)
{
    unsigned char maskKey[4] = {0};    // 掩码
    unsigned char temp1, temp2;
    char Mask = 0;
    int count, ret;
    unsigned int i, len = 0, dataStart = 2;
    if(dataLen < 2)
        return WCT_ERR;

    ret = webSocket_getInType(data);
    if(ret == WCT_ERR) 
        return WCT_ERR;

    //
    if((data[1] & 0x80) == 0x80)
    {
        Mask = 1;
        count = 4;
    }
    else
    {
        Mask = 0;
        count = 0;
    }
    //
    len = data[1] & 0x7F;
    //
    if(len == 126)
    {
        if(dataLen < 4)
            return WCT_ERR;
        len = data[2];
        len = (len << 8) + data[3];
        if(dataLen < len + 4 + count)
            return WCT_ERR;
        if(Mask)
        {
            maskKey[0] = data[4];
            maskKey[1] = data[5];
            maskKey[2] = data[6];
            maskKey[3] = data[7];
            dataStart = 8;
        }
        else
            dataStart = 4;
    }
    else if(len == 127)
    {
        if(dataLen < 10)
            return WCT_ERR;
        if(data[2] != 0 || data[3] != 0 || data[4] != 0 || data[5] != 0)    // 使用8个字节存储长度时, 前4位必须为0, 装不下那么多数据...
            return WCT_ERR;
        len = data[6];
        len = (len << 8) + data[7];
        len = (len << 8) + data[8];
        len = (len << 8) + data[9];
        if(dataLen < len + 10 + count)
            return WCT_ERR;
        if(Mask)
        {
            maskKey[0] = data[10];
            maskKey[1] = data[11];
            maskKey[2] = data[12];
            maskKey[3] = data[13];
            dataStart = 14;
        }
        else
            dataStart = 10;
    }
    else
    {
        if(dataLen < len + 2 + count)
            return WCT_ERR;
        if(Mask)
        {
            maskKey[0] = data[2];
            maskKey[1] = data[3];
            maskKey[2] = data[4];
            maskKey[3] = data[5];
            dataStart = 6;
        }
        else
            dataStart = 2;
    }
    //
    if(dataLen < len + dataStart)
        return WCT_ERR;
    //
    if(packageMaxLen < len + 1)
        return WCT_ERR;
    //
    if(Mask)    // 解包数据使用掩码时, 使用异或解码, maskKey[4]依次和数据异或运算, 逻辑如下
    {
        //printf("depackage : len/%d\r\n", len);
        for(i = 0, count = 0; i < len; i++)
        {
            temp1 = maskKey[count];
            temp2 = data[i + dataStart];
            *package++ =  (char)(((~temp1)&temp2) | (temp1&(~temp2)));  // 异或运算后得到数据
            count += 1;
            if((unsigned)count >= sizeof(maskKey))    // maskKey[4]循环使用
                count = 0;
            //printf("%.2X|%.2X|%.2X, ", temp1, temp2, *(package-1));
        }
        *package = '\0';
    }
    else    // 解包数据没使用掩码, 直接复制数据段
    {
        memcpy(package, &data[dataStart], len);
        package[len] = '\0';
    }
    *packageLen = len;
    //
    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
leaq -0x34(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x4290
movb $0x0, -0x37(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x2, -0x4c(%rbp)
cmpl $0x2, -0x14(%rbp)
jae 0x5b5e
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x5ea8
movq -0x10(%rbp), %rdi
callq 0x5890
movl %eax, -0x40(%rbp)
cmpl $-0x2, -0x40(%rbp)
jne 0x5b7c
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x5ea8
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
andl $0x80, %eax
cmpl $0x80, %eax
jne 0x5b9d
movb $0x1, -0x37(%rbp)
movl $0x4, -0x3c(%rbp)
jmp 0x5ba8
movb $0x0, -0x37(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
andl $0x7f, %eax
movl %eax, -0x48(%rbp)
cmpl $0x7e, -0x48(%rbp)
jne 0x5c4f
cmpl $0x4, -0x14(%rbp)
jae 0x5bd2
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x5ea8
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
shll $0x8, %eax
movq -0x10(%rbp), %rcx
movzbl 0x3(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x48(%rbp)
movl -0x14(%rbp), %eax
movl -0x48(%rbp), %ecx
addl $0x4, %ecx
addl -0x3c(%rbp), %ecx
cmpl %ecx, %eax
jae 0x5c0c
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x5ea8
cmpb $0x0, -0x37(%rbp)
je 0x5c43
movq -0x10(%rbp), %rax
movb 0x4(%rax), %al
movb %al, -0x34(%rbp)
movq -0x10(%rbp), %rax
movb 0x5(%rax), %al
movb %al, -0x33(%rbp)
movq -0x10(%rbp), %rax
movb 0x6(%rax), %al
movb %al, -0x32(%rbp)
movq -0x10(%rbp), %rax
movb 0x7(%rax), %al
movb %al, -0x31(%rbp)
movl $0x8, -0x4c(%rbp)
jmp 0x5c4a
movl $0x4, -0x4c(%rbp)
jmp 0x5da9
cmpl $0x7f, -0x48(%rbp)
jne 0x5d4b
cmpl $0xa, -0x14(%rbp)
jae 0x5c6b
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x5ea8
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x0, %eax
jne 0x5c9f
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x0, %eax
jne 0x5c9f
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x0, %eax
jne 0x5c9f
movq -0x10(%rbp), %rax
movzbl 0x5(%rax), %eax
cmpl $0x0, %eax
je 0x5cab
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x5ea8
movq -0x10(%rbp), %rax
movzbl 0x6(%rax), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
shll $0x8, %eax
movq -0x10(%rbp), %rcx
movzbl 0x7(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
shll $0x8, %eax
movq -0x10(%rbp), %rcx
movzbl 0x8(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
shll $0x8, %eax
movq -0x10(%rbp), %rcx
movzbl 0x9(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x48(%rbp)
movl -0x14(%rbp), %eax
movl -0x48(%rbp), %ecx
addl $0xa, %ecx
addl -0x3c(%rbp), %ecx
cmpl %ecx, %eax
jae 0x5d0b
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x5ea8
cmpb $0x0, -0x37(%rbp)
je 0x5d42
movq -0x10(%rbp), %rax
movb 0xa(%rax), %al
movb %al, -0x34(%rbp)
movq -0x10(%rbp), %rax
movb 0xb(%rax), %al
movb %al, -0x33(%rbp)
movq -0x10(%rbp), %rax
movb 0xc(%rax), %al
movb %al, -0x32(%rbp)
movq -0x10(%rbp), %rax
movb 0xd(%rax), %al
movb %al, -0x31(%rbp)
movl $0xe, -0x4c(%rbp)
jmp 0x5d49
movl $0xa, -0x4c(%rbp)
jmp 0x5da7
movl -0x14(%rbp), %eax
movl -0x48(%rbp), %ecx
addl $0x2, %ecx
addl -0x3c(%rbp), %ecx
cmpl %ecx, %eax
jae 0x5d67
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x5ea8
cmpb $0x0, -0x37(%rbp)
je 0x5d9e
movq -0x10(%rbp), %rax
movb 0x2(%rax), %al
movb %al, -0x34(%rbp)
movq -0x10(%rbp), %rax
movb 0x3(%rax), %al
movb %al, -0x33(%rbp)
movq -0x10(%rbp), %rax
movb 0x4(%rax), %al
movb %al, -0x32(%rbp)
movq -0x10(%rbp), %rax
movb 0x5(%rax), %al
movb %al, -0x31(%rbp)
movl $0x6, -0x4c(%rbp)
jmp 0x5da5
movl $0x2, -0x4c(%rbp)
jmp 0x5da7
jmp 0x5da9
movl -0x14(%rbp), %eax
movl -0x48(%rbp), %ecx
addl -0x4c(%rbp), %ecx
cmpl %ecx, %eax
jae 0x5dc2
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x5ea8
movl -0x24(%rbp), %eax
movl -0x48(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jae 0x5ddb
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x5ea8
cmpb $0x0, -0x37(%rbp)
je 0x5e76
movl $0x0, -0x44(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x5e6d
movslq -0x3c(%rbp), %rax
movb -0x34(%rbp,%rax), %al
movb %al, -0x35(%rbp)
movq -0x10(%rbp), %rax
movl -0x44(%rbp), %ecx
addl -0x4c(%rbp), %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %al
movb %al, -0x36(%rbp)
movzbl -0x35(%rbp), %eax
xorl $-0x1, %eax
movzbl -0x36(%rbp), %ecx
andl %ecx, %eax
movzbl -0x35(%rbp), %ecx
movzbl -0x36(%rbp), %edx
xorl $-0x1, %edx
andl %edx, %ecx
orl %ecx, %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpq $0x4, %rax
jb 0x5e60
movl $0x0, -0x3c(%rbp)
jmp 0x5e62
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x5df3
movq -0x20(%rbp), %rax
movb $0x0, (%rax)
jmp 0x5e99
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x4c(%rbp), %eax
addq %rax, %rsi
movl -0x48(%rbp), %eax
movl %eax, %edx
callq 0x43f0
movq -0x20(%rbp), %rax
movl -0x48(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movl -0x48(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movl -0x40(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)