// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x189540
movq -0x10(%rbp), %rax
movabsq $0x57db6d8be3c4895, %rcx # imm = 0x57DB6D8BE3C4895
cmpq %rcx, 0x68(%rax)
jne 0x18aabc
movl $0x1, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x18ab98
movq -0x10(%rbp), %rax
movabsq $-0x7abde7d5679ec020, %rcx # imm = 0x8542182A98613FE0
cmpq %rcx, 0x68(%rax)
je 0x18aae4
movq -0x10(%rbp), %rax
movabsq $0x6bd653c8edd98eed, %rcx # imm = 0x6BD653C8EDD98EED
cmpq %rcx, 0x68(%rax)
jne 0x18ab1d
movl $0x1, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
subq $0x1e0, %rax            # imm = 0x1E0
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x18b270
cmpl $0x0, %eax
jne 0x18ab1b
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x18ab96
movq -0x10(%rbp), %rax
movabsq $0x66f2137a99395f3e, %rcx # imm = 0x66F2137A99395F3E
cmpq %rcx, 0x68(%rax)
je 0x18ab81
movq -0x10(%rbp), %rax
movabsq $0x3230b4d5fca84644, %rcx # imm = 0x3230B4D5FCA84644
cmpq %rcx, 0x68(%rax)
je 0x18ab81
movq -0x10(%rbp), %rax
movabsq $0x3d0e01c3764195, %rcx # imm = 0x3D0E01C3764195
cmpq %rcx, 0x68(%rax)
je 0x18ab81
movq -0x10(%rbp), %rax
movabsq $0x1bbcf0ae0422828e, %rcx # imm = 0x1BBCF0AE0422828E
cmpq %rcx, 0x68(%rax)
je 0x18ab81
movq -0x10(%rbp), %rax
movabsq $0x684ca6156c27b16b, %rcx # imm = 0x684CA6156C27B16B
cmpq %rcx, 0x68(%rax)
jne 0x18ab94
movl $0x1, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x18ab96
jmp 0x18ab98
cmpl $0x0, -0x1c(%rbp)
jne 0x18aba0
jmp 0x18ac07
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x18b270
cmpl $0x0, %eax
jne 0x18abb4
jmp 0x18ac07
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rcx
movq -0x18(%rbp), %rax
subq 0x10(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
cmpq $0x2, 0x18(%rax)
jge 0x18abe8
jmp 0x18ac07
movq -0x18(%rbp), %rax
movb $0x64, 0x1(%rax)
movq -0x18(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x18(%rbp), %rax
leaq 0x20508(%rip), %rcx     # 0x1ab10b
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x64, %eax
jne 0x18ac2d
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
leaq 0x25417(%rip), %rsi     # 0x1b003a
movl $0x28, %edx
callq 0x15aa30
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)