// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/ole1.c
// Function: do_ole_object_presentation
static int do_ole_object_presentation(deark *c, lctx *d,
	i64 pos1, i64 len, unsigned int formatID, i64 *bytes_consumed)
{
	i64 pos = pos1;
	i64 endpos = pos1 + len;
	i64 dlen;
	i64 stringlen;
	struct de_stringreaderdata *classname_srd = NULL;
	struct de_stringreaderdata *clipfmtname_srd = NULL;
	const char *name;
	int retval = 0;

	name = (formatID==3)?"static":"presentation";
	stringlen = de_getu32le_p(&pos);
	classname_srd = dbuf_read_string(c->infile, pos, stringlen, 260, DE_CONVFLAG_STOP_AT_NUL,
		d->input_encoding);
	de_dbg(c, "%s ClassName: \"%s\"", name, ucstring_getpsz(classname_srd->str));
	pos += stringlen;

	// TODO: Better handle the fields between ClassName and PresentationData
	// (and maybe after PresentationData?).

	if(!de_strcmp(classname_srd->sz, "DIB")) {
		pos += 8;
		dlen = de_getu32le_p(&pos);
		if(pos + dlen > endpos) goto done;
		de_dbg_indent(c, 1);
		de_run_module_by_id_on_slice(c, "dib", NULL, c->infile, pos, dlen);
		de_dbg_indent(c, -1);
		pos += dlen;
	}
	else if(!de_strcmp(classname_srd->sz, "METAFILEPICT")) {
		pos += 8; // ??
		dlen = de_getu32le_p(&pos);
		de_dbg(c, "metafile size: %d", (int)dlen); // Includes "mfp", apparently
		pos += 8; // "mfp" struct
		dbuf_create_file_from_slice(c->infile, pos, dlen-8, "wmf", NULL, 0);
		pos += dlen-8;
	}
	else if(!de_strcmp(classname_srd->sz, "BITMAP")) {
		do_static_bitmap(c, d, pos, endpos-pos);
		goto done; // FIXME, calculate length
	}
	else {
		u32 clipfmt;
		i64 clp_data_size;
		u8 buf[16];

		// This is a GenericPresentationObject, a.k.a. clipboard format,
		// either a StandardClipboardFormatPresentationObject
		// or a RegisteredClipboardFormatPresentationObject.
		clipfmt = (u32)de_getu32le_p(&pos);
		de_dbg(c, "clipboard fmt: %u", (unsigned int)clipfmt);

		if(clipfmt==0) {
			stringlen = de_getu32le_p(&pos);
			clipfmtname_srd = dbuf_read_string(c->infile, pos, stringlen, 260, DE_CONVFLAG_STOP_AT_NUL,
				d->input_encoding);
			de_dbg(c, "clipboard fmt name: \"%s\"", ucstring_getpsz(clipfmtname_srd->str));
			pos += stringlen;
		}

		clp_data_size = de_getu32le_p(&pos);
		de_dbg(c, "clipboard data size: %"I64_FMT, clp_data_size);

		de_read(buf, pos, de_min_int((i64)sizeof(buf), clp_data_size));

		if(clipfmtname_srd) {
			if(!de_strcmp(classname_srd->sz, "PBrush") &&
				buf[0]=='B' && buf[1]=='M')
			{
				dbuf_create_file_from_slice(c->infile, pos, clp_data_size, "bmp", NULL, 0);
			}
			else {
				de_warn(c, "OLE clipboard type (\"%s\"/\"%s\") is not supported",
					ucstring_getpsz(classname_srd->str),
					ucstring_getpsz(clipfmtname_srd->str));
			}
		}
		else {
			de_warn(c, "OLE clipboard type %u is not supported", (unsigned int)clipfmt);
		}

		pos += clp_data_size;
	}

	*bytes_consumed = pos-pos1;
	retval = 1;

done:
	de_destroy_stringreaderdata(c, classname_srd);
	de_destroy_stringreaderdata(c, clipfmtname_srd);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, 0x18(%rsp)
movq %rcx, %r15
movq %rsi, %r13
movq %rdi, %rbx
leaq 0x8(%rsp), %r14
movq %rdx, (%r14)
movq %rdx, 0x10(%rsp)
addq %rdx, %r15
cmpl $0x3, %r8d
leaq 0x8ca18(%rip), %rax     # 0x124824
leaq 0x8ca18(%rip), %rbp     # 0x12482b
cmoveq %rax, %rbp
movq 0x18(%rdi), %rdi
movq %r14, %rsi
callq 0xe40bf
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq %r13, 0x40(%rsp)
movl (%r13), %r9d
pushq $0x1
popq %r8
movl $0x104, %ecx            # imm = 0x104
movq %rax, %rdx
callq 0xe495c
movq %rax, %r13
movq 0x18(%rax), %rdi
callq 0xeb7e4
leaq 0x8cbaa(%rip), %rsi     # 0x124a04
movq %rbx, %rdi
movq %rbp, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xde896
addq (%r14), %r12
movq %r12, (%r14)
movq 0x8(%r13), %r14
leaq 0x9b8f2(%rip), %rsi     # 0x13376d
movq %r14, %rdi
callq 0x181a0
testl %eax, %eax
je 0x97eef
leaq 0xa53d0(%rip), %rsi     # 0x13d25e
movq %r14, %rdi
callq 0x181a0
testl %eax, %eax
je 0x97f20
leaq 0xa53af(%rip), %rsi     # 0x13d254
movq %r14, %rdi
callq 0x181a0
testl %eax, %eax
je 0x97fd3
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xe40bf
movq %rax, %r14
leaq 0x8cb5c(%rip), %rsi     # 0x124a29
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
movq %r14, 0x38(%rsp)
testl %r14d, %r14d
je 0x98071
xorl %ebp, %ebp
jmp 0x980c8
addq $0x8, %r12
leaq 0x8(%rsp), %r14
movq %r12, (%r14)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe40bf
movq %rax, %rbp
movq (%r14), %rax
addq %rbp, %rax
cmpq %r15, %rax
jle 0x97f80
xorl %r14d, %r14d
xorl %r12d, %r12d
jmp 0x981c5
addq $0x8, %r12
leaq 0x8(%rsp), %r14
movq %r12, (%r14)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe40bf
movq %rax, %rbp
leaq 0x8cad5(%rip), %rsi     # 0x124a17
xorl %r12d, %r12d
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
movq (%r14), %rsi
addq $0x8, %rsi
movq %rsi, (%r14)
movq 0x18(%rbx), %rdi
addq $-0x8, %rbp
leaq 0x7751d(%rip), %rcx     # 0x10f487
movq %rbp, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe4e0b
addq (%r14), %rbp
movq %rbp, (%r14)
jmp 0x97fc0
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rcx
movq 0x8(%rsp), %r8
leaq 0x7e2b4(%rip), %rsi     # 0x11624f
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %edx, %edx
movq %rbp, %r9
callq 0xdf9b8
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
addq 0x8(%rsp), %rbp
movq %rbp, 0x8(%rsp)
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
pushq $0x1
popq %r14
jmp 0x981bf
addq $0x8, %r12
leaq 0x20(%rsp), %r14
movq %r12, (%r14)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe40bf
movq %rax, %r12
leaq 0x8cad6(%rip), %rsi     # 0x124acb
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
movq (%r14), %rdx
leaq 0x782b8(%rip), %rsi     # 0x1102c4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %r13, %rbp
movq 0x18(%rbx), %r13
movq (%r14), %r14
subq %r14, %r15
movq %r12, %rdi
xorl %r12d, %r12d
movq %r15, %rsi
callq 0xdfb45
leaq 0x7e1ca(%rip), %rsi     # 0x11620d
leaq 0x6ee1d(%rip), %rdx     # 0x106e67
movq %rbx, %rdi
movq %r13, %rcx
movq %rbp, %r13
movq %r14, %r8
movq %rax, %r9
callq 0xdfa28
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
xorl %r14d, %r14d
jmp 0x981c5
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0xe40bf
movq %rax, %r15
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x40(%rsp), %rax
movl (%rax), %r9d
pushq $0x1
popq %r8
movl $0x104, %ecx            # imm = 0x104
movq %r15, %rdx
callq 0xe495c
movq %rax, %rbp
movq 0x18(%rax), %rdi
callq 0xeb7e4
leaq 0x8c983(%rip), %rsi     # 0x124a3b
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
addq %r15, (%r14)
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0xe40bf
movq %rax, %r15
leaq 0x8c971(%rip), %rsi     # 0x124a54
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %r12
movq (%r14), %r14
pushq $0x10
popq %rdi
movq %r15, %rsi
callq 0xdfb45
leaq 0x20(%rsp), %rsi
movq %r12, %rdi
movq %r14, %rdx
movq %rax, %rcx
callq 0xe3cb7
testq %rbp, %rbp
movq %rbp, %r12
je 0x9815f
movq 0x8(%r13), %rdi
leaq 0x8c945(%rip), %rsi     # 0x124a6d
callq 0x181a0
testl %eax, %eax
jne 0x98177
cmpb $0x42, 0x20(%rsp)
jne 0x98177
cmpb $0x4d, 0x21(%rsp)
jne 0x98177
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
leaq 0x7bf23(%rip), %rcx     # 0x114072
movq %r15, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe4e0b
jmp 0x981a4
leaq 0x8c93e(%rip), %rsi     # 0x124aa4
movq %rbx, %rdi
movq 0x38(%rsp), %rdx
xorl %eax, %eax
callq 0xdf05a
jmp 0x981a4
movq 0x18(%r13), %rdi
callq 0xeb7e4
movq %rax, %r14
movq 0x18(%r12), %rdi
callq 0xeb7e4
leaq 0x8c8e0(%rip), %rsi     # 0x124a74
movq %rbx, %rdi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xdf05a
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
pushq $0x1
popq %r14
movq 0x8(%rsp), %rbp
addq %r15, %rbp
movq %rbp, 0x8(%rsp)
subq %rcx, %rbp
movq %rbp, (%rax)
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe4b94
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe4b94
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq