// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/deark-dbuf.c
// Function: dbuf_create_output_file
dbuf *dbuf_create_output_file(deark *c, const char *ext1, de_finfo *fi,
	unsigned int createflags)
{
	char nbuf[500];
	char msgbuf[200];
	char ext[128];
	int have_ext;
	dbuf *f;
	const char *basefn;
	u8 is_directory = 0;
	char *name_from_finfo = NULL;
	i64 name_from_finfo_len = 0;

	if(ext1) {
		have_ext = 1;
		sanitize_ext(ext1, ext, sizeof(ext));
	}
	else {
		have_ext = 0;
		ext[0] = '\0';
	}

	if(have_ext && fi && fi->original_filename_flag) {
		de_dbg(c, "[internal warning: Incorrect use of create_output_file]");
	}

	f = create_dbuf_lowlevel(c);
	f->max_len_hard = c->max_output_file_size;
	f->is_managed = 1;

	if(fi && fi->is_volume_label) {
		if(c->output_style!=DE_OUTPUTSTYLE_ARCHIVE || c->archive_fmt!=DE_ARCHIVEFMT_ZIP) {
			de_dbg(c, "skipping volume label");
			f->btype = DBUF_TYPE_NULL;
			goto done;
		}
	}

	if(fi && fi->is_directory) {
		is_directory = 1;
	}

	if(is_directory && !c->keep_dir_entries) {
		de_dbg(c, "skipping 'directory' file");
		f->btype = DBUF_TYPE_NULL;
		goto done;
	}

	if(c->extract_policy==DE_EXTRACTPOLICY_MAINONLY) {
		if(createflags&DE_CREATEFLAG_IS_AUX) {
			de_dbg(c, "skipping 'auxiliary' file");
			f->btype = DBUF_TYPE_NULL;
			goto done;
		}
	}
	else if(c->extract_policy==DE_EXTRACTPOLICY_AUXONLY) {
		if(!(createflags&DE_CREATEFLAG_IS_AUX)) {
			de_dbg(c, "skipping 'main' file");
			f->btype = DBUF_TYPE_NULL;
			goto done;
		}
	}

	f->file_id = c->file_count;
	c->file_count++;

	basefn = c->base_output_filename ? c->base_output_filename : "output";

	if(fi && ucstring_isnonempty(fi->file_name_internal)) {
		name_from_finfo_len = 1 + ucstring_count_utf8_bytes(fi->file_name_internal);
		name_from_finfo = de_malloc(c, name_from_finfo_len);
		ucstring_to_sz(fi->file_name_internal, name_from_finfo, (size_t)name_from_finfo_len, 0,
			DE_ENCODING_UTF8);
	}

	if(c->output_style==DE_OUTPUTSTYLE_ARCHIVE && !c->base_output_filename &&
		fi && fi->is_directory &&
		(fi->is_root_dir || (fi->detect_root_dot_dir && fi->orig_name_was_dot)))
	{
		de_strlcpy(nbuf, ".", sizeof(nbuf));
	}
	else if(c->special_1st_filename && (f->file_id==c->first_output_file) &&
		!is_directory)
	{
		de_strlcpy(nbuf, c->special_1st_filename, sizeof(nbuf));
	}
	else if(c->output_style==DE_OUTPUTSTYLE_ARCHIVE && !c->base_output_filename &&
		fi && fi->original_filename_flag && name_from_finfo)
	{
		// TODO: This is a "temporary" hack to allow us to, when both reading from
		// and writing to an archive format, use some semblance of the correct
		// filename (instead of "output.xxx.yyy").
		// There are some things that we don't handle optimally, such as
		// subdirectories.
		// A major redesign of the file naming logic would be good.
		de_strlcpy(nbuf, name_from_finfo, sizeof(nbuf));
	}
	else {
		char fn_suffix[256];

		if(have_ext && name_from_finfo) {
			de_snprintf(fn_suffix, sizeof(fn_suffix), "%s.%s", name_from_finfo, ext);
		}
		else if(have_ext) {
			de_strlcpy(fn_suffix, ext, sizeof(fn_suffix));
		}
		else if(is_directory && name_from_finfo) {
			de_snprintf(fn_suffix, sizeof(fn_suffix), "%s.dir", name_from_finfo);
		}
		else if(name_from_finfo) {
			de_strlcpy(fn_suffix, name_from_finfo, sizeof(fn_suffix));
		}
		else if(is_directory) {
			de_strlcpy(fn_suffix, "dir", sizeof(fn_suffix));
		}
		else {
			de_strlcpy(fn_suffix, "bin", sizeof(fn_suffix));
		}

		de_snprintf(nbuf, sizeof(nbuf), "%s.%03d.%s", basefn, f->file_id, fn_suffix);
	}

	f->name = de_strdup(c, nbuf);

	if(fi) {
		// The finfo object passed to us at file creation is not required to
		// remain valid, so make a copy of anything in it that we might need
		// later.
		f->fi_copy = de_finfo_create(c);
		finfo_shallow_copy(c, fi, f->fi_copy);
		fi->riscos_appended_type = 0;

		// Here's where we respect the -intz option, by using it to convert to
		// UTC in some cases.
		if(f->fi_copy->timestamp[DE_TIMESTAMPIDX_MODIFY].is_valid && f->fi_copy->timestamp[DE_TIMESTAMPIDX_MODIFY].tzcode==DE_TZCODE_LOCAL &&
			c->input_tz_offs_seconds!=0)
		{
			de_timestamp_cvt_to_utc(&f->fi_copy->timestamp[DE_TIMESTAMPIDX_MODIFY], -c->input_tz_offs_seconds);
		}

		if(f->fi_copy->internal_mod_time.is_valid && f->fi_copy->internal_mod_time.tzcode==DE_TZCODE_LOCAL &&
			c->input_tz_offs_seconds!=0)
		{
			de_timestamp_cvt_to_utc(&f->fi_copy->internal_mod_time, -c->input_tz_offs_seconds);
		}
	}

	if(f->file_id < c->first_output_file) {
		f->btype = DBUF_TYPE_NULL;
		goto done;
	}

	if(f->file_id >= c->first_output_file + c->max_output_files)
	{
		f->btype = DBUF_TYPE_NULL;
		if(f->file_id == c->first_output_file + c->max_output_files) {
			if(!c->user_set_max_output_files) {
				de_err(c, "Limit of %d output files exceeded", c->max_output_files);
			}
		}
		goto done;
	}

	c->num_files_extracted++;

	if(c->extrlist_dbuf) {
		dbuf_printf(c->extrlist_dbuf, "%s\n", f->name);
		dbuf_flush_lowlevel(c->extrlist_dbuf);
	}

	if(c->enable_wbuffer_test && !(createflags & DE_CREATEFLAG_NO_WBUFFER))
	{
		dbuf_enable_wbuffer(f);
	}

	if(c->enable_oinfo) {
		f->crco_for_oinfo = de_crcobj_create(c, DE_CRCOBJ_CRC32_IEEE);
	}

	if(c->list_mode) {
		f->btype = DBUF_TYPE_NULL;
		if(c->list_mode_include_file_id) {
			de_msg(c, "%d:%s", f->file_id, f->name);
		}
		else {
			de_msg(c, "%s", f->name);
		}
		goto done;
	}

	if(c->output_style==DE_OUTPUTSTYLE_ARCHIVE && c->archive_fmt==DE_ARCHIVEFMT_TAR) {
		de_info(c, "Adding %s to TAR file", f->name);
		f->btype = DBUF_TYPE_ODBUF;
		// A dummy max_len_hard value. The parent will do the checking.
		f->max_len_hard = DE_DUMMY_MAX_FILE_SIZE;
		f->writing_to_tar_archive = 1;
		de_tar_start_member_file(c, f);
	}
	else if(c->output_style==DE_OUTPUTSTYLE_ARCHIVE) { // ZIP
		i64 initial_alloc;
		de_info(c, "Adding %s to ZIP file", f->name);
		f->btype = DBUF_TYPE_MEMBUF;
		f->max_len_hard = DE_MAX_MEMBUF_SIZE;
		if(is_directory) {
			// A directory entry is not expected to have any data associated
			// with it (besides the files it contains).
			initial_alloc = 16;
		}
		else {
			initial_alloc = 65536;
		}
		f->membuf_buf = de_malloc(c, initial_alloc);
		f->membuf_alloc = initial_alloc;
		f->write_memfile_to_zip_archive = 1;
	}
	else if(c->output_style==DE_OUTPUTSTYLE_STDOUT) {
		de_info(c, "Writing %s to [stdout]", f->name);
		f->btype = DBUF_TYPE_STDOUT;
		// TODO: Should we increase f->max_len_hard?
		f->fp = stdout;
	}
	else {
		de_info(c, "Writing %s", f->name);
		f->btype = DBUF_TYPE_OFILE;
		f->fp = de_fopen_for_write(c, f->name, msgbuf, sizeof(msgbuf),
			c->overwrite_mode, 0);

		if(!f->fp) {
			de_err(c, "Failed to write %s: %s", f->name, msgbuf);
			f->btype = DBUF_TYPE_NULL;
			c->serious_error_flag = 1;
		}
	}

done:
	de_free(c, name_from_finfo);
	return f;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x398, %rsp            # imm = 0x398
movl %ecx, %r15d
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
testq %rsi, %rsi
je 0xe4eff
leaq 0x120(%rsp), %r14
movl $0x80, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0xde492
movabsq $0x1000000000000d, %rax # imm = 0x1000000000000D
movzbl (%r14), %ecx
testl %ecx, %ecx
je 0xe4edc
leal -0x30(%rcx), %edx
cmpb $0xa, %dl
jb 0xe4ed1
movl %ecx, %edx
andb $-0x21, %dl
addb $-0x41, %dl
cmpb $0x1a, %dl
jb 0xe4ed1
addl $-0x2b, %ecx
cmpl $0x34, %ecx
ja 0xe4ed6
btq %rcx, %rax
jae 0xe4ed6
incq %r14
jmp 0xe4ea6
movb $0x5f, (%r14)
jmp 0xe4ed1
testq %r12, %r12
je 0xe4f10
movb $0x1, %bpl
cmpb $0x0, 0x8(%r12)
je 0xe4f12
leaq 0x56e6e(%rip), %rsi     # 0x13bd61
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
jmp 0xe4f12
movb $0x0, 0x120(%rsp)
testq %r12, %r12
setne %bpl
jmp 0xe4f12
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0xe5835
movq %rax, %r14
movq 0xb0(%rbx), %rax
movq %rax, 0x20(%r14)
movb $0x1, 0x4(%r14)
movb $0x1, %cl
testb %bpl, %bpl
je 0xe4f6f
cmpb $0x0, 0xa(%r12)
je 0xe4f53
cmpl $0x1, 0x7c(%rbx)
jne 0xe5021
cmpl $0x1, 0x80(%rbx)
jne 0xe5021
cmpb $0x0, 0x9(%r12)
je 0xe4f6f
cmpb $0x0, 0xcf(%rbx)
je 0xe5069
movb $0x1, %dil
xorl %ecx, %ecx
jmp 0xe4f71
xorl %edi, %edi
movl 0x8c(%rbx), %eax
cmpl $0x2, %eax
je 0xe4f93
cmpl $0x1, %eax
jne 0xe4fa5
testb $0x1, %r15b
je 0xe4fa5
leaq 0x56e3b(%rip), %rsi     # 0x13bdc9
jmp 0xe5070
testb $0x1, %r15b
jne 0xe4fa5
leaq 0x56e43(%rip), %rsi     # 0x13bde3
jmp 0xe5070
movl 0x38(%rbx), %eax
movl %eax, 0x64(%r14)
incl %eax
movl %eax, 0x38(%rbx)
movq 0x150(%rbx), %rax
testq %rax, %rax
leaq 0x59760(%rip), %rdx     # 0x13e722
cmovneq %rax, %rdx
movq %rdx, 0x10(%rsp)
testb %bpl, %bpl
movl %r15d, 0x1c(%rsp)
movl %ecx, 0xc(%rsp)
je 0xe501c
movl %edi, 0x8(%rsp)
movq (%r12), %rdi
callq 0xeaf8a
testl %eax, %eax
je 0xe502a
movq (%r12), %rdi
callq 0xeb519
leaq 0x1(%rax), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xdecaf
movq (%r12), %rdi
pushq $0x2
popq %r8
movq %rax, %rsi
movq %r15, %rdx
movq %rax, %r15
xorl %ecx, %ecx
callq 0xeb5de
jmp 0xe502d
xorl %r15d, %r15d
jmp 0xe5031
leaq 0x56d71(%rip), %rsi     # 0x13bd99
jmp 0xe5070
xorl %r15d, %r15d
movl 0x8(%rsp), %edi
movl 0x7c(%rbx), %eax
cmpl $0x1, %eax
jne 0xe5092
cmpq $0x0, 0x150(%rbx)
setne %cl
movl %ebp, %edx
xorb $0x1, %dl
orb %cl, %dl
jne 0xe5092
cmpb $0x0, 0x9(%r12)
je 0xe5092
cmpb $0x0, 0xb(%r12)
je 0xe5082
leaq 0x4961a(%rip), %rsi     # 0x12e67e
jmp 0xe5117
leaq 0x56d3f(%rip), %rsi     # 0x13bdaf
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
jmp 0xe52a4
cmpb $0x0, 0xc(%r12)
je 0xe5092
cmpb $0x0, 0xd(%r12)
jne 0xe505d
movq 0x158(%rbx), %rsi
testq %rsi, %rsi
je 0xe50b0
movl 0x64(%r14), %ecx
cmpl 0x98(%rbx), %ecx
setne %cl
orb %dil, %cl
je 0xe5117
cmpl $0x1, %eax
jne 0xe50e8
cmpq $0x0, 0x150(%rbx)
setne %al
movl %ebp, %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0xe50e8
testq %r15, %r15
je 0xe50e8
cmpb $0x0, 0x8(%r12)
je 0xe50e8
leaq 0x1a0(%rsp), %rdi
movl $0x1f4, %edx            # imm = 0x1F4
movq %r15, %rsi
jmp 0xe5124
testq %r13, %r13
je 0xe512b
testq %r15, %r15
je 0xe512b
leaq 0x48731(%rip), %rdx     # 0x12d82a
leaq 0x20(%rsp), %rdi
leaq 0x120(%rsp), %r8
movl $0x100, %esi            # imm = 0x100
movq %r15, %rcx
xorl %eax, %eax
callq 0xde40c
jmp 0xe514b
leaq 0x1a0(%rsp), %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0xde492
jmp 0xe5177
testq %r13, %r13
je 0xe53a4
leaq 0x20(%rsp), %rdi
leaq 0x120(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0xde492
movq 0x10(%rsp), %r13
movl 0x64(%r14), %r8d
leaq 0x56ca4(%rip), %rdx     # 0x13bdff
leaq 0x1a0(%rsp), %rdi
leaq 0x20(%rsp), %r9
movl $0x1f4, %esi            # imm = 0x1F4
movq %r13, %rcx
xorl %eax, %eax
callq 0xde40c
leaq 0x1a0(%rsp), %rsi
movq %rbx, %rdi
callq 0xdf791
movq %rax, 0x68(%r14)
testb %bpl, %bpl
je 0xe5296
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, 0xd0(%r14)
movb 0x9(%r12), %cl
movb %cl, 0x9(%rax)
movb 0xa(%r12), %cl
movb %cl, 0xa(%rax)
movb 0x10(%r12), %cl
movb %cl, 0x10(%rax)
movb 0x11(%r12), %cl
movb %cl, 0x11(%rax)
movl 0xa0(%r12), %ecx
movl %ecx, 0xa0(%rax)
movl 0x14(%r12), %ecx
movl %ecx, 0x14(%rax)
pushq $0x18
popq %rcx
cmpq $0x68, %rcx
je 0xe51f1
movups (%r12,%rcx), %xmm0
movups %xmm0, (%rax,%rcx)
addq $0x10, %rcx
jmp 0xe51dc
movups 0x68(%r12), %xmm0
movups %xmm0, 0x68(%rax)
movups 0x78(%r12), %xmm0
movups %xmm0, 0x78(%rax)
movq 0x88(%r12), %rcx
movq %rcx, 0x88(%rax)
movb 0xe(%r12), %cl
movb %cl, 0xe(%rax)
movq 0x98(%r12), %rcx
movq %rcx, 0x98(%rax)
movq 0xa4(%r12), %rcx
movq %rcx, 0xa4(%rax)
movb $0x0, 0x11(%r12)
movq 0xd0(%r14), %rdi
cmpb $0x0, 0x18(%rdi)
je 0xe5272
cmpb $0x2, 0x19(%rdi)
jne 0xe5272
movq 0x100(%rbx), %rsi
testq %rsi, %rsi
je 0xe5272
addq $0x18, %rdi
negq %rsi
callq 0xe0491
movq 0xd0(%r14), %rdi
cmpb $0x0, 0x68(%rdi)
je 0xe5296
cmpb $0x2, 0x69(%rdi)
jne 0xe5296
movq 0x100(%rbx), %rsi
testq %rsi, %rsi
je 0xe5296
addq $0x68, %rdi
negq %rsi
callq 0xe0491
movl 0x64(%r14), %eax
movl 0x98(%rbx), %ecx
cmpl %ecx, %eax
jge 0xe52c8
andl $0x0, (%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded32
movq %r14, %rax
addq $0x398, %rsp            # imm = 0x398
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x9c(%rbx), %edx
addl %edx, %ecx
cmpl %ecx, %eax
jge 0xe5375
incl 0x3c(%rbx)
movq 0x148(%rbx), %rdi
testq %rdi, %rdi
je 0xe5305
movq 0x68(%r14), %rdx
leaq 0x1e49a(%rip), %rsi     # 0x10378c
xorl %eax, %eax
callq 0xe5905
movq 0x148(%rbx), %rdi
callq 0xe59b3
btl $0x9, 0x1c(%rsp)
jb 0xe531e
cmpb $0x0, 0x12b(%rbx)
je 0xe531e
movq %r14, %rdi
callq 0xe589c
cmpb $0x0, 0x96(%rbx)
je 0xe5339
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0xe0ae9
movq %rax, 0x80(%r14)
cmpb $0x0, 0x94(%rbx)
je 0xe53dc
andl $0x0, (%r14)
cmpb $0x0, 0x95(%rbx)
je 0xe543d
movl 0x64(%r14), %edx
movq 0x68(%r14), %rcx
leaq 0x56ac6(%rip), %rsi     # 0x13be2c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf42e
jmp 0xe52a8
andl $0x0, (%r14)
cmpl %ecx, %eax
jne 0xe52a8
cmpb $0x0, 0xa0(%rbx)
jne 0xe52a8
leaq 0x56a75(%rip), %rsi     # 0x13be0a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0xe52a8
testq %r15, %r15
sete %al
xorb $0x1, %dil
orb %al, %dil
movq 0x10(%rsp), %r13
jne 0xe5457
leaq 0x56a35(%rip), %rdx     # 0x13bdf8
leaq 0x20(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %r15, %rcx
xorl %eax, %eax
callq 0xde40c
jmp 0xe5150
movl 0x7c(%rbx), %eax
cmpl $0x2, %eax
je 0xe5472
cmpl $0x1, %eax
jne 0xe54a1
cmpl $0x2, 0x80(%rbx)
movq 0x68(%r14), %rdx
jne 0xe5521
leaq 0x56a29(%rip), %rsi     # 0x13be32
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf37f
movl $0x8, (%r14)
movabsq $0x100000000000000, %rax # imm = 0x100000000000000
movq %rax, 0x20(%r14)
movb $0x1, 0x61(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe31b4
jmp 0xe52a8
movq 0x68(%r14), %rdx
leaq 0x228b7(%rip), %rsi     # 0x107cff
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf42e
jmp 0xe52a8
testq %r15, %r15
je 0xe5511
leaq 0x20(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %r15, %rsi
jmp 0xe5587
movq 0x68(%r14), %rdx
leaq 0x569e1(%rip), %rsi     # 0x13be5e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf37f
movl $0x5, (%r14)
movq 0xacb3b(%rip), %rax     # 0x191fd0
movq (%rax), %rax
movq %rax, 0x10(%r14)
jmp 0xe52a8
movq 0x68(%r14), %rdx
leaq 0x569c9(%rip), %rsi     # 0x13be75
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf37f
movl $0x2, (%r14)
movq 0x68(%r14), %rsi
movl 0xd8(%rbx), %r8d
leaq 0x20(%rsp), %rdx
movl $0xc8, %ecx
movq %rbx, %rdi
xorl %r9d, %r9d
callq 0xfca20
movq %rax, 0x10(%r14)
testq %rax, %rax
jne 0xe52a8
movq 0x68(%r14), %rdx
leaq 0x56855(%rip), %rsi     # 0x13bd4a
leaq 0x20(%rsp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
andl $0x0, (%r14)
movb $0x1, 0x4c(%rbx)
jmp 0xe52a8
cmpb $0x0, 0xc(%rsp)
je 0xe5576
leaq 0x42fbe(%rip), %rsi     # 0x1284dd
jmp 0xe557d
leaq 0x56920(%rip), %rsi     # 0x13be48
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf37f
movl $0x3, (%r14)
movq $0x77359400, 0x20(%r14) # imm = 0x77359400
cmpb $0x0, 0xc(%rsp)
movl $0x10000, %eax          # imm = 0x10000
movq %r15, %r12
pushq $0x10
popq %r15
cmovneq %rax, %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xdecaf
movq %rax, 0x78(%r14)
movq %r15, 0x70(%r14)
movq %r12, %r15
movb $0x1, 0x60(%r14)
jmp 0xe52a8
leaq 0x4dcfa(%rip), %rsi     # 0x133277
leaq 0x20(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0xde492
jmp 0xe5150