// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/arj.c
// Function: de_run_arj
static void de_run_arj(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	u8 archive_start_req_flag = 0;
	u8 used_scan = 0;
	i64 archive_start_req = 0;
	i64 exact_archive_start = 0;
	const char *s;
	u8 scan_opt; // 0 or 1, 0xff for unset
	int modcode_R = 0;
	struct options_struct *arj_opts = NULL;
	struct arjvol_struct *firstvol = NULL;
	struct arjvol_struct *mpvol = NULL;

	arj_opts = de_malloc(c, sizeof(struct options_struct));
	arj_opts->mparams = mparams;

	if(c->module_disposition==DE_MODDISP_INTERNAL) {
		modcode_R = de_havemodcode(c, mparams, 'R');
	}

	if(c->module_disposition!=DE_MODDISP_INTERNAL) {
		scan_opt = (u8)de_get_ext_option_bool(c, "arj:scan", 0xff);
	}
	else {
		scan_opt = 0xff;
	}

	// Starting point for the scan for the archive header
	// (or the exact header pos if scan=0).
	if(c->module_disposition!=DE_MODDISP_INTERNAL) {
		s = de_get_ext_option(c, "arj:entrypoint");
		if(s) {
			archive_start_req_flag = 1;
			archive_start_req = de_atoi64(s);
		}
	}

	// Try various things to find the start of the ARJ archive.
	// TODO?: This is unpleasantly complicated.

	if(archive_start_req_flag && scan_opt==0) {
		exact_archive_start = archive_start_req;
		goto after_archive_start_known;
	}

	if(modcode_R && mparams && mparams->in_params.obj1) {
		struct fmtutil_specialexe_detection_data *edd;

		edd = (struct fmtutil_specialexe_detection_data*)mparams->in_params.obj1;
		if(edd->payload_valid) {
			exact_archive_start = edd->payload_pos;
			goto after_archive_start_known;
		}
	}

	if(c->module_disposition==DE_MODDISP_AUTODETECT) {
		exact_archive_start = 0; // The "identify" routine only looks here
		goto after_archive_start_known;
	}

	if(scan_opt!=0) {
		int ret;
		i64 scan_startpos;
		u8 allow_bad_crc;

		if(archive_start_req_flag) {
			scan_startpos = archive_start_req;
			allow_bad_crc = 1;
		}
		else if(is_exe_format(c)) {
			scan_startpos = get_exe_overlay_pos(c);
			allow_bad_crc = 0;
		}
		else {
			scan_startpos = 0;
			allow_bad_crc = 1;
		}

		if(allow_bad_crc) {
			if(is_arj_data_at(c, scan_startpos)) {
				exact_archive_start = scan_startpos;
				goto after_archive_start_known;
			}
		}

		ret = fmtutil_scan_for_arj_data(c->infile, scan_startpos, c->infile->len-scan_startpos, 0,
			&exact_archive_start);
		if(ret) {
			if(exact_archive_start != archive_start_req) {
				used_scan = 1;
			}
			goto after_archive_start_known;
		}
	}

	exact_archive_start = 0; // Give up

after_archive_start_known:
	arj_opts->archive_start = exact_archive_start;

	if(dbuf_memcmp(c->infile, arj_opts->archive_start, g_arj_hdr_id, 2)) {
		if(!modcode_R) {
			de_err(c, "Not an ARJ file, or could not find ARJ data");
		}
		goto done;
	}

	if(used_scan) {
		de_dbg(c, "ARJ data found at %"I64_FMT, arj_opts->archive_start);
	}

	if(c->module_disposition!=DE_MODDISP_INTERNAL) {
		const char *s;

		s = de_get_ext_option(c, "arj:reloc");
		if(s) {
			arj_opts->reloc_opt = s;
			do_run_arj_relocator(c, arj_opts);
			goto done;
		}
	}

	if(modcode_R) {
		do_run_arj_relocator(c, arj_opts);
		goto done;
	}

	d = de_malloc(c, sizeof(lctx));

	de_declare_fmt(c, "ARJ");
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_UNKNOWN);
	if(c->mp_opt_used) {
		if(c->module_disposition!=DE_MODDISP_INTERNAL) {
			// Did the user use "-mp"?
			d->mp_opt_used = 1;
		}
	}

	d->crco = de_crcobj_create(c, DE_CRCOBJ_CRC32_IEEE);

	firstvol = de_malloc(c, sizeof(struct arjvol_struct));
	firstvol->volnum = 0;
	firstvol->inf = c->infile;
	firstvol->archive_start = arj_opts->archive_start;

	do_arj_volume(c, d, firstvol);

	if(c->mp_data) {
		int k;

		for(k=0; k<c->mp_data->count; k++) {
			dbuf *inf_part;

			de_dbg(c, "[mp file %d: %s]", k, c->mp_data->item[k].fn);
			if(mpvol) {
				de_free(c, mpvol);
			}
			mpvol = de_malloc(c, sizeof(struct arjvol_struct));
			mpvol->volnum = k+1;
			mpvol->archive_start = 0;

			inf_part = de_mp_acquire_dbuf(c, k+1);
			if(!inf_part) goto done;  // todo: error?
			mpvol->inf = inf_part;
			do_arj_volume(c, d, mpvol);
			de_mp_release_dbuf(c, k+1, &inf_part);
		}
	}

done:
	if(d) {
		arj_logical_member_finish_and_free(c, d, 1);
		de_crcobj_destroy(d->crco);
		de_free(c, d);
	}
	de_free(c, firstvol);
	de_free(c, mpvol);
	de_free(c, arj_opts);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r12
movq %rdi, %rbx
movq $0x0, 0x18(%rsp)
movl $0x18, %esi
callq 0xe8b2b
movq %r12, (%rax)
cmpl $0x3, 0x24(%rbx)
movq %rax, 0x10(%rsp)
jne 0x35cab
movq %rbx, %rdi
movq %r12, %rsi
movl $0x52, %edx
callq 0xea4d9
testl %eax, %eax
setne %al
movl %eax, 0xc(%rsp)
cmpl $0x3, 0x24(%rbx)
jne 0x35cb3
movb $0x1, %r14b
xorl %r15d, %r15d
jmp 0x35d04
movl $0x0, 0xc(%rsp)
leaq 0xe2295(%rip), %rsi     # 0x117f4f
movq %rbx, %rdi
movl $0xff, %edx
callq 0xe9bd0
movl %eax, %ebp
testb %bpl, %bpl
sete %r13b
movb $0x1, %r14b
cmpl $0x3, 0x24(%rbx)
je 0x35d09
leaq 0xe2278(%rip), %rsi     # 0x117f58
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x35d09
movq %rax, %rdi
callq 0xe9c2f
movq %rax, %r15
testb %bpl, %bpl
je 0x35e24
xorl %r14d, %r14d
xorl %r13d, %r13d
jmp 0x35d0c
xorl %r15d, %r15d
testq %r12, %r12
sete %al
movl 0xc(%rsp), %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0x35d41
movq 0x38(%r12), %rax
testq %rax, %rax
je 0x35d41
cmpb $0x0, 0x3(%rax)
je 0x35d41
movq 0x10(%rax), %r15
movq %r15, 0x18(%rsp)
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x35e29
cmpl $0x1, 0x24(%rbx)
je 0x35e8b
testb %r13b, %r13b
jne 0x35e8b
movq %r15, %r12
testb %r14b, %r14b
je 0x35da3
movq 0x18(%rbx), %rdi
xorl %r12d, %r12d
leaq 0x20(%rsp), %rbp
movl $0x2, %ecx
movq %rbp, %rsi
xorl %edx, %edx
callq 0xee8d4
movb (%rbp), %al
movb 0x1(%rbp), %cl
movl %eax, %edx
xorb $0x4d, %dl
movl %ecx, %esi
xorb $0x5a, %sil
xorb $0x5a, %al
xorb $0x4d, %cl
orb %al, %cl
setne %al
orb %dl, %sil
je 0x35e2e
testb %al, %al
je 0x35e2e
leaq 0x2(%r12), %rax
leaq 0x20(%rsp), %rsi
movq %rax, (%rsi)
movq 0x18(%rbx), %rdi
callq 0xeebeb
leaq -0xa29(%rax), %rcx
cmpq $-0xa0c, %rcx           # imm = 0xF5F4
jbe 0x35e5c
leaq 0x20(%rsp), %rbp
movq (%rbp), %rcx
addq %rcx, %rax
addq $0x4, %rax
movq %rax, (%rbp)
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeebeb
movq (%rbp), %rsi
testq %rax, %rax
je 0x35e02
addq %rax, %rsi
addq $0x4, %rsi
movq %rsi, 0x20(%rsp)
movq 0x10(%rsp), %r14
movq 0x18(%rbx), %rdi
callq 0xeebc8
cmpl $0xea60, %eax           # imm = 0xEA60
jne 0x35e5c
movq %r12, 0x18(%rsp)
movb $0x1, %bpl
movq %r12, %r15
jmp 0x35e9f
movq %r15, 0x18(%rsp)
movb $0x1, %bpl
jmp 0x35e9a
movl $0xf0, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %rbp
movq 0x18(%rbx), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x104f96
movq 0x48(%rbp), %r12
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xe8be2
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r12, %rdx
leaq 0x18(%rsp), %r8
movq %r12, %rsi
xorl %ecx, %ecx
callq 0xf9ad4
testl %eax, %eax
je 0x35e8b
movq 0x18(%rsp), %rax
cmpq %r15, %rax
sete %bpl
movq %rax, %r15
jmp 0x35e9a
movq $0x0, 0x18(%rsp)
movb $0x1, %bpl
xorl %r15d, %r15d
movq 0x10(%rsp), %r14
movq %r15, 0x10(%r14)
movq 0x18(%rbx), %rdi
leaq 0xe2116(%rip), %rdx     # 0x117fc4
movl $0x2, %ecx
movq %r15, %rsi
callq 0xefc85
testl %eax, %eax
je 0x35edc
cmpb $0x0, 0xc(%rsp)
jne 0x35f28
leaq 0xe209a(%rip), %rsi     # 0x117f67
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x35f2b
testb %bpl, %bpl
jne 0x35ef6
movq 0x10(%r14), %rdx
leaq 0xe20a7(%rip), %rsi     # 0x117f93
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpl $0x3, 0x24(%rbx)
je 0x35f16
leaq 0xe20a6(%rip), %rsi     # 0x117fa9
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x35f16
movq %rax, 0x8(%r14)
jmp 0x35f1d
cmpb $0x0, 0xc(%rsp)
je 0x35f5e
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3616d
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe8be2
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe8be2
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x30, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r15
leaq 0xe1fd6(%rip), %rsi     # 0x117f4b
movq %rbx, %rdi
callq 0xeb125
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xeb220
movl %eax, 0x10(%r15)
cmpb $0x0, 0x7b(%rbx)
je 0x35f9e
cmpl $0x3, 0x24(%rbx)
je 0x35f9e
movb $0x1, 0x14(%r15)
movq %rbx, %rdi
movl $0x10, %esi
callq 0xeb499
movq %rax, 0x20(%r15)
movl $0x38, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r12
movl $0x0, 0x8(%rax)
movq 0x18(%rbx), %rax
movq %rax, (%r12)
movq 0x10(%r14), %rax
movq %rax, 0x10(%r12)
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x36422
movq 0x30(%rbx), %rax
testq %rax, %rax
je 0x360a4
cmpl $0x0, (%rax)
jle 0x360a4
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x8(%rax), %rax
movq (%rax,%r14), %rcx
movq %rbx, %rdi
leaq 0xe1f9e(%rip), %rsi     # 0x117fb3
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
testq %r13, %r13
je 0x3602e
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe8be2
movl $0x38, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r13
incq %rbp
movl %ebp, 0x8(%rax)
movq $0x0, 0x10(%rax)
movq %rbx, %rdi
movl %ebp, %esi
callq 0xebaf7
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x36095
movq %rax, (%r13)
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x36422
movq %rbx, %rdi
movl %ebp, %esi
leaq 0x20(%rsp), %rdx
callq 0xebb50
movq 0x30(%rbx), %rax
movslq (%rax), %rcx
addq $0x10, %r14
cmpq %rcx, %rbp
jl 0x36003
testq %r15, %r15
movq 0x10(%rsp), %r14
jne 0x360a7
jmp 0x35f2e
xorl %r13d, %r13d
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x3658f
movq 0x20(%r15), %rdi
callq 0xeb858
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8be2
jmp 0x35f2e