// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/src/deark-dbuf.c
// Function: dbuf_close
void dbuf_close(dbuf *f)
{
	deark *c;
	if(!f) return;
	c = f->c;

	if(f->wbuffer_bytes_used!=0) dbuf_flush(f);

	if(c->enable_oinfo && f->is_managed) {
		u32 crc = 0;

		if(f->crco_for_oinfo) {
			crc = de_crcobj_getval(f->crco_for_oinfo);
		}
		de_msg(c, "Output file info: ID=%d CRC=%08x size=%"I64_FMT, f->file_id,
			crc, f->len);
	}

	if(f->btype==DBUF_TYPE_OFILE || f->btype==DBUF_TYPE_STDOUT) {
		c->total_output_size += f->len;
	}

	if(f->btype==DBUF_TYPE_MEMBUF && f->write_memfile_to_zip_archive) {
		de_zip_add_file_to_archive(c, f);
		if(f->name) {
			de_dbg3(c, "closing memfile %s", f->name);
		}
	}
	else if(f->writing_to_tar_archive) {
		de_tar_end_member_file(c, f);
	}

	switch(f->btype) {
	case DBUF_TYPE_IFILE:
	case DBUF_TYPE_OFILE:
		if(f->name) {
			de_dbg3(c, "closing file %s", f->name);
		}
		de_fclose(f->fp);
		f->fp = NULL;

		if(f->btype==DBUF_TYPE_OFILE && f->is_managed) {
			de_update_file_attribs(f, c->preserve_file_times);
		}
		break;
	case DBUF_TYPE_FIFO:
		de_fclose(f->fp);
		f->fp = NULL;
		break;
	case DBUF_TYPE_STDOUT:
		if(f->name && f->is_managed) {
			de_dbg3(c, "finished writing %s to stdout", f->name);
		}
		else if(!f->is_managed) {
			de_dbg3(c, "finished writing %s", f->name);
		}
		f->fp = NULL;
		break;
	case DBUF_TYPE_MEMBUF:
	case DBUF_TYPE_IDBUF:
	case DBUF_TYPE_ODBUF:
	case DBUF_TYPE_STDIN:
	case DBUF_TYPE_CUSTOM:
	case DBUF_TYPE_NULL:
		break;
	default:
		de_internal_err_nonfatal(c, "Don't know how to close this type of file (%d)", f->btype);
	}

	de_free(c, f->membuf_buf);
	de_free(c, f->name);
	de_free(c, f->rcache);
	de_free(c, f->wbuffer);
	if(f->crco_for_oinfo) de_crcobj_destroy(f->crco_for_oinfo);
	if(f->fi_copy) de_finfo_destroy(c, f->fi_copy);
	de_free(c, f);

	if(c->total_output_size > c->max_total_output_size) {
		// FIXME: Since we only do this check when a file is closed, it can
		// potentially be subverted in the (rare) case that Deark has multiple
		// output files open simultaneously.
		de_err(c, "Maximum total output size of %"I64_FMT" bytes exceeded",
			c->max_total_output_size);
		de_fatalerror(c);
	}
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0xf0742
movq %rdi, %r14
movq 0x8(%rdi), %rbx
movq 0x40(%rdi), %rdx
testq %rdx, %rdx
je 0xf055c
movq 0x48(%r14), %rsi
movq %r14, %rdi
callq 0xf0a88
movq $0x0, 0x40(%r14)
cmpb $0x0, 0x96(%rbx)
je 0xf059c
cmpb $0x0, 0x4(%r14)
je 0xf059c
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0xf0581
callq 0xeb865
movl %eax, %ecx
jmp 0xf0583
xorl %ecx, %ecx
movl 0x64(%r14), %edx
movq 0x18(%r14), %r8
leaq 0x58df7(%rip), %rsi     # 0x149389
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9461
movl (%r14), %eax
cmpl $0x5, %eax
je 0xf05ae
cmpl $0x3, %eax
je 0xf05bb
cmpl $0x2, %eax
jne 0xf05e9
movq 0x18(%r14), %rcx
addq %rcx, 0x40(%rbx)
cmpl $0x3, %eax
jne 0xf05e9
cmpb $0x0, 0x60(%r14)
je 0xf05e9
movq %rbx, %rdi
movq %r14, %rsi
callq 0xecf03
movq 0x68(%r14), %rdx
testq %rdx, %rdx
je 0xf05fb
leaq 0x58dd6(%rip), %rsi     # 0x1493b3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe889f
jmp 0xf05fb
cmpb $0x0, 0x61(%r14)
je 0xf05fb
movq %rbx, %rdi
movq %r14, %rsi
callq 0xee069
movl (%r14), %edx
cmpq $0x9, %rdx
ja 0xf0668
leaq 0x58b6d(%rip), %rax     # 0x149178
movslq (%rax,%rdx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq 0x68(%r14), %rdx
testq %rdx, %rdx
je 0xf062e
leaq 0x58da2(%rip), %rsi     # 0x1493c6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe889f
movq 0x10(%r14), %rdi
callq 0x1094a6
movq $0x0, 0x10(%r14)
cmpl $0x2, (%r14)
jne 0xf06b2
cmpb $0x0, 0x4(%r14)
je 0xf06b2
movzbl 0xdc(%rbx), %esi
movq %r14, %rdi
callq 0x1095be
jmp 0xf06b2
movq 0x10(%r14), %rdi
callq 0x1094a6
jmp 0xf06aa
leaq 0x58d99(%rip), %rsi     # 0x149408
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9640
jmp 0xf06b2
movq 0x68(%r14), %rdx
movb 0x4(%r14), %al
testq %rdx, %rdx
je 0xf0695
testb %al, %al
je 0xf0699
leaq 0x58d43(%rip), %rsi     # 0x1493d6
jmp 0xf06a0
testb %al, %al
jne 0xf06aa
leaq 0x58d54(%rip), %rsi     # 0x1493f4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe889f
movq $0x0, 0x10(%r14)
movq 0x78(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq 0x68(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq 0xc8(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq 0x48(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0xf06f6
callq 0xeb858
movq 0xd0(%r14), %rsi
testq %rsi, %rsi
je 0xf070a
movq %rbx, %rdi
callq 0xea50e
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
movq 0xb8(%rbx), %rdx
cmpq %rdx, 0x40(%rbx)
jle 0xf0742
leaq 0x58d0e(%rip), %rsi     # 0x149437
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0xe9522
addq $0x8, %rsp
popq %rbx
popq %r14
retq