DWORD getImportThunkRVA(FILE* f, char* dllName, char* funcName) { if (strlen(dllName)==0 || strlen(funcName)==0) return 0; DWORD itSize = SeekImportTable(f); DWORD numDLLs = itSize / sizeof(IMAGE_IMPORT_DESCRIPTOR); for (WORD i=0; i < numDLLs; i++) { IMAGE_IMPORT_DESCRIPTOR iid; fread(&iid, sizeof(IMAGE_IMPORT_DESCRIPTOR), 1, f); DWORD fp = ftell(f); char iidName[256]; fseek(f, getFileOffset(f, iid.Name), SEEK_SET); fread(iidName, 256, 1, f); //find the right dll if (stricmp(iidName, dllName) == 0) { // the FLT no-dvd has no OFT, but for the vitality no-dvd the FT is wrong if (iid.OriginalFirstThunk) fseek(f, getFileOffset(f, iid.OriginalFirstThunk), SEEK_SET); else fseek(f, getFileOffset(f, iid.FirstThunk), SEEK_SET); //now find the function for (WORD j=0;; j++) { DWORD thunkRVA; fread(&thunkRVA, sizeof(DWORD), 1, f); DWORD fp1 = ftell(f); if (thunkRVA==0) break; char funcName1[256]; fseek(f, getFileOffset(f, thunkRVA) + sizeof(WORD), SEEK_SET); fread(funcName1, 256, 1, f); if (stricmp(funcName1, funcName) == 0) { return iid.FirstThunk + j*sizeof(DWORD); } fseek(f, fp1, SEEK_SET); } } fseek(f, fp, SEEK_SET); } return 0; }
int mtFile::runRead( mtThreadWork::DataUser* pkDataUser ) { char* pcFile = getFileName(pkDataUser->pcAccount); if (NULL == pcFile) { return mtProtocol::E_RESULT_FALT_FILE_INVALIDE_PATH; } if (0 == FileOpen(&pkDataUser->hFile, pcFile)) { return mtProtocol::E_RESULT_FALT_FILE_NOT_EXIST; } pkDataUser->iFileOffset = getFileOffset(pkDataUser->pcAccount); pkDataUser->ulTransferBytes = getDataFileBytes(); pkDataUser->kOverlapped.Offset = pkDataUser->iFileOffset; pkDataUser->eOverlappedType = mtThread::E_OLT_LOCKFILEEx; pkDataUser->iFileRunType = E_FILE_RUN_READ_FILE; if (0 == FileLock(pkDataUser->hFile, pkDataUser->ulTransferBytes, &pkDataUser->kOverlapped)) { FileClose(&pkDataUser->hFile); return mtProtocol::E_RESULT_FALT_FILE_LOCK; } return mtProtocol::E_RESULT_SUCCESS; }
int mtFile::completeWriteCreate( mtThreadWork::DataUser* pkDataUser ) { pkDataUser->iFileOffset = getFileOffset(pkDataUser->pcAccount); pkDataUser->ulTransferBytes = getDataFileBytes(); pkDataUser->iFileRunType = E_FILE_RUN_WRITE_FILE; pkDataUser->kOverlapped.Offset = pkDataUser->iFileOffset; pkDataUser->eOverlappedType = mtThread::E_OLT_LOCKFILEEx; if (0 == FileLock(pkDataUser->hFile, pkDataUser->ulTransferBytes, &pkDataUser->kOverlapped, LOCKFILE_EXCLUSIVE_LOCK)) { FileClose(&pkDataUser->hFile); return mtProtocol::E_RESULT_FALT_FILE_LOCK; } return mtProtocol::E_RESULT_SUCCESS; }
/**************************************************************************** Desc: This is a private method that will truncate the spill file back to the specified size. ****************************************************************************/ RCODE F_MultiFileHdl::truncateFile( FLMUINT64 ui64NewSize) { RCODE rc = NE_FLM_OK; FLMUINT uiFileNum = getFileNum( ui64NewSize); IF_FileHdl * pFileHdl; if( RC_BAD( rc = getFileHdl( uiFileNum, TRUE, &pFileHdl))) { goto Exit; } if (RC_BAD( rc = pFileHdl->truncateFile( getFileOffset( ui64NewSize)))) { goto Exit; } Exit: return( rc); }
DWORD SeekImportTable(FILE *f) { DWORD itRVA=0, itSize=0; fseek(f, 0, SEEK_SET); IMAGE_DOS_HEADER dh; fread(&dh, sizeof(IMAGE_DOS_HEADER), 1, f); fseek(f, dh.e_lfanew - sizeof(IMAGE_DOS_HEADER), SEEK_CUR); IMAGE_NT_HEADERS nth; fread(&nth, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), 1, f); if (nth.FileHeader.SizeOfOptionalHeader > 0) { fseek(f, 104, SEEK_CUR); fread(&itRVA, sizeof(DWORD), 1, f); fread(&itSize, sizeof(DWORD), 1, f); fseek(f, getFileOffset(f, itRVA), SEEK_SET); return itSize; } return 0; }
int mtFile::runWrite( mtThreadWork::DataUser* pkDataUser ) { char* pcFile = getFileName(pkDataUser->pcAccount); if (NULL == pcFile) { return mtProtocol::E_RESULT_FALT_FILE_INVALIDE_PATH; } if (FileOpen(&pkDataUser->hFile, pcFile)) { pkDataUser->iFileOffset = getFileOffset(pkDataUser->pcAccount); pkDataUser->ulTransferBytes = getDataFileBytes(); pkDataUser->iFileRunType = E_FILE_RUN_WRITE_FILE; } else { /// ╢╢╫╗нд╪Ч if (FileCreate(&pkDataUser->hFile, pcFile, getFolderBytes())) { pkDataUser->iFileOffset = 0; pkDataUser->ulTransferBytes = getCreateFileBytes(); pkDataUser->iFileRunType = E_FILE_RUN_WRITE_FILE_CREATE; } else { return mtProtocol::E_RESULT_FALT_FILE_CREATE_FUNCTION; } } pkDataUser->kOverlapped.Offset = pkDataUser->iFileOffset; pkDataUser->eOverlappedType = mtThread::E_OLT_LOCKFILEEx; if (0 == FileLock(pkDataUser->hFile, pkDataUser->ulTransferBytes, &pkDataUser->kOverlapped, LOCKFILE_EXCLUSIVE_LOCK)) { FileClose(&pkDataUser->hFile); return mtProtocol::E_RESULT_FALT_FILE_LOCK; } return mtProtocol::E_RESULT_SUCCESS; }
// Generate a header event bool WriteUserLogHeader::GenerateEvent( GenericEvent &event ) { int len = snprintf( event.info, COUNTOF(event.info), "Global JobLog:" " ctime=%d" " id=%s" " sequence=%d" " size=" FILESIZE_T_FORMAT"" " events=%" PRId64"" " offset=" FILESIZE_T_FORMAT"" " event_off=%" PRId64"" " max_rotation=%d" " creator_name=<%s>", (int) getCtime(), getId().Value(), getSequence(), getSize(), getNumEvents(), getFileOffset(), getEventOffset(), getMaxRotation(), getCreatorName().Value() ); if (len < 0 || len == sizeof(event.info)) { // not enough room in the buffer len = (int)COUNTOF(event.info)-1; event.info[len] = 0; // make sure it's null terminated. ::dprintf( D_FULLDEBUG, "Generated (truncated) log header: '%s'\n", event.info ); } else { ::dprintf( D_FULLDEBUG, "Generated log header: '%s'\n", event.info ); while( len < 256 ) { event.info[len++] = ' '; event.info[len] = 0; } } return true; }
/**************************************************************************** Desc: Writes data to the file ****************************************************************************/ RCODE F_MultiFileHdl::write( FLMUINT64 ui64Offset, // Offset FLMUINT uiLength, // Number of bytes to write. void * pvBuffer, // Buffer that contains bytes to be written FLMUINT * puiBytesWritten) // Number of bytes written. { RCODE rc = NE_FLM_OK; FLMUINT uiFileNum = getFileNum( ui64Offset); FLMUINT uiFileOffset = getFileOffset( ui64Offset); FLMUINT uiTmp; FLMUINT uiTotalBytesWritten = 0; FLMUINT uiBytesToWrite; FLMUINT uiMaxWriteLen; IF_FileHdl * pFileHdl; // Don't allow zero-length writes f_assert( uiLength); // Write to the data file(s), moving to new files as needed. for( ;;) { if( RC_BAD( rc = getFileHdl( uiFileNum, TRUE, &pFileHdl))) { goto Exit; } uiMaxWriteLen = m_uiMaxFileSize - uiFileOffset; f_assert( uiMaxWriteLen != 0); uiBytesToWrite = uiLength >= uiMaxWriteLen ? uiMaxWriteLen : uiLength; uiTmp = 0; rc = pFileHdl->write( uiFileOffset, uiBytesToWrite, pvBuffer, &uiTmp); uiTotalBytesWritten += uiTmp; uiLength -= uiTmp; ui64Offset += uiTmp; if( RC_BAD( rc)) { goto Exit; } if( !uiLength) { break; } // Set up for next write pvBuffer = ((FLMBYTE *)pvBuffer) + uiTmp; uiFileNum = getFileNum( ui64Offset); uiFileOffset = getFileOffset( ui64Offset); } Exit: if( ui64Offset > m_ui64EOF) { m_ui64EOF = ui64Offset; } *puiBytesWritten = uiTotalBytesWritten; return( rc); }
/**************************************************************************** Desc: Reads data from the file ****************************************************************************/ RCODE F_MultiFileHdl::read( FLMUINT64 ui64Offset, // Offset to begin reading FLMUINT uiLength, // Number of bytes to read void * pvBuffer, // Buffer FLMUINT * puiBytesRead) // [out] Number of bytes read { RCODE rc = NE_FLM_OK; FLMUINT uiFileNum = getFileNum( ui64Offset); FLMUINT uiFileOffset = getFileOffset( ui64Offset); FLMUINT uiTmp; FLMUINT uiTotalBytesRead = 0; FLMUINT uiBytesToRead; FLMUINT uiMaxReadLen; IF_FileHdl * pFileHdl; // Handle the case of a 0-byte read if( !uiLength) { if( ui64Offset >= m_ui64EOF) { rc = RC_SET( NE_FLM_IO_END_OF_FILE); } goto Exit; } // Read the data file(s), moving to new files as needed. for( ;;) { if( ui64Offset >= m_ui64EOF) { rc = RC_SET( NE_FLM_IO_END_OF_FILE); goto Exit; } uiMaxReadLen = m_uiMaxFileSize - uiFileOffset; f_assert( uiMaxReadLen != 0); uiTmp = (uiLength >= uiMaxReadLen ? uiMaxReadLen : uiLength); uiBytesToRead = (((FLMUINT64)uiTmp > (FLMUINT64)(m_ui64EOF - ui64Offset)) ? (FLMUINT)(m_ui64EOF - ui64Offset) : uiTmp); if( RC_BAD( rc = getFileHdl( uiFileNum, FALSE, &pFileHdl))) { if( rc == NE_FLM_IO_PATH_NOT_FOUND) { // Handle the case of a sparse file by filling the unread // portion of the buffer with zeros. f_memset( pvBuffer, 0, uiBytesToRead); uiTmp = uiBytesToRead; rc = NE_FLM_OK; } else { goto Exit; } } else { if( RC_BAD( rc = pFileHdl->read( uiFileOffset, uiBytesToRead, pvBuffer, &uiTmp))) { if( rc == NE_FLM_IO_END_OF_FILE) { // Handle the case of a sparse file by filling the unread // portion of the buffer with zeros. f_memset( &(((FLMBYTE *)(pvBuffer))[ uiTmp]), 0, (FLMUINT)(uiBytesToRead - uiTmp)); uiTmp = uiBytesToRead; rc = NE_FLM_OK; } else { goto Exit; } } } uiTotalBytesRead += uiTmp; uiLength -= uiTmp; if( !uiLength) { break; } // Set up for next read pvBuffer = ((FLMBYTE *)pvBuffer) + uiTmp; ui64Offset += uiTmp; uiFileNum = getFileNum( ui64Offset); uiFileOffset = getFileOffset( ui64Offset); } Exit: *puiBytesRead = uiTotalBytesRead; return( rc); }
void ResourceManager::pushCurrentFileAndOffsetOntoStack() { fileOffsetStack[indexOfFileOffsetStack] = getFileOffset(this->fileDescriptors[this->curHandleIdx]); handleIndicesStack[indexOfFileOffsetStack++] = this->curHandleIdx; }