// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/macbinary.c
// Function: de_identify_macbinary
static int de_identify_macbinary(deark *c)
{
	int conf = 0;
	int k;
	int has_sig;
	int is_v23 = 0; // v2 or v3
	int good_file_len = 0;
	int good_cc = 0;
	int bad_crc = 0;
	int fnlen;
	i64 n;
	i64 dflen, rflen;
	i64 min_expected_len;
	u32 crc_reported, crc_calc;
	u8 b[128];

	// "old" version number is always 0.
	b[0] = de_getbyte(0);
	if(b[0]!=0) goto done;

	// filename length
	b[1] = de_getbyte(1);
	if(b[1]<1 || b[1]>63) goto done;

	de_read(&b[2], 2, sizeof(b)-2);

	if(b[2]==0) goto done; // First filename byte
	if(b[74]!=0) goto done;
	if(b[82]!=0) goto done;

	// Extended version number
	if(b[122]==129 && b[123]==129) {
		// v2
		is_v23 = 1;
	}
	else if(b[122]==130 && (b[123]==129 || b[123]==130)) {
		// v3
		is_v23 = 1;
	}
	// else v1.
	// Some v1 files have garbage in the last 29 bytes of the file,
	// so we can't assume the extended version number is 0.

	// Ver.III signature, but possibly used in some files that have earlier
	// version numbers.
	has_sig = !de_memcmp(&b[102], (const void*)"mBIN", 4);
	if(has_sig) {
		conf = 90;
		goto done;
	}

	// Check if filename characters are sensible
	fnlen = (int)b[1];
	for(k=0; k<fnlen; k++) {
		if(b[2+k]>0 && b[2+k]<32) {
			// Control char found.

			if(b[2+k]==0x0d && fnlen>1 && k==fnlen-1) {
				// Exception: Allow the last char to be a CR.
				;
			}
			else {
				goto done;
			}
		}
	}

	// File type code. Expect ASCII.
	good_cc = 1;
	for(k=65; k<=68; k++) {
		if(b[k]<32 || b[k]>127) good_cc = 0;
	}

	dflen = de_getu32be_direct(&b[83]);
	rflen = de_getu32be_direct(&b[87]);

	crc_reported = (u32)de_getu16be_direct(&b[124]);

	// Check the file size.

	// Resource forks that go beyond the end of file are too common to
	// disallow.
	if(128 + dflen > c->infile->len) goto done;
	if(128 + rflen + dflen > c->infile->len + 4096) goto done;

	if(rflen>0) {
		min_expected_len = 128 + de_pad_to_n(dflen, 128) + rflen;
	}
	else {
		min_expected_len = 128 + dflen;
	}

	// The file size really should be exactly min_expected_len, or that
	// number padded to the next multiple of 128. But I'm not bold
	// enough to require it.
	if((c->infile->len == min_expected_len) ||
		(c->infile->len == de_pad_to_n(min_expected_len, 128)))
	{
		good_file_len = 1;
	}

	if(is_v23) {
		// Most MacBinary II specific checks go here

		if(!de_is_all_zeroes(&b[102], 14)) {
			if(!good_file_len) goto done;
		}

		// Secondary header length. We don't support this.
		n = de_getu16be_direct(&b[120]);
		if(n!=0) goto done;
	}
	else {
		// Most Original MacBinary format checks go here

		// An empty file is not illegal, but we need as many checks as possible
		// that won't be passed by all 0 bytes.
		if(dflen==0 && rflen==0) goto done;

		// Unused fields in this version should be all 0, though we'll allow
		// nonzero values in some cases.
		if(!de_is_all_zeroes(&b[99], 25)) {
			if(!good_file_len) goto done;
		}
	}

	if(crc_reported!=0 || is_v23) {
		struct de_crcobj *crco;

		crco = de_crcobj_create(c, DE_CRCOBJ_CRC16_XMODEM);
		de_crcobj_addbuf(crco, b, 124);
		crc_calc = de_crcobj_getval(crco);
		de_crcobj_destroy(crco);
		if(crc_calc!=crc_reported && is_v23 && crc_reported!=0) {
			bad_crc = 1;
		}
	}

	if(is_v23 && good_file_len && good_cc) {
		if(bad_crc) {
			conf = 19;
		}
		else {
			conf = 90;
		}
	}
	else if(bad_crc) {
		goto done;
	}
	else if(is_v23) {
		conf = 49;
	}
	else if(good_cc) {
		conf = 29;
	}
	else {
		conf = 19;
	}

done:
	if(conf>0) {
		c->detection_data->is_macbinary = 1;
	}
	return conf;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x165060
movb %al, -0xd0(%rbp)
movzbl -0xd0(%rbp), %eax
cmpl $0x0, %eax
je 0xd116a
jmp 0xd1599
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x1, %esi
callq 0x165060
movb %al, -0xcf(%rbp)
movzbl -0xcf(%rbp), %eax
cmpl $0x1, %eax
jl 0xd119a
movzbl -0xcf(%rbp), %eax
cmpl $0x3f, %eax
jle 0xd119f
jmp 0xd1599
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0xd0(%rbp), %rsi
addq $0x2, %rsi
movl $0x2, %edx
movl $0x7e, %ecx
callq 0x164d10
movzbl -0xce(%rbp), %eax
cmpl $0x0, %eax
jne 0xd11d2
jmp 0xd1599
movzbl -0x86(%rbp), %eax
cmpl $0x0, %eax
je 0xd11e3
jmp 0xd1599
movzbl -0x7e(%rbp), %eax
cmpl $0x0, %eax
je 0xd11f1
jmp 0xd1599
movzbl -0x56(%rbp), %eax
cmpl $0x81, %eax
jne 0xd1210
movzbl -0x55(%rbp), %eax
cmpl $0x81, %eax
jne 0xd1210
movl $0x1, -0x18(%rbp)
jmp 0xd123a
movzbl -0x56(%rbp), %eax
cmpl $0x82, %eax
jne 0xd1238
movzbl -0x55(%rbp), %eax
cmpl $0x81, %eax
je 0xd1231
movzbl -0x55(%rbp), %eax
cmpl $0x82, %eax
jne 0xd1238
movl $0x1, -0x18(%rbp)
jmp 0xd123a
leaq -0xd0(%rbp), %rax
addq $0x66, %rax
movl (%rax), %eax
subl $0x4e49426d, %eax       # imm = 0x4E49426D
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0xd1274
movl $0x5a, -0xc(%rbp)
jmp 0xd1599
movzbl -0xcf(%rbp), %eax
movl %eax, -0x28(%rbp)
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0xd12f5
movl -0x10(%rbp), %eax
addl $0x2, %eax
cltq
movzbl -0xd0(%rbp,%rax), %eax
cmpl $0x0, %eax
jle 0xd12e8
movl -0x10(%rbp), %eax
addl $0x2, %eax
cltq
movzbl -0xd0(%rbp,%rax), %eax
cmpl $0x20, %eax
jge 0xd12e8
movl -0x10(%rbp), %eax
addl $0x2, %eax
cltq
movzbl -0xd0(%rbp,%rax), %eax
cmpl $0xd, %eax
jne 0xd12e1
cmpl $0x1, -0x28(%rbp)
jle 0xd12e1
movl -0x10(%rbp), %eax
movl -0x28(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xd12e1
jmp 0xd12e6
jmp 0xd1599
jmp 0xd12e8
jmp 0xd12ea
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xd1285
movl $0x1, -0x20(%rbp)
movl $0x41, -0x10(%rbp)
cmpl $0x44, -0x10(%rbp)
jg 0xd133f
movslq -0x10(%rbp), %rax
movzbl -0xd0(%rbp,%rax), %eax
cmpl $0x20, %eax
jl 0xd132b
movslq -0x10(%rbp), %rax
movzbl -0xd0(%rbp,%rax), %eax
cmpl $0x7f, %eax
jle 0xd1332
movl $0x0, -0x20(%rbp)
jmp 0xd1334
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xd1303
leaq -0xd0(%rbp), %rdi
addq $0x53, %rdi
callq 0x165430
movq %rax, -0x38(%rbp)
leaq -0xd0(%rbp), %rdi
addq $0x57, %rdi
callq 0x165430
movq %rax, -0x40(%rbp)
leaq -0xd0(%rbp), %rdi
addq $0x7c, %rdi
callq 0x1651b0
movl %eax, -0x4c(%rbp)
movq -0x38(%rbp), %rax
addq $0x80, %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xd1397
jmp 0xd1599
movq -0x40(%rbp), %rax
addq $0x80, %rax
addq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x18(%rcx), %rcx
addq $0x1000, %rcx           # imm = 0x1000
cmpq %rcx, %rax
jle 0xd13c2
jmp 0xd1599
cmpq $0x0, -0x40(%rbp)
jle 0xd13e7
movq -0x38(%rbp), %rdi
movl $0x80, %esi
callq 0x15ce90
addq $0x80, %rax
addq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0xd13f5
movq -0x38(%rbp), %rax
addq $0x80, %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
cmpq -0x48(%rbp), %rax
je 0xd1437
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0xe0(%rbp)
movq -0x48(%rbp), %rdi
movl $0x80, %esi
callq 0x15ce90
movq %rax, %rcx
movq -0xe0(%rbp), %rax
cmpq %rcx, %rax
jne 0xd143e
movl $0x1, -0x1c(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0xd148d
leaq -0xd0(%rbp), %rdi
addq $0x66, %rdi
movl $0xe, %esi
callq 0x169cd0
cmpl $0x0, %eax
jne 0xd146b
cmpl $0x0, -0x1c(%rbp)
jne 0xd1469
jmp 0xd1599
jmp 0xd146b
leaq -0xd0(%rbp), %rdi
addq $0x78, %rdi
callq 0x1651b0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xd148b
jmp 0xd1599
jmp 0xd14c9
cmpq $0x0, -0x38(%rbp)
jne 0xd14a0
cmpq $0x0, -0x40(%rbp)
jne 0xd14a0
jmp 0xd1599
leaq -0xd0(%rbp), %rdi
addq $0x63, %rdi
movl $0x19, %esi
callq 0x169cd0
cmpl $0x0, %eax
jne 0xd14c7
cmpl $0x0, -0x1c(%rbp)
jne 0xd14c5
jmp 0xd1599
jmp 0xd14c7
jmp 0xd14c9
cmpl $0x0, -0x4c(%rbp)
jne 0xd14d5
cmpl $0x0, -0x18(%rbp)
je 0xd153a
movq -0x8(%rbp), %rdi
movl $0x20, %esi
callq 0x15eac0
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
movl $0x7c, %edx
callq 0x15f430
movq -0xd8(%rbp), %rdi
callq 0x15f340
movl %eax, -0x50(%rbp)
movq -0xd8(%rbp), %rdi
callq 0x15f2e0
movl -0x50(%rbp), %eax
cmpl -0x4c(%rbp), %eax
je 0xd1538
cmpl $0x0, -0x18(%rbp)
je 0xd1538
cmpl $0x0, -0x4c(%rbp)
je 0xd1538
movl $0x1, -0x24(%rbp)
jmp 0xd153a
cmpl $0x0, -0x18(%rbp)
je 0xd1564
cmpl $0x0, -0x1c(%rbp)
je 0xd1564
cmpl $0x0, -0x20(%rbp)
je 0xd1564
cmpl $0x0, -0x24(%rbp)
je 0xd155b
movl $0x13, -0xc(%rbp)
jmp 0xd1562
movl $0x5a, -0xc(%rbp)
jmp 0xd1597
cmpl $0x0, -0x24(%rbp)
je 0xd156c
jmp 0xd1599
cmpl $0x0, -0x18(%rbp)
je 0xd157b
movl $0x31, -0xc(%rbp)
jmp 0xd1593
cmpl $0x0, -0x20(%rbp)
je 0xd158a
movl $0x1d, -0xc(%rbp)
jmp 0xd1591
movl $0x13, -0xc(%rbp)
jmp 0xd1593
jmp 0xd1595
jmp 0xd1597
jmp 0xd1599
cmpl $0x0, -0xc(%rbp)
jle 0xd15ab
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movb $0x1, 0x4(%rax)
movl -0xc(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopw (%rax,%rax)