// File: timothyqiu[P]ssoaag/bitmap.cc
// Function: Bitmap::CheckFormat(char const*, unsigned long)
bool Bitmap::CheckFormat(char const *data, size_t size)
{
    // header field signatures
    if (!HasBitmapSignature(data, size)) {
        this->error_description_ = "No bitmap signature";
        return false;
    }

    // there must be a file header
    if (size < sizeof(BITMAPFILEHEADER)) {
        this->error_description_ = "File size too small";
        return false;
    }

    auto *bf = reinterpret_cast<BITMAPFILEHEADER const *>(data);

    // total file size
    if (bf->bfSize != size) {
        this->error_description_ = "File size and data length do not match";
        return false;
    }

    auto *bi = reinterpret_cast<BITMAPINFOHEADER const *>(bf + 1);
    auto *bc = reinterpret_cast<BITMAPCOREHEADER const *>(bf + 1);

    if (bc->bcSize == sizeof(BITMAPCOREHEADER)) {
        // number of planes must be one
        if (bc->bcPlanes != 1) {
            this->error_description_ = "Number of planes not 1";
            return false;
        }

    } else {

        // number of planes must be one
        if (bi->biPlanes != 1) {
            this->error_description_ = "Number of planes not 1";
            return false;
        }

        // some compression method requires bits per pixel to be a specific value
        if (bi->biCompression == BI_RLE8 && bi->biBitCount != 8) {
            this->error_description_ = "Wrong color depth for RLE 8 compression";
            return false;
        }
        if (bi->biCompression == BI_RLE4 && bi->biBitCount != 4) {
            this->error_description_ = "Wrong color depth for RLE 4 compression";
            return false;
        }
    }

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x4150
testb $0x1, %al
jne 0x400a
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
leaq 0x22f9(%rip), %rsi      # 0x62f5
callq 0x22d0
movb $0x0, -0x1(%rbp)
jmp 0x4142
cmpq $0xe, -0x20(%rbp)
jae 0x402e
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
leaq 0x22e9(%rip), %rsi      # 0x6309
callq 0x22d0
movb $0x0, -0x1(%rbp)
jmp 0x4142
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl 0x2(%rax), %eax
cmpq -0x20(%rbp), %rax
je 0x4060
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
leaq 0x22cb(%rip), %rsi      # 0x631d
callq 0x22d0
movb $0x0, -0x1(%rbp)
jmp 0x4142
movq -0x28(%rbp), %rax
addq $0xe, %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
addq $0xe, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl (%rax), %eax
cmpq $0xc, %rax
jne 0x40b3
movq -0x38(%rbp), %rax
movzwl 0x8(%rax), %eax
cmpl $0x1, %eax
je 0x40ae
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
leaq 0x22a4(%rip), %rsi      # 0x6344
callq 0x22d0
movb $0x0, -0x1(%rbp)
jmp 0x4142
jmp 0x413e
movq -0x30(%rbp), %rax
movzwl 0xc(%rax), %eax
cmpl $0x1, %eax
je 0x40da
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
leaq 0x2275(%rip), %rsi      # 0x6344
callq 0x22d0
movb $0x0, -0x1(%rbp)
jmp 0x4142
movq -0x30(%rbp), %rax
cmpl $0x1, 0x10(%rax)
jne 0x410b
movq -0x30(%rbp), %rax
movzwl 0xe(%rax), %eax
cmpl $0x8, %eax
je 0x410b
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
leaq 0x225b(%rip), %rsi      # 0x635b
callq 0x22d0
movb $0x0, -0x1(%rbp)
jmp 0x4142
movq -0x30(%rbp), %rax
cmpl $0x2, 0x10(%rax)
jne 0x413c
movq -0x30(%rbp), %rax
movzwl 0xe(%rax), %eax
cmpl $0x4, %eax
je 0x413c
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
leaq 0x2252(%rip), %rsi      # 0x6383
callq 0x22d0
movb $0x0, -0x1(%rbp)
jmp 0x4142
jmp 0x413e
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)