// File: Marm0t[P]dji-phantom3-parser/parser.cpp
// Function: main
int main(int argc, char** argv)
{
    if (argc < 2 )
    {
        cout << "You should provide filename as an argument" << endl;
        return 1;
    }
    int num = 1;
    if(*(char *)&num == 1)
    {
       // cout << "Little-Endian"<<endl;
    }
    else
    {
        cout << "Big-Endian: exit because initial files are little endian and we're too lazy to do it by ourselves"<<endl;
        return 1;
    }
    string filename(argv[1]);
    cout << "File name: " << filename << endl;

    ifstream file(filename.c_str(), ios::binary);
    if (file.fail() || !file.is_open())
    {
        cout << "Cannot open file " << filename << endl;
        return 1;
    }

    char aBuf;
    int packetsNumber = 0;
    vector<Packet> packets01cf;
    streampos aPos;
    //ignore everything till beginning of first packet (0x55 marker)
    while(!file.eof() && !file.fail() && aBuf != 0x55)
    {
        aPos = file.tellg();
        file.read(&aBuf, 1); // read one byte
    }
    if (file.eof() || file.fail())
    {
        cout << "No dji-phantom-3 packets found in your file!" << endl;
        return 1;
    }
    // go back one byte in the file so we start reading packet from the beginning
    file.seekg(aPos);

    cout << GPSPacket::csvHeader();
    // now lets read all packets one by one
    while (!file.eof() && !file.fail()) 
    {
        // packet example:
        //# 0  1  2  3  4  5  6  7 ...
        // 55 28 00 6c 5c 00 70 f7 01 00 70 70 6e 70 71 70 10 70 78 70 62 88 70 70 70 70 70 70 70 70 70 70 70 70 4e 9d 71 70 60 a7
        //  ^ - we start from here and should read LEN bytes
        // LEN = byte#1 (in example above it is 0x28 (hex) = 40 (dec)
        //Packet packet;
        GPSPacket packet;
        file.read(&aBuf, 1);
        if (aBuf != 0x55)
        {
            // this happens very often especially at the beginning of file
            //cout << "Wrong packet start marker: " << hex << (int)aBuf << endl;
            continue;
        }
        packet.push_back(aBuf);

        // reading byte#1 (packet length)
        file.read(&aBuf, 1);
        uint8_t packetLen = aBuf;
        if (packetLen < PACKET_HEADER_LEN)
        {
            cout << "Packet length is too small (" << dec << packetLen << "), packet corrupted" << endl;
            continue;
        }
        packet.push_back(aBuf);
        
        // reading byte#2: must be always 0x00!
        file.read(&aBuf, 1);
        if (aBuf != 0x00)
        {
            //cout << "Corrupted packet header: byte#2 is not 0x00: " << hex << (int)aBuf << endl;
            continue;
        }
        packet.push_back(aBuf);

        // now lets read the rest of the packet accordint to packet len from byte#1
        // note thaat we have already read 3 first bytes (#0, #1 and #3)
        for (uint8_t i=3; i<packetLen; ++i)
        {
            file.read(&aBuf, 1);
            packet.push_back(aBuf);
        }

        // Packet is ready for parsing!
        packet.parseHex();
        if (packet.isValid())
        {
            cout << packet.toString();
            //cout << packet.toCsvString();
        }
        packetsNumber++;
    }

//    cout << "Number of packets found: " << dec << packetsNumber << endl;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x410, %rsp            # imm = 0x410
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x255d
movq 0x8a9a(%rip), %rdi      # 0xafd0
leaq 0x4ac7(%rip), %rsi      # 0x7004
callq 0x21b0
movq %rax, %rdi
movq 0x8a74(%rip), %rsi      # 0xafc0
callq 0x21e0
movl $0x1, -0x4(%rbp)
jmp 0x2cef
movl $0x1, -0x14(%rbp)
movsbl -0x14(%rbp), %eax
cmpl $0x1, %eax
jne 0x256f
jmp 0x259d
movq 0x8a5a(%rip), %rdi      # 0xafd0
leaq 0x4ab2(%rip), %rsi      # 0x702f
callq 0x21b0
movq %rax, %rdi
movq 0x8a34(%rip), %rsi      # 0xafc0
callq 0x21e0
movl $0x1, -0x4(%rbp)
jmp 0x2cef
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x388(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x380(%rbp)
callq 0x2330
movq -0x388(%rbp), %rsi
movq -0x380(%rbp), %rdx
leaq -0x38(%rbp), %rdi
callq 0x2d10
jmp 0x25d5
leaq -0x39(%rbp), %rdi
callq 0x2200
movq 0x89eb(%rip), %rdi      # 0xafd0
leaq 0x4aa5(%rip), %rsi      # 0x7091
callq 0x21b0
movq %rax, -0x390(%rbp)
jmp 0x25fa
movq -0x390(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x2190
movq %rax, -0x398(%rbp)
jmp 0x2613
movq -0x398(%rbp), %rdi
movq 0x899f(%rip), %rsi      # 0xafc0
callq 0x21e0
jmp 0x2628
leaq -0x38(%rbp), %rdi
callq 0x20b0
movq %rax, %rsi
leaq -0x258(%rbp), %rdi
movl $0x4, %edx
callq 0x2350
jmp 0x2647
movq -0x258(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x258(%rbp,%rax), %rdi
callq 0x2030
movb %al, -0x399(%rbp)
jmp 0x2667
movb -0x399(%rbp), %al
testb $0x1, %al
jne 0x2697
jmp 0x2673
leaq -0x258(%rbp), %rdi
callq 0x22c0
movb %al, -0x39a(%rbp)
jmp 0x2687
movb -0x39a(%rbp), %al
testb $0x1, %al
jne 0x2733
jmp 0x2697
movq 0x8932(%rip), %rdi      # 0xafd0
leaq 0x49f8(%rip), %rsi      # 0x709d
callq 0x21b0
movq %rax, -0x3a8(%rbp)
jmp 0x26b3
movq -0x3a8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x2190
movq %rax, -0x3b0(%rbp)
jmp 0x26cc
movq -0x3b0(%rbp), %rdi
movq 0x88e6(%rip), %rsi      # 0xafc0
callq 0x21e0
jmp 0x26e1
movl $0x1, -0x4(%rbp)
movl $0x1, -0x25c(%rbp)
jmp 0x2cc1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x2200
jmp 0x2cfb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x2ce4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x2cd8
movl $0x0, -0x264(%rbp)
leaq -0x280(%rbp), %rdi
callq 0x2dc0
leaq -0x290(%rbp), %rdi
callq 0x2de0
jmp 0x2757
jmp 0x2759
movq -0x258(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x258(%rbp,%rax), %rdi
callq 0x2360
movb %al, -0x3b1(%rbp)
jmp 0x2779
movb -0x3b1(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x3b2(%rbp)
jne 0x27d6
jmp 0x278e
movq -0x258(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x258(%rbp,%rax), %rdi
callq 0x2030
movb %al, -0x3b3(%rbp)
jmp 0x27ae
movb -0x3b3(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x3b2(%rbp)
jne 0x27d6
jmp 0x27c3
movsbl -0x25d(%rbp), %eax
cmpl $0x55, %eax
setne %al
movb %al, -0x3b2(%rbp)
movb -0x3b2(%rbp), %al
testb $0x1, %al
jne 0x27e2
jmp 0x2858
leaq -0x258(%rbp), %rdi
callq 0x2310
movq %rdx, -0x3c8(%rbp)
movq %rax, -0x3c0(%rbp)
jmp 0x27fe
movq -0x3c8(%rbp), %rax
movq -0x3c0(%rbp), %rcx
movq %rcx, -0x2a0(%rbp)
movq %rax, -0x298(%rbp)
movups -0x2a0(%rbp), %xmm0
movaps %xmm0, -0x290(%rbp)
leaq -0x258(%rbp), %rdi
leaq -0x25d(%rbp), %rsi
movl $0x1, %edx
callq 0x2250
jmp 0x2842
jmp 0x2759
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x2cb3
movq -0x258(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x258(%rbp,%rax), %rdi
callq 0x2360
movb %al, -0x3c9(%rbp)
jmp 0x2878
movb -0x3c9(%rbp), %al
testb $0x1, %al
jne 0x28b0
jmp 0x2884
movq -0x258(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x258(%rbp,%rax), %rdi
callq 0x2030
movb %al, -0x3ca(%rbp)
jmp 0x28a4
movb -0x3ca(%rbp), %al
testb $0x1, %al
jne 0x28b0
jmp 0x28f7
movq 0x8719(%rip), %rdi      # 0xafd0
leaq 0x47f1(%rip), %rsi      # 0x70af
callq 0x21b0
movq %rax, -0x3d8(%rbp)
jmp 0x28cc
movq -0x3d8(%rbp), %rdi
movq 0x86e6(%rip), %rsi      # 0xafc0
callq 0x21e0
jmp 0x28e1
movl $0x1, -0x4(%rbp)
movl $0x1, -0x25c(%rbp)
jmp 0x2ca5
movups -0x290(%rbp), %xmm0
movaps %xmm0, -0x2b0(%rbp)
movq -0x2b0(%rbp), %rsi
movq -0x2a8(%rbp), %rdx
leaq -0x258(%rbp), %rdi
callq 0x2260
jmp 0x2921
leaq -0x2d0(%rbp), %rdi
callq 0x6200
jmp 0x292f
movq 0x869a(%rip), %rdi      # 0xafd0
leaq -0x2d0(%rbp), %rsi
callq 0x2190
jmp 0x2944
leaq -0x2d0(%rbp), %rdi
callq 0x20f0
movq -0x258(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x258(%rbp,%rax), %rdi
callq 0x2360
movb %al, -0x3d9(%rbp)
jmp 0x2970
movb -0x3d9(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x3da(%rbp)
jne 0x29b3
jmp 0x2985
movq -0x258(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x258(%rbp,%rax), %rdi
callq 0x2030
movb %al, -0x3db(%rbp)
jmp 0x29a5
movb -0x3db(%rbp), %al
xorb $-0x1, %al
movb %al, -0x3da(%rbp)
movb -0x3da(%rbp), %al
testb $0x1, %al
jne 0x29c2
jmp 0x2c94
leaq -0x350(%rbp), %rdi
callq 0x2e10
jmp 0x29d0
leaq -0x258(%rbp), %rdi
leaq -0x25d(%rbp), %rsi
movl $0x1, %edx
callq 0x2250
jmp 0x29ea
movsbl -0x25d(%rbp), %eax
cmpl $0x55, %eax
je 0x2a33
movl $0x4, -0x25c(%rbp)
jmp 0x2c64
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x2d0(%rbp), %rdi
callq 0x20f0
jmp 0x2cb3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x2c86
movsbl -0x25d(%rbp), %esi
leaq -0x350(%rbp), %rdi
callq 0x2e50
jmp 0x2a48
leaq -0x258(%rbp), %rdi
leaq -0x25d(%rbp), %rsi
movl $0x1, %edx
callq 0x2250
jmp 0x2a62
movb -0x25d(%rbp), %al
movb %al, -0x351(%rbp)
movzbl -0x351(%rbp), %eax
cmpl $0xa, %eax
jge 0x2b12
movq 0x854b(%rip), %rdi      # 0xafd0
leaq 0x4650(%rip), %rsi      # 0x70dc
callq 0x21b0
movq %rax, -0x3e8(%rbp)
jmp 0x2a9a
movq -0x3e8(%rbp), %rdi
leaq 0x3d8(%rip), %rsi       # 0x2e80
callq 0x2080
movq %rax, -0x3f0(%rbp)
jmp 0x2ab6
movq -0x3f0(%rbp), %rdi
movzbl -0x351(%rbp), %esi
callq 0x2290
movq %rax, -0x3f8(%rbp)
jmp 0x2ad2
movq -0x3f8(%rbp), %rdi
leaq 0x4619(%rip), %rsi      # 0x70f9
callq 0x21b0
movq %rax, -0x400(%rbp)
jmp 0x2aee
movq -0x400(%rbp), %rdi
movq 0x84c4(%rip), %rsi      # 0xafc0
callq 0x21e0
jmp 0x2b03
movl $0x4, -0x25c(%rbp)
jmp 0x2c64
movsbl -0x25d(%rbp), %esi
leaq -0x350(%rbp), %rdi
callq 0x2e50
jmp 0x2b27
leaq -0x258(%rbp), %rdi
leaq -0x25d(%rbp), %rsi
movl $0x1, %edx
callq 0x2250
jmp 0x2b41
movsbl -0x25d(%rbp), %eax
cmpl $0x0, %eax
je 0x2b5c
movl $0x4, -0x25c(%rbp)
jmp 0x2c64
movsbl -0x25d(%rbp), %esi
leaq -0x350(%rbp), %rdi
callq 0x2e50
jmp 0x2b71
movb $0x3, -0x352(%rbp)
movzbl -0x352(%rbp), %eax
movzbl -0x351(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2bcb
leaq -0x258(%rbp), %rdi
leaq -0x25d(%rbp), %rsi
movl $0x1, %edx
callq 0x2250
jmp 0x2ba4
movsbl -0x25d(%rbp), %esi
leaq -0x350(%rbp), %rdi
callq 0x2e50
jmp 0x2bb9
jmp 0x2bbb
movb -0x352(%rbp), %al
addb $0x1, %al
movb %al, -0x352(%rbp)
jmp 0x2b78
leaq -0x350(%rbp), %rdi
callq 0x4fb0
jmp 0x2bd9
leaq -0x350(%rbp), %rdi
callq 0x2eb0
movb %al, -0x401(%rbp)
jmp 0x2bed
movb -0x401(%rbp), %al
testb $0x1, %al
jne 0x2bf9
jmp 0x2c4b
leaq -0x378(%rbp), %rdi
leaq -0x350(%rbp), %rsi
callq 0x5110
jmp 0x2c0e
movq 0x83bb(%rip), %rdi      # 0xafd0
leaq -0x378(%rbp), %rsi
callq 0x2190
jmp 0x2c23
leaq -0x378(%rbp), %rdi
callq 0x20f0
jmp 0x2c4b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x378(%rbp), %rdi
callq 0x20f0
jmp 0x2c86
movl -0x264(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x264(%rbp)
movl $0x0, -0x25c(%rbp)
leaq -0x350(%rbp), %rdi
callq 0x2ed0
movl -0x25c(%rbp), %eax
testl %eax, %eax
je 0x2c81
jmp 0x2c7c
jmp 0x2950
jmp 0x2950
leaq -0x350(%rbp), %rdi
callq 0x2ed0
jmp 0x2cb3
movl $0x0, -0x4(%rbp)
movl $0x1, -0x25c(%rbp)
leaq -0x280(%rbp), %rdi
callq 0x2ef0
jmp 0x2cc1
leaq -0x280(%rbp), %rdi
callq 0x2ef0
jmp 0x2cd8
leaq -0x258(%rbp), %rdi
callq 0x2060
leaq -0x38(%rbp), %rdi
callq 0x20f0
jmp 0x2cef
leaq -0x258(%rbp), %rdi
callq 0x2060
leaq -0x38(%rbp), %rdi
callq 0x20f0
jmp 0x2cfb
movl -0x4(%rbp), %eax
addq $0x410, %rsp            # imm = 0x410
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x2320
nopw %cs:(%rax,%rax)
nop