// File: renzibei[P]TSProxy/utils.cpp
// Function: disAssembleTSPacket(TSPacket*, unsigned char const*, unsigned long)
int disAssembleTSPacket(TSPacket *packet, const uint8_t *src_data, size_t nbyte) {
    if (nbyte < sizeof(packet->contentType)) {
        LogHelper::log(Warn, "nbyte less than sizeof contentType, byte: %lu", nbyte);
        return -1;
    }
    packet->contentType = *src_data++; nbyte -= sizeof(packet->contentType);
    if(nbyte < sizeof(packet->tsVersion)) {
        LogHelper::log(Warn, "nbyte less than sizeof tsVersion, byte: %lu", nbyte);
        return -2;
    }
    ntoh_copy2bytes(&(packet->tsVersion), src_data); src_data += sizeof(packet->tsVersion); nbyte -= sizeof(packet->tsVersion);
    if(nbyte < sizeof(packet->length)) {
        LogHelper::log(Warn, "nbyte less than sizeof length, byte: %lu", nbyte);
        return -3;
    }
    ntoh_copy2bytes(&(packet->length), src_data); src_data += sizeof(packet->length); nbyte -= sizeof(packet->length);
    if(nbyte < packet->length) {
        LogHelper::log(Warn, "nbyte less than packet->length, byte: %lu", nbyte);
        return -4;
    }

    size_t realMsgLen = packet->length - sizeof(packet->random) - sizeof(packet->mac);

    memcpy(packet->msgData, src_data, realMsgLen); src_data += realMsgLen; nbyte -= realMsgLen;
    if(nbyte < sizeof(packet->random) + sizeof(packet->mac)) {
        LogHelper::log(Warn, "nbyte less than sizeof random + mac, byte: %lu", nbyte);
        return -5;
    } 
    // const uint8_t *tempSrc = src_data;
    memcpy(packet->random, src_data, sizeof(packet->random)); src_data += sizeof(packet->random); nbyte -= sizeof(packet->random);
    memcpy(packet->mac, src_data, sizeof(packet->mac));

    // fprintf(stderr, "when disassemble random: ");
    // for(int i = 0; i < 16; ++i) {
    //     fprintf(stderr, "%u ", (uint8_t)tempSrc[i]);
    // }
    // fprintf(stderr, "\nmac: ");
    // for(int i = 0; i < 16; ++i) {
    //     fprintf(stderr, "%u ", (uint8_t)tempSrc[16 + i]);
    // }
    // fprintf(stderr, "\n");
    return 0;
    // disAssembleInnerMsg(&(packet->msgData), src_data, nbyte);
    
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
cmpq $0x1, 0x8(%rsp)
jae 0xce30
movq 0x8(%rsp), %rdx
movl $0x1, %edi
leaq 0x3d22(%rip), %rsi      # 0x10b3e
movb $0x0, %al
callq 0x28f0
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
jmp 0xd02b
movq 0x10(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rsp)
movb (%rax), %cl
movq 0x18(%rsp), %rax
movb %cl, (%rax)
movq 0x8(%rsp), %rax
subq $0x1, %rax
movq %rax, 0x8(%rsp)
cmpq $0x2, 0x8(%rsp)
jae 0xce85
movq 0x8(%rsp), %rdx
movl $0x1, %edi
leaq 0x3cfb(%rip), %rsi      # 0x10b6c
movb $0x0, %al
callq 0x28f0
movl $0xfffffffe, 0x24(%rsp) # imm = 0xFFFFFFFE
jmp 0xd02b
movq 0x10(%rsp), %rax
movzwl (%rax), %edi
callq 0x2390
movw %ax, %cx
movq 0x18(%rsp), %rax
movw %cx, 0x2(%rax)
movq 0x10(%rsp), %rax
addq $0x2, %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
subq $0x2, %rax
movq %rax, 0x8(%rsp)
cmpq $0x2, 0x8(%rsp)
jae 0xcee7
movq 0x8(%rsp), %rdx
movl $0x1, %edi
leaq 0x3cc5(%rip), %rsi      # 0x10b98
movb $0x0, %al
callq 0x28f0
movl $0xfffffffd, 0x24(%rsp) # imm = 0xFFFFFFFD
jmp 0xd02b
movq 0x10(%rsp), %rax
movzwl (%rax), %edi
callq 0x2390
movw %ax, %cx
movq 0x18(%rsp), %rax
movw %cx, 0x4(%rax)
movq 0x10(%rsp), %rax
addq $0x2, %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
subq $0x2, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %rcx
movzwl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jae 0xcf54
movq 0x8(%rsp), %rdx
movl $0x1, %edi
leaq 0x3c81(%rip), %rsi      # 0x10bc1
movb $0x0, %al
callq 0x28f0
movl $0xfffffffc, 0x24(%rsp) # imm = 0xFFFFFFFC
jmp 0xd02b
movq 0x18(%rsp), %rax
movzwl 0x4(%rax), %eax
subq $0x10, %rax
subq $0x10, %rax
movq %rax, (%rsp)
movq 0x18(%rsp), %rdi
addq $0x6, %rdi
movq 0x10(%rsp), %rsi
movq (%rsp), %rdx
callq 0x2180
movq (%rsp), %rax
addq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
movq (%rsp), %rcx
movq 0x8(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x8(%rsp)
cmpq $0x20, 0x8(%rsp)
jae 0xcfc9
movq 0x8(%rsp), %rdx
movl $0x1, %edi
leaq 0x3c33(%rip), %rsi      # 0x10beb
movb $0x0, %al
callq 0x28f0
movl $0xfffffffb, 0x24(%rsp) # imm = 0xFFFFFFFB
jmp 0xd02b
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x10006(%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x1000e(%rax)
movq 0x10(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x10016(%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x1001e(%rax)
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
addq $0x28, %rsp
retq
nopw %cs:(%rax,%rax)