// File: /workspace/github_cpp_makefile_star2_O1/OpenFodder[P]SOS_Unpacker/capsimg/Core/DiskFile.cpp
// Function: CDiskFile::FindFile(char*, char const*, char const*)
int CDiskFile::FindFile(char *result, const char *filename, const char *filter)
{
	// result is not valid by default
	int resvalid = 0;

	// copy source as result by default
	int rescopy = 1;

	// stop if no buffer specified for the result
	if (!result)
		return resvalid;

	// the default result is an empty string
	result[0] = 0;

	// stop if the filename is not specified
	if (!filename)
		return resvalid;

	// length of path and file name components separated
	int pathlen = 0, namelen = 0;

	// wildcard usage counter, wildcards enabled
	int wcused = 0;
	int wcenabled = 1;

	// process the entire source filename or pattern
	for (int rpos = 0; wcenabled && filename[rpos]; rpos++) {
		namelen++;

		switch (filename[rpos]) {
			// if path delimiter found
		case '/':
		case '\\':
			// stop if a wilcard has already been used; only allow wildcards in filename part, not in the path
			if (wcused) {
				// disable write card processing; the result will be the path upto the illegal wildcard segment
				wcenabled = 0;
				break;
			}

			// set new path length as the entire name until now including the delimiter
			pathlen = rpos + 1;

			// clear the filename part
			namelen = 0;
			break;

			// count the wildcards
		case '?':
		case '*':
			wcused++;
			break;
		}
	}

	// if wildcards used
	if (wcused) {
		// and wilcardcards are enabled, ie no error in the path
		if (wcenabled) {
			char *pathbuf = NULL;
			const char *dirpath;

			// make a copy of the path if it's defined or use the current directory as the path
			if (pathlen) {
				pathbuf = new char[pathlen + 1];
				memcpy(pathbuf, filename, pathlen);
				pathbuf[pathlen] = 0;
				dirpath = pathbuf;
			} else
				dirpath = ".";

			// open the selected path
			DIR *pdir = opendir(dirpath);
			if (pdir) {
				dirent *pent;
				const char *pattern = filename + pathlen;

				// iterate all directory entries
				while (pent = readdir(pdir)) {
					// skip any entry that is not a regular file
#if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
					if (pent->d_type != DT_REG)
						continue;
#endif

					// check entry to match the filename pattern
					char *fn = pent->d_name;
					if (FileNameMatch(pattern, fn)) {
						// if matches, check entry to match the filter pattern if filter is specified
						if (!filter || FileNameMatch(filter, fn)) {
							// success; copy the original path and add the entry found
							memcpy(result, filename, pathlen);
							strcpy(result + pathlen, fn);

							// keep this result, don't copy the source
							rescopy = 0;

							// result is valid
							resvalid = 1;
							break;
						}
					}
				}

				closedir(pdir);
			}

			// free path buffer
			delete[] pathbuf;
		}
	} else {
		// wilcards unused, result is valid
		resvalid = 1;
	}

	// copy the source if that's the result
	if (rescopy) {
		int reslen = pathlen + namelen;
		memcpy(result, filename, reslen);
		result[reslen] = 0;
	}

	return resvalid;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
xorl %r12d, %r12d
testq %rdi, %rdi
je 0xe4da
movq %rsi, %r14
movq %rdi, %rbx
movb $0x0, (%rdi)
testq %rsi, %rsi
je 0xe4da
movb (%r14), %sil
testb %sil, %sil
je 0xe373
xorl %ebp, %ebp
movl $0x1, %eax
xorl %ecx, %ecx
xorl %r13d, %r13d
incl %ebp
movzbl %sil, %esi
xorl %edi, %edi
cmpl $0x3e, %esi
jg 0xe33b
cmpl $0x2a, %esi
je 0xe357
cmpl $0x2f, %esi
je 0xe345
jmp 0xe35b
cmpl $0x3f, %esi
je 0xe357
cmpl $0x5c, %esi
jne 0xe35b
movb $0x1, %dil
testl %ecx, %ecx
jne 0xe35b
xorl %ebp, %ebp
xorl %ecx, %ecx
xorl %edi, %edi
movl %eax, %r13d
jmp 0xe35b
incl %ecx
xorl %edi, %edi
testb %dil, %dil
jne 0xe36c
movb (%r14,%rax), %sil
incq %rax
testb %sil, %sil
jne 0xe322
testl %ecx, %ecx
sete %al
jmp 0xe37c
movb $0x1, %al
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %edi, %edi
orb %al, %dil
je 0xe38d
movzbl %al, %r12d
xorl %r15d, %r15d
jmp 0xe4bc
movq %rbx, (%rsp)
movq %rdx, 0x10(%rsp)
xorl %r12d, %r12d
movslq %r13d, %rbx
testl %r13d, %r13d
movq %rbx, 0x18(%rsp)
je 0xe3d1
leal 0x1(%rbx), %eax
movslq %eax, %rdi
callq 0x4040
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4190
movb $0x0, (%r15,%rbx)
movq %r15, 0x20(%rsp)
movq %r15, %rdi
jmp 0xe3e1
leaq 0x3f3a(%rip), %rdi      # 0x12312
movq $0x0, 0x20(%rsp)
movq (%rsp), %rbx
callq 0x42f0
testq %rax, %rax
je 0xe4aa
movq %rax, %r15
movl %r13d, 0xc(%rsp)
movq 0x18(%rsp), %rax
leaq (%r14,%rax), %rcx
movq %rcx, 0x30(%rsp)
addq %rbx, %rax
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
movl $0x1, (%rsp)
movq %r15, %rdi
callq 0x4300
testq %rax, %rax
je 0xe493
movq %rax, %r13
cmpb $0x8, 0x12(%rax)
jne 0xe41b
addq $0x13, %r13
movq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0xe4ec
movb $0x1, %cl
testl %eax, %eax
je 0xe48f
cmpq $0x0, 0x10(%rsp)
je 0xe463
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0xe4ec
movb $0x1, %cl
testl %eax, %eax
je 0xe48f
movq %rbx, %rdi
movq %r14, %rsi
movq 0x18(%rsp), %rdx
callq 0x4190
movq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x41e0
movl $0x1, %r12d
xorl %ecx, %ecx
movl $0x0, (%rsp)
testb %cl, %cl
jne 0xe41b
movq %r15, %rdi
callq 0x4320
cmpl $0x0, (%rsp)
sete %r15b
movl 0xc(%rsp), %r13d
jmp 0xe4ad
xorl %r15d, %r15d
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xe4bc
callq 0x4280
testb %r15b, %r15b
jne 0xe4da
addl %ebp, %r13d
movslq %r13d, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4190
movb $0x0, (%rbx,%r15)
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq