// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/iccprofile.c
// Function: typedec_desc
static void typedec_desc(deark *c, struct typedec_params *p)
{
	de_ucstring *s = NULL;
	i64 invdesclen, uloclen;
	i64 langcode;
	i64 lstrstartpos;
	i64 bytes_to_read;
	de_encoding encoding;
	i64 pos = p->pos1;

	if(p->len<12) goto done;

	pos += 8;

	// ASCII invariant description
	invdesclen = de_getu32be(pos); // invariant desc. len, including NUL byte
	pos += 4;
	s = ucstring_create(c);
	dbuf_read_to_ucstring_n(c->infile, pos, invdesclen, DE_DBG_MAX_STRLEN,
		s, 0, DE_ENCODING_ASCII);
	ucstring_truncate_at_NUL(s);
	de_dbg(c, "invariant desc.: \"%s\"", ucstring_getpsz(s));
	pos += invdesclen;
	if(pos >= p->pos1+p->len) goto done;

	// Unicode localizable description
	ucstring_empty(s);

	langcode = de_getu32be(pos);
	pos += 4;

	uloclen = de_getu32be(pos);
	pos += 4;

	if(uloclen>0) {
		// TODO: How to interpret the language code?
		de_dbg(c, "language code: %d", (int)langcode);
	}

	lstrstartpos = pos;
	bytes_to_read = uloclen*2;

	encoding = DE_ENCODING_UTF16BE;
	if(uloclen>=1) {
		i32 firstchar;
		// Check for a BOM. The spec doesn't say much about the format of
		// Unicode text in 'desc' tags. It does say that "All profile data must
		// be encoded as big-endian", so maybe that means UTF-16LE is not
		// allowed. In practice, some strings begin with a BOM.
		firstchar = (i32)de_getu16be(lstrstartpos);
		if(firstchar==0xfeff) { // UTF-16BE BOM
			lstrstartpos += 2;
			bytes_to_read -= 2;
		}
		else if(firstchar==0xffef) { // UTF-16LE BOM
			lstrstartpos += 2;
			bytes_to_read -= 2;
			encoding = DE_ENCODING_UTF16LE;
		}
	}

	dbuf_read_to_ucstring_n(c->infile, lstrstartpos, bytes_to_read, DE_DBG_MAX_STRLEN*2,
		s, 0, encoding);
	ucstring_truncate_at_NUL(s);
	if(s->len>0) {
		de_dbg(c, "localizable desc.: \"%s\"", ucstring_getpsz(s));
	}
	pos += uloclen*2;
	if(pos >= p->pos1+p->len) goto done;

	// Macintosh localizable description
	// (not implemented)

done:
	ucstring_destroy(s);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpq $0xc, 0x18(%rsi)
jge 0x6ecb5
xorl %ebx, %ebx
jmp 0x6ee08
movq %rsi, %r15
movq %rdi, %r14
movq 0x10(%rsi), %rbp
leaq 0x8(%rbp), %rsi
movq 0x18(%rdi), %rdi
callq 0xe404f
movq %rax, %r12
leaq 0xc(%rbp), %r13
movq %r14, %rdi
callq 0xeac74
movq %rax, %rbx
movq 0x18(%r14), %rdi
movl $0x1, (%rsp)
movl $0x1f4, %ecx            # imm = 0x1F4
movq %r13, %rsi
movq %r12, %rdx
movq %rax, %r8
xorl %r9d, %r9d
callq 0xe4ced
movq %rbx, %rdi
callq 0xeacc0
movq %rbx, %rdi
callq 0xeb7e4
leaq 0xab006(%rip), %rsi     # 0x119d1c
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
addq %rbp, %r12
addq $0xc, %r12
movq 0x18(%r15), %rax
addq 0x10(%r15), %rax
cmpq %rax, %r12
jge 0x6ee08
movq %rbx, %rdi
callq 0xeac85
movq 0x18(%r14), %rdi
movq %r12, %rsi
callq 0xe404f
movq %rax, %rbp
leaq 0x4(%r12), %rsi
movq 0x18(%r14), %rdi
callq 0xe404f
movq %rax, %r15
leaq 0x8(%r12), %r13
testq %rax, %rax
jle 0x6edaa
leaq 0xaafbe(%rip), %rsi     # 0x119d32
movq %r14, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
addq %r15, %r15
movq 0x18(%r14), %rdi
movq %r13, %rsi
callq 0xe3f46
cmpl $0xffef, %eax           # imm = 0xFFEF
je 0x6edb2
pushq $0x4
popq %rcx
cmpl $0xfeff, %eax           # imm = 0xFEFF
jne 0x6edc0
addq $0xa, %r12
addq $-0x2, %r15
jmp 0x6edbd
addq %r15, %r15
pushq $0x4
popq %rcx
jmp 0x6edc0
addq $0xa, %r12
addq $-0x2, %r15
pushq $0x3
popq %rcx
movq %r12, %r13
movq 0x18(%r14), %rdi
movl %ecx, (%rsp)
movl $0x3e8, %ecx            # imm = 0x3E8
movq %r13, %rsi
movq %r15, %rdx
movq %rbx, %r8
xorl %r9d, %r9d
callq 0xe4ced
movq %rbx, %rdi
callq 0xeacc0
cmpq $0x0, 0x10(%rbx)
jle 0x6ee08
movq %rbx, %rdi
callq 0xeb7e4
leaq 0xaaf49(%rip), %rsi     # 0x119d44
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xeaf3c