// File: /workspace/github_c_makefile_star2_O1/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, 0x10(%rsp)
movl $0x18, %esi
callq 0xe969b
movq %rax, 0x8(%rsp)
movq %r12, (%rax)
cmpl $0x3, 0x24(%rbx)
jne 0x35e08
movq %rbx, %rdi
movq %r12, %rsi
movl $0x52, %edx
callq 0xea97e
testl %eax, %eax
setne %bpl
jmp 0x35e0a
xorl %ebp, %ebp
cmpl $0x3, 0x24(%rbx)
jne 0x35e15
xorl %r14d, %r14d
jmp 0x35e2f
leaq 0xe1ffb(%rip), %rsi     # 0x117e17
movq %rbx, %rdi
movl $0xff, %edx
callq 0xea727
testb %al, %al
sete %r14b
movb $0x1, %r13b
cmpl $0x3, 0x24(%rbx)
je 0x35e5f
leaq 0xe1fe1(%rip), %rsi     # 0x117e20
movq %rbx, %rdi
callq 0xea6cb
testq %rax, %rax
je 0x35e5f
movq %rax, %rdi
callq 0xea786
movq %rax, %r15
xorl %r13d, %r13d
movl %r14d, %eax
jmp 0x35e64
xorl %r15d, %r15d
xorl %eax, %eax
testb %al, %al
je 0x35e6f
movq %r15, 0x10(%rsp)
jmp 0x35eac
testq %r12, %r12
sete %al
movl %ebp, %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0x35e99
movq 0x38(%r12), %rax
testq %rax, %rax
je 0x35e99
cmpb $0x0, 0x3(%rax)
je 0x35e99
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0x35eac
cmpl $0x1, 0x24(%rbx)
jne 0x35fa4
movq $0x0, 0x10(%rsp)
xorl %r14d, %r14d
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %rsi
movq %rsi, 0x10(%r12)
movq 0x18(%rbx), %rdi
leaq 0xe1fc3(%rip), %rdx     # 0x117e8c
movl $0x2, %ecx
callq 0xf025e
testl %eax, %eax
je 0x35ef2
testb %bpl, %bpl
jne 0x35f42
leaq 0xe1f4c(%rip), %rsi     # 0x117e2f
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x35f45
testb %r14b, %r14b
je 0x35f0d
movq 0x10(%r12), %rdx
leaq 0xe1f58(%rip), %rsi     # 0x117e5b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
cmpl $0x3, 0x24(%rbx)
je 0x35f2e
leaq 0xe1f57(%rip), %rsi     # 0x117e71
movq %rbx, %rdi
callq 0xea6cb
testq %rax, %rax
je 0x35f2e
movq %rax, 0x8(%r12)
jmp 0x35f37
testb %bpl, %bpl
je 0x35fba
movq %rbx, %rdi
movq %r12, %rsi
callq 0x362f9
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %r15d, %r15d
testq %r15, %r15
je 0x35f74
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x3671d
movq 0x20(%r15), %rdi
callq 0xebe1d
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe9752
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe9752
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe9752
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb %r14b, %r14b
movq 0x8(%rsp), %r12
je 0x3610e
xorl %r14d, %r14d
jmp 0x3624e
movl $0x30, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r15
leaq 0xe1e42(%rip), %rsi     # 0x117e13
movq %rbx, %rdi
callq 0xeb6d6
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xeb7d1
movl %eax, 0x10(%r15)
cmpb $0x0, 0x7b(%rbx)
je 0x35ffa
cmpl $0x3, 0x24(%rbx)
je 0x35ffa
movb $0x1, 0x14(%r15)
movq %rbx, %rdi
movl $0x10, %esi
callq 0xeba56
movq %rax, 0x20(%r15)
movl $0x38, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r14
movl $0x0, 0x8(%rax)
movq 0x18(%rbx), %rax
movq %rax, (%r14)
movq 0x10(%r12), %rax
movq %rax, 0x10(%r14)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x365ad
cmpq $0x0, 0x30(%rbx)
je 0x3618a
movq 0x30(%rbx), %rax
cmpl $0x0, (%rax)
jle 0x3618a
movq %r14, 0x20(%rsp)
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x8(%rax), %rax
movq (%rax,%r12), %rcx
movq %rbx, %rdi
leaq 0xe1e04(%rip), %rsi     # 0x117e7b
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
testq %r13, %r13
je 0x36090
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe9752
movl $0x38, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r13
leaq 0x1(%rbp), %r14
movl %r14d, 0x8(%rax)
movq $0x0, 0x10(%rax)
movq %rbx, %rdi
movl %r14d, %esi
callq 0xec0f5
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x360ff
movq %rax, (%r13)
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x365ad
incl %ebp
movq %rbx, %rdi
movl %ebp, %esi
leaq 0x18(%rsp), %rdx
callq 0xec15f
movq 0x30(%rbx), %rax
movslq (%rax), %rcx
addq $0x10, %r12
movq %r14, %rbp
cmpq %rcx, %r14
jl 0x36065
movq 0x8(%rsp), %r12
movq 0x20(%rsp), %r14
jmp 0x35f4b
testb %r13b, %r13b
je 0x36192
movq 0x18(%rbx), %rdi
xorl %r12d, %r12d
leaq 0x18(%rsp), %r14
movl $0x2, %ecx
movq %r14, %rsi
xorl %edx, %edx
callq 0xeeee8
movb (%r14), %al
movb 0x1(%r14), %cl
movl %eax, %edx
xorb $0x4d, %dl
movl %ecx, %esi
xorb $0x5a, %sil
xorb $0x5a, %al
xorb $0x4d, %cl
orb %al, %cl
setne %cl
orb %dl, %sil
je 0x36158
movl $0x0, %eax
testb %cl, %cl
jne 0x36197
movl $0xf0, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r14
movq 0x18(%rbx), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x1053ee
movq 0x48(%r14), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
movb $0x1, %al
jmp 0x36197
xorl %r13d, %r13d
jmp 0x35f4b
xorl %eax, %eax
movq %r15, %r12
testb %al, %al
jne 0x36211
leaq 0x2(%r12), %rax
leaq 0x18(%rsp), %rsi
movq %rax, (%rsi)
movq 0x18(%rbx), %rdi
callq 0xef1ed
leaq -0xa29(%rax), %rcx
cmpq $-0xa0b, %rcx           # imm = 0xF5F5
jb 0x36211
leaq 0x18(%rsp), %rsi
movq (%rsi), %rcx
addq %rcx, %rax
addq $0x4, %rax
movq %rax, (%rsi)
movq 0x18(%rbx), %rdi
callq 0xef1ed
testq %rax, %rax
je 0x361f2
movq 0x18(%rsp), %rcx
addq %rcx, %rax
addq $0x4, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rbx), %rdi
movq 0x18(%rsp), %rsi
callq 0xef1ca
cmpl $0xea60, %eax           # imm = 0xEA60
jne 0x36211
movq %r12, 0x10(%rsp)
xorl %r14d, %r14d
jmp 0x3623b
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r12, %rdx
xorl %r14d, %r14d
leaq 0x10(%rsp), %r8
movq %r12, %rsi
xorl %ecx, %ecx
callq 0xf9fc8
testl %eax, %eax
je 0x3623f
cmpq %r15, 0x10(%rsp)
setne %r14b
xorl %eax, %eax
jmp 0x36241
movb $0x1, %al
movq 0x8(%rsp), %r12
testb %al, %al
je 0x35eb4
movq $0x0, 0x10(%rsp)
jmp 0x35eb4