// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/stuffit.c
// Function: do_decompr_fixedhuff
static void do_decompr_fixedhuff(deark *c, struct de_dfilter_in_params *dcmpri,
	struct de_dfilter_out_params *dcmpro, struct de_dfilter_results *dres)
{
	struct sit_fixedhuffctx *hctx = NULL;
	i64 i;
	i64 pos, endpos;
	i64 nbytes_written = 0;
	int saved_indent_level;
	struct de_dfilter_ctx *pb_dfctx = NULL;
	struct de_dfilter_out_params pb_dcmpro;
	struct de_dfilter_results pb_dres;

	de_dbg_indent_save(c, &saved_indent_level);
	hctx = de_malloc(c, sizeof(struct sit_fixedhuffctx));
	hctx->c = c;
	hctx->modname = "fixedhuffman";
	hctx->dcmpri = dcmpri;
	hctx->dcmpro = dcmpro;
	hctx->dres = dres;
	hctx->ht = fmtutil_huffman_create_decoder(c, FIXEDHUFF_NUMCODES, 0);

	sit_fixedhuff_init_tree(hctx);
	if(hctx->errflag) goto done;

	if(c->debug_level>=4) {
		fmtutil_huffman_dump(c, hctx->ht);
	}

	pos = dcmpri->pos;
	endpos = dcmpri->pos + dcmpri->len;

	while(1) { // For each block...
		i64 blocksize_raw;
		i64 blocksize;
		i64 block_endpos;
		i64 ndefs;
		i64 prev_len;
		i64 nbytes_written_this_block;

		if(hctx->errflag) goto done;
		if(dcmpro->len_known && (nbytes_written>=dcmpro->expected_len)) {
			de_dbg2(c, "[stopping due to sufficient output]");
			goto done;
		}
		if(pos + 4 > endpos) {
			de_dbg2(c, "[stopping, no room for a block at %"I64_FMT"]", pos);
			goto done;
		}
		de_dbg2(c, "block at %"I64_FMT, pos);
		de_dbg_indent(c, 1);

		blocksize_raw = dbuf_geti32be_p(dcmpri->f, &pos);
		de_dbg2(c, "block size code: %"I64_FMT, blocksize_raw);

		if(pb_dfctx) {
			de_dfilter_destroy(pb_dfctx);
			pb_dfctx = NULL;
		}
		de_dfilter_init_objects(c, NULL, &pb_dcmpro, &pb_dres);
		pb_dcmpro.f = dcmpro->f;
		if(dcmpro->len_known) {
			// We apparently aren't told this block's decompressed size after PackBits
			// decompression.
			// Set the PackBits decoder's expected output len (really max len)
			// to the maximum possible number of decompressed bytes still needed.
			pb_dcmpro.expected_len = dcmpro->expected_len - nbytes_written;
			pb_dcmpro.len_known = 1;
		}
		pb_dfctx = de_dfilter_create(c, dfilter_packbits_codec, NULL, &pb_dcmpro, &pb_dres);

		dbuf_flush(dcmpro->f);
		prev_len = dcmpro->f->len;

		if(blocksize_raw >= 0) { // PackBits + Huffman
			i64 intermediate_len;
			i64 nbytes_decoded_intermed = 0; // After Huffman decompression, before packbits
			struct de_bitreader bitrd;

			blocksize = blocksize_raw;
			if(blocksize<10) {
				goto done;
			}

			block_endpos = pos - 4 + blocksize;
			if(block_endpos > endpos) {
				hctx->errflag = 1;
				goto done;
			}

			// This field seems to be the 'size in bytes' after Huffman decompression,
			// as opposed to (say) the number of Huffman codes, which should be one
			// larger (for the STOP code).
			intermediate_len = dbuf_getu32be_p(dcmpri->f, &pos);
			de_dbg2(c, "intermediate len: %"I64_FMT, intermediate_len);
			if(intermediate_len > DE_MAX_SANE_OBJECT_SIZE) { // TODO what should the limit be?
				hctx->errflag = 1;
				goto done;
			}

			ndefs = dbuf_geti16be_p(dcmpri->f, &pos);
			de_dbg2(c, "num code defs: %d", (int)ndefs);

			if(ndefs<0 || ndefs>256) {
				de_dfilter_set_errorf(c, dres, hctx->modname, "Can't handle num_defs=%d", (int)ndefs);
				goto done;
			}

			for(i=0; i<ndefs; i++) {
				hctx->translation[i] = dbuf_getbyte_p(dcmpri->f, &pos);
				if(c->debug_level>=3) {
					de_dbg3(c, "ll:%d = hl:%u", (int)i, (UI)hctx->translation[i]);
				}
			}

			de_dbg2(c, "compressed data (PackBits+Huffman) at %"I64_FMT, pos);
			de_zeromem(&bitrd, sizeof(struct de_bitreader));
			bitrd.f = dcmpri->f;
			bitrd.curpos = pos;
			bitrd.endpos = block_endpos;

			while(1) {
				int ret;
				fmtutil_huffman_valtype val = 0;

				if(nbytes_decoded_intermed >= intermediate_len) break; // Have enough output data

				ret = fmtutil_huffman_read_next_value(hctx->ht->bk, &bitrd, &val, NULL);
				if(bitrd.eof_flag) break;
				if(!ret) {
					de_dfilter_set_errorf(c, dres, hctx->modname, "Error reading Huffman codes");
					goto done;
				}
				if(val<0 || val>255) {
					break; // "stop" code
				}

				de_dfilter_addbuf(pb_dfctx, &hctx->translation[(int)val], 1);
				nbytes_decoded_intermed++;
			}
		}
		else { // just PackBits
			blocksize = -blocksize_raw;

			if(blocksize<4) {
				goto done;
			}

			block_endpos = pos - 4 + blocksize;
			if(block_endpos > endpos) {
				hctx->errflag = 1;
				goto done;
			}

			de_dbg2(c, "compressed data (PackBits) at %"I64_FMT, pos);
			de_dfilter_addslice(pb_dfctx, dcmpri->f, pos, blocksize-4);
		}

		// Note: I'm assuming that each block is compressed independently (with
		// PackBits), but I'm not 100% sure. It could be that the whole file is
		// first compressed with PackBits, and then split into segments. If so,
		// this won't always work.
		dbuf_flush(dcmpro->f);
		nbytes_written_this_block = dcmpro->f->len - prev_len;
		de_dbg2(c, "decompressed to %"I64_FMT" bytes", nbytes_written_this_block);
		nbytes_written += nbytes_written_this_block;

		pos = block_endpos;
		de_dbg_indent(c, -1);
	}

done:
	if(pb_dfctx) de_dfilter_destroy(pb_dfctx);

