// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdi, %rbx
xorps %xmm0, %xmm0
movq %rsp, %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movl $0xc8, %esi
callq 0xaa18
leaq 0x18(%rsp), %rax
andq $0x0, (%rax)
leaq 0xd0(%rbx), %r15
leaq 0x164(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq 0x4c(%rbx), %rax
movq %rax, 0x28(%rsp)
leaq 0x7128(%rip), %r14      # 0xf1f8
movq %r15, %rdi
callq 0xb1ba
movl %eax, %ebp
movq %r15, %rdi
xorl %esi, %esi
callq 0xaef0
decl %eax
cmpl $0xc, %eax
ja 0x80d0
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
movl 0x170(%rbx), %r13d
movslq %r13d, %r12
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
ja 0x8129
leaq 0x1(%r12), %rsi
movq %rsp, %rdi
leaq 0x18(%rsp), %rdx
callq 0xaaae
movq (%rsp), %rax
movl %ebp, 0x4(%rax,%r12,8)
movq %r15, %rdi
callq 0xb050
jmp 0x81c0
pushq $0x15
popq %rcx
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
rep  movsl (%rsi), %es:(%rdi)
movl $0x1, 0x48(%rbx)
jmp 0x80d0
movl 0x170(%rbx), %r13d
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
ja 0x81ba
movslq %r13d, %rsi
incq %rsi
movq %rsp, %rdi
leaq 0x18(%rsp), %rdx
callq 0xaaae
jmp 0x81b6
movl 0x198(%rbx), %r13d
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
ja 0x81ba
movslq %r13d, %rsi
incq %rsi
movq %rsp, %rdi
leaq 0x18(%rsp), %rdx
callq 0xaaae
movq (%rsp), %rax
movslq %r13d, %rcx
movl %ebp, (%rax,%rcx,8)
cmpl 0x38(%rbx), %r13d
jle 0x81ca
movl %r13d, 0x38(%rbx)
testl %r13d, %r13d
je 0x80d0
movl %r13d, %ecx
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rcx, %rdx
jbe 0x80d0
movslq %r13d, %r13
cmpl $0x0, (%rax,%r13,8)
je 0x80d0
cmpl $0x0, 0x4(%rax,%r13,8)
je 0x80d0
movl $0x148, %edx            # imm = 0x148
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x3110
movq %r15, %rdi
callq 0xb1ba
movl %eax, %ebp
movq (%rsp), %rax
movl (%rax,%r13,8), %esi
movq %r15, %rdi
callq 0xb1dc
movq %r15, %rdi
xorl %esi, %esi
callq 0xaef0
cmpl $0xb, %eax
je 0x8281
cmpl $0xd, %eax
jne 0x82f2
movl $0x3, 0x30(%rsp)
movq 0x164(%rbx), %rax
movq %rax, 0x40(%rsp)
movl 0x18c(%rbx), %eax
movl %eax, 0x48(%rsp)
pushq $0x11
popq %rcx
leaq 0x130(%rsp), %rdi
movq 0x20(%rsp), %rsi
rep  movsl (%rsi), %es:(%rdi)
incl 0x34(%rbx)
jmp 0x8298
movl $0x2, 0x30(%rsp)
movq 0x168(%rbx), %rax
movq %rax, 0x40(%rsp)
incl 0x30(%rbx)
movq (%rsp), %rax
movl (%rax,%r13,8), %ecx
movl %ecx, 0x4c(%rsp)
movl 0x4(%rax,%r13,8), %esi
movl %esi, 0x50(%rsp)
movq %r15, %rdi
callq 0xb1dc
movq %r15, %rdi
xorl %esi, %esi
callq 0xaef0
movq %r15, %rdi
callq 0xb192
movl %eax, 0x54(%rsp)
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
callq 0xc7a2
testl %eax, %eax
jne 0x8315
movl $0x1, 0x18(%rbx)
movq %r15, %rdi
movl %ebp, %esi
callq 0xb1dc
jmp 0x80d0
pushq $0x2
popq %rbx
movq %rsp, %rdi
callq 0xab2a
movl %ebx, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0x82f5
movl %eax, %ebx
jmp 0x82f5
jmp 0x8323
jmp 0x8323
jmp 0x8323
jmp 0x8323
jmp 0x8323
movq %rax, %rbx
movq %rsp, %rdi
callq 0xab2a
movq %rbx, %rdi
callq 0x3380