// File: /workspace/github_c_makefile_star2_O1/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 0x105c44
movq 0x68(%r14), %rcx
movb $0x1, %al
xorl %r15d, %r15d
movabsq $0x3230b4d5fca84643, %rdx # imm = 0x3230B4D5FCA84643
cmpq %rdx, %rcx
jg 0x106709
movabsq $0x57db6d8be3c4894, %rdx # imm = 0x57DB6D8BE3C4894
cmpq %rdx, %rcx
jg 0x106737
movabsq $-0x7abde7d5679ec020, %rdx # imm = 0x8542182A98613FE0
cmpq %rdx, %rcx
je 0x106775
movabsq $0x3d0e01c3764195, %rdx # imm = 0x3D0E01C3764195
jmp 0x106750
movabsq $0x684ca6156c27b16a, %rdx # imm = 0x684CA6156C27B16A
cmpq %rdx, %rcx
jg 0x106757
movabsq $0x3230b4d5fca84644, %rdx # imm = 0x3230B4D5FCA84644
cmpq %rdx, %rcx
je 0x1067ac
movabsq $0x66f2137a99395f3e, %rdx # imm = 0x66F2137A99395F3E
jmp 0x106750
movabsq $0x57db6d8be3c4895, %rdx # imm = 0x57DB6D8BE3C4895
cmpq %rdx, %rcx
je 0x1067ac
movabsq $0x1bbcf0ae0422828e, %rdx # imm = 0x1BBCF0AE0422828E
cmpq %rdx, %rcx
je 0x1067ac
jmp 0x1067b6
movabsq $0x684ca6156c27b16b, %rdx # imm = 0x684CA6156C27B16B
cmpq %rdx, %rcx
je 0x1067ac
movabsq $0x6bd653c8edd98eed, %rdx # imm = 0x6BD653C8EDD98EED
cmpq %rdx, %rcx
jne 0x1067b6
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 0xeeee8
cmpb $0x1a, (%r12)
jne 0x1067b0
cmpb $0x1e, 0xd(%rsp)
jbe 0x1067b4
jmp 0x1067b0
leaq 0x48(%r14), %r13
movq (%r13), %r15
xorl %eax, %eax
testb %al, %al
je 0x1067e6
cmpb $0x64, 0x1(%rbx)
jne 0x1067d8
addq $0x50, %rbx
leaq 0x1dd7b(%rip), %rsi     # 0x124546
movl $0x28, %edx
movq %rbx, %rdi
callq 0xe8ded
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq (%r14), %rdi
leaq 0xe(%rsp), %r12
movl $0x2, %ecx
movq %r12, %rsi
movq %r15, %rdx
callq 0xeeee8
cmpb $0x1a, (%r12)
jne 0x1067ba
cmpb $0x1e, 0xf(%rsp)
ja 0x1067ba
movq %r15, 0x10(%rbx)
movq (%r14), %rax
movq 0x18(%rax), %rax
subq %r15, %rax
movq %rax, 0x18(%rbx)
cmpq $0x2, %rax
jl 0x1067ba
movb $0x64, 0x1(%rbx)
movb $0x1, 0x3(%rbx)
leaq 0x18c27(%rip), %rax     # 0x11f45a
movq %rax, 0x40(%rbx)
jmp 0x1067ba