// File: /workspace/github_c_makefile_star2_O2/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 $0x1c8, %rsp            # imm = 0x1C8
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x44(%rsp), %rsi
callq 0xdebae
movl $0x138, %esi            # imm = 0x138
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %rbp
movq %rax, 0x10(%rsp)
movq %rbx, (%rax)
leaq 0x66edb(%rip), %rax     # 0x134ea9
movq %rax, 0x8(%rbp)
movq %r15, 0x18(%rsp)
movq %r15, 0x10(%rbp)
movq %r13, 0x18(%rbp)
movq %r14, 0x30(%rsp)
movq %r14, 0x20(%rbp)
xorl %r15d, %r15d
movl $0x101, %esi            # imm = 0x101
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf8601
movq %rax, 0x28(%rbp)
movq (%rbp), %r14
leaq 0x48(%rsp), %rsi
movq %r14, %rdi
callq 0xdebae
leaq 0x66fa1(%rip), %rdx     # 0x134fb7
pushq $0x4
popq %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0xdeb15
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0xdeba7
leaq 0x672d8(%rip), %rax     # 0x13530d
leaq 0x672de(%rip), %rcx     # 0x13531a
xorl %edx, %edx
cmpq $0xd, %rdx
je 0xce078
movzbl (%rdx,%rax), %edi
leaq (%r15,%rdi), %rsi
subq $0x1, %rdi
jb 0xce070
cmpq $0x100, %r15            # imm = 0x100
ja 0xce139
movb (%rdx,%rcx), %r8b
movb %r8b, 0xc0(%rsp,%r15)
incq %r15
jmp 0xce04c
incq %rdx
movq %rsi, %r15
jmp 0xce03e
xorl %esi, %esi
movq $0x0, 0x8(%rsp)
xorl %r12d, %r12d
cmpq $0x101, %r12            # imm = 0x101
je 0xce139
movzbl 0xc0(%rsp,%r12), %r15d
testl %esi, %esi
je 0xce0b4
movl %esi, %ecx
subl %r15d, %ecx
jbe 0xce0b8
movq 0x8(%rsp), %rdx
shrq %cl, %rdx
incq %rdx
jmp 0xce0cd
xorl %edx, %edx
jmp 0xce0cd
movq 0x8(%rsp), %rdx
incq %rdx
cmpl %r15d, %esi
jae 0xce0cd
movl %r15d, %ecx
subl %esi, %ecx
shlq %cl, %rdx
cmpl $0x3, (%r14)
movq %rdx, 0x8(%rsp)
jle 0xce104
leaq 0x70(%rsp), %rdi
pushq $0x48
popq %rsi
movl %r15d, %ecx
callq 0xdfc03
movq %r14, %rdi
leaq 0x4329e(%rip), %rsi     # 0x111390
movq %rax, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xdea83
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
movl %r15d, %ecx
movl %r12d, %r8d
callq 0xf7e56
incq %r12
movl %r15d, %esi
testl %eax, %eax
jne 0xce086
movq 0x10(%rsp), %rax
movl $0x1, 0x30(%rax)
movl 0x48(%rsp), %esi
movq %r14, %rdi
callq 0xdebb7
movq 0x10(%rsp), %r14
cmpl $0x0, 0x30(%r14)
jne 0xce53d
cmpl $0x4, (%rbx)
jl 0xce166
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0xf811c
movq 0x18(%rsp), %r15
movq 0x8(%r15), %rax
movq %rsp, %rcx
movq %rax, (%rcx)
addq 0x10(%r15), %rax
movq %rax, 0x38(%rsp)
addq $0x34, %rbp
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
movq %r13, 0x60(%rsp)
cmpl $0x0, 0x30(%r14)
jne 0xce529
cmpb $0x0, 0x8(%r13)
je 0xce1b4
movq 0x8(%rsp), %rax
cmpq 0x10(%r13), %rax
jge 0xce4e2
movq (%rsp), %rdx
leaq 0x4(%rdx), %rax
cmpq 0x38(%rsp), %rax
jg 0xce4c5
movq %rbx, %rdi
leaq 0x55efc(%rip), %rsi     # 0x1240cd
xorl %eax, %eax
callq 0xde9f1
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq (%r15), %rdi
movq %rsp, %rsi
callq 0xe40fc
movq %rax, %r15
movq %rbx, %rdi
leaq 0x66d06(%rip), %rsi     # 0x134f01
movq %rax, %rdx
xorl %eax, %eax
callq 0xde9f1
testq %r12, %r12
je 0xce212
movq %r12, %rdi
callq 0xee9dd
movq %rbx, %rdi
xorl %esi, %esi
leaq 0x48(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
callq 0xee7ee
movq (%r13), %rax
movq %rax, 0x48(%rsp)
cmpb $0x0, 0x8(%r13)
je 0xce24c
movq 0x10(%r13), %rax
subq 0x8(%rsp), %rax
movq %rax, 0x58(%rsp)
movb $0x1, 0x50(%rsp)
movq %rbx, %rdi
leaq 0x208a5(%rip), %rsi     # 0xeeafb
xorl %edx, %edx
leaq 0x48(%rsp), %rcx
leaq 0xc0(%rsp), %r8
callq 0xee930
movq %rax, %r12
movq (%r13), %rdi
callq 0xe58e8
movq (%r13), %rax
movq 0x18(%rax), %rcx
testq %r15, %r15
movq %rcx, 0x68(%rsp)
js 0xce408
cmpq $0xa, %r15
jb 0xce529
movq (%rsp), %rax
leaq (%r15,%rax), %r13
addq $-0x4, %r13
cmpq 0x38(%rsp), %r13
jg 0xce4d8
movq %r12, 0x28(%rsp)
movq 0x18(%rsp), %r12
movq (%r12), %rdi
movq %rsp, %rsi
callq 0xe4071
movq %rax, %r15
movq %rbx, %rdi
leaq 0x42c3d(%rip), %rsi     # 0x110f0d
movq %rax, %rdx
xorl %eax, %eax
callq 0xde9f1
cmpq $0x5f5e101, %r15        # imm = 0x5F5E101
jge 0xce4f5
movq (%r12), %rdi
movq %rsp, %rsi
callq 0xe4026
movq %rax, %r14
movq %rbx, %rdi
leaq 0x66c16(%rip), %rsi     # 0x134f16
movl %r14d, %edx
xorl %eax, %eax
callq 0xde9f1
cmpq $0x100, %r14            # imm = 0x100
ja 0xce4ff
xorl %r12d, %r12d
cmpq %r12, %r14
je 0xce355
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq %rsp, %rsi
callq 0xe3f2a
movb %al, (%rbp,%r12)
cmpl $0x3, (%rbx)
jl 0xce350
movzbl %al, %ecx
movq %rbx, %rdi
leaq 0x66bfb(%rip), %rsi     # 0x134f41
movl %r12d, %edx
xorl %eax, %eax
callq 0xdea83
incq %r12
jmp 0xce31a
movq (%rsp), %rdx
movq %rbx, %rdi
leaq 0x66bec(%rip), %rsi     # 0x134f4f
xorl %eax, %eax
callq 0xde9f1
leaq 0x88(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x70(%rsp)
movq (%rsp), %rax
movq %rax, 0x78(%rsp)
movq %r13, 0x80(%rsp)
testq %r15, %r15
movl $0x0, %eax
cmovleq %rax, %r15
movq 0x10(%rsp), %r14
movq 0x28(%rsp), %r12
andl $0x0, 0x24(%rsp)
subq $0x1, %r15
jb 0xce456
movq 0x28(%r14), %rax
movq 0x8(%rax), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x24(%rsp), %rdx
xorl %ecx, %ecx
callq 0xf8071
cmpb $0x0, 0x88(%rsp)
jne 0xce456
testl %eax, %eax
je 0xce4a9
movl 0x24(%rsp), %esi
cmpq $0xff, %rsi
ja 0xce456
addq %rbp, %rsi
movq %r12, %rdi
pushq $0x1
popq %rdx
callq 0xee97f
jmp 0xce3b1
cmpq $-0x4, %r15
ja 0xce529
movq (%rsp), %rdx
movq %rdx, %r13
subq %r15, %r13
addq $-0x4, %r13
cmpq 0x38(%rsp), %r13
jg 0xce4d8
movq %rbx, %rdi
leaq 0x66b60(%rip), %rsi     # 0x134f95
xorl %eax, %eax
callq 0xde9f1
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq (%rsp), %rdx
pushq $-0x4
popq %rcx
subq %r15, %rcx
movq %r12, %rdi
callq 0xeea0d
movq 0x60(%rsp), %r15
movq (%r15), %rdi
callq 0xe58e8
movq (%r15), %rax
movq 0x18(%rax), %r14
subq 0x68(%rsp), %r14
movq %rbx, %rdi
leaq 0x383ed(%rip), %rsi     # 0x106866
movq %r14, %rdx
xorl %eax, %eax
callq 0xde9f1
addq %r14, 0x8(%rsp)
movq %r13, (%rsp)
movq %r15, %r13
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
jmp 0xce193
movq 0x8(%r14), %rdx
leaq 0x66ac5(%rip), %rcx     # 0x134f79
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
xorl %eax, %eax
callq 0xee823
jmp 0xce529
leaq 0x66a0e(%rip), %rsi     # 0x134eda
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde9f1
jmp 0xce529
movl $0x1, 0x30(%r14)
jmp 0xce529
leaq 0x669cd(%rip), %rsi     # 0x134eb6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde9f1
jmp 0xce529
movl $0x1, 0x30(%r14)
jmp 0xce524
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdx
leaq 0x66a19(%rip), %rcx     # 0x134f28
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
movl %r14d, %r8d
movq %rax, %r14
xorl %eax, %eax
callq 0xee823
movq 0x28(%rsp), %r12
testq %r12, %r12
je 0xce536
movq %r12, %rdi
callq 0xee9dd
cmpl $0x0, 0x30(%r14)
je 0xce54e
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
callq 0xee91c
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0xf86b0
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
movl 0x44(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq