// File: /workspace/github_cpp_makefile_star2_O1/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 0x881a
testl %eax, %eax
je 0xcfe5
movq %rbx, %rdi
callq 0xd03a
movq %r15, %rdi
callq 0x882c
movl 0x20(%rbx), %ecx
movl $0x6, %r13d
subl %eax, %ecx
je 0xcfeb
movl $0x3, %r13d
cmpl $0xc, %ecx
jb 0xcfeb
leaq 0x24(%rbx), %r14
movq %r15, %rdi
movq %r14, %rsi
movl $0xc, %edx
callq 0x87e0
cmpl $0xc, %eax
jne 0xcfeb
leaq 0x84(%rbx), %rdi
movl 0x8(%r14), %eax
movl %eax, 0x90(%rbx)
movq (%r14), %rax
movq %rax, 0x88(%rbx)
callq 0xd0a4
movl %eax, %ebp
testl %r12d, %r12d
setne %al
cmpl $0x7, %ebp
sete %cl
movl $0x7, %r13d
testb %cl, %al
jne 0xcfeb
leaq 0x8c(%rbx), %rax
movl $0x3, %ecx
movq %rax, %rdx
movl (%rdx), %esi
bswapl %esi
movl %esi, (%rdx)
addq $0x4, %rdx
decl %ecx
cmpl $0x1, %ecx
ja 0xcf57
movl (%rax), %r12d
addl $-0xc, %r12d
testl %r12d, %r12d
jle 0xd00a
movq %r15, %rdi
callq 0x882c
movl 0x20(%rbx), %ecx
subl %eax, %ecx
movl $0x3, %r13d
cmpl %r12d, %ecx
jl 0xcfeb
cmpl $0x54, %r12d
ja 0xcffd
leaq 0x30(%rbx), %rax
movq %r15, %rdi
movq %rax, %r15
movq %rax, %rsi
movl %r12d, %edx
callq 0x87e0
cmpl %r12d, %eax
jne 0xcfeb
leaq 0x94(%rbx), %rax
movl $0x15, %ecx
movq %rax, %rdi
movq %r15, %rsi
rep  movsl (%rsi), %es:(%rdi)
cmpl $0x4, %r12d
jb 0xd00a
movl %r12d, %ecx
shrl $0x2, %ecx
incl %ecx
movl (%rax), %edx
bswapl %edx
movl %edx, (%rax)
addq $0x4, %rax
decl %ecx
cmpl $0x1, %ecx
jg 0xcfd2
jmp 0xd00a
movl $0x1, %r13d
movl %r13d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x8800
cmpl $0x54, %r12d
ja 0xd035
movl $0x0, 0x2c(%rbx)
movl 0x8c(%rbx), %esi
movl 0x90(%rbx), %ebx
movq %r14, %rdi
callq 0x506f
movl $0x4, %r13d
cmpl %eax, %ebx
jne 0xcfeb
movl %ebp, %r13d
jmp 0xcfeb