// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/misc3.c
// Function: de_run_lif_kdc
static void de_run_lif_kdc(deark *c, de_module_params *mparams)
{
	de_arch_lctx *d = NULL;
	i64 pos = 0;
	struct de_arch_member_data *md = NULL;
	dbuf *f2 = NULL;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	d = de_arch_create_lctx(c);
	d->is_le = 0;
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP437);
	d->crco = de_crcobj_create(c, DE_CRCOBJ_CRC16_IBM3740);
	f2 = dbuf_create_membuf(c, 17, 0);

	while(1) {
		i64 f2_pos;
		u32 crc1_reported;

		if(pos >= c->infile->len) goto done;
		if(md) {
			de_arch_destroy_md(c, md);
			md = NULL;
		}

		dbuf_empty(f2);
		// Decode the hex-encoded part of the header, so that we can read it
		// more easily.
		if(!lif_kdc_convert_hdr(c, pos, f2)) {
			d->need_errmsg = 1;
			goto done;
		}

		md = de_arch_create_md(c, d);
		md->member_hdr_pos = pos;
		md->member_hdr_size = 54;

		de_dbg(c, "member at %"I64_FMT, md->member_hdr_pos);
		de_dbg_indent(c, 1);

		d->inf = f2;
		f2_pos = 0;

		de_arch_read_field_dttm_p(d, &md->fi->timestamp[DE_TIMESTAMPIDX_MODIFY], "mod",
			DE_ARCH_TSTYPE_DOS_DT, &f2_pos);
		de_arch_read_field_cmpr_len_p(md, &f2_pos);
		de_arch_read_field_orig_len_p(md, &f2_pos);

		crc1_reported = (u32)dbuf_getu16be_p(f2, &f2_pos);
		de_dbg(c, "crc of cmpr. data (reported): 0x%04x", (UI)crc1_reported);
		md->crc_reported = (u32)dbuf_getu16be_p(f2, &f2_pos);
		de_dbg(c, "crc of orig. data (reported): 0x%04x", (UI)md->crc_reported);

		md->cmpr_meth = (UI)dbuf_getbyte_p(f2, &f2_pos);
		de_dbg(c, "cmpr. method: %u", md->cmpr_meth);
		d->inf = c->infile;

		pos = md->member_hdr_pos + 34;
		// TODO: How long is the filename field?
		dbuf_read_to_ucstring(c->infile, pos, 12, md->filename, DE_CONVFLAG_STOP_AT_NUL,
			d->input_encoding);
		de_dbg(c, "filename: \"%s\"", ucstring_getpsz_d(md->filename));

		md->cmpr_pos = md->member_hdr_pos + md->member_hdr_size;
		de_dbg(c, "compressed data at %"I64_FMT", len=%"I64_FMT, md->cmpr_pos, md->cmpr_len);

		md->validate_crc = 1;
		if(md->cmpr_meth==1) {
			md->dfn = noncompressed_decompressor_fn;
			de_arch_extract_member_file(md);
		}
		else if(md->cmpr_meth==2) {
			md->dfn = lif_method2_decompressor_fn;
			de_arch_extract_member_file(md);
		}
		else {
			de_err(c, "Unsupported compression: %u", (UI)md->cmpr_meth);
		}

		de_dbg_indent(c, -1);
		pos = md->cmpr_pos + md->cmpr_len;
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
	if(md) {
		de_arch_destroy_md(c, md);
		md = NULL;
	}
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "Bad or unsupported LIF file");
		}
		de_arch_destroy_lctx(c, d);
	}
	dbuf_close(f2);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
leaq 0x24(%rsp), %rsi
callq 0xe958c
movq %rbx, %rdi
callq 0xfdb0a
movq %rax, %r14
movl $0x0, 0x10(%rax)
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb7d1
movl %eax, 0x18(%r14)
movq %rbx, %rdi
movl $0x23, %esi
callq 0xeba56
movq %rax, 0x30(%r14)
movl $0x11, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf0faf
movq %rax, 0x10(%rsp)
leaq 0xc(%rsp), %r12
xorl %r15d, %r15d
xorl %ebp, %ebp
movq %r14, 0x28(%rsp)
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r15
jge 0x70057
testq %rbp, %rbp
je 0x6fe4f
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xfdab8
movq 0x10(%rsp), %rdi
callq 0xf1a5f
movq %r15, 0x30(%rsp)
movq %r15, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x11, %r13d
movq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0xef14d
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef14d
movl %eax, %r14d
movzbl %bpl, %edi
movq %r12, %r15
movq %r12, %rsi
callq 0xec1cc
cmpl $0x0, 0xc(%rsp)
jne 0x7005b
movl %eax, %ebp
movzbl %r14b, %edi
movq %r15, %rsi
callq 0xec1cc
cmpl $0x0, 0xc(%rsp)
jne 0x7005b
shlb $0x4, %bpl
orb %bpl, %al
movzbl %al, %esi
movq 0x10(%rsp), %rdi
callq 0xf11be
decl %r13d
jne 0x6fe71
movq %rbx, %rdi
movq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0xfda6c
movq %rax, %rbp
movq 0x30(%rsp), %rdx
movq %rdx, 0x18(%rax)
movq $0x36, 0x20(%rax)
movq %rbx, %rdi
leaq 0xcb8d6(%rip), %rsi     # 0x13b7e3
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x10(%rsp), %r15
movq %r15, 0x8(%r14)
movq $0x0, 0x18(%rsp)
movq 0x50(%rbp), %rsi
addq $0x18, %rsi
movq %r14, %rdi
leaq 0xa8159(%rip), %rdx     # 0x11809e
movl $0x3, %ecx
leaq 0x18(%rsp), %r13
movq %r13, %r8
callq 0xfdc9b
movq %rbp, %rdi
movq %r13, %rsi
callq 0xfdbe0
movq %rbp, %rdi
movq %r13, %rsi
callq 0xfdb9b
movq %r15, %rdi
movq %r13, %rsi
callq 0xef193
movq %rbx, %rdi
leaq 0xb626c(%rip), %rsi     # 0x1261ee
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq %r15, %rdi
movq %r13, %rsi
callq 0xef193
movl %eax, 0x48(%rbp)
movq %rbx, %rdi
leaq 0xb6270(%rip), %rsi     # 0x126213
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq %r15, %rdi
movq %r13, %rsi
callq 0xef14d
movzbl %al, %edx
movl %edx, 0x90(%rbp)
movq %rbx, %rdi
leaq 0xb5cb2(%rip), %rsi     # 0x125c7c
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %rdi, 0x8(%r14)
movq 0x18(%rbp), %rsi
movq 0x58(%rbp), %rcx
addq $0x22, %rsi
movl 0x18(%r14), %r9d
movl $0xc, %edx
movl $0x1, %r8d
callq 0xf01b0
movq 0x58(%rbp), %rdi
callq 0xf7557
movq %rbx, %rdi
leaq 0xcb9c8(%rip), %rsi     # 0x13b9d4
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x20(%rbp), %rdx
movq 0x30(%rbp), %rcx
addq 0x18(%rbp), %rdx
movq %rdx, 0x40(%rbp)
movq %rbx, %rdi
leaq 0xb5fcb(%rip), %rsi     # 0x125ffb
xorl %eax, %eax
callq 0xe9226
movb $0x1, 0xa1(%rbp)
movl 0x90(%rbp), %edx
cmpl $0x1, %edx
je 0x70073
cmpl $0x2, %edx
jne 0x7008b
leaq 0x1fa4(%rip), %rax      # 0x71ff9
jmp 0x7007a
xorl %eax, %eax
jmp 0x700b3
movq 0x28(%rsp), %r14
movb $0x1, 0x14(%r14)
xorl %ebp, %ebp
xorl %eax, %eax
movq %r15, %r12
movq 0x30(%rsp), %r15
jmp 0x700b3
leaq 0x1c15(%rip), %rax      # 0x71c8f
movq %rax, 0xa8(%rbp)
movq %rbp, %rdi
callq 0xfdec1
jmp 0x7009c
movq %rbx, %rdi
leaq 0xa370d(%rip), %rsi     # 0x1137a2
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x30(%rbp), %r15
addq 0x40(%rbp), %r15
movb $0x1, %al
testb %al, %al
jne 0x6fe31
movl 0x24(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
testq %rbp, %rbp
je 0x700d7
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xfdab8
testq %r14, %r14
je 0x700ff
cmpb $0x0, 0x14(%r14)
je 0x700f4
leaq 0xb614e(%rip), %rsi     # 0x126238
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movq %r14, %rsi
callq 0xfdb25
movq 0x10(%rsp), %rdi
callq 0xf0b13
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq