// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/jpeg.c
// Function: finalize_fpxr_stream
static void finalize_fpxr_stream(deark *c, lctx *d, struct fpxr_entity_struct *fe)
{
	de_finfo *fi = NULL;
	dbuf *outf = NULL;
	de_ucstring *name2 = NULL;

	if(!fe || !fe->stream) goto done;
	if(fe->done_flag || fe->is_storage) goto done;

	if(fe->stream->len != fe->stream_size) {
		de_warn(c, "Expected FPXR stream #%u to have %"I64_FMT" bytes, found %"I64_FMT,
			(unsigned int)fe->index, fe->stream_size, fe->stream->len);
	}

	// Process some known streams
	if(fe->name_srd) {
		if(fe->name_srd->sz_utf8 && !de_strcmp(fe->name_srd->sz_utf8, "/FUJIFILM/Preview")) {
			do_fpxr_fujifilm_preview(c, d, fe);
		}

		// The FlashPix spec says "Names in an IStorage that begin with the
		// value '\0x05' are reserved exclusively for the storage of property
		// sets."
		//
		// It probably means the last *component* of the name begins with 0x05.
		// 0x05 shouldn't appear anywhere else, I think, so I'll just search
		// the whole string for it.
		if(ucstring_contains_char(fe->name_srd->str, 0x05)) {
			do_fpxr_olepropset_stream(c, d, fe);
		}
	}

	if(c->extract_level<2) goto done;

	fi = de_finfo_create(c);

	name2 = ucstring_create(c);
	if(fe->name_srd) {
		ucstring_append_ucstring(name2, fe->name_srd->str);
	}
	if(name2->len>0) {
		ucstring_append_char(name2, '.');
	}
	ucstring_append_sz(name2, "fpxr.bin", DE_ENCODING_LATIN1);
	de_finfo_set_name_from_ucstring(c, fi, name2, 0);

	outf = dbuf_create_output_file(c, NULL, fi, DE_CREATEFLAG_IS_AUX);
	dbuf_copy(fe->stream, 0, fe->stream->len, outf);

done:
	if(fe && fe->stream) {
		dbuf_close(fe->stream);
		fe->stream = NULL;
	}
	if(fe) {
		fe->done_flag = 1;
	}
	ucstring_destroy(name2);
	dbuf_close(outf);
	de_finfo_destroy(c, fi);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %rbx
testq %rsi, %rsi
je 0x7f437
movq %rsi, %r14
movq 0x10(%rsi), %rax
testq %rax, %rax
je 0x7f442
cmpl $0x0, 0x24(%r14)
jne 0x7f419
cmpl $0x0, 0x20(%r14)
jne 0x7f419
movq 0x18(%rax), %r8
movq 0x18(%r14), %rcx
cmpq %rcx, %r8
je 0x7f334
movl (%r14), %edx
leaq 0x9eda8(%rip), %rsi     # 0x11e0d2
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x7f410
movq 0x20(%rax), %rdi
testq %rdi, %rdi
je 0x7f39d
leaq 0x9edb7(%rip), %rsi     # 0x11e108
callq 0x181a0
testl %eax, %eax
jne 0x7f39d
movq 0x10(%r14), %rdi
cmpq $0x64, 0x18(%rdi)
jl 0x7f39d
leaq 0x881dd(%rip), %rdx     # 0x107549
pushq $0x2f
popq %rsi
pushq $0x3
popq %rcx
callq 0xe4d48
testl %eax, %eax
jne 0x7f39d
movq 0x10(%r14), %rdi
movq 0x18(%rdi), %rdx
addq $-0x2f, %rdx
leaq 0x9ed95(%rip), %rcx     # 0x11e123
pushq $0x2f
popq %rsi
pushq $0x1
popq %r9
xorl %r8d, %r8d
callq 0xe4e0b
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x7f410
movq 0x10(%rax), %rcx
xorl %edx, %edx
testq %rcx, %rcx
cmovleq %rdx, %rcx
cmpq %rdx, %rcx
je 0x7f410
movq 0x8(%rax), %rsi
cmpl $0x5, (%rsi,%rdx,4)
leaq 0x1(%rdx), %rdx
jne 0x7f3b7
movl (%r14), %edx
leaq 0x9ed5f(%rip), %rsi     # 0x11e133
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x10(%r14), %rcx
movq 0x18(%rcx), %r9
leaq 0x8da67(%rip), %rsi     # 0x10ce5f
movq %rbx, %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0xdf9b8
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpl $0x2, 0x90(%rbx)
jge 0x7f477
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0x7f44b
callq 0xe5591
andq $0x0, 0x10(%r14)
jmp 0x7f44b
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
jmp 0x7f453
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %r15d, %r15d
movl $0x1, 0x24(%r14)
movq %r15, %rdi
callq 0xeaf3c
movq %r13, %rdi
callq 0xe5591
movq %rbx, %rdi
movq %r12, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xdfd24
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %r12
movq %rbx, %rdi
callq 0xeac74
movq %rax, %r15
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x7f4a2
movq 0x18(%rax), %rsi
movq %r15, %rdi
callq 0xead71
cmpq $0x0, 0x10(%r15)
jle 0x7f4b4
pushq $0x2e
popq %rsi
movq %r15, %rdi
callq 0xeada5
leaq 0x9ec5f(%rip), %rsi     # 0x11e11a
pushq $0x5
popq %rdx
movq %r15, %rdi
callq 0xeae42
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xdfd55
pushq $0x1
popq %rcx
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0xe4e62
movq %rax, %r13
movq 0x10(%r14), %rdi
movq 0x18(%rdi), %rdx
xorl %esi, %esi
movq %rax, %rcx
callq 0xe45f4
jmp 0x7f422