// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/zip.c
// Function: do_run_zip_relocator
static void do_run_zip_relocator(deark *c, de_module_params *mparams,
	int internalmode, const char *reloc_opt)
{
	struct zipreloc_ctx *d = NULL;
	struct fmtutil_specialexe_detection_data *edd_from_parent = NULL;
	struct zip_wcd_ctx *wcdctx_prescan = NULL;
	u32 sig;
	int found_cdir = 0;
	int eocd_found;

	d = de_malloc(c, sizeof(struct zipreloc_ctx));

	if(internalmode) {
		d->relocpos = 0;
		d->quiet = 1;
		if(mparams) {
			edd_from_parent = (struct fmtutil_specialexe_detection_data*)mparams->in_params.obj1;
		}
	}
	else {
		if(reloc_opt) {
			d->relocpos = de_atoi64(reloc_opt);
		}
		if(d->relocpos<0) d->relocpos = 0;
	}

	// (Trying to make Deark not call fmtutil_find_zip_eocd() more than once per
	// file, and it makes a mess of things...)
	if(edd_from_parent && edd_from_parent->zip_eocd_looked_for) {
		eocd_found = edd_from_parent->zip_eocd_found;
		if(eocd_found) {
			d->eocd_pos = edd_from_parent->zip_eocd_pos;
		}
	}
	else if(c->detection_data && c->detection_data->zip_eocd_looked_for) {
		eocd_found = (int)c->detection_data->zip_eocd_found;
		if(eocd_found) {
			d->eocd_pos = c->detection_data->zip_eocd_pos;
		}
	}
	else {
		eocd_found = fmtutil_find_zip_eocd(c, c->infile, 0, &d->eocd_pos);
	}
	if(!eocd_found) {
		zipreloc_err(c, d, "Not a ZIP file, or central directory not found.");
		goto done;
	}
	de_dbg(c, "end-of-central-dir record found at %"I64_FMT,
		d->eocd_pos);

	simple_read_eocd(c, c->infile, d->eocd_pos, &d->eocd);
	d->cdir_offset_reported = d->eocd.cdir_offset;

	de_dbg_indent(c, 1);
	simple_dbg_eocd(c, &d->eocd);
	de_dbg_indent(c, -1);

	if(d->eocd.is_likely_zip64) {
		zipreloc_err(c, d, "Relocating Zip64 is not supported");
		goto done;
	}

	if(d->eocd.cdir_num_entries_this_seg != d->eocd.cdir_num_entries_total) {
		d->errflag = 1;
		d->need_errmsg = 1;
		d->seg_id_mismatch_flag = 1;
		goto done;
	}

	if(d->eocd.cdir_starting_seg_num != d->eocd.this_seg_num) {
		d->errflag = 1;
		d->need_errmsg = 1;
		d->seg_id_mismatch_flag = 1;
		goto done;
	}

	sig = (u32)de_getu32be(d->cdir_offset_reported);
	if(sig==CODE_PK12) {
		found_cdir = 1;
		d->cdir_offset_actual = d->cdir_offset_reported;
	}

	if(!found_cdir) {
		i64 pos2;

		pos2 = d->eocd_pos - d->eocd.cdir_byte_size;
		sig = (u32)de_getu32be(pos2);
		if(sig==CODE_PK12) {
			de_dbg(c, "central dir found at %"I64_FMT, pos2);
			d->cdir_offset_actual = pos2;
			d->offset_correction = d->cdir_offset_actual - d->cdir_offset_reported;
			found_cdir = 1;
		}
	}

	if(!found_cdir) {
		zipreloc_err(c, d, "Central directory not found (expected at %"I64_FMT")",
			d->cdir_offset_reported);
		goto done;
	}

	if(d->cdir_offset_actual + d->eocd.cdir_byte_size > d->eocd_pos) {
		// We require the EOCD record to appear after the central dir.
		d->errflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	if(d->eocd_pos+22+d->eocd.archive_comment_len > c->infile->len) {
		d->errflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	// Pre-scan the central dir to find the offset of the local directory that
	// appears first in the file.
	d->min_ldir_offset = d->cdir_offset_actual; // initialize to max possible value
	de_dbg(c, "[scanning central dir]");
	de_dbg_indent(c, 1);

	wcdctx_prescan = de_malloc(c, sizeof(struct zip_wcd_ctx));
	wcdctx_prescan->userdata = (void*)d;
	wcdctx_prescan->cbfn = zipreloc_wcd_prescan;
	wcdctx_prescan->max_entries = d->eocd.cdir_num_entries_this_seg;
	wcdctx_prescan->inf = c->infile;
	wcdctx_prescan->inf_startpos = d->cdir_offset_actual;
	wcdctx_prescan->inf_endpos = c->infile->len;

	zip_wcd_run(c, wcdctx_prescan);
	d->errflag = wcdctx_prescan->errflag;
	d->need_errmsg = wcdctx_prescan->need_errmsg;

	de_dbg_indent(c, -1);

	if(d->errflag) goto done;

	de_dbg(c, "min ldir offs: %"I64_FMT, d->min_ldir_offset);
	zip_relocator_main(c, d);

	if(mparams) {
		// Inform the caller of success
		mparams->out_params.flags |= 0x1;
	}

done:
	de_free(c, wcdctx_prescan);
	if(d) {
		if(d->errflag && d->need_errmsg) {
			zipreloc_err(c, d, "Cannot optimize/relocate this ZIP file%s",
				(d->seg_id_mismatch_flag?" (disk spanning issue)":""));
		}
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r12
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
movl $0x90, %esi
callq 0xdecaf
movq %rax, %r14
testl %ebp, %ebp
je 0xd3223
andq $0x0, 0x8(%r14)
movb $0x1, 0x3(%r14)
testq %r15, %r15
je 0xd3244
movq 0x38(%r15), %rax
testq %rax, %rax
je 0xd3244
cmpb $0x0, 0x4(%rax)
je 0xd3244
cmpb $0x0, 0x5(%rax)
je 0xd32e5
movq 0x28(%rax), %rdx
jmp 0xd3261
testq %r12, %r12
je 0xd3236
movq %r12, %rdi
callq 0xdfb3b
movq %rax, 0x8(%r14)
jmp 0xd323a
movq 0x8(%r14), %rax
testq %rax, %rax
jns 0xd3244
andq $0x0, 0x8(%r14)
movq 0x28(%rbx), %rax
testq %rax, %rax
je 0xd3267
cmpb $0x0, 0x6(%rax)
je 0xd3267
cmpb $0x0, 0x7(%rax)
je 0xd32e5
movq 0x8(%rax), %rdx
movq %rdx, 0x50(%r14)
jmp 0xd3284
movq 0x18(%rbx), %rsi
leaq 0x50(%r14), %r12
movq %rbx, %rdi
xorl %edx, %edx
movq %r12, %rcx
callq 0xed59e
testl %eax, %eax
je 0xd32e5
movq (%r12), %rdx
leaq 0x62ee9(%rip), %rsi     # 0x136174
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq 0x50(%r14), %rsi
movq %r14, %r12
addq $0x10, %r12
movq %r12, %rdx
callq 0xd368d
movq 0x38(%r14), %rax
movq %rax, 0x58(%r14)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %rbx, %rdi
movq %r12, %rsi
callq 0xd3734
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpb $0x0, 0x48(%r14)
je 0xd32fe
leaq 0x62eb8(%rip), %rdx     # 0x13619b
jmp 0xd32ec
leaq 0x62e58(%rip), %rdx     # 0x136144
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0xd3b1f
jmp 0xd3343
movq 0x20(%r14), %rax
cmpq 0x28(%r14), %rax
jne 0xd3335
movq 0x18(%r14), %rax
cmpq 0x10(%r14), %rax
jne 0xd3335
movq 0x18(%rbx), %rdi
movq 0x58(%r14), %rsi
callq 0xe404f
cmpl $0x504b0102, %eax       # imm = 0x504B0102
jne 0xd339f
movq 0x58(%r14), %rax
pushq $0x60
popq %rcx
movq %rax, %r12
jmp 0xd33dc
movw $0x101, (%r14)          # imm = 0x101
movb $0x1, 0x2(%r14)
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r12, %rsi
callq 0xded32
cmpb $0x0, (%r14)
je 0xd3386
cmpb $0x0, 0x1(%r14)
je 0xd3386
cmpb $0x0, 0x2(%r14)
leaq 0x648f9(%rip), %rax     # 0x137c60
leaq 0x62ed0(%rip), %rcx     # 0x13623e
cmoveq %rax, %rcx
leaq 0x62e9c(%rip), %rdx     # 0x136215
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0xd3b1f
movq %rbx, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xded32
movq 0x50(%r14), %r12
subq 0x30(%r14), %r12
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe404f
cmpl $0x504b0102, %eax       # imm = 0x504B0102
jne 0xd3410
leaq 0x64155(%rip), %rsi     # 0x137516
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
movq %r12, 0x60(%r14)
movq %r12, %rax
subq 0x58(%r14), %rax
pushq $0x70
popq %rcx
movq %rax, (%r14,%rcx)
movq 0x30(%r14), %rcx
addq %r12, %rcx
movq 0x50(%r14), %rax
cmpq %rax, %rcx
jg 0xd3405
movq 0x40(%r14), %rcx
addq %rcx, %rax
addq $0x16, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xd3430
movw $0x101, (%r14)          # imm = 0x101
jmp 0xd3340
movq 0x58(%r14), %rcx
leaq 0x62da2(%rip), %rdx     # 0x1361bd
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0xd3b1f
jmp 0xd3343
movq %r12, 0x68(%r14)
leaq 0x62db0(%rip), %rsi     # 0x1361eb
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
pushq $0x78
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r12
movq %r14, 0x8(%rax)
leaq 0x745(%rip), %rax       # 0xd3bae
movq %rax, (%r12)
movq 0x20(%r14), %rax
movq %rax, 0x30(%r12)
movq 0x18(%rbx), %rax
movq %rax, 0x18(%r12)
movq 0x60(%r14), %rcx
movq %rcx, 0x20(%r12)
movq 0x18(%rax), %rax
movq %rax, 0x28(%r12)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xd383b
movb 0x39(%r12), %al
movb %al, (%r14)
movb 0x3a(%r12), %al
movb %al, 0x1(%r14)
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpb $0x0, (%r14)
jne 0xd3343
movq 0x68(%r14), %rdx
leaq 0x62d35(%rip), %rsi     # 0x136202
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x8(%r14), %rax
subq 0x68(%r14), %rax
movq %rax, 0x78(%r14)
leaq 0x62dca(%rip), %rsi     # 0x1362b4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
leaq 0x6859c(%rip), %rsi     # 0x13ba97
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xe4e62
movq %rax, 0x88(%r14)
movq 0x8(%r14), %rsi
movq %rax, %rdi
callq 0xe5d20
movq 0x60(%r14), %rdx
movq 0x68(%r14), %rsi
subq %rsi, %rdx
movq 0x18(%rbx), %rdi
movq 0x88(%r14), %rcx
callq 0xe45f4
pushq $0x78
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r13
movq %r14, 0x8(%rax)
leaq 0x773(%rip), %rax       # 0xd3cc1
movq %rax, (%r13)
movq 0x20(%r14), %rax
movq %rax, 0x30(%r13)
movq 0x18(%rbx), %rax
movq %rax, 0x18(%r13)
movq 0x60(%r14), %rcx
movq %rcx, 0x20(%r13)
movq 0x18(%rax), %rax
movq %rax, 0x28(%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0xd383b
cmpb $0x0, 0x39(%r13)
je 0xd3595
movb $0x1, (%r14)
movb 0x3a(%r13), %al
movb %al, 0x1(%r14)
jmp 0xd365b
cmpb $0x0, (%r14)
jne 0xd365b
movq 0x30(%r14), %rdx
movq 0x80(%r14), %rax
movq 0x60(%r14), %rsi
addq %rax, %rsi
subq %rax, %rdx
movq 0x18(%rbx), %rdi
movq 0x88(%r14), %rcx
callq 0xe45f4
movq 0x30(%r14), %rsi
addq 0x60(%r14), %rsi
movq 0x50(%r14), %rdx
subq %rsi, %rdx
movq 0x18(%rbx), %rdi
movq 0x88(%r14), %rcx
callq 0xe45f4
movq 0x18(%rbx), %rdi
movq 0x50(%r14), %rsi
movq 0x88(%r14), %rcx
pushq $0x4
popq %rbp
movq %rbp, %rdx
callq 0xe45f4
movq 0x88(%r14), %rdi
movq %rbp, %rsi
callq 0xe5d20
movq 0x50(%r14), %rsi
movq 0x88(%r14), %rcx
addq $0x8, %rsi
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rdx
callq 0xe45f4
movq 0x78(%r14), %rsi
movq 0x88(%r14), %rdi
addq 0x60(%r14), %rsi
callq 0xe5f7f
movq 0x40(%r14), %rdx
movq 0x50(%r14), %rsi
addq $0x14, %rsi
addq $0x2, %rdx
movq 0x18(%rbx), %rdi
movq 0x88(%r14), %rcx
callq 0xe45f4
movq 0x88(%r14), %rdi
callq 0xe5591
andq $0x0, 0x88(%r14)
movq %rbx, %rdi
movq %r13, %rsi
callq 0xded32
testq %r15, %r15
je 0xd3343
orb $0x1, 0x40(%r15)
jmp 0xd3343