// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/diet.c
// Function: identify_diet_fmt
static void identify_diet_fmt(deark *c, struct diet_identify_data *idd, u8 idmode,
	struct fmtutil_exe_info *ei)
{
	static const u8 *sig_9d89 = (const u8*)"\x9d\x89";
	static const u8 *sig_dlz = (const u8*)"dlz";
	static const u8 *sig_int21 = (const u8*)"\xb4\x4c\xcd\x21";
	static const u8 *sig_old = (const u8*)"\xfd\xf3\xa5\xfc\x8b\xf7\xbf\x00";
	static const u8 *sig_8edb = (const u8*)"\x8e\xdb\x8e\xc0\x33\xf6\x33\xff\xb9";
	u8 buf[9];

	de_read(buf, 0, sizeof(buf));

	if(buf[0]==0xbe) {
		if(!dbuf_memcmp(c->infile, 35, sig_dlz, 3)) {
			if(!dbuf_memcmp(c->infile, 17, sig_old, 8))
			{
				idd->ftype = FTYPE_COM;
				idd->fmt = FMT_COM_102;
				idd->dlz_pos_known = 1;
				idd->dlz_pos = 35;
				goto done;
			}
		}
	}

	if(buf[0]==0xbf) {
		if(!dbuf_memcmp(c->infile, 17, sig_old, 8))
		{
			idd->ftype = FTYPE_COM;
			idd->fmt = FMT_COM_100;
			idd->crc_pos_known = 1;
			idd->crc_pos = 35;
			idd->cmpr_pos_known = 1;
			idd->cmpr_pos = 37;
			goto done;
		}
	}

	if(buf[0]==0xf9) {
		if(!dbuf_memcmp(c->infile, 65, sig_dlz, 3)) {
			if(!dbuf_memcmp(c->infile, 10, sig_9d89, 2)) {
				idd->ftype = FTYPE_COM;
				idd->fmt = FMT_COM_144;
				idd->dlz_pos_known = 1;
				idd->dlz_pos = 65;
				goto done;
			}
		}
	}

	if(buf[0]==0xb4) {
		if(!de_memcmp(&buf[0], sig_int21, 4)) {
			if(!de_memcmp(&buf[4], sig_9d89, 2)) {
				idd->ftype = FTYPE_DATA;
				if(!de_memcmp(&buf[6], sig_dlz, 3)) {
					idd->fmt = FMT_DATA_144;
					idd->dlz_pos_known = 1;
					idd->dlz_pos =  6;
					goto done;
				}
				idd->fmt = FMT_DATA_100;
				idd->crc_pos_known = 1;
				idd->crc_pos = 6;
				idd->cmpr_pos_known = 1;
				idd->cmpr_pos = 8;
				goto done;
			}
		}
	}

	if(buf[0]==0x9d) {
		if(!de_memcmp(&buf[0], sig_9d89, 2)) {
			if(!de_memcmp(&buf[2], sig_dlz, 3)) {
				idd->ftype = FTYPE_DATA;
				idd->fmt = FMT_DATA_102;
				idd->dlz_pos_known = 1;
				idd->dlz_pos = 2;
				goto done;
			}
		}
	}

	// Don't autodetect EXE -- It's handled by the "exe" module.
	if(idmode) goto done;

