// File: /workspace/github_c_makefile_star2_O2/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 0x14(%rsp), %rsi
callq 0xdebae
movq %rbx, %rdi
callq 0xf16ce
movq %rax, %r14
andl $0x0, 0x10(%rax)
pushq $0xd
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe08d2
movl %eax, 0x18(%r14)
pushq $0x23
popq %rsi
movq %rbx, %rdi
callq 0xe0ae9
movq %rax, 0x30(%r14)
pushq $0x11
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, 0x8(%rsp)
pushq $0x11
popq %rbp
leaq 0x4(%rsp), %r15
xorl %ecx, %ecx
xorl %r13d, %r13d
movq %r14, 0x18(%rsp)
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %rcx
jge 0x6c01c
movq %rcx, 0x20(%rsp)
testq %r13, %r13
je 0x6bdbd
movq %rbx, %rdi
movq %r13, %rsi
callq 0xf167c
movq 0x8(%rsp), %rdi
callq 0xe62cf
movq 0x20(%rsp), %rax
movq %rax, 0x30(%rsp)
andl $0x0, 0x4(%rsp)
subl $0x1, %ebp
jb 0x6be48
movq 0x18(%rbx), %rdi
leaq 0x30(%rsp), %r14
movq %r14, %rsi
callq 0xe3f2a
movl %eax, %r13d
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe3f2a
movl %eax, %r14d
movzbl %r13b, %edi
movq %r15, %r12
movq %r15, %rsi
callq 0xe11c4
cmpl $0x0, 0x4(%rsp)
jne 0x6c00f
movl %eax, %r13d
movzbl %r14b, %edi
movq %r12, %rsi
callq 0xe11c4
cmpl $0x0, 0x4(%rsp)
jne 0x6c00f
shlb $0x4, %r13b
orb %r13b, %al
movzbl %al, %esi
movq 0x8(%rsp), %rdi
callq 0xe5bc5
jmp 0x6bdd6
movq %rbx, %rdi
movq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0xf1630
movq %rax, %r13
movq 0x20(%rsp), %rdx
movq %rdx, 0x18(%rax)
movq $0x36, 0x20(%rax)
movq %rbx, %rdi
leaq 0xc29f7(%rip), %rsi     # 0x12e86d
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq 0x8(%rsp), %r12
movq %r12, 0x8(%r14)
andq $0x0, 0x28(%rsp)
movq 0x50(%r13), %rsi
addq $0x18, %rsi
movq %r14, %rdi
leaq 0x9f23d(%rip), %rdx     # 0x10b0e6
pushq $0x3
popq %rcx
leaq 0x28(%rsp), %rbp
movq %rbp, %r8
callq 0xf1866
movq %r13, %rdi
movq %rbp, %rsi
callq 0xf17ab
movq %r13, %rdi
movq %rbp, %rsi
callq 0xf1766
movq %r12, %rdi
movq %rbp, %rsi
callq 0xe3f6e
movq %rbx, %rdi
leaq 0xad33a(%rip), %rsi     # 0x11921e
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq %r12, %rdi
movq %rbp, %rsi
callq 0xe3f6e
movl %eax, 0x48(%r13)
movq %rbx, %rdi
leaq 0xad33d(%rip), %rsi     # 0x119243
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq %r12, %rdi
movq %rbp, %rsi
callq 0xe3f2a
movzbl %al, %edx
movl %edx, 0x90(%r13)
movq %rbx, %rdi
leaq 0xacd7e(%rip), %rsi     # 0x118cac
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %rdi, 0x8(%r14)
movq 0x18(%r13), %rsi
movq 0x58(%r13), %rcx
addq $0x22, %rsi
movl 0x18(%r14), %r9d
pushq $0xc
popq %rdx
pushq $0x1
popq %r8
callq 0xe4c9a
movq 0x58(%r13), %rdi
callq 0xeb877
movq %rbx, %rdi
leaq 0xc2af2(%rip), %rsi     # 0x12ea5e
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x20(%r13), %rdx
movq 0x30(%r13), %rcx
addq 0x18(%r13), %rdx
movq %rdx, 0x40(%r13)
movq %rbx, %rdi
leaq 0xad09b(%rip), %rsi     # 0x11902b
xorl %eax, %eax
callq 0xde896
movb $0x1, 0xa1(%r13)
movl 0x90(%r13), %edx
cmpl $0x2, %edx
je 0x6bfcb
cmpl $0x1, %edx
pushq $0x11
popq %rbp
jne 0x6bfe6
leaq 0x1b51(%rip), %rax      # 0x6db0b
movq %rax, 0xa8(%r13)
movq %r13, %rdi
callq 0xf1a85
jmp 0x6bff7
leaq 0x1ebf(%rip), %rax      # 0x6de91
movq %rax, 0xa8(%r13)
movq %r13, %rdi
callq 0xf1a85
pushq $0x11
popq %rbp
jmp 0x6bff7
movq %rbx, %rdi
leaq 0x9a802(%rip), %rsi     # 0x1067f2
xorl %eax, %eax
callq 0xdf2a5
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq 0x30(%r13), %rcx
addq 0x40(%r13), %rcx
jmp 0x6bd9a
movq 0x18(%rsp), %r14
movb $0x1, 0x14(%r14)
xorl %r13d, %r13d
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
testq %r13, %r13
je 0x6c038
movq %rbx, %rdi
movq %r13, %rsi
callq 0xf167c
cmpb $0x0, 0x14(%r14)
je 0x6c050
leaq 0xad222(%rip), %rsi     # 0x119268
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf16e7
movq 0x8(%rsp), %rdi
callq 0xe5591
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq