// File: /workspace/github_c_makefile_star2_O1/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 0x72f6d
xorl %ebx, %ebx
jmp 0x730cf
movq %rsi, %r12
movq %rdi, %r14
movq 0x10(%rsi), %rbp
leaq 0x8(%rbp), %rsi
movq 0x18(%rdi), %rdi
callq 0xef32a
movq %rax, %r15
leaq 0xc(%rbp), %r13
movq %r14, %rdi
callq 0xf6870
movq %rax, %rbx
movq 0x18(%r14), %rdi
movl $0x1, (%rsp)
movl $0x1f4, %ecx            # imm = 0x1F4
movq %r13, %rsi
movq %r15, %rdx
movq %rax, %r8
xorl %r9d, %r9d
callq 0xf0203
movq %rbx, %rdi
callq 0xf68c1
movq %rbx, %rdi
callq 0xf74bd
leaq 0xb3d1e(%rip), %rsi     # 0x126cec
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
addq %rbp, %r15
addq $0xc, %r15
movq 0x18(%r12), %rax
addq 0x10(%r12), %rax
cmpq %rax, %r15
jge 0x730cf
movq %rbx, %rdi
callq 0xf6883
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xef32a
movq %rax, %r12
leaq 0x4(%r15), %rsi
movq 0x18(%r14), %rdi
callq 0xef32a
movq %rax, %rbp
testq %rax, %rax
jle 0x73035
leaq 0xb3cda(%rip), %rsi     # 0x126d02
movq %r14, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
leaq 0x8(%r15), %r13
leaq (,%rbp,2), %r12
movl $0x4, %eax
testq %rbp, %rbp
jle 0x73087
movq 0x18(%r14), %rdi
movq %r13, %rsi
callq 0xef169
cmpl $0xffef, %eax           # imm = 0xFFEF
je 0x73077
cmpl $0xfeff, %eax           # imm = 0xFEFF
jne 0x73070
addq $0xa, %r15
addq $-0x2, %r12
movq %r15, %r13
movl $0x4, %eax
jmp 0x73087
addq $0xa, %r15
addq $-0x2, %r12
movl $0x3, %eax
movq %r15, %r13
movq 0x18(%r14), %rdi
movl %eax, (%rsp)
movl $0x3e8, %ecx            # imm = 0x3E8
movq %r13, %rsi
movq %r12, %rdx
movq %rbx, %r8
xorl %r9d, %r9d
callq 0xf0203
movq %rbx, %rdi
callq 0xf68c1
cmpq $0x0, 0x10(%rbx)
jle 0x730cf
movq %rbx, %rdi
callq 0xf74bd
leaq 0xb3c52(%rip), %rsi     # 0x126d14
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf6bb0