// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
testq %rdx, %rdx
je 0x5a58
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movb (%rsi), %al
movb %al, (%rdi)
cmpq $0x2, %rdx
ja 0x5a77
decq %rbx
leaq 0x2128(%rip), %rsi      # 0x7b67
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x273c
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x5b3e
leaq 0x20da(%rip), %rsi      # 0x7b39
movl $0x1, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x273c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x5b3e
movzwl 0x1(%r15), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r14)
leaq -0x3(%rbx), %rdx
cmpq $0x1, %rdx
ja 0x5aac
leaq 0x20fd(%rip), %rsi      # 0x7b93
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jmp 0x5b3e
movzwl 0x3(%r15), %eax
rolw $0x8, %ax
movw %ax, 0x4(%r14)
addq $-0x5, %rbx
movzwl %ax, %r12d
cmpq %r12, %rbx
jae 0x5ae4
leaq 0x20ee(%rip), %rsi      # 0x7bbc
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x273c
movl $0xfffffffc, %eax       # imm = 0xFFFFFFFC
jmp 0x5b3e
addq $0x5, %r15
addq $-0x20, %r12
leaq 0x6(%r14), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2150
subq %r12, %rbx
cmpq $0x1f, %rbx
ja 0x5b21
leaq 0x20db(%rip), %rsi      # 0x7be6
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x273c
movl $0xfffffffb, %eax       # imm = 0xFFFFFFFB
jmp 0x5b3e
movups (%r15,%r12), %xmm0
movups %xmm0, 0x10006(%r14)
movups 0x10(%r15,%r12), %xmm0
movups %xmm0, 0x10016(%r14)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq