// File: /workspace/github_cpp_makefile_star2_O2/OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::GetSampleGap(ImageStreamInfo*)
int CCapsImageStd::GetSampleGap(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 cpgapEnd:
				if (bitcount)
					return imgeTrackData;

				pstr->readend=1;
				break;

			// set total sample size
			case cpgapCount:
				pstr->streambc=bitcount;
				continue;

			// data sample
			case cpgapData:
				if (!bitcount)
					return imgeTrackData;

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

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

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

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

	// 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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movl 0x1c(%rsi), %ecx
movq 0x28(%rsi), %r14
pushq $0xa
popq %r15
pushq $0x9
popq %rbp
movl 0x20(%rbx), %edx
cmpl %edx, %ecx
jae 0x9d9a
movl %ecx, %eax
incl %ecx
movzbl (%r14,%rax), %r13d
xorl %eax, %eax
cmpl $0x20, %r13d
jae 0x9cf5
movl %ecx, %r12d
jmp 0x9d12
movl %r13d, %esi
shrl $0x5, %esi
leal (%rsi,%rcx), %r12d
cmpl %edx, %r12d
ja 0x9d9a
movl %ecx, %edi
addq %r14, %rdi
callq 0xce28
andl $0x1f, %r13d
movl 0x10(%rbx), %edx
leal (,%rax,8), %ecx
testl %edx, %edx
cmovnel %eax, %ecx
cmpl $0x1, %r13d
jne 0x9d33
movl %ecx, 0x4c(%rbx)
movl %r12d, %ecx
jmp 0x9cd4
cmpl $0x2, %r13d
je 0x9d5e
testl %r13d, %r13d
jne 0x9d9d
movl %r15d, %ebp
testl %ecx, %ecx
jne 0x9d9d
leal 0x7(%rax), %esi
shrl $0x3, %esi
testl %edx, %edx
cmovel %eax, %esi
movl $0x1, 0x3c(%rbx)
addl %r12d, %esi
xorl %ecx, %ecx
jmp 0x9d78
testl %ecx, %ecx
je 0x9d9a
leal 0x7(%rax), %esi
shrl $0x3, %esi
testl %edx, %edx
cmovel %eax, %esi
addl %r12d, %esi
movl %r15d, %ebp
cmpl 0x20(%rbx), %esi
ja 0x9d9d
movl %esi, 0x1c(%rbx)
movl %r12d, %eax
addq %rax, %r14
movq %r14, 0x68(%rbx)
movl %ecx, 0x50(%rbx)
movl %ecx, 0x58(%rbx)
movl 0x4c(%rbx), %eax
testl %eax, %eax
cmovel %ecx, %eax
movl %eax, 0x54(%rbx)
xorl %ebp, %ebp
jmp 0x9d9d
movl %r15d, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq