// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq %rdi, %r14
xorl %ebx, %ebx
leaq 0x80a10(%rip), %r15     # 0x128730
xorl %r12d, %r12d
cmpq $0x10, %r12
je 0xa7edc
movslq (%r12,%r15), %rsi
addq %r15, %rsi
movq %r14, %rdi
callq 0xdfcd5
addq $0x4, %r12
testl %eax, %eax
je 0xa7d23
movq 0x18(%r14), %rdi
pushq $0x20
popq %rsi
callq 0xe3f46
movq %rax, %r15
movq 0x18(%r14), %rdi
leaq 0x8(%rsp), %r12
pushq $0x3c
popq %rdx
pushq $0x8
popq %rcx
movq %r12, %rsi
callq 0xe3cb7
movq %r12, %rdi
callq 0xa81c2
testl %eax, %eax
je 0xa7edc
leaq 0xc(%rsp), %rdi
callq 0xa81c2
testl %eax, %eax
je 0xa7edc
testb $0x1, %r15b
jne 0xa7dbf
xorl %eax, %eax
pushq $0x19
popq %rbp
leaq 0x809a2(%rip), %rcx     # 0x128740
pushq $0x64
popq %rbx
cmpq $0x10, %rax
je 0xa7dd0
movslq (%rax,%rcx), %rdx
movq 0x8(%rsp), %rsi
addq $0x4, %rax
cmpq (%rdx,%rcx), %rsi
jne 0xa7da1
jmp 0xa7edc
cmpl $0x6c707061, 0x8(%rsp)  # imm = 0x6C707061
pushq $0x5a
popq %rax
pushq $0x19
popq %rbp
cmovel %eax, %ebp
movq 0x18(%r14), %rdi
xorl %ebx, %ebx
leaq 0x20(%rsp), %rsi
pushq $0x20
popq %rcx
xorl %edx, %edx
callq 0xe3cb7
xorl %eax, %eax
cmpq $0x20, %rax
je 0xa7edc
movb 0x20(%rsp,%rax), %cl
testb %cl, %cl
je 0xa7e06
incq %rax
cmpb $0x20, %cl
jae 0xa7de7
jmp 0xa7edc
movq 0x18(%r14), %rdi
pushq $0x34
popq %rsi
callq 0xe404f
movq %rax, %r12
movq 0x18(%r14), %rdi
pushq $0x38
popq %rsi
callq 0xe404f
movq %rax, 0x10(%rsp)
movq 0x18(%r14), %rdi
pushq $0x4c
popq %rsi
callq 0xe3f46
movl %r15d, %ecx
andl $0x1, %ecx
leaq 0x8(,%rcx,2), %r13
imulq %rax, %r13
addq $0x4e, %r13
movq 0x18(%r14), %rdi
movq 0x18(%rdi), %rcx
cmpq %rcx, %r13
jg 0xa7edc
testq %r12, %r12
je 0xa7e6d
cmpq %r13, %r12
movq %r12, %r13
movq 0x10(%rsp), %rdx
jge 0xa7e72
jmp 0xa7edc
movq 0x10(%rsp), %rdx
cmpq %rcx, %r13
jg 0xa7edc
testq %rdx, %rdx
je 0xa7e84
cmpq %r13, %rdx
movq %rdx, %r13
jl 0xa7edc
cmpq %rcx, %r13
jg 0xa7edc
testq %rax, %rax
jle 0xa7e98
testb $0x1, %r15b
jne 0xa7e9d
pushq $0x4e
jmp 0xa7ec2
pushq $0x9
popq %rbx
jmp 0xa7edc
leaq 0x1c(%rsp), %r15
pushq $0x4e
popq %rdx
pushq $0x4
popq %rcx
movq %r15, %rsi
callq 0xe3cb7
movq %r15, %rdi
callq 0xa81c2
testl %eax, %eax
je 0xa7edc
movq 0x18(%r14), %rdi
pushq $0x54
popq %rsi
callq 0xe404f
cmpq %r13, %rax
jl 0xa7edc
movq 0x18(%r14), %rcx
xorl %edx, %edx
cmpq 0x18(%rcx), %rax
cmovgl %edx, %ebp
movl %ebp, %ebx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq