// File: /workspace/github_c_makefile_star2_O1/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
movl $0x138, %esi            # imm = 0x138
callq 0xe969b
movq %rax, %r14
movq 0x18(%rbx), %rax
movq %rax, 0x20(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x634f2
leaq 0xc0a2d(%rip), %rsi     # 0x123b8e
movq %rbx, %rdi
callq 0xeb6d6
movb $0x1, %al
movb %al, 0x42(%r14)
movb %al, 0x45(%r14)
movq 0x18(%rbx), %rdi
leaq 0xc0a28(%rip), %rdx     # 0x123ba6
movl $0x32, %esi
movl $0x8, %ecx
callq 0xf025e
testl %eax, %eax
jne 0x631af
movq 0x18(%rbx), %rdi
leaq 0xc0a13(%rip), %rdx     # 0x123baf
movl $0x800, %esi            # imm = 0x800
movl $0x8, %ecx
callq 0xf025e
testl %eax, %eax
je 0x631f2
movb $0x1, 0x13(%r14)
xorl %r15d, %r15d
testq %r14, %r14
je 0x631df
cmpb $0x0, 0x13(%r14)
je 0x631d4
leaq 0xc09ee(%rip), %rsi     # 0x123bb8
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xf0b13
movb $0x1, 0x15(%r14)
movq $0xc3c, 0x28(%r14)      # imm = 0xC3C
leaq 0xc085a(%rip), %rsi     # 0x123a60
xorl %r15d, %r15d
movl $0xc3c, %edx            # imm = 0xC3C
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0x30, %esi
callq 0xef1ca
movl $0xf400, %edx           # imm = 0xF400
subq %rax, %rdx
movq %rdx, 0x30(%r14)
leaq 0xc0613(%rip), %rsi     # 0x12384c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6373a
cmpb $0x0, 0x12(%r14)
jne 0x631b7
movq 0x30(%r14), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf0faf
movq %rax, %r15
movq %rax, %rdi
callq 0xf0e3f
cmpq $0x0, 0x30(%r14)
jle 0x632a2
xorl %r12d, %r12d
movq 0x18(%rbx), %rdi
movq 0x28(%r14), %rsi
addq %r12, %rsi
callq 0xef0d5
xorb $0x1, %al
movzbl %al, %esi
movq %r15, %rdi
callq 0xf11be
incq %r12
cmpq 0x30(%r14), %r12
jl 0x6327c
movq %r15, %rdi
callq 0xf0ea4
movq %r15, 0x20(%r14)
movq $0x0, 0x28(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x63581
jmp 0x631b7