// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/arc.c
// Function: de_identify_arc
static int de_identify_arc(deark *c)
{
	static const char *exts[] = {"arc", "ark", "pak", "spk", "sdn", "com"};
	u8 has_ext = 0;
	UI ext_idx = 0;
	UI strictness;
	int ends_with_trailer = 0;
	int ends_with_comments = 0;
	int starts_with_trailer = 0;
	i64 arc_start = 0;
	size_t k;
	u8 cmpr_meth;
	u8 buf[5];

	de_read(buf, 0, sizeof(buf));

	// Look for 0x1a in the first 4 bytes. Some .COM-style self-extracting
	// archives start with 1-3 bytes of code before the ARC marker.
	if(!find_arc_marker(c, buf, sizeof(buf)-1, &arc_start)) {
		return 0;
	}

	cmpr_meth = buf[arc_start+1];

	if(cmpr_meth==0) {
		// Don't tolerate empty archives that don't start at the beginning of file.
		if(arc_start!=0) return 0;

		starts_with_trailer = 1;
	}

	// Get info about file extension
	for(k=0; k<DE_ARRAYCOUNT(exts); k++) {
		if(de_input_file_has_ext(c, exts[k])) {
			has_ext = 1;
			ext_idx = (UI)k;
			break;
		}
	}

	// Only tolerate leading junk for a few file extensions
	if(arc_start>0) {
		if(ext_idx==0 || ext_idx==1 || ext_idx==5) { // .arc, .ark, .com
			;
		}
		else {
			return 0;
		}
	}

	// Look at some of the file, to see if it seems ok.
	if(has_ext && ext_idx<=1 && arc_start==0) {
		strictness = 0;
	}
	else if(has_ext && ext_idx<=3 && arc_start==0) {
		strictness = 1;
	}
	else {
		strictness = 2;
	}
	if(!is_valid_file_at(c->infile, arc_start, c->infile->len, strictness)) {
		return 0;
	}

	// Handle 2-byte files
	if(starts_with_trailer && c->infile->len==2) {
		if(!has_ext) return 0;
		if(ext_idx==0) return 100;
		if(ext_idx<=3) return 15;
		return 0;
	}

	if(de_getu16be(c->infile->len-2) == 0x1a00) {
		// Standard ARC trailer
		ends_with_trailer = 1;
	}

	if(de_getu32be(c->infile->len-8) == 0x504baa55) {
		// PKARC trailer, for files with comments
		ends_with_comments = 1;
	}

	if(!ends_with_trailer && !ends_with_comments) {
		// PAK-style extensions
		if(de_getu16be(c->infile->len-2) == 0xfe00) {
			ends_with_comments = 1;
		}
	}

	if(starts_with_trailer) {
		if(ends_with_comments) return 25;
		else return 0;
	}
	if(has_ext && (ends_with_trailer || ends_with_comments)) return 90;
	if(ends_with_trailer || ends_with_comments) return 35;
	if(has_ext) return 24;
	if(arc_start==0) return 19;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
leaq 0x30(%rsp), %r15
andq $0x0, (%r15)
movq 0x18(%rdi), %rdi
xorl %r14d, %r14d
leaq 0x1b(%rsp), %r12
pushq $0x5
popq %rcx
movq %r12, %rsi
xorl %edx, %edx
callq 0xe3cb7
pushq $0x4
popq %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x2695c
testl %eax, %eax
je 0x25a59
movq 0x30(%rsp), %r12
movb 0x1c(%rsp,%r12), %al
movb %al, 0x6(%rsp)
testb %al, %al
sete %al
testq %r12, %r12
setne %cl
testb %al, %cl
jne 0x25a59
leaq 0x1(%r12), %rbp
leaq 0xe22c5(%rip), %r13     # 0x107c80
movq %r13, %r15
xorl %r14d, %r14d
cmpq $0x6, %r14
je 0x259e2
movslq (%r15), %rsi
addq %r13, %rsi
movq %rbx, %rdi
callq 0xdfcd5
testl %eax, %eax
jne 0x259e8
incq %r14
addq $0x4, %r15
jmp 0x259c1
xorl %eax, %eax
xorl %edx, %edx
jmp 0x259f2
cmpq $0x6, %r14
setb %al
movl %r14d, %edx
pushq $0x23
popq %rcx
testq %r12, %r12
jle 0x25a07
xorl %r14d, %r14d
cmpl $0x5, %edx
ja 0x25a59
btl %edx, %ecx
jae 0x25a59
movq %rax, 0x10(%rsp)
testq %r12, %r12
sete %r13b
cmpl $0x2, %edx
setb 0x7(%rsp)
movq %rdx, 0x28(%rsp)
cmpl $0x4, %edx
setb %r15b
movq 0x18(%rbx), %r14
movq 0x18(%r14), %rax
movq %rax, 0x20(%rsp)
leaq 0x8(%rsp), %rsi
movq %rbp, (%rsi)
movq %r14, %rdi
callq 0xe3f2a
movl %eax, %ebp
cmpb $0xc, %al
setae %al
cmpb $0x14, %bpl
setne %cl
testb %cl, %al
je 0x25a6b
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb %bpl, %bpl
je 0x25ada
andb 0x10(%rsp), %r13b
andb %r13b, %r15b
cmpb $0x14, %bpl
setae %al
orb %r15b, %al
jne 0x25a95
movq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0xe3eb4
cmpb $0x20, %al
jb 0x25a56
andb 0x7(%rsp), %r13b
addq $0xd, 0x8(%rsp)
testb %r13b, %r13b
jne 0x25ada
leaq 0x8(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xe40bf
movq (%r15), %rcx
xorl %edx, %edx
cmpb $0x1, %bpl
setne %dl
leaq (%rax,%rdx,4), %rax
addq %rcx, %rax
addq $0x6, %rax
movq %rax, (%r15)
cmpq 0x20(%rsp), %rax
jne 0x25bcc
cmpb $0x0, 0x6(%rsp)
setne %al
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
cmpq $0x2, %rsi
setne %cl
orb %al, %cl
je 0x25b6a
addq $-0x2, %rsi
callq 0xe3f46
movq %rax, %r14
cmpq $0x1a00, %rax           # imm = 0x1A00
sete %bpl
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x8, %rsi
callq 0xe404f
cmpq $0x504baa55, %rax       # imm = 0x504BAA55
sete %al
orb %al, %bpl
jne 0x25b44
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x2, %rsi
callq 0xe3f46
cmpq $0xfe00, %rax           # imm = 0xFE00
sete %al
cmpb $0x0, 0x6(%rsp)
movq 0x10(%rsp), %rsi
je 0x25b97
cmpq $0x1a00, %r14           # imm = 0x1A00
sete %cl
orb %al, %cl
testb %cl, %sil
je 0x25ba8
pushq $0x5a
popq %r14
jmp 0x25a59
xorl %r14d, %r14d
cmpb $0x0, 0x10(%rsp)
movq 0x28(%rsp), %rdx
je 0x25a59
xorl %eax, %eax
cmpl $0x4, %edx
pushq $0xf
popq %rcx
cmovael %eax, %ecx
testl %edx, %edx
pushq $0x64
popq %r14
cmovnel %ecx, %r14d
jmp 0x25a59
xorl %ecx, %ecx
testb %al, %al
pushq $0x19
popq %r14
cmovel %ecx, %r14d
jmp 0x25a59
xorl %eax, %eax
testq %r12, %r12
pushq $0x13
popq %rdx
cmovnel %eax, %edx
testb %sil, %sil
pushq $0x18
popq %r14
cmovel %edx, %r14d
testb %cl, %cl
pushq $0x23
popq %rax
cmovnel %eax, %r14d
jmp 0x25a59
movq 0x20(%rsp), %rcx
addq $-0x2, %rcx
cmpq %rcx, %rax
jg 0x25a56
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0xe3f2a
cmpb $0x1a, %al
jne 0x25a56
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0xe3f2a
cmpb $0xc, %al
jb 0x25ada
cmpb $0x1e, %al
ja 0x25a56
movzbl %al, %eax
movl $0x40700000, %ecx       # imm = 0x40700000
btl %eax, %ecx
jb 0x25ada
jmp 0x25a56