// File: /workspace/github_c_makefile_star2_O3/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 0x872d2(%rip), %r15     # 0x135a10
movslq (%r14,%r15), %rsi
addq %r15, %rsi
movq %rbx, %rdi
callq 0xea491
testl %eax, %eax
jne 0xae770
addq $0x4, %r14
cmpq $0x10, %r14
jne 0xae73e
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbx), %rdi
movl $0x20, %esi
callq 0xeeb67
movq %rax, %r14
movq 0x18(%rbx), %rdi
leaq 0x4(%rsp), %rsi
movl $0x3c, %edx
movl $0x8, %ecx
callq 0xee8d4
xorl %r15d, %r15d
xorl %eax, %eax
movb 0x4(%rsp,%rax), %cl
addb $-0x7f, %cl
cmpb $-0x5f, %cl
jb 0xae75e
incq %rax
cmpq $0x4, %rax
jne 0xae79e
movl $0x4, %eax
movb 0x4(%rsp,%rax), %cl
addb $-0x7f, %cl
cmpb $-0x5f, %cl
jb 0xae75e
incq %rax
cmpq $0x8, %rax
jne 0xae7b8
testb $0x1, %r14b
jne 0xae800
xorl %eax, %eax
leaq 0x87244(%rip), %rcx     # 0x135a20
movq 0x4(%rsp), %rdx
movslq (%rax,%rcx), %rsi
cmpq (%rsi,%rcx), %rdx
je 0xae8f8
addq $0x4, %rax
cmpq $0x10, %rax
jne 0xae7e1
movl $0x19, %ebp
jmp 0xae815
cmpl $0x6c707061, 0x4(%rsp)  # imm = 0x6C707061
movl $0x5a, %eax
movl $0x19, %ebp
cmovel %eax, %ebp
movq 0x18(%rbx), %rdi
xorl %r12d, %r12d
leaq 0x10(%rsp), %rsi
movl $0x20, %ecx
xorl %edx, %edx
callq 0xee8d4
movb 0x10(%rsp,%r12), %al
testb %al, %al
je 0xae854
xorl %r15d, %r15d
cmpb $0x20, %al
jb 0xae75e
leaq 0x1(%r12), %rax
cmpq $0x1f, %r12
movq %rax, %r12
jne 0xae82d
jmp 0xae75e
movl %ebp, 0xc(%rsp)
movq 0x18(%rbx), %rdi
movl $0x34, %esi
callq 0xeed0d
movq %rax, %r12
movq 0x18(%rbx), %rdi
movl $0x38, %esi
callq 0xeed0d
movq %rax, %rbp
movq 0x18(%rbx), %rdi
movl $0x4c, %esi
callq 0xeeb67
movl %r14d, %ecx
andl $0x1, %ecx
leaq 0x8(,%rcx,2), %r13
imulq %rax, %r13
addq $0x4e, %r13
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rcx
xorl %r15d, %r15d
cmpq %rcx, %r13
jg 0xae75e
testq %r12, %r12
je 0xae8c3
cmpq %r13, %r12
movq %r12, %r13
jl 0xae75e
cmpq %rcx, %r13
jg 0xae75e
testq %rbp, %rbp
je 0xae8dd
cmpq %r13, %rbp
movq %rbp, %r13
jl 0xae75e
cmpq %rcx, %r13
jg 0xae75e
testq %rax, %rax
jle 0xae903
testb $0x1, %r14b
jne 0xae90e
movl $0x4e, %esi
jmp 0xae924
movl $0x64, %r15d
jmp 0xae75e
movl $0x9, %r15d
jmp 0xae75e
callq 0xaebf0
testl %eax, %eax
je 0xae75e
movq 0x18(%rbx), %rdi
movl $0x54, %esi
callq 0xeed0d
cmpq %r13, %rax
movl 0xc(%rsp), %esi
jl 0xae75e
movq 0x18(%rbx), %rcx
xorl %edx, %edx
cmpq 0x18(%rcx), %rax
cmovgl %edx, %esi
movl %esi, %r15d
jmp 0xae75e