// File: sc0ty[P]CODExtract/src/codfile/codfile.cpp
// Function: CODFile::LoadParent()
bool CODFile::LoadParent()
{
    if (!isOpen) throw "Load file first";

    // parent already loaded
    if (!isChildCOD()) return false;

    /* extract parent filename from cod file */

    // data segment header
    TDATASEGHEADER *dh = (TDATASEGHEADER *)DataSeg;

    // sanity check
    if (dh->exported_string_offset > HeadSeg->data_size)
        throw "Incorrect file format";

    // export names ptr array
    u16 *expn = (u16 *)(DataSeg + sizeof(TDATASEGHEADER) +
                        sizeof(u16)*dh->num_classes +
                        sizeof(u16)*2*dh->num_modules);

    // check if ptr is in data section
    if ( *expn >= HeadSeg->data_size)
        throw "Incorrect file format";

    /* create parent filename */

    string fn = FName;
    fn = &DataSeg[*expn];
	size_t s;

	// find every $ characters
	while ((s = fn.find('$')) != string::npos)
	{
		int c;
		istringstream iss(fn.substr(s+1, 2));
		iss >> hex >> c;
		fn.replace(s, 3, 1, (char)c);
	}

	// extract directory from path
    size_t sz = FName.find_last_of("\\/");
	if (sz != string::npos) fn = FName.substr(0, sz+1) + fn;
    fn += ".cod";

    // load parent cod file
    LoadFile(fn.c_str());

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x250(%rbp)
testb $0x1, 0x48(%rax)
jne 0x47b7
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
leaq 0x2a10(%rip), %rax      # 0x71b4
movq %rax, (%rdi)
movq 0x581a(%rip), %rsi      # 0x9fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x33e0
movq -0x250(%rbp), %rdi
callq 0x4af0
testb $0x1, %al
jne 0x47d0
movb $0x0, -0x1(%rbp)
jmp 0x4acd
movq -0x250(%rbp), %rcx
movq 0x38(%rcx), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x6(%rax), %eax
movq 0x28(%rcx), %rcx
movzwl 0x28(%rcx), %ecx
cmpl %ecx, %eax
jle 0x481a
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
leaq 0x2997(%rip), %rax      # 0x719e
movq %rax, (%rdi)
movq 0x57b7(%rip), %rsi      # 0x9fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x33e0
movq -0x250(%rbp), %rcx
movq 0x38(%rcx), %rax
addq $0x34, %rax
movq -0x18(%rbp), %rdx
movzbl 0x5(%rdx), %edx
shlq %rdx
addq %rdx, %rax
movq -0x18(%rbp), %rdx
movzbl 0x4(%rdx), %edx
shlq $0x2, %rdx
addq %rdx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movzwl (%rax), %eax
movq 0x28(%rcx), %rcx
movzwl 0x28(%rcx), %ecx
cmpl %ecx, %eax
jl 0x4884
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
leaq 0x292d(%rip), %rax      # 0x719e
movq %rax, (%rdi)
movq 0x574d(%rip), %rsi      # 0x9fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x33e0
movq -0x250(%rbp), %rsi
addq $0x8, %rsi
leaq -0x40(%rbp), %rdi
movq %rdi, -0x258(%rbp)
callq 0x30d0
movq -0x250(%rbp), %rax
movq -0x258(%rbp), %rdi
movq 0x38(%rax), %rsi
movq -0x20(%rbp), %rax
movzwl (%rax), %eax
addq %rax, %rsi
callq 0x3320
jmp 0x48c2
jmp 0x48c4
leaq -0x40(%rbp), %rdi
movl $0x24, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x3300
movq %rax, -0x58(%rbp)
cmpq $-0x1, %rax
je 0x49d0
movq -0x58(%rbp), %rdx
incq %rdx
leaq -0x200(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movl $0x2, %ecx
callq 0x3230
jmp 0x4902
leaq -0x1e0(%rbp), %rdi
leaq -0x200(%rbp), %rsi
movl $0x8, %edx
callq 0x33d0
jmp 0x491c
leaq -0x200(%rbp), %rdi
callq 0x3140
leaq 0xad1(%rip), %rsi       # 0x5400
leaq -0x1e0(%rbp), %rdi
callq 0x33c0
movq %rax, -0x260(%rbp)
jmp 0x4944
movq -0x260(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x3130
jmp 0x4956
movq -0x58(%rbp), %rsi
movsbl -0x5c(%rbp), %r8d
leaq -0x40(%rbp), %rdi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x33a0
jmp 0x4974
leaq -0x1e0(%rbp), %rdi
callq 0x3100
jmp 0x48c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4ac2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x3140
jmp 0x4ac2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x3100
jmp 0x4ac2
movq -0x250(%rbp), %rdi
addq $0x8, %rdi
leaq 0x27e2(%rip), %rsi      # 0x71c4
movq $-0x1, %rdx
callq 0x31e0
movq %rax, -0x208(%rbp)
cmpq $-0x1, -0x208(%rbp)
je 0x4a87
movq -0x250(%rbp), %rsi
addq $0x8, %rsi
movq -0x208(%rbp), %rcx
incq %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x248(%rbp), %rdi
callq 0x3230
jmp 0x4a2a
leaq -0x228(%rbp), %rdi
leaq -0x248(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x5430
jmp 0x4a43
leaq -0x40(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x32c0
leaq -0x228(%rbp), %rdi
callq 0x3140
leaq -0x248(%rbp), %rdi
callq 0x3140
jmp 0x4a87
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x3140
jmp 0x4ac2
leaq 0x2739(%rip), %rsi      # 0x71c7
leaq -0x40(%rbp), %rdi
callq 0x31f0
jmp 0x4a99
leaq -0x40(%rbp), %rdi
callq 0x30c0
movq -0x250(%rbp), %rdi
movq %rax, %rsi
callq 0x4270
jmp 0x4ab3
movb $0x1, -0x1(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3140
jmp 0x4acd
leaq -0x40(%rbp), %rdi
callq 0x3140
jmp 0x4adb
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x33f0
nopw %cs:(%rax,%rax)