// File: /workspace/github_c_makefile_star2_O3/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 0x9b897(%rip), %rsi     # 0x12fb4d
xorl %ebp, %ebp
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
leaq 0x204(%r12), %rax
cmpq %rax, 0x18(%rdi)
jge 0x942fd
leaq 0xc(%r14), %rdi
leaq 0x9b881(%rip), %rsi     # 0x12fb6d
movl $0x50, %edx
callq 0xe8219
xorl %ebp, %ebp
jmp 0x9441b
addq $0x1f0, %r12            # imm = 0x1F0
movl $0x10, %edx
movq %r12, %rsi
callq 0xf1856
testl %eax, %eax
je 0x9432e
movq 0x18(%rbx), %rdi
movl $0x8, %edx
movq %r15, %rsi
callq 0xf1856
xorl %ebp, %ebp
testl %eax, %eax
sete %bpl
movq %r15, (%rsp)
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %r15
jge 0x943c8
xorl %r15d, %r15d
movq %rsp, %r13
xorl %r12d, %r12d
movq %r13, %rsi
callq 0xeeb4b
testb %al, %al
js 0x94377
movzbl %al, %eax
leaq 0x1(%rax), %rcx
addq (%rsp), %rcx
movq %rcx, (%rsp)
addq %rax, %r12
incq %r12
cmpq $0x48, %r12
je 0x9439b
jle 0x943a1
jmp 0x9445b
movl %eax, %ecx
negb %cl
movq (%rsp), %rcx
jo 0x943a1
movzbl %al, %eax
incq %rcx
movq %rcx, (%rsp)
subq %rax, %r12
addq $0x101, %r12            # imm = 0x101
cmpq $0x48, %r12
jne 0x943b6
incq %r15
xorl %r12d, %r12d
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rcx
jge 0x943d0
cmpq $0x2d0, %r15            # imm = 0x2D0
jl 0x94349
jmp 0x943d0
jle 0x943a1
leaq 0xc(%r14), %rdi
leaq 0x9b7ca(%rip), %rsi     # 0x12fb8d
jmp 0x94466
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x943f8
movq %r15, %rax
xorq $0x2d0, %rax            # imm = 0x2D0
orq %r12, %rax
jne 0x943f8
leaq 0xc(%r14), %rdi
leaq 0x9b7b1(%rip), %rsi     # 0x12fb9a
movl $0x50, %edx
callq 0xe8219
orl $0x2, %ebp
jmp 0x9441b
leaq 0xc(%r14), %rdi
shll $0x3, %r12d
leaq 0x9b7a0(%rip), %rdx     # 0x12fba7
movl $0x50, %esi
movl %r12d, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0xe8193
incl %ebp
movl %ebp, 0x8(%r14)
movl $0x200, %edx            # imm = 0x200
addl (%r14), %edx
addq $0xc, %r14
leaq 0x9b798(%rip), %rsi     # 0x12fbca
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc(%r14), %rdi
leaq 0x9b716(%rip), %rsi     # 0x12fb7c
movl $0x50, %edx
callq 0xe8219
jmp 0x9441b
nop