// File: /workspace/github_c_makefile_star2_O3/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 $0x18, %rsp
movq %rdi, %rbx
leaq 0x10(%rsp), %rsi
callq 0xe8a1c
movq %rbx, %rdi
callq 0xfd4b2
movq %rax, %r14
movl $0x1, 0x10(%rax)
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb220
movl %eax, 0x18(%r14)
movq %rsp, %r15
movq $0x0, (%r15)
leaq 0xb879e(%rip), %rsi     # 0x125d63
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeed83
movq %rax, 0x20(%r14)
leaq 0xa7142(%rip), %rsi     # 0x114737
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq $-0x10000, %rax         # imm = 0xFFFF0000
movq 0x20(%r14), %rcx
addq %rax, %rcx
cmpq %rax, %rcx
ja 0x6d62b
leaq 0xb875c(%rip), %rsi     # 0x125d78
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x6d8ff
movq 0x18(%rbx), %rdi
movq %rsp, %r12
movq %r12, %rsi
callq 0xeed83
movq %rax, %r15
leaq 0xb874a(%rip), %rsi     # 0x125d8e
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq (%r12), %r12
movq 0x20(%r14), %r13
shlq $0x5, %r13
leaq (%r12,%r13), %rax
movq %rax, 0x28(%r14)
leaq 0xb8738(%rip), %rsi     # 0x125db1
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
movl $0x10, %esi
callq 0xeb499
movq %rax, 0x30(%r14)
movq 0x18(%rbx), %rsi
movq %rax, %rdi
movq %r12, %rdx
movq %r13, %rcx
callq 0xeb944
movq 0x30(%r14), %rdi
callq 0xeb865
movl %eax, %ebp
leaq 0xb86ff(%rip), %rsi     # 0x125dc7
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
cmpl %r15d, %ebp
je 0x6d6ef
leaq 0xb870c(%rip), %rsi     # 0x125dec
movq %rbx, %rdi
movl %r15d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x28(%r14), %rdx
leaq 0xb873f(%rip), %rsi     # 0x125e46
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpq $0x0, 0x20(%r14)
jle 0x6d8ff
xorl %r13d, %r13d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xfd414
movq %rax, %r15
movq %r13, 0x10(%rax)
movq (%rsp), %rax
movq %rax, 0x18(%r15)
leaq 0x20(%rax), %rcx
movq %rcx, (%rsp)
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
leaq 0x14(%rsp), %rsi
callq 0xe8a1c
movq 0x28(%r14), %r8
movq %r8, 0x40(%r15)
movl 0x10(%r15), %edx
movq 0x18(%r15), %rcx
movq %rbx, %rdi
leaq 0xb86f1(%rip), %rsi     # 0x125e5e
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rbp
movq %rbp, %rsi
callq 0xeed83
movq %rax, %r12
movq %rbx, %rdi
leaq 0xb86e8(%rip), %rsi     # 0x125e87
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
movq 0x58(%r15), %rcx
movl 0x18(%r14), %r9d
movl $0xf, %edx
movl $0x1, %r8d
callq 0xefbd7
addq $0xf, 0x8(%rsp)
movq 0x58(%r15), %rdi
callq 0xf70f6
movq %rbx, %rdi
leaq 0xce56b(%rip), %rsi     # 0x13bd4e
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeeb4b
movzbl %al, %edx
movl %edx, 0x90(%r15)
movq %rbx, %rdi
leaq 0xb869f(%rip), %rsi     # 0x125eac
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
movq %rbp, %rsi
callq 0xfd538
movq %r15, %rdi
movq %rbp, %rsi
callq 0xfd57d
movq 0x30(%r15), %rax
addq %rax, 0x28(%r14)
movq 0x50(%r15), %rsi
addq $0x18, %rsi
movq %r14, %rdi
leaq 0xaa992(%rip), %rdx     # 0x1181d6
movl $0x3, %ecx
movq %rbp, %r8
callq 0xfd638
movq %r15, %rdi
callq 0xfd7af
testl %eax, %eax
je 0x6d8cc
movq 0x30(%r14), %rdi
callq 0xeb7fe
movq 0x30(%r14), %rdi
movq 0x18(%rbx), %rsi
movq 0x30(%r15), %rcx
movq 0x40(%r15), %rdx
callq 0xeb944
movq 0x30(%r14), %rdi
callq 0xeb865
movl %eax, %ebp
movq %rbx, %rdi
leaq 0xb862d(%rip), %rsi     # 0x125ebd
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
cmpl %r12d, %ebp
je 0x6d8b4
movq %rbx, %rdi
leaq 0xb863c(%rip), %rsi     # 0x125ee4
movl %r12d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe9295
leaq 0x3a96(%rip), %rax      # 0x71351
movq %rax, 0xa8(%r15)
movq %r15, %rdi
callq 0xfd85e
jmp 0x6d8d4
movl $0x1, 0x38(%r14)
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movq %rbx, %rdi
movq %r15, %rsi
callq 0xfd460
cmpl $0x0, 0x38(%r14)
jne 0x6d8ff
incq %r13
cmpq 0x20(%r14), %r13
jl 0x6d71f
movq %rbx, %rdi
movq %r14, %rsi
callq 0xfd4cd
movl 0x10(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq