// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::ScanImage()
int CCapsImageStd::ScanImage()
{
	vector <ScanInfo> si;
	si.reserve(DEF_SCANINFO);
	ScanInfo empty_si = { 0, 0 };
	DiskTrackInfo dt;

	// process complete file
	for (int run = 1; run;) {
		int pos = loader.GetPosition();
		int type = loader.ReadChunk();
		PCAPSCHUNK pc = loader.GetChunk();
		int did = 0;

		switch (type) {
			// end of file
			case CCapsLoader::ccidEof:
				run = 0;
				continue;

			// remember data position
			case CCapsLoader::ccidData:
				did = pc->cg.mod.data.did;
				if ((unsigned)did >= si.size())
					si.resize(did + 1, empty_si);
				si[did].data = pos;
				loader.SkipData();
				break;

			// remember track header position
			case CCapsLoader::ccidTrck:
				did = pc->cg.mod.trck.did;
				if ((unsigned)did >= si.size())
					si.resize(did + 1, empty_si);
				si[did].track = pos;
				break;

			// handle file/format errors
			case CCapsLoader::ccidErrFile:
			case CCapsLoader::ccidErrType:
			case CCapsLoader::ccidErrShort:
			case CCapsLoader::ccidErrHeader:
			case CCapsLoader::ccidErrData:
				return imgeGeneric;

			// image info
			case CCapsLoader::ccidInfo:
				dii.ci = pc->cg.mod.info;
				dii.civalid = 1;
				continue;

			// remember track image descriptor
			case CCapsLoader::ccidImge:
				did = pc->cg.mod.imge.did;
				if ((unsigned)did >= si.size())
					si.resize(did + 1, empty_si);
				si[did].track = pos;
				break;

			// ignore other data
			default:
				continue;
		}

		// find highest data id
		if (did > dii.didmax)
			dii.didmax = did;

		// if did found, process if both header and data available
		if (did && (unsigned)did < si.size()) {
			if (si[did].track && si[did].data) {
				// reset track info
				memset(&dt, 0, sizeof(dt));

				// save image position
				int actpos = loader.GetPosition();

				// create track info
				loader.SetPosition(si[did].track);
				type = loader.ReadChunk();
				pc = loader.GetChunk();

				// set track by type
				switch (type) {
					case CCapsLoader::ccidTrck:
						dt.type = dtitCapsDump;
						dt.cylinder = pc->cg.mod.trck.cyl;
						dt.head = pc->cg.mod.trck.head;
						dii.dmpcount++;
						break;

					case CCapsLoader::ccidImge:
						dt.type = dtitCapsImage;
						dt.cylinder = pc->cg.mod.imge.cylinder;
						dt.head = pc->cg.mod.imge.head;
						dt.sectorcnt = pc->cg.mod.imge.blkcnt;
						dt.ci = pc->cg.mod.imge;
						dii.relcount++;
						break;

					default:
						return imgeGeneric;
				}

				// remember file positions
				dt.headerpos = si[did].track;
				dt.datapos = si[did].data;

				// create data info
				loader.SetPosition(si[did].data);
				loader.ReadChunk();
				dt.datasize = loader.GetDataSize();

				// write info
				int res = AddTrack(&dt);
				if (res != imgeOk)
					return res;

				// save to image is not allowed
				dii.modimage = 1;

				// restore image position
				loader.SetPosition(actpos);
			}
		}
	}

	return imgeOk;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1c0(%rbp)
leaq -0x28(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0x137d0
movq -0x1b8(%rbp), %rdi
movl $0xc8, %esi
callq 0x137f0
jmp 0xe53d
leaq -0x3c(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x4140
movl $0x1, -0x18c(%rbp)
cmpl $0x0, -0x18c(%rbp)
je 0xeb74
movq -0x1c0(%rbp), %rdi
addq $0xd0, %rdi
callq 0x15920
movl %eax, -0x1c4(%rbp)
jmp 0xe57f
movq -0x1c0(%rbp), %rdi
movl -0x1c4(%rbp), %eax
movl %eax, -0x190(%rbp)
addq $0xd0, %rdi
xorl %esi, %esi
callq 0x15380
movl %eax, -0x1c8(%rbp)
jmp 0xe5a8
movq -0x1c0(%rbp), %rdi
movl -0x1c8(%rbp), %eax
movl %eax, -0x194(%rbp)
addq $0xd0, %rdi
callq 0x138f0
movq %rax, -0x1d0(%rbp)
jmp 0xe5d0
movq -0x1d0(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movl $0x0, -0x1a4(%rbp)
movl -0x194(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x1d8(%rbp)
subl $0xc, %eax
ja 0xe814
movq -0x1d8(%rbp), %rax
leaq 0xfb68(%rip), %rcx      # 0x1e178
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x13a00
jmp 0xeb9a
movl $0x0, -0x18c(%rbp)
jmp 0xe557
movq -0x1a0(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x1a4(%rbp)
movl -0x1a4(%rbp), %eax
movq %rax, -0x1e0(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x13910
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
cmpq %rcx, %rax
jb 0xe693
movl -0x1a4(%rbp), %eax
incl %eax
movslq %eax, %rsi
leaq -0x28(%rbp), %rdi
leaq -0x3c(%rbp), %rdx
callq 0x13930
jmp 0xe691
jmp 0xe693
movl -0x190(%rbp), %eax
movl %eax, -0x1e4(%rbp)
movslq -0x1a4(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x139e0
movl -0x1e4(%rbp), %ecx
movq -0x1c0(%rbp), %rdi
movl %ecx, 0x4(%rax)
addq $0xd0, %rdi
callq 0x155c0
jmp 0xe6cd
jmp 0xe819
movq -0x1a0(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x1a4(%rbp)
movl -0x1a4(%rbp), %eax
movq %rax, -0x1f0(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x13910
movq %rax, %rcx
movq -0x1f0(%rbp), %rax
cmpq %rcx, %rax
jb 0xe723
movl -0x1a4(%rbp), %eax
incl %eax
movslq %eax, %rsi
leaq -0x28(%rbp), %rdi
leaq -0x3c(%rbp), %rdx
callq 0x13930
jmp 0xe721
jmp 0xe723
movl -0x190(%rbp), %eax
movl %eax, -0x1f4(%rbp)
movslq -0x1a4(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x139e0
movl -0x1f4(%rbp), %ecx
movl %ecx, (%rax)
jmp 0xe819
movl $0x2, -0x4(%rbp)
movl $0x1, -0x1a8(%rbp)
jmp 0xeb85
movq -0x1c0(%rbp), %rdi
movq -0x1a0(%rbp), %rsi
addq $0x4, %rsi
addq $0xc, %rsi
addq $0x8, %rdi
addq $0x44, %rdi
movl $0x54, %edx
callq 0x41a0
movq -0x1c0(%rbp), %rax
movl $0x1, 0x48(%rax)
jmp 0xe557
movq -0x1a0(%rbp), %rax
movl 0x44(%rax), %eax
movl %eax, -0x1a4(%rbp)
movl -0x1a4(%rbp), %eax
movq %rax, -0x200(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x13910
movq %rax, %rcx
movq -0x200(%rbp), %rax
cmpq %rcx, %rax
jb 0xe7ee
movl -0x1a4(%rbp), %eax
incl %eax
movslq %eax, %rsi
leaq -0x28(%rbp), %rdi
leaq -0x3c(%rbp), %rdx
callq 0x13930
jmp 0xe7ec
jmp 0xe7ee
movl -0x190(%rbp), %eax
movl %eax, -0x204(%rbp)
movslq -0x1a4(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x139e0
movl -0x204(%rbp), %ecx
movl %ecx, (%rax)
jmp 0xe819
jmp 0xe557
movq -0x1c0(%rbp), %rcx
movl -0x1a4(%rbp), %eax
cmpl 0x38(%rcx), %eax
jle 0xe83b
movq -0x1c0(%rbp), %rax
movl -0x1a4(%rbp), %ecx
movl %ecx, 0x38(%rax)
cmpl $0x0, -0x1a4(%rbp)
je 0xeb6f
movl -0x1a4(%rbp), %eax
movq %rax, -0x210(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x13910
movq %rax, %rcx
movq -0x210(%rbp), %rax
cmpq %rcx, %rax
jae 0xeb6f
movslq -0x1a4(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x139e0
cmpl $0x0, (%rax)
je 0xeb6d
movslq -0x1a4(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x139e0
cmpl $0x0, 0x4(%rax)
je 0xeb6d
leaq -0x188(%rbp), %rdi
xorl %esi, %esi
movl $0x148, %edx            # imm = 0x148
callq 0x4140
movq -0x1c0(%rbp), %rdi
addq $0xd0, %rdi
callq 0x15920
movl %eax, -0x214(%rbp)
jmp 0xe8d2
movq -0x1c0(%rbp), %rax
movl -0x214(%rbp), %ecx
movl %ecx, -0x1ac(%rbp)
addq $0xd0, %rax
movq %rax, -0x220(%rbp)
movslq -0x1a4(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x139e0
movq -0x220(%rbp), %rdi
movl (%rax), %esi
callq 0x15970
jmp 0xe912
movq -0x1c0(%rbp), %rdi
addq $0xd0, %rdi
xorl %esi, %esi
callq 0x15380
movl %eax, -0x224(%rbp)
jmp 0xe92f
movq -0x1c0(%rbp), %rdi
movl -0x224(%rbp), %eax
movl %eax, -0x194(%rbp)
addq $0xd0, %rdi
callq 0x138f0
movq %rax, -0x230(%rbp)
jmp 0xe957
movq -0x230(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movl -0x194(%rbp), %eax
movl %eax, -0x234(%rbp)
subl $0xb, %eax
je 0xe988
jmp 0xe978
movl -0x234(%rbp), %eax
subl $0xd, %eax
je 0xe9c7
jmp 0xea3a
movq -0x1c0(%rbp), %rax
movl $0x2, -0x188(%rbp)
movq -0x1a0(%rbp), %rcx
movl 0x14(%rcx), %ecx
movl %ecx, -0x178(%rbp)
movq -0x1a0(%rbp), %rcx
movl 0x18(%rcx), %ecx
movl %ecx, -0x174(%rbp)
movl 0x30(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x30(%rax)
jmp 0xea50
movl $0x3, -0x188(%rbp)
movq -0x1a0(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x178(%rbp)
movq -0x1a0(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x174(%rbp)
movq -0x1a0(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x170(%rbp)
movq -0x1a0(%rbp), %rsi
addq $0x4, %rsi
addq $0xc, %rsi
leaq -0x188(%rbp), %rdi
addq $0x100, %rdi            # imm = 0x100
movl $0x44, %edx
callq 0x41a0
movq -0x1c0(%rbp), %rax
movl 0x34(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x34(%rax)
jmp 0xea50
movl $0x2, -0x4(%rbp)
movl $0x1, -0x1a8(%rbp)
jmp 0xeb85
movslq -0x1a4(%rbp), %rsi
leaq -0x28(%rbp), %rdi
movq %rdi, -0x248(%rbp)
callq 0x139e0
movq -0x248(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x16c(%rbp)
movslq -0x1a4(%rbp), %rsi
callq 0x139e0
movq -0x248(%rbp), %rdi
movq %rax, %rcx
movq -0x1c0(%rbp), %rax
movl 0x4(%rcx), %ecx
movl %ecx, -0x168(%rbp)
addq $0xd0, %rax
movq %rax, -0x240(%rbp)
movslq -0x1a4(%rbp), %rsi
callq 0x139e0
movq -0x240(%rbp), %rdi
movl 0x4(%rax), %esi
callq 0x15970
jmp 0xeac6
movq -0x1c0(%rbp), %rdi
addq $0xd0, %rdi
xorl %esi, %esi
callq 0x15380
jmp 0xeadd
movq -0x1c0(%rbp), %rdi
addq $0xd0, %rdi
callq 0x158c0
movl %eax, -0x24c(%rbp)
jmp 0xeaf8
movq -0x1c0(%rbp), %rdi
movl -0x24c(%rbp), %eax
movl %eax, -0x164(%rbp)
leaq -0x188(%rbp), %rsi
callq 0x17ec0
movl %eax, -0x250(%rbp)
jmp 0xeb1f
movl -0x250(%rbp), %eax
movl %eax, -0x1b0(%rbp)
cmpl $0x0, -0x1b0(%rbp)
je 0xeb49
movl -0x1b0(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x1a8(%rbp)
jmp 0xeb85
movq -0x1c0(%rbp), %rdi
movl $0x1, 0x18(%rdi)
addq $0xd0, %rdi
movl -0x1ac(%rbp), %esi
callq 0x15970
jmp 0xeb6b
jmp 0xeb6d
jmp 0xeb6f
jmp 0xe557
movl $0x0, -0x4(%rbp)
movl $0x1, -0x1a8(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x13a00
movl -0x4(%rbp), %eax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x4420
nopw %cs:(%rax,%rax)