// File: Marm0t[P]dji-phantom3-parser/packet.cpp
// Function: Packet::parseHex()
void Packet::parseHex()
{
    if (_packv.size() < PACKET_HEADER_LEN)
    {
        cerr << "Error while parsing message: message to short, size: " << dec <<_packv.size() << endl;
        _valid = false;
        return;
    }

    // byte 1 - packet len
    _packetLen = _packv[1];
    // packet len in byte1 must be the same as _packv vector length
    // otherwise consider that message is corrupted
    if (_packetLen != _packv.size() )
    {
        cerr << "Error while parsing message: message length in packet ("<< dec << (int)_packetLen
             << ") is not equal to number of bytes read from file("<< dec << _packv.size() << ")" << endl;
        _valid = false;
        return;
    }

    // bytes 3 and 4 - packet type (2 bytes)
    char aType[2];
    for (int i = 0; i<2; ++i) aType[i] = _packv[i+3];
    memcpy(&_packetType, aType, 2);

    // bytes 6-9 - message ID (4 bytes)
    char aMsgId[4];
    for (int i = 0; i<4; ++i)
    {
        aMsgId[i] = _packv[i+6];
    }
    memcpy(&_msgId, aMsgId, 4);

    _valid = true;

    // try to convert all data bytes into doubles
    dataToDoubles();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x20(%rbp)
addq $0x8, %rdi
callq 0x3750
cmpq $0xa, %rax
jae 0x4e3e
movq 0x6206(%rip), %rdi      # 0xaff0
leaq 0x23ba(%rip), %rsi      # 0x71ab
callq 0x21b0
movq %rax, %rdi
leaq -0x1f80(%rip), %rsi     # 0x2e80
callq 0x2080
movq -0x20(%rbp), %rdi
movq %rax, -0x28(%rbp)
addq $0x8, %rdi
callq 0x3750
movq -0x28(%rbp), %rdi
movq %rax, %rsi
callq 0x2050
movq %rax, %rdi
movq 0x6194(%rip), %rsi      # 0xafc0
callq 0x21e0
movq -0x20(%rbp), %rax
movb $0x0, 0x28(%rax)
jmp 0x4fa1
movq -0x20(%rbp), %rdi
addq $0x8, %rdi
movl $0x1, %esi
callq 0x6290
movq -0x20(%rbp), %rdi
movb (%rax), %al
movb %al, 0x20(%rdi)
movzbl 0x20(%rdi), %eax
movq %rax, -0x30(%rbp)
addq $0x8, %rdi
callq 0x3750
movq %rax, %rcx
movq -0x30(%rbp), %rax
cmpq %rcx, %rax
je 0x4f12
movq 0x616f(%rip), %rdi      # 0xaff0
leaq 0x2359(%rip), %rsi      # 0x71e1
callq 0x21b0
movq %rax, %rdi
leaq -0x2017(%rip), %rsi     # 0x2e80
callq 0x2080
movq %rax, %rdi
movq -0x20(%rbp), %rax
movzbl 0x20(%rax), %esi
callq 0x2300
movq %rax, %rdi
leaq 0x2363(%rip), %rsi      # 0x7219
callq 0x21b0
movq %rax, %rdi
leaq -0x2045(%rip), %rsi     # 0x2e80
callq 0x2080
movq -0x20(%rbp), %rdi
movq %rax, -0x38(%rbp)
addq $0x8, %rdi
callq 0x3750
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x2050
movq %rax, %rdi
leaq 0x235a(%rip), %rsi      # 0x724b
callq 0x21b0
movq %rax, %rdi
movq 0x60c0(%rip), %rsi      # 0xafc0
callq 0x21e0
movq -0x20(%rbp), %rax
movb $0x0, 0x28(%rax)
jmp 0x4fa1
movl $0x0, -0x10(%rbp)
cmpl $0x2, -0x10(%rbp)
jge 0x4f4a
movq -0x20(%rbp), %rdi
addq $0x8, %rdi
movl -0x10(%rbp), %eax
addl $0x3, %eax
movslq %eax, %rsi
callq 0x6290
movb (%rax), %cl
movslq -0x10(%rbp), %rax
movb %cl, -0xa(%rbp,%rax)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x4f19
movq -0x20(%rbp), %rax
movw -0xa(%rbp), %cx
movw %cx, 0x22(%rax)
movl $0x0, -0x18(%rbp)
cmpl $0x4, -0x18(%rbp)
jge 0x4f8e
movq -0x20(%rbp), %rdi
addq $0x8, %rdi
movl -0x18(%rbp), %eax
addl $0x6, %eax
movslq %eax, %rsi
callq 0x6290
movb (%rax), %cl
movslq -0x18(%rbp), %rax
movb %cl, -0x14(%rbp,%rax)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x4f5d
movq -0x20(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, 0x24(%rdi)
movb $0x1, 0x28(%rdi)
callq 0x48f0
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)