// File: /workspace/github_c_makefile_star2_O2/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 0xf(%rsp), %r12
pushq $0x9
popq %rcx
movq %r12, %rsi
xorl %edx, %edx
callq 0xe3cb7
movb (%r12), %r12b
cmpb $-0x42, %r12b
jne 0x4a044
movq 0x18(%r15), %rdi
leaq 0xc74fe(%rip), %rdx     # 0x111510
pushq $0x23
popq %rsi
pushq $0x3
popq %rcx
callq 0xe4d48
testl %eax, %eax
jne 0x4a03f
movq 0x18(%r15), %rdi
leaq 0xc73a4(%rip), %rdx     # 0x1113d0
pushq $0x11
popq %rsi
pushq $0x8
popq %rcx
callq 0xe4d48
testl %eax, %eax
je 0x4a244
movb 0xf(%rsp), %r12b
cmpb $-0x41, %r12b
jne 0x4a06d
movq 0x18(%r15), %rdi
leaq 0xc737b(%rip), %rdx     # 0x1113d0
pushq $0x11
popq %rsi
pushq $0x8
popq %rcx
callq 0xe4d48
testl %eax, %eax
je 0x4a21a
movb 0xf(%rsp), %r12b
cmpb $-0x7, %r12b
jne 0x4a0b0
movq 0x18(%r15), %rdi
leaq 0xc7492(%rip), %rdx     # 0x111510
pushq $0x41
popq %rsi
pushq $0x3
popq %rcx
callq 0xe4d48
testl %eax, %eax
jne 0x4a0ab
movq 0x18(%r15), %rdi
leaq 0xc7475(%rip), %rdx     # 0x11150d
pushq $0xa
popq %rsi
pushq $0x2
popq %rcx
callq 0xe4d48
testl %eax, %eax
je 0x4a26a
movb 0xf(%rsp), %r12b
cmpb $-0x63, %r12b
je 0x4a118
movzbl %r12b, %eax
cmpl $0xb4, %eax
jne 0x4a13d
cmpl $0x21cd4cb4, 0xf(%rsp)  # imm = 0x21CD4CB4
jne 0x4a13d
cmpw $0x899d, 0x13(%rsp)     # imm = 0x899D
jne 0x4a13d
movl $0x1, 0x4(%rbx)
leaq 0x15(%rsp), %rdi
leaq 0xc7429(%rip), %rsi     # 0x111510
pushq $0x3
popq %rdx
callq 0x18230
testl %eax, %eax
je 0x4a37d
movl $0x1, (%rbx)
movw $0x101, 0x9(%rbx)       # imm = 0x101
movq $0x6, 0x18(%rbx)
movq $0x8, 0x20(%rbx)
jmp 0x4a38f
cmpw $0x899d, 0xf(%rsp)      # imm = 0x899D
jne 0x4a13d
leaq 0x11(%rsp), %rdi
leaq 0xc73e3(%rip), %rsi     # 0x111510
pushq $0x3
popq %rdx
callq 0x18230
testl %eax, %eax
je 0x4a325
testb %bpl, %bpl
jne 0x4a38f
movl %r12d, %ecx
xorb $0x4d, %cl
movb 0x10(%rsp), %al
movl %eax, %edx
xorb $0x5a, %dl
orb %cl, %dl
je 0x4a168
xorb $0x5a, %r12b
xorb $0x4d, %al
orb %r12b, %al
jne 0x4a38f
movq 0x18(%r15), %rdi
pushq $0x8
popq %r13
movq %r13, %rsi
callq 0xe3fa3
movq %rax, %r12
shlq $0x4, %r12
movq 0x18(%r15), %rdi
leaq 0x2d(%r12), %rsi
leaq 0xc738a(%rip), %rdx     # 0x111519
movq %r13, %rcx
callq 0xe4d48
movl %eax, %ebp
testl %eax, %eax
je 0x4a262
movq 0x18(%r15), %rdi
leaq 0x28(%r12), %rsi
leaq 0xc7368(%rip), %rdx     # 0x111519
pushq $0x8
popq %rcx
callq 0xe4d48
testl %eax, %eax
je 0x4a288
movq 0x18(%r15), %rdi
leaq 0x14(%r12), %rsi
leaq 0xc7348(%rip), %rdx     # 0x111519
pushq $0x8
popq %rcx
callq 0xe4d48
testl %eax, %eax
je 0x4a340
movq 0x18(%r15), %rdi
leaq 0x17(%r12), %rsi
leaq 0xc7328(%rip), %rdx     # 0x111519
pushq $0x8
popq %rcx
callq 0xe4d48
testl %eax, %eax
jne 0x4a38f
pushq $0x17
popq %rax
movb $0x1, %cl
movl %ecx, 0x4(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x8(%rsp)
jmp 0x4a29e
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 0x4a38f
movabsq $0x200000005, %rax      # imm = 0x200000005
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
movq $0x23, 0x10(%rbx)
jmp 0x4a38f
pushq $0x2d
popq %rax
xorl %r13d, %r13d
jmp 0x4a28e
movabsq $0x200000006, %rax      # imm = 0x200000006
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
movq $0x41, 0x10(%rbx)
jmp 0x4a38f
pushq $0x28
popq %rax
movb $0x1, %r13b
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
movq 0x18(%r15), %rdi
leaq (%r12,%rax), %rsi
addq $0x1a, %rsi
callq 0xe3eb4
cmpb $-0x6b, %al
jne 0x4a2b7
movb $0x1, 0xc(%rbx)
testl %ebp, %ebp
je 0x4a2d7
testb %r13b, %r13b
je 0x4a303
movabsq $0x300000009, %rax      # imm = 0x300000009
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
addq $0x4b, %r12
jmp 0x4a2ec
movabsq $0x30000000a, %rax      # imm = 0x30000000A
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
addq $0x4c, %r12
movq %r12, 0x10(%rbx)
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4a3c1
jmp 0x4a38f
cmpb $0x0, 0x8(%rsp)
je 0x4a351
movabsq $0x300000008, %rax      # imm = 0x300000008
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
addq $0x37, %r12
movq %r12, 0x10(%rbx)
jmp 0x4a38f
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, (%rbx)
movb $0x1, 0x8(%rbx)
movq $0x2, 0x10(%rbx)
jmp 0x4a38f
pushq $0x14
popq %rax
movb $0x1, %cl
movl %ecx, 0x8(%rsp)
xorl %r13d, %r13d
jmp 0x4a296
cmpb $0x0, 0x4(%rsp)
je 0x4a38f
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 0x4a38f
movl $0x3, (%rbx)
movb $0x1, 0x8(%rbx)
movq $0x6, 0x10(%rbx)
cmpb $0x0, 0x8(%rbx)
je 0x4a3b2
movw $0x101, 0x9(%rbx)       # imm = 0x101
movq 0x10(%rbx), %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
paddq 0xc7033(%rip), %xmm0    # 0x1113e0
movdqu %xmm0, 0x18(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq