// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
movl $0x17c, %esi            # imm = 0x17C
movq %rbx, %rdi
callq 0xe8b2b
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 0xee8d4
leaq 0xbea96(%rip), %rdx     # 0x124b5a
movq %rsp, %r9
movl $0x17c, %esi            # imm = 0x17C
movl $0xd, %ecx
movq %r15, %rdi
movl $0x3f, %r8d
callq 0xeba66
testl %eax, %eax
je 0x6617c
addq (%rsp), %r12
movq %r12, (%rsp)
leaq 0xbe91a(%rip), %rsi     # 0x124a10
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movq (%rsp), %rdx
addq $0xd, %rdx
movq %rdx, 0x18(%r14)
leaq 0xbff1f(%rip), %rsi     # 0x126035
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
incq %rsi
callq 0xeebc8
movq %rax, %r12
addq $-0x100, %r12
leaq 0xb1427(%rip), %rsi     # 0x117568
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r12
jg 0x6617c
subq 0x18(%r14), %r12
movq %r12, 0x20(%r14)
leaq 0xcbbe3(%rip), %rsi     # 0x131d4a
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x20(%r14), %rdx
testb %dl, %dl
je 0x661bb
movb $0x1, 0x30(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8be2
cmpb $0x0, 0x30(%r14)
je 0x661a4
leaq 0xbe889(%rip), %rsi     # 0x124a23
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movq %r14, %rsi
callq 0x67183
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
sarq $0x8, %rdx
movq %rdx, 0x10(%r14)
leaq 0xbe71b(%rip), %rsi     # 0x1248e5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x10(%r14), %rax
cmpq $0x10, %rax
ja 0x6617c
movl $0x14100, %ecx          # imm = 0x14100
btq %rax, %rcx
jae 0x6617c
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, 0x28(%r14)
movq 0x18(%rbx), %rdi
movq 0x18(%r14), %rdx
movq 0x20(%r14), %rcx
movq %rax, %rsi
callq 0xee8d4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6702a
movq %r14, %rdi
callq 0x6709b
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x67131
jmp 0x66181