// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/misc3.c
// Function: de_run_cpshrink
static void de_run_cpshrink(deark *c, de_module_params *mparams)
{
	de_arch_lctx *d = NULL;
	i64 pos;
	i64 member_hdrs_pos;
	i64 member_hdrs_len;
	u32 member_hdrs_crc_reported;
	u32 member_hdrs_crc_calc;
	i64 i;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	d = de_arch_create_lctx(c);
	d->is_le = 1;
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP437);

	pos = 0;
	de_dbg(c, "archive header at %d", (int)pos);
	de_dbg_indent(c, 1);
	// Not sure if this is a 16-bit, or 32-bit, field, but CP Shrink doesn't
	// work right if the 2 bytes at offset 2 are not 0.
	d->num_members = de_getu32le_p(&pos);
	de_dbg(c, "number of members: %"I64_FMT, d->num_members);
	if(d->num_members<1 || d->num_members>0xffff) {
		de_err(c, "Bad member file count");
		goto done;
	}
	member_hdrs_crc_reported = (u32)de_getu32le_p(&pos);
	de_dbg(c, "member hdrs crc (reported): 0x%08x", (UI)member_hdrs_crc_reported);
	de_dbg_indent(c, -1);

	member_hdrs_pos = pos;
	member_hdrs_len = d->num_members * 32;
	d->cmpr_data_curpos = member_hdrs_pos+member_hdrs_len;

	de_dbg(c, "member headers at %"I64_FMT, member_hdrs_pos);
	de_dbg_indent(c, 1);
	d->crco = de_crcobj_create(c, DE_CRCOBJ_CRC32_IEEE);
	de_crcobj_addslice(d->crco, c->infile, member_hdrs_pos, member_hdrs_len);
	member_hdrs_crc_calc = de_crcobj_getval(d->crco);
	de_dbg(c, "member hdrs crc (calculated): 0x%08x", (UI)member_hdrs_crc_calc);
	if(member_hdrs_crc_calc!=member_hdrs_crc_reported) {
		de_err(c, "Header CRC check failed (expected 0x%08x, got 0x%08x). "
			"This is not a valid CP Shrink file", (UI)member_hdrs_crc_reported,
			(UI)member_hdrs_crc_calc);
	}
	de_dbg_indent(c, -1);

	de_dbg(c, "cmpr data starts at %"I64_FMT, d->cmpr_data_curpos);

	for(i=0; i<d->num_members; i++) {
		struct de_arch_member_data *md;

		md = de_arch_create_md(c, d);
		md->member_idx = i;
		md->member_hdr_pos = pos;
		pos += 32;

		cpshrink_do_member(c, d, md);
		de_arch_destroy_md(c, md);
		if(d->fatalerrflag) goto done;
	}

done:
	de_arch_destroy_lctx(c, d);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
