// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/packit.c
// Function: packit_do_huffman_cmpr_member
static void packit_do_huffman_cmpr_member(deark *c, struct packit_ctx *d,
	struct packit_md *pmd)
{
	int saved_indent_level;
	struct de_dfilter_in_params dcmpri;
	struct de_dfilter_out_params dcmpro;
	struct de_dfilter_results dres;

	de_dbg_indent_save(c, &saved_indent_level);
	pmd->unc_data = dbuf_create_membuf(c, 0, 0);

	// This is one of those irritating formats where we know neither the size of
	// the compressed data, nor the size of the decompressed data.
	// And the compression format isn't even self-terminating.
	// We're supposed to decompress until we've reached 94 decompressed
	// bytes (the header). Then decode those bytes to figure out how many more
	// bytes of decompressed data there are, then continue the decompression
	// where we left off.
	// Unfortunately, we don't have a good way to do that, and it's too much
	// trouble to implement one.
	// So we'll decompress just the header, decode it, then start over and
	// decompress everything.

	// Decompress the header
	de_dfilter_init_objects(c, &dcmpri, &dcmpro, &dres);
	dcmpri.f = c->infile;
	dcmpri.pos = pmd->marker_pos+4;
	dcmpri.len = c->infile->len - dcmpri.pos;
	dcmpro.f = pmd->unc_data;
	dcmpro.len_known = 1;
	dcmpro.expected_len = 94;

	de_dbg(c, "decompressing (pass 1)");
	de_dbg_indent(c, 1);
	fmtutil_huff_packit_codectype1(c, &dcmpri, &dcmpro, &dres, NULL);
	dbuf_flush(dcmpro.f);
	if(dres.errcode) {
		de_err(c, "Decompression failed: %s", de_dfilter_get_errmsg(c, &dres));
		d->fatalerrflag = 1;
		goto done;
	}
	de_dbg_indent(c, -1);

	// Decode the header
	pmd->inf = pmd->unc_data;
	pmd->hdr_pos_in_inf = 0;
	packit_read_member_header(c, d, pmd);
	if(d->fatalerrflag) goto done;

#define PACKIT_MAX_FILE_DATA 50000000
	if(pmd->dfork_len + pmd->rfork_len > PACKIT_MAX_FILE_DATA) {
		d->fatalerrflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	// Decompress everything
	de_dfilter_init_objects(c, &dcmpri, &dcmpro, &dres);
	dbuf_empty(pmd->unc_data);
	dcmpri.f = c->infile;
	dcmpri.pos = pmd->marker_pos+4;
	dcmpri.len = c->infile->len - dcmpri.pos;
	dcmpro.f = pmd->unc_data;
	dcmpro.len_known = 1;
	dcmpro.expected_len = 94 + pmd->dfork_len + pmd->rfork_len + 2;

	de_dbg(c, "decompressing (pass 2)");
	de_dbg_indent(c, 1);
	fmtutil_huff_packit_codectype1(c, &dcmpri, &dcmpro, &dres, NULL);
	dbuf_flush(dcmpro.f);
	if(dres.errcode) {
		de_err(c, "Decompression failed: %s", de_dfilter_get_errmsg(c, &dres));
		d->fatalerrflag = 1;
		goto done;
	}
	if(!dres.bytes_consumed_valid) {
		d->fatalerrflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	de_dbg(c, "decompressed %"I64_FMT" bytes to %"I64_FMT,
		dres.bytes_consumed, pmd->unc_data->len);
	pmd->total_encoded_size = 4 + dres.bytes_consumed;

	de_dbg_indent(c, -1);

	// Validate what we decompressed
	packit_scan_member_data(c, d, pmd);

done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x168020
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xe8(%rax)
movq -0x8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0xb8(%rbp), %rcx
callq 0x1773f0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
subq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0xe8(%rax), %rax
movq %rax, -0x58(%rbp)
movb $0x1, -0x50(%rbp)
movq $0x5e, -0x48(%rbp)
movq -0x8(%rbp), %rdi
leaq 0xb19b8(%rip), %rsi     # 0x1c3d46
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0xb8(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x179480
movq -0x58(%rbp), %rdi
callq 0x167c30
cmpl $0x0, -0xb8(%rbp)
je 0x112411
movq -0x8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x177360
movq -0xc0(%rbp), %rdi
movq %rax, %rdx
leaq 0x8dbff(%rip), %rsi     # 0x19fffd
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
jmp 0x11261a
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x18(%rbp), %rax
movq 0xe8(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x111c90
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x11245d
jmp 0x11261a
movq -0x18(%rbp), %rax
movq 0xd0(%rax), %rax
movq -0x18(%rbp), %rcx
addq 0xe0(%rcx), %rax
cmpq $0x2faf080, %rax        # imm = 0x2FAF080
jle 0x11248f
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x11261a
movq -0x8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0xb8(%rbp), %rcx
callq 0x1773f0
movq -0x18(%rbp), %rax
movq 0xe8(%rax), %rdi
callq 0x169310
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
subq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0xe8(%rax), %rax
movq %rax, -0x58(%rbp)
movb $0x1, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq 0xd0(%rax), %rax
addq $0x5e, %rax
movq -0x18(%rbp), %rcx
addq 0xe0(%rcx), %rax
addq $0x2, %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
leaq 0xb1837(%rip), %rsi     # 0x1c3d5d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0xb8(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x179480
movq -0x58(%rbp), %rdi
callq 0x167c30
cmpl $0x0, -0xb8(%rbp)
je 0x1125a6
movq -0x8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x177360
movq -0xc8(%rbp), %rdi
movq %rax, %rdx
leaq 0x8da67(%rip), %rsi     # 0x19fffd
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
jmp 0x11261a
cmpb $0x0, -0xb4(%rbp)
jne 0x1125c0
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x11261a
movq -0x8(%rbp), %rdi
movq -0xb0(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0xe8(%rax), %rax
movq 0x18(%rax), %rcx
leaq 0x8c762(%rip), %rsi     # 0x19ed43
movb $0x0, %al
callq 0x15b0d0
movq -0xb0(%rbp), %rcx
addq $0x4, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x1121b0
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x15b5a0
addq $0xd0, %rsp
popq %rbp
retq
nop