// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x3e(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x5, %ecx
callq 0x164d10
movq -0x10(%rbp), %rdi
leaq -0x3e(%rbp), %rsi
movl $0x4, %edx
leaq -0x30(%rbp), %rcx
callq 0x30450
cmpl $0x0, %eax
jne 0x2f575
movl $0x0, -0x4(%rbp)
jmp 0x2f7ed
movq -0x30(%rbp), %rax
movb -0x3d(%rbp,%rax), %al
movb %al, -0x39(%rbp)
movzbl -0x39(%rbp), %eax
cmpl $0x0, %eax
jne 0x2f5a3
cmpq $0x0, -0x30(%rbp)
je 0x2f59c
movl $0x0, -0x4(%rbp)
jmp 0x2f7ed
movl $0x1, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
cmpq $0x6, -0x38(%rbp)
jae 0x2f5ec
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rcx
leaq 0x1f5e7f(%rip), %rax    # 0x225440
movq (%rax,%rcx,8), %rsi
callq 0x15d180
cmpl $0x0, %eax
je 0x2f5dc
movb $0x1, -0x11(%rbp)
movq -0x38(%rbp), %rax
movl %eax, -0x18(%rbp)
jmp 0x2f5ec
jmp 0x2f5de
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x2f5ab
cmpq $0x0, -0x30(%rbp)
jle 0x2f615
cmpl $0x0, -0x18(%rbp)
je 0x2f605
cmpl $0x1, -0x18(%rbp)
je 0x2f605
cmpl $0x5, -0x18(%rbp)
jne 0x2f607
jmp 0x2f613
movl $0x0, -0x4(%rbp)
jmp 0x2f7ed
jmp 0x2f615
movzbl -0x11(%rbp), %eax
cmpl $0x0, %eax
je 0x2f634
cmpl $0x1, -0x18(%rbp)
ja 0x2f634
cmpq $0x0, -0x30(%rbp)
jne 0x2f634
movl $0x0, -0x1c(%rbp)
jmp 0x2f65c
movzbl -0x11(%rbp), %eax
cmpl $0x0, %eax
je 0x2f653
cmpl $0x3, -0x18(%rbp)
ja 0x2f653
cmpq $0x0, -0x30(%rbp)
jne 0x2f653
movl $0x1, -0x1c(%rbp)
jmp 0x2f65a
movl $0x2, -0x1c(%rbp)
jmp 0x2f65c
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
movl -0x1c(%rbp), %ecx
callq 0x330c0
cmpl $0x0, %eax
jne 0x2f68d
movl $0x0, -0x4(%rbp)
jmp 0x2f7ed
cmpl $0x0, -0x28(%rbp)
je 0x2f6e4
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
cmpq $0x2, 0x18(%rax)
jne 0x2f6e4
cmpb $0x0, -0x11(%rbp)
jne 0x2f6b4
movl $0x0, -0x4(%rbp)
jmp 0x2f7ed
cmpl $0x0, -0x18(%rbp)
jne 0x2f6c6
movl $0x64, -0x4(%rbp)
jmp 0x2f7ed
cmpl $0x3, -0x18(%rbp)
ja 0x2f6d8
movl $0xf, -0x4(%rbp)
jmp 0x2f7ed
movl $0x0, -0x4(%rbp)
jmp 0x2f7ed
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rsi
subq $0x2, %rsi
callq 0x1651d0
cmpq $0x1a00, %rax           # imm = 0x1A00
jne 0x2f710
movl $0x1, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rsi
subq $0x8, %rsi
callq 0x165470
cmpq $0x504baa55, %rax       # imm = 0x504BAA55
jne 0x2f73c
movl $0x1, -0x24(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x2f776
cmpl $0x0, -0x24(%rbp)
jne 0x2f776
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rsi
subq $0x2, %rsi
callq 0x1651d0
cmpq $0xfe00, %rax           # imm = 0xFE00
jne 0x2f774
movl $0x1, -0x24(%rbp)
jmp 0x2f776
cmpl $0x0, -0x28(%rbp)
je 0x2f794
cmpl $0x0, -0x24(%rbp)
je 0x2f78b
movl $0x19, -0x4(%rbp)
jmp 0x2f7ed
movl $0x0, -0x4(%rbp)
jmp 0x2f7ed
movzbl -0x11(%rbp), %eax
cmpl $0x0, %eax
je 0x2f7b2
cmpl $0x0, -0x20(%rbp)
jne 0x2f7a9
cmpl $0x0, -0x24(%rbp)
je 0x2f7b2
movl $0x5a, -0x4(%rbp)
jmp 0x2f7ed
cmpl $0x0, -0x20(%rbp)
jne 0x2f7be
cmpl $0x0, -0x24(%rbp)
je 0x2f7c7
movl $0x23, -0x4(%rbp)
jmp 0x2f7ed
cmpb $0x0, -0x11(%rbp)
je 0x2f7d6
movl $0x18, -0x4(%rbp)
jmp 0x2f7ed
cmpq $0x0, -0x30(%rbp)
jne 0x2f7e6
movl $0x13, -0x4(%rbp)
jmp 0x2f7ed
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)