leaq 0x18(%rsp), %rsi
callq 0xdebae
movq %rbx, %rdi
callq 0xf16ce
movq %rax, %r14
movl $0x1, 0x10(%rax)
pushq $0xd
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe08d2
movl %eax, 0x18(%r14)
leaq 0x8(%rsp), %r15
andq $0x0, (%r15)
leaq 0xaf249(%rip), %rsi     # 0x118b63
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe40bf
movq %rax, 0x20(%r14)
leaq 0x9dd64(%rip), %rsi     # 0x1076ac
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq $-0x10000, %rax         # imm = 0xFFFF0000
movq 0x20(%r14), %rcx
addq %rax, %rcx
cmpq %rax, %rcx
ja 0x6997e
leaq 0xaf209(%rip), %rsi     # 0x118b78
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x69c42
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0xe40bf
movq %rax, %r15
leaq 0xaf1f5(%rip), %rsi     # 0x118b8e
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq (%r12), %r12
movq 0x20(%r14), %r13
shlq $0x5, %r13
leaq (%r12,%r13), %rax
movq %rax, 0x28(%r14)
leaq 0xaf1e5(%rip), %rsi     # 0x118bb1
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0xe0ae9
movq %rax, 0x30(%r14)
movq 0x18(%rbx), %rsi
movq %rax, %rdi
movq %r12, %rdx
movq %r13, %rcx
callq 0xe0f33
movq 0x30(%r14), %rdi
callq 0xe0e95
movl %eax, %ebp
leaq 0xaf1b0(%rip), %rsi     # 0x118bc7
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
cmpl %r15d, %ebp
je 0x69a3e
leaq 0xaf1bd(%rip), %rsi     # 0x118bec
movq %rbx, %rdi
movl %r15d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xdf2a5
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x28(%r14), %rdx
leaq 0xaf1f2(%rip), %rsi     # 0x118c46
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
xorl %r13d, %r13d
cmpq 0x20(%r14), %r13
jge 0x69c42
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf1630
movq %rax, %r15
movq %r13, 0x10(%rax)
movq 0x8(%rsp), %rax
movq %rax, 0x18(%r15)
leaq 0x20(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x1c(%rsp), %rsi
callq 0xdebae
movq 0x28(%r14), %r8
movq %r8, 0x40(%r15)
movl 0x10(%r15), %edx
movq 0x18(%r15), %rcx
movq %rbx, %rdi
leaq 0xaf1a3(%rip), %rsi     # 0x118c5e
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rbp
movl %ebp, %esi
callq 0xdeba7
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0xe40bf
movq %rbx, %rdi
leaq 0xaf19d(%rip), %rsi     # 0x118c87
movq %rax, 0x20(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq 0x58(%r15), %rcx
movl 0x18(%r14), %r9d
pushq $0xf
popq %rdx
movl %ebp, %r8d
callq 0xe4c9a
addq $0xf, 0x10(%rsp)
movq 0x58(%r15), %rdi
callq 0xeb877
movq %rbx, %rdi
leaq 0xc4f31(%rip), %rsi     # 0x12ea5e
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f2a
movzbl %al, %edx
movl %edx, 0x90(%r15)
movq %rbx, %rdi
leaq 0xaf155(%rip), %rsi     # 0x118cac
xorl %eax, %eax
callq 0xde896
movq %r15, %rdi
movq %r12, %rsi
callq 0xf1766
movq %r15, %rdi
movq %r12, %rsi
callq 0xf17ab
movq 0x30(%r15), %rax
addq %rax, 0x28(%r14)
movq 0x50(%r15), %rsi
addq $0x18, %rsi
movq %r14, %rdi
leaq 0xa1558(%rip), %rdx     # 0x10b0e6
pushq $0x3
popq %rcx
movq %r12, %r8
callq 0xf1866
movq %r15, %rdi
callq 0xf19d5
testl %eax, %eax
je 0x69c15
movq 0x30(%r14), %rdi
callq 0xe0e44
movq 0x30(%r14), %rdi
movq 0x18(%rbx), %rsi
movq 0x30(%r15), %rcx
movq 0x40(%r15), %rdx
callq 0xe0f33
movq 0x30(%r14), %rdi
callq 0xe0e95
movl %eax, %ebp
movq %rbx, %rdi
leaq 0xaf0e5(%rip), %rsi     # 0x118cbd
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x20(%rsp), %rdx
cmpl %edx, %ebp
je 0x69bfd
movq %rbx, %rdi
leaq 0xaf0f0(%rip), %rsi     # 0x118ce4
movl %ebp, %ecx
xorl %eax, %eax
callq 0xdf2a5
leaq 0x384b(%rip), %rax      # 0x6d44f
movq %rax, 0xa8(%r15)
movq %r15, %rdi
callq 0xf1a85
jmp 0x69c1d
movl $0x1, 0x38(%r14)
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movq %rbx, %rdi
movq %r15, %rsi
callq 0xf167c
incq %r13
cmpl $0x0, 0x38(%r14)
je 0x69a61
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf16e7
movl 0x18(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq