// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/ole1.c
// Function: do_ole_object_embedded
static int do_ole_object_embedded(deark *c, lctx *d,
	i64 pos1, i64 len, int exact_size_known, i64 *bytes_consumed)
{
	i64 pos = pos1;
	i64 stringlen;
	i64 data_len;
	i64 bytes_consumed2 = 0;
	int ret;
	int recognized = 0;
	const char *ext = NULL;
	int handled = 0;
	u8 buf[16];
	struct de_stringreaderdata *classname_srd = NULL;
	struct de_stringreaderdata *topicname_srd = NULL;
	struct de_stringreaderdata *itemname_srd = NULL;
	int retval = 0;

	// TODO: This code (for the next 3 fields) is duplicated in the function for
	// "linked" objects.

	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, "embedded ClassName: \"%s\"", ucstring_getpsz(classname_srd->str));
	pos += stringlen;

	stringlen = de_getu32le_p(&pos);
	topicname_srd = dbuf_read_string(c->infile, pos, stringlen, 260, DE_CONVFLAG_STOP_AT_NUL,
		d->input_encoding);
	de_dbg(c, "TopicName/filename: \"%s\"", ucstring_getpsz(topicname_srd->str));
	pos += stringlen;

	stringlen = de_getu32le_p(&pos);
	itemname_srd = dbuf_read_string(c->infile, pos, stringlen, 260, DE_CONVFLAG_STOP_AT_NUL,
		d->input_encoding);
	de_dbg(c, "ItemName/params: \"%s\"", ucstring_getpsz(itemname_srd->str));
	pos += stringlen;

	data_len = de_getu32le_p(&pos);
	de_dbg(c, "NativeData: pos=%"I64_FMT", len=%"I64_FMT, pos, data_len);

	// TODO: I don't know the extent to which it's better to sniff the data, or
	// rely on the typename.
	de_read(buf, pos, sizeof(buf));

	if(!de_strcmp(classname_srd->sz, "Package")) {
		recognized = 1;
		handled = do_ole_package(c, d, pos, data_len);
	}
	else if(!de_strcmp(classname_srd->sz, "SoundRec") &&
		!de_memcmp(&buf[0], (const void*)"RIFF", 4) &&
		!de_memcmp(&buf[8], (const void*)"WAVE", 4) )
	{
		ext = "wav";
	}
	else if(!de_strncmp(classname_srd->sz, "Word.Document.", 14) ||
		!de_strncmp(classname_srd->sz, "Word.Picture.", 13))
	{
		ext = "doc";
	}
	else if (!de_strncmp(classname_srd->sz, "Excel.Chart.", 12) ||
		!de_strcmp(classname_srd->sz, "ExcelWorksheet"))
	{
		ext = "xls";
	}
	else if(!de_strcmp(classname_srd->sz, "CDraw") &&
		!de_memcmp(&buf[0], (const void*)"RIFF", 4) &&
		!de_memcmp(&buf[8], (const void*)"CDR", 3) )
	{
		ext = "cdr"; // Looks like CorelDRAW
	}
	else if (!de_strcmp(classname_srd->sz, "PaintShopPro") &&
		!de_memcmp(&buf[0], (const void*)"\x28\0\0\0", 4))
	{
		de_run_module_by_id_on_slice(c, "dib", NULL, c->infile, pos, data_len);
		handled = 1;
	}
	if(!de_strcmp(classname_srd->sz, "ShapewareVISIO20")) {
		ext = "vsd";
	}
	else if(buf[0]=='B' && buf[1]=='M') {
		// TODO: Detect true length of data?
		// TODO: This detection may be too aggressive.
		ext = "bmp";
	}

	if(ext && !handled) {
		dbuf_create_file_from_slice(c->infile, pos, data_len, ext, NULL, 0);
		handled = 1;
	}

	if(!handled) {
		if(d->extract_all) {
			extract_unknown_ole_obj(c, d, pos, data_len, classname_srd);
		}
		else if(!recognized) {
			de_warn(c, "Unknown/unsupported type of OLE object (\"%s\") at %"I64_FMT,
				ucstring_getpsz(classname_srd->str), pos1);
		}
	}

	pos += data_len;
	// Nested "presentation" object
	ret = do_ole_object(c, d, pos, pos1+len-pos, exact_size_known, 1,
		&bytes_consumed2);
	if(!ret) goto done;
	pos += bytes_consumed2;

	*bytes_consumed = pos-pos1;
	retval = 1;
done:
	de_destroy_stringreaderdata(c, classname_srd);
	de_destroy_stringreaderdata(c, topicname_srd);
	de_destroy_stringreaderdata(c, itemname_srd);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq %r9, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x50(%rbp)
