// File: /workspace/github_cpp_makefile_star2_O2/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, %r15d
movq %rdi, %rbx
leaq 0x10(%rdi), %r12
movq %r12, %rdi
callq 0x7076
testl %eax, %eax
je 0xaf2c
movq %rbx, %rdi
callq 0xb050
movq %r12, %rdi
callq 0x7088
movl 0x20(%rbx), %ecx
subl %eax, %ecx
jne 0xaf40
pushq $0x6
jmp 0xaf2e
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x3
popq %rbp
cmpl $0xc, %ecx
jb 0xaf2f
leaq 0x24(%rbx), %r14
pushq $0xc
popq %r13
movq %r12, %rdi
movq %r14, %rsi
movl %r13d, %edx
callq 0x703c
cmpl $0xc, %eax
jne 0xaf2f
leaq 0x84(%rbx), %rdi
movl 0x8(%r14), %eax
movl %eax, 0x90(%rbx)
movq (%r14), %rax
movq %rax, 0x88(%rbx)
callq 0xb0ba
testl %r15d, %r15d
setne %dl
cmpl $0x7, %eax
sete %cl
testb %cl, %dl
je 0xaf97
pushq $0x7
jmp 0xaf2e
movl %eax, 0x4(%rsp)
leaq 0x8c(%rbx), %rdi
pushq $0x8
popq %rsi
callq 0xb0e0
movl 0x8c(%rbx), %r15d
addl $-0xc, %r15d
testl %r15d, %r15d
jle 0xb017
movq %r12, %rdi
callq 0x7088
movl 0x20(%rbx), %ecx
subl %eax, %ecx
cmpl %r15d, %ecx
jl 0xaf2f
cmpl $0x54, %r15d
ja 0xb039
leaq 0x30(%rbx), %r13
movq %r12, %rdi
movq %r13, %rsi
movl %r15d, %edx
callq 0x703c
cmpl %r15d, %eax
jne 0xaf2f
leaq 0x94(%rbx), %rax
pushq $0x15
popq %rcx
movq %rax, %rdi
movq %r13, %rsi
rep  movsl (%rsi), %es:(%rdi)
movq %rax, %rdi
movl %r15d, %esi
callq 0xb0e0
movl 0x8c(%rbx), %r13d
jmp 0xb019
jne 0xb046
andl $0x0, 0x2c(%rbx)
movl 0x90(%rbx), %ebx
movq %r14, %rdi
movl %r13d, %esi
callq 0x3f42
cmpl %eax, %ebx
je 0xb046
pushq $0x4
jmp 0xaf2e
movq %r12, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x705c
movl 0x4(%rsp), %ebp
jmp 0xaf2f
nop