// File: /workspace/github_c_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdx, %rbx
movq %rsi, %r14
callq 0x1057dc
movq 0x68(%r14), %rax
movabsq $0x3230b4d5fca84643, %rcx # imm = 0x3230B4D5FCA84643
cmpq %rcx, %rax
jg 0x10628b
movabsq $0x57db6d8be3c4894, %rcx # imm = 0x57DB6D8BE3C4894
cmpq %rcx, %rax
jg 0x1062b9
movabsq $-0x7abde7d5679ec020, %rcx # imm = 0x8542182A98613FE0
cmpq %rcx, %rax
je 0x1062fe
movabsq $0x3d0e01c3764195, %rcx # imm = 0x3D0E01C3764195
jmp 0x1062d2
movabsq $0x684ca6156c27b16a, %rcx # imm = 0x684CA6156C27B16A
cmpq %rcx, %rax
jg 0x1062dc
movabsq $0x3230b4d5fca84644, %rcx # imm = 0x3230B4D5FCA84644
cmpq %rcx, %rax
je 0x106335
movabsq $0x66f2137a99395f3e, %rcx # imm = 0x66F2137A99395F3E
jmp 0x1062d2
movabsq $0x57db6d8be3c4895, %rcx # imm = 0x57DB6D8BE3C4895
cmpq %rcx, %rax
je 0x106335
movabsq $0x1bbcf0ae0422828e, %rcx # imm = 0x1BBCF0AE0422828E
cmpq %rcx, %rax
je 0x106335
jmp 0x10638e
movabsq $0x684ca6156c27b16b, %rcx # imm = 0x684CA6156C27B16B
cmpq %rcx, %rax
je 0x106335
movabsq $0x6bd653c8edd98eed, %rcx # imm = 0x6BD653C8EDD98EED
cmpq %rcx, %rax
jne 0x10638e
leaq 0x48(%r14), %r13
movq $-0x1e0, %r15           # imm = 0xFE20
addq 0x48(%r14), %r15
movq (%r14), %rdi
leaq 0xc(%rsp), %r12
movl $0x2, %ecx
movq %r12, %rsi
movq %r15, %rdx
callq 0xee8d4
cmpb $0x1a, (%r12)
jne 0x106339
cmpb $0x1e, 0xd(%rsp)
jbe 0x10633d
jmp 0x106339
leaq 0x48(%r14), %r13
movq (%r13), %r15
movq (%r14), %rdi
leaq 0xe(%rsp), %r12
movl $0x2, %ecx
movq %r12, %rsi
movq %r15, %rdx
callq 0xee8d4
cmpb $0x1a, (%r12)
jne 0x10638e
cmpb $0x1e, 0xf(%rsp)
ja 0x10638e
movq %r15, 0x10(%rbx)
movq (%r14), %rax
movq 0x18(%rax), %rax
subq %r15, %rax
movq %rax, 0x18(%rbx)
cmpq $0x2, %rax
jl 0x10638e
movb $0x64, 0x1(%rbx)
movb $0x1, 0x3(%rbx)
leaq 0x191d8(%rip), %rax     # 0x11f562
movq %rax, 0x40(%rbx)
cmpb $0x64, 0x1(%rbx)
jne 0x1063ac
addq $0x50, %rbx
leaq 0x1e3c7(%rip), %rsi     # 0x124766
movl $0x28, %edx
movq %rbx, %rdi
callq 0xe8219
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq