// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/fmtutil-exe.c
// Function: detect_exesfx_arc
static void detect_exesfx_arc(deark *c,
	struct fmtutil_exe_info *ei, struct fmtutil_specialexe_detection_data *edd)
{
	int found = 0;
	i64 foundpos = 0;

	calc_entrypoint_crc(c, ei);
	if(ei->entrypoint_crcs==0x057db6d8be3c4895ULL) { // MKSARC v1.00 from ARC v6.01
		found = 1;
		foundpos = ei->end_of_dos_code;
	}
	else if(ei->entrypoint_crcs==0x8542182a98613fe0ULL ||
		ei->entrypoint_crcs==0x6bd653c8edd98eedULL)
	{
		// MKSARC v1.01 from ARC v6.02
		// (Found two different versions with the same version number.)
		// This version of MKSARC has a bug. Compared to the v1.00, the start of DOS
		// code was reduced by 480, from 512 to 32. But the *end* of DOS code was not
		// adjusted accordingly, leaving it 480 higher than it should be. This is
		// important, because the end of DOS code is where we expect the ARC data to
		// start.
		found = 1;
		foundpos = ei->end_of_dos_code - 480;
		if(!is_arc_data_at(ei, foundpos)) {
			foundpos = ei->end_of_dos_code; // In case there's a version without the bug
		}
	}
	else if(ei->entrypoint_crcs==0x66f2137a99395f3eLL || // MKSARC v7.10, from ARC v7.10
		ei->entrypoint_crcs==0x3230b4d5fca84644ULL || // MKSARC v7.12, from ARC v7.12
		ei->entrypoint_crcs==0x003d0e01c3764195ULL || // PKARC 3.5
		ei->entrypoint_crcs==0x1bbcf0ae0422828eULL || // PKARC 3.6
		ei->entrypoint_crcs==0x684ca6156c27b16bULL) // PKPAK 3.61
	{
		found = 1;
		foundpos = ei->end_of_dos_code;
	}
	// TODO: Detect MKSARC v7.12 with the /P option (OS/2 protected mode).
	// Extraction would work, if we could detect it.

	if(!found) goto done;
	if(!is_arc_data_at(ei, foundpos)) {
		goto done;
	}

	edd->payload_pos = foundpos;
	edd->payload_len = ei->f->len - edd->payload_pos;
	if(edd->payload_len<2) goto done;
	// TODO: It would be nice to strip any padding from the end of the extracted ARC
	// file, but that could be more trouble than it's worth.

	edd->detected_fmt = DE_SPECIALEXEFMT_SFX;
	edd->payload_valid = 1;
	edd->payload_file_ext = "arc";

done:
	if(edd->detected_fmt==DE_SPECIALEXEFMT_SFX) {
		de_strlcpy(edd->detected_fmt_name, "ARC", sizeof(edd->detected_fmt_name));
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rdx, %rbx
movq %rsi, %r14
callq 0xf9044
movq 0x68(%r14), %rax
movabsq $-0x7abde7d5679ec020, %rcx # imm = 0x8542182A98613FE0
cmpq %rcx, %rax
je 0xf9aa3
movabsq $0x3d0e01c3764195, %rcx # imm = 0x3D0E01C3764195
cmpq %rcx, %rax
je 0xf9abd
movabsq $0x6bd653c8edd98eed, %rcx # imm = 0x6BD653C8EDD98EED
cmpq %rcx, %rax
je 0xf9aa3
movabsq $0x1bbcf0ae0422828e, %rcx # imm = 0x1BBCF0AE0422828E
cmpq %rcx, %rax
je 0xf9abd
movabsq $0x3230b4d5fca84644, %rcx # imm = 0x3230B4D5FCA84644
cmpq %rcx, %rax
je 0xf9abd
movabsq $0x66f2137a99395f3e, %rcx # imm = 0x66F2137A99395F3E
cmpq %rcx, %rax
je 0xf9abd
movabsq $0x684ca6156c27b16b, %rcx # imm = 0x684CA6156C27B16B
cmpq %rcx, %rax
je 0xf9abd
movabsq $0x57db6d8be3c4895, %rcx # imm = 0x57DB6D8BE3C4895
cmpq %rcx, %rax
je 0xf9abd
jmp 0xf9afe
movq $-0x1e0, %r15           # imm = 0xFE20
addq 0x48(%r14), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xf9ead
testl %eax, %eax
jne 0xf9ac4
leaq 0x48(%r14), %rax
movq (%rax), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xf9ead
testl %eax, %eax
je 0xf9afe
movq %r15, 0x10(%rbx)
movq (%r14), %rax
movq 0x18(%rax), %rax
subq %r15, %rax
movq %rax, 0x18(%rbx)
cmpq $0x2, %rax
jl 0xf9afe
movb $0x64, 0x1(%rbx)
movb $0x1, 0x3(%rbx)
leaq 0x18998(%rip), %rax     # 0x112492
movq %rax, 0x40(%rbx)
cmpb $0x64, 0x1(%rbx)
jne 0xf9b1f
addq $0x50, %rbx
leaq 0x1da57(%rip), %rsi     # 0x117566
pushq $0x28
popq %rdx
movq %rbx, %rdi
popq %rbx
popq %r14
popq %r15
jmp 0xde492
popq %rbx
popq %r14
popq %r15
retq