/*
=============
Sys_LoadImage

=============
*/
void Sys_LoadImage( ){

    void *dl;
    char *error;
    char module[MAX_OSPATH];

    Com_sprintf(module, sizeof(module), "%s/%s", Sys_BinaryPath(), COD4_DLL);

    if(!Sys_LoadImagePrepareFile( module ))
    {
        printf("An error has occurred. Exiting...\n");
        _exit(1);
    }

    dl = dlopen(module, RTLD_LAZY);

    if(dl == NULL)
    {
        error = dlerror();
        printf("Failed to load required module: %s Error: %s\n", module, error);
        _exit(1);

    }
    /* No retrieving of symbols where none are :( */

    if(!Sys_PatchImage())
    {
        printf("Failed to patch module: %s\n", module);
        _exit(1);
    }
}
/*
=============
Sys_LoadImage

=============
*/
qboolean Sys_LoadImage( ){

    byte *fileimage;
    int len;

    /* Is this file here ? */
    len = FS_FOpenFileRead(BIN_FILENAME, NULL);
    if(len != DLLMOD_FILESIZE)
    {/* Nope !*/

        Sec_Update( qtrue );
        len = FS_FOpenFileRead(BIN_FILENAME, NULL);
        if(len != DLLMOD_FILESIZE)
        {/* Nope !*/
            Com_PrintError("Failed to load the CoD4 Game. Can not startup the game\n");
            return qfalse;
        }
    }
    Sec_Update( qfalse );

    len = FS_ReadFile(BIN_FILENAME, (void**)&fileimage);
    if(!fileimage)
    {
	Com_PrintError("Couldn't open "BIN_FILENAME". CoD4 can not startup.\n");
	return qfalse;
    }
    if(len != DLLMOD_FILESIZE)
    {
	Com_PrintError(BIN_FILENAME" is corrupted! CoD4 can not startup.\n");
	FS_FreeFile(fileimage);
	return qfalse;
    }

    Com_Memcpy(BIN_SECT_TEXT_START, fileimage + BIN_SECT_TEXT_FOFFSET, BIN_SECT_TEXT_LENGTH);
    Com_Memcpy(BIN_SECT_RODATA_START, fileimage + BIN_SECT_RODATA_FOFFSET, BIN_SECT_RODATA_LENGTH);
    Com_Memcpy(BIN_SECT_DATA_START, fileimage + BIN_SECT_DATA_FOFFSET, BIN_SECT_DATA_LENGTH);
    Com_Memset(BIN_SECT_PLT_START, 0xCC, BIN_SECT_PLT_LENGTH);

    Sys_CoD4Linker();

    FS_FreeFile(fileimage);

    if(!Sys_PatchImage())
    {
        return qfalse;
    }
    if(Sys_MemoryProtectExec(BIN_SECT_PLT_START, BIN_SECT_PLT_LENGTH) == qfalse)
    {
        FS_FreeFile(fileimage);
        return qfalse;
    }
    if(Sys_MemoryProtectExec(BIN_SECT_TEXT_START, BIN_SECT_TEXT_LENGTH) == qfalse)
    {
        FS_FreeFile(fileimage);
        return qfalse;
    }
    if(Sys_MemoryProtectReadonly(BIN_SECT_RODATA_START, BIN_SECT_RODATA_LENGTH) == qfalse)
    {
        FS_FreeFile(fileimage);
        return qfalse;
    }
    if(Sys_MemoryProtectWrite(BIN_SECT_DATA_START, BIN_SECT_DATA_LENGTH) == qfalse)
    {
        FS_FreeFile(fileimage);
        return qfalse;
    }
    if(Sys_MemoryProtectWrite(BIN_SECT_BSS_START, BIN_SECT_BSS_LENGTH) == qfalse)
    {
        FS_FreeFile(fileimage);
        return qfalse;
    }

    Sys_ImageRunInitFunctions();

    return qtrue;
}
/*
=============
Sys_LoadImage

=============
*/
qboolean Sys_LoadImage( ){

    byte *fileimage;
    int len;
    char hash[128];
    long unsigned sizeofhash;

    /* Is this file here ? */
    len = FS_FOpenFileRead(BIN_FILENAME, NULL);

    if(len != DLLMOD_FILESIZE)
    {/* Nope !*/

        Sec_Update( qtrue );
        len = FS_FOpenFileRead(BIN_FILENAME, NULL);
        if(len != DLLMOD_FILESIZE)
        {/* Nope !*/
            Com_PrintError("Failed to load the CoD4 Game. Can not startup the game\n");
            return qfalse;
        }
    }
    Sec_Update( qfalse );

    len = FS_ReadFile(BIN_FILENAME, (void**)&fileimage);


    if(!fileimage)
    {
	Com_PrintError("Couldn't open "BIN_FILENAME". CoD4 can not startup.\n");
	return qfalse;
    }

    if(len != DLLMOD_FILESIZE)
    {
	Com_PrintError(BIN_FILENAME" has an invalid length! CoD4 can not startup.\n");
	FS_FreeFile(fileimage);
	return qfalse;
    }

    sizeofhash = sizeof(hash);
    Sec_HashMemory(SEC_HASH_TIGER, fileimage, len, hash, &sizeofhash, qfalse);

    if(Q_stricmp(hash ,DLLMOD_HASH))
    {
	Com_Printf("Tiger = %s\n", hash);
	Com_PrintError(BIN_FILENAME" checksum missmatch! CoD4 can not startup.\n");
	FS_FreeFile(fileimage);
	return qfalse;
    }


    Com_Memcpy(BIN_SECT_TEXT_START, fileimage + BIN_SECT_TEXT_FOFFSET, BIN_SECT_TEXT_LENGTH);
    Com_Memcpy(BIN_SECT_RODATA_START, fileimage + BIN_SECT_RODATA_FOFFSET, BIN_SECT_RODATA_LENGTH);
    Com_Memcpy(BIN_SECT_DATA_START, fileimage + BIN_SECT_DATA_FOFFSET, BIN_SECT_DATA_LENGTH);
    Com_Memset(BIN_SECT_PLT_START, 0xCC, BIN_SECT_PLT_LENGTH);

    Sys_CoD4Linker();

    FS_FreeFile(fileimage);

    if(!Sys_PatchImage())
    {
        return qfalse;
    }
	/* The order here is crucial as this sections will overlap */
    if(Sys_MemoryProtectReadonly(BIN_SECT_RODATA_START, BIN_SECT_RODATA_LENGTH) == qfalse)
    {
        FS_FreeFile(fileimage);
        return qfalse;
    }
    if(Sys_MemoryProtectWrite(BIN_SECT_DATA_START, BIN_SECT_DATA_LENGTH) == qfalse)
    {
        FS_FreeFile(fileimage);
        return qfalse;
    }
    if(Sys_MemoryProtectWrite(BIN_SECT_BSS_START, BIN_SECT_BSS_LENGTH) == qfalse)
    {
        FS_FreeFile(fileimage);
        return qfalse;
    }
	/* 4 bytes is gap between .plt end and .text start */
    if(Sys_MemoryProtectExec(BIN_SECT_PLT_START, BIN_SECT_PLT_LENGTH + BIN_SECT_TEXT_LENGTH + 4) == qfalse)
    {
        FS_FreeFile(fileimage);
        return qfalse;
    }
	
    Sys_ImageRunInitFunctions();

    return qtrue;
}