// File: /workspace/github_cpp_makefile_star2_O1/sc0ty[P]CODExtract/src/codfile/codfile.cpp
// Function: CODFile::LoadFile(char const*)
void CODFile::LoadFile(const char *fname)
{
    Close();
    FName = fname;

    // open file
    ifstream is;
    is.open(fname, ios::binary);
    if (!is.is_open()) throw "Cannot read file";

    // read header
    HeadSeg = new TCODFILEHEADER;
    is.read((char *)HeadSeg, sizeof(TCODFILEHEADER));

    // check for zip file
    if (HeadSeg->flashid == ZIPSignature)
    {
        is.close();
        throw "Compressed ZIP file - extract it first";
    }

    // sanity check
    if (is.eof()) throw "Incorrect file format (file too short)";
    if (HeadSeg->data_size < sizeof(TDATASEGHEADER)) throw "Incorrect file format";

    // read code segment
    CodeSeg = new char[HeadSeg->code_size];
    is.read((char *)CodeSeg, HeadSeg->code_size);
    if (is.eof()) throw "Incorrect file format";

    // read data segment
    DataSeg = new char[HeadSeg->data_size];
    is.read((char *)DataSeg, HeadSeg->data_size);
    if (is.eof()) throw "Incorrect file format";

    is.close();
    isOpen = true;

    /* read resources */

    // data segment header
    TDATASEGHEADER *dh = (TDATASEGHEADER *)DataSeg;

    // sanity check
    if ((dh->exported_string_offset > dh->data_bytes_offset) ||
        (dh->data_bytes_offset > HeadSeg->data_size))
        throw "Incorrect file format";

    // resources array
    TCODRESOURCE *res = (TCODRESOURCE *)(DataSeg + dh->exported_string_offset);

    // resources array size
    int ressz = (dh->data_bytes_offset - dh->exported_string_offset) / sizeof(TCODRESOURCE);

    CODResource *r;

    // read resources from data segment
    for (int i=0; i<ressz; i++)
    {
        r = new CODResource(&res[i], DataSeg, HeadSeg->data_size);
        Resources.push_back( *r );
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rsi, %r14
movq %rdi, %rbx
callq 0x315c
leaq 0x8(%rbx), %r15
movq 0x10(%rbx), %r12
movq %r14, %rdi
callq 0x20f0
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x22b0
leaq -0x230(%rbp), %r15
movq %r15, %rdi
callq 0x21f0
movq %r15, %rdi
movq %r14, %rsi
movl $0x4, %edx
callq 0x2130
leaq -0x1b8(%rbp), %rdi
callq 0x2070
testb %al, %al
je 0x3430
movl $0x2c, %edi
callq 0x21b0
movq %rax, 0x28(%rbx)
leaq -0x230(%rbp), %rdi
movl $0x2c, %edx
movq %rax, %rsi
callq 0x2220
movq 0x28(%rbx), %r15
cmpl $0x4034b50, (%r15)      # imm = 0x4034B50
je 0x3439
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x2, -0x210(%rbp,%rax)
jne 0x344e
leaq 0x1eee(%rip), %r14      # 0x519e
cmpw $0x34, 0x28(%r15)
jb 0x3455
movzwl 0x26(%r15), %edi
callq 0x2030
movq %rax, 0x30(%rbx)
movzwl 0x26(%r15), %edx
leaq -0x230(%rbp), %rdi
movq %rax, %rsi
callq 0x2220
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x2, -0x210(%rbp,%rax)
jne 0x3455
movq 0x28(%rbx), %r15
movzwl 0x28(%r15), %edi
callq 0x2030
movq %rax, 0x38(%rbx)
movzwl 0x28(%r15), %edx
leaq -0x230(%rbp), %rdi
movq %rax, %rsi
callq 0x2220
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x2, -0x210(%rbp,%rax)
jne 0x3455
leaq -0x230(%rbp), %rdi
callq 0x2060
movb $0x1, 0x48(%rbx)
movq 0x38(%rbx), %r14
movzwl 0x6(%r14), %ecx
movzwl 0x8(%r14), %eax
cmpw %ax, %cx
ja 0x340b
movq 0x28(%rbx), %rdx
cmpw 0x28(%rdx), %ax
ja 0x340b
subq %rcx, %rax
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rdx
movq %rdx, %r15
shrq $0x2, %r15
testl %r15d, %r15d
jle 0x33ed
addq %rcx, %r14
leaq 0x50(%rbx), %r12
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl $0x20, %edi
callq 0x21b0
movq %rax, %r13
movq 0x28(%rbx), %rax
movq 0x38(%rbx), %rdx
movzwl 0x28(%rax), %ecx
movq %r13, %rdi
movq %r14, %rsi
callq 0x3ef8
movl $0x30, %edi
callq 0x21b0
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups %xmm1, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movq %rax, %rdi
movq %r12, %rsi
callq 0x2050
incq 0x60(%rbx)
addq $0x6, %r14
decq %r15
jne 0x3395
leaq -0x230(%rbp), %rdi
callq 0x2080
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x2110
leaq 0x1d82(%rip), %rcx      # 0x519e
movq %rcx, (%rax)
movq 0x3b92(%rip), %rsi      # 0x6fb8
movq %rax, %rdi
xorl %edx, %edx
callq 0x2300
leaq 0x1d08(%rip), %r14      # 0x513f
jmp 0x3455
leaq -0x230(%rbp), %rdi
callq 0x2060
leaq 0x1d04(%rip), %r14      # 0x5150
jmp 0x3455
leaq 0x1d22(%rip), %r14      # 0x5177
movl $0x8, %edi
callq 0x2110
movq %r14, (%rax)
movq 0x3b4f(%rip), %rsi      # 0x6fb8
movq %rax, %rdi
xorl %edx, %edx
callq 0x2300
jmp 0x3489
jmp 0x3489
movq %rax, %rbx
movl $0x20, %esi
movq %r13, %rdi
callq 0x21c0
jmp 0x348c
movq %rax, %rbx
leaq -0x230(%rbp), %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x2310