// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
xorl %r12d, %r12d
leaq 0x1b(%rsp), %rsi
movl $0x5, %ecx
xorl %edx, %edx
callq 0xeeee8
movb 0x1b(%rsp,%r12), %cl
cmpb $0x1a, %cl
je 0x2633b
incq %r12
cmpq $0x4, %r12
jne 0x26325
xorl %r12d, %r12d
xorl %eax, %eax
cmpb $0x1a, %cl
jne 0x26424
movb 0x1c(%rsp,%r12), %bpl
testb %bpl, %bpl
sete %cl
testq %r12, %r12
setne %dl
testb %cl, %dl
jne 0x26424
leaq 0xf90f4(%rip), %rsi     # 0x11f45a
movq %rbx, %rdi
callq 0xea936
xorl %r15d, %r15d
testl %eax, %eax
je 0x2637c
movb $0x1, 0x5(%rsp)
jmp 0x263b7
leaq 0xee8ad(%rip), %r14     # 0x114c30
cmpq $0x5, %r15
je 0x263af
movslq 0x4(%r14,%r15,4), %rsi
addq %r14, %rsi
movq %rbx, %rdi
callq 0xea936
incq %r15
testl %eax, %eax
je 0x26383
leaq -0x1(%r15), %rax
cmpq $0x5, %rax
setb 0x5(%rsp)
jmp 0x263b7
setb 0x5(%rsp)
xorl %r15d, %r15d
testq %r12, %r12
je 0x263cf
xorl %eax, %eax
cmpl $0x5, %r15d
ja 0x26424
movl $0x23, %ecx
btl %r15d, %ecx
jae 0x26424
movb %bpl, 0x7(%rsp)
leaq 0x1(%r12), %rax
testq %r12, %r12
sete %r13b
cmpl $0x2, %r15d
setb 0x6(%rsp)
cmpl $0x4, %r15d
setb %r14b
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rcx
movq %rcx, 0x20(%rsp)
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movq %rdi, 0x10(%rsp)
callq 0xef14d
movl %eax, %ebp
cmpb $0xc, %al
setae %al
cmpb $0x14, %bpl
setne %cl
testb %cl, %al
je 0x26433
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb %bpl, %bpl
je 0x264a6
andb 0x5(%rsp), %r13b
andb %r13b, %r14b
cmpb $0x14, %bpl
setae %al
orb %r14b, %al
jne 0x2645f
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0xef0d5
cmpb $0x20, %al
jb 0x26422
andb 0x6(%rsp), %r13b
addq $0xd, 0x8(%rsp)
testb %r13b, %r13b
jne 0x264a6
leaq 0x8(%rsp), %r14
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0xef3a0
movq (%r14), %rcx
xorl %edx, %edx
cmpb $0x1, %bpl
setne %dl
leaq (%rax,%rdx,4), %rax
addq %rcx, %rax
addq $0x6, %rax
movq %rax, (%r14)
cmpq 0x20(%rsp), %rax
jne 0x26597
movb 0x7(%rsp), %r13b
testb %r13b, %r13b
jne 0x264e6
movq 0x18(%rbx), %rax
cmpq $0x2, 0x18(%rax)
jne 0x264e6
xorl %eax, %eax
cmpb $0x0, 0x5(%rsp)
je 0x26424
xorl %eax, %eax
cmpl $0x4, %r15d
movl $0xf, %ecx
cmovael %eax, %ecx
testl %r15d, %r15d
movl $0x64, %eax
cmovnel %ecx, %eax
jmp 0x26424
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x2, %rsi
callq 0xef169
movq %rax, %r14
cmpq $0x1a00, %rax           # imm = 0x1A00
sete %bpl
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x8, %rsi
callq 0xef32a
cmpq $0x504baa55, %rax       # imm = 0x504BAA55
sete %al
orb %al, %bpl
jne 0x2653d
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x2, %rsi
callq 0xef169
cmpq $0xfe00, %rax           # imm = 0xFE00
sete %al
testb %r13b, %r13b
je 0x26586
cmpq $0x1a00, %r14           # imm = 0x1A00
sete %cl
orb %al, %cl
movl $0x5a, %eax
testb %cl, 0x5(%rsp)
jne 0x26424
xorl %eax, %eax
testq %r12, %r12
movl $0x13, %edx
cmovnel %eax, %edx
cmpb $0x0, 0x5(%rsp)
movl $0x18, %esi
cmovel %edx, %esi
testb %cl, %cl
movl $0x23, %eax
cmovel %esi, %eax
jmp 0x26424
xorl %ecx, %ecx
testb %al, %al
movl $0x19, %eax
cmovel %ecx, %eax
jmp 0x26424
movq 0x20(%rsp), %rcx
addq $-0x2, %rcx
cmpq %rcx, %rax
jg 0x26422
leaq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0xef14d
cmpb $0x1a, %al
jne 0x26422
leaq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0xef14d
cmpb $0xc, %al
jb 0x264a6
cmpb $0x1e, %al
ja 0x26422
movzbl %al, %eax
movl $0x40700000, %ecx       # imm = 0x40700000
btl %eax, %ecx
jb 0x264a6
jmp 0x26422