// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
movq %rcx, %r12
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
movl $0x90, %esi
callq 0xe969b
movq %rax, %r14
testl %ebp, %ebp
je 0xdc77b
movq $0x0, 0x8(%r14)
movb $0x1, 0x3(%r14)
testq %r15, %r15
je 0xdc79b
movq 0x38(%r15), %rax
jmp 0xdc79d
testq %r12, %r12
je 0xdc78c
movq %r12, %rdi
callq 0xea786
movq %rax, 0x8(%r14)
cmpq $0x0, 0x8(%r14)
jns 0xdc79b
movq $0x0, 0x8(%r14)
xorl %eax, %eax
testq %rax, %rax
je 0xdc7b8
cmpb $0x0, 0x4(%rax)
je 0xdc7b8
cmpb $0x0, 0x5(%rax)
je 0xdc85a
addq $0x28, %rax
jmp 0xdc7d5
movq 0x28(%rbx), %rax
testq %rax, %rax
je 0xdc7de
cmpb $0x0, 0x6(%rax)
je 0xdc7de
cmpb $0x0, 0x7(%rax)
je 0xdc85a
addq $0x8, %rax
movq (%rax), %rax
movq %rax, 0x50(%r14)
jmp 0xdc7f4
movq 0x18(%rbx), %rsi
leaq 0x50(%r14), %rcx
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf93ef
testl %eax, %eax
je 0xdc85a
movq 0x50(%r14), %rdx
leaq 0x668e5(%rip), %rsi     # 0x1430e4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq 0x50(%r14), %rsi
leaq 0x10(%r14), %r12
movq %r12, %rdx
callq 0xdd3be
movq 0x38(%r14), %rax
movq %rax, 0x58(%r14)
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdd465
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpb $0x0, 0x48(%r14)
je 0xdc873
leaq 0x668b3(%rip), %rdx     # 0x14310b
jmp 0xdc861
leaq 0x66853(%rip), %rdx     # 0x1430b4
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0xdd99e
jmp 0xdc8bb
movq 0x20(%r14), %rax
cmpq 0x28(%r14), %rax
jne 0xdc8ad
movq 0x18(%r14), %rax
cmpq 0x10(%r14), %rax
jne 0xdc8ad
movq 0x18(%rbx), %rdi
movq 0x58(%r14), %rsi
callq 0xef32a
cmpl $0x504b0102, %eax       # imm = 0x504B0102
jne 0xdc91f
movq 0x58(%r14), %r12
movl $0x60, %eax
jmp 0xdc95b
movw $0x101, (%r14)          # imm = 0x101
movb $0x1, 0x2(%r14)
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe9752
testq %r14, %r14
je 0xdc916
cmpb $0x0, (%r14)
je 0xdc903
cmpb $0x0, 0x1(%r14)
je 0xdc903
cmpb $0x0, 0x2(%r14)
leaq 0x682ec(%rip), %rax     # 0x144bd0
leaq 0x668c3(%rip), %rcx     # 0x1431ae
cmoveq %rax, %rcx
leaq 0x6688f(%rip), %rdx     # 0x143185
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0xdd99e
movq %rbx, %rdi
movq %r14, %rsi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xe9752
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x50(%r14), %r12
subq 0x30(%r14), %r12
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef32a
cmpl $0x504b0102, %eax       # imm = 0x504B0102
jne 0xdc993
leaq 0x67b45(%rip), %rsi     # 0x144486
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq %r12, 0x60(%r14)
subq 0x58(%r14), %r12
movl $0x70, %eax
movq %r12, (%r14,%rax)
movq 0x50(%r14), %rcx
movq 0x60(%r14), %rax
movq 0x30(%r14), %rdx
addq %rax, %rdx
cmpq %rcx, %rdx
jg 0xdc988
movq 0x40(%r14), %rdx
addq %rdx, %rcx
addq $0x16, %rcx
movq 0x18(%rbx), %rdx
cmpq 0x18(%rdx), %rcx
jle 0xdc9b3
movw $0x101, (%r14)          # imm = 0x101
jmp 0xdc8b8
movq 0x58(%r14), %rcx
leaq 0x6678f(%rip), %rdx     # 0x14312d
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0xdd99e
jmp 0xdc8bb
movq %rax, 0x68(%r14)
leaq 0x6679d(%rip), %rsi     # 0x14315b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movl $0x78, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r12
movq %r14, 0x8(%rax)
leaq 0x103d(%rip), %rax      # 0xdda2d
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 0xdd6b9
movb 0x39(%r12), %al
movb %al, (%r14)
movb 0x3a(%r12), %al
movb %al, 0x1(%r14)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpb $0x0, (%r14)
jne 0xdc8bb
movq 0x68(%r14), %rdx
leaq 0x6671c(%rip), %rsi     # 0x143172
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movq %r14, %rsi
callq 0xddb49
testq %r15, %r15
je 0xdc8bb
orb $0x1, 0x40(%r15)
jmp 0xdc8bb