// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/exectext.c
// Function: de_run_ascom
static void de_run_ascom(deark *c, de_module_params *mparams)
{
	i64 n;
	lctx *d = NULL;
	dbuf *tmpdbuf = NULL;

	d = create_lctx(c);
	exectext_set_common_enc_opts(c, d, DE_ENCODING_CP437);
	de_declare_fmt(c, "ASCOM (executable text)");
	d->chartypes[10] = ETCT_CONTROL;
	d->chartypes[13] = ETCT_CONTROL;

	// Some arbitrary tests, to try to make sure we have the right version
	if(dbuf_memcmp(c->infile, 50, (const u8*)"\x72\x41\xc2\xfe\xd1\x7f\xca\xfe", 8) ||
		dbuf_memcmp(c->infile, 2048, (const u8*)"\xee\x40\x20\xf2\x47\xae\xfe\x43", 8))
	{
		d->need_errmsg = 1;
		goto done;
	}

	d->is_encrypted = 1;
	// TODO?: Figure out how to read this offset from the file.
	d->tpos = 3132;
	de_dbg(c, "tpos: %"I64_FMT, d->tpos);
	n = de_getu16le(48);
	d->tlen = 0xf400 - n;
	de_dbg(c, "tlen: %"I64_FMT, d->tlen);
	exectext_check_tpos(c, d);
	if(d->errflag) goto done;

	// Note: The heading is around offset 3040, XORed with 0xfe.

	tmpdbuf = dbuf_create_membuf(c, d->tlen, 0);
	dbuf_enable_wbuffer(tmpdbuf);
	ascom_decrypt(c, d, tmpdbuf);
	d->inf = tmpdbuf;
	d->tpos = 0;

	exectext_extract_default(c, d);

done:
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "Not an ASCOM file, or unsupported version");
		}
		destroy_lctx(c, d);
	}
	dbuf_close(tmpdbuf);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
callq 0x5fc9f
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5fcb7
leaq 0xb7235(%rip), %rsi     # 0x116bae
movq %rbx, %rdi
callq 0xe07f7
movb $0x1, %al
movb %al, 0x42(%r14)
movb %al, 0x45(%r14)
movq 0x18(%rbx), %rdi
leaq 0xb7230(%rip), %rdx     # 0x116bc6
pushq $0x32
popq %rsi
pushq $0x8
popq %rcx
callq 0xe4d48
testl %eax, %eax
jne 0x5f9c1
movq 0x18(%rbx), %rdi
leaq 0xb721f(%rip), %rdx     # 0x116bcf
pushq $0x8
popq %rcx
movl $0x800, %esi            # imm = 0x800
callq 0xe4d48
testl %eax, %eax
je 0x5f9ff
movb $0x1, 0x13(%r14)
xorl %r15d, %r15d
cmpb $0x0, 0x13(%r14)
je 0x5f9e1
leaq 0xb7201(%rip), %rsi     # 0x116bd8
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xe5591
movb $0x1, 0x15(%r14)
movq $0xc3c, 0x28(%r14)      # imm = 0xC3C
leaq 0xb706d(%rip), %rsi     # 0x116a80
xorl %r15d, %r15d
movl $0xc3c, %edx            # imm = 0xC3C
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0x30
popq %rsi
callq 0xe3fa3
movl $0xf400, %edx           # imm = 0xF400
subq %rax, %rdx
movq %rdx, 0x30(%r14)
leaq 0xb6e28(%rip), %rsi     # 0x11686c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5fefd
cmpb $0x0, 0x12(%r14)
jne 0x5f9c9
movq 0x30(%r14), %rsi
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r15
movq %rax, %rdi
callq 0xe589c
cmpq 0x30(%r14), %r12
jge 0x5faa8
movq 0x18(%rbx), %rdi
movq 0x28(%r14), %rsi
addq %r12, %rsi
callq 0xe3eb4
xorb $0x1, %al
movzbl %al, %esi
movq %r15, %rdi
callq 0xe5bc5
incq %r12
jmp 0x5fa80
movq %r15, %rdi
callq 0xe58e8
movq %r15, 0x20(%r14)
andq $0x0, 0x28(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5fd39
jmp 0x5f9c9