// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl $0x0, -0x2c(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x2, %ecx
jb 0x49bc
addl $0x14, %r9d
cmpl $0x5, %r9d
ja 0x49bc
movl %ecx, %r10d
movl %esi, %ebx
movq %rdi, %r12
shll $0x3, %r9d
movabsq $0x8a8988828100, %rsi   # imm = 0x8A8988828100
movl %r9d, %ecx
shrq %cl, %rsi
movb %sil, (%rdx)
leaq 0x1(%rdx), %r14
testb %r8b, %r8b
je 0x4880
movb $-0x80, (%r14)
cmpl $0x7d, %ebx
ja 0x4921
orb %bl, 0x1(%rdx)
addq $0x2, %rdx
movl $0x2, %r13d
movq %rdx, %r14
testb %r8b, %r8b
je 0x494c
leal (%rbx,%r13), %ecx
addl $0x4, %ecx
cmpl %r10d, %ecx
ja 0x49bc
leaq -0x2c(%rbp), %r15
movq %r15, %rdi
movl $0x4, %esi
callq 0x44f4
movb (%r15), %al
movb %al, (%r14)
movb 0x1(%r15), %al
movb %al, 0x1(%r14)
movb 0x2(%r15), %al
movb %al, 0x2(%r14)
movb 0x3(%r15), %al
movb %al, 0x3(%r14)
addq $0x4, %r14
addl $0x4, %r13d
testl %ebx, %ebx
je 0x49b0
movl %ebx, %ecx
xorl %edx, %edx
xorl %eax, %eax
xorl %esi, %esi
movslq %esi, %rsi
movb (%r12,%rax), %dil
xorb -0x2c(%rbp,%rsi), %dil
movb %dil, (%r14,%rax)
incl %esi
cmpl $0x4, %esi
cmovael %edx, %esi
incq %rax
cmpq %rax, %rcx
jne 0x48f9
addq %rax, %r14
jmp 0x49b2
cmpl $0xffff, %ebx           # imm = 0xFFFF
ja 0x496a
cmpl $0x4, %r10d
jb 0x49bc
orb $0x7e, 0x1(%rdx)
movb %bh, 0x2(%rdx)
movb %bl, 0x3(%rdx)
addq $0x4, %rdx
movl $0x4, %r13d
jmp 0x4896
addl %ebx, %r13d
cmpl %r10d, %r13d
ja 0x49bc
movl %ebx, %r15d
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3350
addq %r15, %r14
jmp 0x49b5
cmpl $-0x1, %ebx
je 0x49a5
cmpl $0xa, %r10d
jb 0x49bc
orb $0x7f, 0x1(%rdx)
movl %ebx, %ecx
shrl $0x18, %ecx
movl $0x0, 0x2(%rdx)
movb %cl, 0x6(%rdx)
movl %ebx, %ecx
shrl $0x10, %ecx
movb %cl, 0x7(%rdx)
movb %bh, 0x8(%rdx)
movb %bl, 0x9(%rdx)
addq $0xa, %rdx
movl $0xa, %r13d
jmp 0x4896
movl $0x1, %r13d
jmp 0x4899
xorl %eax, %eax
addl %eax, %r13d
movb $0x0, (%r14)
movl %r13d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq