// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::GetSampleRaw(ImageStreamInfo*)
int CCapsImageStd::GetSampleRaw(PIMAGESTREAMINFO pstr)
{
	// check current offset in ipf encoding
	uint32_t ofs=pstr->strofs;

	if (ofs >= pstr->strsize)
		return imgeTrackData;

	// get stream code and sizeof(count)
	uint8_t *buf=pstr->strbase;
	int code=buf[ofs++];

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

	// 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;

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

			pstr->readend=1;
			bitcount=0;
			break;

		// raw data sample, size in bytes
		case cpdatRaw:
			if (!count)
				return imgeTrackData;

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

			bitcount=count << 3;
			break;

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

	// next offset in ipf stream after the current sample
	pstr->strofs=ofs+count;

	// 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->remstreambc=bitcount;
	pstr->remsamplebc=bitcount;

	return imgeOk;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jb 0x12092
movl $0xa, -0x4(%rbp)
jmp 0x121cb
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x28(%rbp)
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, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
sarl $0x5, %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
andl $0x1f, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x12109
movl -0x1c(%rbp), %eax
addl -0x30(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jbe 0x120e9
movl $0xa, -0x4(%rbp)
jmp 0x121cb
movq -0x28(%rbp), %rdi
movl -0x1c(%rbp), %eax
addq %rax, %rdi
movl -0x30(%rbp), %esi
callq 0x18bf0
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
jmp 0x12110
movl $0x0, -0x34(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x3c(%rbp)
testl %eax, %eax
je 0x12126
jmp 0x1211c
movl -0x3c(%rbp), %eax
subl $0x4, %eax
je 0x1214c
jmp 0x1217e
cmpl $0x0, -0x34(%rbp)
je 0x12138
movl $0xa, -0x4(%rbp)
jmp 0x121cb
movq -0x18(%rbp), %rax
movl $0x1, 0x3c(%rax)
movl $0x0, -0x38(%rbp)
jmp 0x12187
cmpl $0x0, -0x34(%rbp)
jne 0x1215b
movl $0xa, -0x4(%rbp)
jmp 0x121cb
movl -0x1c(%rbp), %eax
addl -0x34(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jbe 0x12173
movl $0xa, -0x4(%rbp)
jmp 0x121cb
movl -0x34(%rbp), %eax
shll $0x3, %eax
movl %eax, -0x38(%rbp)
jmp 0x12187
movl $0x9, -0x4(%rbp)
jmp 0x121cb
movl -0x1c(%rbp), %ecx
addl -0x34(%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 -0x38(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x50(%rax)
movl -0x38(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x54(%rax)
movl -0x38(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x58(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)