movl $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movl $0x104, %ecx            # imm = 0x104
movl $0x1, %r8d
callq 0x1664c0
movq %rax, -0x88(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0xb8(%rbp), %rdi
movq %rax, %rdx
leaq 0xd5592(%rip), %rsi     # 0x1bd515
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movl $0x104, %ecx            # imm = 0x104
movl $0x1, %r8d
callq 0x1664c0
movq %rax, -0x90(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x90(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0xb0(%rbp), %rdi
movq %rax, %rdx
leaq 0xd5529(%rip), %rsi     # 0x1bd52e
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movl $0x104, %ecx            # imm = 0x104
movl $0x1, %r8d
callq 0x1664c0
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0xa8(%rbp), %rdi
movq %rax, %rdx
leaq 0xd54c0(%rip), %rsi     # 0x1bd547
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq -0x48(%rbp), %rcx
leaq 0xd54ad(%rip), %rsi     # 0x1bd56f
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x80(%rbp), %rsi
movq -0x38(%rbp), %rdx
movl $0x10, %ecx
callq 0x164d10
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd5497(%rip), %rsi     # 0x1bd58c
callq 0x191a0
cmpl $0x0, %eax
jne 0xe8123
movl $0x1, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x48(%rbp), %rcx
callq 0xe8930
movl %eax, -0x64(%rbp)
jmp 0xe82e0
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd545f(%rip), %rsi     # 0x1bd594
callq 0x191a0
cmpl $0x0, %eax
jne 0xe817f
leaq -0x80(%rbp), %rax
movl (%rax), %eax
subl $0x46464952, %eax       # imm = 0x46464952
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xe817f
leaq -0x80(%rbp), %rax
addq $0x8, %rax
movl (%rax), %eax
subl $0x45564157, %eax       # imm = 0x45564157
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xe817f
leaq 0xd4210(%rip), %rax     # 0x1bc386
movq %rax, -0x60(%rbp)
jmp 0xe82de
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd540c(%rip), %rsi     # 0x1bd59d
movl $0xe, %edx
callq 0x19050
cmpl $0x0, %eax
je 0xe81c1
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd53fa(%rip), %rsi     # 0x1bd5ac
movl $0xd, %edx
callq 0x19050
cmpl $0x0, %eax
jne 0xe81d1
leaq 0xd53f2(%rip), %rax     # 0x1bd5ba
movq %rax, -0x60(%rbp)
jmp 0xe82dc
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd53db(%rip), %rsi     # 0x1bd5be
movl $0xc, %edx
callq 0x19050
cmpl $0x0, %eax
je 0xe820e
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd53c7(%rip), %rsi     # 0x1bd5cb
callq 0x191a0
cmpl $0x0, %eax
jne 0xe821e
leaq 0xd53c5(%rip), %rax     # 0x1bd5da
movq %rax, -0x60(%rbp)
jmp 0xe82da
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd53ae(%rip), %rsi     # 0x1bd5de
callq 0x191a0
cmpl $0x0, %eax
jne 0xe827b
leaq -0x80(%rbp), %rax
movl (%rax), %eax
subl $0x46464952, %eax       # imm = 0x46464952
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xe827b
leaq -0x80(%rbp), %rdi
addq $0x8, %rdi
leaq 0xd5385(%rip), %rsi     # 0x1bd5e4
movl $0x3, %edx
callq 0x19170
cmpl $0x0, %eax
jne 0xe827b
leaq 0xc41e7(%rip), %rax     # 0x1ac45c
movq %rax, -0x60(%rbp)
jmp 0xe82d8
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd535b(%rip), %rsi     # 0x1bd5e8
callq 0x191a0
cmpl $0x0, %eax
jne 0xe82d6
leaq -0x80(%rbp), %rax
movl (%rax), %eax
subl $0x28, %eax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xe82d6
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x38(%rbp), %r8
movq -0x48(%rbp), %r9
leaq 0xc6b2d(%rip), %rsi     # 0x1aedf3
xorl %eax, %eax
movl %eax, %edx
callq 0x15c9f0
movl $0x1, -0x64(%rbp)
jmp 0xe82d8
jmp 0xe82da
jmp 0xe82dc
jmp 0xe82de
jmp 0xe82e0
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd5303(%rip), %rsi     # 0x1bd5f5
callq 0x191a0
cmpl $0x0, %eax
jne 0xe8309
leaq 0xd5303(%rip), %rax     # 0x1bd606
movq %rax, -0x60(%rbp)
jmp 0xe8328
movzbl -0x80(%rbp), %eax
cmpl $0x42, %eax
jne 0xe8326
movzbl -0x7f(%rbp), %eax
cmpl $0x4d, %eax
jne 0xe8326
leaq 0xc49e5(%rip), %rax     # 0x1acd07
movq %rax, -0x60(%rbp)
jmp 0xe8328
cmpq $0x0, -0x60(%rbp)
je 0xe835d
cmpl $0x0, -0x64(%rbp)
jne 0xe835d
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x60(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x166be0
movl $0x1, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jne 0xe83cc
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0xe838b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x88(%rbp), %r8
callq 0xe8d20
jmp 0xe83ca
cmpl $0x0, -0x58(%rbp)
jne 0xe83c8
movq -0x8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0xc0(%rbp), %rdi
movq %rax, %rdx
movq -0x18(%rbp), %rcx
leaq 0xd5249(%rip), %rsi     # 0x1bd60a
movb $0x0, %al
callq 0x15bc10
jmp 0xe83ca
jmp 0xe83cc
movq -0x48(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x18(%rbp), %rcx
addq -0x20(%rbp), %rcx
subq -0x38(%rbp), %rcx
movl -0x24(%rbp), %r8d
movl $0x1, %r9d
leaq -0x50(%rbp), %rax
movq %rax, (%rsp)
callq 0xe76d0
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jne 0xe8412
jmp 0xe8437
movq -0x50(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rcx
subq -0x18(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x9c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x166830
movq -0x8(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x166830
movq -0x8(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq 0x166830
movl -0x9c(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)