// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x138, %esi            # imm = 0x138
callq 0x15b780
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
leaq 0x8d761(%rip), %rcx     # 0x1cde79
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movl $0x101, %esi            # imm = 0x101
xorl %eax, %eax
movl %eax, %edx
callq 0x188560
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rdi
callq 0x140d30
movq -0x28(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x140775
jmp 0x140cb8
movq -0x8(%rbp), %rax
cmpl $0x4, (%rax)
jl 0x14078f
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0x187cc0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x10(%rcx), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x1407be
jmp 0x140cb8
movq -0x18(%rbp), %rax
movzbl 0x8(%rax), %eax
cmpl $0x0, %eax
je 0x1407f0
movq -0x48(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jl 0x1407f0
movq -0x8(%rbp), %rdi
leaq 0x8d6a2(%rip), %rsi     # 0x1cde86
movb $0x0, %al
callq 0x15b350
jmp 0x140cb8
movq -0x38(%rbp), %rax
addq $0x4, %rax
cmpq -0x40(%rbp), %rax
jle 0x140819
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x8d69d(%rip), %rsi     # 0x1cdeaa
movb $0x0, %al
callq 0x15b350
jmp 0x140cb8
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x7c5d1(%rip), %rsi     # 0x1bcdf9
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165630
movq %rax, -0xd8(%rbp)
movq -0x8(%rbp), %rdi
movq -0xd8(%rbp), %rdx
leaq 0x8d66b(%rip), %rsi     # 0x1cded1
movb $0x0, %al
callq 0x15b350
cmpq $0x0, -0x58(%rbp)
je 0x140885
movq -0x58(%rbp), %rdi
callq 0x177720
movq $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq -0x70(%rbp), %rdx
leaq -0xd0(%rbp), %rcx
callq 0x1773f0
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
cmpb $0x0, 0x8(%rax)
je 0x1408c6
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
subq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
movb $0x1, -0x68(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x3709f(%rip), %rsi     # 0x177970
xorl %eax, %eax
movl %eax, %edx
leaq -0x70(%rbp), %rcx
leaq -0xd0(%rbp), %r8
callq 0x1775a0
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x167c30
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0xf8(%rbp)
cmpq $0x0, -0xd8(%rbp)
jl 0x140bc6
movq $0x0, -0x110(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xe0(%rbp)
cmpq $0xa, -0xe0(%rbp)
jge 0x14093d
jmp 0x140cb8
movq -0x38(%rbp), %rax
subq $0x4, %rax
addq -0xe0(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
cmpq -0x40(%rbp), %rax
jle 0x140970
movq -0x28(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x140cb8
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1654b0
movq %rax, -0x108(%rbp)
movq -0x8(%rbp), %rdi
movq -0x108(%rbp), %rdx
leaq 0x691f8(%rip), %rsi     # 0x1a9b91
movb $0x0, %al
callq 0x15b350
cmpq $0x5f5e100, -0x108(%rbp) # imm = 0x5F5E100
jle 0x1409bd
movq -0x28(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x140cb8
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1653b0
movq %rax, -0xf0(%rbp)
movq -0x8(%rbp), %rdi
movq -0xf0(%rbp), %rax
movl %eax, %edx
leaq 0x8d4fe(%rip), %rsi     # 0x1cdee6
movb $0x0, %al
callq 0x15b350
cmpq $0x0, -0xf0(%rbp)
jl 0x140a06
cmpq $0x100, -0xf0(%rbp) # imm = 0x100
jle 0x140a33
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0xf0(%rbp), %rax
movl %eax, %r8d
leaq 0x8d4d1(%rip), %rcx     # 0x1cdef8
movb $0x0, %al
callq 0x177450
jmp 0x140cb8
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0xf0(%rbp), %rax
jge 0x140aa4
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165170
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
movb %dl, 0x34(%rax,%rcx)
movq -0x8(%rbp), %rax
cmpl $0x3, (%rax)
jl 0x140a94
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
movzbl 0x34(%rax,%rcx), %ecx
leaq 0x8d484(%rip), %rsi     # 0x1cdf11
movb $0x0, %al
callq 0x15b400
jmp 0x140a96
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x140a3b
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x8d46c(%rip), %rsi     # 0x1cdf1f
movb $0x0, %al
callq 0x15b350
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x19140
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x140(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x130(%rbp)
movl $0x0, -0x148(%rbp)
movq -0x110(%rbp), %rax
cmpq -0x108(%rbp), %rax
jl 0x140b13
jmp 0x140bc1
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rdi
leaq -0x140(%rbp), %rsi
leaq -0x148(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x187bc0
movl %eax, -0x144(%rbp)
cmpb $0x0, -0x128(%rbp)
je 0x140b47
jmp 0x140bc1
cmpl $0x0, -0x144(%rbp)
jne 0x140b73
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x8d3e2(%rip), %rcx     # 0x1cdf49
movb $0x0, %al
callq 0x177450
jmp 0x140cb8
cmpl $0x0, -0x148(%rbp)
jl 0x140b88
cmpl $0xff, -0x148(%rbp)
jle 0x140b8a
jmp 0x140bc1
movq -0x58(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x34, %rsi
movslq -0x148(%rbp), %rax
addq %rax, %rsi
movl $0x1, %edx
callq 0x177620
movq -0x110(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x110(%rbp)
jmp 0x140af4
jmp 0x140c4d
xorl %eax, %eax
subq -0xd8(%rbp), %rax
movq %rax, -0xe0(%rbp)
cmpq $0x4, -0xe0(%rbp)
jge 0x140be5
jmp 0x140cb8
movq -0x38(%rbp), %rax
subq $0x4, %rax
addq -0xe0(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
cmpq -0x40(%rbp), %rax
jle 0x140c18
movq -0x28(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x140cb8
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x8d33e(%rip), %rsi     # 0x1cdf65
movb $0x0, %al
callq 0x15b350
movq -0x58(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x38(%rbp), %rdx
movq -0xe0(%rbp), %rcx
subq $0x4, %rcx
callq 0x177770
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x167c30
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
subq -0xf8(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x8(%rbp), %rdi
movq -0x100(%rbp), %rdx
leaq 0x5eb72(%rip), %rsi     # 0x19f7f6
movb $0x0, %al
callq 0x15b350
movq -0x100(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
jmp 0x1407af
cmpq $0x0, -0x58(%rbp)
je 0x140cc8
movq -0x58(%rbp), %rdi
callq 0x177720
cmpq $0x0, -0x28(%rbp)
je 0x140d0c
movq -0x28(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x140cee
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x177560
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0x1886d0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x15b5a0
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopw %cs:(%rax,%rax)