// File: /workspace/github_c_makefile_star2_O3/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, %rbx
callq 0xf5358
movq %rax, %r14
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 %rbx, %rdi
callq 0xe96f7
movq %rax, 0x20(%r14)
movq 0x18(%rbx), %r12
movl $0x10, %esi
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %rbx
movq %r12, (%rax)
xorl %eax, %eax
leaq 0x8(%rsp), %rdx
movq %rax, (%rdx)
leaq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movb $0x0, 0x30(%rsp)
movq 0x8(%rbx), %rsi
movq %r12, %rdi
callq 0xf166d
testl %eax, %eax
je 0xe3c0a
movq %rbx, 0x28(%rsp)
movq 0x8(%rsp), %rcx
movq 0x8(%rbx), %rdx
cmpq $0x100, %rcx            # imm = 0x100
jge 0xe3c01
movq (%rbx), %rdi
leaq 0x30(%rsp), %rsi
callq 0xee8d4
movq 0x8(%rsp), %rax
movb $0x0, 0x30(%rsp,%rax)
movq 0x18(%rsp), %rax
addq %rax, 0x8(%rbx)
movq 0x18(%r14), %r15
cmpq $0x10ffff, %r15         # imm = 0x10FFFF
jg 0xe3c44
movq 0x20(%r14), %r13
movq 0x20(%rsp), %rdx
cmpq %rdx, %r15
jl 0xe3a64
leaq (%rdx,%rdx), %r12
movl $0x28, %ecx
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %r12, %r8
callq 0xe9758
movq %rax, %r13
movq %rax, 0x20(%r14)
movq %r12, 0x20(%rsp)
leaq 0x30(%rsp), %rdi
movl $0x3a, %esi
callq 0xe82ee
testq %rax, %rax
je 0xe3c44
movq %rax, %r12
leaq (%r15,%r15,4), %rax
leaq (,%rax,8), %r15
addq %r13, %r15
movb $0x0, (%r12)
movq %r12, %r13
incq %r13
movq %r13, %rdi
callq 0x180e0
movq %rax, %rbp
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10937d
movl %eax, 0x4(%r15)
cmpl $0x10ffff, %eax         # imm = 0x10FFFF
ja 0xe3c44
leaq 0x1f(%rbp), %r8
testq %rbp, %rbp
cmovnsq %rbp, %r8
shrq $0x5, %r8
shll $0x3, %r8d
movl %r8d, 0x8(%r15)
movl $0x10, 0xc(%r15)
movl 0x18(%r14), %edx
movq 0x10(%rsp), %rdi
leaq 0x61d4a(%rip), %rsi     # 0x14583d
movl %eax, %ecx
movl $0x10, %r9d
xorl %eax, %eax
callq 0xe87e4
movl 0x8(%r15), %eax
leal -0x21(%rax), %ecx
cmpl $-0x19, %ecx
jb 0xe3c44
addq $0x7, %rax
shrq $0x3, %rax
movq %rax, 0x18(%r15)
movslq 0xc(%r15), %rsi
imulq %rax, %rsi
movq 0x10(%rsp), %rdi
callq 0xe8b2b
movq %rax, 0x20(%r15)
movq 0x18(%r15), %rbp
movslq 0xc(%r15), %rbx
movzbl (%r13), %edi
leaq 0x8(%rsp), %rsi
callq 0xebbbc
cmpl $0x0, 0x8(%rsp)
je 0xe3b98
incq 0x18(%r14)
movl 0x8(%r15), %eax
cmpl (%r14), %eax
jle 0xe3b62
movl %eax, (%r14)
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movq %rax, 0x18(%rsp)
movb $0x0, 0x30(%rsp)
movq 0x28(%rsp), %rbx
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x18(%rsp), %rcx
callq 0xf166d
testl %eax, %eax
jne 0xe39ea
jmp 0xe3c0a
movl %eax, %r13d
imulq %rbx, %rbp
cmpq $0x2, %rbp
movl $0x1, %eax
cmovlq %rax, %rbp
decq %rbp
xorl %ebx, %ebx
movzbl 0x2(%r12,%rbx,2), %edi
leaq 0x8(%rsp), %rsi
callq 0xebbbc
cmpl $0x0, 0x8(%rsp)
jne 0xe3b52
shlb $0x4, %r13b
orb %r13b, %al
movq 0x20(%r15), %rcx
movb %al, (%rcx,%rbx)
cmpq %rbx, %rbp
je 0xe3b52
movzbl 0x3(%r12,%rbx,2), %edi
incq %rbx
leaq 0x8(%rsp), %rsi
callq 0xebbbc
movl %eax, %r13d
cmpl $0x0, 0x8(%rsp)
je 0xe3bb1
jmp 0xe3b52
addq 0x18(%rsp), %rdx
movq %rdx, 0x8(%rbx)
movl 0x18(%r14), %edx
leaq 0x3b388(%rip), %rsi     # 0x11ef9d
movq 0x10(%rsp), %rdi
xorl %eax, %eax
callq 0xe8617
cmpq $0x0, 0x18(%r14)
jle 0xe3c44
cmpl $0x0, (%r14)
jle 0xe3c44
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xf576f
jmp 0xe3c5e
movq 0x8(%rbx), %rdx
leaq 0x61c05(%rip), %rsi     # 0x145854
movq 0x10(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9295
movq %r12, %rdi
movq %rbx, %rsi
callq 0xe8be2
movq 0x20(%r14), %rsi
testq %rsi, %rsi
je 0xe3caa
cmpq $0x0, 0x18(%r14)
jle 0xe3ca2
movl $0x20, %ebx
xorl %r15d, %r15d
movq 0x20(%r14), %rax
movq (%rax,%rbx), %rsi
movq %r12, %rdi
callq 0xe8be2
incq %r15
addq $0x28, %rbx
cmpq 0x18(%r14), %r15
jl 0xe3c81
movq 0x20(%r14), %rsi
movq %r12, %rdi
callq 0xe8be2
movq $0x0, 0x20(%r14)
movq %r12, %rdi
movq %r14, %rsi
callq 0xf536d
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop