BOOL GetSectionConfigData(PVOID pvPEBase,PVOID *ppvData,DWORD *pdwDataSize) { BOOL bRet = FALSE; PIMAGE_SECTION_HEADER pvSection; if (pvSection = SearchSection(pvPEBase,SECTION_CONFIG_NAME)) { PSECTION_CONFIG_HEADER pSecCfgHeader = MAKE_PTR(pvPEBase,pvSection->VirtualAddress,PSECTION_CONFIG_HEADER); PVOID pvDecryptedData; if (pvDecryptedData = malloc(pSecCfgHeader->dwCompressedSize)) { EncryptRc4(pSecCfgHeader->bRc4Key,sizeof(pSecCfgHeader->bRc4Key),pvDecryptedData,&pSecCfgHeader[1],pSecCfgHeader->dwCompressedSize); if (bRet = DecompressBuffer(pvDecryptedData,pSecCfgHeader->dwCompressedSize,ppvData,pSecCfgHeader->dwDecompressedSize)) { *pdwDataSize = pSecCfgHeader->dwDecompressedSize; } free(pvDecryptedData); } } return bRet; }
void ResourceFile::AllocateResources() { if (!get_) { #if defined(EMSCRIPTEN) { auto filename = path_.GetFilePath(); std::ifstream file(filename.c_str(), std::ios::binary); if (file.is_open()) { file.seekg(0, std::ios::end); std::streampos filelength = file.tellg(); file.seekg(0, std::ios::beg); buffer_.resize((int)filelength); file.read(&buffer_[0], filelength); CHECK_ASSERT(file.gcount() == filelength); file.close(); LOGI("%s has been loaded with size=%d", filename.c_str(), buffer_.size()); } else { LOGE("Cannot load %s", filename.c_str()); } } #else { CHECK_ASSERT(isLocal_); auto filename = path_.GetFilePath(); SDL_RWops* context = SDL_RWFromFile(filename.c_str(), "rb"); if (context) { SDL_RWseek(context, 0, RW_SEEK_END); Sint64 filelength = SDL_RWtell(context); SDL_RWseek(context, 0, RW_SEEK_SET); buffer_.resize((int)filelength); SDL_RWread(context, &buffer_[0], buffer_.size(), 1); SDL_RWclose(context); LOGI("%s has been loaded with size=%u", filename.c_str(), (unsigned)buffer_.size()); } else { LOGE("Cannot load %s with error %s", filename.c_str(), SDL_GetError()); } } #endif } if (path_.GetExtension() == "lz4") buffer_ = DecompressBuffer(buffer_); }
// Assumes that VerifyPackageIntegrity() has been used. Returns TRUE, if successful (otherwise FALSE). // Creates a table of pointers to buffers containing the following objects for each file: // filename size, filename (not null-terminated!), file size, file CRC-32, uncompressed file contents. // For details, see the definition of the DECOMPRESSED_FILE structure. BOOL SelfExtractInMemory (char *path) { int filePos = 0, fileNo = 0; int fileDataEndPos = 0; int fileDataStartPos = 0; unsigned int uncompressedLen = 0; unsigned int compressedLen = 0; unsigned char *compressedData = NULL; unsigned char *bufPos = NULL, *bufEndPos = NULL; FreeAllFileBuffers(); fileDataEndPos = (int) FindStringInFile (path, MagEndMarker, strlen (MagEndMarker)); if (fileDataEndPos < 0) { Error ("CANNOT_READ_FROM_PACKAGE"); return FALSE; } fileDataEndPos--; fileDataStartPos = (int) FindStringInFile (path, MAG_START_MARKER, strlen (MAG_START_MARKER)); if (fileDataStartPos < 0) { Error ("CANNOT_READ_FROM_PACKAGE"); return FALSE; } fileDataStartPos += strlen (MAG_START_MARKER); filePos = fileDataStartPos; // Read the stored total size of the uncompressed data if (!LoadInt32 (path, &uncompressedLen, filePos)) { Error ("CANNOT_READ_FROM_PACKAGE"); return FALSE; } filePos += 4; // Read the stored total size of the compressed data if (!LoadInt32 (path, &compressedLen, filePos)) { Error ("CANNOT_READ_FROM_PACKAGE"); return FALSE; } filePos += 4; if (compressedLen != fileDataEndPos - fileDataStartPos - 8 + 1) { Error ("DIST_PACKAGE_CORRUPTED"); } DecompressedData = malloc (uncompressedLen + 524288); // + 512K reserve if (DecompressedData == NULL) { Error ("ERR_MEM_ALLOC"); return FALSE; } bufPos = DecompressedData; bufEndPos = bufPos + uncompressedLen - 1; compressedData = LoadFileBlock (path, filePos, compressedLen); if (compressedData == NULL) { free (DecompressedData); DecompressedData = NULL; Error ("CANNOT_READ_FROM_PACKAGE"); return FALSE; } // Decompress the data if (DecompressBuffer (DecompressedData, compressedData, compressedLen) != uncompressedLen) { Error ("DIST_PACKAGE_CORRUPTED"); goto sem_end; } while (bufPos <= bufEndPos && fileNo < NBR_COMPRESSED_FILES) { // Filename length Decompressed_Files[fileNo].fileNameLength = mgetWord (bufPos); // Filename Decompressed_Files[fileNo].fileName = bufPos; bufPos += Decompressed_Files[fileNo].fileNameLength; // CRC-32 of the file Decompressed_Files[fileNo].crc = mgetLong (bufPos); // File length Decompressed_Files[fileNo].fileLength = mgetLong (bufPos); // File content Decompressed_Files[fileNo].fileContent = bufPos; bufPos += Decompressed_Files[fileNo].fileLength; // Verify CRC-32 of the file (to verify that it didn't get corrupted while creating the solid archive). if (Decompressed_Files[fileNo].crc != GetCrc32 (Decompressed_Files[fileNo].fileContent, Decompressed_Files[fileNo].fileLength)) { Error ("DIST_PACKAGE_CORRUPTED"); goto sem_end; } fileNo++; } if (fileNo < NBR_COMPRESSED_FILES) { Error ("DIST_PACKAGE_CORRUPTED"); goto sem_end; } free (compressedData); return TRUE; sem_end: FreeAllFileBuffers(); free (compressedData); return FALSE; }