// File: /workspace/github_c_makefile_star2_O2/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
movq 0x140(%rdi), %r12
testq %r12, %r12
jne 0xe31e9
movq %r15, %rdi
callq 0xe3084
movq 0x140(%r15), %r12
testq %r12, %r12
je 0xe3404
movq 0x20(%r12), %rsi
movq %r15, %rdi
callq 0xe3189
movl $0x180, %esi            # imm = 0x180
movq %r15, %rdi
callq 0xdecaf
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 0xe32b0
cmpq $0x0, 0xd0(%rbx)
je 0xe32b0
movq %r14, %r12
addq $0x40, %r12
xorl %ebp, %ebp
xorl %r13d, %r13d
cmpq $0x50, %r13
je 0xe32b0
movq 0xd0(%rbx), %rax
cmpb $0x0, 0x18(%rax,%r13)
je 0xe3278
addq %r13, %rax
addq $0x18, %rax
movups (%rax), %xmm0
movups %xmm0, (%r12,%r13,4)
leaq (%r12,%rbp), %rdi
callq 0xe0461
movq %rax, 0x10(%r12,%r13,4)
jmp 0xe327d
testq %r13, %r13
je 0xe3287
addq $0x10, %r13
addq $0x40, %rbp
jmp 0xe3244
cmpb $0x0, 0xdf(%r15)
je 0xe329e
movq %r15, %rdi
movq %r12, %rsi
callq 0xe0fba
jmp 0xe3268
movq %r15, %rdi
movq %r12, %rsi
callq 0xe07c9
movb $0x1e, 0x42(%r14)
jmp 0xe3268
movq 0xd0(%rbx), %rax
testq %rax, %rax
je 0xe32ca
cmpb $0x0, 0x9(%rax)
je 0xe32ca
movb $0x1, (%r14)
xorl %ebp, %ebp
jmp 0xe32d2
cmpb $0x0, (%r14)
sete %bpl
movq 0x68(%rbx), %r12
movq %r12, %rdi
callq 0x180e0
movq %rax, 0x8(%r14)
testb %bpl, %bpl
je 0xe32ff
movq %r15, %rdi
movq %r12, %rsi
callq 0xdf791
movq %rax, %r15
movq %rax, 0x38(%r14)
movq 0x8(%r14), %rax
jmp 0xe333f
addq $0x2, %rax
movq %r15, %rdi
movq %rax, %rsi
callq 0xdecaf
movq %rax, 0x38(%r14)
movq 0x8(%r14), %rsi
addq $0x2, %rsi
movq 0x68(%rbx), %rcx
leaq 0x587cf(%rip), %rdx     # 0x13baf4
movq %rax, %rdi
xorl %eax, %eax
callq 0xde40c
movq 0x38(%r14), %r15
movq %r15, %rdi
callq 0x180e0
movq %rax, 0x8(%r14)
movq (%rsp), %r12
cmpq $0x65, %rax
jae 0xe336d
movq %r15, %rdi
movq %rax, %rsi
callq 0xe1a05
testl %eax, %eax
je 0xe336d
movq 0x28(%r14), %rax
leaq 0x17(%rax), %rcx
cmpb $0x0, 0x3(%r14)
movq %rcx, 0x28(%r14)
jne 0xe3376
jmp 0xe3385
movb $0x1, 0x3(%r14)
movq 0x28(%r14), %rax
movq 0x8(%r14), %rcx
addq %rcx, %rax
addq $0x24, %rax
movq %rax, 0x28(%r14)
movq %r14, %rdi
xorl %esi, %esi
callq 0xe341c
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0xe341c
pushq $0x3
popq %rsi
movq %r14, %rdi
callq 0xe341c
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0xe341c
movq 0x28(%r14), %rax
testq %rax, %rax
jle 0xe33c0
movb $0x1, 0x1(%r14)
jmp 0xe33c7
cmpb $0x0, 0x1(%r14)
je 0xe3413
addq $0x1ff, %rax            # imm = 0x1FF
movl $0x200, %ecx            # imm = 0x200
cqto
idivq %rcx
movq %rax, %rsi
shlq $0x9, %rsi
addq $0x400, %rsi            # imm = 0x400
movq %rax, 0x20(%r14)
movq %rsi, 0x18(%r14)
movq 0x8(%r12), %rdi
callq 0xe5d20
movq 0x8(%r12), %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
movl $0x200, %esi            # imm = 0x200
xorl %eax, %eax
jmp 0xe33e5