Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "PeBmLoader.h"
- #include <stdio.h>
- #include <cstring>
- #include <vector>
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_memfile.h>
- #include "WindowsDef.h"
- template <typename PE_STRUCT>
- static void Get_Pe_Struct(PE_STRUCT *& pe_struct,FILE * file,DWORD address)
- {
- fseek(file,address,SEEK_SET);
- fread(pe_struct,sizeof(char),sizeof(PE_STRUCT),file);
- }
- static void Get_Res_Section_Header(IMAGE_SECTION_HEADER *& section_header,
- int NumberOfSections,
- DWORD offset_pe_header,
- FILE * file)
- {
- for(int i = 0 ; i < NumberOfSections ; i++ )
- {
- DWORD offset = offset_pe_header +
- sizeof(IMAGE_NT_HEADERS) +
- (sizeof(IMAGE_SECTION_HEADER) * i ) ;
- Get_Pe_Struct(section_header,file,offset);
- if(strcmp((char*)(section_header->Name),".rsrc") == 0)
- return ;
- }
- }
- bool PeBmLoader::Get_Bit_Map_Dat(BitMapDat *& bitmapdat,
- int id,
- int gfxfileindex,
- FILE *file)
- {
- IMAGE_RESOURCE_DIRECTORY_ENTRY * ResourceEntry = new IMAGE_RESOURCE_DIRECTORY_ENTRY;
- IMAGE_RESOURCE_DIRECTORY * ResourceDirectory = new IMAGE_RESOURCE_DIRECTORY;
- DWORD ResourceDirectoryAddress = this->gfxs[gfxfileindex].ResourceDirectoryAddress;
- DWORD SecAddress = this->gfxs[gfxfileindex].SecAddress;
- DWORD SecRVA = this->gfxs[gfxfileindex].SecRVA;
- bool found = false;
- Get_Pe_Struct(ResourceDirectory,file,ResourceDirectoryAddress);
- for(int i = 0; !found && i < ResourceDirectory->NumberOfNamedEntries +
- ResourceDirectory->NumberOfIdEntries; i++)
- {
- DWORD DirEAddress = ResourceDirectoryAddress+
- sizeof(IMAGE_RESOURCE_DIRECTORY)+
- sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY)*i;
- Get_Pe_Struct(ResourceEntry,file,DirEAddress);
- if(ResourceEntry->Id == 2 ) // 2 for bitmap resources "Bitmap"
- {
- //in the BITMAP RES
- ResourceDirectoryAddress = ResourceEntry->OffsetToDirectory + SecAddress;
- Get_Pe_Struct(ResourceDirectory,file,ResourceDirectoryAddress);
- for (int p = 0; !found &&p < ResourceDirectory->NumberOfNamedEntries +
- ResourceDirectory->NumberOfIdEntries; p++)
- {
- DirEAddress = ResourceDirectoryAddress+
- sizeof(IMAGE_RESOURCE_DIRECTORY) +
- sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY)*p;
- Get_Pe_Struct(ResourceEntry,file,DirEAddress);
- if(ResourceEntry->Id == id )
- {
- //in the required directory,looping till a dead end!
- found = true;
- while(ResourceEntry->DataIsDirectory)
- {
- ResourceDirectoryAddress = ResourceEntry->OffsetToDirectory + SecAddress;
- Get_Pe_Struct(ResourceDirectory,file,ResourceDirectoryAddress);
- DirEAddress = ResourceDirectoryAddress+sizeof(IMAGE_RESOURCE_DIRECTORY);
- Get_Pe_Struct(ResourceEntry,file,DirEAddress);
- }
- IMAGE_RESOURCE_DATA_ENTRY * data_entry = new IMAGE_RESOURCE_DATA_ENTRY;
- Get_Pe_Struct(data_entry,file,ResourceEntry->OffsetToData+SecAddress);
- bitmapdat->entry_address = SecAddress + data_entry->OffsetToData - SecRVA;
- bitmapdat->dump_size = data_entry->Size;
- bitmapdat->total_size = bitmapdat->dump_size + sizeof(BITMAPFILEHEADER);
- delete data_entry;
- /*Bitmap file header loading */
- bitmapdat->bitmap_file_header.bfType = 0x4D42; // BM signature
- bitmapdat->bitmap_file_header.bfSize = bitmapdat->total_size;
- bitmapdat->bitmap_file_header.bfReserved1 = 0x00;
- bitmapdat->bitmap_file_header.bfReserved2 = 0x00;
- bitmapdat->bitmap_file_header.bfOffBits = sizeof(BITMAPFILEHEADER)+
- sizeof(BITMAPINFOHEADER);
- }
- }
- }
- }
- delete ResourceEntry;
- delete ResourceDirectory;
- return found;
- }
- void PeBmLoader::Get_Bit_Map_File(char *& b_file,BitMapDat * bitmapdat,FILE *file)
- {
- memset(b_file,0,bitmapdat->total_size);
- memcpy(b_file,&bitmapdat->bitmap_file_header,sizeof(BITMAPFILEHEADER));
- fseek(file,bitmapdat->entry_address,SEEK_SET);
- fread(b_file+sizeof(BITMAPFILEHEADER),sizeof(char),bitmapdat->dump_size,file);
- }
- ALLEGRO_BITMAP * PeBmLoader::Get_Bit_Map(int id,const char * dir)
- {
- for(size_t i = 0 ; i < this->gfxs.size() ; i++)
- {
- if(strstr(this->gfxs[i].dir,dir))
- return Get_Bit_Map(id,i);
- }
- return 0;
- }
- ALLEGRO_BITMAP * PeBmLoader::Get_Bit_Map(int id,int index)
- {
- ALLEGRO_FILE * mapped_file;
- ALLEGRO_BITMAP * bitmap;
- BitMapDat * bitmapdat;
- char *b_file;
- const char *dir = this->gfxs[index].dir;
- FILE *file;
- file = fopen ( dir,"rb" );
- if (file==NULL)
- {
- //[Error to be handled]
- //Fail not found!
- //what else could it be when you fail to open a file in read only mode?!
- return NULL;
- }
- bitmapdat = new BitMapDat;
- bool entry_found = Get_Bit_Map_Dat(bitmapdat,id,index,file);
- if(!entry_found)
- {
- //[Error to be handled]
- //Entry is not found!
- delete bitmapdat;
- return NULL;
- }
- b_file = new char[bitmapdat->total_size];
- Get_Bit_Map_File(b_file,bitmapdat,file);
- mapped_file = al_open_memfile((void*)b_file,bitmapdat->total_size,"r");
- bitmap = al_load_bitmap_f(mapped_file, ".bmp");
- al_fclose(mapped_file);
- fclose(file);
- delete [] b_file;
- delete bitmapdat;
- return bitmap;
- }
- bool PeBmLoader::LoadGfxFile(const char* dir)
- {
- FILE *file = fopen ( dir, "rb" );
- if (file==NULL)
- {
- //[Error to be handled]
- //Fail not found!
- //what else could it be when you fail to open a file in read only mode?!
- return false;
- }
- IMAGE_DOS_HEADER * image_dos_header = new IMAGE_DOS_HEADER;
- IMAGE_NT_HEADERS * pe_header = new IMAGE_NT_HEADERS;
- IMAGE_SECTION_HEADER * section_header = new IMAGE_SECTION_HEADER;
- IMAGE_DATA_DIRECTORY * DataDirectory = new IMAGE_DATA_DIRECTORY ;
- DWORD ResourceDirectoryRVA;
- DWORD SecAddress;
- DWORD SecRVA;
- DWORD ResourceDirectoryAddress;
- Get_Pe_Struct(image_dos_header,file,0);
- Get_Pe_Struct(pe_header,file,image_dos_header->e_lfanew);
- Get_Res_Section_Header(section_header,pe_header->FileHeader.NumberOfSections,
- image_dos_header->e_lfanew,
- file);
- *DataDirectory = pe_header->OptionalHeader.DataDirectory [2];
- ResourceDirectoryRVA = DataDirectory->VirtualAddress;
- SecAddress = section_header->PointerToRawData;
- SecRVA = section_header->VirtualAddress;
- ResourceDirectoryAddress = SecAddress + ResourceDirectoryRVA - SecRVA;
- GfxFile gfxfile;
- gfxfile.dir = dir;
- gfxfile.ResourceDirectoryAddress = ResourceDirectoryAddress;
- gfxfile.SecAddress = SecAddress;
- gfxfile.SecRVA = SecRVA;
- this->gfxs.push_back(gfxfile);
- delete image_dos_header;
- delete pe_header;
- delete section_header;
- delete DataDirectory;
- fclose(file);
- return true;
- }
Add Comment
Please, Sign In to add comment