// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/DiskImageFactory.cpp
// Function: CDiskImageFactory::IsKFStream(CapsFile*)
int CDiskImageFactory::IsKFStream(PCAPSFILE pcf)
{
	// error if file can't be opened
	CCapsFile file;
	if (file.Open(pcf))
		return citError;

	C2OOBHdr oob;
	uint8_t buf[512];

	// remaining file size
	int frem = file.GetSize();

	// process the file as long as the data read is an OOB info area
	while (true) {
		// stop if OOB header is longer than the remaining file size
		int rlen = sizeof(oob);
		if (rlen > frem)
			break;

		// read OOB header, stop on error
		if (file.Read((PUBYTE)&oob, rlen) != rlen)
			return citError;

		// update the remaining file size
		frem -= rlen;

		// check that OOB is Info
		if (oob.sign != c2eOOB || oob.type != c2otInfo)
			break;

		// read info size value as little-endian
		uint8_t *po = (uint8_t *)&oob;
		int so = offsetof(C2OOBHdr, size);
		int infosize = po[so+1] << 8 | po[so];

		// stop if info size is 0 or larger than remaining file size or larger than the buffer
		if (!infosize || infosize > frem || infosize > sizeof(buf))
			break;
		
		// read info, stop on error
		if (file.Read(buf, infosize) != infosize)
			return citError;

		// update the remaining file size
		frem -= infosize;

		// success, if KryoFlux text found in info
		if (strstr((char *)buf, "KryoFlux"))
			return citKFStream;
	}

	// file reading stopped somewhere, type is unknown
	return citUnknown;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x280, %rsp            # imm = 0x280
movq %rdi, -0x10(%rbp)
leaq -0x20(%rbp), %rdi
movq %rdi, -0x268(%rbp)
callq 0xc450
movq -0x268(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0xc550
movl %eax, -0x25c(%rbp)
jmp 0x19367
movl -0x25c(%rbp), %eax
cmpl $0x0, %eax
je 0x1939f
movl $0x0, -0x4(%rbp)
movl $0x1, -0x30(%rbp)
jmp 0x1953f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x20(%rbp), %rdi
callq 0xc480
jmp 0x19554
leaq -0x20(%rbp), %rdi
callq 0xc7b0
movl %eax, -0x26c(%rbp)
jmp 0x193b0
movl -0x26c(%rbp), %eax
movl %eax, -0x244(%rbp)
jmp 0x193be
movl $0x10, -0x248(%rbp)
movl -0x248(%rbp), %eax
cmpl -0x244(%rbp), %eax
jle 0x193db
jmp 0x19531
movl -0x248(%rbp), %edx
leaq -0x20(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xc6e0
movl %eax, -0x270(%rbp)
jmp 0x193f6
movl -0x270(%rbp), %eax
cmpl -0x248(%rbp), %eax
je 0x19417
movl $0x0, -0x4(%rbp)
movl $0x1, -0x30(%rbp)
jmp 0x1953f
movl -0x248(%rbp), %ecx
movl -0x244(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x244(%rbp)
movzbl -0x40(%rbp), %eax
cmpl $0xd, %eax
jne 0x1943d
movzbl -0x3f(%rbp), %eax
cmpl $0x4, %eax
je 0x19442
jmp 0x19531
leaq -0x40(%rbp), %rax
movq %rax, -0x250(%rbp)
movl $0x2, -0x254(%rbp)
movq -0x250(%rbp), %rax
movl -0x254(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x250(%rbp), %rcx
movslq -0x254(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x258(%rbp)
cmpl $0x0, -0x258(%rbp)
je 0x194b1
movl -0x258(%rbp), %eax
cmpl -0x244(%rbp), %eax
jg 0x194b1
movslq -0x258(%rbp), %rax
cmpq $0x200, %rax            # imm = 0x200
jbe 0x194b3
jmp 0x19531
movl -0x258(%rbp), %edx
leaq -0x20(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0xc6e0
movl %eax, -0x274(%rbp)
jmp 0x194d1
movl -0x274(%rbp), %eax
cmpl -0x258(%rbp), %eax
je 0x194ef
movl $0x0, -0x4(%rbp)
movl $0x1, -0x30(%rbp)
jmp 0x1953f
movl -0x258(%rbp), %ecx
movl -0x244(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x244(%rbp)
leaq -0x240(%rbp), %rdi
leaq 0x4e1b(%rip), %rsi      # 0x1e32c
callq 0x4090
cmpq $0x0, %rax
je 0x1952c
movl $0x4, -0x4(%rbp)
movl $0x1, -0x30(%rbp)
jmp 0x1953f
jmp 0x193bc
movl $0x1, -0x4(%rbp)
movl $0x1, -0x30(%rbp)
leaq -0x20(%rbp), %rdi
callq 0xc480
movl -0x4(%rbp), %eax
addq $0x280, %rsp            # imm = 0x280
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x4420
nopl (%rax)