// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::ProcessStreamRaw(ImageStreamInfo*)
void CCapsImageStd::ProcessStreamRaw(PIMAGESTREAMINFO pstr)
{
	// get process state
	uint32_t bitpos=pstr->prcbitpos;
	int maxbc=pstr->prcrembc;
	int skipbc=pstr->prcskipbc;
	int actbc=0;

	// process remaining bits
	while (maxbc > 0) {
		// read the ipf stream
		int readbc=ReadSample(pstr, rawreadlen);

		// number of bits not read/missing
		int diffbc=rawreadlen-readbc;

		// write track data if anything was read
		if (readbc > 0) {
			// number of bits to be written
			int writebc=readbc;

			// skip bits if needed and write bitfield
			if (writebc > skipbc) {
				writebc-=skipbc;
				skipbc=0;

				// get value read
				uint32_t value=pstr->readvalue;

				// if less bits to write than available, right align field to bit#0
				if (writebc > maxbc) {
					value >>= (writebc-maxbc);
					writebc=maxbc;
				}

				trackbuf.WriteBitWrap(bitpos, value, writebc);

				// next write position
				actbc+=writebc;
				maxbc-=writebc;
				bitpos+=writebc;

				// handle track buffer wrap around (disk rotation)
				if (bitpos >= di.trackbc)
					bitpos-=di.trackbc;
			} else
				skipbc-=writebc;
		}

		// stop processing if encoding method changed or stream stopped 
		if (diffbc)
			break;
	}

	// set the type of the very first data
	if (!pstr->prcwritebc && pstr->prcencstate && actbc)
		di.block[pstr->actblock].fdenc=ibieRaw;

	// save process state
	pstr->prcbitpos=bitpos;
	pstr->prcrembc=maxbc;
	pstr->prcskipbc=skipbc;
	pstr->prcwritebc+=actbc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl 0x70(%rax), %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movl 0x74(%rax), %eax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl 0x78(%rax), %eax
movl %eax, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
cmpl $0x0, -0x18(%rbp)
jle 0x12496
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl 0x21c(%rdi), %edx
callq 0x11ac0
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, -0x24(%rbp)
movl 0x21c(%rax), %eax
subl -0x24(%rbp), %eax
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x24(%rbp)
jle 0x12489
movl -0x24(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jle 0x1247c
movl -0x1c(%rbp), %ecx
movl -0x2c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl 0x40(%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jle 0x12423
movl -0x2c(%rbp), %ecx
subl -0x18(%rbp), %ecx
movl -0x30(%rbp), %eax
shrl %cl, %eax
movl %eax, -0x30(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rdi
addq $0xb8, %rdi
movl -0x14(%rbp), %esi
movl -0x30(%rbp), %edx
movl -0x2c(%rbp), %ecx
callq 0x13a60
movq -0x38(%rbp), %rcx
movl -0x2c(%rbp), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x2c(%rbp), %edx
movl -0x18(%rbp), %eax
subl %edx, %eax
movl %eax, -0x18(%rbp)
movl -0x2c(%rbp), %eax
addl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl 0x1c0(%rcx), %eax
jb 0x1247a
movq -0x38(%rbp), %rax
movl 0x1c0(%rax), %ecx
movl -0x14(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x14(%rbp)
jmp 0x12487
movl -0x2c(%rbp), %ecx
movl -0x1c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
jmp 0x12489
cmpl $0x0, -0x28(%rbp)
je 0x12491
jmp 0x12496
jmp 0x1239d
movq -0x10(%rbp), %rax
cmpl $0x0, 0x80(%rax)
jne 0x124d4
movq -0x10(%rbp), %rax
cmpl $0x0, 0x7c(%rax)
je 0x124d4
cmpl $0x0, -0x20(%rbp)
je 0x124d4
movq -0x38(%rbp), %rax
movq 0x1f8(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x4(%rcx), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl $0x1, 0x20(%rax)
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x70(%rax)
movl -0x18(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x74(%rax)
movl -0x1c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x78(%rax)
movl -0x20(%rbp), %ecx
movq -0x10(%rbp), %rax
addl 0x80(%rax), %ecx
movl %ecx, 0x80(%rax)
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)