// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/tiff.c
// Function: handler_usercomment
static void handler_usercomment(deark *c, lctx *d, const struct taginfo *tg, const struct tagnuminfo *tni)
{
	static u8 charcode[8];
	de_ucstring *s = NULL;
	de_encoding enc = DE_ENCODING_UNKNOWN;
	i64 bytes_per_char = 1;

	if(tg->datatype != DATATYPE_UNDEF) goto done;
	if(tg->total_size < 8) goto done;

	de_read(charcode, tg->val_offset, 8);

	if(!de_memcmp(charcode, "ASCII\0\0\0", 8)) {
		enc = DE_ENCODING_ASCII;
	}
	else if(!de_memcmp(charcode, "UNICODE\0", 8)) {
		enc = d->is_le ? DE_ENCODING_UTF16LE : DE_ENCODING_UTF16BE;
		bytes_per_char = 2;
	}

	if(enc == DE_ENCODING_UNKNOWN) goto done;

	s = ucstring_create(c);
	dbuf_read_to_ucstring_n(c->infile, tg->val_offset + 8, tg->total_size - 8,
		DE_TIFF_MAX_CHARS_TO_PRINT*bytes_per_char, s, 0, enc);

	// Should we truncate at NUL, or not? The Exif spec says "NULL termination
	// is not necessary", but it doesn't say whether it is *allowed*.
	// In practice, if we don't do this, we sometimes end up printing a lot of
	// garbage.
	ucstring_truncate_at_NUL(s);

	// FIXME: This is not quite right, though it's not important. We really
	// need to read the entire string, not just the first
	// DE_TIFF_MAX_CHARS_TO_PRINT bytes, in order to determine which characters
	// are trailing spaces.
	ucstring_strip_trailing_spaces(s);

	de_dbg(c, "%s: \"%s\"", tni->tagname, ucstring_getpsz(s));

done:
	ucstring_destroy(s);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x1, -0x38(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x7, 0xc(%rax)
je 0x13203e
jmp 0x132173
movq -0x18(%rbp), %rax
cmpq $0x8, 0x30(%rax)
jge 0x13204e
jmp 0x132173
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0xf3bc3(%rip), %rsi     # 0x225c28
movl $0x8, %ecx
callq 0x164d10
movq 0xf3bb2(%rip), %rax     # 0x225c28
movabsq $0x4949435341, %rcx     # imm = 0x4949435341
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x132097
movl $0x1, -0x2c(%rbp)
jmp 0x1320d9
movq 0xf3b8a(%rip), %rax     # 0x225c28
movabsq $0x45444f43494e55, %rcx # imm = 0x45444F43494E55
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1320d7
movq -0x10(%rbp), %rax
movl (%rax), %edx
movl $0x4, %eax
movl $0x3, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x2c(%rbp)
movq $0x2, -0x38(%rbp)
jmp 0x1320d9
cmpl $0x0, -0x2c(%rbp)
jne 0x1320e4
jmp 0x132173
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
addq $0x8, %rsi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdx
subq $0x8, %rdx
imulq $0x1f4, -0x38(%rbp), %rcx # imm = 0x1F4
movq -0x28(%rbp), %r8
movl -0x2c(%rbp), %eax
xorl %r9d, %r9d
movl %eax, (%rsp)
callq 0x166a40
movq -0x28(%rbp), %rdi
callq 0x1712b0
movq -0x28(%rbp), %rdi
callq 0x171360
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rdi
callq 0x172500
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq %rax, %rcx
leaq 0x85a9a(%rip), %rsi     # 0x1b7c06
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rdi
callq 0x171750
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)