// File: /workspace/github_c_makefile_star2_O1/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 0xe969b
movq %rax, %r14
xorl %edx, %edx
cmpl $0x2, 0x90(%rbx)
setge %dl
leaq 0xbe76e(%rip), %rsi     # 0x124728
movq %rbx, %rdi
callq 0xea727
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movl $0x1, %esi
callq 0xef0d5
movzbl %al, %r15d
movq 0x18(%rbx), %rdi
leaq -0x1(%r15), %rsi
callq 0xef0d5
movzbl %al, %edx
movq %rdx, 0x10(%r14)
leaq 0xbe6d4(%rip), %rsi     # 0x1246c5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
addq $0x1b, %r15
movq %r15, %rsi
callq 0xef1ca
leaq -0x100(%rax), %rdx
movq %rdx, 0x18(%r14)
leaq 0xbfde8(%rip), %rsi     # 0x125e05
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x10(%r14), %rax
movq %rax, %rsi
shlq $0x8, %rsi
movq %rsi, 0x20(%r14)
addq $-0x15, %rax
cmpq $-0x12, %rax
jb 0x66051
movq 0x18(%r14), %rax
addq %rsi, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x6609f
movb $0x1, 0x30(%r14)
xorl %r15d, %r15d
xorl %r12d, %r12d
testq %r14, %r14
je 0x66084
cmpb $0x0, 0x30(%r14)
je 0x66079
leaq 0xbe714(%rip), %rsi     # 0x124783
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movq %r14, %rsi
callq 0x673ab
movq %rbx, %rdi
movq %r12, %rsi
callq 0xea9b3
movq %r15, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xebe1d
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
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x67354
movl $0x0, %r12d
testb %bpl, %bpl
je 0x6605c
movq %rbx, %rdi
movl $0x10, %esi
callq 0xeba56
movq %rax, %r15
movq 0x20(%r14), %rdx
movq 0x28(%r14), %rsi
movq %rax, %rdi
callq 0xebe72
movq %r15, %rdi
callq 0xebe2a
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 0x66144
leaq 0xbe5f8(%rip), %rsi     # 0x12473a
jmp 0x66195
leaq 0xbe606(%rip), %rsi     # 0x124751
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq 0x28(%r14), %rsi
movq 0x18(%r14), %rdx
movq 0x20(%r14), %rcx
callq 0xeeee8
movq %r15, %rdi
callq 0xebdc3
movq 0x20(%r14), %rdx
movq 0x28(%r14), %rsi
movq %r15, %rdi
callq 0xebe72
movq %r15, %rdi
callq 0xebe2a
cmpl %ebp, %eax
jne 0x661a7
leaq 0xbe5ce(%rip), %rsi     # 0x124763
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
jmp 0x6605c
movq %rbx, %rdi
callq 0xea9a9
movq %rax, %r12
leaq 0xbe578(%rip), %rdx     # 0x124731
movq %rbx, %rdi
movq %rax, %rsi
xorl %ecx, %ecx
movl $0x5, %r8d
callq 0xeac7c
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movl $0x1, %ecx
callq 0x67354
jmp 0x6605c