// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/bmp.c
// Function: de_scan_jpeg_data
static void de_scan_jpeg_data(deark *c, dbuf *f, i64 pos1, i64 len,
	struct de_scan_jpeg_data_ctx *sd)
{
	i64 pos = pos1;
	i64 endpos = pos1+len;

	while(1) {
		i64 seg_startpos;
		i64 seg_len;
		u8 b1;

		if(pos+4 > endpos) goto done;
		seg_startpos = pos;
		if(dbuf_getbyte_p(f, &pos) != 0xff) {
			sd->error_flag = 1;
			goto done;
		}

		b1 = dbuf_getbyte_p(f, &pos);
		if(b1==0 || b1==0xff) {
			sd->error_flag = 1;
			goto done;
		}

		if(seg_startpos==pos1) { // Expecting SOI
			if(b1!=0xd8) goto done;
			sd->soi_pos = seg_startpos;
			continue;
		}

		if(b1==0xd9) { // EOI
			goto done;
		}

		// (Not expecting any other bare markers)

		seg_len = 2 + dbuf_getu16be_p(f, &pos);

		switch(b1) {
		case 0xc4: // DHT
			sd->found_dht = 1;
			break;
		case 0xda: // SOS
			sd->found_sos = 1;
			sd->sos_pos = seg_startpos;
			goto done; // we don't scan past SOS
		case 0xdb: // DQT
			sd->found_dqt = 1;
			break;
		case 0xe0: // APP0
			if(seg_len>=11 && !sd->found_jfif) {
				if(!dbuf_memcmp(f, seg_startpos+4, "JFIF\0", 5)) {
					sd->found_jfif = 1;
					sd->jfif_pos = seg_startpos;
				}
			}
			break;
		case 0xe1: // APP1
			if(seg_len>=11 && !sd->found_pic) {
				if(!dbuf_memcmp(f, seg_startpos+4, "PIC\0", 4)) {
					sd->found_pic = 1;
					sd->pic_pos = seg_startpos;
					sd->pic_len = seg_len;
				}
			}
			break;
		case 0xe2: // APP2
			if(seg_len>8 && !sd->found_8bim) {
				if(!dbuf_memcmp(f, seg_startpos+4, "8BIM", 4)) {
					sd->found_8bim = 1;
					sd->_8bim_pos = seg_startpos;
					sd->_8bim_len = seg_len;
				}
			}
			break;
		case 0xc0: case 0xc1: case 0xc2: case 0xc3:
		case 0xc5: case 0xc6: case 0xc7:
		case 0xc9: case 0xca: case 0xcb:
		case 0xcd: case 0xce: case 0xcf:
			sd->found_sof = 1;
			sd->sof_pos = seg_startpos;
			break;
		}

		pos = seg_startpos + seg_len;
	}

done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
addq $0x4, %rax
cmpq -0x38(%rbp), %rax
jle 0x46a73
jmp 0x46c9e
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
cmpl $0xff, %eax
je 0x46a9e
movq -0x28(%rbp), %rax
movb $0x1, (%rax)
jmp 0x46c9e
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, -0x49(%rbp)
movzbl -0x49(%rbp), %eax
cmpl $0x0, %eax
je 0x46ac2
movzbl -0x49(%rbp), %eax
cmpl $0xff, %eax
jne 0x46ace
movq -0x28(%rbp), %rax
movb $0x1, (%rax)
jmp 0x46c9e
movq -0x40(%rbp), %rax
cmpq -0x18(%rbp), %rax
jne 0x46af9
movzbl -0x49(%rbp), %eax
cmpl $0xd8, %eax
je 0x46ae8
jmp 0x46c9e
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x46a60
movzbl -0x49(%rbp), %eax
cmpl $0xd9, %eax
jne 0x46b09
jmp 0x46c9e
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165210
addq $0x2, %rax
movq %rax, -0x48(%rbp)
movzbl -0x49(%rbp), %eax
addl $0xffffff40, %eax       # imm = 0xFFFFFF40
movl %eax, %ecx
movq %rcx, -0x58(%rbp)
subl $0x22, %eax
ja 0x46c8d
movq -0x58(%rbp), %rax
leaq 0x15ca13(%rip), %rcx    # 0x1a3554
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x28(%rbp), %rax
movb $0x1, 0x2(%rax)
jmp 0x46c8d
movq -0x28(%rbp), %rax
movb $0x1, 0x1(%rax)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x46c9e
movq -0x28(%rbp), %rax
movb $0x1, 0x3(%rax)
jmp 0x46c8d
cmpq $0xb, -0x48(%rbp)
jl 0x46bc6
movq -0x28(%rbp), %rax
cmpb $0x0, 0x5(%rax)
jne 0x46bc6
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
addq $0x4, %rsi
leaq 0x15ca60(%rip), %rdx    # 0x1a3601
movl $0x5, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0x46bc4
movq -0x28(%rbp), %rax
movb $0x1, 0x5(%rax)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
jmp 0x46bc6
jmp 0x46c8d
cmpq $0xb, -0x48(%rbp)
jl 0x46c20
movq -0x28(%rbp), %rax
cmpb $0x0, 0x6(%rax)
jne 0x46c20
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
addq $0x4, %rsi
leaq 0x15ca18(%rip), %rdx    # 0x1a3607
movl $0x4, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0x46c1e
movq -0x28(%rbp), %rax
movb $0x1, 0x6(%rax)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x48(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0x46c20
jmp 0x46c8d
cmpq $0x8, -0x48(%rbp)
jle 0x46c77
movq -0x28(%rbp), %rax
cmpb $0x0, 0x7(%rax)
jne 0x46c77
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
addq $0x4, %rsi
leaq 0x18f30f(%rip), %rdx    # 0x1d5f55
movl $0x4, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0x46c75
movq -0x28(%rbp), %rax
movb $0x1, 0x7(%rax)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x48(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x40(%rax)
jmp 0x46c77
jmp 0x46c8d
movq -0x28(%rbp), %rax
movb $0x1, 0x4(%rax)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x40(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x46a60
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)