// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8dcb0
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0xd, %edx
callq 0x8dcf0
movq -0x8(%rbp), %rdi
leaq 0x121ed5(%rip), %rsi    # 0x1af69f
callq 0x15e380
movq -0x20(%rbp), %rax
movb $0x1, 0x42(%rax)
movq -0x20(%rbp), %rax
movb $0x1, 0x45(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x32, %esi
leaq 0x121ec4(%rip), %rdx    # 0x1af6b7
movl $0x8, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0x8d825
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x800, %esi            # imm = 0x800
leaq 0x121eaa(%rip), %rdx    # 0x1af6c0
movl $0x8, %ecx
callq 0x166ab0
cmpl $0x0, %eax
je 0x8d832
movq -0x20(%rbp), %rax
movb $0x1, 0x13(%rax)
jmp 0x8d910
movq -0x20(%rbp), %rax
movb $0x1, 0x15(%rax)
movq -0x20(%rbp), %rax
movq $0xc3c, 0x28(%rax)      # imm = 0xC3C
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0x121d18(%rip), %rsi    # 0x1af571
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x30, %esi
callq 0x165280
movq %rax, -0x18(%rbp)
movl $0xf400, %ecx           # imm = 0xF400
subq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rdx
leaq 0x121ac3(%rip), %rsi    # 0x1af35d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x8e1d0
movq -0x20(%rbp), %rax
cmpb $0x0, 0x12(%rax)
je 0x8d8ba
jmp 0x8d910
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rsi
xorl %edx, %edx
callq 0x168020
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x8fb80
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x8df30
cmpq $0x0, -0x20(%rbp)
je 0x8d940
movq -0x20(%rbp), %rax
cmpb $0x0, 0x13(%rax)
je 0x8d933
movq -0x8(%rbp), %rdi
leaq 0x121d9d(%rip), %rsi    # 0x1af6c9
movb $0x0, %al
callq 0x15bfb0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x8e0b0
movq -0x28(%rbp), %rdi
callq 0x1676a0
addq $0x30, %rsp
popq %rbp
retq
nop