// File: /workspace/github_c_makefile_star2_O0/sszllzss[P]webSocket_Libevent_ThreadPool/ev_websocket.c
// Function: webSocket_enPackage(unsigned char*, unsigned int, unsigned char*, unsigned int, bool, Websocket_CommunicationType)
int webSocket_enPackage(unsigned char *data, unsigned int dataLen, unsigned char *package, unsigned int packageMaxLen, bool isMask, Websocket_CommunicationType type)
{
    unsigned char maskKey[4] = {0};    // 掩码
    unsigned char temp1, temp2;
    int count;
    unsigned int i, len = 0;

    if(packageMaxLen < 2)
        return -1;

    if(type == WCT_MINDATA)
        *package++ = 0x00;
    else if(type == WCT_TXTDATA)
        *package++ = 0x81;
    else if(type == WCT_BINDATA)
        *package++ = 0x82;
    else if(type == WCT_DISCONN)
        *package++ = 0x88;
    else if(type == WCT_PING)
        *package++ = 0x89;
    else if(type == WCT_PONG)
        *package++ = 0x8A;
    else
        return -1;
    //
    if(isMask)
        *package = 0x80;
    len += 1;
    //
    if(dataLen < 126)
    {
        *package++ |= (dataLen&0x7F);
        len += 1;
    }
    else if(dataLen < 65536)
    {
        if(packageMaxLen < 4)
            return -1;
        *package++ |= 0x7E;
        *package++ = (char)((dataLen >> 8) & 0xFF);
        *package++ = (unsigned char)((dataLen >> 0) & 0xFF);
        len += 3;
    }
    else if(dataLen < 0xFFFFFFFF)
    {
        if(packageMaxLen < 10)
            return -1;
        *package++ |= 0x7F;
        *package++ = 0; //(char)((dataLen >> 56) & 0xFF);   // 数据长度变量是 unsigned int dataLen, 暂时没有那么多数据
        *package++ = 0; //(char)((dataLen >> 48) & 0xFF);
        *package++ = 0; //(char)((dataLen >> 40) & 0xFF);
        *package++ = 0; //(char)((dataLen >> 32) & 0xFF);
        *package++ = (char)((dataLen >> 24) & 0xFF);        // 到这里就够传4GB数据了
        *package++ = (char)((dataLen >> 16) & 0xFF);
        *package++ = (char)((dataLen >> 8) & 0xFF);
        *package++ = (char)((dataLen >> 0) & 0xFF);
        len += 9;
    }
    //
    if(isMask)    // 数据使用掩码时, 使用异或解码, maskKey[4]依次和数据异或运算, 逻辑如下
    {
        if(packageMaxLen < len + dataLen + 4)
            return -1;
        webSocket_getRandomString(maskKey, sizeof(maskKey));    // 随机生成掩码
        *package++ = maskKey[0];
        *package++ = maskKey[1];
        *package++ = maskKey[2];
        *package++ = maskKey[3];
        len += 4;
        for(i = 0, count = 0; i < dataLen; i++)
        {
            temp1 = maskKey[count];
            temp2 = data[i];
            *package++ = (char)(((~temp1)&temp2) | (temp1&(~temp2)));  // 异或运算后得到数据
            count += 1;
            if((unsigned)count >= sizeof(maskKey))    // maskKey[4]循环使用
                count = 0;
        }
        len += i;
        *package = '\0';
    }
    else    // 数据没使用掩码, 直接复制数据段
    {
        if(packageMaxLen < len + dataLen)
            return -1;
        memcpy(package, data, dataLen);
        package[dataLen] = '\0';
        len += dataLen;
    }
    //
    return len;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movb %r8b, %al
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
andb $0x1, %al
movb %al, -0x25(%rbp)
movl %r9d, -0x2c(%rbp)
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x4290
movl $0x0, -0x40(%rbp)
cmpl $0x2, -0x24(%rbp)
jae 0x54ab
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5886
cmpl $-0x14, -0x2c(%rbp)
jne 0x54c8
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $0x0, (%rax)
jmp 0x5560
cmpl $-0x13, -0x2c(%rbp)
jne 0x54e2
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $-0x7f, (%rax)
jmp 0x555e
cmpl $-0x12, -0x2c(%rbp)
jne 0x54fc
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $-0x7e, (%rax)
jmp 0x555c
cmpl $-0x11, -0x2c(%rbp)
jne 0x5516
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $-0x78, (%rax)
jmp 0x555a
cmpl $-0x10, -0x2c(%rbp)
jne 0x5530
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $-0x77, (%rax)
jmp 0x5558
cmpl $-0xf, -0x2c(%rbp)
jne 0x554a
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $-0x76, (%rax)
jmp 0x5556
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5886
jmp 0x5558
jmp 0x555a
jmp 0x555c
jmp 0x555e
jmp 0x5560
testb $0x1, -0x25(%rbp)
je 0x556d
movq -0x20(%rbp), %rax
movb $-0x80, (%rax)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
cmpl $0x7e, -0x14(%rbp)
jae 0x55a6
movl -0x14(%rbp), %edx
andl $0x7f, %edx
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movzbl (%rax), %ecx
orl %edx, %ecx
movb %cl, (%rax)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x5722
cmpl $0x10000, -0x14(%rbp)   # imm = 0x10000
jae 0x5622
cmpl $0x4, -0x24(%rbp)
jae 0x55c1
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5886
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movzbl (%rax), %ecx
orl $0x7e, %ecx
movb %cl, (%rax)
movl -0x14(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
movl -0x14(%rbp), %eax
shrl $0x0, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
movl -0x40(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x40(%rbp)
jmp 0x5720
cmpl $-0x1, -0x14(%rbp)
jae 0x571e
cmpl $0xa, -0x24(%rbp)
jae 0x563e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5886
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movzbl (%rax), %ecx
orl $0x7f, %ecx
movb %cl, (%rax)
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $0x0, (%rax)
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $0x0, (%rax)
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $0x0, (%rax)
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $0x0, (%rax)
movl -0x14(%rbp), %eax
shrl $0x18, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
movl -0x14(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
movl -0x14(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
movl -0x14(%rbp), %eax
shrl $0x0, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
movl -0x40(%rbp), %eax
addl $0x9, %eax
movl %eax, -0x40(%rbp)
jmp 0x5720
jmp 0x5722
testb $0x1, -0x25(%rbp)
je 0x5844
movl -0x24(%rbp), %eax
movl -0x40(%rbp), %ecx
addl -0x14(%rbp), %ecx
addl $0x4, %ecx
cmpl %ecx, %eax
jae 0x5748
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5886
leaq -0x30(%rbp), %rdi
movl $0x4, %esi
callq 0x5040
movb -0x30(%rbp), %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
movb -0x2f(%rbp), %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
movb -0x2e(%rbp), %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
movb -0x2d(%rbp), %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
movl -0x40(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x5832
movslq -0x38(%rbp), %rax
movb -0x30(%rbp,%rax), %al
movb %al, -0x31(%rbp)
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
movb (%rax,%rcx), %al
movb %al, -0x32(%rbp)
movzbl -0x31(%rbp), %eax
xorl $-0x1, %eax
movzbl -0x32(%rbp), %ecx
andl %ecx, %eax
movzbl -0x31(%rbp), %ecx
movzbl -0x32(%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 -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpq $0x4, %rax
jb 0x5825
movl $0x0, -0x38(%rbp)
jmp 0x5827
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x57bd
movl -0x3c(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movb $0x0, (%rax)
jmp 0x5880
movl -0x24(%rbp), %eax
movl -0x40(%rbp), %ecx
addl -0x14(%rbp), %ecx
cmpl %ecx, %eax
jae 0x585a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5886
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %eax
movl %eax, %edx
callq 0x43f0
movq -0x20(%rbp), %rax
movl -0x14(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movl -0x14(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop