// File: renzibei[P]TSProxy/utils.cpp
// Function: assembleTSPacket(unsigned char*, unsigned long, TSPacket const*)
ssize_t assembleTSPacket(uint8_t *dst, size_t buffer_size, const TSPacket *src_packet) {
    uint8_t *old_dst = dst;
    if (buffer_size < sizeof(src_packet->contentType) + sizeof(src_packet->tsVersion) + sizeof(src_packet->length) ) {
        LogHelper::log(Warn, "buffer size less than TSPacket header, buffer_size: %lu", buffer_size);
        return -1;
    }
    *dst++ = src_packet->contentType; 
    hton_copy2bytes(dst, &(src_packet->tsVersion)); dst += sizeof(src_packet->tsVersion);
    hton_copy2bytes(dst, &(src_packet->length)); dst += sizeof(src_packet->length);
    buffer_size -= sizeof(src_packet->contentType) + sizeof(src_packet->tsVersion) + sizeof(src_packet->length);

    size_t realMsgLen = src_packet->length - sizeof(src_packet->random) - sizeof(src_packet->mac);
    if (buffer_size < realMsgLen) {
        LogHelper::log(Warn, "buffer size less than TSPacket payload length, buffer_size: %lu, payload length: %lu", buffer_size, realMsgLen);
        return -2;
    }
    // size_t delta_dst = assembleInnerMsg(dst, &(src_packet->msgData)); dst += delta_dst;
    
    memcpy(dst, src_packet->msgData, realMsgLen); dst += realMsgLen;
    buffer_size -= realMsgLen;
    if (buffer_size < sizeof(src_packet->random) + sizeof(src_packet->mac)) {
        LogHelper::log(Warn, "buffer size less than TSPacket random, mac");
        return -3;
    }
    // uint8_t *tempDst = dst;
    memcpy(dst, src_packet->random, sizeof(src_packet->random));
    dst += sizeof(src_packet->random);
    memcpy(dst, src_packet->mac, sizeof(src_packet->mac));
    dst += sizeof(src_packet->mac);

    // fprintf(stderr, "when assemble random: ");
    // for(int i = 0; i < 16; ++i) {
    //     fprintf(stderr, "%u ", (uint8_t)tempDst[i]);
    // }
    // fprintf(stderr, "\nmac: ");
    // for(int i = 0; i < 16; ++i) {
    //     fprintf(stderr, "%u ", (uint8_t)tempDst[16 + i]);
    // }
    // fprintf(stderr, "\n");

    return dst - old_dst;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x5, 0x20(%rsp)
jae 0xd20b
movq 0x20(%rsp), %rdx
movl $0x1, %edi
leaq 0x3aaa(%rip), %rsi      # 0x10ca0
movb $0x0, %al
callq 0x28f0
movq $-0x1, 0x30(%rsp)
jmp 0xd399
movq 0x18(%rsp), %rax
movb (%rax), %cl
movq 0x28(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x28(%rsp)
movb %cl, (%rax)
movq 0x18(%rsp), %rax
movzwl 0x2(%rax), %edi
callq 0x2060
movw %ax, %cx
movq 0x28(%rsp), %rax
movw %cx, (%rax)
movq 0x28(%rsp), %rax
addq $0x2, %rax
movq %rax, 0x28(%rsp)
movq 0x18(%rsp), %rax
movzwl 0x4(%rax), %edi
callq 0x2060
movw %ax, %cx
movq 0x28(%rsp), %rax
movw %cx, (%rax)
movq 0x28(%rsp), %rax
addq $0x2, %rax
movq %rax, 0x28(%rsp)
movq 0x20(%rsp), %rax
subq $0x5, %rax
movq %rax, 0x20(%rsp)
movq 0x18(%rsp), %rax
movzwl 0x4(%rax), %eax
subq $0x10, %rax
subq $0x10, %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
cmpq 0x8(%rsp), %rax
jae 0xd2ce
movq 0x20(%rsp), %rdx
movq 0x8(%rsp), %rcx
movl $0x1, %edi
leaq 0x3a1f(%rip), %rsi      # 0x10cd8
movb $0x0, %al
callq 0x28f0
movq $-0x2, 0x30(%rsp)
jmp 0xd399
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rsi
addq $0x6, %rsi
movq 0x8(%rsp), %rdx
callq 0x2180
movq 0x8(%rsp), %rax
addq 0x28(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x8(%rsp), %rcx
movq 0x20(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x20(%rsp)
cmpq $0x20, 0x20(%rsp)
jae 0xd32d
movl $0x1, %edi
leaq 0x3a12(%rip), %rsi      # 0x10d2d
movb $0x0, %al
callq 0x28f0
movq $-0x3, 0x30(%rsp)
jmp 0xd399
movq 0x28(%rsp), %rax
movq 0x18(%rsp), %rcx
movq 0x10006(%rcx), %rdx
movq %rdx, (%rax)
movq 0x1000e(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq 0x28(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
movq 0x18(%rsp), %rcx
movq 0x10016(%rcx), %rdx
movq %rdx, (%rax)
movq 0x1001e(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq 0x28(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rcx
subq %rcx, %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)