// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil.c
// Function: fmtutil_get_bmpinfo
int fmtutil_get_bmpinfo(deark *c, dbuf *f, struct de_bmpinfo *bi, i64 pos,
	i64 len, unsigned int flags)
{
	i64 fhs; // file header size
	i64 bmih_pos;
	struct de_fourcc cmpr4cc;
	char cmprname_dbgstr[80];

	de_zeromem(bi, sizeof(struct de_bmpinfo));
	de_zeromem(&cmpr4cc, sizeof(struct de_fourcc));

	fhs = (flags & DE_BMPINFO_HAS_FILEHEADER) ? 14 : 0;

	if(fhs+len < 16) return 0;

	if(fhs) {
		if(flags & DE_BMPINFO_HAS_HOTSPOT) {
			bi->hotspot_x = (int)dbuf_getu16le(f, pos+6);
			bi->hotspot_y = (int)dbuf_getu16le(f, pos+8);
			de_dbg(c, "hotspot: (%d,%d)", bi->hotspot_x, bi->hotspot_y);
		}

		bi->bitsoffset = dbuf_getu32le(f, pos+10);
		de_dbg(c, "bits offset: %d", (int)bi->bitsoffset);
	}

	bmih_pos = pos + fhs;

	bi->infohdrsize = dbuf_getu32le(f, bmih_pos);

	if(bi->infohdrsize==0x474e5089 && (flags & DE_BMPINFO_ICO_FORMAT)) {
		// We don't examine PNG-formatted icons, but we can identify them.
		bi->infohdrsize = 0;
		bi->file_format = DE_BMPINFO_FMT_PNG;
		return 1;
	}

	de_dbg(c, "info header size: %d", (int)bi->infohdrsize);

	if(bi->infohdrsize==12) {
		bi->bytes_per_pal_entry = 3;
		bi->width = dbuf_getu16le(f, bmih_pos+4);
		bi->height = dbuf_getu16le(f, bmih_pos+6);
		bi->bitcount = dbuf_getu16le(f, bmih_pos+10);
	}
	else if(bi->infohdrsize>=16 && bi->infohdrsize<=124) {
		bi->bytes_per_pal_entry = 4;
		bi->width = dbuf_getu32le(f, bmih_pos+4);
		bi->height = dbuf_geti32le(f, bmih_pos+8);
		if(bi->height<0) {
			bi->is_topdown = 1;
			bi->height = -bi->height;
		}
		bi->bitcount = dbuf_getu16le(f, bmih_pos+14);
		if(bi->infohdrsize>=20) {
			bi->compression_field = (u32)dbuf_getu32le(f, bmih_pos+16);
			if(flags & DE_BMPINFO_CMPR_IS_4CC) {
				dbuf_read_fourcc(f, bmih_pos+16, &cmpr4cc, 4, 0x0);
			}
		}
		if(bi->infohdrsize>=24) {
			bi->sizeImage_field = dbuf_getu32le(f, bmih_pos+20);
		}
		if(bi->infohdrsize>=36) {
			bi->pal_entries = dbuf_getu32le(f, bmih_pos+32);
		}
	}
	else {
		return 0;
	}

	if(flags & DE_BMPINFO_ICO_FORMAT) bi->height /= 2;

	if(bi->bitcount>=1 && bi->bitcount<=8) {
		if(bi->pal_entries==0) {
			bi->pal_entries = de_pow2(bi->bitcount);
		}
		// I think the NumColors field (in icons) is supposed to be the maximum number of
		// colors implied by the bit depth, not the number of colors in the palette.
		bi->num_colors = de_pow2(bi->bitcount);
	}
	else {
		// An arbitrary value. All that matters is that it's >=256.
		bi->num_colors = 16777216;
	}

	de_dbg_dimensions(c, bi->width, bi->height);
	de_dbg(c, "bit count: %d", (int)bi->bitcount);

	if((flags & DE_BMPINFO_CMPR_IS_4CC) && (bi->compression_field>0xffff)) {
		de_snprintf(cmprname_dbgstr, sizeof(cmprname_dbgstr), "'%s'", cmpr4cc.id_dbgstr);
	}
	else {
		fmtutil_get_bmp_compression_name(bi->compression_field,
			cmprname_dbgstr, sizeof(cmprname_dbgstr), 0);
	}
	de_dbg(c, "compression: %u (%s)", (unsigned int)bi->compression_field, cmprname_dbgstr);

	if(bi->sizeImage_field!=0) {
		de_dbg(c, "sizeImage: %u", (unsigned int)bi->sizeImage_field);
	}

	de_dbg(c, "palette entries: %u", (unsigned int)bi->pal_entries);
	if(bi->pal_entries>256 && bi->bitcount>8) {
		de_warn(c, "Ignoring bad palette size (%u entries)", (unsigned int)bi->pal_entries);
		bi->pal_entries = 0;
	}

	bi->pal_bytes = bi->bytes_per_pal_entry*bi->pal_entries;
	bi->size_of_headers_and_pal = fhs + bi->infohdrsize + bi->pal_bytes;

	// FIXME: cmpr type 3 doesn't always mean BITFIELDS
	if(bi->compression_field==3) {
		bi->size_of_headers_and_pal += 12; // BITFIELDS
	}

	bi->is_compressed = !((bi->compression_field==0) ||
		(bi->compression_field==3 && bi->bitcount>1));

	if(flags & DE_BMPINFO_NOERR) {
		if(!de_good_image_dimensions_noerr(c, bi->width, bi->height)) {
			return 0;
		}
	}
	else {
		if(!de_good_image_dimensions(c, bi->width, bi->height)) {
			return 0;
		}
	}

	// TODO: This needs work, to decide how to handle compressed images.
	// TODO: What about BI_BITFIELDS images?
	if(bi->compression_field==0) {
		// Try to figure out the true size of the resource, minus any padding.

		bi->rowspan = ((bi->bitcount*bi->width +31)/32)*4;
		bi->foreground_size = bi->rowspan * bi->height;
		de_dbg(c, "foreground size: %d", (int)bi->foreground_size);

		if(flags & DE_BMPINFO_ICO_FORMAT) {
			bi->mask_rowspan = ((bi->width +31)/32)*4;
			bi->mask_size = bi->mask_rowspan * bi->height;
			de_dbg(c, "mask size: %d", (int)bi->mask_size);
		}
		else {
			bi->mask_size = 0;
		}

		bi->total_size = bi->size_of_headers_and_pal + bi->foreground_size + bi->mask_size;
	}
	else {
		// Don't try to figure out the true size of compressed or other unusual images.
		bi->total_size = len;
	}

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq -0x20(%rbp), %rdi
xorl %esi, %esi
movl $0xa0, %edx
callq 0x19140
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x19140
movl -0x34(%rbp), %edx
andl $0x1, %edx
xorl %eax, %eax
movl $0xe, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
cltq
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq -0x30(%rbp), %rax
cmpq $0x10, %rax
jge 0x172de6
movl $0x0, -0x4(%rbp)
jmp 0x1734c5
cmpq $0x0, -0x40(%rbp)
je 0x172e88
movl -0x34(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x172e50
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x6, %rsi
callq 0x165280
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x8, %rsi
callq 0x165280
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x20(%rbp), %rax
movl 0x8(%rax), %ecx
leaq 0x47bb8(%rip), %rsi     # 0x1baa01
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0xa, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %edx
leaq 0x63009(%rip), %rsi     # 0x1d5e8a
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x165540
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x20(%rbp), %rax
cmpq $0x474e5089, 0x18(%rax) # imm = 0x474E5089
jne 0x172ee7
movl -0x34(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x172ee7
movq -0x20(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x20(%rbp), %rax
movl $0x1, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x1734c5
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0x62f9e(%rip), %rsi     # 0x1d5e9a
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
cmpq $0xc, 0x18(%rax)
jne 0x172f73
movq -0x20(%rbp), %rax
movq $0x3, 0x48(%rax)
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0x4, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0x6, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0xa, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0x1730c9
movq -0x20(%rbp), %rax
cmpq $0x10, 0x18(%rax)
jl 0x1730bb
movq -0x20(%rbp), %rax
cmpq $0x7c, 0x18(%rax)
jg 0x1730bb
movq -0x20(%rbp), %rax
movq $0x4, 0x48(%rax)
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0x4, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0x8, %rsi
callq 0x165600
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jge 0x173000
movq -0x20(%rbp), %rax
movl $0x1, 0x9c(%rax)
movq -0x20(%rbp), %rax
xorl %ecx, %ecx
subq 0x28(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0xe, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x20(%rbp), %rax
cmpq $0x14, 0x18(%rax)
jl 0x17306b
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0x10, %rsi
callq 0x165540
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x38(%rax)
movl -0x34(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x173069
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0x10, %rsi
leaq -0x78(%rbp), %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0x169860
jmp 0x17306b
movq -0x20(%rbp), %rax
cmpq $0x18, 0x18(%rax)
jl 0x173092
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0x14, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x20(%rbp), %rax
cmpq $0x24, 0x18(%rax)
jl 0x1730b9
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0x20, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x50(%rax)
jmp 0x1730c7
movl $0x0, -0x4(%rbp)
jmp 0x1734c5
jmp 0x1730c9
movl -0x34(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x1730fb
movq -0x20(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq 0x28(%rax), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0xd8(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x20(%rbp), %rax
cmpq $0x1, 0x30(%rax)
jl 0x17314e
movq -0x20(%rbp), %rax
cmpq $0x8, 0x30(%rax)
jg 0x17314e
movq -0x20(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jne 0x173134
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x15ce50
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x15ce50
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x58(%rax)
jmp 0x17315a
movq -0x20(%rbp), %rax
movq $0x1000000, 0x58(%rax)  # imm = 0x1000000
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x15bba0
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movl %eax, %edx
leaq 0x53b5d(%rip), %rsi     # 0x1c6ce5
movb $0x0, %al
callq 0x15b0d0
movl -0x34(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x1731cb
movq -0x20(%rbp), %rax
cmpl $0xffff, 0x38(%rax)     # imm = 0xFFFF
jbe 0x1731cb
leaq -0xd0(%rbp), %rdi
leaq -0x78(%rbp), %rcx
addq $0x10, %rcx
movl $0x50, %esi
leaq 0x52c78(%rip), %rdx     # 0x1c5e3a
movb $0x0, %al
callq 0x15a980
jmp 0x1731e5
movq -0x20(%rbp), %rax
movl 0x38(%rax), %edi
leaq -0xd0(%rbp), %rsi
movl $0x50, %edx
xorl %ecx, %ecx
callq 0x172ca0
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x38(%rax), %edx
leaq -0xd0(%rbp), %rcx
leaq 0x5e4e1(%rip), %rsi     # 0x1d16df
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x17322c
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, %edx
leaq 0x62c8a(%rip), %rsi     # 0x1d5eaf
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rax
movl %eax, %edx
leaq 0x31ba5(%rip), %rsi     # 0x1a4de6
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
cmpq $0x100, 0x50(%rax)      # imm = 0x100
jle 0x173289
movq -0x20(%rbp), %rax
cmpq $0x8, 0x30(%rax)
jle 0x173289
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rax
movl %eax, %edx
leaq 0x62c47(%rip), %rsi     # 0x1d5ebd
movb $0x0, %al
callq 0x15bc10
movq -0x20(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rcx
movq -0x20(%rbp), %rax
imulq 0x50(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x40(%rbp), %rcx
movq -0x20(%rbp), %rax
addq 0x18(%rax), %rcx
movq -0x20(%rbp), %rax
addq 0x80(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x20(%rbp), %rax
cmpl $0x3, 0x38(%rax)
jne 0x1732e7
movq -0x20(%rbp), %rax
movq 0x88(%rax), %rcx
addq $0xc, %rcx
movq %rcx, 0x88(%rax)
movq -0x20(%rbp), %rcx
movb $0x1, %al
cmpl $0x0, 0x38(%rcx)
movb %al, -0xd9(%rbp)
je 0x173329
movq -0x20(%rbp), %rcx
xorl %eax, %eax
cmpl $0x3, 0x38(%rcx)
movb %al, -0xda(%rbp)
jne 0x17331d
movq -0x20(%rbp), %rax
cmpq $0x1, 0x30(%rax)
setg %al
movb %al, -0xda(%rbp)
movb -0xda(%rbp), %al
movb %al, -0xd9(%rbp)
movb -0xd9(%rbp), %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x98(%rax)
movl -0x34(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x173377
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x16a7f0
cmpl $0x0, %eax
jne 0x173375
movl $0x0, -0x4(%rbp)
jmp 0x1734c5
jmp 0x1733a3
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x1733a1
movl $0x0, -0x4(%rbp)
jmp 0x1734c5
jmp 0x1733a3
movq -0x20(%rbp), %rax
cmpl $0x0, 0x38(%rax)
jne 0x1734af
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rcx
imulq 0x20(%rcx), %rax
addq $0x1f, %rax
movl $0x20, %ecx
cqto
idivq %rcx
movq %rax, %rcx
shlq $0x2, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x20(%rbp), %rax
imulq 0x28(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rax
movl %eax, %edx
leaq 0x62ad7(%rip), %rsi     # 0x1d5ee4
movb $0x0, %al
callq 0x15b0d0
movl -0x34(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x17347b
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
addq $0x1f, %rax
movl $0x20, %ecx
cqto
idivq %rcx
movq %rax, %rcx
shlq $0x2, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x20(%rbp), %rax
movq 0x70(%rax), %rcx
movq -0x20(%rbp), %rax
imulq 0x28(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
movl %eax, %edx
leaq 0x62a86(%rip), %rsi     # 0x1d5ef8
movb $0x0, %al
callq 0x15b0d0
jmp 0x173487
movq -0x20(%rbp), %rax
movq $0x0, 0x78(%rax)
movq -0x20(%rbp), %rax
movq 0x88(%rax), %rcx
movq -0x20(%rbp), %rax
addq 0x68(%rax), %rcx
movq -0x20(%rbp), %rax
addq 0x78(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x90(%rax)
jmp 0x1734be
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x90(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)