// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq 0x210(%rdi), %rax
movl $0x2, %ebp
cmpl %esi, 0x128(%rax)
jbe 0x91fb
movq %rdi, %rbx
movq 0x1e8(%rdi), %r14
testq %r14, %r14
je 0x91fb
cmpl $0x0, 0x1f4(%rbx)
je 0x91fb
movl %esi, %r15d
movl 0x24(%rax), %ecx
leal 0x1(%r15), %r13d
shlq $0x5, %r13
movl $0x7, %ebp
cmpq %rcx, %r13
ja 0x91fb
movq %rax, 0x18(%rsp)
movl %r15d, %eax
shlq $0x5, %rax
movups (%r14,%rax), %xmm0
movups 0x10(%r14,%rax), %xmm1
leaq 0x30(%rsp), %r12
movaps %xmm1, 0x10(%r12)
movaps %xmm0, (%r12)
movq %r12, %rdi
movl $0x20, %esi
movq %rcx, %rbp
callq 0xd0e2
movq %rbp, %rdx
movl $0x7, %ebp
cmpl %edx, 0x1c(%r12)
jae 0x91fb
movq 0x18(%rsp), %r12
movl 0x128(%r12), %eax
decl %eax
cmpl %r15d, %eax
je 0x8ff4
leaq (%r14,%r13), %rdi
addq $0x1c, %rdi
movl $0x4, %esi
callq 0xf16e
movl %eax, %edx
movl 0x4c(%rsp), %eax
cmpl %edx, %eax
jae 0x91fb
movl $0xe, %ebp
cmpl $0x1, 0x40(%rsp)
jne 0x91fb
movl 0xbc(%r12), %ecx
movl %ecx, 0x8(%rsp)
movl $0x0, 0x24(%rsp)
leaq 0x9191(%rip), %rdi      # 0x121bc
movq %rbx, 0x28(%rsp)
movl %eax, %r12d
cmpl %edx, %eax
jae 0x91d4
leal 0x1(%r12), %eax
movl %r12d, %ecx
movzbl (%r14,%rcx), %ebp
xorl %r15d, %r15d
cmpl $0x20, %ebp
jb 0x9090
movl %ebp, %esi
shrl $0x5, %esi
leal (%rsi,%rax), %r13d
cmpl %edx, %r13d
ja 0x91b9
movl %eax, %edi
addq %r14, %rdi
movq %rdx, %r15
movq %r12, %rbx
movl %r8d, %r12d
callq 0xf16e
movl %r12d, %r8d
movq %rbx, %r12
movq 0x28(%rsp), %rbx
leaq 0x9135(%rip), %rdi      # 0x121bc
movq %r15, %rdx
movl %eax, %r15d
movl %r13d, %eax
andl $0x1f, %ebp
cmpl $0x5, %ebp
ja 0x91c0
movl %ebp, %ecx
movslq (%rdi,%rcx,4), %rcx
addq %rdi, %rcx
jmpq *%rcx
testl %r15d, %r15d
jne 0x91b9
movl $0x1, 0x24(%rsp)
jmp 0x91ae
movl $0xa, %ebp
testl %r15d, %r15d
je 0x91c5
leal (%r15,%rax), %esi
cmpl %edx, %esi
ja 0x91c5
testb $0x4, 0x208(%rbx)
jne 0x9150
jmp 0x91b0
movl $0xa, %ebp
testl %r15d, %r15d
je 0x91c5
leal (%r15,%rax), %esi
cmpl %edx, %esi
ja 0x91c5
testb $0x2, 0x208(%rbx)
jne 0x9150
jmp 0x91b0
xorl %ecx, %ecx
movl $0xa, %ebp
testl %r15d, %r15d
je 0x91c7
addl %eax, %r15d
movl %r15d, %esi
cmpl %edx, %r15d
jbe 0x91b0
jmp 0x91c7
movl $0xa, %ebp
testl %r15d, %r15d
je 0x91c5
leal (%r15,%rax), %esi
cmpl %edx, %esi
ja 0x91c5
testb $0x1, 0x208(%rbx)
je 0x91b0
movl 0x8(%rsp), %ebp
movslq %ebp, %rdi
movq 0x18(%rsp), %rcx
addq 0x48(%rcx), %rdi
movl %esi, 0xc(%rsp)
movl %eax, %esi
addq %r14, %rsi
movq %rdx, 0x10(%rsp)
movl %r15d, %edx
movl %r8d, %r13d
callq 0x4190
movl 0xc(%rsp), %esi
movl %r13d, %r8d
leaq 0x9035(%rip), %rdi      # 0x121bc
movq 0x10(%rsp), %rdx
addl %ebp, %r15d
movl %r15d, 0x8(%rsp)
jmp 0x91b0
testl %r15d, %r15d
je 0x91b9
movl 0x208(%rbx), %ecx
shll $0x1c, %ecx
sarl $0x1f, %ecx
andl %r15d, %ecx
addl %ecx, 0x8(%rsp)
movl %eax, %esi
movb $0x1, %cl
movl %esi, %eax
movl %r8d, %ebp
jmp 0x91c7
movl $0xa, %ebp
jmp 0x91c5
movl $0x9, %ebp
xorl %ecx, %ecx
movl %ebp, %r8d
testb %cl, %cl
jne 0x9030
jmp 0x91d7
movl %r8d, %ebp
cmpl %edx, %r12d
movq 0x18(%rsp), %rax
jb 0x91fb
cmpl $0x0, 0x24(%rsp)
je 0x91f6
movl 0x8(%rsp), %ecx
movl %ecx, 0xbc(%rax)
xorl %ebp, %ebp
jmp 0x91fb
movl $0xa, %ebp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq