// File: /workspace/github_c_makefile_star2_O0/sszllzss[P]webSocket_Libevent_ThreadPool/websocket_common.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, type;
    int count, ret;
    unsigned int i, len = 0, dataStart = 2;
    if(dataLen < 2)
        return -1;

    type = data[0]&0x0F;

    if((data[0]&0x80) == 0x80)
    {
        if(type == 0x01) 
            ret = WCT_TXTDATA;
        else if(type == 0x02) 
            ret = WCT_BINDATA;
        else if(type == 0x08) 
            ret = WCT_DISCONN;
        else if(type == 0x09) 
            ret = WCT_PING;
        else if(type == 0x0A) 
            ret = WCT_PONG;
        else 
            return WCT_ERR;
    }
    else if(type == 0x00) 
        ret = WCT_MINDATA;
    else
        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 0x2130
movb $0x0, -0x37(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x2, -0x4c(%rbp)
cmpl $0x2, -0x14(%rbp)
jae 0x321e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x35fa
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
andl $0xf, %eax
movb %al, -0x38(%rbp)
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
andl $0x80, %eax
cmpl $0x80, %eax
jne 0x32ae
movsbl -0x38(%rbp), %eax
cmpl $0x1, %eax
jne 0x3250
movl $0xffffffed, -0x40(%rbp) # imm = 0xFFFFFFED
jmp 0x32ac
movsbl -0x38(%rbp), %eax
cmpl $0x2, %eax
jne 0x3262
movl $0xffffffee, -0x40(%rbp) # imm = 0xFFFFFFEE
jmp 0x32aa
movsbl -0x38(%rbp), %eax
cmpl $0x8, %eax
jne 0x3274
movl $0xffffffef, -0x40(%rbp) # imm = 0xFFFFFFEF
jmp 0x32a8
movsbl -0x38(%rbp), %eax
cmpl $0x9, %eax
jne 0x3286
movl $0xfffffff0, -0x40(%rbp) # imm = 0xFFFFFFF0
jmp 0x32a6
movsbl -0x38(%rbp), %eax
cmpl $0xa, %eax
jne 0x3298
movl $0xfffffff1, -0x40(%rbp) # imm = 0xFFFFFFF1
jmp 0x32a4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x35fa
jmp 0x32a6
jmp 0x32a8
jmp 0x32aa
jmp 0x32ac
jmp 0x32ce
movsbl -0x38(%rbp), %eax
cmpl $0x0, %eax
jne 0x32c0
movl $0xffffffec, -0x40(%rbp) # imm = 0xFFFFFFEC
jmp 0x32cc
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x35fa
jmp 0x32ce
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
andl $0x80, %eax
cmpl $0x80, %eax
jne 0x32ef
movb $0x1, -0x37(%rbp)
movl $0x4, -0x3c(%rbp)
jmp 0x32fa
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 0x33a1
cmpl $0x4, -0x14(%rbp)
jae 0x3324
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x35fa
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 0x335e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x35fa
cmpb $0x0, -0x37(%rbp)
je 0x3395
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 0x339c
movl $0x4, -0x4c(%rbp)
jmp 0x34fb
cmpl $0x7f, -0x48(%rbp)
jne 0x349d
cmpl $0xa, -0x14(%rbp)
jae 0x33bd
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x35fa
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x0, %eax
jne 0x33f1
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x0, %eax
jne 0x33f1
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x0, %eax
jne 0x33f1
movq -0x10(%rbp), %rax
movzbl 0x5(%rax), %eax
cmpl $0x0, %eax
je 0x33fd
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x35fa
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 0x345d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x35fa
cmpb $0x0, -0x37(%rbp)
je 0x3494
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 0x349b
movl $0xa, -0x4c(%rbp)
jmp 0x34f9
movl -0x14(%rbp), %eax
movl -0x48(%rbp), %ecx
addl $0x2, %ecx
addl -0x3c(%rbp), %ecx
cmpl %ecx, %eax
jae 0x34b9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x35fa
cmpb $0x0, -0x37(%rbp)
je 0x34f0
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 0x34f7
movl $0x2, -0x4c(%rbp)
jmp 0x34f9
jmp 0x34fb
movl -0x14(%rbp), %eax
movl -0x48(%rbp), %ecx
addl -0x4c(%rbp), %ecx
cmpl %ecx, %eax
jae 0x3514
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x35fa
movl -0x24(%rbp), %eax
movl -0x48(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jae 0x352d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x35fa
cmpb $0x0, -0x37(%rbp)
je 0x35c8
movl $0x0, -0x44(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x35bf
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 0x35b2
movl $0x0, -0x3c(%rbp)
jmp 0x35b4
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x3545
movq -0x20(%rbp), %rax
movb $0x0, (%rax)
jmp 0x35eb
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x4c(%rbp), %eax
addq %rax, %rsi
movl -0x48(%rbp), %eax
movl %eax, %edx
callq 0x21a0
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)