// File: /workspace/github_c_makefile_star2_O1/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 %rsi, %r14
movq %rdi, %rbx
testq %rsi, %rsi
je 0x847f1
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x847f1
cmpl $0x0, 0x24(%r14)
jne 0x847f1
cmpl $0x0, 0x20(%r14)
jne 0x847f1
movq 0x18(%rax), %r8
movq 0x18(%r14), %rcx
cmpq %rcx, %r8
je 0x84744
movl (%r14), %edx
leaq 0xa6938(%rip), %rsi     # 0x12b072
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x847e8
movq 0x20(%rax), %rdi
testq %rdi, %rdi
je 0x84772
leaq 0xa6947(%rip), %rsi     # 0x12b0a8
callq 0x181a0
testl %eax, %eax
jne 0x84772
movq %r14, %rdi
callq 0x848d1
movq 0x8(%r14), %rax
movq 0x18(%rax), %rcx
testq %rcx, %rcx
je 0x847e8
movq 0x10(%rcx), %rax
testq %rax, %rax
jle 0x847e8
movq 0x8(%rcx), %rcx
xorl %edx, %edx
cmpl $0x5, (%rcx,%rdx,4)
je 0x8479e
incq %rdx
cmpq %rdx, %rax
jne 0x8478e
jmp 0x847e8
movl (%r14), %edx
leaq 0xa692b(%rip), %rsi     # 0x12b0d3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x10(%r14), %rcx
movq 0x18(%rcx), %r9
leaq 0x95661(%rip), %rsi     # 0x119e2f
movq %rbx, %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0xea5fa
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpl $0x2, 0x90(%rbx)
jge 0x84841
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
testq %r14, %r14
je 0x8481d
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0x84815
callq 0xf0b13
movq $0x0, 0x10(%r14)
movl $0x1, 0x24(%r14)
movq %r12, %rdi
callq 0xf6bb0
movq %r13, %rdi
callq 0xf0b13
movq %rbx, %rdi
movq %r15, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xea9b3
movq %rbx, %rdi
callq 0xea9a9
movq %rax, %r15
movq %rbx, %rdi
callq 0xf6870
movq %rax, %r12
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x8486c
movq 0x18(%rax), %rsi
movq %r12, %rdi
callq 0xf6969
cmpq $0x0, 0x10(%r12)
jle 0x84881
movq %r12, %rdi
movl $0x2e, %esi
callq 0xf69a2
leaq 0xa6832(%rip), %rsi     # 0x12b0ba
movq %r12, %rdi
movl $0x5, %edx
callq 0xf6a40
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0xea9e4
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
movl $0x1, %ecx
callq 0xf037a
movq %rax, %r13
movq 0x10(%r14), %rdi
movq 0x18(%rdi), %rdx
xorl %esi, %esi
movq %rax, %rcx
callq 0xefabc
jmp 0x847fa