// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/j2c.c
// Function: do_read_scan_data
static int do_read_scan_data(deark *c, lctx *d, struct page_ctx *pg,
	i64 pos1, i64 *bytes_consumed)
{
	i64 pos = pos1;
	u8 b0, b1;

	*bytes_consumed = c->infile->len - pos1; // default
	de_dbg(c, "scan data at %d", (int)pos1);

	de_dbg_indent(c, 1);

	if(pg->j2c_sot_length>0) {
		// The previous SOT segment may have told us where this scan data ends.
		*bytes_consumed = pg->j2c_sot_pos + pg->j2c_sot_length - pos1;
		if(*bytes_consumed < 0) *bytes_consumed = 0;
		de_dbg(c, "[%"I64_FMT" bytes of scan data at %"I64_FMT"]",
			*bytes_consumed, pos1);
		pg->j2c_sot_pos = 0;
		pg->j2c_sot_length = 0;
		goto done;
	}

	while(1) {
		if(pos >= c->infile->len) goto done;
		b0 = de_getbyte_p(&pos);
		if(b0==0xff) {
			b1 = de_getbyte_p(&pos);
			if(b1==0x00) {
				; // an escaped 0xff
			}
			else if(b1<0x90) {
				// In J2C, 0xff bytes are not escaped if they're followed by a
				// a byte less than 0x90.
				;
			}
			else if(b1==0xff) { // a "fill byte" (TODO: Does J2C have these?)
				pos--;
			}
			else {
				// A marker that is not part of the scan.
				// Subtract the bytes consumed by it, and stop.
				pos -= 2;
				*bytes_consumed = pos - pos1;
				de_dbg(c, "end of scan data found at %d (len=%d)", (int)pos, (int)*bytes_consumed);
				break;
			}
		}
	}

done:
	de_dbg_indent(c, -1);
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
subq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq 0xef90d(%rip), %rsi     # 0x1b7339
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jle 0xc7ab6
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
addq 0x10(%rax), %rcx
subq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, (%rax)
jge 0xc7a7c
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rdx
movq -0x20(%rbp), %rcx
leaq 0xf0c98(%rip), %rsi     # 0x1b872a
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0xc7b82
jmp 0xc7ab8
movq -0x30(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0xc7acf
jmp 0xc7b82
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, -0x31(%rbp)
movzbl -0x31(%rbp), %eax
cmpl $0xff, %eax
jne 0xc7b7b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, -0x32(%rbp)
movzbl -0x32(%rbp), %eax
cmpl $0x0, %eax
jne 0xc7b11
jmp 0xc7b79
movzbl -0x32(%rbp), %eax
cmpl $0x90, %eax
jge 0xc7b1e
jmp 0xc7b77
movzbl -0x32(%rbp), %eax
cmpl $0xff, %eax
jne 0xc7b37
movq -0x30(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xc7b75
movq -0x30(%rbp), %rax
subq $0x2, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rcx
subq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl %eax, %ecx
leaq 0xef7dd(%rip), %rsi     # 0x1b7349
movb $0x0, %al
callq 0x15b0d0
jmp 0xc7b80
jmp 0xc7b77
jmp 0xc7b79
jmp 0xc7b7b
jmp 0xc7ab8
jmp 0xc7b82
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl $0x1, %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)