// File: /workspace/github_c_makefile_star2_O1/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 0xf0d2c
movq %rdi, %r14
movq 0x8(%rdi), %rbx
movq 0x40(%rdi), %rdx
testq %rdx, %rdx
je 0xf0b44
movq 0x48(%r14), %rsi
movq %r14, %rdi
callq 0xf1072
movq $0x0, 0x40(%r14)
cmpb $0x0, 0x96(%rbx)
je 0xf0b84
cmpb $0x0, 0x4(%r14)
je 0xf0b84
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0xf0b69
callq 0xebe2a
movl %eax, %ecx
jmp 0xf0b6b
xorl %ecx, %ecx
movl 0x64(%r14), %edx
movq 0x18(%r14), %r8
leaq 0x58277(%rip), %rsi     # 0x148df1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9fc3
movl (%r14), %eax
cmpl $0x5, %eax
je 0xf0b96
cmpl $0x3, %eax
je 0xf0ba3
cmpl $0x2, %eax
jne 0xf0bd1
movq 0x18(%r14), %rcx
addq %rcx, 0x40(%rbx)
cmpl $0x3, %eax
jne 0xf0bd1
cmpb $0x0, 0x60(%r14)
je 0xf0bd1
movq %rbx, %rdi
movq %r14, %rsi
callq 0xed564
movq 0x68(%r14), %rdx
testq %rdx, %rdx
je 0xf0be3
leaq 0x58256(%rip), %rsi     # 0x148e1b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9461
jmp 0xf0be3
cmpb $0x0, 0x61(%r14)
je 0xf0be3
movq %rbx, %rdi
movq %r14, %rsi
callq 0xee66d
movl (%r14), %edx
cmpq $0x9, %rdx
ja 0xf0c50
leaq 0x57fed(%rip), %rax     # 0x148be0
movslq (%rax,%rdx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq 0x68(%r14), %rdx
testq %rdx, %rdx
je 0xf0c16
leaq 0x58222(%rip), %rsi     # 0x148e2e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9461
movq 0x10(%r14), %rdi
callq 0x1099be
movq $0x0, 0x10(%r14)
cmpl $0x2, (%r14)
jne 0xf0c9c
cmpb $0x0, 0x4(%r14)
je 0xf0c9c
movzbl 0xdc(%rbx), %esi
movq %r14, %rdi
callq 0x109ad6
jmp 0xf0c9c
movq 0x10(%r14), %rdi
callq 0x1099be
jmp 0xf0c94
leaq 0x58219(%rip), %rsi     # 0x148e70
movq %rbx, %rdi
xorl %eax, %eax
callq 0xea1a2
jmp 0xf0c9c
movq 0x68(%r14), %rdx
testq %rdx, %rdx
je 0xf0c7c
cmpb $0x0, 0x4(%r14)
je 0xf0c7c
leaq 0x581c4(%rip), %rsi     # 0x148e3e
jmp 0xf0c8a
cmpb $0x0, 0x4(%r14)
jne 0xf0c94
leaq 0x581d2(%rip), %rsi     # 0x148e5c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9461
movq $0x0, 0x10(%r14)
movq 0x78(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq 0x68(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq 0xc8(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq 0x48(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0xf0ce0
callq 0xebe1d
movq 0xd0(%r14), %rsi
testq %rsi, %rsi
je 0xf0cf4
movq %rbx, %rdi
callq 0xea9b3
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
movq 0xb8(%rbx), %rdx
cmpq %rdx, 0x40(%rbx)
jle 0xf0d2c
leaq 0x5818c(%rip), %rsi     # 0x148e9f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0xea084
addq $0x8, %rsp
popq %rbx
popq %r14
retq