	if(hctx) {
		if(hctx->errflag) {
			de_dfilter_set_generic_error(c, dres, hctx->modname);
		}

		fmtutil_huffman_destroy_decoder(c, hctx->ht);
		de_free(c, hctx);
	}

	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rcx, %rbp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x4c(%rsp), %rsi
callq 0xe8a1c
movl $0x138, %esi            # imm = 0x138
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r13
movq %rax, %r12
movq %rbx, (%rax)
leaq 0x6b112(%rip), %rax     # 0x1422f9
movq %rax, 0x8(%r13)
movq %r15, 0x8(%rsp)
movq %r15, 0x10(%r13)
movq %r14, 0x30(%rsp)
movq %r14, 0x18(%r13)
movq %rbp, 0x20(%r13)
xorl %r15d, %r15d
movl $0x101, %esi            # imm = 0x101
movq %rbx, %rdi
xorl %edx, %edx
callq 0x104d0e
movq %rax, 0x28(%r13)
movq %r13, 0x58(%rsp)
movq (%r13), %r14
leaq 0x60(%rsp), %rsi
movq %r14, %rdi
callq 0xe8a1c
leaq 0x6b1d3(%rip), %rdx     # 0x142407
movq %r14, %rdi
movl $0x4, %esi
xorl %eax, %eax
callq 0xe895a
movq %r14, %rdi
movl $0x1, %esi
callq 0xe8a15
leaq 0x6b506(%rip), %rax     # 0x14275d
leaq 0x6b50c(%rip), %rcx     # 0x14276a
xorl %edx, %edx
movzbl (%r15,%rax), %esi
cmpb $0x1, %sil
adcl $0x0, %esi
movzbl %sil, %esi
cmpq $0x100, %rdx            # imm = 0x100
ja 0xd735b
movb (%r15,%rcx), %dil
movb %dil, 0xd0(%rsp,%rdx)
incq %rdx
decq %rsi
jne 0xd7270
incq %r15
cmpq $0xd, %r15
jne 0xd7260
xorl %r15d, %r15d
xorl %edx, %edx
xorl %eax, %eax
movq %r12, 0x20(%rsp)
movzbl 0xd0(%rsp,%r15), %r12d
testl %eax, %eax
je 0xd72c2
movl %eax, %ecx
subl %r12d, %ecx
jbe 0xd72c6
shrq %cl, %rdx
incq %rdx
jmp 0xd72d6
xorl %edx, %edx
jmp 0xd72d6
incq %rdx
cmpl %r12d, %eax
jae 0xd72d6
movl %r12d, %ecx
subl %eax, %ecx
shlq %cl, %rdx
cmpl $0x3, (%r14)
movq %rdx, %r13
jle 0xd7311
movl $0x48, %esi
leaq 0x80(%rsp), %rdi
movq %r13, %rdx
movl %r12d, %ecx
callq 0xe9d03
movq %r14, %rdi
leaq 0x47163(%rip), %rsi     # 0x11e464
movq %rax, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe889f
movq %r13, %rdx
movq 0x20(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
movl %r12d, %ecx
movl %r15d, %r8d
callq 0x1044c1
testl %eax, %eax
je 0xd734d
incq %r15
movl %r12d, %eax
cmpq $0x101, %r15            # imm = 0x101
movq 0x20(%rsp), %r12
movq %r13, %rdx
jne 0xd72a6
jmp 0xd735b
movq 0x20(%rsp), %r12
movl $0x1, 0x30(%r12)
movl 0x60(%rsp), %esi
movq %r14, %rdi
callq 0xe8a25
cmpl $0x0, 0x30(%r12)
jne 0xd77c1
cmpl $0x3, (%rbx)
jle 0xd73aa
movq 0x28(%r12), %rsi
movq %rbx, %rdi
callq 0x1047f5
movl 0x30(%r12), %eax
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rsi
movq %rsi, (%rsp)
testl %eax, %eax
movq 0x30(%rsp), %r15
jne 0xd77b9
addq 0x10(%rcx), %rsi
jmp 0xd73c0
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rsi
movq %rsi, (%rsp)
addq 0x10(%rax), %rsi
movq 0x30(%rsp), %r15
addq $0x34, 0x58(%rsp)
movq %rsp, %r14
movq $0x0, 0x40(%rsp)
movq $0x0, 0x28(%rsp)
movq %rbp, 0x18(%rsp)
movq %r12, 0x20(%rsp)
movq %rsi, 0x38(%rsp)
cmpb $0x0, 0x8(%r15)
je 0xd7400
movq 0x40(%rsp), %rax
cmpq 0x10(%r15), %rax
jge 0xd776f
movq (%rsp), %rdx
leaq 0x4(%rdx), %rax
cmpq %rsi, %rax
jg 0xd775c
movq %rbx, %rdi
leaq 0x59fa2(%rip), %rsi     # 0x1313bd
xorl %eax, %eax
callq 0xe87e4
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq %r14, %r13
movq %r14, %rsi
callq 0xeee1d
movq %rax, %r14
movq %rbx, %rdi
leaq 0x6af02(%rip), %rsi     # 0x142351
movq %rax, %rdx
xorl %eax, %eax
callq 0xe87e4
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0xd7468
callq 0xfa3d4
movq %rbx, %rdi
xorl %esi, %esi
leaq 0x60(%rsp), %rdx
leaq 0xd0(%rsp), %rcx
callq 0xfa1dc
movq (%r15), %rax
movq %rax, 0x60(%rsp)
cmpb $0x0, 0x8(%r15)
je 0xd74a1
movq 0x10(%r15), %rax
subq 0x40(%rsp), %rax
movq %rax, 0x70(%rsp)
movb $0x1, 0x68(%rsp)
movq %rbx, %rdi
leaq 0x230d1(%rip), %rsi     # 0xfa57c
xorl %edx, %edx
leaq 0x60(%rsp), %rcx
leaq 0xd0(%rsp), %r8
callq 0xfa325
movq %rax, 0x28(%rsp)
movq (%r15), %rdi
callq 0xf08ba
movq (%r15), %rax
movq 0x18(%rax), %rcx
testq %r14, %r14
movq %rcx, 0x78(%rsp)
js 0xd767f
cmpq $0xa, %r14
jb 0xd77aa
movq (%rsp), %rax
addq %r14, %rax
addq $-0x4, %rax
cmpq 0x38(%rsp), %rax
jg 0xd774c
movq %rax, 0x50(%rsp)
movq 0x8(%rsp), %r15
movq (%r15), %rdi
movq %r13, %rbp
movq %r13, %rsi
callq 0xeed31
movq %rax, %r14
movq %rbx, %rdi
leaq 0x46abb(%rip), %rsi     # 0x11dfe1
movq %rax, %rdx
xorl %eax, %eax
callq 0xe87e4
cmpq $0x5f5e101, %r14        # imm = 0x5F5E101
jge 0xd774c
movq (%r15), %rdi
movq %rbp, %rsi
callq 0xeec86
movq %rax, %r12
movq %rbx, %rdi
leaq 0x6ae11(%rip), %rsi     # 0x142366
movl %r12d, %edx
xorl %eax, %eax
callq 0xe87e4
cmpq $0x100, %r12            # imm = 0x100
ja 0xd7782
testq %r12, %r12
movq 0x58(%rsp), %rbp
je 0xd75b2
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq %r13, %rsi
callq 0xeeb4b
movb %al, (%rbp,%r15)
cmpl $0x3, (%rbx)
jl 0xd75aa
movzbl %al, %ecx
movq %rbx, %rdi
leaq 0x6adf1(%rip), %rsi     # 0x142391
movl %r15d, %edx
xorl %eax, %eax
callq 0xe889f
incq %r15
cmpq %r15, %r12
jne 0xd7579
movq (%rsp), %rdx
movq %rbx, %rdi
leaq 0x6addf(%rip), %rsi     # 0x14239f
xorl %eax, %eax
callq 0xe87e4
leaq 0x98(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x80(%rsp)
movq (%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x90(%rsp)
movl $0x0, 0x14(%rsp)
testq %r14, %r14
movq 0x20(%rsp), %r12
movq 0x30(%rsp), %r15
jle 0xd7678
movq 0x28(%r12), %rax
movq 0x8(%rax), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x14(%rsp), %rdx
xorl %ecx, %ecx
callq 0x1046f3
cmpb $0x0, 0x98(%rsp)
jne 0xd7678
testl %eax, %eax
je 0xd772c
movl 0x14(%rsp), %esi
cmpq $0xff, %rsi
ja 0xd7678
addq %rbp, %rsi
movl $0x1, %edx
movq 0x28(%rsp), %rdi
callq 0xfa376
movl $0x0, 0x14(%rsp)
decq %r14
jne 0xd761d
movq 0x50(%rsp), %rbp
jmp 0xd76d3
cmpq $-0x4, %r14
ja 0xd77aa
movq (%rsp), %rdx
movq %rdx, %rbp
subq %r14, %rbp
addq $-0x4, %rbp
cmpq 0x38(%rsp), %rbp
jg 0xd774c
movq %rbx, %rdi
leaq 0x6ad39(%rip), %rsi     # 0x1423e5
xorl %eax, %eax
callq 0xe87e4
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq (%rsp), %rdx
movq $-0x4, %rcx
subq %r14, %rcx
movq 0x28(%rsp), %rdi
callq 0xfa404
movq (%r15), %rdi
callq 0xf08ba
movq (%r15), %rax
movq 0x18(%rax), %r14
subq 0x78(%rsp), %r14
movq %rbx, %rdi
leaq 0x3c1d5(%rip), %rsi     # 0x1138c6
movq %r14, %rdx
xorl %eax, %eax
callq 0xe87e4
addq %r14, 0x40(%rsp)
movq %rbp, (%rsp)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpl $0x0, 0x30(%r12)
movq 0x18(%rsp), %rbp
movq 0x38(%rsp), %rsi
movq %r13, %r14
je 0xd73ea
jmp 0xd77aa
movq 0x8(%r12), %rdx
leaq 0x6ac91(%rip), %rcx     # 0x1423c9
movq %rbx, %rdi
movq 0x18(%rsp), %rbp
movq %rbp, %rsi
xorl %eax, %eax
callq 0xfa21a
jmp 0xd77aa
movl $0x1, 0x30(%r12)
movq 0x18(%rsp), %rbp
jmp 0xd77aa
leaq 0x6abc7(%rip), %rsi     # 0x14232a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe87e4
jmp 0xd77aa
leaq 0x6ab90(%rip), %rsi     # 0x142306
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe87e4
jmp 0xd77aa
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdx
leaq 0x6abe6(%rip), %rcx     # 0x142378
movq %rbx, %rdi
movq 0x18(%rsp), %rbp
movq %rbp, %rsi
movl %r12d, %r8d
movq %rax, %r12
xorl %eax, %eax
callq 0xfa21a
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0xd77b9
callq 0xfa3d4
cmpl $0x0, 0x30(%r12)
je 0xd77d1
movq 0x8(%r12), %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xfa311
movq 0x28(%r12), %rsi
movq %rbx, %rdi
callq 0x104dc7
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe8be2
movl 0x4c(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq