// File: /workspace/github_cpp_makefile_star2_O2/OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImage.cpp
// Function: CCapsImage::CompareBlock(unsigned int)
int CCapsImage::CompareBlock(unsigned blk)
{
	PDISKTRACKINFO pti=di.pdt;
	uint8_t *buf=di.data;

	// check block number requested
	if (blk>=pti->ci.blkcnt || !buf || !di.datacount)
		return imgeGeneric;

	UDWORD maxofs=pti->datasize;

	// check buffer overrun
	if ((blk+1)*sizeof(CapsBlock) > maxofs)
		return imgeShort;

	// read block data in native format
	CapsBlock cb;
	memcpy(&cb, buf+blk*sizeof(CapsBlock), sizeof(CapsBlock));
	CCapsLoader::Swap((PUDWORD)&cb, sizeof(CapsBlock));

	// check buffer overrun
	if (cb.dataoffset >= maxofs)
		return imgeShort;

	// block stream limit is either the next block or buffer end
	if (blk != pti->ci.blkcnt-1)
		maxofs=ReadValue(buf+(blk+1)*sizeof(CapsBlock)+offsetof(CapsBlock, dataoffset), sizeof(UDWORD));

	// check buffer overrun
	if (cb.dataoffset >= maxofs)
		return imgeShort;

	// check encoder type
	switch (cb.enctype) {
		case cpencMFM:
			break;

		default:
			return imgeIncompatible;
	}

	// decode stream
	int end=false;
	int pos=pti->comppos;
	for (UDWORD ofs=cb.dataoffset; ofs < maxofs; ) {
		int code=buf[ofs++];

		// read count
		int vc=code>>CAPS_SIZE_S;
		code&=CAPS_DATAMASK;
		UDWORD count;

		if (vc) {
			if (ofs+vc > maxofs)
				return imgeTrackData;

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

		// process data
		switch (code) {
			// stream end
			case cpdatEnd:
				// safety checks
				if (count)
					return imgeTrackData;
				end=true;
				break;

			case cpdatData:
				// safety checks
				if (!count)
					return imgeTrackData;
				if (ofs+count > maxofs)
					return imgeTrackData;

				// copy data if requested
				if (di.flag & DI_COMP_DATA) {
					memcpy(pti->trackbuf+pos, buf+ofs, count);
					pos+=count;
				}
				ofs+=count;
				break;

			case cpdatGap:
				// safety checks
				if (!count)
					return imgeTrackData;
				if (ofs+count > maxofs)
					return imgeTrackData;

				// skip gap data
				ofs+=count;
				break;

			case cpdatMark:
				// safety checks
				if (!count)
					return imgeTrackData;
				if (ofs+count > maxofs)
					return imgeTrackData;

				// copy raw data if requested
				if (di.flag & DI_COMP_MARK) {
					memcpy(pti->trackbuf+pos, buf+ofs, count);
					pos+=count;
				}
				ofs+=count;
				break;

			case cpdatRaw:
				// safety checks
				if (!count)
					return imgeTrackData;
				if (ofs+count > maxofs)
					return imgeTrackData;

				// copy raw data if requested
				if (di.flag & DI_COMP_RAW) {
					memcpy(pti->trackbuf+pos, buf+ofs, count);
					pos+=count;
				}
				ofs+=count;
				break;

			case cpdatFData:
				// safety checks
				if (!count)
					return imgeTrackData;

				// set flakey data to 0 (memory already cleared) if requested
				if (di.flag & DI_COMP_FDATA)
					pos+=count;
				break;

			default:
				return imgeTrackStream;
		}
	}

	// no cpdatEnd encountered, definitely a problem
	if (!end)
		return imgeTrackData;

	pti->comppos=pos;
	return imgeOk;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq 0x210(%rdi), %rdx
pushq $0x2
popq %rcx
cmpl %esi, 0x128(%rdx)
jbe 0x7917
movq %rdi, %r14
movq 0x1e8(%rdi), %r13
testq %r13, %r13
je 0x7917
cmpl $0x0, 0x1f4(%r14)
je 0x7917
movl %esi, %ebp
movl 0x24(%rdx), %ecx
movl %esi, %eax
movq %rax, %r12
shlq $0x5, %r12
addq $0x20, %r12
cmpq %rcx, %r12
jbe 0x7712
pushq $0x7
popq %rcx
jmp 0x7917
movq %rdx, 0x8(%rsp)
shlq $0x5, %rax
movups (%r13,%rax), %xmm0
movups 0x10(%r13,%rax), %xmm1
leaq 0x20(%rsp), %rbx
movaps %xmm1, 0x10(%rbx)
movaps %xmm0, (%rbx)
pushq $0x20
popq %rsi
movq %rbx, %rdi
movq %rcx, %r15
callq 0xb0e0
movq %r15, %r8
movl 0x1c(%rbx), %r15d
pushq $0x7
popq %rcx
cmpl %r8d, %r15d
jae 0x7917
movq 0x8(%rsp), %rdx
movl 0x128(%rdx), %eax
decl %eax
cmpl %ebp, %eax
je 0x7786
leaq (%r12,%r13), %rdi
addq $0x1c, %rdi
pushq $0x4
popq %rsi
movl %ecx, %ebx
callq 0xce28
movq 0x8(%rsp), %rdx
movl %ebx, %ecx
movl %eax, %r8d
movl 0x3c(%rsp), %r15d
cmpl %r8d, %r15d
jae 0x7917
cmpl $0x1, 0x30(%rsp)
jne 0x78f1
movl 0xbc(%rdx), %r12d
movl $0x0, 0x14(%rsp)
leaq 0x7a20(%rip), %rbp      # 0xf1d0
movq %r8, 0x18(%rsp)
cmpl %r8d, %r15d
jae 0x78ff
movl %r15d, %eax
incl %r15d
movzbl (%r13,%rax), %ebx
xorl %ecx, %ecx
cmpl $0x20, %ebx
jb 0x780b
movq %r13, %rbp
movl %r12d, %r13d
movl %ebx, %esi
shrl $0x5, %esi
leal (%rsi,%r15), %r12d
cmpl %r8d, %r12d
ja 0x78f8
movl %r15d, %edi
addq %rbp, %rdi
callq 0xce28
movq 0x18(%rsp), %r8
movl %eax, %ecx
movl %r12d, %r15d
movl %r13d, %r12d
movq %rbp, %r13
leaq 0x79c5(%rip), %rbp      # 0xf1d0
andl $0x1f, %ebx
cmpl $0x5, %ebx
ja 0x7928
movl %ebx, %eax
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
movslq %r12d, %rdi
movl %r15d, %esi
movl %ecx, %edx
jmpq *%rax
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
testl %ecx, %ecx
je 0x77b5
jmp 0x78f8
testl %ecx, %ecx
je 0x78f8
addl %ecx, %r15d
cmpl %r8d, %r15d
ja 0x78f8
testb $0x4, 0x208(%r14)
je 0x77b5
jmp 0x78b6
testl %ecx, %ecx
je 0x78f8
addl %ecx, %r15d
cmpl %r8d, %r15d
ja 0x78f8
testb $0x2, 0x208(%r14)
je 0x77b5
jmp 0x78b6
testl %ecx, %ecx
je 0x78f8
addl %r15d, %ecx
movl %ecx, %r15d
cmpl %r8d, %ecx
jbe 0x77b5
jmp 0x78f8
testl %ecx, %ecx
je 0x78f8
addl %ecx, %r15d
cmpl %r8d, %r15d
ja 0x78f8
testb $0x1, 0x208(%r14)
je 0x77b5
movq 0x8(%rsp), %rax
addq 0x48(%rax), %rdi
addq %r13, %rsi
movl %ecx, %ebx
callq 0x3160
movq 0x18(%rsp), %r8
addl %ebx, %r12d
jmp 0x77b5
testl %ecx, %ecx
je 0x78f8
movl 0x208(%r14), %eax
shll $0x1c, %eax
sarl $0x1f, %eax
andl %ecx, %eax
addl %eax, %r12d
jmp 0x77b5
pushq $0xe
jmp 0x770c
pushq $0xa
jmp 0x770c
cmpl $0x0, 0x14(%rsp)
pushq $0xa
popq %rcx
movq 0x8(%rsp), %rax
je 0x7917
movl %r12d, 0xbc(%rax)
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x9
jmp 0x770c
nop