// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/pklite.c
// Function: do_decompress
static void do_decompress(deark *c, lctx *d)
{
	struct decompr_internal_state *dctx = NULL;
	struct de_lz77buffer *ringbuf = NULL;
	u8 b;
	u8 allow_prehistory = 0;
	UI value_of_long_ml_code;
	UI value_of_ml2_0_code;
	UI value_of_ml2_1_code = 0xffff;
	UI value_of_lit0_code = 0xffff;
	UI long_matchlen_bias;

	de_dbg(c, "decompressing cmpr code at %"I64_FMT, d->dparams.cmpr_data_pos);
	de_dbg_indent(c, 1);

	dctx = de_malloc(c, sizeof(struct decompr_internal_state));
	dctx->d = d;
	dctx->inf = c->infile;
	dctx->dparams = &d->dparams;

	if(d->ver_reported.ver_num == 0x100) {
		// Need this for v1.00beta/1990-07-17. It makes use of an implied
		// 0-valued byte before the actual data (or it has a bug that does
		// that).
		allow_prehistory = 1;
	}

	if(d->dparams.large_cmpr) {
		if(d->dparams.v120_cmpr) {
			// There are 17 normal codes, and 4 special
			value_of_long_ml_code = 17;
			value_of_ml2_0_code = value_of_long_ml_code+1;
			value_of_ml2_1_code = value_of_long_ml_code+2;
			value_of_lit0_code = value_of_long_ml_code+3;
			long_matchlen_bias = 20;
		}
		else {
			// There are 22 normal codes, and 2 special
			value_of_long_ml_code = 22;
			value_of_ml2_0_code = value_of_long_ml_code+1;
			long_matchlen_bias = 25;
		}
	}
	else {
		if(d->dparams.v120_cmpr) {
			// There are 7 normal codes, and 4 special
			value_of_long_ml_code = 7;
			value_of_ml2_0_code = value_of_long_ml_code+1;
			value_of_ml2_1_code = value_of_long_ml_code+2;
			value_of_lit0_code = value_of_long_ml_code+3;
			long_matchlen_bias = 10;
		}
		else {
			// There are 7 normal codes, and 2 special
			value_of_long_ml_code = 7;
			value_of_ml2_0_code = value_of_long_ml_code+1;
			long_matchlen_bias = 10;
		}
	}

	make_matchlengths_tree(c, dctx);
	make_offsets_tree(c, dctx);

	d->dcmpr_code = dbuf_create_membuf(c, 0, 0);
	dbuf_set_length_limit(d->dcmpr_code, 1048576);
	dbuf_enable_wbuffer(d->dcmpr_code);

	ringbuf = de_lz77buffer_create(c, 8192);
	ringbuf->userdata = (void*)dctx;
	ringbuf->writebyte_cb = my_lz77buf_writebytecb;

	dctx->dcmpr_cur_ipos = d->dparams.cmpr_data_pos;
	dctx->bbll.is_lsb = 1;
	de_bitbuf_lowlevel_empty(&dctx->bbll);

	fill_bitbuf(c, dctx);

	while(1) {
		u8 x;
		UI len_raw;
		UI matchlen;
		UI offs_hi_bits = 0;
		u8 offs_lo_byte;
		u8 offs_have_hi_bits = 0;
		UI matchpos;

		if(d->errflag) goto after_dcmpr;

		x = pklite_getbit(c, dctx);
		if(x==0) {
			b = dbuf_getbyte_p(dctx->inf, &dctx->dcmpr_cur_ipos);
			if(d->dparams.extra_cmpr==1) {
				b ^= (u8)(dctx->bbll.nbits_in_bitbuf);
			}
			else if(d->dparams.extra_cmpr==2) {
				b ^= 0xff;
			}
			if(c->debug_level>=3) {
				de_dbg3(c, "lit 0x%02x", (UI)b);
			}
			de_lz77buffer_add_literal_byte(ringbuf, b);
			continue;
		}

		len_raw = read_pklite_code_using_tree(c, dctx, dctx->lengths_tree);
		if(d->errflag) goto after_dcmpr;

		if(len_raw<value_of_long_ml_code) {
			matchlen = len_raw+3;
		}
		else if(len_raw==value_of_ml2_0_code) {
			matchlen = 2;
			// Leave offs_hi_bits at 0.
			offs_have_hi_bits = 1;
		}
		else if(len_raw==value_of_long_ml_code) {
			b = dbuf_getbyte_p(dctx->inf, &dctx->dcmpr_cur_ipos);

			if(b >= 0xfd) {
				if(b==0xfd && d->dparams.large_cmpr) {
					do_uncompressed_area(c, dctx, ringbuf);
					continue;
				}
				if(b==0xfe && d->dparams.large_cmpr) {
					// (segment separator) Just a no-op?
					de_dbg3(c, "code 0xfe");
					continue;
				}
				if(b==0xff) {
					de_dbg3(c, "stop code");
					goto after_dcmpr; // Normal completion
				}
				de_err(c, "Unexpected code (0x%02x) or unsupported feature", (UI)b);
				d->errflag = 1;
				d->errmsg_handled = 1;
				goto after_dcmpr;
			}
			matchlen = (UI)b+long_matchlen_bias;
		}
		else if(len_raw==value_of_lit0_code) {
			if(c->debug_level>=3) {
				de_dbg3(c, "lit 0x00 (special)");
			}
			de_lz77buffer_add_literal_byte(ringbuf, 0x00);
			continue;
		}
		else if(len_raw==value_of_ml2_1_code) {
			matchlen = 2;
			offs_hi_bits = 1;
			offs_have_hi_bits = 1;
		}
		else {
			d->errflag = 1;
			goto done;
		}

		if(!offs_have_hi_bits) {
			offs_hi_bits = read_pklite_code_using_tree(c, dctx, dctx->offsets_tree);
		}

		offs_lo_byte = dbuf_getbyte_p(dctx->inf, &dctx->dcmpr_cur_ipos);
		offs_lo_byte ^= d->dparams.offset_xor_key;
		if(d->errflag) goto after_dcmpr;

		matchpos = (offs_hi_bits<<8) | (UI)offs_lo_byte;

		if(c->debug_level>=3) {
			de_dbg3(c, "match pos=%u len=%u", matchpos, matchlen);
		}

		// PKLITE confirmed to use distances 1 to 8191. Have not observed matchpos=0.
		// Have not observed it to use distances larger than the number of bytes
		// decompressed so far [with one small exception].
		if(allow_prehistory && (matchpos == dctx->dcmpr_code_nbytes_written+1)) {
			;
		}
		else if(matchpos==0 || (i64)matchpos>dctx->dcmpr_code_nbytes_written) {
			de_err(c, "Bad or unsupported compressed data (dist=%u, expected 1 to %"I64_FMT")",
				matchpos, dctx->dcmpr_code_nbytes_written);
			d->errflag = 1;
			d->errmsg_handled = 1;
			goto after_dcmpr;
		}
		de_lz77buffer_copy_from_hist(ringbuf,
				(UI)(ringbuf->curpos-matchpos), matchlen);
	}

after_dcmpr:
	if(!d->dcmpr_code) goto done;
	dbuf_flush(d->dcmpr_code);

	if(!d->errflag) {
		d->cmpr_data_endpos = dctx->dcmpr_cur_ipos;
		de_dbg(c, "cmpr data end: %"I64_FMT, d->cmpr_data_endpos);
		de_dbg(c, "decompressed %"I64_FMT" bytes to %"I64_FMT,
			d->cmpr_data_endpos-d->dparams.cmpr_data_pos, d->dcmpr_code->len);

		if(dctx->has_uncompressed_area) {
			de_warn(c, "This file has an \"uncompressed area\", and might not be "
				"decompressed correctly.");
		}
	}

done:
	if(dctx) {
		fmtutil_huffman_destroy_decoder(c, dctx->lengths_tree);
		fmtutil_huffman_destroy_decoder(c, dctx->offsets_tree);
		de_free(c, dctx);
	}
	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x10(%rsi), %r12
movq 0x10(%rsi), %rdx
leaq 0x6288f(%rip), %rsi     # 0x11e676
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movl $0x50, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r14
movq %r15, (%rax)
movq 0x18(%rbx), %rax
movq %rax, 0x8(%r14)
movq %r12, 0x10(%r14)
movl 0xe0(%r15), %eax
movl %eax, 0x24(%rsp)
cmpb $0x0, 0x19(%r15)
movb 0x1a(%r15), %al
leaq 0x40(%r14), %rsi
je 0xbbe8a
xorl %ecx, %ecx
testb %al, %al
sete %cl
leal 0x11(%rcx,%rcx,4), %eax
movl %eax, 0x8(%rsp)
leal 0x12(%rcx,%rcx,4), %eax
movl %eax, 0xc(%rsp)
movl $0xffff, %eax           # imm = 0xFFFF
movl $0x13, %edx
cmovel %eax, %edx
movl %edx, 0x1c(%rsp)
movl $0x14, %edx
cmovel %eax, %edx
movl %edx, 0x20(%rsp)
leal 0x14(%rcx,%rcx,4), %eax
movl %eax, 0x18(%rsp)
je 0xbbecb
leaq 0x7d227(%rip), %rdx     # 0x1390a0
leaq 0x7ccd6(%rip), %r8      # 0x138b56
movq %rbx, %rdi
movl $0x15, %ecx
jmp 0xbbee1
movl $0xffff, %edx           # imm = 0xFFFF
movl $0x9, %ecx
testb %al, %al
cmovel %edx, %ecx
movl %ecx, 0x1c(%rsp)
movl $0xa, %eax
movl $0xa, 0x18(%rsp)
cmovnel %eax, %edx
movl %edx, 0x20(%rsp)
je 0xbbee8
leaq 0x7d216(%rip), %rdx     # 0x1390d0
leaq 0x7cc95(%rip), %r8      # 0x138b56
movq %rbx, %rdi
movl $0xb, %ecx
jmp 0xbbefe
leaq 0x7d17e(%rip), %rdx     # 0x139050
leaq 0x7cc7d(%rip), %r8      # 0x138b56
movq %rbx, %rdi
movl $0x18, %ecx
callq 0xbce39
jmp 0xbbf13
leaq 0x7d191(%rip), %rdx     # 0x139080
leaq 0x7cc60(%rip), %r8      # 0x138b56
movq %rbx, %rdi
movl $0x9, %ecx
callq 0xbce39
movl $0x7, 0x8(%rsp)
movl $0x8, 0xc(%rsp)
movq 0x10(%r14), %rax
leaq 0x48(%r14), %rsi
cmpb $0x0, 0xa(%rax)
leaq 0x7d1ca(%rip), %rax     # 0x1390f0
leaq 0x7d203(%rip), %rdx     # 0x139130
cmoveq %rax, %rdx
leaq 0x6cecb(%rip), %r8      # 0x128e03
movq %rbx, %rdi
movl $0x20, %ecx
callq 0xbce39
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf09c5
movq %rax, 0xd8(%r15)
movl $0x100000, %esi         # imm = 0x100000
movq %rax, %rdi
callq 0xf0a7c
movq 0xd8(%r15), %rdi
callq 0xf0855
movq %rbx, %rdi
movl $0x2000, %esi           # imm = 0x2000
callq 0xfad8f
movq %rax, %r12
movq %r14, (%rax)
leaq 0xd77(%rip), %rax       # 0xbcd02
movq %rax, 0x8(%r12)
movq 0x10(%r15), %rax
movq %rax, 0x28(%r14)
leaq 0x30(%r14), %rdi
movb $0x1, 0x30(%r14)
callq 0xf1bdd
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbcd1b
cmpb $0x0, (%r15)
je 0xbc06b
movq 0xd8(%r15), %rdi
testq %rdi, %rdi
je 0xbc02d
callq 0xf08ba
cmpb $0x0, (%r15)
jne 0xbc02d
movq 0x28(%r14), %rdx
movq %rdx, 0x88(%r15)
leaq 0x627dd(%rip), %rsi     # 0x11e7c1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x88(%r15), %rdx
movq 0xd8(%r15), %rax
subq 0x10(%r15), %rdx
movq 0x18(%rax), %rcx
leaq 0x56c8d(%rip), %rsi     # 0x112c98
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpb $0x0, 0x18(%r14)
je 0xbc02d
leaq 0x7cae4(%rip), %rsi     # 0x138b07
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x40(%r14), %rsi
movq %rbx, %rdi
callq 0x104dc7
movq 0x48(%r14), %rsi
movq %rbx, %rdi
callq 0x104dc7
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8a15
movq %r14, %rax
addq $0x28, %rax
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbcd94
testb %al, %al
je 0xbc0c2
movq 0x40(%r14), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbcdd6
cmpb $0x0, (%r15)
jne 0xbbfbb
cmpl 0x8(%rsp), %eax
jae 0xbc0f0
addl $0x3, %eax
movl %eax, %ebp
movq 0x48(%r14), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbcdd6
movl %eax, %r13d
shll $0x8, %r13d
jmp 0xbc102
movq 0x8(%r14), %rdi
movq 0x10(%rsp), %rsi
callq 0xeeb4b
movzbl 0x18(%r15), %ecx
cmpl $0x2, %ecx
je 0xbc1dd
cmpl $0x1, %ecx
jne 0xbc1df
xorb 0x34(%r14), %al
jmp 0xbc1df
xorl %r13d, %r13d
movl $0x2, %ebp
cmpl 0xc(%rsp), %eax
jne 0xbc182
movq 0x8(%r14), %rdi
movq 0x10(%rsp), %rsi
callq 0xeeb4b
cmpb $0x0, (%r15)
jne 0xbbfbb
xorb 0x1b(%r15), %al
movzbl %al, %eax
orl %eax, %r13d
cmpl $0x3, (%rbx)
jl 0xbc13f
movq %rbx, %rdi
leaq 0x6259b(%rip), %rsi     # 0x11e6ce
movl %r13d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe889f
movq 0x20(%r14), %rcx
movl %r13d, %eax
cmpl $0x100, 0x24(%rsp)      # imm = 0x100
jne 0xbc159
leaq 0x1(%rcx), %rdx
cmpq %rax, %rdx
je 0xbc16b
testl %r13d, %r13d
je 0xbc2ce
cmpq %rax, %rcx
jl 0xbc2ce
movl 0x10(%r12), %esi
subl %r13d, %esi
movq %r12, %rdi
movl %ebp, %edx
callq 0xfae7b
jmp 0xbc204
cmpl 0x8(%rsp), %eax
jne 0xbc213
movq 0x8(%r14), %rdi
movq 0x10(%rsp), %rsi
callq 0xeeb4b
movzbl %al, %ebp
cmpb $-0x3, %bpl
jb 0xbc236
cmpl $0xfd, %ebp
je 0xbc254
cmpl $0xfe, %ebp
jne 0xbc30b
cmpb $0x0, 0x19(%r15)
je 0xbc2f6
movq %rbx, %rdi
leaq 0x7c8a6(%rip), %rsi     # 0x138a7a
xorl %eax, %eax
callq 0xe889f
jmp 0xbc204
notb %al
cmpl $0x3, (%rbx)
movzbl %al, %ebp
jl 0xbc1fa
movq %rbx, %rdi
leaq 0x624a4(%rip), %rsi     # 0x11e695
movl %ebp, %edx
xorl %eax, %eax
callq 0xe889f
movq %r12, %rdi
movl %ebp, %esi
callq 0xfae4e
cmpb $0x0, (%r15)
je 0xbc077
jmp 0xbbfbb
cmpl 0x20(%rsp), %eax
jne 0xbc23f
cmpl $0x3, (%rbx)
jl 0xbc22f
movq %rbx, %rdi
leaq 0x7c88c(%rip), %rsi     # 0x138ab4
xorl %eax, %eax
callq 0xe889f
movq %r12, %rdi
xorl %esi, %esi
jmp 0xbc1ff
addl 0x18(%rsp), %ebp
jmp 0xbc0aa
movl $0x100, %r13d           # imm = 0x100
cmpl 0x1c(%rsp), %eax
je 0xbc102
jmp 0xbc2ed
cmpb $0x0, 0x19(%r15)
je 0xbc2f6
movb $0x1, 0x18(%r14)
movq 0x8(%r14), %rdi
movq 0x10(%rsp), %rsi
callq 0xeeb4b
movzbl %al, %r13d
movq 0x28(%r14), %rdx
movq %rbx, %rdi
leaq 0x7c91a(%rip), %rsi     # 0x138b9e
movq %r13, %rcx
xorl %eax, %eax
callq 0xe889f
movl $0x9, %ebp
movq %r12, %rdi
xorl %esi, %esi
callq 0xfae4e
decq %rbp
jne 0xbc293
testq %r13, %r13
je 0xbc204
movq 0x8(%r14), %rdi
movq 0x10(%rsp), %rsi
callq 0xeeb4b
movzbl %al, %esi
movq %r12, %rdi
callq 0xfae4e
decq %r13
jne 0xbc2ab
jmp 0xbc204
leaq 0x7c7f2(%rip), %rsi     # 0x138ac7
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9295
movw $0x101, (%r15)          # imm = 0x101
jmp 0xbbfbb
movb $0x1, (%r15)
jmp 0xbc02d
leaq 0x7c787(%rip), %rsi     # 0x138a84
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9295
jmp 0xbc2e2
leaq 0x6238e(%rip), %rsi     # 0x11e6a0
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe889f
jmp 0xbbfbb