// File: /workspace/github_c_makefile_star2_O1/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 0x3(%rsp), %r12
movl $0x9, %ecx
movq %r12, %rsi
xorl %edx, %edx
callq 0xeeee8
cmpb $-0x42, (%r12)
jne 0x4be33
movq 0x18(%r15), %rdi
leaq 0xd26db(%rip), %rdx     # 0x11e4d9
movl $0x23, %esi
movl $0x3, %ecx
callq 0xf025e
testl %eax, %eax
jne 0x4be33
movq 0x18(%r15), %rdi
leaq 0xd2594(%rip), %rdx     # 0x11e3b0
movl $0x11, %esi
movl $0x8, %ecx
callq 0xf025e
testl %eax, %eax
je 0x4bff9
cmpb $-0x41, 0x3(%rsp)
jne 0x4be5c
movq 0x18(%r15), %rdi
leaq 0xd256b(%rip), %rdx     # 0x11e3b0
movl $0x11, %esi
movl $0x8, %ecx
callq 0xf025e
testl %eax, %eax
je 0x4bfcf
cmpb $-0x7, 0x3(%rsp)
jne 0x4bea3
movq 0x18(%r15), %rdi
leaq 0xd266b(%rip), %rdx     # 0x11e4d9
movl $0x41, %esi
movl $0x3, %ecx
callq 0xf025e
testl %eax, %eax
jne 0x4bea3
movq 0x18(%r15), %rdi
leaq 0xd264a(%rip), %rdx     # 0x11e4d6
movl $0xa, %esi
movl $0x2, %ecx
callq 0xf025e
testl %eax, %eax
je 0x4c022
movzbl 0x3(%rsp), %eax
cmpl $0x9d, %eax
je 0x4bed1
cmpl $0xb4, %eax
jne 0x4bef6
cmpl $0x21cd4cb4, 0x3(%rsp)  # imm = 0x21CD4CB4
jne 0x4becd
cmpw $0x899d, 0x7(%rsp)      # imm = 0x899D
je 0x4c0be
cmpb $-0x63, %al
jne 0x4bef6
cmpw $0x899d, 0x3(%rsp)      # imm = 0x899D
jne 0x4bef6
movzwl 0x5(%rsp), %ecx
xorl $0x6c64, %ecx           # imm = 0x6C64
movzbl 0x7(%rsp), %edx
xorl $0x7a, %edx
orw %cx, %dx
je 0x4c040
testb %bpl, %bpl
jne 0x4c1d0
movl %eax, %edx
xorb $0x4d, %dl
movb 0x4(%rsp), %cl
movl %ecx, %esi
xorb $0x5a, %sil
orb %dl, %sil
je 0x4bf20
xorb $0x5a, %al
xorb $0x4d, %cl
orb %al, %cl
jne 0x4c1d0
movq 0x18(%r15), %rdi
movl $0x8, %esi
callq 0xef1ca
movq %rax, %r12
shlq $0x4, %r12
movq 0x18(%r15), %rdi
leaq 0x2d(%r12), %rsi
leaq 0xd259d(%rip), %rdx     # 0x11e4e2
movl $0x8, %ecx
callq 0xf025e
movl %eax, %ebp
testl %eax, %eax
je 0x4c017
movq 0x18(%r15), %rdi
leaq 0x28(%r12), %rsi
leaq 0xd2579(%rip), %rdx     # 0x11e4e2
movl $0x8, %ecx
callq 0xf025e
testl %eax, %eax
je 0x4c05e
movq 0x18(%r15), %rdi
leaq 0x14(%r12), %rsi
leaq 0xd2557(%rip), %rdx     # 0x11e4e2
movl $0x8, %ecx
callq 0xf025e
testl %eax, %eax
je 0x4c16d
movq 0x18(%r15), %rdi
leaq 0x17(%r12), %rsi
leaq 0xd2535(%rip), %rdx     # 0x11e4e2
movl $0x8, %ecx
callq 0xf025e
testl %eax, %eax
je 0x4c17e
movb $0x1, %cl
xorl %esi, %esi
xorl %r13d, %r13d
xorl %edi, %edi
xorl %eax, %eax
jmp 0x4c06d
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 0x4c1d0
movabsq $0x200000005, %rax      # imm = 0x200000005
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
movq $0x23, 0x10(%rbx)
jmp 0x4c1d0
movl $0x2d, %eax
xorl %ecx, %ecx
xorl %esi, %esi
jmp 0x4c068
movabsq $0x200000006, %rax      # imm = 0x200000006
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
movq $0x41, 0x10(%rbx)
jmp 0x4c1d0
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
movq $0x2, 0x10(%rbx)
jmp 0x4c1d0
movl $0x28, %eax
movb $0x1, %sil
xorl %ecx, %ecx
xorl %r13d, %r13d
xorl %edi, %edi
testb %cl, %cl
jne 0x4c1d0
movl %edi, 0xc(%rsp)
movq %r14, 0x10(%rsp)
movq %rsi, %r14
movq 0x18(%r15), %rdi
leaq (%r12,%rax), %rsi
addq $0x1a, %rsi
callq 0xef0d5
cmpb $-0x6b, %al
jne 0x4c09a
movb $0x1, 0xc(%rbx)
testl %ebp, %ebp
je 0x4c101
testb %r14b, %r14b
je 0x4c14d
movabsq $0x300000009, %rax      # imm = 0x300000009
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
addq $0x4b, %r12
jmp 0x4c116
movl $0x1, 0x4(%rbx)
movzwl 0x9(%rsp), %eax
xorl $0x6c64, %eax           # imm = 0x6C64
movzbl 0xb(%rsp), %ecx
xorl $0x7a, %ecx
orw %ax, %cx
je 0x4c1be
movl $0x1, (%rbx)
movw $0x101, 0x9(%rbx)       # imm = 0x101
movq $0x6, 0x18(%rbx)
movq $0x8, 0x20(%rbx)
jmp 0x4c1d0
movabsq $0x30000000a, %rax      # imm = 0x30000000A
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
addq $0x4c, %r12
movq %r12, 0x10(%rbx)
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x4c1d0
movq 0x18(%r15), %rdi
movq 0x48(%rax), %rsi
addq $-0xd, %rsi
callq 0xef169
cmpl $0xed55, %eax           # imm = 0xED55
jne 0x4c1d0
movb $0x1, 0xb(%rbx)
jmp 0x4c1d0
testb %r13b, %r13b
je 0x4c192
movabsq $0x300000008, %rax      # imm = 0x300000008
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
addq $0x37, %r12
movq %r12, 0x10(%rbx)
jmp 0x4c1d0
movl $0x14, %eax
movb $0x1, %r13b
xorl %ecx, %ecx
xorl %esi, %esi
jmp 0x4c06b
movl $0x17, %eax
movb $0x1, %dil
xorl %ecx, %ecx
xorl %esi, %esi
xorl %r13d, %r13d
jmp 0x4c06d
cmpb $0x0, 0xc(%rsp)
je 0x4c1d0
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 0x4c1d0
movl $0x3, (%rbx)
movb $0x1, 0x8(%rbx)
movq $0x6, 0x10(%rbx)
cmpb $0x0, 0x8(%rbx)
je 0x4c1f0
movw $0x101, 0x9(%rbx)       # imm = 0x101
movq 0x10(%rbx), %rax
leaq 0x6(%rax), %rcx
movq %rcx, 0x18(%rbx)
addq $0xb, %rax
movq %rax, 0x20(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq