// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::FindGapStreamEnd(ImageStreamInfo*, int)
int CCapsImageStd::FindGapStreamEnd(PIMAGESTREAMINFO pstr, int next)
{
	uint8_t *buf=di.data;
	int end=0;

	uint32_t ofs;
	for (ofs=pstr->strstart; !end && ofs < pstr->strend; ) {
		int code=buf[ofs++];

		// read code, sizeof(count), count
		int vc=code >> CAPS_SIZE_S;
		code&=CAPS_DATAMASK;
		uint32_t count;

		if (vc) {
			if (ofs+vc > pstr->strend)
				return imgeTrackData;

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

		switch (code) {
			// end of stream
			case cpgapEnd:
				end=1;
				break;

			// skip total bitcount setting, already at next element
			case cpgapCount:
				break;

			// add sample data size and go to next element
			case cpgapData:
				ofs += trackbuf.CalculateByteSize(count);
				break;

			// no other data is allowed
			default:
				return imgeTrackStream;
		}
	}

	// no cpgapEnd encountered, stream error
	if (!end)
		return imgeTrackData;

	// update stream start/end position
	if (next)
		pstr->strstart=ofs;
	else
		pstr->strend=ofs;

	// check buffer overrun for stream limit
	if (pstr->strstart >= pstr->strend)
		return imgeShort;

	// update stream pointer and maximum size
	pstr->strbase=di.data+pstr->strstart;
	pstr->strsize=pstr->strend-pstr->strstart;

	return imgeOk;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq 0x1e8(%rax), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x30(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x2c(%rbp)
movb %al, -0x49(%rbp)
jne 0x11752
movl -0x30(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x18(%rcx), %eax
setb %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
testb $0x1, %al
jne 0x1175e
jmp 0x1181c
movq -0x28(%rbp), %rax
movl -0x30(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x30(%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 0x117c9
movl -0x30(%rbp), %eax
addl -0x38(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jbe 0x117a9
movl $0xa, -0x4(%rbp)
jmp 0x11899
movq -0x28(%rbp), %rdi
movl -0x30(%rbp), %eax
addq %rax, %rdi
movl -0x38(%rbp), %esi
callq 0x18bf0
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x117d0
movl $0x0, -0x3c(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x50(%rbp)
testl %eax, %eax
je 0x117f0
jmp 0x117dc
movl -0x50(%rbp), %eax
subl $0x1, %eax
je 0x117f9
jmp 0x117e6
movl -0x50(%rbp), %eax
subl $0x2, %eax
je 0x117fb
jmp 0x1180b
movl $0x1, -0x2c(%rbp)
jmp 0x11817
jmp 0x11817
movl -0x3c(%rbp), %edi
callq 0x5510
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x11817
movl $0x9, -0x4(%rbp)
jmp 0x11899
jmp 0x11737
cmpl $0x0, -0x2c(%rbp)
jne 0x1182b
movl $0xa, -0x4(%rbp)
jmp 0x11899
cmpl $0x0, -0x1c(%rbp)
je 0x1183d
movl -0x30(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x14(%rax)
jmp 0x11847
movl -0x30(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jb 0x11860
movl $0x7, -0x4(%rbp)
jmp 0x11899
movq -0x48(%rbp), %rax
movq 0x1e8(%rax), %rcx
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
addq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x18(%rbp), %rax
subl 0x14(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x20(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)