// File: /workspace/github_c_makefile_star2_O1/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 0xf0402
leaq 0x20(%rsp), %r14
movl $0x80, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0xe8ded
movb (%r14), %dl
testb %dl, %dl
je 0xf0407
leaq 0x21(%rsp), %rax
movabsq $0x1000000000000d, %rcx # imm = 0x1000000000000D
leal -0x30(%rdx), %esi
cmpb $0xa, %sil
jb 0xf03f1
movl %edx, %esi
andb $-0x21, %sil
addb $-0x41, %sil
cmpb $0x1a, %sil
jb 0xf03f1
movzbl %dl, %edx
addl $-0x2b, %edx
cmpl $0x34, %edx
ja 0xf03fc
btq %rdx, %rcx
jae 0xf03fc
movb (%rax), %dl
incq %rax
testb %dl, %dl
jne 0xf03c7
jmp 0xf0407
movb $0x5f, -0x1(%rax)
jmp 0xf03f1
movb $0x0, 0x20(%rsp)
testq %r13, %r13
sete %al
testq %r12, %r12
sete %cl
orb %al, %cl
jne 0xf0430
cmpb $0x0, 0x8(%r12)
je 0xf0430
leaq 0x58873(%rip), %rsi     # 0x148c99
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movl $0xd8, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r14
movq %rbx, 0x8(%rax)
movl $0xffffffff, 0x64(%rax) # imm = 0xFFFFFFFF
movq 0xb0(%rbx), %rax
movq %rax, 0x20(%r14)
movb $0x1, 0x4(%r14)
testq %r12, %r12
je 0xf0486
cmpb $0x0, 0xa(%r12)
je 0xf0477
cmpl $0x1, 0x7c(%rbx)
jne 0xf04be
cmpl $0x1, 0x80(%rbx)
jne 0xf04be
cmpb $0x0, 0x9(%r12)
sete %cl
setne %bpl
jmp 0xf048a
xorl %ebp, %ebp
movb $0x1, %cl
testb %bpl, %bpl
je 0xf049c
cmpb $0x0, 0xcf(%rbx)
je 0xf0587
movl 0x8c(%rbx), %eax
cmpl $0x2, %eax
je 0xf04ca
cmpl $0x1, %eax
jne 0xf04dc
testb $0x1, %r15b
je 0xf04dc
leaq 0x58848(%rip), %rsi     # 0x148d01
jmp 0xf058e
leaq 0x5880c(%rip), %rsi     # 0x148cd1
jmp 0xf058e
testb $0x1, %r15b
jne 0xf04dc
leaq 0x58844(%rip), %rsi     # 0x148d1b
jmp 0xf058e
movb %cl, 0xf(%rsp)
movl 0x38(%rbx), %eax
movl %eax, 0x64(%r14)
incl %eax
movq 0x150(%rbx), %rcx
testq %rcx, %rcx
leaq 0x5b108(%rip), %rdx     # 0x14b602
cmovneq %rcx, %rdx
movq %rdx, 0x10(%rsp)
movl %eax, 0x38(%rbx)
testq %r12, %r12
movl %r15d, 0x1c(%rsp)
je 0xf0551
movq (%r12), %rdi
callq 0xf6c00
testl %eax, %eax
je 0xf0551
movq (%r12), %rdi
callq 0xf71a1
leaq 0x1(%rax), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe969b
movq (%r12), %rdi
movq %rax, %rsi
movq %r15, %rdx
movq %rax, %r15
xorl %ecx, %ecx
movl $0x2, %r8d
callq 0xf727b
jmp 0xf0554
xorl %r15d, %r15d
movl 0x7c(%rbx), %eax
cmpl $0x1, %eax
jne 0xf05b0
testq %r12, %r12
je 0xf05b0
cmpq $0x0, 0x150(%rbx)
jne 0xf05b0
cmpb $0x0, 0x9(%r12)
je 0xf05b0
cmpb $0x0, 0xb(%r12)
je 0xf05a0
leaq 0x4b034(%rip), %rsi     # 0x13b5b6
jmp 0xf0630
leaq 0x58759(%rip), %rsi     # 0x148ce7
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
jmp 0xf07da
cmpb $0x0, 0xc(%r12)
je 0xf05b0
cmpb $0x0, 0xd(%r12)
jne 0xf057b
movq 0x158(%rbx), %rsi
testq %rsi, %rsi
je 0xf05ce
movl 0x64(%r14), %ecx
cmpl 0x98(%rbx), %ecx
setne %cl
orb %bpl, %cl
je 0xf0630
cmpl $0x1, %eax
jne 0xf0601
testq %r12, %r12
je 0xf0601
cmpq $0x0, 0x150(%rbx)
jne 0xf0601
testq %r15, %r15
je 0xf0601
cmpb $0x0, 0x8(%r12)
je 0xf0601
leaq 0x1a0(%rsp), %rdi
movl $0x1f4, %edx            # imm = 0x1F4
movq %r15, %rsi
jmp 0xf063d
testq %r13, %r13
je 0xf0644
testq %r15, %r15
je 0xf0644
leaq 0x4a170(%rip), %rdx     # 0x13a782
leaq 0xa0(%rsp), %rdi
leaq 0x20(%rsp), %r8
movl $0x100, %esi            # imm = 0x100
movq %r15, %rcx
xorl %eax, %eax
callq 0xe8d67
jmp 0xf0664
leaq 0x1a0(%rsp), %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0xe8ded
jmp 0xf0693
testq %r13, %r13
je 0xf08fd
leaq 0xa0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0xe8ded
movq 0x10(%rsp), %r13
movl 0x64(%r14), %r8d
leaq 0x586c3(%rip), %rdx     # 0x148d37
leaq 0x1a0(%rsp), %rdi
leaq 0xa0(%rsp), %r9
movl $0x1f4, %esi            # imm = 0x1F4
movq %r13, %rcx
xorl %eax, %eax
callq 0xe8d67
leaq 0x1a0(%rsp), %rsi
movq %rbx, %rdi
callq 0xea3c7
movq %rax, 0x68(%r14)
testq %r12, %r12
je 0xf07cc
movq %rbx, %rdi
callq 0xea9a9
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)
movl $0x18, %ecx
movups (%r12,%rcx), %xmm0
movups %xmm0, (%rax,%rcx)
addq $0x10, %rcx
cmpq $0x68, %rcx
jne 0xf06fa
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)
movl 0x98(%r12), %ecx
movl %ecx, 0x98(%rax)
movl 0x9c(%r12), %ecx
movl %ecx, 0x9c(%rax)
movl 0xa4(%r12), %ecx
movl %ecx, 0xa4(%rax)
movl 0xa8(%r12), %ecx
movl %ecx, 0xa8(%rax)
movb $0x0, 0x11(%r12)
movq 0xd0(%r14), %rdi
cmpb $0x0, 0x18(%rdi)
je 0xf07a1
cmpb $0x2, 0x19(%rdi)
jne 0xf07a1
movq 0x100(%rbx), %rsi
testq %rsi, %rsi
je 0xf07a1
addq $0x18, %rdi
negq %rsi
callq 0xeb2ca
movq 0xd0(%r14), %rdi
cmpb $0x0, 0x68(%rdi)
je 0xf07cc
cmpb $0x2, 0x69(%rdi)
jne 0xf07cc
movq 0x100(%rbx), %rsi
testq %rsi, %rsi
je 0xf07cc
addq $0x68, %rdi
negq %rsi
callq 0xeb2ca
movl 0x64(%r14), %eax
movl 0x98(%rbx), %ecx
cmpl %ecx, %eax
jge 0xf0801
movl $0x0, (%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe9752
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 0xf08cd
incl 0x3c(%rbx)
movq 0x148(%rbx), %rdi
testq %rdi, %rdi
je 0xf083e
movq 0x68(%r14), %rdx
leaq 0x1ff75(%rip), %rsi     # 0x1107a0
xorl %eax, %eax
callq 0xf0ec4
movq 0x148(%rbx), %rdi
callq 0xf0f7d
btl $0x9, 0x1c(%rsp)
jb 0xf0871
cmpb $0x0, 0x12b(%rbx)
je 0xf0871
movq 0x8(%r14), %rdi
cmpb $0x0, 0x12c(%rdi)
jne 0xf0871
cmpq $0x0, 0x48(%r14)
jne 0xf0871
movl $0x200, %esi            # imm = 0x200
callq 0xe969b
movq %rax, 0x48(%r14)
cmpb $0x0, 0x96(%rbx)
je 0xf088e
movq %rbx, %rdi
movl $0x10, %esi
callq 0xeba56
movq %rax, 0x80(%r14)
cmpb $0x0, 0x94(%rbx)
je 0xf0938
movl $0x0, (%r14)
cmpb $0x0, 0x95(%rbx)
je 0xf0999
movl 0x64(%r14), %edx
movq 0x68(%r14), %rcx
leaq 0x584a6(%rip), %rsi     # 0x148d64
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9fc3
jmp 0xf07e1
movl $0x0, (%r14)
jne 0xf07e1
cmpb $0x0, 0xa0(%rbx)
jne 0xf07e1
leaq 0x58454(%rip), %rsi     # 0x148d42
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0xf07e1
testq %r15, %r15
sete %al
xorb $0x1, %bpl
orb %al, %bpl
movq 0x10(%rsp), %r13
jne 0xf09b3
leaq 0x58414(%rip), %rdx     # 0x148d30
leaq 0xa0(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %r15, %rcx
xorl %eax, %eax
callq 0xe8d67
jmp 0xf0669
movl 0x7c(%rbx), %eax
cmpl $0x2, %eax
je 0xf0a4d
cmpl $0x1, %eax
jne 0xf0a7c
cmpl $0x2, 0x80(%rbx)
jne 0xf09e4
movq 0x68(%r14), %rdx
leaq 0x58405(%rip), %rsi     # 0x148d6a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9ef5
movl $0x8, (%r14)
movabsq $0x100000000000000, %rax # imm = 0x100000000000000
movq %rax, 0x20(%r14)
movb $0x1, 0x61(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xee343
jmp 0xf07e1
movq 0x68(%r14), %rdx
leaq 0x2430b(%rip), %rsi     # 0x114caf
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9fc3
jmp 0xf07e1
testq %r15, %r15
je 0xf09cd
leaq 0xa0(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %r15, %rsi
jmp 0xf0b09
cmpb $0x0, 0xf(%rsp)
je 0xf0af5
leaq 0x44a52(%rip), %rsi     # 0x135431
jmp 0xf0afc
cmpl $0x2, %eax
je 0xf0a4d
cmpl $0x1, %eax
jne 0xf0a7c
movq 0x68(%r14), %rdx
leaq 0x58383(%rip), %rsi     # 0x148d80
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9ef5
movl $0x3, (%r14)
movq $0x77359400, 0x20(%r14) # imm = 0x77359400
cmpb $0x0, 0xf(%rsp)
movl $0x10000, %eax          # imm = 0x10000
movq %r15, %r12
movl $0x10, %r15d
cmovneq %rax, %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe969b
movq %rax, 0x78(%r14)
movq %r15, 0x70(%r14)
movq %r12, %r15
movb $0x1, 0x60(%r14)
jmp 0xf07e1
movq 0x68(%r14), %rdx
leaq 0x5833e(%rip), %rsi     # 0x148d96
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9ef5
movl $0x5, (%r14)
movq 0x9e560(%rip), %rax     # 0x18efd0
movq (%rax), %rax
movq %rax, 0x10(%r14)
jmp 0xf07e1
movq 0x68(%r14), %rdx
leaq 0x58326(%rip), %rsi     # 0x148dad
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9ef5
movl $0x2, (%r14)
movq 0x68(%r14), %rsi
movl 0xd8(%rbx), %r8d
leaq 0xa0(%rsp), %rdx
movl $0xc8, %ecx
movq %rbx, %rdi
xorl %r9d, %r9d
callq 0x1099c3
movq %rax, 0x10(%r14)
testq %rax, %rax
jne 0xf07e1
movq 0x68(%r14), %rdx
leaq 0x581af(%rip), %rsi     # 0x148c82
leaq 0xa0(%rsp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movl $0x0, (%r14)
movb $0x1, 0x4c(%rbx)
jmp 0xf07e1
leaq 0x4f6eb(%rip), %rsi     # 0x1401e7
leaq 0xa0(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0xe8ded
jmp 0xf0669