// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/emf.c
// Function: do_identify_and_extract_compressed_bitmap
static void do_identify_and_extract_compressed_bitmap(deark *c, lctx *d,
	i64 pos, i64 len)
{
	i64 nbytes_to_extract;
	i64 foundpos;
	struct fmtutil_fmtid_ctx *idctx = NULL;

	if(len<=0) goto done;
	if(pos+len > c->infile->len) goto done;
	nbytes_to_extract = len; // default

	// Having dived six layers of abstraction deep into EMF+ format,
	// we finally come to an actual embedded image.
	// And we *still* don't know what format it's in! We apparently have to
	// sniff the data and make a guess.

	de_dbg(c, "bitmap at %"I64_FMT", padded_len=%"I64_FMT, pos, len);

	idctx = de_malloc(c, sizeof(struct fmtutil_fmtid_ctx));
	idctx->inf = c->infile;
	idctx->inf_pos = pos;
	idctx->inf_len = len;
	idctx->mode = FMTUTIL_FMTIDMODE_ALL_IMG;
	fmtutil_fmtid(c, idctx);

	// Expecting PNG, JPEG, GIF, TIFF
	if(!idctx->fmtid) {
		de_warn(c, "Unidentified bitmap format at %"I64_FMT, pos);
		goto done;
	}

	if(idctx->fmtid==FMTUTIL_FMTID_PNG) {
		// The 'len' field includes 0 to 3 padding bytes, which we want to
		// remove. All PNG files end with ae 42 60 82.
		if(dbuf_search_byte(c->infile, '\x82', pos+len-4, 4, &foundpos)) {
			nbytes_to_extract = foundpos + 1 - pos;
		}
	}
	// TODO: For JPEG and GIF, try to detect the true end of file.

	if(nbytes_to_extract<=0) return;
	dbuf_create_file_from_slice(c->infile, pos, nbytes_to_extract, idctx->ext_sz, NULL, 0);

done:
	de_free(c, idctx);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x38(%rbp)
cmpq $0x0, -0x20(%rbp)
jg 0x5c22c
jmp 0x5c34f
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x5c247
jmp 0x5c34f
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq 0x14b756(%rip), %rsi    # 0x1a79b8
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x78, %esi
callq 0x15b780
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x38(%rbp), %rax
movb $0x1, (%rax)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x176960
movq -0x38(%rbp), %rax
cmpl $0x0, 0x68(%rax)
jne 0x5c2d9
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x14b706(%rip), %rsi    # 0x1a79d6
movb $0x0, %al
callq 0x15bc10
jmp 0x5c34f
movq -0x38(%rbp), %rax
cmpl $0xc, 0x68(%rax)
jne 0x5c321
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rdx
addq -0x20(%rbp), %rdx
subq $0x4, %rdx
movl $0x82, %esi
movl $0x4, %ecx
leaq -0x30(%rbp), %r8
callq 0x1667a0
cmpl $0x0, %eax
je 0x5c31f
movq -0x30(%rbp), %rax
addq $0x1, %rax
subq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x5c321
cmpq $0x0, -0x28(%rbp)
jg 0x5c32a
jmp 0x5c35c
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x38(%rbp), %rcx
addq $0x6c, %rcx
xorl %eax, %eax
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x166be0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x15b820
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)