// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %r9, 0x18(%rsp)
movq %rcx, %r12
movq %rsi, %rbp
movq %rdi, %rbx
movq %rsp, %r14
movq %rdx, (%r14)
movq %rdx, 0x10(%rsp)
addq %rdx, %r12
cmpl $0x3, %r8d
leaq 0x9358b(%rip), %rax     # 0x13178c
leaq 0x9358b(%rip), %r15     # 0x131793
cmoveq %rax, %r15
movq 0x18(%rdi), %rdi
movq %r14, %rsi
callq 0xef3a0
movq %rax, %r13
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq %rbp, 0x30(%rsp)
movl (%rbp), %r9d
movl $0x104, %ecx            # imm = 0x104
movq %rax, %rdx
movl $0x1, %r8d
callq 0xefe67
movq %rax, %rbp
movq 0x18(%rax), %rdi
callq 0xf74bd
leaq 0x9371b(%rip), %rsi     # 0x13196c
movq %rbx, %rdi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xe9226
addq (%r14), %r13
movq %r13, (%r14)
movq %rbp, 0x8(%rsp)
movq 0x8(%rbp), %r14
leaq 0xa2466(%rip), %rsi     # 0x1406dd
movq %r14, %rdi
callq 0x181a0
testl %eax, %eax
je 0x9e3e9
movq %rbx, %rbp
movq %r12, %rbx
leaq 0xabeea(%rip), %rsi     # 0x14a17e
movq %r14, %rdi
callq 0x181a0
testl %eax, %eax
je 0x9e41d
leaq 0xabec9(%rip), %rsi     # 0x14a174
movq %r14, %rdi
callq 0x181a0
testl %eax, %eax
je 0x9e4cd
movq %rbp, %rbx
movq 0x18(%rbp), %rdi
movq %rsp, %rsi
callq 0xef3a0
movq %rax, %rbp
leaq 0x936bd(%rip), %rsi     # 0x131991
xorl %r13d, %r13d
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
testl %ebp, %ebp
jne 0x9e33e
movq 0x18(%rbx), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0xef3a0
movq %rax, %r15
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x30(%rsp), %rax
movl (%rax), %r9d
movl $0x104, %ecx            # imm = 0x104
movq %r15, %rdx
movl $0x1, %r8d
callq 0xefe67
movq %rax, %r13
movq 0x18(%rax), %rdi
callq 0xf74bd
leaq 0x93675(%rip), %rsi     # 0x1319a3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
addq %r15, (%r14)
movq 0x18(%rbx), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0xef3a0
movq %rax, %r15
leaq 0x93665(%rip), %rsi     # 0x1319bc
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %r12
movq (%r14), %r14
movl $0x10, %edi
movq %r15, %rsi
callq 0xea792
leaq 0x20(%rsp), %rsi
movq %r12, %rdi
movq %r14, %rdx
movq %rax, %rcx
callq 0xeeee8
testq %r13, %r13
je 0x9e56e
movq 0x8(%rsp), %rbp
movq 0x8(%rbp), %rdi
leaq 0x93631(%rip), %rsi     # 0x1319d5
callq 0x181a0
testl %eax, %eax
jne 0x9e588
cmpb $0x42, 0x20(%rsp)
jne 0x9e588
cmpb $0x4d, 0x21(%rsp)
jne 0x9e588
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
leaq 0x82c7c(%rip), %rcx     # 0x121052
movq %r15, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xf0321
jmp 0x9e5b4
addq $0x8, %r13
movq %rsp, %r15
movq %r13, (%r15)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef3a0
movq %rax, %r14
movq (%r15), %rax
addq %r14, %rax
cmpq %r12, %rax
jle 0x9e47c
xorl %r13d, %r13d
movq 0x8(%rsp), %rbp
xorl %r14d, %r14d
jmp 0x9e5d2
addq $0x8, %r13
movq %rsp, %r14
movq %r13, (%r14)
movq %rbp, %rbx
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xef3a0
movq %rax, %r15
leaq 0x9353f(%rip), %rsi     # 0x13197f
xorl %r13d, %r13d
movq %rbp, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
movq (%r14), %rsi
addq $0x8, %rsi
movq %rsi, (%r14)
movq 0x18(%rbp), %rdi
addq $-0x8, %r15
leaq 0x7dffe(%rip), %rcx     # 0x11c467
movq %r15, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xf0321
addq %r15, (%r14)
jmp 0x9e4b9
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rcx
movq (%rsp), %r8
leaq 0x84d97(%rip), %rsi     # 0x12322f
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %edx, %edx
movq %r14, %r9
callq 0xea5fa
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
addq %r14, (%rsp)
movq 0x8(%rsp), %rbp
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdx
jmp 0x9e5c2
addq $0x8, %r13
leaq 0x20(%rsp), %r14
movq %r13, (%r14)
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xef3a0
movq %rax, %r15
leaq 0x93544(%rip), %rsi     # 0x131a33
xorl %r13d, %r13d
movq %rbp, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
movq (%r14), %rdx
leaq 0x7ed9b(%rip), %rsi     # 0x11d2a4
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbp, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbp), %r12
movq (%r14), %r14
movq %rbx, %rsi
movq %rbp, %rbx
subq %r14, %rsi
movq %r15, %rdi
callq 0xea792
leaq 0x84cae(%rip), %rsi     # 0x1231ed
leaq 0x758d1(%rip), %rdx     # 0x113e17
movq %rbp, %rdi
movq %r12, %rcx
movq %r14, %r8
movq %rax, %r9
callq 0xea66a
movq %rbp, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
xorl %r14d, %r14d
movq 0x8(%rsp), %rbp
jmp 0x9e5d2
leaq 0x93497(%rip), %rsi     # 0x131a0c
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9b27
movq 0x8(%rsp), %rbp
jmp 0x9e5b4
movq 0x18(%rbp), %rdi
callq 0xf74bd
movq %rax, %r14
movq 0x18(%r13), %rdi
callq 0xf74bd
leaq 0x93438(%rip), %rsi     # 0x1319dc
movq %rbx, %rdi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xe9b27
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdx
addq %r15, (%rsp)
movq (%rsp), %rax
subq %rdx, %rax
movq %rax, (%rcx)
movl $0x1, %r14d
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xf00aa
movq %rbx, %rdi
movq %r13, %rsi
callq 0xf00aa
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq