// File: 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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x28(%rbp)
addq $0x10, %rdi
callq 0xc790
cmpl $0x0, %eax
jne 0x153b1
movl $0x1, -0x4(%rbp)
jmp 0x155b0
movq -0x28(%rbp), %rdi
callq 0x155c0
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
callq 0xc7d0
movq -0x28(%rbp), %rcx
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl 0x20(%rcx), %eax
jne 0x153e2
movl $0x6, -0x4(%rbp)
jmp 0x155b0
movq -0x28(%rbp), %rax
movl 0x20(%rax), %eax
subl -0x18(%rbp), %eax
cltq
cmpq $0xc, %rax
jae 0x15400
movl $0x3, -0x4(%rbp)
jmp 0x155b0
movq -0x28(%rbp), %rsi
movq %rsi, %rdi
addq $0x10, %rdi
addq $0x24, %rsi
movl $0xc, %edx
callq 0xc6e0
cltq
cmpq $0xc, %rax
je 0x1542d
movl $0x3, -0x4(%rbp)
jmp 0x155b0
movq -0x28(%rbp), %rdi
movq 0x24(%rdi), %rax
movq %rax, 0x88(%rdi)
movl 0x2c(%rdi), %eax
movl %eax, 0x90(%rdi)
addq $0x84, %rdi
callq 0x15680
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x1546c
cmpl $0x7, -0x1c(%rbp)
jne 0x1546c
movl $0x7, -0x4(%rbp)
jmp 0x155b0
movq -0x28(%rbp), %rdi
addq $0x84, %rdi
addq $0x4, %rdi
addq $0x4, %rdi
movl $0x8, %esi
callq 0x15710
movq -0x28(%rbp), %rax
movl 0x8c(%rax), %eax
subq $0xc, %rax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jle 0x15560
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
callq 0xc7d0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, -0x18(%rbp)
movl 0x20(%rax), %eax
subl -0x18(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x154d1
movl $0x3, -0x4(%rbp)
jmp 0x155b0
movslq -0x20(%rbp), %rax
cmpq $0x54, %rax
ja 0x1554c
movq -0x28(%rbp), %rsi
movq %rsi, %rdi
addq $0x10, %rdi
addq $0x24, %rsi
addq $0xc, %rsi
movl -0x20(%rbp), %edx
callq 0xc6e0
cmpl -0x20(%rbp), %eax
je 0x15507
movl $0x3, -0x4(%rbp)
jmp 0x155b0
movq -0x28(%rbp), %rdi
movq %rdi, %rsi
addq $0x24, %rsi
addq $0xc, %rsi
addq $0x84, %rdi
addq $0x4, %rdi
addq $0xc, %rdi
movl $0x54, %edx
callq 0x41a0
movq -0x28(%rbp), %rdi
addq $0x84, %rdi
addq $0x4, %rdi
addq $0xc, %rdi
movl -0x20(%rbp), %esi
callq 0x15710
jmp 0x1555e
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
movl -0x20(%rbp), %esi
xorl %edx, %edx
callq 0xc740
jmp 0x15560
cmpl $0x0, -0x20(%rbp)
jl 0x155aa
movslq -0x20(%rbp), %rax
cmpq $0x54, %rax
ja 0x155aa
movq -0x28(%rbp), %rax
movl $0x0, 0x2c(%rax)
movl 0x90(%rax), %ecx
movl %ecx, -0x2c(%rbp)
movq %rax, %rdi
addq $0x24, %rdi
movl 0x8c(%rax), %esi
callq 0x5890
movl %eax, %ecx
movl -0x2c(%rbp), %eax
cmpl %ecx, %eax
je 0x155a8
movl $0x4, -0x4(%rbp)
jmp 0x155b0
jmp 0x155aa
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)