// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::GetSampleData(ImageStreamInfo*)
int CCapsImageStd::GetSampleData(PIMAGESTREAMINFO pstr)
{
	// get current ipf encoding
	uint32_t ofs=pstr->strofs;
	uint8_t *buf=pstr->strbase;

	uint32_t bitcount, bytecount;

	while (1) {
		// check offset to be within stream boundaries
		if (ofs >= pstr->strsize)
			return imgeTrackData;

		// get stream code and sizeof(count)
		int code=buf[ofs++];

		int vc=code >> CAPS_SIZE_S;
		code&=CAPS_DATAMASK;
		uint32_t count;

		// read the value of the count parameter
		if (vc) {
			if (ofs+vc > pstr->strsize)
				return imgeTrackData;

			count=ReadValue(buf+ofs, vc);
			ofs+=vc;
		} else
			count=0;

		// support data size in bits or bytes
		if (pstr->sizemodebit) {
			bitcount=count;
			bytecount = trackbuf.CalculateByteSize(bitcount);
		} else {
			bytecount=count;
			bitcount=bytecount << 3;
		}

		switch (code) {
			// stream finished
			case cpdatEnd:
				if (bitcount)
					return imgeTrackData;

				pstr->readend=1;
				break;

			// mark sample
			case cpdatMark:
				// switch to unencoded stream
				pstr->setencmode=isiemRaw;

			// data sample use default, encoded stream
			case cpdatData:
			case cpdatGap:
				if (!bitcount)
					return imgeTrackData;

				if (ofs+bytecount > pstr->strsize)
					return imgeTrackData;
				break;

			// weak bits
			case cpdatFData:
				pstr->setencmode=isiemWeak;
				break;

			// any other data type is an error
			default:
				return imgeTrackStream;
		}

		// stop processing after complete data element or stream end
		break;
	}

	// handle normal or weak data
	if (pstr->setencmode != isiemWeak) {
		// next offset in ipf stream after the current sample
		pstr->strofs=ofs+bytecount;

		// position of the sample data, left justified, starting at bit#7
		pstr->samplebase=buf+ofs;

		// set all sample counters to a simple countdown for the whole sample
		pstr->samplebc=bitcount;
		pstr->remsamplebc=bitcount;
	} else {
		// next offset in ipf stream, no sample present for weak data
		pstr->strofs=ofs;

		// position of the weak sample data, left justified, starting at bit#7
		pstr->samplebase=pstr->weakdata;

		// set all sample counters to a simple countdown for a 8 bit sample
		pstr->samplebc=8;
		pstr->remsamplebc=8;

		// set weak data
		pstr->weakdata[0]=0;
	}

	// set total data size to sample size if undefined, or use the defined data size
	pstr->remstreambc=pstr->streambc ? pstr->streambc : bitcount;

	return imgeOk;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jb 0x11ca2
movl $0xa, -0x4(%rbp)
jmp 0x11e79
movq -0x28(%rbp), %rax
movl -0x1c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x1c(%rbp)
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
sarl $0x5, %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
andl $0x1f, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x11d0d
movl -0x1c(%rbp), %eax
addl -0x38(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jbe 0x11ced
movl $0xa, -0x4(%rbp)
jmp 0x11e79
movq -0x28(%rbp), %rdi
movl -0x1c(%rbp), %eax
addq %rax, %rdi
movl -0x38(%rbp), %esi
callq 0x18bf0
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
jmp 0x11d14
movl $0x0, -0x3c(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x11d31
movl -0x3c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x5510
movl %eax, -0x30(%rbp)
jmp 0x11d40
movl -0x3c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
shll $0x3, %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
movq %rax, -0x48(%rbp)
subq $0x5, %rax
ja 0x11dc7
movq -0x48(%rbp), %rax
leaq 0xc490(%rip), %rcx      # 0x1e1e8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, -0x2c(%rbp)
je 0x11d73
movl $0xa, -0x4(%rbp)
jmp 0x11e79
movq -0x18(%rbp), %rax
movl $0x1, 0x3c(%rax)
jmp 0x11dd3
movq -0x18(%rbp), %rax
movl $0x0, 0x48(%rax)
cmpl $0x0, -0x2c(%rbp)
jne 0x11d9d
movl $0xa, -0x4(%rbp)
jmp 0x11e79
movl -0x1c(%rbp), %eax
addl -0x30(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jbe 0x11db8
movl $0xa, -0x4(%rbp)
jmp 0x11e79
jmp 0x11dd3
movq -0x18(%rbp), %rax
movl $0x2, 0x48(%rax)
jmp 0x11dd3
movl $0x9, -0x4(%rbp)
jmp 0x11e79
jmp 0x11dd5
movq -0x18(%rbp), %rax
cmpl $0x2, 0x48(%rax)
je 0x11e14
movl -0x1c(%rbp), %ecx
addl -0x30(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x28(%rbp), %rcx
movl -0x1c(%rbp), %eax
addq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x68(%rax)
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x50(%rax)
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x58(%rax)
jmp 0x11e4c
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x18(%rbp), %rcx
addq $0x34, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x18(%rbp), %rax
movl $0x8, 0x50(%rax)
movq -0x18(%rbp), %rax
movl $0x8, 0x58(%rax)
movq -0x18(%rbp), %rax
movb $0x0, 0x34(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x4c(%rax)
je 0x11e62
movq -0x18(%rbp), %rax
movl 0x4c(%rax), %eax
movl %eax, -0x4c(%rbp)
jmp 0x11e68
movl -0x2c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x54(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)