// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/palmpdb.c
// Function: de_identify_pdb_prc
static int de_identify_pdb_prc(deark *c)
{
	u8 has_ext = 0;
	u8 is_prc;
	u8 appl_type = 0;
	u8 id[8];
	u8 buf[32];
	u32 attribs;
	i64 appinfo_offs;
	i64 sortinfo_offs;
	i64 n;
	i64 num_recs;
	i64 recdata_offs;
	i64 curpos;

	static const char *exts[] = {"pdb", "prc", "pqa", "mobi"};
	static const char *ids[] = {"vIMGView", "TEXtREAd", "pqa clpr", "BOOKMOBI"};
	size_t k;

	for(k=0; k<DE_ARRAYCOUNT(exts); k++) {
		if(de_input_file_has_ext(c, exts[k])) {
			has_ext = 1;
			break;
		}
	}
	if(!has_ext) return 0;

	attribs = (u32)de_getu16be(32);
	is_prc = (attribs & 0x0001)?1:0;

	// It is not easy to identify PDB format from its contents.
	// But it's good to do what we can, because the .pdb file extension
	// is used by several other formats.

	// Check for sensible type/creator codes
	de_read(id, 60, 8);
	if(!looks_like_a_4cc_b(id)) return 0;
	if(!looks_like_a_4cc_b(&id[4])) return 0;

	// Check for known file types
	if(is_prc) {
		if(!de_memcmp(id, (const void*)"appl", 4)) appl_type = 1;
	}
	else {
		for(k=0; k<DE_ARRAYCOUNT(ids); k++) {
			if(!de_memcmp(id, ids[k], 8)) return 100;
		}
	}

	// There must be at least one NUL byte in the first 32 bytes,
	// and any bytes before the NUL must presumably be printable.
	de_read(buf, 0, 32);
	n = 0;
	for(k=0; k<32; k++) {
		if(buf[k]=='\0') {
			n=1;
			break;
		}
		if(buf[k]<32) return 0;
	}
	if(n==0) return 0;

	appinfo_offs = de_getu32be(52);
	sortinfo_offs = de_getu32be(56);
	num_recs = de_getu16be(72+4);

	curpos = 72 + 6 + num_recs*(is_prc?10:8);
	if(curpos>c->infile->len) return 0;

	if(appinfo_offs!=0) {
		if(appinfo_offs<curpos) return 0;
		curpos = appinfo_offs;
	}
	if(curpos>c->infile->len) return 0;

	if(sortinfo_offs!=0) {
		if(sortinfo_offs<curpos) return 0;
		curpos = sortinfo_offs;
	}
	if(curpos>c->infile->len) return 0;

	if(num_recs>0) {
		// Sanity-check the first record.
		// TODO? We could check more than one record.
		if(is_prc) {
			if(!looks_like_a_4cc_f(c->infile, 72+6+0)) return 0;
		}
		if(is_prc)
			recdata_offs = de_getu32be(72+6+6);
		else
			recdata_offs = de_getu32be(72+6+0);
		if(recdata_offs<curpos) return 0;
		curpos = recdata_offs;
		if(curpos>c->infile->len) return 0;
	}

	if(num_recs>0) {
		if(appl_type) return 90;
		return 25;
	}
	return 9;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
xorl %r14d, %r14d
leaq 0x864d9(%rip), %r15     # 0x135684
movslq (%r14,%r15), %rsi
addq %r15, %rsi
movq %rbx, %rdi
callq 0xea936
testl %eax, %eax
jne 0xaf1cf
addq $0x4, %r14
cmpq $0x10, %r14
jne 0xaf1ab
xorl %ebp, %ebp
jmp 0xaf391
movq 0x18(%rbx), %rdi
movl $0x20, %esi
callq 0xef169
movq %rax, %r14
movq 0x18(%rbx), %rdi
leaq 0x4(%rsp), %rsi
movl $0x3c, %edx
movl $0x8, %ecx
callq 0xeeee8
xorl %eax, %eax
movb 0x4(%rsp,%rax), %cl
addb $-0x7f, %cl
cmpb $-0x5f, %cl
jb 0xaf213
leaq 0x1(%rax), %rdx
cmpq $0x3, %rax
movq %rdx, %rax
jne 0xaf1fa
xorl %ebp, %ebp
cmpb $-0x5f, %cl
jb 0xaf391
movl $0x4, %eax
movb 0x4(%rsp,%rax), %cl
addb $-0x7f, %cl
cmpb $-0x5f, %cl
jb 0xaf23c
leaq 0x1(%rax), %rdx
cmpq $0x7, %rax
movq %rdx, %rax
jne 0xaf223
cmpb $-0x5f, %cl
jb 0xaf391
testb $0x1, %r14b
jne 0xaf279
xorl %eax, %eax
leaq 0x86440(%rip), %rcx     # 0x135694
movq 0x4(%rsp), %rdx
movslq (%rax,%rcx), %rsi
cmpq (%rsi,%rcx), %rdx
je 0xaf38c
addq $0x4, %rax
cmpq $0x10, %rax
jne 0xaf259
movl $0x19, %r12d
jmp 0xaf290
cmpl $0x6c707061, 0x4(%rsp)  # imm = 0x6C707061
movl $0x5a, %eax
movl $0x19, %r12d
cmovel %eax, %r12d
movq 0x18(%rbx), %rdi
xorl %r15d, %r15d
leaq 0x10(%rsp), %rsi
movl $0x20, %ecx
xorl %edx, %edx
callq 0xeeee8
xorl %ebp, %ebp
movb 0x10(%rsp,%r15), %al
testb %al, %al
je 0xaf2cd
cmpb $0x20, %al
jb 0xaf391
leaq 0x1(%r15), %rax
cmpq $0x1f, %r15
movq %rax, %r15
jne 0xaf2aa
jmp 0xaf391
movl %r12d, 0xc(%rsp)
movq 0x18(%rbx), %rdi
movl $0x34, %esi
callq 0xef32a
movq %rax, %r15
movq 0x18(%rbx), %rdi
movl $0x38, %esi
callq 0xef32a
movq %rax, %r13
movq 0x18(%rbx), %rdi
movl $0x4c, %esi
callq 0xef169
movl %r14d, %ecx
andl $0x1, %ecx
leaq 0x8(,%rcx,2), %r12
imulq %rax, %r12
addq $0x4e, %r12
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rcx
xorl %ebp, %ebp
cmpq %rcx, %r12
jg 0xaf391
testq %r15, %r15
je 0xaf334
cmpq %r12, %r15
movq %r15, %r12
jl 0xaf391
cmpq %rcx, %r12
jg 0xaf391
testq %r13, %r13
je 0xaf346
cmpq %r12, %r13
movq %r13, %r12
jl 0xaf391
cmpq %rcx, %r12
jg 0xaf391
testq %rax, %rax
jle 0xaf3a2
movl $0x4e, %esi
testb $0x1, %r14b
je 0xaf369
callq 0xaf747
movl $0x54, %esi
testl %eax, %eax
je 0xaf391
movq 0x18(%rbx), %rdi
callq 0xef32a
cmpq %r12, %rax
movl 0xc(%rsp), %esi
jl 0xaf391
movq 0x18(%rbx), %rcx
xorl %edx, %edx
cmpq 0x18(%rcx), %rax
cmovgl %edx, %esi
movl %esi, %ebp
jmp 0xaf391
movl $0x64, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x9, %ebp
jmp 0xaf391