// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::ProcessStreamWeak(ImageStreamInfo*)
void CCapsImageStd::ProcessStreamWeak(PIMAGESTREAMINFO pstr)
{
	// get process state
	uint32_t bitpos=pstr->prcbitpos;
	int maxbc=pstr->prcrembc;
	int skipbc=pstr->prcskipbc;
	int actbc=0;
		
	// process remaining bits
	if (maxbc > 0) {
		// get weak field size without encoding
		int writebc=pstr->remstreambc;

		// skip reading the sample associated with this field and get the next sample
		ReadSampleInit(pstr);

		// calculate field size after encoding
		switch (pstr->actenctype) {
			case cpencMFM:
				writebc <<= 1;
				break;
		}

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

			// limit to available bit size
			if (writebc > maxbc)
				writebc=maxbc;

			// mark weak data, use group#0 for IPF image
			DiskDataMark ddm;
			ddm.group = 0;
			ddm.position=bitpos;
			ddm.size=writebc;
			AddFD(di.pdt, &ddm, 1, DEF_FDALLOC);

			// clear field
			trackbuf.ClearBitWrap(bitpos, 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;
	}

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

	// 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 0x128c4
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x54(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rsi
callq 0x119c0
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
subl $0x1, %eax
jne 0x12804
jmp 0x127fc
movl -0x24(%rbp), %eax
shll %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jle 0x128b7
movl -0x1c(%rbp), %ecx
movl -0x24(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x24(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x18(%rbp), %eax
jle 0x12830
movl -0x18(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x38(%rbp), %rax
movl $0x0, -0x30(%rbp)
movl -0x14(%rbp), %ecx
movl %ecx, -0x2c(%rbp)
movl -0x24(%rbp), %ecx
movl %ecx, -0x28(%rbp)
movq 0x210(%rax), %rdi
leaq -0x30(%rbp), %rsi
movl $0x1, %edx
movl $0x8, %ecx
callq 0x18c40
movq -0x38(%rbp), %rdi
addq $0xb8, %rdi
movl -0x14(%rbp), %esi
movl -0x24(%rbp), %edx
callq 0x13b00
movq -0x38(%rbp), %rcx
movl -0x24(%rbp), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x24(%rbp), %edx
movl -0x18(%rbp), %eax
subl %edx, %eax
movl %eax, -0x18(%rbp)
movl -0x24(%rbp), %eax
addl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl 0x1c0(%rcx), %eax
jb 0x128b5
movq -0x38(%rbp), %rax
movl 0x1c0(%rax), %ecx
movl -0x14(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x14(%rbp)
jmp 0x128c2
movl -0x24(%rbp), %ecx
movl -0x1c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
jmp 0x128c4
movq -0x10(%rbp), %rax
cmpl $0x0, 0x80(%rax)
jne 0x12902
movq -0x10(%rbp), %rax
cmpl $0x0, 0x7c(%rax)
je 0x12902
cmpl $0x0, -0x20(%rbp)
je 0x12902
movq -0x38(%rbp), %rax
movq 0x1f8(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x4(%rcx), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl $0x3, 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)