// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x18(%rsp)
pushq $0x18
popq %rsi
callq 0xdecaf
movq %r12, (%rax)
cmpl $0x3, 0x24(%rbx)
movq %rax, 0x10(%rsp)
jne 0x347c0
pushq $0x52
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdfcf4
testl %eax, %eax
setne %al
movl %eax, 0xc(%rsp)
cmpl $0x3, 0x24(%rbx)
jne 0x347c8
movb $0x1, %r14b
xorl %r15d, %r15d
jmp 0x34819
movl $0x0, 0xc(%rsp)
leaq 0xd6690(%rip), %rsi     # 0x10ae5f
movq %rbx, %rdi
movl $0xff, %edx
callq 0xdfae5
movl %eax, %ebp
testb %bpl, %bpl
sete %r13b
movb $0x1, %r14b
cmpl $0x3, 0x24(%rbx)
je 0x3481e
leaq 0xd6673(%rip), %rsi     # 0x10ae68
movq %rbx, %rdi
callq 0xdfa87
testq %rax, %rax
je 0x3481e
movq %rax, %rdi
callq 0xdfb3b
movq %rax, %r15
testb %bpl, %bpl
je 0x34937
xorl %r14d, %r14d
xorl %r13d, %r13d
jmp 0x34821
xorl %r15d, %r15d
testq %r12, %r12
sete %al
movl 0xc(%rsp), %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0x34856
movq 0x38(%r12), %rax
testq %rax, %rax
je 0x34856
cmpb $0x0, 0x3(%rax)
je 0x34856
movq 0x10(%rax), %r15
movq %r15, 0x18(%rsp)
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x3493c
cmpl $0x1, 0x24(%rbx)
je 0x3499e
testb %r13b, %r13b
jne 0x3499e
movq %r15, %r12
testb %r14b, %r14b
je 0x348b6
movq 0x18(%rbx), %rdi
xorl %r12d, %r12d
leaq 0x20(%rsp), %rbp
pushq $0x2
popq %rcx
movq %rbp, %rsi
xorl %edx, %edx
callq 0xe3cb7
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 0x34941
testb %al, %al
je 0x34941
leaq 0x2(%r12), %rax
leaq 0x20(%rsp), %rsi
movq %rax, (%rsi)
movq 0x18(%rbx), %rdi
callq 0xe3fc4
leaq -0xa29(%rax), %rcx
cmpq $-0xa0c, %rcx           # imm = 0xF5F4
jbe 0x3496f
leaq 0x20(%rsp), %rbp
movq (%rbp), %rcx
addq %rcx, %rax
addq $0x4, %rax
movq %rax, (%rbp)
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xe3fc4
movq (%rbp), %rsi
testq %rax, %rax
je 0x34915
addq %rax, %rsi
addq $0x4, %rsi
movq %rsi, 0x20(%rsp)
movq 0x10(%rsp), %r14
movq 0x18(%rbx), %rdi
callq 0xe3fa3
cmpl $0xea60, %eax           # imm = 0xEA60
jne 0x3496f
movq %r12, 0x18(%rsp)
movb $0x1, %bpl
movq %r12, %r15
jmp 0x349af
movq %r15, 0x18(%rsp)
movb $0x1, %bpl
jmp 0x349aa
movl $0xf0, %esi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %rbp
movq 0x18(%rbx), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xf8849
movq 0x48(%rbp), %r12
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xded32
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r12, %rdx
leaq 0x18(%rsp), %r8
movq %r12, %rsi
xorl %ecx, %ecx
callq 0xee14d
testl %eax, %eax
je 0x3499e
movq 0x18(%rsp), %rax
cmpq %r15, %rax
sete %bpl
movq %rax, %r15
jmp 0x349aa
andq $0x0, 0x18(%rsp)
movb $0x1, %bpl
xorl %r15d, %r15d
movq 0x10(%rsp), %r14
movq %r15, 0x10(%r14)
movq 0x18(%rbx), %rdi
leaq 0xd6516(%rip), %rdx     # 0x10aed4
pushq $0x2
popq %rcx
movq %r15, %rsi
callq 0xe4d48
testl %eax, %eax
je 0x349ea
cmpb $0x0, 0xc(%rsp)
jne 0x34a36
leaq 0xd649c(%rip), %rsi     # 0x10ae77
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x34a39
testb %bpl, %bpl
jne 0x34a04
movq 0x10(%r14), %rdx
leaq 0xd64a9(%rip), %rsi     # 0x10aea3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
cmpl $0x3, 0x24(%rbx)
je 0x34a24
leaq 0xd64a8(%rip), %rsi     # 0x10aeb9
movq %rbx, %rdi
callq 0xdfa87
testq %rax, %rax
je 0x34a24
movq %rax, 0x8(%r14)
jmp 0x34a2b
cmpb $0x0, 0xc(%rsp)
je 0x34a6c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x34c5f
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r12, %rsi
callq 0xded32
movq %rbx, %rdi
movq %r13, %rsi
callq 0xded32
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x30
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r15
leaq 0xd63da(%rip), %rsi     # 0x10ae5b
movq %rbx, %rdi
callq 0xe07f7
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe08d2
movl %eax, 0x10(%r15)
cmpb $0x0, 0x7b(%rbx)
je 0x34aaa
cmpl $0x3, 0x24(%rbx)
je 0x34aaa
movb $0x1, 0x14(%r15)
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0xe0ae9
movq %rax, 0x20(%r15)
pushq $0x38
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r12
andl $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 0x34f08
movq 0x30(%rbx), %rax
testq %rax, %rax
je 0x34ba0
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r13d, %r13d
movslq (%rax), %rcx
cmpq %rcx, %rbp
jge 0x34b91
movq 0x8(%rax), %rax
movq (%rax,%r14), %rcx
movq %rbx, %rdi
leaq 0xd63a6(%rip), %rsi     # 0x10aec3
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
testq %r13, %r13
je 0x34b36
movq %rbx, %rdi
movq %r13, %rsi
callq 0xded32
movq %rbx, %rdi
pushq $0x38
popq %rsi
callq 0xdecaf
movq %rax, %r13
incq %rbp
movl %ebp, 0x8(%rax)
andq $0x0, 0x10(%rax)
movq %rbx, %rdi
movl %ebp, %esi
callq 0xe10d7
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x34b91
movq %rax, (%r13)
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x34f08
movq %rbx, %rdi
movl %ebp, %esi
leaq 0x20(%rsp), %rdx
callq 0xe1130
movq 0x30(%rbx), %rax
addq $0x10, %r14
jmp 0x34aff
testq %r15, %r15
movq 0x10(%rsp), %r14
jne 0x34ba3
jmp 0x34a3c
xorl %r13d, %r13d
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3504a
movq 0x20(%r15), %rdi
callq 0xe0e8d
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded32
jmp 0x34a3c