// File: 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
subq $0x2a0, %rsp            # imm = 0x2A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x4150
movq -0x260(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x8, %rdi
callq 0x3320
leaq -0x218(%rbp), %rdi
movq %rdi, -0x258(%rbp)
callq 0x32b0
movq -0x258(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x4, %edx
callq 0x3150
jmp 0x42d1
leaq -0x218(%rbp), %rdi
callq 0x3390
movb %al, -0x261(%rbp)
jmp 0x42e5
movb -0x261(%rbp), %al
testb $0x1, %al
jne 0x4334
jmp 0x42f1
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
leaq 0x2e3a(%rip), %rax      # 0x713f
movq %rax, (%rdi)
movq 0x5cb9(%rip), %rsi      # 0x9fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x33e0
jmp 0x476a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x4752
movl $0x2c, %edi
callq 0x3210
movq %rax, -0x270(%rbp)
jmp 0x4347
movq -0x260(%rbp), %rax
movq -0x270(%rbp), %rcx
movq %rcx, 0x28(%rax)
movq 0x28(%rax), %rsi
leaq -0x218(%rbp), %rdi
movl $0x2c, %edx
callq 0x32e0
jmp 0x4370
movq -0x260(%rbp), %rax
movq 0x28(%rax), %rax
cmpl $0x4034b50, (%rax)      # imm = 0x4034B50
jne 0x43bd
leaq -0x218(%rbp), %rdi
callq 0x3060
jmp 0x4391
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
leaq 0x2dab(%rip), %rax      # 0x7150
movq %rax, (%rdi)
movq 0x5c19(%rip), %rsi      # 0x9fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x33e0
jmp 0x476a
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x3420
movb %al, -0x271(%rbp)
jmp 0x43dd
movb -0x271(%rbp), %al
testb $0x1, %al
jne 0x43e9
jmp 0x4415
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
leaq 0x2d7a(%rip), %rax      # 0x7177
movq %rax, (%rdi)
movq 0x5bc1(%rip), %rsi      # 0x9fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x33e0
jmp 0x476a
movq -0x260(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0x28(%rax), %eax
cmpq $0x34, %rax
jae 0x4456
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
leaq 0x2d60(%rip), %rax      # 0x719e
movq %rax, (%rdi)
movq 0x5b80(%rip), %rsi      # 0x9fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x33e0
jmp 0x476a
movq -0x260(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0x26(%rax), %eax
movl %eax, %edi
callq 0x3030
movq %rax, -0x280(%rbp)
jmp 0x4475
movq -0x260(%rbp), %rcx
movq -0x280(%rbp), %rax
movq %rax, 0x30(%rcx)
movq 0x28(%rcx), %rax
movq 0x30(%rcx), %rsi
movzwl 0x26(%rax), %eax
movl %eax, %edx
leaq -0x218(%rbp), %rdi
callq 0x32e0
jmp 0x44a3
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x3420
movb %al, -0x281(%rbp)
jmp 0x44c3
movb -0x281(%rbp), %al
testb $0x1, %al
jne 0x44cf
jmp 0x44fb
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
leaq 0x2cbb(%rip), %rax      # 0x719e
movq %rax, (%rdi)
movq 0x5adb(%rip), %rsi      # 0x9fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x33e0
jmp 0x476a
movq -0x260(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0x28(%rax), %eax
movl %eax, %edi
callq 0x3030
movq %rax, -0x290(%rbp)
jmp 0x451a
movq -0x260(%rbp), %rcx
movq -0x290(%rbp), %rax
movq %rax, 0x38(%rcx)
movq 0x28(%rcx), %rax
movq 0x38(%rcx), %rsi
movzwl 0x28(%rax), %eax
movl %eax, %edx
leaq -0x218(%rbp), %rdi
callq 0x32e0
jmp 0x4548
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x3420
movb %al, -0x291(%rbp)
jmp 0x4568
movb -0x291(%rbp), %al
testb $0x1, %al
jne 0x4574
jmp 0x45a0
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
leaq 0x2c16(%rip), %rax      # 0x719e
movq %rax, (%rdi)
movq 0x5a36(%rip), %rsi      # 0x9fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x33e0
jmp 0x476a
leaq -0x218(%rbp), %rdi
callq 0x3060
jmp 0x45ae
movq -0x260(%rbp), %rax
movb $0x1, 0x48(%rax)
movq 0x38(%rax), %rax
movq %rax, -0x230(%rbp)
movq -0x230(%rbp), %rax
movzwl 0x6(%rax), %eax
movq -0x230(%rbp), %rcx
movzwl 0x8(%rcx), %ecx
cmpl %ecx, %eax
jg 0x45fc
movq -0x260(%rbp), %rcx
movq -0x230(%rbp), %rax
movzwl 0x8(%rax), %eax
movq 0x28(%rcx), %rcx
movzwl 0x28(%rcx), %ecx
cmpl %ecx, %eax
jle 0x4628
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
leaq 0x2b8e(%rip), %rax      # 0x719e
movq %rax, (%rdi)
movq 0x59ae(%rip), %rsi      # 0x9fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x33e0
jmp 0x476a
movq -0x260(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x230(%rbp), %rcx
movzwl 0x6(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x238(%rbp)
movq -0x230(%rbp), %rax
movzwl 0x8(%rax), %eax
movq -0x230(%rbp), %rcx
movzwl 0x6(%rcx), %ecx
subl %ecx, %eax
cltq
movl $0x6, %ecx
xorl %edx, %edx
divq %rcx
movl %eax, -0x23c(%rbp)
movl $0x0, -0x24c(%rbp)
movl -0x24c(%rbp), %eax
cmpl -0x23c(%rbp), %eax
jge 0x473d
movl $0x20, %edi
callq 0x3210
movq %rax, -0x2a0(%rbp)
jmp 0x46a4
movq -0x2a0(%rbp), %rdi
movq -0x260(%rbp), %rcx
movq -0x238(%rbp), %rax
movslq -0x24c(%rbp), %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,2), %rsi
movq 0x28(%rcx), %rax
movq 0x38(%rcx), %rdx
movzwl 0x28(%rax), %ecx
callq 0x6050
jmp 0x46db
movq -0x260(%rbp), %rdi
movq -0x2a0(%rbp), %rax
movq %rax, -0x248(%rbp)
addq $0x50, %rdi
movq -0x248(%rbp), %rsi
callq 0x53c0
jmp 0x4702
jmp 0x4704
movl -0x24c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24c(%rbp)
jmp 0x467f
movq -0x2a0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
movl $0x20, %esi
callq 0x3220
jmp 0x4752
leaq -0x218(%rbp), %rdi
callq 0x3070
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
leaq -0x218(%rbp), %rdi
callq 0x3070
movq -0x220(%rbp), %rdi
callq 0x33f0
nopw (%rax,%rax)