// File: 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
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 0x11ed2
movl $0xa, -0x4(%rbp)
jmp 0x12055
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 0x11f3d
movl -0x1c(%rbp), %eax
addl -0x38(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jbe 0x11f1d
movl $0xa, -0x4(%rbp)
jmp 0x12055
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 0x11f44
movl $0x0, -0x3c(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x11f61
movl -0x3c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x5510
movl %eax, -0x30(%rbp)
jmp 0x11f70
movl -0x3c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
shll $0x3, %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x40(%rbp)
testl %eax, %eax
je 0x11f90
jmp 0x11f7c
movl -0x40(%rbp), %eax
subl $0x1, %eax
je 0x11faf
jmp 0x11f86
movl -0x40(%rbp), %eax
subl $0x2, %eax
je 0x11fbe
jmp 0x11fea
cmpl $0x0, -0x2c(%rbp)
je 0x11fa2
movl $0xa, -0x4(%rbp)
jmp 0x12055
movq -0x18(%rbp), %rax
movl $0x1, 0x3c(%rax)
jmp 0x11ff3
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4c(%rax)
jmp 0x11eba
cmpl $0x0, -0x2c(%rbp)
jne 0x11fd0
movl $0xa, -0x4(%rbp)
jmp 0x12055
movl -0x1c(%rbp), %eax
addl -0x30(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jbe 0x11fe8
movl $0xa, -0x4(%rbp)
jmp 0x12055
jmp 0x11ff3
movl $0x9, -0x4(%rbp)
jmp 0x12055
jmp 0x11ff5
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)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x4c(%rax)
je 0x1203e
movq -0x18(%rbp), %rax
movl 0x4c(%rax), %eax
movl %eax, -0x44(%rbp)
jmp 0x12044
movl -0x2c(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x54(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop