// File: /workspace/github_c_makefile_star2_O3/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 0x8(%rsp), %rsi
callq 0xe8a1c
movq %rbx, %rdi
callq 0xfd4b2
movq %rax, %r12
movl $0x0, 0x10(%rax)
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb220
movl %eax, 0x18(%r12)
movq %rbx, %rdi
movl $0x23, %esi
callq 0xeb499
movq %rax, 0x30(%r12)
movl $0x11, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, 0x10(%rsp)
movq 0x18(%rbx), %rax
cmpq $0x0, 0x18(%rax)
jle 0x6fe6c
leaq 0xc(%rsp), %r14
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %r12, 0x18(%rsp)
testq %rbp, %rbp
je 0x6fbf1
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xfd460
movq 0x10(%rsp), %rdi
callq 0xf145f
movq %r15, 0x20(%rsp)
movq %r15, 0x30(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x11, %r12d
movq 0x18(%rbx), %rdi
leaq 0x30(%rsp), %r13
movq %r13, %rsi
callq 0xeeb4b
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb4b
movl %eax, %r13d
movzbl %bpl, %edi
movq %r14, %r15
movq %r14, %rsi
callq 0xebbbc
cmpl $0x0, 0xc(%rsp)
jne 0x6fe61
movl %eax, %ebp
movzbl %r13b, %edi
movq %r15, %rsi
callq 0xebbbc
cmpl $0x0, 0xc(%rsp)
jne 0x6fe61
shlb $0x4, %bpl
orb %bpl, %al
movzbl %al, %esi
movq 0x10(%rsp), %rdi
callq 0xf0bd6
decl %r12d
jne 0x6fc13
movq %rbx, %rdi
movq 0x18(%rsp), %r12
movq %r12, %rsi
callq 0xfd414
movq %rax, %rbp
movq 0x20(%rsp), %rdx
movq %rdx, 0x18(%rax)
movq $0x36, 0x20(%rax)
movq %rbx, %rdi
leaq 0xcbeae(%rip), %rsi     # 0x13bb5d
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x10(%rsp), %r15
movq %r15, 0x8(%r12)
movq $0x0, 0x28(%rsp)
movq 0x50(%rbp), %rsi
addq $0x18, %rsi
movq %r12, %rdi
leaq 0xa84ee(%rip), %rdx     # 0x1181d6
movl $0x3, %ecx
leaq 0x28(%rsp), %r13
movq %r13, %r8
callq 0xfd638
movq %rbp, %rdi
movq %r13, %rsi
callq 0xfd57d
movq %rbp, %rdi
movq %r13, %rsi
callq 0xfd538
movq %r15, %rdi
movq %r13, %rsi
callq 0xeeb91
movq %rbx, %rdi
leaq 0xb66f9(%rip), %rsi     # 0x12641e
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
movq %r13, %rsi
callq 0xeeb91
movl %eax, 0x48(%rbp)
movq %rbx, %rdi
leaq 0xb66fd(%rip), %rsi     # 0x126443
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
movq %r13, %rsi
callq 0xeeb4b
movzbl %al, %edx
movl %edx, 0x90(%rbp)
movq %rbx, %rdi
leaq 0xb613f(%rip), %rsi     # 0x125eac
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %rdi, 0x8(%r12)
movq 0x18(%rbp), %rsi
movq 0x58(%rbp), %rcx
addq $0x22, %rsi
movl 0x18(%r12), %r9d
movl $0xc, %edx
movl $0x1, %r8d
callq 0xefbd7
movq 0x58(%rbp), %rdi
callq 0xf70f6
movq %rbx, %rdi
leaq 0xcbf9d(%rip), %rsi     # 0x13bd4e
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x20(%rbp), %rdx
movq 0x30(%rbp), %rcx
addq 0x18(%rbp), %rdx
movq %rdx, 0x40(%rbp)
movq %rbx, %rdi
leaq 0xb6456(%rip), %rsi     # 0x12622b
xorl %eax, %eax
callq 0xe8617
movb $0x1, 0xa1(%rbp)
movl 0x90(%rbp), %edx
cmpl $0x2, %edx
je 0x6fdfc
cmpl $0x1, %edx
jne 0x6fe14
leaq 0x1bfb(%rip), %rax      # 0x719f5
jmp 0x6fe03
leaq 0x1f57(%rip), %rax      # 0x71d5a
movq %rax, 0xa8(%rbp)
movq %rbp, %rdi
callq 0xfd85e
jmp 0x6fe25
movq %rbx, %rdi
leaq 0xa3a34(%rip), %rsi     # 0x113852
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x30(%rbp), %r15
addq 0x40(%rbp), %r15
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r15
jl 0x6fbe1
movl 0x8(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xfd460
jmp 0x6fe78
movq 0x18(%rsp), %r12
movb $0x1, 0x14(%r12)
movl 0x8(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
cmpb $0x0, 0x14(%r12)
je 0x6fe91
leaq 0xb65e1(%rip), %rsi     # 0x126468
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movq %r12, %rsi
callq 0xfd4cd
movq 0x10(%rsp), %rdi
callq 0xf052b
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq