// File: /workspace/github_c_makefile_star2_O1/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 0xf57ec
movq %rax, %rbp
movw $0x101, 0x11(%rax)      # imm = 0x101
movl $0x10, 0x4(%rax)
movl $0x400, %eax            # imm = 0x400
movq %rax, 0x20(%rsp)
movl $0x400, %esi            # imm = 0x400
movl $0x28, %edx
movq %r14, %rdi
callq 0xea259
movq %rax, 0x20(%rbp)
movq 0x18(%r14), %rbx
movl $0x10, %esi
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0xe969b
movq %rax, %r15
movq %rbx, (%rax)
movq %rsp, %r12
leaq 0x30(%rsp), %r13
movq %rbp, 0x10(%rsp)
xorl %eax, %eax
movq %rax, (%rsp)
movq %rax, 0x18(%rsp)
movb $0x0, 0x30(%rsp)
movq (%r15), %rdi
movq 0x8(%r15), %rsi
movq %r12, %rdx
leaq 0x18(%rsp), %rcx
callq 0xf1c72
testl %eax, %eax
je 0xe476f
movq (%rsp), %rcx
movq 0x8(%r15), %rdx
cmpq $0x100, %rcx            # imm = 0x100
jge 0xe4766
movq (%r15), %rdi
movq %r13, %rsi
callq 0xeeee8
movq (%rsp), %rax
movb $0x0, 0x30(%rsp,%rax)
movq 0x18(%rsp), %rax
addq %rax, 0x8(%r15)
movq 0x18(%rbp), %rbx
cmpq $0x10ffff, %rbx         # imm = 0x10FFFF
jle 0xe45ba
xorl %r14d, %r14d
jmp 0xe46a1
movq 0x20(%rsp), %rdx
cmpq %rdx, %rbx
jl 0xe45ea
leaq (%rdx,%rdx), %r12
movq 0x20(%rbp), %rsi
movl $0x28, %ecx
movq 0x8(%rsp), %rdi
movq %r12, %r8
callq 0xea2ba
movq %rax, 0x20(%rbp)
movq %r12, 0x20(%rsp)
movq %rsp, %r12
leaq (%rbx,%rbx,4), %rbx
shlq $0x3, %rbx
addq 0x20(%rbp), %rbx
movq %r13, %rdi
movl $0x3a, %esi
callq 0xe8ed3
testq %rax, %rax
je 0xe4699
movq %rax, %rbp
movb $0x0, (%rax)
incq %rbp
movq %rbp, %rdi
callq 0x180e0
movq %rax, %r12
xorl %r14d, %r14d
movq %r13, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x109895
movl %eax, 0x4(%rbx)
cmpl $0x10ffff, %eax         # imm = 0x10FFFF
ja 0xe468f
leaq 0x1f(%r12), %r8
testq %r12, %r12
cmovnsq %r12, %r8
shrq $0x5, %r8
shll $0x3, %r8d
movl %r8d, 0x8(%rbx)
movl $0x10, 0xc(%rbx)
movq 0x10(%rsp), %rcx
movl 0x18(%rcx), %edx
xorl %r14d, %r14d
movq 0x8(%rsp), %r12
movq %r12, %rdi
leaq 0x60ce8(%rip), %rsi     # 0x14535d
movl %eax, %ecx
movl $0x10, %r9d
xorl %eax, %eax
callq 0xe93cf
movl 0x8(%rbx), %eax
leal -0x21(%rax), %ecx
cmpl $-0x19, %ecx
jae 0xe46b4
movq 0x10(%rsp), %rbp
movq %rsp, %r12
jmp 0xe46a1
xorl %r14d, %r14d
movq 0x10(%rsp), %rbp
testb %r14b, %r14b
leaq 0x30(%rsp), %r13
jne 0xe4547
jmp 0xe47a8
addq $0x7, %rax
shrq $0x3, %rax
movq %rax, 0x18(%rbx)
movslq 0xc(%rbx), %rsi
imulq %rax, %rsi
movq %r12, %rdi
callq 0xe969b
movq %rax, 0x20(%rbx)
movslq 0xc(%rbx), %rax
imulq 0x18(%rbx), %rax
movq %rax, 0x28(%rsp)
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %rsp, %r12
movzbl (%rbp,%r14), %edi
movq %r12, %rsi
callq 0xec1cc
cmpl $0x0, (%rsp)
je 0xe4704
incq %r14
jmp 0xe471f
movq %r12, %rsi
movl %eax, %r12d
movzbl 0x1(%rbp,%r14), %edi
addq $0x2, %r14
callq 0xec1cc
cmpl $0x0, (%rsp)
je 0xe472a
xorl %eax, %eax
testb %al, %al
movq %rsp, %r12
jne 0xe46eb
jmp 0xe4746
shlb $0x4, %r12b
orb %r12b, %al
movq 0x20(%rbx), %rcx
movb %al, (%rcx,%r13)
incq %r13
cmpq 0x28(%rsp), %r13
setl %al
jmp 0xe4721
movq 0x10(%rsp), %rbp
incq 0x18(%rbp)
movl 0x8(%rbx), %eax
movb $0x1, %r14b
cmpl (%rbp), %eax
jle 0xe46a1
movl %eax, (%rbp)
jmp 0xe46a1
addq 0x18(%rsp), %rdx
movq %rdx, 0x8(%r15)
movl 0x18(%rbp), %edx
leaq 0x3a71c(%rip), %rsi     # 0x11ee95
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0xe9226
cmpq $0x0, 0x18(%rbp)
jle 0xe47a8
cmpl $0x0, (%rbp)
jle 0xe47a8
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xf5c29
jmp 0xe47c2
movq 0x8(%r15), %rdx
leaq 0x60bc1(%rip), %rsi     # 0x145374
movq 0x8(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %r12, %rdi
movq %r15, %rsi
callq 0xe9752
testq %rbp, %rbp
je 0xe4824
cmpq $0x0, 0x20(%rbp)
je 0xe4811
cmpq $0x0, 0x18(%rbp)
jle 0xe4805
movl $0x20, %ebx
xorl %r14d, %r14d
movq 0x20(%rbp), %rax
movq (%rax,%rbx), %rsi
movq %r12, %rdi
callq 0xe9752
incq %r14
addq $0x28, %rbx
cmpq 0x18(%rbp), %r14
jl 0xe47e8
movq 0x20(%rbp), %rsi
movq %r12, %rdi
callq 0xe9752
movq $0x0, 0x20(%rbp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0xf5801
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop