// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x70(%rbp)
movq 0x210(%rax), %rcx
movq %rcx, -0x20(%rbp)
movq 0x1e8(%rax), %rax
movq %rax, -0x28(%rbp)
movl -0x14(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x128(%rcx), %eax
jae 0xd3a3
cmpq $0x0, -0x28(%rbp)
je 0xd3a3
movq -0x70(%rbp), %rax
cmpl $0x0, 0x1f4(%rax)
jne 0xd3af
movl $0x2, -0x4(%rbp)
jmp 0xd75c
movq -0x20(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
shlq $0x5, %rax
movl -0x2c(%rbp), %ecx
cmpq %rcx, %rax
jbe 0xd3d9
movl $0x7, -0x4(%rbp)
jmp 0xd75c
movq -0x28(%rbp), %rax
movl -0x14(%rbp), %ecx
shlq $0x5, %rcx
movq (%rax,%rcx), %rdx
movq %rdx, -0x4c(%rbp)
movq 0x8(%rax,%rcx), %rdx
movq %rdx, -0x44(%rbp)
movq 0x10(%rax,%rcx), %rdx
movq %rdx, -0x3c(%rbp)
movq 0x18(%rax,%rcx), %rax
movq %rax, -0x34(%rbp)
leaq -0x4c(%rbp), %rdi
movl $0x20, %esi
callq 0x15710
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jb 0xd429
movl $0x7, -0x4(%rbp)
jmp 0xd75c
movl -0x14(%rbp), %eax
movq -0x20(%rbp), %rcx
movl 0x128(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0xd461
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
shlq $0x5, %rax
addq %rax, %rdi
addq $0x1c, %rdi
movl $0x4, %esi
callq 0x18bf0
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jb 0xd475
movl $0x7, -0x4(%rbp)
jmp 0xd75c
movl -0x3c(%rbp), %eax
subl $0x1, %eax
jne 0xd481
jmp 0xd47f
jmp 0xd48d
movl $0xe, -0x4(%rbp)
jmp 0xd75c
movl $0x0, -0x50(%rbp)
movq -0x20(%rbp), %rax
movl 0xbc(%rax), %eax
movl %eax, -0x54(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0xd739
movq -0x28(%rbp), %rax
movl -0x58(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x58(%rbp)
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
sarl $0x5, %eax
movl %eax, -0x60(%rbp)
movl -0x5c(%rbp), %eax
andl $0x1f, %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x60(%rbp)
je 0xd51a
movl -0x58(%rbp), %eax
addl -0x60(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jbe 0xd4fa
movl $0xa, -0x4(%rbp)
jmp 0xd75c
movq -0x28(%rbp), %rdi
movl -0x58(%rbp), %eax
addq %rax, %rdi
movl -0x60(%rbp), %esi
callq 0x18bf0
movl %eax, -0x64(%rbp)
movl -0x60(%rbp), %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0xd521
movl $0x0, -0x64(%rbp)
movl -0x5c(%rbp), %eax
movq %rax, -0x78(%rbp)
subq $0x5, %rax
ja 0xd72b
movq -0x78(%rbp), %rax
leaq 0x10c13(%rip), %rcx     # 0x1e150
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, -0x64(%rbp)
je 0xd558
movl $0xa, -0x4(%rbp)
jmp 0xd75c
movl $0x1, -0x50(%rbp)
jmp 0xd734
cmpl $0x0, -0x64(%rbp)
jne 0xd576
movl $0xa, -0x4(%rbp)
jmp 0xd75c
movl -0x58(%rbp), %eax
addl -0x64(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jbe 0xd58d
movl $0xa, -0x4(%rbp)
jmp 0xd75c
movq -0x70(%rbp), %rax
movl 0x208(%rax), %eax
andq $0x2, %rax
cmpq $0x0, %rax
je 0xd5cd
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdi
movslq -0x54(%rbp), %rax
addq %rax, %rdi
movq -0x28(%rbp), %rsi
movl -0x58(%rbp), %eax
addq %rax, %rsi
movl -0x64(%rbp), %eax
movl %eax, %edx
callq 0x41a0
movl -0x64(%rbp), %eax
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x64(%rbp), %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0xd734
cmpl $0x0, -0x64(%rbp)
jne 0xd5ed
movl $0xa, -0x4(%rbp)
jmp 0xd75c
movl -0x58(%rbp), %eax
addl -0x64(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jbe 0xd604
movl $0xa, -0x4(%rbp)
jmp 0xd75c
movl -0x64(%rbp), %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0xd734
cmpl $0x0, -0x64(%rbp)
jne 0xd624
movl $0xa, -0x4(%rbp)
jmp 0xd75c
movl -0x58(%rbp), %eax
addl -0x64(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jbe 0xd63b
movl $0xa, -0x4(%rbp)
jmp 0xd75c
movq -0x70(%rbp), %rax
movl 0x208(%rax), %eax
andq $0x1, %rax
cmpq $0x0, %rax
je 0xd67b
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdi
movslq -0x54(%rbp), %rax
addq %rax, %rdi
movq -0x28(%rbp), %rsi
movl -0x58(%rbp), %eax
addq %rax, %rsi
movl -0x64(%rbp), %eax
movl %eax, %edx
callq 0x41a0
movl -0x64(%rbp), %eax
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x64(%rbp), %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0xd734
cmpl $0x0, -0x64(%rbp)
jne 0xd69b
movl $0xa, -0x4(%rbp)
jmp 0xd75c
movl -0x58(%rbp), %eax
addl -0x64(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jbe 0xd6b2
movl $0xa, -0x4(%rbp)
jmp 0xd75c
movq -0x70(%rbp), %rax
movl 0x208(%rax), %eax
andq $0x4, %rax
cmpq $0x0, %rax
je 0xd6f2
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdi
movslq -0x54(%rbp), %rax
addq %rax, %rdi
movq -0x28(%rbp), %rsi
movl -0x58(%rbp), %eax
addq %rax, %rsi
movl -0x64(%rbp), %eax
movl %eax, %edx
callq 0x41a0
movl -0x64(%rbp), %eax
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x64(%rbp), %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0xd734
cmpl $0x0, -0x64(%rbp)
jne 0xd70c
movl $0xa, -0x4(%rbp)
jmp 0xd75c
movq -0x70(%rbp), %rax
movl 0x208(%rax), %eax
andq $0x8, %rax
cmpq $0x0, %rax
je 0xd729
movl -0x64(%rbp), %eax
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
jmp 0xd734
movl $0x9, -0x4(%rbp)
jmp 0xd75c
jmp 0xd4a7
cmpl $0x0, -0x50(%rbp)
jne 0xd748
movl $0xa, -0x4(%rbp)
jmp 0xd75c
movl -0x54(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xbc(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)