// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
movb $0x0, -0x13(%rbp)
movq $0x0, -0x80(%rbp)
cmpq $0x4, -0x80(%rbp)
jae 0xfe889
movq -0x10(%rbp), %rdi
movq -0x80(%rbp), %rcx
leaq 0x126ffb(%rip), %rax    # 0x225860
movq (%rax,%rcx,8), %rsi
callq 0x15d180
cmpl $0x0, %eax
je 0xfe879
movb $0x1, -0x11(%rbp)
jmp 0xfe889
jmp 0xfe87b
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0xfe84f
cmpb $0x0, -0x11(%rbp)
jne 0xfe89b
movl $0x0, -0x4(%rbp)
jmp 0xfebea
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x20, %esi
callq 0x1651d0
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %edx
andl $0x1, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, -0x12(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x1b(%rbp), %rsi
movl $0x3c, %edx
movl $0x8, %ecx
callq 0x164d10
leaq -0x1b(%rbp), %rdi
callq 0x101280
cmpl $0x0, %eax
jne 0xfe8fb
movl $0x0, -0x4(%rbp)
jmp 0xfebea
leaq -0x1b(%rbp), %rdi
addq $0x4, %rdi
callq 0x101280
cmpl $0x0, %eax
jne 0xfe919
movl $0x0, -0x4(%rbp)
jmp 0xfebea
cmpb $0x0, -0x12(%rbp)
je 0xfe93b
leaq -0x1b(%rbp), %rax
movl (%rax), %eax
subl $0x6c707061, %eax       # imm = 0x6C707061
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xfe939
movb $0x1, -0x13(%rbp)
jmp 0xfe98f
movq $0x0, -0x80(%rbp)
cmpq $0x4, -0x80(%rbp)
jae 0xfe98d
leaq -0x1b(%rbp), %rax
movq -0x80(%rbp), %rdx
leaq 0x126f27(%rip), %rcx    # 0x225880
movq (%rcx,%rdx,8), %rcx
movq (%rcx), %rcx
movq (%rax), %rax
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xfe97d
movl $0x64, -0x4(%rbp)
jmp 0xfebea
jmp 0xfe97f
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0xfe943
jmp 0xfe98f
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x20, %ecx
callq 0x164d10
movq $0x0, -0x60(%rbp)
movq $0x0, -0x80(%rbp)
cmpq $0x20, -0x80(%rbp)
jae 0xfea02
movq -0x80(%rbp), %rax
movzbl -0x40(%rbp,%rax), %eax
cmpl $0x0, %eax
jne 0xfe9d8
movq $0x1, -0x60(%rbp)
jmp 0xfea02
movq -0x80(%rbp), %rax
movzbl -0x40(%rbp,%rax), %eax
cmpl $0x20, %eax
jge 0xfe9f2
movl $0x0, -0x4(%rbp)
jmp 0xfebea
jmp 0xfe9f4
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0xfe9b9
cmpq $0x0, -0x60(%rbp)
jne 0xfea15
movl $0x0, -0x4(%rbp)
jmp 0xfebea
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x34, %esi
callq 0x165470
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x38, %esi
callq 0x165470
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x4c, %esi
callq 0x1651d0
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movzbl -0x12(%rbp), %esi
movl $0x8, %ecx
movl $0xa, %edx
cmpl $0x0, %esi
cmovnel %edx, %ecx
movslq %ecx, %rcx
imulq %rcx, %rax
addq $0x4e, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xfea9c
movl $0x0, -0x4(%rbp)
jmp 0xfebea
cmpq $0x0, -0x50(%rbp)
je 0xfeac1
movq -0x50(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0xfeab9
movl $0x0, -0x4(%rbp)
jmp 0xfebea
movq -0x50(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xfeadf
movl $0x0, -0x4(%rbp)
jmp 0xfebea
cmpq $0x0, -0x58(%rbp)
je 0xfeb04
movq -0x58(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0xfeafc
movl $0x0, -0x4(%rbp)
jmp 0xfebea
movq -0x58(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xfeb22
movl $0x0, -0x4(%rbp)
jmp 0xfebea
cmpq $0x0, -0x68(%rbp)
jle 0xfebc4
cmpb $0x0, -0x12(%rbp)
je 0xfeb58
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x4e, %esi
callq 0x1012e0
cmpl $0x0, %eax
jne 0xfeb56
movl $0x0, -0x4(%rbp)
jmp 0xfebea
jmp 0xfeb58
cmpb $0x0, -0x12(%rbp)
je 0xfeb76
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x54, %esi
callq 0x165470
movq %rax, -0x70(%rbp)
jmp 0xfeb8c
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x4e, %esi
callq 0x165470
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0xfeb9f
movl $0x0, -0x4(%rbp)
jmp 0xfebea
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xfebc2
movl $0x0, -0x4(%rbp)
jmp 0xfebea
jmp 0xfebc4
cmpq $0x0, -0x68(%rbp)
jle 0xfebe3
cmpb $0x0, -0x13(%rbp)
je 0xfebda
movl $0x5a, -0x4(%rbp)
jmp 0xfebea
movl $0x19, -0x4(%rbp)
jmp 0xfebea
movl $0x9, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)