// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/bintext.c
// Function: de_run_artworx_adf
static void de_run_artworx_adf(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	struct de_char_context *charctx = NULL;
	i64 data_start;
	i64 data_len;

	d = de_malloc(c, sizeof(lctx));

	// TODO: ADF files can probably have SAUCE records, so we should read
	// the SAUCE data if present. But there does not seem to be a defined
	// SAUCE file type for ADF.

	charctx = de_create_charctx(c, 0);
	charctx->prefer_image_output = 1;

	data_start = 1+192+4096;
	data_len = c->infile->len - data_start;
	if(data_len<0) goto done;

	d->csctx.width_in_chars = 80;
	d->csctx.height_in_chars = data_len / (d->csctx.width_in_chars*2);

	de_dbg(c, "guessed width: %d chars", (int)d->csctx.width_in_chars);
	de_dbg(c, "calculated height: %d chars", (int)d->csctx.height_in_chars);
	if(d->csctx.height_in_chars<1) goto done;
	d->has_palette = 0;
	d->has_font = 1;
	d->compression = 0;
	d->has_512chars = 0;
	d->csctx.nonblink = 1;

	do_read_palette(c, d, charctx, 1, 1);

	{
		// TODO: This duplicates a lot of the xbin code.

		d->font = de_create_bitmap_font(c);
		d->font->has_nonunicode_codepoints = 1;
		d->font->has_unicode_codepoints = 1;
		d->font->prefer_unicode = 0;
		d->font->num_chars = 256;
		d->font_height = 16;
		d->font_data_len = d->font->num_chars * d->font_height;

		do_read_font_data(c, d, 1+192);

		if(d->is_standard_font) {
			charctx->suppress_custom_font_warning = 1;
		}

		if(!do_generate_font(c, d)) goto done;

		if(c->extract_level>=2 && d->font) {
			de_font_decide_output_fmt(c);
			if(c->font_fmt_req!=DE_FONTFMT_IMAGE) {
				d->font->force_fontfile_output = 1;
			}
			do_extract_font(c, d);
		}

		charctx->font = d->font;
	}

	d->csctx.inf = c->infile;
	d->csctx.inf_pos = data_start;
	d->csctx.inf_len = data_len;
	fmtutil_char_simple_run(c, &d->csctx, charctx);

done:
	de_free_charctx(c, charctx);
	free_lctx(c, d);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %rbx
pushq $0x70
popq %rsi
callq 0xdecaf
movq %rax, %r12
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe88f4
movq %rax, %r15
movb $0x1, (%rax)
movq 0x18(%rbx), %rax
movq 0x18(%rax), %r13
cmpq $0x10c1, %r13           # imm = 0x10C1
jl 0x2fbac
addq $-0x10c1, %r13          # imm = 0xEF3F
movq $0x50, 0x50(%r14)
movl $0xa0, %ecx
movq %r13, %rax
xorl %edx, %edx
divq %rcx
movq %rax, 0x58(%r14)
leaq 0xda8cb(%rip), %rsi     # 0x10a36b
pushq $0x50
popq %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movl 0x58(%r14), %edx
leaq 0xda86f(%rip), %rsi     # 0x10a327
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
cmpq $0x0, 0x58(%r14)
jle 0x2fbac
movl $0x100, 0x2(%r14)       # imm = 0x100
movb $0x1, 0x4d(%r14)
pushq $0x1
popq %rdx
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x30a01
movq %rbx, %rdi
callq 0xe9d18
movq %rax, 0x28(%r14)
movw $0x101, 0x10(%rax)      # imm = 0x101
movb $0x0, 0x12(%rax)
movq $0x100, 0x18(%rax)      # imm = 0x100
movq $0x10, 0x8(%r14)
movq $0x1000, 0x10(%r14)     # imm = 0x1000
movl $0xc1, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30b47
cmpl $0x0, 0x20(%r14)
je 0x2fb35
movb $0x1, 0x4(%r15)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30c54
testl %eax, %eax
je 0x2fbac
addq $0x30, %r12
cmpl $0x2, 0x90(%rbx)
movq 0x28(%r14), %rax
jl 0x2fb86
testq %rax, %rax
je 0x2fb84
movq %rbx, %rdi
callq 0xeab2d
cmpb $0x3, 0xd5(%rbx)
je 0x2fb73
movq 0x28(%r14), %rax
movb $0x1, 0x13(%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30d49
movq 0x28(%r14), %rax
jmp 0x2fb86
xorl %eax, %eax
movq %rax, 0x60(%r15)
movq 0x18(%rbx), %rax
movq %rax, 0x30(%r14)
movq $0x10c1, 0x38(%r14)     # imm = 0x10C1
movq %r13, 0x40(%r14)
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xee2e5
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe89d2
movq %rbx, %rdi
movq %r14, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x30db6