// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/exectext.c
// Function: de_run_textlife
static void de_run_textlife(deark *c, de_module_params *mparams)
{
	struct textlife_ctx *tctx = NULL;
	lctx *d = NULL;

	d = create_lctx(c);
	tctx = de_malloc(c, sizeof(struct textlife_ctx));
	tctx->msgpfx = "[TextLife] ";
	tctx->ei = de_malloc(c, sizeof(struct fmtutil_exe_info));

	exectext_set_common_enc_opts(c, d, DE_ENCODING_CP437);

	// TextLife filters/optimizes the data before storing it. Known filters:
	// * Convert CR+LF to CR.
	// * Replace runs of 8 spaces with TAB.
	// I don't think extracting the raw bytes from the file is a useful thing
	// to do. So we always at least undo the filtering.
	// Files can contain C0 control characters used for special purposes (e.g.
	// italics), but I don't think they will cause serious problems with
	// encoding translation. So we just leave them as-is.
	if(d->proctype==ET_PROCTYPE_RAW) {
		d->proctype = ET_PROCTYPE_FMTCONV_ONLY;
	}

	d->chartypes[3] = ETCT_CONTROL;
	d->chartypes[4] = ETCT_CONTROL;
	d->chartypes[5] = ETCT_CONTROL;
	d->chartypes[6] = ETCT_CONTROL;
	d->chartypes[9] = ETCT_8SPACES;
	d->chartypes[0x0d] = ETCT_CRLF;
	d->chartypes[0x0f] = ETCT_CONTROL;
	d->chartypes[0x10] = ETCT_CONTROL;
	d->chartypes[0x11] = ETCT_CONTROL;

	fmtutil_collect_exe_info(c, c->infile, tctx->ei);

	tctx->edd.restrict_to_fmt = DE_SPECIALEXEFMT_TEXTLIFE;
	fmtutil_detect_specialexe(c, tctx->ei, &tctx->edd);
	if(tctx->edd.detected_fmt!=DE_SPECIALEXEFMT_TEXTLIFE) {
		d->need_errmsg = 1;
		goto done;
	}

	de_dbg(c, "text marker pos: %"I64_FMT, tctx->edd.special_pos_1);
	d->tpos = tctx->edd.special_pos_1+6;
	de_dbg(c, "tpos: %"I64_FMT, d->tpos);
	d->tlen = de_getu16le(tctx->edd.special_pos_1 - 2) - 1;
	de_dbg(c, "tlen: %"I64_FMT, d->tlen);

	exectext_extract_default(c, d);

done:
	if(d) {
		if(d->need_errmsg && tctx) {
			de_err(c, "%sBad or unsupported TextLife file", tctx->msgpfx);
		}
		de_free(c, d);
		d = NULL;
	}
	if(tctx) {
		de_free(c, tctx->ei);
		de_free(c, tctx);
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rdi, %rbx
movl $0x138, %esi            # imm = 0x138
callq 0xe8b2b
movq %rax, %r14
movq 0x18(%rbx), %rax
movq %rax, 0x20(%r14)
movl $0x88, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r15
leaq 0xc0bdb(%rip), %rax     # 0x123dc2
movq %rax, (%r15)
movl $0xf0, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, 0x8(%r15)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x63336
cmpl $0x1, 0x18(%r14)
jne 0x63215
movl $0x2, 0x18(%r14)
movl $0x1010101, 0x3b(%r14)  # imm = 0x1010101
movb $0x64, 0x41(%r14)
movb $0x66, 0x45(%r14)
movw $0x101, 0x47(%r14)      # imm = 0x101
movb $0x1, 0x49(%r14)
movq 0x18(%rbx), %rsi
movq 0x8(%r15), %rdx
movq %rbx, %rdi
callq 0x104f96
movq %r15, %rdx
addq $0x10, %rdx
movb $-0x33, 0x10(%r15)
movq 0x8(%r15), %rsi
movq %rbx, %rdi
callq 0x105864
cmpb $-0x33, 0x11(%r15)
jne 0x632d2
movq 0x30(%r15), %rdx
leaq 0xc0b61(%rip), %rsi     # 0x123dce
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x30(%r15), %rdx
addq $0x6, %rdx
movq %rdx, 0x28(%r14)
leaq 0xc09b6(%rip), %rsi     # 0x123c40
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq 0x30(%r15), %rsi
addq $-0x2, %rsi
callq 0xeebc8
leaq -0x1(%rax), %rdx
movq %rdx, 0x30(%r14)
leaq 0xc0778(%rip), %rsi     # 0x123a2c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movq %r14, %rsi
callq 0x633b6
cmpb $0x0, 0x13(%r14)
jne 0x632d7
jmp 0x632eb
movb $0x1, 0x13(%r14)
movq (%r15), %rdx
leaq 0xc0b02(%rip), %rsi     # 0x123de3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
movq 0x8(%r15), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq %rbx, %rdi
movq %r15, %rsi
popq %rbx
popq %r14
popq %r15
jmp 0xe8be2