// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
testl %ebp, %ebp
je 0xdbdea
movq $0x0, 0x8(%r14)
movb $0x1, 0x3(%r14)
testq %r15, %r15
je 0xdbe0e
movq 0x38(%r15), %rax
testq %rax, %rax
je 0xdbe0e
cmpb $0x0, 0x4(%rax)
je 0xdbe0e
cmpb $0x0, 0x5(%rax)
je 0xdbeb3
movq 0x28(%rax), %rdx
jmp 0xdbe2b
testq %r12, %r12
je 0xdbdfd
movq %r12, %rdi
callq 0xe9c2f
movq %rax, 0x8(%r14)
jmp 0xdbe01
movq 0x8(%r14), %rax
testq %rax, %rax
jns 0xdbe0e
movq $0x0, 0x8(%r14)
movq 0x28(%rbx), %rax
testq %rax, %rax
je 0xdbe31
cmpb $0x0, 0x6(%rax)
je 0xdbe31
cmpb $0x0, 0x7(%rax)
je 0xdbeb3
movq 0x8(%rax), %rdx
movq %rdx, 0x50(%r14)
jmp 0xdbe4e
movq 0x18(%rbx), %rsi
leaq 0x50(%r14), %r12
movq %rbx, %rdi
xorl %edx, %edx
movq %r12, %rcx
callq 0xf8ed5
testl %eax, %eax
je 0xdbeb3
movq (%r12), %rdx
leaq 0x6776f(%rip), %rsi     # 0x1435c4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq 0x50(%r14), %rsi
movq %r14, %r12
addq $0x10, %r12
movq %r12, %rdx
callq 0xdca1a
movq 0x38(%r14), %rax
movq %rax, 0x58(%r14)
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdcac1
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpb $0x0, 0x48(%r14)
je 0xdbecc
leaq 0x6773a(%rip), %rdx     # 0x1435eb
jmp 0xdbeba
leaq 0x676da(%rip), %rdx     # 0x143594
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0xdd001
jmp 0xdbf13
movq 0x20(%r14), %rax
cmpq 0x28(%r14), %rax
jne 0xdbf05
movq 0x18(%r14), %rax
cmpq 0x10(%r14), %rax
jne 0xdbf05
movq 0x18(%rbx), %rdi
movq 0x58(%r14), %rsi
callq 0xeed0d
cmpl $0x504b0102, %eax       # imm = 0x504B0102
jne 0xdbf69
movq 0x58(%r14), %rax
movl $0x60, %ecx
movq %rax, %r12
jmp 0xdbfa8
movw $0x101, (%r14)          # imm = 0x101
movb $0x1, 0x2(%r14)
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe8be2
cmpb $0x0, (%r14)
je 0xdbf56
cmpb $0x0, 0x1(%r14)
je 0xdbf56
cmpb $0x0, 0x2(%r14)
leaq 0x69179(%rip), %rax     # 0x1450b0
leaq 0x67750(%rip), %rcx     # 0x14368e
cmoveq %rax, %rcx
leaq 0x6771c(%rip), %rdx     # 0x143665
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0xdd001
movq %rbx, %rdi
movq %r14, %rsi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xe8be2
movq 0x50(%r14), %r12
subq 0x30(%r14), %r12
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeed0d
cmpl $0x504b0102, %eax       # imm = 0x504B0102
jne 0xdbfdc
leaq 0x689db(%rip), %rsi     # 0x144966
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movq %r12, 0x60(%r14)
movq %r12, %rax
subq 0x58(%r14), %rax
movl $0x70, %ecx
movq %rax, (%r14,%rcx)
movq 0x30(%r14), %rcx
addq %r12, %rcx
movq 0x50(%r14), %rax
cmpq %rax, %rcx
jg 0xdbfd1
movq 0x40(%r14), %rcx
addq %rcx, %rax
addq $0x16, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xdbffc
movw $0x101, (%r14)          # imm = 0x101
jmp 0xdbf10
movq 0x58(%r14), %rcx
leaq 0x67626(%rip), %rdx     # 0x14360d
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0xdd001
jmp 0xdbf13
movq %r12, 0x68(%r14)
leaq 0x67634(%rip), %rsi     # 0x14363b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movl $0x78, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r12
movq %r14, 0x8(%rax)
leaq 0x1057(%rip), %rax      # 0xdd090
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 0xdcd15
movb 0x39(%r12), %al
movb %al, (%r14)
movb 0x3a(%r12), %al
movb %al, 0x1(%r14)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpb $0x0, (%r14)
jne 0xdbf13
movq 0x68(%r14), %rdx
leaq 0x675b3(%rip), %rsi     # 0x143652
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movq %r14, %rsi
callq 0xdd1a7
testq %r15, %r15
je 0xdbf13
orb $0x1, 0x40(%r15)
jmp 0xdbf13