// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/jpeg.c
// Function: do_xmp_extension_segment
static void do_xmp_extension_segment(deark *c, lctx *d,
	i64 pos1, i64 data_size)
{
	i64 thisseg_full_extxmp_len;
	i64 segment_offset;
	u8 thisseg_digest_raw[32];
	de_ucstring *digest_str = NULL;
	i64 pos = pos1;
	i64 dlen;
	int is_first_segment = 0;

	de_dbg(c, "extended XMP segment, dpos=%d, dlen=%d", (int)pos1, (int)(data_size));
	de_dbg_indent(c, 1);
	if(d->extxmp_error_flag) goto done;

	de_read(thisseg_digest_raw, pos, 32);
	pos += 32;
	digest_str = ucstring_create(c);
	ucstring_append_bytes(digest_str, thisseg_digest_raw, 32, 0, DE_ENCODING_ASCII);
	de_dbg(c, "digest: \"%s\"", ucstring_getpsz(digest_str));

	if(d->extxmp_found && de_memcmp(thisseg_digest_raw, d->extxmp_digest, 32)) {
		// We only care about the extended XMP segments whose digest matches that
		// indicated in the main XMP segment. Unfortunately, we don't know what that
		// is, because we don't parse XMP. We'll just hope that the first extended
		// XMP segment has the correct digest.
		if(!d->extxmp_warned_flag) {
			de_warn(c, "Multiple extended XMP blocks found. All but the first will be ignored.");
			d->extxmp_warned_flag = 1;
		}
		goto done;
	}

	if(!d->extxmp_found) {
		is_first_segment = 1;
		d->extxmp_found = 1;
		de_memcpy(d->extxmp_digest, thisseg_digest_raw, 32);
	}

	thisseg_full_extxmp_len = de_getu32be_p(&pos);
	if(is_first_segment) {
		d->extxmp_total_len = thisseg_full_extxmp_len;
	}
	de_dbg(c, "full ext. XMP length: %d", (int)thisseg_full_extxmp_len);
	if(thisseg_full_extxmp_len != d->extxmp_total_len) {
		de_warn(c, "Inconsistent extended XMP block lengths");
		d->extxmp_error_flag = 1;
		goto done;
	}

	if(d->extxmp_total_len > 10000000) {
		de_warn(c, "Extended XMP block too large");
		d->extxmp_error_flag = 1;
		goto done;
	}

	segment_offset = de_getu32be_p(&pos);
	de_dbg(c, "offset of this segment: %d", (int)segment_offset);

	dlen = data_size - (pos-pos1);
	de_dbg(c, "[%d bytes of ext. XMP data at %d]", (int)dlen, (int)pos);

	if(segment_offset + dlen > d->extxmp_total_len) {
		de_warn(c, "Extended XMP segment too long");
		d->extxmp_error_flag = 1;
		goto done;
	}

	if(!d->extxmp_membuf) {
		d->extxmp_membuf = dbuf_create_membuf(c, d->extxmp_total_len, 0x1);
	}
	dbuf_copy_at(c->infile, pos, dlen, d->extxmp_membuf, segment_offset);

done:
	de_dbg_indent(c, -1);
	ucstring_destroy(digest_str);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x6c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movl %eax, %ecx
leaq 0xf75d8(%rip), %rsi     # 0x1b6e21
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
cmpl $0x0, 0x60(%rax)
je 0xbf86d
jmp 0xbfb01
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x50(%rbp), %rsi
movq -0x60(%rbp), %rdx
movl $0x20, %ecx
callq 0x164d10
movq -0x60(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movl $0x20, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x171fa0
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x58(%rbp), %rdi
callq 0x172500
movq -0x78(%rbp), %rdi
movq %rax, %rdx
leaq 0xf756f(%rip), %rsi     # 0x1b6e48
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpl $0x0, 0x58(%rax)
je 0xbf931
leaq -0x50(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x70, %rsi
movl $0x20, %edx
callq 0x19170
cmpl $0x0, %eax
je 0xbf931
movq -0x10(%rbp), %rax
cmpl $0x0, 0x5c(%rax)
jne 0xbf92c
movq -0x8(%rbp), %rdi
leaq 0xf753b(%rip), %rsi     # 0x1b6e55
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
movl $0x1, 0x5c(%rax)
jmp 0xbfb01
movq -0x10(%rbp), %rax
cmpl $0x0, 0x58(%rax)
jne 0xbf977
movl $0x1, -0x6c(%rbp)
movq -0x10(%rbp), %rax
movl $0x1, 0x58(%rax)
movq -0x10(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rcx, 0x70(%rax)
movq -0x48(%rbp), %rcx
movq %rcx, 0x78(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x80(%rax)
movq -0x38(%rbp), %rcx
movq %rcx, 0x88(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1654b0
movq %rax, -0x28(%rbp)
cmpl $0x0, -0x6c(%rbp)
je 0xbf9a1
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0xf74ea(%rip), %rsi     # 0x1b6e9c
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x90(%rcx), %rax
je 0xbf9ec
movq -0x8(%rbp), %rdi
leaq 0xf74e0(%rip), %rsi     # 0x1b6eb5
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
movl $0x1, 0x60(%rax)
jmp 0xbfb01
movq -0x10(%rbp), %rax
cmpq $0x989680, 0x90(%rax) # imm = 0x989680
jle 0xbfa1f
movq -0x8(%rbp), %rdi
leaq 0xf74d5(%rip), %rsi     # 0x1b6edd
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
movl $0x1, 0x60(%rax)
jmp 0xbfb01
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1654b0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0xf74b5(%rip), %rsi     # 0x1b6efa
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
movq -0x60(%rbp), %rcx
subq -0x18(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rax
movl %eax, %edx
movq -0x60(%rbp), %rax
movl %eax, %ecx
leaq 0xf749f(%rip), %rsi     # 0x1b6f15
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq -0x68(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x90(%rcx), %rax
jle 0xbfab1
movq -0x8(%rbp), %rdi
leaq 0xf749a(%rip), %rsi     # 0x1b6f37
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
movl $0x1, 0x60(%rax)
jmp 0xbfb01
movq -0x10(%rbp), %rax
cmpq $0x0, 0x68(%rax)
jne 0xbfae0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rcx
movq -0x30(%rbp), %r8
callq 0x166410
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x58(%rbp), %rdi
callq 0x171750
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)