// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
leaq 0x54(%rsp), %rsi
callq 0xe958c
movl $0x138, %esi            # imm = 0x138
movq %rbx, %rdi
callq 0xe969b
movq %rax, %rbp
movq %rax, %r15
movq %rbx, (%rax)
leaq 0x6a1ea(%rip), %rax     # 0x141e19
movq %rax, 0x8(%rbp)
movq %r13, 0x10(%rsp)
movq %r13, 0x10(%rbp)
movq %r12, 0x18(%rbp)
movq %r14, 0x40(%rsp)
movq %r14, 0x20(%rbp)
xorl %r13d, %r13d
movl $0x101, %esi            # imm = 0x101
movq %rbx, %rdi
xorl %edx, %edx
callq 0x105165
movq %rax, 0x28(%rbp)
movq %rbp, 0x58(%rsp)
movq (%rbp), %r14
leaq 0x60(%rsp), %rsi
movq %r14, %rdi
callq 0xe958c
leaq 0x6a2ab(%rip), %rdx     # 0x141f27
movq %r14, %rdi
movl $0x4, %esi
xorl %eax, %eax
callq 0xe94f3
movq %r14, %rdi
movl $0x1, %esi
callq 0xe9585
leaq 0x6a5de(%rip), %rax     # 0x14227d
leaq 0x6a5e4(%rip), %rcx     # 0x14228a
xorl %edx, %edx
movzbl (%r13,%rax), %esi
cmpb $0x1, %sil
adcl $0x0, %esi
movzbl %sil, %esi
cmpq $0x100, %rdx            # imm = 0x100
ja 0xd7d92
movb (%r13,%rcx), %dil
movb %dil, 0xd0(%rsp,%rdx)
incq %rdx
decq %rsi
jne 0xd7cb9
incq %r13
cmpq $0xd, %r13
jne 0xd7ca8
xorl %r13d, %r13d
xorl %edx, %edx
xorl %eax, %eax
movzbl 0xd0(%rsp,%r13), %ebp
testl %eax, %eax
je 0xd7d06
movl %eax, %ecx
subl %ebp, %ecx
jbe 0xd7d0a
shrq %cl, %rdx
incq %rdx
jmp 0xd7d18
xorl %edx, %edx
jmp 0xd7d18
incq %rdx
cmpl %ebp, %eax
jae 0xd7d18
movl %ebp, %ecx
subl %eax, %ecx
shlq %cl, %rdx
cmpl $0x4, (%r14)
movq %rdx, 0x18(%rsp)
jl 0xd7d58
movl $0x48, %esi
leaq 0x80(%rsp), %rdi
movq 0x18(%rsp), %rdx
movl %ebp, %ecx
callq 0xea85a
movq %r14, %rdi
leaq 0x4660e(%rip), %rsi     # 0x11e354
movq %rax, %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0xe9461
movq 0x18(%rsp), %rdx
movq 0x28(%r15), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
movl %ebp, %ecx
movl %r13d, %r8d
callq 0x104901
testl %eax, %eax
je 0xd7d8a
incq %r13
movl %ebp, %eax
cmpq $0x101, %r13            # imm = 0x101
movq 0x18(%rsp), %rdx
jne 0xd7ceb
jmp 0xd7d92
movl $0x1, 0x30(%r15)
movl 0x60(%rsp), %esi
movq %r14, %rdi
callq 0xe9595
cmpl $0x0, 0x30(%r15)
je 0xd7e02
xorl %r14d, %r14d
testq %r14, %r14
je 0xd7db5
movq %r14, %rdi
callq 0xfa8d8
cmpl $0x0, 0x30(%r15)
je 0xd7dcd
movq 0x8(%r15), %rdx
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
callq 0xfa815
movq 0x28(%r15), %rsi
movq %rbx, %rdi
callq 0x10521e
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe9752
movl 0x54(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x4, (%rbx)
jl 0xd7e13
movq 0x28(%r15), %rsi
movq %rbx, %rdi
callq 0x104c1c
movq 0x10(%rsp), %r13
movq 0x8(%r13), %rax
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
addq 0x10(%r13), %rax
movq %rax, 0x48(%rsp)
addq $0x34, 0x58(%rsp)
xorl %r14d, %r14d
movq $0x0, 0x18(%rsp)
cmpl $0x0, 0x30(%r15)
je 0xd7e52
xorl %ebp, %ebp
testb %bpl, %bpl
jne 0xd7e3f
jmp 0xd7da8
cmpb $0x0, 0x8(%r12)
je 0xd7e6a
movq 0x18(%rsp), %rax
cmpq 0x10(%r12), %rax
jge 0xd7f80
movq 0x8(%rsp), %rdx
leaq 0x4(%rdx), %rax
cmpq 0x48(%rsp), %rax
jle 0xd7e8f
xorl %ebp, %ebp
movq %rbx, %rdi
leaq 0x69fc4(%rip), %rsi     # 0x141e4a
xorl %eax, %eax
callq 0xe93cf
jmp 0xd7e48
movq %rbx, %rdi
leaq 0x591e4(%rip), %rsi     # 0x13107d
xorl %eax, %eax
callq 0xe93cf
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq (%r13), %rdi
leaq 0x8(%rsp), %rsi
callq 0xef463
movq %rax, %r13
movq %rbx, %rdi
leaq 0x69fa9(%rip), %rsi     # 0x141e71
movq %rax, %rdx
xorl %eax, %eax
callq 0xe93cf
testq %r14, %r14
je 0xd7edf
movq %r14, %rdi
callq 0xfa8d8
movq %rbx, %rdi
xorl %esi, %esi
leaq 0x60(%rsp), %rdx
leaq 0xd0(%rsp), %rcx
callq 0xfa6e0
movq (%r12), %rax
movq %rax, 0x60(%rsp)
cmpb $0x0, 0x8(%r12)
je 0xd7f1b
movq 0x10(%r12), %rax
subq 0x18(%rsp), %rax
movq %rax, 0x70(%rsp)
movb $0x1, 0x68(%rsp)
movq %rbx, %rdi
leaq 0x22b4f(%rip), %rsi     # 0xfaa74
xorl %edx, %edx
leaq 0x60(%rsp), %rcx
leaq 0xd0(%rsp), %r8
callq 0xfa829
movq %rax, %r14
movq (%r12), %rdi
callq 0xf0ea4
movq (%r12), %rax
movq 0x18(%rax), %rbp
testq %r13, %r13
js 0xd7f98
cmpq $0xa, %r13
jl 0xd7f78
movq 0x8(%rsp), %rax
addq %r13, %rax
addq $-0x4, %rax
movq %rax, 0x38(%rsp)
cmpq 0x48(%rsp), %rax
jle 0xd7fca
movl $0x1, 0x30(%r15)
xorl %r13d, %r13d
jmp 0xd81f6
xorl %ebp, %ebp
movq %rbx, %rdi
leaq 0x69e9a(%rip), %rsi     # 0x141e26
xorl %eax, %eax
callq 0xe93cf
jmp 0xd7e48
cmpq $-0x4, %r13
jg 0xd8244
movq 0x8(%rsp), %rdx
movq %rdx, %rax
subq %r13, %rax
addq $-0x4, %rax
movq %rax, 0x38(%rsp)
cmpq 0x48(%rsp), %rax
jle 0xd8017
movl $0x1, 0x30(%r15)
jmp 0xd8244
movq %rbp, 0x28(%rsp)
movq 0x10(%rsp), %r13
movq (%r13), %rdi
leaq 0x8(%rsp), %rsi
callq 0xef34e
movq %rax, %rbp
movq %rbx, %rdi
leaq 0x45ee2(%rip), %rsi     # 0x11ded1
movq %rax, %rdx
xorl %eax, %eax
callq 0xe93cf
movq %rbp, 0x78(%rsp)
cmpq $0x5f5e101, %rbp        # imm = 0x5F5E101
jl 0xd804c
movl $0x1, 0x30(%r15)
xorl %r13d, %r13d
jmp 0xd81f1
movq %rbx, %rdi
leaq 0x69ee4(%rip), %rsi     # 0x141f05
xorl %eax, %eax
callq 0xe93cf
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rsp), %rdx
movq $-0x4, %rcx
subq %r13, %rcx
movq %r14, %rdi
callq 0xfa908
jmp 0xd81fb
movq (%r13), %rdi
leaq 0x8(%rsp), %rsi
callq 0xef295
movq %rax, %rbp
movq %rbx, %rdi
leaq 0x69e1f(%rip), %rsi     # 0x141e86
movl %ebp, %edx
xorl %eax, %eax
callq 0xe93cf
cmpq $0x100, %rbp            # imm = 0x100
jbe 0xd809e
movq 0x8(%r15), %rdx
xorl %r13d, %r13d
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
leaq 0x69e09(%rip), %rcx     # 0x141e98
movl %ebp, %r8d
xorl %eax, %eax
callq 0xfa71e
jmp 0xd81f1
testq %rbp, %rbp
je 0xd80e1
xorl %r13d, %r13d
movq 0x10(%rsp), %rax
movq (%rax), %rdi
leaq 0x8(%rsp), %rsi
callq 0xef14d
movb %al, 0x34(%r15,%r13)
cmpl $0x3, (%rbx)
jl 0xd80d9
movzbl %al, %ecx
movq %rbx, %rdi
leaq 0x69de2(%rip), %rsi     # 0x141eb1
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9461
incq %r13
cmpq %r13, %rbp
jne 0xd80a6
movq 0x8(%rsp), %rdx
movq %rbx, %rdi
leaq 0x69dcf(%rip), %rsi     # 0x141ebf
xorl %eax, %eax
callq 0xe93cf
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x80(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x90(%rsp)
xorl %eax, %eax
movl $0x0, 0x24(%rsp)
movl $0x9, %ebp
cmpq 0x78(%rsp), %rax
jge 0xd8187
movq %rax, 0x30(%rsp)
movq 0x28(%r15), %rax
movq 0x8(%rax), %rdi
xorl %r13d, %r13d
leaq 0x80(%rsp), %rsi
leaq 0x24(%rsp), %rdx
xorl %ecx, %ecx
callq 0x104b4d
cmpb $0x0, 0x98(%rsp)
je 0xd8190
movq 0x30(%rsp), %rax
jmp 0xd818a
xorl %r13d, %r13d
testl %ebp, %ebp
je 0xd813e
jmp 0xd81e9
testl %eax, %eax
je 0xd81c5
movl 0x24(%rsp), %esi
xorl %r13d, %r13d
cmpq $0xff, %rsi
ja 0xd8180
addq 0x58(%rsp), %rsi
movl $0x1, %edx
movq %r14, %rdi
callq 0xfa87a
movq 0x30(%rsp), %rax
incq %rax
movb $0x1, %r13b
xorl %ebp, %ebp
jmp 0xd818a
movq 0x8(%r15), %rdx
xorl %r13d, %r13d
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
leaq 0x69d0e(%rip), %rcx     # 0x141ee9
xorl %eax, %eax
callq 0xfa71e
movl $0x2, %ebp
jmp 0xd8180
cmpl $0x9, %ebp
jne 0xd81f1
movb $0x1, %r13b
movq 0x28(%rsp), %rbp
testb %r13b, %r13b
je 0xd8244
movq (%r12), %rdi
callq 0xf0ea4
movq (%r12), %rax
movq 0x18(%rax), %r13
subq %rbp, %r13
movq %rbx, %rdi
leaq 0x3b5fd(%rip), %rsi     # 0x113816
movq %r13, %rdx
xorl %eax, %eax
callq 0xe93cf
addq %r13, 0x18(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movb $0x1, %bpl
jmp 0xd8246
xorl %ebp, %ebp
movq 0x10(%rsp), %r13
jmp 0xd7e48