// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/unifont.c
// Function: de_run_unifont_hex
static void de_run_unifont_hex(deark *c, de_module_params *mparams)
{
	struct de_bitmap_font *font = NULL;
	i64 char_array_numalloc = 0;
	char linebuf[256];
	struct de_linereader *lr = NULL;
	int ok = 0;

	font = de_create_bitmap_font(c);
	font->has_unicode_codepoints = 1;
	font->prefer_unicode = 1;
	font->nominal_height = 16;
	char_array_numalloc = 1024;
	font->char_array = de_mallocarray(c, char_array_numalloc, sizeof(struct de_bitmap_font_char));

	lr = de_linereader_create(c, c->infile);

	while(de_linereader_readnextline(c, lr, linebuf, sizeof(linebuf), 0)) {
		i64 idx;
		struct de_bitmap_font_char *ch;
		i64 fdata_len;
		char *dptr; // Pointer into linebuf, to the char after the ":"

		if(font->num_chars>=17*65536) goto done;

		idx = font->num_chars;
		if(idx >= char_array_numalloc) {
			i64 new_numalloc = char_array_numalloc*2;
			font->char_array = de_reallocarray(c, font->char_array,
				char_array_numalloc, sizeof(struct de_bitmap_font_char),
				new_numalloc);
			char_array_numalloc = new_numalloc;
		}
		ch = &font->char_array[idx];

		dptr = de_strchr(linebuf, ':');
		if(!dptr) goto done;
		*dptr = '\0';
		dptr++;

		fdata_len = (i64)de_strlen(dptr);
		ch->codepoint_unicode = (i32)de_strtoll(linebuf, NULL, 16);
		if(ch->codepoint_unicode<0 || ch->codepoint_unicode>=17*65536) goto done;

		ch->width = (int)((fdata_len/32)*8);
		ch->height = 16;
		de_dbg2(c, "char[%d] U+%04X %d"DE_CHAR_TIMES"%d",
			(int)font->num_chars, (unsigned int)ch->codepoint_unicode,
			ch->width, ch->height);
		if(ch->width<8 || ch->width>32) goto done;
		ch->rowspan = (ch->width+7)/8;
		ch->bitmap = de_malloc(c, ch->rowspan * ch->height);
		decode_fontdata(c, dptr, ch);

		font->num_chars++;
		if(ch->width > font->nominal_width) {
			font->nominal_width = ch->width;
		}
	}

	de_dbg(c, "number of characters: %d", (int)font->num_chars);
	if(font->num_chars<1) goto done;
	if(font->nominal_width<1) goto done;

	de_font_bitmap_font_to_image(c, font, NULL, 0);
	ok = 1;

done:
	if(!ok) {
		de_err(c, "Error parsing HEX font file (offset %"I64_FMT")", lr->f_pos);
	}
	de_linereader_destroy(c, lr);
	if(font) {
		if(font->char_array) {
			i64 k;
			for(k=0; k<font->num_chars; k++) {
				de_free(c, font->char_array[k].bitmap);
			}
			de_free(c, font->char_array);
		}
		font->char_array = NULL;
		de_destroy_bitmap_font(c, font);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdi, %r14
callq 0xe9d18
movq %rax, %rbx
movw $0x101, 0x11(%rax)      # imm = 0x101
movl $0x10, 0x4(%rax)
movl $0x400, %eax            # imm = 0x400
movq %rax, 0x20(%rsp)
pushq $0x28
popq %rdx
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdi
callq 0xdf66f
movq %rax, 0x20(%rbx)
movq 0x18(%r14), %r12
pushq $0x10
popq %rsi
movq %r14, %rdi
callq 0xdecaf
movq %rax, %r15
movq %r12, (%rax)
leaq 0x8(%rsp), %rbp
movq %rbx, (%rsp)
movq %rax, 0x10(%rsp)
movq %r14, 0x28(%rsp)
andq $0x0, 0x8(%rsp)
andq $0x0, 0x18(%rsp)
movb $0x0, 0x30(%rsp)
movq 0x8(%r15), %rsi
movq %r12, %rdi
movq %rbp, %rdx
leaq 0x18(%rsp), %rcx
callq 0xe6482
testl %eax, %eax
je 0xda0e7
movq 0x8(%rsp), %rcx
movq 0x8(%r15), %rdx
cmpq $0x100, %rcx            # imm = 0x100
jge 0xda0de
movq (%r15), %rdi
leaq 0x30(%rsp), %rsi
callq 0xe3cb7
movq 0x8(%rsp), %rax
movb $0x0, 0x30(%rsp,%rax)
movq 0x18(%rsp), %rax
addq %rax, 0x8(%r15)
movq 0x18(%rbx), %rbx
cmpq $0x10ffff, %rbx         # imm = 0x10FFFF
jg 0xda125
movq (%rsp), %r13
movq 0x20(%r13), %r12
movq 0x20(%rsp), %rdx
cmpq %rdx, %rbx
jl 0xd9f9b
leaq (%rdx,%rdx), %r15
movq %r14, %rdi
movq %r12, %rsi
pushq $0x28
popq %rcx
movq %r15, %r8
callq 0xdf6b6
movq %rax, %r12
movq %rax, 0x20(%r13)
movq %r15, 0x20(%rsp)
leaq 0x30(%rsp), %rdi
pushq $0x3a
popq %rsi
callq 0xde55b
testq %rax, %rax
je 0xda125
movq %rax, %r13
imulq $0x28, %rbx, %rax
addq %rax, %r12
movb $0x0, (%r13)
movq %r13, %rdi
incq %rdi
callq 0x180e0
movq %rax, %r15
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
pushq $0x10
popq %rdx
callq 0xfc8f5
movq %rax, %rcx
movl %ecx, 0x4(%r12)
cmpl $0x10ffff, %ecx         # imm = 0x10FFFF
ja 0xda125
movq %r15, %rax
cqto
pushq $0x20
popq %rsi
idivq %rsi
leal (,%rax,8), %r8d
movl %r8d, 0x8(%r12)
movl $0x10, 0xc(%r12)
movq (%rsp), %rax
movl 0x18(%rax), %edx
movq %r14, %rdi
leaq 0x5e3ca(%rip), %rsi     # 0x1383ed
pushq $0x10
popq %r9
xorl %eax, %eax
callq 0xde9f1
movl 0x8(%r12), %eax
leal -0x21(%rax), %ecx
cmpl $-0x19, %ecx
jb 0xda125
addq $0x7, %rax
shrq $0x3, %rax
movq %rax, 0x18(%r12)
movslq 0xc(%r12), %rsi
imulq %rax, %rsi
movq %r14, %rdi
callq 0xdecaf
movq %rax, 0x20(%r12)
movslq 0xc(%r12), %rbx
imulq 0x18(%r12), %rbx
pushq $0x1
popq %r14
movzbl -0x1(%r13,%r14,2), %edi
movq %rbp, %rsi
callq 0xe11c4
cmpl $0x0, 0x8(%rsp)
jne 0xda0b9
movl %eax, %r15d
movzbl (%r13,%r14,2), %edi
movq %rbp, %rsi
callq 0xe11c4
cmpl $0x0, 0x8(%rsp)
jne 0xda0b9
shlb $0x4, %r15b
orb %r15b, %al
movq 0x20(%r12), %rcx
movb %al, -0x1(%rcx,%r14)
cmpq %rbx, %r14
jge 0xda0b9
incq %r14
jmp 0xda071
movq (%rsp), %rbx
incq 0x18(%rbx)
movl 0x8(%r12), %eax
cmpl (%rbx), %eax
jle 0xda0cc
movl %eax, (%rbx)
movq 0x10(%rsp), %r15
movq (%r15), %r12
movq 0x28(%rsp), %r14
jmp 0xd9ef4
addq 0x18(%rsp), %rdx
movq %rdx, 0x8(%r15)
movl 0x18(%rbx), %edx
leaq 0x37ddc(%rip), %rsi     # 0x111ecd
movq %r14, %rdi
xorl %eax, %eax
callq 0xde896
cmpq $0x0, 0x18(%rbx)
jle 0xda125
movq (%rsp), %rax
cmpl $0x0, (%rax)
jle 0xda125
movq %r14, %rdi
movq (%rsp), %r15
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xea0f6
movq 0x10(%rsp), %rbx
jmp 0xda143
movq 0x10(%rsp), %rbx
movq 0x8(%rbx), %rdx
leaq 0x5e2cf(%rip), %rsi     # 0x138404
movq %r14, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq (%rsp), %r15
movq %r14, %rdi
movq %rbx, %rsi
callq 0xded32
cmpq $0x0, 0x20(%r15)
pushq $0x20
popq %r12
je 0xda182
xorl %ebx, %ebx
movq 0x20(%r15), %rsi
cmpq 0x18(%r15), %rbx
jge 0xda17a
movq (%rsi,%r12), %rsi
movq %r14, %rdi
callq 0xded32
incq %rbx
addq $0x28, %r12
jmp 0xda15b
movq %r14, %rdi
callq 0xded32
andq $0x0, 0x20(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0xe9d28
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq