// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x38
popq %rsi
callq 0xdecaf
movq %rax, %r14
xorl %edx, %edx
cmpl $0x2, 0x90(%rbx)
setge %dl
leaq 0xb51c6(%rip), %rsi     # 0x117748
movq %rbx, %rdi
callq 0xdfae5
movl %eax, %ebp
movq 0x18(%rbx), %rdi
pushq $0x1
popq %rsi
callq 0xe3eb4
movzbl %al, %r15d
movq 0x18(%rbx), %rdi
leaq -0x1(%r15), %rsi
callq 0xe3eb4
movzbl %al, %edx
movq %rdx, 0x10(%r14)
leaq 0xb512e(%rip), %rsi     # 0x1176e5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
addq $0x1b, %r15
movq %r15, %rsi
callq 0xe3fa3
leaq -0x100(%rax), %rdx
movq %rdx, 0x18(%r14)
leaq 0xb6852(%rip), %rsi     # 0x118e35
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x10(%r14), %rax
movq %rax, %rsi
shlq $0x8, %rsi
movq %rsi, 0x20(%r14)
addq $-0x15, %rax
cmpq $-0x12, %rax
jb 0x62617
movq 0x18(%r14), %rax
addq %rsi, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x62660
movb $0x1, 0x30(%r14)
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpb $0x0, 0x30(%r14)
je 0x6263a
leaq 0xb5173(%rip), %rsi     # 0x1177a3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6392b
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdfd24
movq %r15, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xe0e8d
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
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x638d9
movl $0x0, %r12d
testb %bpl, %bpl
je 0x62622
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0xe0ae9
movq %rax, %r15
movq 0x20(%r14), %rdx
movq 0x28(%r14), %rsi
movq %rax, %rdi
callq 0xe0ecf
movq %r15, %rdi
callq 0xe0e95
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 0x62703
leaq 0xb5059(%rip), %rsi     # 0x11775a
jmp 0x62754
leaq 0xb5067(%rip), %rsi     # 0x117771
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq 0x28(%r14), %rsi
movq 0x18(%r14), %rdx
movq 0x20(%r14), %rcx
callq 0xe3cb7
movq %r15, %rdi
callq 0xe0e44
movq 0x20(%r14), %rdx
movq 0x28(%r14), %rsi
movq %r15, %rdi
callq 0xe0ecf
movq %r15, %rdi
callq 0xe0e95
cmpl %ebp, %eax
jne 0x62766
leaq 0xb502f(%rip), %rsi     # 0x117783
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
jmp 0x62622
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %r12
leaq 0xb4fd9(%rip), %rdx     # 0x117751
pushq $0x5
popq %r8
movq %rbx, %rdi
movq %rax, %rsi
xorl %ecx, %ecx
callq 0xdffbd
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x638d9
jmp 0x62622