// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x38
popq %rsi
callq 0xdecaf
movq %rax, %r14
movl $0x17c, %esi            # imm = 0x17C
movq %rbx, %rdi
callq 0xdecaf
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 0xe3cb7
leaq 0xb50cb(%rip), %rdx     # 0x11795a
pushq $0xd
popq %rcx
pushq $0x3f
popq %r8
movq %rsp, %r9
movl $0x17c, %esi            # imm = 0x17C
movq %r15, %rdi
callq 0xe1021
testl %eax, %eax
je 0x62943
addq (%rsp), %r12
movq %r12, (%rsp)
leaq 0xb4f53(%rip), %rsi     # 0x117810
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
movq (%rsp), %rdx
addq $0xd, %rdx
movq %rdx, 0x18(%r14)
leaq 0xb6558(%rip), %rsi     # 0x118e35
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
incq %rsi
callq 0xe3fa3
movq %rax, %r12
addq $-0x100, %r12
leaq 0xa7bd0(%rip), %rsi     # 0x10a4d8
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r12
jg 0x62943
subq 0x18(%r14), %r12
movq %r12, 0x20(%r14)
leaq 0xc2130(%rip), %rsi     # 0x124a5e
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x20(%r14), %rdx
testb %dl, %dl
je 0x62982
movb $0x1, 0x30(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded32
cmpb $0x0, 0x30(%r14)
je 0x6296b
leaq 0xb4ec2(%rip), %rsi     # 0x117823
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6392b
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
sarq $0x8, %rdx
movq %rdx, 0x10(%r14)
leaq 0xb4d54(%rip), %rsi     # 0x1176e5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x10(%r14), %rax
cmpq $0x10, %rax
ja 0x62943
movl $0x14100, %ecx          # imm = 0x14100
btq %rax, %rcx
jae 0x62943
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, 0x28(%r14)
movq 0x18(%rbx), %rdi
movq 0x18(%r14), %rdx
movq 0x20(%r14), %rcx
movq %rax, %rsi
callq 0xe3cb7
movq %rbx, %rdi
movq %r14, %rsi
callq 0x637dd
movq %r14, %rdi
callq 0x6384a
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x638d9
jmp 0x62948