// File: /workspace/github_c_makefile_star2_O2/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 0x5c3bb(%rip), %rsi     # 0x1115a6
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
pushq $0x50
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
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 0xb5280
xorl %ecx, %ecx
testb %al, %al
sete %cl
leal 0x11(%rcx,%rcx,4), %eax
movl %eax, 0x4(%rsp)
leal 0x12(%rcx,%rcx,4), %eax
movl %eax, 0x20(%rsp)
movl $0xffff, %eax           # imm = 0xFFFF
pushq $0x13
popq %rdx
cmovel %eax, %edx
movl %edx, 0x18(%rsp)
pushq $0x14
popq %rdx
cmovel %eax, %edx
movl %edx, 0x1c(%rsp)
leal 0x14(%rcx,%rcx,4), %eax
movl %eax, 0x14(%rsp)
je 0xb52c1
leaq 0x76b2b(%rip), %rdx     # 0x12bda0
leaq 0x765da(%rip), %r8      # 0x12b856
pushq $0x15
jmp 0xb52e3
movl $0xffff, %edx           # imm = 0xFFFF
pushq $0x9
popq %rcx
pushq $0x7
popq %rdi
movl %edi, 0x4(%rsp)
pushq $0x8
popq %rdi
movl %edi, 0x20(%rsp)
testb %al, %al
cmovel %edx, %ecx
movl %ecx, 0x18(%rsp)
pushq $0xa
popq %rax
movl %eax, 0x14(%rsp)
cmovnel %eax, %edx
movl %edx, 0x1c(%rsp)
je 0xb52d3
leaq 0x76b1a(%rip), %rdx     # 0x12bdd0
leaq 0x76599(%rip), %r8      # 0x12b856
pushq $0xb
jmp 0xb52e3
leaq 0x76a88(%rip), %rdx     # 0x12bd50
leaq 0x76587(%rip), %r8      # 0x12b856
pushq $0x18
jmp 0xb52e3
leaq 0x76aa6(%rip), %rdx     # 0x12bd80
leaq 0x76575(%rip), %r8      # 0x12b856
pushq $0x9
popq %rcx
movq %rbx, %rdi
callq 0xb5877
movq 0x10(%r14), %rax
leaq 0x48(%r14), %rsi
cmpb $0x0, 0xa(%rax)
leaq 0x76af1(%rip), %rax     # 0x12bdf0
leaq 0x76b2a(%rip), %rdx     # 0x12be30
cmoveq %rax, %rdx
leaq 0x668b2(%rip), %r8      # 0x11bbc3
pushq $0x20
popq %rcx
movq %rbx, %rdi
callq 0xb5877
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe59cd
movq %rax, 0xd8(%r15)
movl $0x100000, %esi         # imm = 0x100000
movq %rax, %rdi
callq 0xe5a74
movq 0xd8(%r15), %rdi
callq 0xe589c
movq %rbx, %rdi
movl $0x2000, %esi           # imm = 0x2000
callq 0xef27c
movq %rax, %r12
movq %r14, (%rax)
leaq 0x3ed(%rip), %rax       # 0xb574f
movq %rax, 0x8(%r12)
movq 0x10(%r15), %rax
leaq 0x28(%r14), %rcx
movq %rcx, 0x8(%rsp)
movq %rax, 0x28(%r14)
movq %r14, %rdi
addq $0x30, %rdi
movb $0x1, 0x30(%r14)
callq 0xe6a40
movq %rbx, %rdi
movq %r14, %rsi
callq 0xb5768
cmpb $0x0, (%r15)
jne 0xb5607
movq %rbx, %rdi
movq %r14, %rsi
callq 0xb57d6
testb %al, %al
je 0xb53e9
movq 0x40(%r14), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xb5817
cmpb $0x0, (%r15)
jne 0xb5607
cmpl 0x4(%rsp), %eax
jae 0xb5417
addl $0x3, %eax
movl %eax, %ebp
movq 0x48(%r14), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xb5817
movl %eax, %r13d
shll $0x8, %r13d
jmp 0xb5427
movq 0x8(%r14), %rdi
movq 0x8(%rsp), %rsi
callq 0xe3f2a
movzbl 0x18(%r15), %ecx
cmpl $0x2, %ecx
je 0xb5505
cmpl $0x1, %ecx
jne 0xb5507
xorb 0x34(%r14), %al
jmp 0xb5507
xorl %r13d, %r13d
pushq $0x2
popq %rbp
cmpl 0x20(%rsp), %eax
jne 0xb54a7
movq 0x8(%r14), %rdi
movq 0x8(%rsp), %rsi
callq 0xe3f2a
cmpb $0x0, (%r15)
jne 0xb5607
xorb 0x1b(%r15), %al
movzbl %al, %eax
orl %eax, %r13d
cmpl $0x3, (%rbx)
jl 0xb5464
movq %rbx, %rdi
leaq 0x5c1a6(%rip), %rsi     # 0x1115fe
movl %r13d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xdea83
movq 0x20(%r14), %rcx
movl %r13d, %eax
cmpl $0x100, 0x24(%rsp)      # imm = 0x100
jne 0xb547e
leaq 0x1(%rcx), %rdx
cmpq %rax, %rdx
je 0xb5490
testl %r13d, %r13d
je 0xb55ed
cmpq %rax, %rcx
jl 0xb55ed
movl 0x10(%r12), %esi
subl %r13d, %esi
movq %r12, %rdi
movl %ebp, %edx
callq 0xef363
jmp 0xb5394
cmpl 0x4(%rsp), %eax
jne 0xb5531
movq 0x8(%r14), %rdi
movq 0x8(%rsp), %rsi
callq 0xe3f2a
movzbl %al, %ebp
cmpb $-0x3, %bpl
jb 0xb555c
cmpl $0xfd, %ebp
je 0xb557a
cmpl $0xfe, %ebp
jne 0xb56d3
cmpb $0x0, 0x19(%r15)
je 0xb56bb
movq %rbx, %rdi
leaq 0x76281(%rip), %rsi     # 0x12b77a
xorl %eax, %eax
callq 0xdea83
jmp 0xb5394
notb %al
cmpl $0x3, (%rbx)
movzbl %al, %ebp
jl 0xb5522
movq %rbx, %rdi
leaq 0x5c0ac(%rip), %rsi     # 0x1115c5
movl %ebp, %edx
xorl %eax, %eax
callq 0xdea83
movq %r12, %rdi
movl %ebp, %esi
callq 0xef336
jmp 0xb5394
cmpl 0x1c(%rsp), %eax
jne 0xb5565
cmpl $0x3, (%rbx)
jl 0xb554d
movq %rbx, %rdi
leaq 0x7626e(%rip), %rsi     # 0x12b7b4
xorl %eax, %eax
callq 0xdea83
movq %r12, %rdi
xorl %esi, %esi
callq 0xef336
jmp 0xb5394
addl 0x14(%rsp), %ebp
jmp 0xb53d1
movl $0x100, %r13d           # imm = 0x100
cmpl 0x18(%rsp), %eax
je 0xb5427
jmp 0xb56b5
cmpb $0x0, 0x19(%r15)
je 0xb56bb
movb $0x1, 0x18(%r14)
movq 0x8(%r14), %rdi
movq 0x8(%rsp), %rsi
callq 0xe3f2a
movzbl %al, %r13d
movq 0x28(%r14), %rdx
movq %rbx, %rdi
leaq 0x762f4(%rip), %rsi     # 0x12b89e
movq %r13, %rcx
xorl %eax, %eax
callq 0xdea83
pushq $0x9
popq %rbp
subq $0x1, %rbp
jb 0xb55e2
movq %r12, %rdi
xorl %esi, %esi
callq 0xef336
jmp 0xb55b7
movq 0x8(%r14), %rdi
movq 0x8(%rsp), %rsi
callq 0xe3f2a
movzbl %al, %esi
movq %r12, %rdi
callq 0xef336
subq $0x1, %r13
jae 0xb55c9
jmp 0xb5394
leaq 0x761d3(%rip), %rsi     # 0x12b7c7
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xdf2a5
movw $0x101, (%r15)          # imm = 0x101
movq 0xd8(%r15), %rdi
testq %rdi, %rdi
je 0xb5679
callq 0xe58e8
cmpb $0x0, (%r15)
jne 0xb5679
movq 0x28(%r14), %rdx
movq %rdx, 0x88(%r15)
leaq 0x5c0c1(%rip), %rsi     # 0x1116f1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x88(%r15), %rdx
movq 0xd8(%r15), %rax
subq 0x10(%r15), %rdx
movq 0x18(%rax), %rcx
leaq 0x50621(%rip), %rsi     # 0x105c78
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
cmpb $0x0, 0x18(%r14)
je 0xb5679
leaq 0x76198(%rip), %rsi     # 0x12b807
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movq 0x40(%r14), %rsi
movq %rbx, %rdi
callq 0xf86b0
movq 0x48(%r14), %rsi
movq %rbx, %rdi
callq 0xf86b0
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
pushq $-0x1
popq %rsi
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xdeba7
movb $0x1, (%r15)
jmp 0xb5679
leaq 0x760c2(%rip), %rsi     # 0x12b784
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xdf2a5
jmp 0xb5601
leaq 0x5bef6(%rip), %rsi     # 0x1115d0
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdea83
jmp 0xb5607