// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/misc.c
// Function: de_run_fontedit
static void de_run_fontedit(deark *c, de_module_params *mparams)
{
	struct vgafont_ctx *d = NULL;
	i64 jmp1;
	u8 opt_extract_template;
	de_finfo *fi_tmpl = NULL;
	struct de_crcobj *crco = NULL;

	d = de_malloc(c, sizeof(struct vgafont_ctx));
	opt_extract_template = (u8)de_get_ext_option_bool(c, "fontedit:template",
		(c->extract_level>=2 ? 1 : 0));

	jmp1 = de_getbyte(1) + 2;
	d->height = (i64)de_getbyte(jmp1-3);
	de_dbg(c, "char size: 8"DE_CHAR_TIMES"%d", (int)d->height);
	d->font_data_pos = de_getu16le(jmp1+25) - 0x100;
	de_dbg(c, "data pos: %"I64_FMT, d->font_data_pos);
	d->font_data_size = d->height*256;

	if(d->height<VGAFONT_MINH || d->height>VGAFONT_MAXH ||
		(d->font_data_pos + d->font_data_size > c->infile->len))
	{
		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);

	if(opt_extract_template) {
		u32 crc1, crc2;

		crco = de_crcobj_create(c, DE_CRCOBJ_CRC32_IEEE);
		de_crcobj_addbuf(crco, d->fontdata, d->font_data_size);
		crc1 = de_crcobj_getval(crco);

		d->font_data_pos += 4096;
		if(d->font_data_pos + d->font_data_size > c->infile->len) {
			de_dbg(c, "[template not present]");
			goto done;
		}
		de_dbg(c, "template pos: %"I64_FMT, d->font_data_pos);

		// This is a bit hacky, but we can just change the font data out
		// from under the pointers created by vgafont_common_config2().
		de_read(d->fontdata, d->font_data_pos, d->font_data_size);
		de_crcobj_reset(crco);
		de_crcobj_addbuf(crco, d->fontdata, d->font_data_size);
		crc2 = de_crcobj_getval(crco);

		if(crc2==crc1) {
			de_dbg(c, "[template is same as main font]");
			goto done;
		}

		fi_tmpl = de_finfo_create(c);
		de_finfo_set_name_from_sz(c, fi_tmpl, "template", 0, DE_ENCODING_LATIN1);
		vgafont_main(c, d, fi_tmpl, DE_CREATEFLAG_IS_AUX);
	}

done:
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "Bad or unsupported FONTEDIT font");
		}
		destroy_vgafont_ctx(c, d);
	}
	de_finfo_destroy(c, fi_tmpl);
	de_crcobj_destroy(crco);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %rbx
movl $0x38, %esi
callq 0xe8b2b
movq %rax, %r14
xorl %edx, %edx
cmpl $0x2, 0x90(%rbx)
setge %dl
leaq 0xbeba1(%rip), %rsi     # 0x124948
movq %rbx, %rdi
callq 0xe9bd0
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movl $0x1, %esi
callq 0xeead3
movzbl %al, %r15d
movq 0x18(%rbx), %rdi
leaq -0x1(%r15), %rsi
callq 0xeead3
movzbl %al, %edx
movq %rdx, 0x10(%r14)
leaq 0xbeb07(%rip), %rsi     # 0x1248e5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
addq $0x1b, %r15
movq %r15, %rsi
callq 0xeebc8
leaq -0x100(%rax), %rdx
movq %rdx, 0x18(%r14)
leaq 0xc022b(%rip), %rsi     # 0x126035
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x10(%r14), %rax
movq %rax, %rsi
shlq $0x8, %rsi
movq %rsi, 0x20(%r14)
addq $-0x15, %rax
cmpq $-0x12, %rax
jb 0x65e3e
movq 0x18(%r14), %rax
addq %rsi, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x65e87
movb $0x1, 0x30(%r14)
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpb $0x0, 0x30(%r14)
je 0x65e61
leaq 0xbeb4c(%rip), %rsi     # 0x1249a3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movq %r14, %rsi
callq 0x67183
movq %rbx, %rdi
movq %r12, %rsi
callq 0xea50e
movq %r15, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xeb858
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
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x67131
movl $0x0, %r12d
testb %bpl, %bpl
je 0x65e49
movq %rbx, %rdi
movl $0x10, %esi
callq 0xeb499
movq %rax, %r15
movq 0x20(%r14), %rdx
movq 0x28(%r14), %rsi
movq %rax, %rdi
callq 0xeb8ad
movq %r15, %rdi
callq 0xeb865
movl %eax, %ebp
movl $0x1000, %edx           # imm = 0x1000
addq 0x18(%r14), %rdx
movq %rdx, 0x18(%r14)
movq 0x20(%r14), %rax
addq %rdx, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x65f2c
leaq 0xbea30(%rip), %rsi     # 0x12495a
jmp 0x65f7d
leaq 0xbea3e(%rip), %rsi     # 0x124971
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq 0x28(%r14), %rsi
movq 0x18(%r14), %rdx
movq 0x20(%r14), %rcx
callq 0xee8d4
movq %r15, %rdi
callq 0xeb7fe
movq 0x20(%r14), %rdx
movq 0x28(%r14), %rsi
movq %r15, %rdi
callq 0xeb8ad
movq %r15, %rdi
callq 0xeb865
cmpl %ebp, %eax
jne 0x65f8f
leaq 0xbea06(%rip), %rsi     # 0x124983
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
jmp 0x65e49
movq %rbx, %rdi
callq 0xea504
movq %rax, %r12
leaq 0xbe9b0(%rip), %rdx     # 0x124951
movq %rbx, %rdi
movq %rax, %rsi
xorl %ecx, %ecx
movl $0x5, %r8d
callq 0xea78f
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movl $0x1, %ecx
callq 0x67131
jmp 0x65e49