// File: /workspace/github_c_makefile_star2_O2/joncampbell123[P]swfhax/swf_lzma_reader.c
// Function: swf_reader_lzma_read
int swf_reader_lzma_read(void *io,void *buf,int len) {
	unsigned char *cbuf = (unsigned char*)buf;
	swf_reader *r = (swf_reader*)io;
	int rd,accum=0;
	size_t cc;

	/* pass back the first 8 bytes uncompressed */
	while (len > 0 && r->current_pos < 8) {
		if (swf_reader_raw_seek((swf_reader*)io,r->current_pos)) return -1;
		cc = (size_t)((uint64_t)8 - r->current_pos);
		if (cc > (size_t)len) cc = (size_t)len;
		r->current_pos = r->raw_current_pos;
		rd = swf_reader_raw_read((swf_reader*)io,cbuf,(int)cc);

		/* need to patch first byte at offset 0 to "FWS" instead of "CWS" */
		if (r->current_pos == 0 && rd > 0) cbuf[0] = 'F';

		r->current_pos = r->raw_current_pos;
		if (rd < (int)cc) return rd;
		accum += rd;
		cbuf += rd;
		len -= rd;
	}

	while (len > 0) {
		assert(sliding_window_v4_is_sane(r->decom_buf));
		assert(sliding_window_v4_is_sane(r->out_buf));
		sliding_window_v4_lazy_flush(r->out_buf);
		sliding_window_v4_lazy_flush(r->decom_buf);
		if ((cc=sliding_window_v4_data_available(r->out_buf)) > 0) {
			if (cc > (size_t)len) cc = (size_t)len;
			memcpy(cbuf,r->out_buf->data,cc);
			r->out_buf->data += cc;
			r->current_pos += cc;
			accum += cc;
			cbuf += cc;
			len -= cc;

			assert(sliding_window_v4_is_sane(r->out_buf));
		}
		else {
			if (r->zlib_eof) {
				r->eof = 1;
				break;
			}

			assert(sliding_window_v4_is_sane(r->decom_buf));

			/* and then we need to translate Adobe's LZMA header format to the one
			 * the LZMA library expects... because apparently Adobe wanted to make
			 * an incompatible variant or something? */
			if (r->raw_current_pos == 8) {
				/* LZMA header format:
				 *
				 * bytes 0-4: LZMA properties
				 * bytes 5-12: Uncompressed length
				 * bytes 13+: compressed data
				 *
				 * Adobe SWF format:
				 *
				 * bytes 0-3: ZWS header
				 * bytes 4-7: Uncompressed length
				 * bytes 8-11: Compressed length
				 * bytes 12-16: LZMA properties
				 * bytes 17+: compressed data */
				sliding_window_v4_empty(r->decom_buf);
				assert(r->decom_buf->data == r->decom_buf->buffer);
				if (swf_reader_raw_seek(r,0)) return -1;
				if ((cc=sliding_window_v4_can_write(r->decom_buf)) >= 16) {
					rd = swf_reader_raw_read((swf_reader*)io,r->decom_buf->end,cc - 16);
					if (rd > 0) r->decom_buf->end += rd;
					assert(sliding_window_v4_is_sane(r->decom_buf));
					sliding_window_v4_lazy_flush(r->decom_buf);

					/* sigh... alright, now we need to modify the buffer to apply this transformation */
					/* SWF 12-16 LZMA props -> LZMA 0-4 props */
					memmove(r->decom_buf->data+4,r->decom_buf->data+12,5);
					/* dummy uncompressed length LZMA 5-12 */
					memset(r->decom_buf->data+4+5,0xFF,8);
					/* move data ptr forward 4 bytes. our storing LZMA (5) + len (8)
					 * becomes 5+8 = 13 bytes to compressed data */
					r->decom_buf->data += 4;
				}
				else {
					fprintf(stderr,"LZMA: Unable to head check\n");
					return -1;
				}
			}
			else {
				if ((cc=sliding_window_v4_can_write(r->decom_buf)) > 0) {
					rd = swf_reader_raw_read((swf_reader*)io,r->decom_buf->end,cc);
					if (rd > 0) r->decom_buf->end += rd;
					assert(sliding_window_v4_is_sane(r->decom_buf));
					sliding_window_v4_lazy_flush(r->decom_buf);
				}
			}

			r->lzma.next_in = r->decom_buf->data;
			r->lzma.avail_in = sliding_window_v4_data_available(r->decom_buf);
			r->lzma.next_out = r->out_buf->end;
			r->lzma.avail_out = sliding_window_v4_can_write(r->out_buf);
			rd = lzma_code(&r->lzma,LZMA_RUN);
			if (rd == LZMA_STREAM_END) {
				r->zlib_eof = 1;
			}
			else if (rd != LZMA_OK) {
				fprintf(stderr,"LZMA error %d\n",rd);
				r->zlib_eof = 1;
				break;
			}
			r->decom_buf->data = (unsigned char*)r->lzma.next_in;
			r->out_buf->end = r->lzma.next_out;
			assert(sliding_window_v4_is_sane(r->decom_buf));
		}
	}

	return accum;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %r15d
movq %rsi, %r12
movq %rdi, %r14
xorl %ebx, %ebx
testl %r15d, %r15d
jle 0x3e03
movq 0x8(%r14), %rsi
cmpq $0x7, %rsi
ja 0x3e03
movq %r14, %rdi
callq 0x2535
testl %eax, %eax
jne 0x408a
pushq $0x8
popq %r13
subq 0x8(%r14), %r13
movl %r15d, %eax
cmpq %rax, %r13
cmovaeq %rax, %r13
movq (%r14), %rax
movq %rax, 0x8(%r14)
movq %r14, %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0x2860
cmpq $0x0, 0x8(%r14)
jne 0x3de7
testl %eax, %eax
jle 0x3de7
movb $0x46, (%r12)
movq (%r14), %rcx
movq %rcx, 0x8(%r14)
cmpl %r13d, %eax
jl 0x4042
addl %eax, %ebx
movl %eax, %ecx
addq %rcx, %r12
subl %eax, %r15d
jmp 0x3d91
leaq 0xf8(%r14), %rax
movq %rax, -0x30(%rbp)
testl %r15d, %r15d
jle 0x408d
movq 0x78(%r14), %rdi
callq 0x4a4a
testl %eax, %eax
je 0x409e
movq 0x80(%r14), %rdi
callq 0x4a4a
testl %eax, %eax
je 0x40b7
movq 0x80(%r14), %rdi
callq 0x49dc
movq 0x78(%r14), %rdi
callq 0x49dc
movq 0x80(%r14), %rax
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rax
subq %rsi, %rax
jne 0x3fec
cmpb $0x0, 0x70(%r14)
jne 0x4046
movq 0x78(%r14), %rdi
callq 0x4a4a
testl %eax, %eax
je 0x40d0
movq 0x78(%r14), %rax
cmpq $0x8, (%r14)
jne 0x3f24
movq (%rax), %rcx
movq %rcx, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %r14, %rdi
xorl %esi, %esi
callq 0x2535
testl %eax, %eax
jne 0x408a
movq 0x78(%r14), %rax
movq 0x8(%rax), %rdx
movq 0x18(%rax), %rsi
subq %rsi, %rdx
cmpq $0x10, %rdx
jb 0x406e
addl $-0x10, %edx
movq %r14, %rdi
callq 0x2860
movq 0x78(%r14), %rdi
testl %eax, %eax
jle 0x3ee2
movl %eax, %eax
addq %rax, 0x18(%rdi)
callq 0x4a4a
testl %eax, %eax
je 0x410c
movq 0x78(%r14), %rdi
callq 0x49dc
movq 0x78(%r14), %rax
movq 0x10(%rax), %rax
movb 0x10(%rax), %cl
movb %cl, 0x8(%rax)
movl 0xc(%rax), %ecx
movl %ecx, 0x4(%rax)
movq 0x78(%r14), %rax
movq 0x10(%rax), %rax
orq $-0x1, 0x9(%rax)
movq 0x78(%r14), %rax
addq $0x4, 0x10(%rax)
jmp 0x3f63
movq 0x8(%rax), %rdx
movq 0x18(%rax), %rsi
cmpq %rsi, %rdx
je 0x3f63
subl %esi, %edx
movq %r14, %rdi
callq 0x2860
movq 0x78(%r14), %rdi
testl %eax, %eax
jle 0x3f49
movl %eax, %eax
addq %rax, 0x18(%rdi)
callq 0x4a4a
testl %eax, %eax
je 0x412b
movq 0x78(%r14), %rdi
callq 0x49dc
movq 0x78(%r14), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0xf8(%r14)
movq 0x18(%rax), %rax
subq %rcx, %rax
movq %rax, 0x100(%r14)
movq 0x80(%r14), %rax
movq 0x18(%rax), %rcx
movq %rcx, 0x110(%r14)
movq 0x8(%rax), %rax
subq %rcx, %rax
movq %rax, 0x118(%r14)
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x1050
testl %eax, %eax
je 0x3fb9
cmpl $0x1, %eax
jne 0x404d
movb $0x1, 0x70(%r14)
movq 0x78(%r14), %rdi
movq 0xf8(%r14), %rax
movq %rax, 0x10(%rdi)
movq 0x80(%r14), %rax
movq 0x110(%r14), %rcx
movq %rcx, 0x18(%rax)
callq 0x4a4a
testl %eax, %eax
jne 0x3e0e
jmp 0x40ed
movl %r15d, %r13d
cmpq %r13, %rax
cmovbq %rax, %r13
movq %r12, %rdi
movq %r13, %rdx
callq 0x1100
movq 0x80(%r14), %rdi
addq %r13, 0x10(%rdi)
addq %r13, 0x8(%r14)
addl %r13d, %ebx
addq %r13, %r12
subl %r13d, %r15d
callq 0x4a4a
testl %eax, %eax
jne 0x3e0e
leaq 0x24fb(%rip), %rdi      # 0x6528
leaq 0x24b4(%rip), %rsi      # 0x64e8
leaq 0x24bf(%rip), %rcx      # 0x64fa
pushq $0x6b
jmp 0x40e7
movl %eax, %ebx
jmp 0x408d
movb $0x1, 0x71(%r14)
jmp 0x408d
movq 0x4fa4(%rip), %rcx      # 0x8ff8
movq (%rcx), %rdi
leaq 0x250c(%rip), %rsi      # 0x656a
movl %eax, %edx
xorl %eax, %eax
callq 0x10f0
movb $0x1, 0x70(%r14)
jmp 0x408d
movq 0x4f83(%rip), %rax      # 0x8ff8
movq (%rax), %rcx
leaq 0x24cf(%rip), %rdi      # 0x654e
pushq $0x1b
popq %rsi
pushq $0x1
popq %rdx
callq 0x11a0
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x241b(%rip), %rdi      # 0x64c0
leaq 0x243c(%rip), %rsi      # 0x64e8
leaq 0x2447(%rip), %rcx      # 0x64fa
pushq $0x5e
jmp 0x40e7
leaq 0x246a(%rip), %rdi      # 0x6528
leaq 0x2423(%rip), %rsi      # 0x64e8
leaq 0x242e(%rip), %rcx      # 0x64fa
pushq $0x5f
jmp 0x40e7
leaq 0x23e9(%rip), %rdi      # 0x64c0
leaq 0x240a(%rip), %rsi      # 0x64e8
leaq 0x2415(%rip), %rcx      # 0x64fa
pushq $0x73
popq %rdx
callq 0x10a0
leaq 0x23cc(%rip), %rdi      # 0x64c0
leaq 0x23ed(%rip), %rsi      # 0x64e8
leaq 0x23f8(%rip), %rcx      # 0x64fa
movl $0xb5, %edx
callq 0x10a0
leaq 0x23ad(%rip), %rdi      # 0x64c0
leaq 0x23ce(%rip), %rsi      # 0x64e8
leaq 0x23d9(%rip), %rcx      # 0x64fa
movl $0x8c, %edx
callq 0x10a0
leaq 0x238e(%rip), %rdi      # 0x64c0
leaq 0x23af(%rip), %rsi      # 0x64e8
leaq 0x23ba(%rip), %rcx      # 0x64fa
movl $0xa1, %edx
callq 0x10a0
nop