// File: /workspace/github_cpp_makefile_star2_O2/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 0x2c32
leaq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x2320
leaq -0x230(%rbp), %r15
movq %r15, %rdi
callq 0x21f0
pushq $0x4
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2120
leaq -0x1b8(%rbp), %rdi
callq 0x2060
testb %al, %al
je 0x2eb6
pushq $0x2c
popq %rdi
callq 0x21a0
movq %rax, 0x28(%rbx)
leaq -0x230(%rbp), %rdi
pushq $0x2c
popq %rdx
movq %rax, %rsi
callq 0x2210
movq 0x28(%rbx), %rax
cmpl $0x4034b50, (%rax)      # imm = 0x4034B50
je 0x2ebf
movq -0x230(%rbp), %rcx
movq -0x18(%rcx), %rcx
testb $0x2, -0x210(%rbp,%rcx)
jne 0x2ed4
leaq 0x143a(%rip), %r15      # 0x419e
cmpw $0x34, 0x28(%rax)
jb 0x2edb
movzwl 0x26(%rax), %r14d
movq %r14, %rdi
callq 0x2030
movq %rax, 0x30(%rbx)
leaq -0x230(%rbp), %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x2210
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x2, -0x210(%rbp,%rax)
jne 0x2edb
movq 0x28(%rbx), %rax
movzwl 0x28(%rax), %r14d
movq %r14, %rdi
callq 0x2030
movq %rax, 0x38(%rbx)
leaq -0x230(%rbp), %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x2210
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x2, -0x210(%rbp,%rax)
jne 0x2edb
leaq -0x230(%rbp), %rdi
callq 0x2050
movb $0x1, 0x48(%rbx)
movq 0x38(%rbx), %r14
movzwl 0x6(%r14), %ecx
movzwl 0x8(%r14), %eax
cmpw %ax, %cx
ja 0x2e93
movq 0x28(%rbx), %rdx
cmpw 0x28(%rdx), %ax
ja 0x2e93
addq %rcx, %r14
subq %rcx, %rax
xorl %ecx, %ecx
pushq $0x6
popq %rsi
xorl %edx, %edx
divq %rsi
leaq 0x50(%rbx), %r15
testl %eax, %eax
cmovgl %eax, %ecx
imulq $0x6, %rcx, %r12
testq %r12, %r12
je 0x2e75
pushq $0x20
popq %rdi
callq 0x21a0
movq %rax, %r13
movq 0x28(%rbx), %rax
movq 0x38(%rbx), %rdx
movzwl 0x28(%rax), %ecx
movq %r13, %rdi
movq %r14, %rsi
callq 0x3838
movq %r15, %rdi
movq %r13, %rsi
callq 0x35e4
addq $-0x6, %r12
addq $0x6, %r14
jmp 0x2e39
leaq -0x230(%rbp), %rdi
callq 0x2070
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x2100
leaq 0x12fc(%rip), %rcx      # 0x419e
movq %rcx, (%rax)
movq 0x311c(%rip), %rsi      # 0x5fc8
movq %rax, %rdi
xorl %edx, %edx
callq 0x22e0
leaq 0x1282(%rip), %r15      # 0x413f
jmp 0x2edb
leaq -0x230(%rbp), %rdi
callq 0x2050
leaq 0x127e(%rip), %r15      # 0x4150
jmp 0x2edb
leaq 0x129c(%rip), %r15      # 0x4177
pushq $0x8
popq %rdi
callq 0x2100
movq %r15, (%rax)
movq 0x30db(%rip), %rsi      # 0x5fc8
movq %rax, %rdi
xorl %edx, %edx
callq 0x22e0
jmp 0x2f0b
jmp 0x2f0b
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r13, %rdi
callq 0x21b0
jmp 0x2f0e
movq %rax, %rbx
leaq -0x230(%rbp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x22f0