// File: /workspace/github_c_makefile_star2_O1/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 0xe958c
movq %rbx, %rdi
callq 0xfdb0a
movq %rax, %r14
movl $0x1, 0x10(%rax)
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb7d1
movl %eax, 0x18(%r14)
movq %rsp, %r15
movq $0x0, (%r15)
leaq 0xb833e(%rip), %rsi     # 0x125b33
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef3a0
movq %rax, 0x20(%r14)
leaq 0xa6e37(%rip), %rsi     # 0x11465c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq $-0x10000, %rax         # imm = 0xFFFF0000
movq 0x20(%r14), %rcx
addq %rax, %rcx
cmpq %rax, %rcx
ja 0x6d85b
leaq 0xb82fc(%rip), %rsi     # 0x125b48
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x6db2f
movq 0x18(%rbx), %rdi
movq %rsp, %r12
movq %r12, %rsi
callq 0xef3a0
movq %rax, %r15
leaq 0xb82ea(%rip), %rsi     # 0x125b5e
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq (%r12), %r12
movq 0x20(%r14), %r13
shlq $0x5, %r13
leaq (%r12,%r13), %rax
movq %rax, 0x28(%r14)
leaq 0xb82d8(%rip), %rsi     # 0x125b81
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %rbx, %rdi
movl $0x10, %esi
callq 0xeba56
movq %rax, 0x30(%r14)
movq 0x18(%rbx), %rsi
movq %rax, %rdi
movq %r12, %rdx
movq %r13, %rcx
callq 0xebf09
movq 0x30(%r14), %rdi
callq 0xebe2a
movl %eax, %ebp
leaq 0xb829f(%rip), %rsi     # 0x125b97
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
cmpl %r15d, %ebp
je 0x6d91f
leaq 0xb82ac(%rip), %rsi     # 0x125bbc
movq %rbx, %rdi
movl %r15d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x28(%r14), %rdx
leaq 0xb82df(%rip), %rsi     # 0x125c16
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
cmpq $0x0, 0x20(%r14)
jle 0x6db2f
xorl %r13d, %r13d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xfda6c
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 0xe958c
movq 0x28(%r14), %r8
movq %r8, 0x40(%r15)
movl 0x10(%r15), %edx
movq 0x18(%r15), %rcx
movq %rbx, %rdi
leaq 0xb8291(%rip), %rsi     # 0x125c2e
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rbp
movq %rbp, %rsi
callq 0xef3a0
movq %rax, %r12
movq %rbx, %rdi
leaq 0xb8288(%rip), %rsi     # 0x125c57
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
movq 0x58(%r15), %rcx
movl 0x18(%r14), %r9d
movl $0xf, %edx
movl $0x1, %r8d
callq 0xf01b0
addq $0xf, 0x8(%rsp)
movq 0x58(%r15), %rdi
callq 0xf7557
movq %rbx, %rdi
leaq 0xcdfc1(%rip), %rsi     # 0x13b9d4
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xef14d
movzbl %al, %edx
movl %edx, 0x90(%r15)
movq %rbx, %rdi
leaq 0xb823f(%rip), %rsi     # 0x125c7c
xorl %eax, %eax
callq 0xe9226
movq %r15, %rdi
movq %rbp, %rsi
callq 0xfdb9b
movq %r15, %rdi
movq %rbp, %rsi
callq 0xfdbe0
movq 0x30(%r15), %rax
addq %rax, 0x28(%r14)
movq 0x50(%r15), %rsi
addq $0x18, %rsi
movq %r14, %rdi
leaq 0xaa62a(%rip), %rdx     # 0x11809e
movl $0x3, %ecx
movq %rbp, %r8
callq 0xfdc9b
movq %r15, %rdi
callq 0xfde12
testl %eax, %eax
je 0x6dafc
movq 0x30(%r14), %rdi
callq 0xebdc3
movq 0x30(%r14), %rdi
movq 0x18(%rbx), %rsi
movq 0x30(%r15), %rcx
movq 0x40(%r15), %rdx
callq 0xebf09
movq 0x30(%r14), %rdi
callq 0xebe2a
movl %eax, %ebp
movq %rbx, %rdi
leaq 0xb81cd(%rip), %rsi     # 0x125c8d
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
cmpl %r12d, %ebp
je 0x6dae4
movq %rbx, %rdi
leaq 0xb81dc(%rip), %rsi     # 0x125cb4
movl %r12d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe9df7
leaq 0x3b14(%rip), %rax      # 0x715ff
movq %rax, 0xa8(%r15)
movq %r15, %rdi
callq 0xfdec1
jmp 0x6db04
movl $0x1, 0x38(%r14)
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
movq %rbx, %rdi
movq %r15, %rsi
callq 0xfdab8
cmpl $0x0, 0x38(%r14)
jne 0x6db2f
incq %r13
cmpq 0x20(%r14), %r13
jl 0x6d94f
movq %rbx, %rdi
movq %r14, %rsi
callq 0xfdb25
movl 0x10(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq