// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
addq $0x200, %rax            # imm = 0x200
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0xe258b(%rip), %rsi     # 0x1bb618
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
addq $0x4, %rcx
cmpq %rcx, %rax
jge 0xd90e4
movq -0x18(%rbp), %rdi
addq $0xc, %rdi
leaq 0xe256e(%rip), %rsi     # 0x1bb638
movl $0x50, %edx
callq 0x15aa30
movq -0x18(%rbp), %rax
movl $0x0, 0x8(%rax)
jmp 0xd92e9
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
subq $0x10, %rsi
movl $0x10, %edx
callq 0x169d30
cmpl $0x0, %eax
je 0xd9127
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movl $0x8, %edx
callq 0x169d30
cmpl $0x0, %eax
jne 0xd9127
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jge 0xd9277
cmpq $0x2d0, -0x48(%rbp)     # imm = 0x2D0
jl 0xd9154
jmp 0xd9277
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, -0x19(%rbp)
movzbl -0x19(%rbp), %eax
cmpl $0x7f, %eax
jg 0xd91eb
movzbl -0x19(%rbp), %eax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x48, -0x40(%rbp)
jne 0xd91b2
movq $0x0, -0x40(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0xd91e6
cmpq $0x48, -0x40(%rbp)
jle 0xd91e4
movq -0x18(%rbp), %rdi
addq $0xc, %rdi
leaq 0xe247f(%rip), %rsi     # 0x1bb647
movl $0x50, %edx
callq 0x15aa30
movl -0x4c(%rbp), %ecx
addl $0x0, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
jmp 0xd92e9
jmp 0xd91e6
jmp 0xd9272
movzbl -0x19(%rbp), %eax
cmpl $0x81, %eax
jl 0xd9270
movzbl -0x19(%rbp), %eax
movl %eax, %ecx
movl $0x101, %eax            # imm = 0x101
subq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x48, -0x40(%rbp)
jne 0xd923d
movq $0x0, -0x40(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0xd926e
cmpq $0x48, -0x40(%rbp)
jle 0xd926c
movq -0x18(%rbp), %rdi
addq $0xc, %rdi
leaq 0xe2405(%rip), %rsi     # 0x1bb658
movl $0x50, %edx
callq 0x15aa30
movl -0x4c(%rbp), %ecx
addl $0x0, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
jmp 0xd92e9
jmp 0xd926e
jmp 0xd9270
jmp 0xd9272
jmp 0xd912f
cmpq $0x0, -0x40(%rbp)
jne 0xd92b0
cmpq $0x2d0, -0x48(%rbp)     # imm = 0x2D0
jne 0xd92b0
movq -0x18(%rbp), %rdi
addq $0xc, %rdi
leaq 0xe23ce(%rip), %rsi     # 0x1bb665
movl $0x50, %edx
callq 0x15aa30
movl -0x4c(%rbp), %ecx
addl $0x2, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
jmp 0xd92e9
movq -0x18(%rbp), %rdi
addq $0xc, %rdi
movq -0x40(%rbp), %rax
shlq $0x3, %rax
movl %eax, %ecx
movq -0x48(%rbp), %rax
movl %eax, %r8d
movl $0x50, %esi
leaq 0xe239d(%rip), %rdx     # 0x1bb672
movb $0x0, %al
callq 0x15a980
movl -0x4c(%rbp), %ecx
addl $0x1, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rax
addq $0x200, %rax            # imm = 0x200
movl %eax, %edx
movq -0x18(%rbp), %rcx
addq $0xc, %rcx
leaq 0xe238a(%rip), %rsi     # 0x1bb695
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)