// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %r12
leaq 0x200(%r12), %r15
leaq 0x9abfb(%rip), %rsi     # 0x12f869
xorl %ebp, %ebp
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
leaq 0x204(%r12), %rax
cmpq %rax, 0x18(%rdi)
jge 0x94cb5
leaq 0xc(%r14), %rdi
leaq 0x9abe5(%rip), %rsi     # 0x12f889
movl $0x50, %edx
callq 0xe8ded
xorl %ebp, %ebp
jmp 0x94dd6
addq $0x1f0, %r12            # imm = 0x1F0
movl $0x10, %edx
movq %r12, %rsi
callq 0xf1efb
testl %eax, %eax
je 0x94ce6
movq 0x18(%rbx), %rdi
movl $0x8, %edx
movq %r15, %rsi
callq 0xf1efb
xorl %ebp, %ebp
testl %eax, %eax
sete %bpl
movq %r15, (%rsp)
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %r15
jge 0x94d72
xorl %r15d, %r15d
movq %rsp, %r13
xorl %r12d, %r12d
movq %r13, %rsi
callq 0xef14d
testb %al, %al
js 0x94d27
movzbl %al, %eax
leaq 0x1(%rax), %rcx
addq %rcx, (%rsp)
addq %rax, %r12
incq %r12
cmpq $0x48, %r12
je 0x94d44
jle 0x94d4a
jmp 0x94dc1
movl %eax, %ecx
negb %cl
jo 0x94d4a
incq (%rsp)
movzbl %al, %eax
subq %rax, %r12
addq $0x101, %r12            # imm = 0x101
cmpq $0x48, %r12
jne 0x94d63
incq %r15
xorl %r12d, %r12d
movq (%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0x94d78
cmpq $0x2d0, %r15            # imm = 0x2D0
jl 0x94cfd
jmp 0x94d78
jle 0x94d4a
leaq 0xc(%r14), %rdi
leaq 0x9ab39(%rip), %rsi     # 0x12f8a9
jmp 0x94dcc
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r15, %rax
xorq $0x2d0, %rax            # imm = 0x2D0
leaq 0xc(%r14), %rdi
orq %r12, %rax
jne 0x94da0
leaq 0x9ab25(%rip), %rsi     # 0x12f8b6
movl $0x50, %edx
callq 0xe8ded
orl $0x2, %ebp
jmp 0x94dd6
shll $0x3, %r12d
leaq 0x9ab18(%rip), %rdx     # 0x12f8c3
movl $0x50, %esi
movl %r12d, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0xe8d67
incl %ebp
jmp 0x94dd6
leaq 0xc(%r14), %rdi
leaq 0x9aacc(%rip), %rsi     # 0x12f898
movl $0x50, %edx
callq 0xe8ded
movl %ebp, 0x8(%r14)
movl $0x200, %edx            # imm = 0x200
addl (%r14), %edx
addq $0xc, %r14
leaq 0x9aaf9(%rip), %rsi     # 0x12f8e6
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop