// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/misc.c
// Function: de_run_evafont
static void de_run_evafont(deark *c, de_module_params *mparams)
{
	struct vgafont_ctx *d = NULL;
	u8 *mem = NULL;
	i64 foundpos;
	i64 font_data_endpos;
	int ret;

	d = de_malloc(c, sizeof(struct vgafont_ctx));

	// Tracing through the file seems difficult. Instead we search for the byte
	// pattern that appears just before the font data. It's nice that it also
	// contains a pointer to the end of the font data.

	// The pattern can appear as early as 310 (v2.01 VGA), and as late as 612
	// (v3.05 EGA).
#define EVAFONT_BUF_POS1 280
#define EVAFONT_BUF_LEN1 380
	mem = de_malloc(c, EVAFONT_BUF_LEN1);
	de_read(mem, EVAFONT_BUF_POS1, EVAFONT_BUF_LEN1);
	ret = de_memsearch_match(mem, EVAFONT_BUF_LEN1,
		(const u8*)"\xba??\xb1\x04\xd3\xea\x42\xb8\x00\x31\xcd\x21", 13,
		'?', &foundpos);
	if(!ret) {
		d->need_errmsg = 1;
		goto done;
	}
	foundpos += EVAFONT_BUF_POS1;
	de_dbg(c, "[found sig at %"I64_FMT"]", foundpos);

	d->font_data_pos = foundpos + 13;
	de_dbg(c, "data pos: %"I64_FMT, d->font_data_pos);

	font_data_endpos = de_getu16le(foundpos+1);
	font_data_endpos -= 256;
	de_dbg(c, "data endpos: %"I64_FMT, font_data_endpos);
	if(font_data_endpos > c->infile->len) {
		d->need_errmsg = 1;
		goto done;
	}

	d->font_data_size = font_data_endpos - d->font_data_pos;
	de_dbg(c, "data size: %"I64_FMT, d->font_data_size);

	if(d->font_data_size % 256) {
		d->need_errmsg = 1;
		goto done;
	}

	d->height = d->font_data_size / 256;

	de_dbg(c, "char size: 8"DE_CHAR_TIMES"%d", (int)d->height);

	if(d->height!=8 && d->height!=14 && d->height!=16) {
		d->need_errmsg = 1;
		goto done;
	}

	d->fontdata = de_malloc(c, d->font_data_size);
	de_read(d->fontdata, d->font_data_pos, d->font_data_size);

	vgafont_common_config1(c, d);
	vgafont_common_config2(c, d);
	vgafont_main(c, d, NULL, 0);

done:
	de_free(c, mem);
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "Bad or unsupported EVAfont font");
		}
		destroy_vgafont_ctx(c, d);
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl $0x38, %esi
callq 0xe969b
movq %rax, %r14
movl $0x17c, %esi            # imm = 0x17C
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r15
movq 0x18(%rbx), %rdi
movl $0x118, %r12d           # imm = 0x118
movl $0x118, %edx            # imm = 0x118
movl $0x17c, %ecx            # imm = 0x17C
movq %rax, %rsi
callq 0xeeee8
leaq 0xbe65e(%rip), %rdx     # 0x12493a
movq %rsp, %r9
movl $0x17c, %esi            # imm = 0x17C
movl $0xd, %ecx
movq %r15, %rdi
movl $0x3f, %r8d
callq 0xec044
testl %eax, %eax
je 0x66394
addq (%rsp), %r12
movq %r12, (%rsp)
leaq 0xbe4e2(%rip), %rsi     # 0x1247f0
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq (%rsp), %rdx
addq $0xd, %rdx
movq %rdx, 0x18(%r14)
leaq 0xbfad7(%rip), %rsi     # 0x125e05
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
incq %rsi
callq 0xef1ca
movq %rax, %r12
addq $-0x100, %r12
leaq 0xb116f(%rip), %rsi     # 0x1174c8
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r12
jg 0x66394
subq 0x18(%r14), %r12
movq %r12, 0x20(%r14)
leaq 0xcb647(%rip), %rsi     # 0x1319c6
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x20(%r14), %rdx
testb %dl, %dl
je 0x663d8
movb $0x1, 0x30(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe9752
testq %r14, %r14
je 0x663cc
cmpb $0x0, 0x30(%r14)
je 0x663c1
leaq 0xbe44c(%rip), %rsi     # 0x124803
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movq %r14, %rsi
callq 0x673ab
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
sarq $0x8, %rdx
movq %rdx, 0x10(%r14)
leaq 0xbe2de(%rip), %rsi     # 0x1246c5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x10(%r14), %rax
cmpq $0x10, %rax
ja 0x66394
movl $0x14100, %ecx          # imm = 0x14100
btq %rax, %rcx
jae 0x66394
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0xe969b
movq %rax, 0x28(%r14)
movq 0x18(%rbx), %rdi
movq 0x18(%r14), %rdx
movq 0x20(%r14), %rcx
movq %rax, %rsi
callq 0xeeee8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x67241
movq %r14, %rdi
callq 0x672b2
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x67354
jmp 0x66399