// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/macpaint.c
// Function: test_valid_image
static void test_valid_image(deark *c, lctx *d, struct validity_info *vi)
{
	u8 b;
	i64 count;
	i64 pos;
	i64 imgstart;
	i64 xpos_b = 0;
	i64 ypos = 0;
	int v = 0;

	imgstart = vi->pos+512;
	de_dbg(c, "checking for image at offset %d", (int)imgstart);
	de_dbg_indent(c, 1);

	// Minimum bytes per row is 2.
	// For a valid (non-truncated) file, file size must be at least
	// pos1 + 512 + 2*MACPAINT_HEIGHT. But we want to tolerate truncated
	// files as well.
	if(c->infile->len < imgstart + 4) {
		de_strlcpy(vi->msg, "file too small", sizeof(vi->msg));
		vi->retval = 0;
		goto done;
	}

	// Look for a boundary between all-0 bytes, and not-all-0 bytes.
	if(dbuf_is_all_zeroes(c->infile, imgstart-16, 16) &&
		!dbuf_is_all_zeroes(c->infile, imgstart, 8))
	{
		v++;
	}

	pos = imgstart;

	while(pos < c->infile->len) {
		if(ypos>=MACPAINT_HEIGHT) {
			break;
		}

		b = de_getbyte_p(&pos);

		if(b<=127) {
			count = 1+(i64)b;
			pos += count;
			xpos_b += count;
			if(xpos_b==MACPAINT_WIDTH_BYTES) {
				xpos_b = 0;
				ypos++;
			}
			else if(xpos_b > MACPAINT_WIDTH_BYTES) {
				de_strlcpy(vi->msg, "literal too long", sizeof(vi->msg));
				vi->retval = v+0;
				goto done;
			}
		}
		else if(b>=129) {
			count = 257 - (i64)b;
			pos++;
			xpos_b += count;
			if(xpos_b == MACPAINT_WIDTH_BYTES) {
				xpos_b = 0;
				ypos++;
			}
			else if(xpos_b > MACPAINT_WIDTH_BYTES) {
				de_strlcpy(vi->msg, "run too long", sizeof(vi->msg));
				vi->retval = v+0;
				goto done;
			}
		}
	}

	if(xpos_b==0 && ypos==MACPAINT_HEIGHT) {
		de_strlcpy(vi->msg, "decodes okay", sizeof(vi->msg));
		vi->retval = v+2;
		goto done;
	}

	de_snprintf(vi->msg, sizeof(vi->msg), "premature end of file (x=%d, y=%d)",
		(int)(xpos_b*8), (int)ypos);
	vi->retval = v+1;

done:
	de_dbg(c, "image at offset %d: %s", (int)(vi->pos+512), vi->msg);
	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %r12
leaq 0x200(%r12), %r15
leaq 0x93af5(%rip), %rsi     # 0x1228c5
xorl %ebp, %ebp
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
leaq 0x204(%r12), %rax
cmpq %rax, 0x18(%rdi)
jge 0x8ee13
leaq 0xc(%r14), %rdi
leaq 0x93ae1(%rip), %rsi     # 0x1228e5
pushq $0x50
popq %rdx
callq 0xde492
xorl %eax, %eax
jmp 0x8ef0b
addq $0x1f0, %r12            # imm = 0x1F0
pushq $0x10
popq %rdx
movq %r12, %rsi
callq 0xe66d2
testl %eax, %eax
je 0x8ee40
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rdx
movq %r15, %rsi
callq 0xe66d2
xorl %ebp, %ebp
testl %eax, %eax
sete %bpl
movl %ebp, 0xc(%rsp)
leaq 0x10(%rsp), %r13
movq %r15, (%r13)
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %r15
jge 0x8eec2
cmpq $0x2cf, %r12            # imm = 0x2CF
jg 0x8eec2
movq %r13, %rsi
callq 0xe3f2a
movzbl %al, %ecx
testb %al, %al
jns 0x8ee9c
negb %al
movq 0x10(%rsp), %r15
jo 0x8ee52
incq %r15
movq %r15, 0x10(%rsp)
subq %rcx, %rbp
addq $0x101, %rbp            # imm = 0x101
cmpq $0x48, %rbp
je 0x8eebd
jle 0x8ee52
jmp 0x8ef56
leaq 0x1(%rcx), %r15
addq 0x10(%rsp), %r15
movq %r15, 0x10(%rsp)
addq %rcx, %rbp
incq %rbp
cmpq $0x48, %rbp
je 0x8eebd
jle 0x8ee52
jmp 0x8ef49
incq %r12
jmp 0x8ee50
movq %r12, %rax
xorq $0x2d0, %rax            # imm = 0x2D0
leaq 0xc(%r14), %rdi
orq %rbp, %rax
jne 0x8eeec
leaq 0x93a37(%rip), %rsi     # 0x122912
pushq $0x50
popq %rdx
callq 0xde492
movl 0xc(%rsp), %eax
orl $0x2, %eax
jmp 0x8ef0b
shll $0x3, %ebp
leaq 0x93a29(%rip), %rdx     # 0x12291f
pushq $0x50
popq %rsi
movl %ebp, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0xde40c
movl 0xc(%rsp), %eax
incl %eax
movl %eax, 0x8(%r14)
movl $0x200, %edx            # imm = 0x200
addl (%r14), %edx
addq $0xc, %r14
leaq 0x93a20(%rip), %rsi     # 0x122942
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc(%r14), %rdi
leaq 0x939a0(%rip), %rsi     # 0x1228f4
jmp 0x8ef61
leaq 0xc(%r14), %rdi
leaq 0x939a4(%rip), %rsi     # 0x122905
pushq $0x50
popq %rdx
callq 0xde492
movl 0xc(%rsp), %eax
jmp 0x8ef0b
nop