// File: /workspace/github_cpp_makefile_star2_O3/OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsLoader.cpp
// Function: CCapsLoader::ReadChunk(int)
int CCapsLoader::ReadChunk(int idbrk)
{
	// handle file error
	if (!file.IsOpen())
		return ccidErrFile;

	// skip data area if any
	SkipData();

	// check EOF
	int pos=file.GetPosition();
	if (pos == flen)
		return ccidEof;

	// check for ID chunk size
	if (flen-pos < sizeof(CapsID))
		return ccidErrShort;

	// read ID chunk
	if (file.Read((PUBYTE)&xchunk.file, sizeof(CapsID)) != sizeof(CapsID))
		return ccidErrShort;
	chunk.cg.file=xchunk.file;

	// identify chunk type if possible
	int type=GetChunkType(&chunk);

	// stop if break on unknown types
	if (idbrk && type==ccidUnknown)
		return ccidUnknown;

	// set ID to host format
	Swap(PUDWORD((PUBYTE)&chunk.cg.file+sizeof(chunk.cg.file.name)), sizeof(CapsID)-sizeof(chunk.cg.file.name));

	// load modifier for known chunk types, skip for unknown types
	int modsize=chunk.cg.file.size-sizeof(CapsID);
	if (modsize > 0) {
		// check for valid modifier size
		pos=file.GetPosition();
		if (flen-pos < modsize)
			return ccidErrShort;

		// read mod chunk if buffer has enough space or skip it
		if (modsize <= sizeof(CapsMod)) {
			// read mod chunk
			if (file.Read((PUBYTE)&xchunk.mod, modsize) != modsize)
				return ccidErrShort;

			// set header to host format
			chunk.cg.mod=xchunk.mod;
			Swap(PUDWORD((PUBYTE)&chunk.cg.mod), modsize);
		} else {
			// seek to new chunk
			file.Seek(modsize, 0);
		}
	}

	// check CRC value for completely read headers
	if (modsize>=0 && modsize<=sizeof(CapsMod)) {
		xchunk.file.hcrc=0;
		if (chunk.cg.file.hcrc != CalcCRC((PUBYTE)&xchunk.file, chunk.cg.file.size))
			return ccidErrHeader;
	}

	return type;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %r12d
movq %rdi, %rbx
leaq 0x10(%rdi), %r15
movq %r15, %rdi
callq 0x863e
testl %eax, %eax
je 0xd05f
movq %rbx, %rdi
callq 0xd114
movq %r15, %rdi
callq 0x8650
movl %eax, %ecx
movl 0x20(%rbx), %edx
movl $0x6, %eax
subl %ecx, %edx
je 0xd064
cmpl $0xc, %edx
jb 0xd058
leaq 0x24(%rbx), %r14
movq %r15, %rdi
movq %r14, %rsi
movl $0xc, %edx
callq 0x8604
cmpl $0xc, %eax
jne 0xd058
leaq 0x84(%rbx), %rdi
movl 0x8(%r14), %eax
movl %eax, 0x90(%rbx)
movq (%r14), %rax
movq %rax, 0x88(%rbx)
callq 0xd17e
movl %eax, %ebp
testl %r12d, %r12d
setne %cl
cmpl $0x7, %eax
sete %dl
movl $0x7, %eax
testb %dl, %cl
jne 0xd064
movl 0x8c(%rbx), %r12d
movl 0x90(%rbx), %eax
bswapl %r12d
movl %r12d, 0x8c(%rbx)
bswapl %eax
movl %eax, 0x90(%rbx)
addl $-0xc, %r12d
testl %r12d, %r12d
jle 0xd0ce
movq %r15, %rdi
callq 0x8650
movl 0x20(%rbx), %ecx
subl %eax, %ecx
cmpl %r12d, %ecx
jge 0xd073
movl $0x3, %eax
jmp 0xd064
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x54, %r12d
ja 0xd100
leaq 0x30(%rbx), %r13
movq %r15, %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x8604
movl %eax, %ecx
movl $0x3, %eax
cmpl %r12d, %ecx
jne 0xd064
leaq 0x94(%rbx), %rax
movl $0x15, %ecx
movq %rax, %rdi
movq %r13, %rsi
rep  movsl (%rsi), %es:(%rdi)
cmpl $0x4, %r12d
jb 0xd0d4
movl %r12d, %ecx
shrl $0x2, %ecx
incl %ecx
movl (%rax), %edx
bswapl %edx
movl %edx, (%rax)
addq $0x4, %rax
decl %ecx
cmpl $0x1, %ecx
ja 0xd0bd
cmpl $0x54, %r12d
ja 0xd10d
movl $0x0, 0x2c(%rbx)
movl 0x8c(%rbx), %esi
movl 0x90(%rbx), %ebx
movq %r14, %rdi
callq 0x5055
movl %eax, %ecx
movl $0x4, %eax
cmpl %ecx, %ebx
jne 0xd064
jmp 0xd10d
movq %r15, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x8624
movl %ebp, %eax
jmp 0xd064