// File: /workspace/github_c_makefile_star2_O2/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 0xe5794
movq %rdi, %r14
movq 0x8(%rdi), %rbx
cmpq $0x0, 0x40(%rdi)
je 0xe55b4
movq %r14, %rdi
callq 0xe58e8
cmpb $0x0, 0x96(%rbx)
je 0xe55f4
cmpb $0x0, 0x4(%r14)
je 0xe55f4
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0xe55d9
callq 0xe0e95
movl %eax, %ecx
jmp 0xe55db
xorl %ecx, %ecx
movl 0x64(%r14), %edx
movq 0x18(%r14), %r8
leaq 0x568cf(%rip), %rsi     # 0x13beb9
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf42e
movl (%r14), %eax
cmpl $0x5, %eax
je 0xe5606
cmpl $0x3, %eax
je 0xe5613
cmpl $0x2, %eax
jne 0xe5641
movq 0x18(%r14), %rcx
addq %rcx, 0x40(%rbx)
cmpl $0x3, %eax
jne 0xe5641
cmpb $0x0, 0x60(%r14)
je 0xe5641
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe240a
movq 0x68(%r14), %rdx
testq %rdx, %rdx
je 0xe5653
leaq 0x568ae(%rip), %rsi     # 0x13bee3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdea83
jmp 0xe5653
cmpb $0x0, 0x61(%r14)
je 0xe5653
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe34e1
movl (%r14), %edx
cmpq $0x9, %rdx
ja 0xe56bd
leaq 0x56645(%rip), %rax     # 0x13bca8
movslq (%rax,%rdx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq 0x68(%r14), %rdx
testq %rdx, %rdx
je 0xe5686
leaq 0x5687a(%rip), %rsi     # 0x13bef6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdea83
movq 0x10(%r14), %rdi
callq 0xfca1b
andq $0x0, 0x10(%r14)
cmpl $0x2, (%r14)
jne 0xe5704
cmpb $0x0, 0x4(%r14)
je 0xe5704
movzbl 0xdc(%rbx), %esi
movq %r14, %rdi
callq 0xfcb33
jmp 0xe5704
movq 0x10(%r14), %rdi
callq 0xfca1b
jmp 0xe56ff
leaq 0x56874(%rip), %rsi     # 0x13bf38
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf5b8
jmp 0xe5704
movq 0x68(%r14), %rdx
movb 0x4(%r14), %al
testq %rdx, %rdx
je 0xe56ea
testb %al, %al
je 0xe56ee
leaq 0x5681e(%rip), %rsi     # 0x13bf06
jmp 0xe56f5
testb %al, %al
jne 0xe56ff
leaq 0x5682f(%rip), %rsi     # 0x13bf24
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdea83
andq $0x0, 0x10(%r14)
movq 0x78(%r14), %rsi
movq %rbx, %rdi
callq 0xded32
movq 0x68(%r14), %rsi
movq %rbx, %rdi
callq 0xded32
movq 0xc8(%r14), %rsi
movq %rbx, %rdi
callq 0xded32
movq 0x48(%r14), %rsi
movq %rbx, %rdi
callq 0xded32
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0xe5748
callq 0xe0e8d
movq 0xd0(%r14), %rsi
testq %rsi, %rsi
je 0xe575c
movq %rbx, %rdi
callq 0xdfd24
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
movq 0xb8(%rbx), %rdx
cmpq %rdx, 0x40(%rbx)
jle 0xe5794
leaq 0x567ec(%rip), %rsi     # 0x13bf67
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0xdf4d0
addq $0x8, %rsp
popq %rbx
popq %r14
retq