// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/psf.c
// Function: do_psf2_unicode_table
static void do_psf2_unicode_table(deark *c, lctx *d, struct de_bitmap_font *font)
{
	i64 cur_idx;
	i64 pos;
	int ret;
	i64 foundpos;
	i64 char_data_len;
	u8 char_data_buf[200];
	i32 ch;
	i64 utf8len;

	de_dbg(c, "Unicode table at %d", (int)d->unicode_table_pos);
	de_dbg_indent(c, 1);

	pos = d->unicode_table_pos;
	cur_idx = 0;
	while(1) {
		i64 pos_in_char_data;
		i64 cp_idx;

		if(cur_idx >= d->num_glyphs) break;
		if(pos >= c->infile->len) break;

		// Figure out the size of the data for this glyph
		ret = dbuf_search_byte(c->infile, 0xff, pos,
			c->infile->len - pos, &foundpos);
		if(!ret) break;
		char_data_len = foundpos - pos;
		if(char_data_len<0) char_data_len=0;
		else if(char_data_len>(i64)sizeof(char_data_buf)) char_data_len=(i64)sizeof(char_data_buf);

		// Read all the data for this glyph
		de_read(char_data_buf, pos, char_data_len);

		// Read the codepoints for this glyph
		cp_idx = 0;
		pos_in_char_data = 0;
		while(1) {
			if(pos_in_char_data >= char_data_len) break;
			if(cp_idx > MAX_EXTRA_CODEPOINTS) break; // sanity check

			ret = de_utf8_to_uchar(&char_data_buf[pos_in_char_data], char_data_len-pos_in_char_data,
				&ch, &utf8len);
			if(!ret) {
				// If there are any multi-codepoint aliases for this glyph, we
				// expect de_utf8_to_uchar() to fail when it hits the 0xfe byte.
				// So, this is not necessarily an error.
				break;
			}

			if(cp_idx==0) {
				// This is the primary Unicode codepoint for this glyph
				de_dbg2(c, "char[%d] = U+%04x", (int)cur_idx, (unsigned int)ch);
				font->char_array[cur_idx].codepoint_unicode = ch;
			}
			else {
				do_extra_codepoint(c, d, font, cur_idx, ch);
			}

			cp_idx++;
			pos_in_char_data += utf8len;
		}

		if(cp_idx==0) {
			de_warn(c, "Missing codepoint for char #%d", (int)cur_idx);
		}

		// Advance to the next glyph
		pos = foundpos+1;
		cur_idx++;
	}

	font->has_unicode_codepoints = 1;
	font->prefer_unicode = 1;

	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movl %eax, %edx
leaq 0xab1fb(%rip), %rsi     # 0x1c4fc7
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jl 0x119e08
jmp 0x119fe2
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x119e1f
jmp 0x119fe2
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
subq -0x28(%rbp), %rcx
movl $0xff, %esi
leaq -0x38(%rbp), %r8
callq 0x1667a0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x119e57
jmp 0x119fe2
movq -0x38(%rbp), %rax
subq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jge 0x119e74
movq $0x0, -0x40(%rbp)
jmp 0x119e88
cmpq $0xc8, -0x40(%rbp)
jle 0x119e86
movq $0xc8, -0x40(%rbp)
jmp 0x119e88
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x110(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x40(%rbp), %rcx
callq 0x164d10
movq $0x0, -0x130(%rbp)
movq $0x0, -0x128(%rbp)
movq -0x128(%rbp), %rax
cmpq -0x40(%rbp), %rax
jl 0x119ecc
jmp 0x119fa3
cmpq $0x7d0, -0x130(%rbp) # imm = 0x7D0
jle 0x119ede
jmp 0x119fa3
leaq -0x110(%rbp), %rdi
addq -0x128(%rbp), %rdi
movq -0x40(%rbp), %rsi
subq -0x128(%rbp), %rsi
leaq -0x114(%rbp), %rdx
leaq -0x120(%rbp), %rcx
callq 0x160d00
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x119f18
jmp 0x119fa3
cmpq $0x0, -0x130(%rbp)
jne 0x119f5b
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
movl -0x114(%rbp), %ecx
leaq 0xab0a2(%rip), %rsi     # 0x1c4fdb
movb $0x0, %al
callq 0x15b350
movl -0x114(%rbp), %ecx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
imulq $0x28, -0x20(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
jmp 0x119f77
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl -0x114(%rbp), %r8d
callq 0x11a1b0
movq -0x130(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x130(%rbp)
movq -0x120(%rbp), %rax
addq -0x128(%rbp), %rax
movq %rax, -0x128(%rbp)
jmp 0x119eba
cmpq $0x0, -0x130(%rbp)
jne 0x119fc5
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq 0xab02f(%rip), %rsi     # 0x1c4fed
movb $0x0, %al
callq 0x15bc10
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x119df5
movq -0x18(%rbp), %rax
movb $0x1, 0x11(%rax)
movq -0x18(%rbp), %rax
movb $0x1, 0x12(%rax)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopl (%rax)