// File: /workspace/github_cpp_makefile_star2_O2/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 0x5b4d
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movb (%rsi), %al
movb %al, (%rdi)
cmpq $0x2, %rdx
ja 0x5b67
decq %rbx
leaq 0x202e(%rip), %rsi      # 0x7b67
pushq $0x1
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x262c
pushq $-0x2
jmp 0x5bfd
leaq 0x1fe5(%rip), %rsi      # 0x7b39
pushq $0x1
popq %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0x262c
pushq $-0x1
jmp 0x5bfd
movzwl 0x1(%r15), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r14)
leaq -0x3(%rbx), %rdx
cmpq $0x1, %rdx
ja 0x5b94
leaq 0x200d(%rip), %rsi      # 0x7b93
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x262c
pushq $-0x3
jmp 0x5bfd
movzwl 0x3(%r15), %eax
rolw $0x8, %ax
movw %ax, 0x4(%r14)
addq $-0x5, %rbx
movzwl %ax, %r12d
cmpq %r12, %rbx
jae 0x5bc7
leaq 0x2006(%rip), %rsi      # 0x7bbc
pushq $0x1
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x262c
pushq $-0x4
jmp 0x5bfd
addq $0x5, %r15
addq $-0x20, %r12
leaq 0x6(%r14), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2160
subq %r12, %rbx
cmpq $0x1f, %rbx
ja 0x5c0a
leaq 0x1ff8(%rip), %rsi      # 0x7be6
pushq $0x1
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x262c
pushq $-0x5
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movups (%r15,%r12), %xmm0
movups %xmm0, 0x10006(%r14)
movups 0x10(%r15,%r12), %xmm0
movups %xmm0, 0x10016(%r14)
xorl %eax, %eax
jmp 0x5bfe