	if((buf[0]=='M' && buf[1]=='Z') || (buf[0]=='Z' && buf[1]=='M')) {
		i64 codestart;
		i64 sig_8edb_pos_rel = 0;
		u8 x;

		// TODO?: Probing for the 8e db 8e... byte pattern is good enough for
		// all the DIET-EXE files I've encountered. But it probably ought to be
		// improved, somehow.
		// I've found some files in which the "dlz" signature has been modified,
		// so checking for it wouldn't help much.

		codestart = 16 * de_getu16le(8); // Expected to be 32

		if(!dbuf_memcmp(c->infile, codestart-32+77, sig_8edb, 8)) {
			sig_8edb_pos_rel = 77-32;
		}
		else if(!dbuf_memcmp(c->infile, codestart-32+72, sig_8edb, 8)) {
			sig_8edb_pos_rel = 72-32;
		}
		else if(!dbuf_memcmp(c->infile, codestart-32+52, sig_8edb, 8)) {
			sig_8edb_pos_rel = 52-32;
		}
		else if(!dbuf_memcmp(c->infile, codestart-32+55, sig_8edb, 8)) {
			sig_8edb_pos_rel = 55-32;
		}

		if(sig_8edb_pos_rel==0) goto done;

		x = de_getbyte(codestart+sig_8edb_pos_rel+26);
		if(x==0x95) {
			idd->maybe_lglz = 1;
		}

		if(sig_8edb_pos_rel == 77-32) {
			idd->ftype = FTYPE_EXE;
			idd->fmt = FMT_EXE_145F;
			idd->dlz_pos_known = 1;
			idd->dlz_pos = codestart-32+108;
			check_for_com2exe(c, idd, ei);
			goto done;
		}

		if(sig_8edb_pos_rel == 72-32) {
			idd->ftype = FTYPE_EXE;
			idd->fmt = FMT_EXE_144;
			idd->dlz_pos_known = 1;
			idd->dlz_pos = codestart-32+107;
			check_for_com2exe(c, idd, ei);
			goto done;
		}

		if(sig_8edb_pos_rel == 52-32) {
			idd->ftype = FTYPE_EXE;
			idd->fmt = FMT_EXE_102;
			idd->dlz_pos_known = 1;
			idd->dlz_pos = codestart-32+87;
			goto done;
		}

		if(sig_8edb_pos_rel == 55-32) {
			idd->ftype = FTYPE_EXE;
			idd->fmt = FMT_EXE_100;
			idd->crc_pos_known = 1;
			idd->crc_pos = 18;
			idd->cmpr_pos_known = 1;
			idd->cmpr_pos = codestart-32+90;
			goto done;
		}
	}

done:
	if(idd->dlz_pos_known) {
		idd->crc_pos_known = 1;
		idd->cmpr_pos_known = 1;
		idd->crc_pos = idd->dlz_pos + 6;
		idd->cmpr_pos = idd->dlz_pos + 11;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r15
movq 0x18(%rdi), %rdi
leaq 0x7(%rsp), %r12
movl $0x9, %ecx
movq %r12, %rsi
xorl %edx, %edx
callq 0xee8d4
movb (%r12), %al
cmpb $-0x42, %al
jne 0x4bd40
movq 0x18(%r15), %rdi
leaq 0xd28d9(%rip), %rdx     # 0x11e5e0
movl $0x23, %esi
movl $0x3, %ecx
callq 0xefc85
testl %eax, %eax
jne 0x4bd3c
movq 0x18(%r15), %rdi
leaq 0xd277f(%rip), %rdx     # 0x11e4a4
movl $0x11, %esi
movl $0x8, %ecx
callq 0xefc85
testl %eax, %eax
je 0x4bf4a
movb 0x7(%rsp), %al
cmpb $-0x41, %al
jne 0x4bd6a
movq 0x18(%r15), %rdi
leaq 0xd2755(%rip), %rdx     # 0x11e4a4
movl $0x11, %esi
movl $0x8, %ecx
callq 0xefc85
testl %eax, %eax
je 0x4bf20
movb 0x7(%rsp), %al
cmpb $-0x7, %al
jne 0x4bdb2
movq 0x18(%r15), %rdi
leaq 0xd2867(%rip), %rdx     # 0x11e5e0
movl $0x41, %esi
movl $0x3, %ecx
callq 0xefc85
testl %eax, %eax
jne 0x4bdae
movq 0x18(%r15), %rdi
leaq 0xd2846(%rip), %rdx     # 0x11e5dd
movl $0xa, %esi
movl $0x2, %ecx
callq 0xefc85
testl %eax, %eax
je 0x4bf72
movb 0x7(%rsp), %al
cmpb $-0x63, %al
je 0x4be17
movzbl %al, %ecx
cmpl $0xb4, %ecx
jne 0x4be3c
cmpl $0x21cd4cb4, 0x7(%rsp)  # imm = 0x21CD4CB4
jne 0x4be3c
cmpw $0x899d, 0xb(%rsp)      # imm = 0x899D
jne 0x4be3c
movl $0x1, 0x4(%rbx)
movzwl 0xd(%rsp), %eax
xorl $0x6c64, %eax           # imm = 0x6C64
movzbl 0xf(%rsp), %ecx
xorl $0x7a, %ecx
orw %ax, %cx
je 0x4c0a4
movl $0x1, (%rbx)
movw $0x101, 0x9(%rbx)       # imm = 0x101
movq $0x6, 0x18(%rbx)
movq $0x8, 0x20(%rbx)
jmp 0x4c0b6
cmpw $0x899d, 0x7(%rsp)      # imm = 0x899D
jne 0x4be3c
movzwl 0x9(%rsp), %ecx
xorl $0x6c64, %ecx           # imm = 0x6C64
movzbl 0xb(%rsp), %edx
xorl $0x7a, %edx
orw %cx, %dx
je 0x4c04a
testb %bpl, %bpl
jne 0x4c0b6
movl %eax, %edx
xorb $0x4d, %dl
movb 0x8(%rsp), %cl
movl %ecx, %esi
xorb $0x5a, %sil
orb %dl, %sil
je 0x4be66
xorb $0x5a, %al
xorb $0x4d, %cl
orb %al, %cl
jne 0x4c0b6
movq 0x18(%r15), %rdi
movl $0x8, %esi
callq 0xeebc8
movq %rax, %r12
shlq $0x4, %r12
movq 0x18(%r15), %rdi
leaq 0x2d(%r12), %rsi
leaq 0xd275e(%rip), %rdx     # 0x11e5e9
movl $0x8, %ecx
callq 0xefc85
movl %eax, %ebp
testl %eax, %eax
je 0x4bf68
movq 0x18(%r15), %rdi
leaq 0x28(%r12), %rsi
leaq 0xd273a(%rip), %rdx     # 0x11e5e9
movl $0x8, %ecx
callq 0xefc85
testl %eax, %eax
je 0x4bf90
movq 0x18(%r15), %rdi
leaq 0x14(%r12), %rsi
leaq 0xd2718(%rip), %rdx     # 0x11e5e9
movl $0x8, %ecx
callq 0xefc85
testl %eax, %eax
je 0x4c065
movq 0x18(%r15), %rdi
leaq 0x17(%r12), %rsi
leaq 0xd26f6(%rip), %rdx     # 0x11e5e9
movl $0x8, %ecx
callq 0xefc85
testl %eax, %eax
jne 0x4c0b6
movl $0x17, %eax
movb $0x1, %cl
movl %ecx, 0x10(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x14(%rsp)
jmp 0x4bfa8
movabsq $0x200000004, %rax      # imm = 0x200000004
movq %rax, (%rbx)
movb $0x1, %al
movb %al, 0x9(%rbx)
movq $0x23, 0x18(%rbx)
movb %al, 0xa(%rbx)
movq $0x25, 0x20(%rbx)
jmp 0x4c0b6
movabsq $0x200000005, %rax      # imm = 0x200000005
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
movq $0x23, 0x10(%rbx)
jmp 0x4c0b6
movl $0x2d, %eax
xorl %r13d, %r13d
jmp 0x4bf98
movabsq $0x200000006, %rax      # imm = 0x200000006
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
movq $0x41, 0x10(%rbx)
jmp 0x4c0b6
movl $0x28, %eax
movb $0x1, %r13b
movl $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movq 0x18(%r15), %rdi
leaq (%r12,%rax), %rsi
addq $0x1a, %rsi
callq 0xeead3
cmpb $-0x6b, %al
jne 0x4bfc1
movb $0x1, 0xc(%rbx)
testl %ebp, %ebp
je 0x4bfe1
testb %r13b, %r13b
je 0x4c028
movabsq $0x300000009, %rax      # imm = 0x300000009
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
addq $0x4b, %r12
jmp 0x4bff6
movabsq $0x30000000a, %rax      # imm = 0x30000000A
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
addq $0x4c, %r12
movq %r12, 0x10(%rbx)
testq %r14, %r14
je 0x4c0b6
movq 0x18(%r15), %rdi
movq 0x48(%r14), %rsi
addq $-0xd, %rsi
callq 0xeeb67
cmpl $0xed55, %eax           # imm = 0xED55
jne 0x4c0b6
movb $0x1, 0xb(%rbx)
jmp 0x4c0b6
cmpb $0x0, 0x14(%rsp)
je 0x4c078
movabsq $0x300000008, %rax      # imm = 0x300000008
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
addq $0x37, %r12
movq %r12, 0x10(%rbx)
jmp 0x4c0b6
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
movq $0x2, 0x10(%rbx)
jmp 0x4c0b6
movl $0x14, %eax
movb $0x1, %cl
movl %ecx, 0x14(%rsp)
xorl %r13d, %r13d
jmp 0x4bfa0
cmpb $0x0, 0x10(%rsp)
je 0x4c0b6
movabsq $0x300000007, %rax      # imm = 0x300000007
movq %rax, (%rbx)
movw $0x101, 0x9(%rbx)       # imm = 0x101
movq $0x12, 0x18(%rbx)
addq $0x3a, %r12
movq %r12, 0x20(%rbx)
jmp 0x4c0b6
movl $0x3, (%rbx)
movb $0x1, 0x8(%rbx)
movq $0x6, 0x10(%rbx)
cmpb $0x0, 0x8(%rbx)
je 0x4c0d9
movw $0x101, 0x9(%rbx)       # imm = 0x101
movq 0x10(%rbx), %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
paddq 0xd23dc(%rip), %xmm0    # 0x11e4b0
movdqu %xmm0, 0x18(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq