// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/src/deark-tar.c
// Function: de_tar_start_member_file
void de_tar_start_member_file(deark *c, dbuf *f)
{
	struct tar_ctx *tctx = NULL;
	struct tar_md *md = NULL;
	int tsidx;

	if(!c->tar_data) {
		de_tar_create_file(c);
	}
	tctx = (struct tar_ctx *)c->tar_data;
	if(!tctx) return;
	destroy_md(c, tctx->md);
	tctx->md = de_malloc(c, sizeof(struct tar_md));
	md = tctx->md;

	f->parent_dbuf = tctx->outf;

	md->headers_pos = tctx->outf->len;

	if(c->preserve_file_times_archives && f->fi_copy) {
		for(tsidx=0; tsidx<DE_TIMESTAMPIDX_COUNT; tsidx++) {
			//if(tsidx != DE_TIMESTAMPIDX_MODIFY) continue;

			if(f->fi_copy->timestamp[tsidx].is_valid) {
				md->tsdata[tsidx].timestamp = f->fi_copy->timestamp[tsidx];
			}
			else if(tsidx == DE_TIMESTAMPIDX_MODIFY) {
				// Special handling if we don't have a mod time.
				if(c->reproducible_output) {
					de_get_reproducible_timestamp(c, &md->tsdata[tsidx].timestamp);
				}
				else {
					de_cached_current_time_to_timestamp(c, &md->tsdata[tsidx].timestamp);
					// Although c->current_time is probably high precision, we treat it as
					// low precision, so as not to write an "mtime" extended header.
					// TODO: If we write "mtime" for some other reason, it can be high prec.
					md->tsdata[tsidx].timestamp.precision = DE_TSPREC_1SEC;
				}
			}
			else {
				// Unavailable timestamp that isn't the mod time.
				continue;
			}

			md->tsdata[tsidx].timestamp_unix = de_timestamp_to_unix_time(&md->tsdata[tsidx].timestamp);
		}
	}

	if(f->fi_copy && f->fi_copy->is_directory) {
		md->is_dir = 1;
	}

	md->namelen = de_strlen(f->name);
	if(md->is_dir) {
		// Append a '/' to directory names
		md->filename = de_malloc(c, (i64)md->namelen+2);
		de_snprintf(md->filename, md->namelen+2, "%s/", f->name);
		md->namelen = de_strlen(md->filename);
	}
	else {
		md->filename = de_strdup(c, f->name);
	}

	if(md->namelen>100) {
		md->need_exthdr_path = 1;
	}
	else if(!de_is_ascii((const u8*)md->filename, md->namelen)) {
		md->need_exthdr_path = 1;
	}

	md->extdata_nbytes_needed += 23; // For "size"; this is enough for 10TB

	if(md->need_exthdr_path) {
		// Likely an overestimate: up to 6 bytes for the item size,
		// 4 for the "path" string, 3 for field separators.
		md->extdata_nbytes_needed += (i64)md->namelen + 13;
	}

	prepare_timestamp_exthdr(c, md, DE_TIMESTAMPIDX_MODIFY);
	prepare_timestamp_exthdr(c, md, DE_TIMESTAMPIDX_ACCESS);
	prepare_timestamp_exthdr(c, md, DE_TIMESTAMPIDX_ATTRCHANGE);
	prepare_timestamp_exthdr(c, md, DE_TIMESTAMPIDX_CREATE);

	if(md->extdata_nbytes_needed>0) {
		md->has_exthdr = 1;
	}

	if(md->has_exthdr) {
		md->exthdr_num_data_blocks = (md->extdata_nbytes_needed+511)/512;
		md->headers_size = (1 + md->exthdr_num_data_blocks + 1) * 512;
	}
	else {
		md->exthdr_num_data_blocks = 0;
		md->headers_size = 512;
	}

	// Reserve space for the tar headers. We won't know the member file size
	// until it has been completely written, so we can't write the headers
	// yet. Instead we'll write them to headers_tmpdbuf, and seek back later
	// and patch them into the main tar file.
	dbuf_write_zeroes(tctx->outf, md->headers_size);

	f->offset_into_parent_dbuf = tctx->outf->len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r15
cmpq $0x0, 0x140(%rdi)
jne 0xee366
movq %r15, %rdi
callq 0xee20c
movq 0x140(%r15), %r12
testq %r12, %r12
je 0xee595
movq 0x20(%r12), %rsi
movq %r15, %rdi
callq 0xee318
movl $0x180, %esi            # imm = 0x180
movq %r15, %rdi
callq 0xe969b
movq %rax, %r14
movq %rax, 0x20(%r12)
movq %r12, (%rsp)
movq 0x8(%r12), %rax
movq %rax, 0x50(%rbx)
movq 0x18(%rax), %rax
movq %rax, 0x10(%r14)
cmpb $0x0, 0xdd(%r15)
je 0xee43d
cmpq $0x0, 0xd0(%rbx)
je 0xee43d
leaq 0x40(%r14), %r12
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0xd0(%rbx), %rax
cmpb $0x0, 0x18(%rax,%r13)
je 0xee3fc
addq %r13, %rax
addq $0x18, %rax
movups (%rax), %xmm0
movups %xmm0, (%r12,%r13,4)
leaq (%r12,%rbp), %rdi
callq 0xeb296
movq %rax, 0x10(%r12,%r13,4)
jmp 0xee401
testq %r13, %r13
je 0xee411
addq $0x10, %r13
addq $0x40, %rbp
cmpq $0x50, %r13
jne 0xee3ce
jmp 0xee43d
cmpb $0x0, 0xdf(%r15)
je 0xee429
leaq (%r12,%rbp), %rsi
movq %r15, %rdi
callq 0xebfb5
jmp 0xee3ec
leaq (%r12,%rbp), %rsi
movq %r15, %rdi
callq 0xeb6a8
movb $0x1e, 0x2(%r12,%r13,4)
jmp 0xee3ec
movq 0xd0(%rbx), %rax
testq %rax, %rax
je 0xee453
cmpb $0x0, 0x9(%rax)
je 0xee453
movb $0x1, (%r14)
movq 0x68(%rbx), %r12
movq %r12, %rdi
callq 0x180e0
movq %rax, 0x8(%r14)
cmpb $0x0, (%r14)
je 0xee4a8
addq $0x2, %rax
movq %r15, %rdi
movq %rax, %rsi
callq 0xe969b
movq %rax, 0x38(%r14)
movq 0x8(%r14), %rsi
addq $0x2, %rsi
movq 0x68(%rbx), %rcx
leaq 0x5a59d(%rip), %rdx     # 0x148a2c
movq %rax, %rdi
xorl %eax, %eax
callq 0xe8d67
movq 0x38(%r14), %rdi
callq 0x180e0
movq %rax, 0x8(%r14)
jmp 0xee4b7
movq %r15, %rdi
movq %r12, %rsi
callq 0xea3c7
movq %rax, 0x38(%r14)
movq 0x8(%r14), %rsi
cmpq $0x64, %rsi
movq (%rsp), %r15
ja 0xee4d2
movq 0x38(%r14), %rdi
callq 0xecaa8
testl %eax, %eax
jne 0xee4d7
movb $0x1, 0x3(%r14)
movq 0x28(%r14), %rax
leaq 0x17(%rax), %rcx
movq %rcx, 0x28(%r14)
cmpb $0x0, 0x3(%r14)
je 0xee4f9
movq 0x8(%r14), %rcx
addq %rcx, %rax
addq $0x24, %rax
movq %rax, 0x28(%r14)
movq %r14, %rdi
xorl %esi, %esi
callq 0xee5a4
movq %r14, %rdi
movl $0x2, %esi
callq 0xee5a4
movq %r14, %rdi
movl $0x3, %esi
callq 0xee5a4
movq %r14, %rdi
movl $0x1, %esi
callq 0xee5a4
movq 0x28(%r14), %rsi
testq %rsi, %rsi
jle 0xee538
movb $0x1, 0x1(%r14)
cmpb $0x0, 0x1(%r14)
je 0xee56f
leaq 0x1ff(%rsi), %rax
addq $0x3fe, %rsi            # imm = 0x3FE
testq %rax, %rax
cmovnsq %rax, %rsi
movq %rsi, %rax
sarq $0x9, %rax
movq %rax, 0x20(%r14)
andq $-0x200, %rsi           # imm = 0xFE00
addq $0x400, %rsi            # imm = 0x400
jmp 0xee57c
movq $0x0, 0x20(%r14)
movl $0x200, %esi            # imm = 0x200
movq %rsi, 0x18(%r14)
movq 0x8(%r15), %rdi
callq 0xf1346
movq 0x8(%r15), %rax
movq 0x18(%rax), %rax
movq %rax, 0x58(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq