// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/src/deark-zip.c
// Function: de_zip_add_file_to_archive
void de_zip_add_file_to_archive(deark *c, dbuf *f)
{
	struct zipw_ctx *zzz;
	struct zipw_md *md = NULL;
	int write_ntfs_times = 0;
	int write_UT_time = 0;

	md = de_malloc(c, sizeof(struct zipw_md));

	if(!c->zip_data) {
		// ZIP file hasn't been created yet
		if(!de_zip_create_file(c)) {
			de_fatalerror(c);
			goto done;
		}
	}

	zzz = (struct zipw_ctx*)c->zip_data;

	de_dbg(c, "adding to zip: name=%s len=%"I64_FMT, f->name, f->len);

	if(f->fi_copy) {
		if(f->fi_copy->is_directory) {
			md->is_directory = 1;
		}
		else if(f->fi_copy->is_volume_label) {
			md->is_volume_label = 1;
		}

		if(f->fi_copy->mode_flags&DE_MODEFLAG_EXE) {
			md->is_executable = 1;
		}
	}

	if(c->preserve_file_times_archives && f->fi_copy && f->fi_copy->timestamp[DE_TIMESTAMPIDX_MODIFY].is_valid) {
		md->modtime = f->fi_copy->timestamp[DE_TIMESTAMPIDX_MODIFY];
		if(md->modtime.precision>DE_TSPREC_1SEC) {
			write_ntfs_times = 1;
		}
	}
	else if(c->reproducible_output) {
		de_get_reproducible_timestamp(c, &md->modtime);
	}
	else {
		de_cached_current_time_to_timestamp(c, &md->modtime);

		// We only write the current time because ZIP format leaves us little
		// choice.
		// Note that although c->current_time is probably high precision,
		// we don't consider that good enough reason to force NTFS timestamps
		// to be written.
	}

	// Note: Timestamps other than the modification time are a low priority.
	// We'll write them in some cases, when it is easy to do so.
	if(c->preserve_file_times_archives && f->fi_copy) {
		md->actime = f->fi_copy->timestamp[DE_TIMESTAMPIDX_ACCESS];
		md->crtime = f->fi_copy->timestamp[DE_TIMESTAMPIDX_CREATE];
	}

	md->modtime_unix = de_timestamp_to_unix_time(&md->modtime);
	set_dos_modtime(md);

	if(is_valid_32bit_unix_time(md->modtime_unix)) {
		// Always write a Unix timestamp if we can.
		write_UT_time = 1;

		if(md->modtime_unix < 0) {
			// This negative Unix time is in range, but problematic,
			// so write NTFS times as well.
			write_ntfs_times = 1;
		}
	}
	else { // Out of range of ZIP's (signed int32) Unix style timestamps
		write_ntfs_times = 1;
	}

	if(write_ntfs_times) {
		md->modtime_as_FILETIME = de_timestamp_to_FILETIME(&md->modtime);
		if(md->modtime_as_FILETIME == 0) {
			write_ntfs_times = 0;
		}
		else {
			md->actime_as_FILETIME = de_timestamp_to_FILETIME(&md->actime);
			md->crtime_as_FILETIME = de_timestamp_to_FILETIME(&md->crtime);
		}
	}

	// Create ZIP "extra data" "Extended Timestamp" and "NTFS" fields,
	// containing the UTC timestamp.

	// Use temporary dbufs to help construct the extra field data.
	md->eflocal = dbuf_create_membuf(c, 256, 0);
	md->efcentral = dbuf_create_membuf(c, 256, 0);

	if(write_UT_time) {
		do_UT_times(c, md, md->eflocal, 0);
		do_UT_times(c, md, md->efcentral, 1);
	}

	if(write_ntfs_times) {
		// Note: Info-ZIP says: "In the current implementations, this field [...]
		// is only stored as local extra field.
		// But 7-Zip supports it *only* as a central extra field.
		// So we'll write both.
		do_ntfs_times(c, md, md->eflocal, 0);
		do_ntfs_times(c, md, md->efcentral, 1);
	}

	if(f->fi_copy && f->fi_copy->has_riscos_data) {
		do_riscos_attribs(c, md, f->fi_copy, md->eflocal);
		do_riscos_attribs(c, md, f->fi_copy, md->efcentral);
	}

	if(md->is_directory) {
		size_t nlen;
		char *name2;

		// Append a "/" to the name
		nlen = de_strlen(f->name);
		name2 = de_malloc(c, (i64)nlen+2);
		de_snprintf(name2, nlen+2, "%s/", f->name);

		zipw_add_memberfile(c, zzz, md, f, name2, MZ_NO_COMPRESSION);

		de_free(c, name2);
	}
	else {
		zipw_add_memberfile(c, zzz, md, f, f->name, zzz->cmprlevel);
	}

done:
	if(md) {
		dbuf_close(md->eflocal);
		dbuf_close(md->efcentral);
		de_free(c, md);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movl $0x70, %esi
callq 0xe969b
movq %rax, %r14
cmpq $0x0, 0x138(%rbx)
jne 0xed59f
movq %rbx, %rdi
callq 0xed42c
testl %eax, %eax
je 0xed85f
movq 0x138(%rbx), %rax
movq %rax, (%rsp)
movq 0x18(%r15), %rcx
movq 0x68(%r15), %rdx
leaq 0x5b454(%rip), %rsi     # 0x148a0d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0xd0(%r15), %rax
testq %rax, %rax
je 0xed5f2
cmpb $0x0, 0x9(%rax)
je 0xed5dc
movb $0x1, 0x59(%r14)
jmp 0xed5e7
cmpb $0x0, 0xa(%rax)
je 0xed5e7
movb $0x1, 0x5a(%r14)
testb $0x2, 0x14(%rax)
je 0xed5f2
movb $0x1, 0x58(%r14)
testq %rax, %rax
je 0xed61c
cmpb $0x0, 0xdd(%rbx)
je 0xed61c
cmpb $0x0, 0x18(%rax)
je 0xed61c
addq $0x18, %rax
movups (%rax), %xmm0
movups %xmm0, (%r14)
cmpb $0x1f, 0x2(%r14)
setb %bpl
jmp 0xed640
cmpb $0x0, 0xdf(%rbx)
je 0xed632
movq %rbx, %rdi
movq %r14, %rsi
callq 0xebfb5
jmp 0xed63d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xeb6a8
movb $0x1, %bpl
cmpb $0x0, 0xdd(%rbx)
je 0xed66e
movq 0xd0(%r15), %rax
testq %rax, %rax
je 0xed66e
movups 0x38(%rax), %xmm0
movups %xmm0, 0x10(%r14)
movq 0xd0(%r15), %rax
movups 0x28(%rax), %xmm0
movups %xmm0, 0x20(%r14)
movq %r14, %rdi
callq 0xeb296
movq %rax, 0x30(%r14)
cmpq $0x12cea5ff, %rax       # imm = 0x12CEA5FF
jg 0xed68e
leaq 0x28(%rsp), %r13
movl $0x12cea600, %edi       # imm = 0x12CEA600
jmp 0xed6a2
movabsq $0x10391447e, %rdi      # imm = 0x10391447E
cmpq %rdi, %rax
jle 0xed6b6
leaq 0x28(%rsp), %r13
movq %r13, %rsi
xorl %edx, %edx
callq 0xeace2
leaq 0x8(%rsp), %rsi
movq %r13, %rdi
jmp 0xed6be
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0xeb2df
movl 0x18(%rsp), %eax
movl 0x1c(%rsp), %ecx
shll $0xb, %eax
shll $0x5, %ecx
addl %eax, %ecx
movl 0x20(%rsp), %eax
sarl %eax
addl %ecx, %eax
movl %eax, 0x3c(%r14)
movl 0xc(%rsp), %eax
movl 0x10(%rsp), %ecx
shll $0x9, %eax
shll $0x5, %ecx
addl %eax, %ecx
movl 0x14(%rsp), %eax
addl %ecx, %eax
addl $0xfff08820, %eax       # imm = 0xFFF08820
movl %eax, 0x38(%r14)
movq 0x30(%r14), %r13
movl $0x80000000, %eax       # imm = 0x80000000
subq %rax, %r13
setb %al
movb $0x1, %r12b
testb %bpl, %al
jne 0xed743
movq %r14, %rdi
callq 0xeadee
movq %rax, 0x40(%r14)
testq %rax, %rax
je 0xed743
leaq 0x10(%r14), %rdi
callq 0xeadee
movq %rax, 0x48(%r14)
leaq 0x20(%r14), %rdi
callq 0xeadee
movq %rax, 0x50(%r14)
xorl %r12d, %r12d
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf0faf
movq %rax, 0x60(%r14)
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf0faf
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
movq %rax, 0x68(%r14)
cmpq %rcx, %r13
jb 0xed797
movq 0x60(%r14), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0xed898
movq 0x68(%r14), %rsi
movq %r14, %rdi
movl $0x1, %edx
callq 0xed898
testb %r12b, %r12b
jne 0xed7b4
movq 0x60(%r14), %rsi
movq %r14, %rdi
callq 0xeda06
movq 0x68(%r14), %rsi
movq %r14, %rdi
callq 0xeda06
movq 0xd0(%r15), %rsi
testq %rsi, %rsi
je 0xed7e5
cmpb $0x0, 0x10(%rsi)
je 0xed7e5
movq 0x60(%r14), %rdx
movq %r14, %rdi
callq 0xeda8f
movq 0xd0(%r15), %rsi
movq 0x68(%r14), %rdx
movq %r14, %rdi
callq 0xeda8f
cmpb $0x0, 0x59(%r14)
movq 0x68(%r15), %r8
je 0xed847
movq %r8, %rdi
callq 0x180e0
leaq 0x2(%rax), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe969b
movq %rax, %rbp
movq 0x68(%r15), %rcx
leaq 0x5b217(%rip), %rdx     # 0x148a2c
movq %rax, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0xe8d67
movq %rbx, %rdi
movq (%rsp), %rsi
movq %r14, %rdx
movq %r15, %rcx
movq %rbp, %r8
xorl %r9d, %r9d
callq 0xedb29
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xe9752
jmp 0xed867
movq (%rsp), %rsi
movl 0x10(%rsi), %r9d
movq %rbx, %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0xedb29
jmp 0xed867
movq %rbx, %rdi
callq 0xea084
testq %r14, %r14
je 0xed889
movq 0x60(%r14), %rdi
callq 0xf0b13
movq 0x68(%r14), %rdi
callq 0xf0b13
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq