/**---------------------------------------------------------------------------- \brief \param \return \code \endcode -----------------------------------------------------------------------------*/ DTSTATUS FileIoHelper::FIOWriteToFile( IN LARGE_INTEGER Offset, IN DWORD Size, IN PUCHAR Buffer ) { _ASSERTE(NULL != Buffer); _ASSERTE(0 != Size); _ASSERTE(NULL != Buffer); if (NULL == Buffer || 0 == Size || NULL == Buffer) return DTS_INVALID_PARAMETER; UCHAR* p = NULL; DTSTATUS status = FIOReference(FALSE, Offset, Size, p); if (TRUE != DT_SUCCEEDED(status)) { return status; } __try { RtlCopyMemory(p, Buffer, Size); } __except (EXCEPTION_EXECUTE_HANDLER) { status = DTS_EXCEPTION_RAISED; } FIOUnreference(); return status; }
/**---------------------------------------------------------------------------- \brief \param \return \code \endcode -----------------------------------------------------------------------------*/ DTSTATUS FileIoHelper::FIOReadFromFile( IN LARGE_INTEGER Offset, IN DWORD Size, IN OUT PUCHAR Buffer ) { _ASSERTE(NULL != Buffer); if (NULL == Buffer) return DTS_INVALID_PARAMETER; UCHAR* p = NULL; DTSTATUS status = FIOReference(TRUE, Offset, Size, p); if (TRUE != DT_SUCCEEDED(status)) { printf("FIOReference() failed. status=0x%08x", status); return status; } __try { RtlCopyMemory(Buffer, p, Size); } __except (EXCEPTION_EXECUTE_HANDLER) { printf("exception. code=0x%08x", GetExceptionCode()); status = DTS_EXCEPTION_RAISED; } FIOUnreference(); return status; }
/**---------------------------------------------------------------------------- \brief \param \return \code \endcode -----------------------------------------------------------------------------*/ void FileIoHelper::FIOClose( ) { if (TRUE != Initialized()) return; FIOUnreference(); CloseHandle(mFileMap); mFileMap = NULL; CloseHandle(mFileHandle); mFileHandle = INVALID_HANDLE_VALUE; }
/**---------------------------------------------------------------------------- \brief \param \return \code \endcode -----------------------------------------------------------------------------*/ DTSTATUS FileIoHelper::FIOReference( IN BOOL ReadOnly, IN LARGE_INTEGER Offset, IN DWORD Size, IN OUT PUCHAR& ReferencedPointer ) { if (TRUE != Initialized()) return DTS_INVALID_OBJECT_STATUS; if (TRUE == IsReadOnly()) { if (TRUE != ReadOnly) { printf("file handle is read-only!"); return DTS_INVALID_PARAMETER; } } _ASSERTE(NULL == mFileView); FIOUnreference(); if (Offset.QuadPart + Size > mFileSize.QuadPart) { printf( "invalid offset. file size=%I64d, req offset=%I64d", mFileSize.QuadPart, Offset.QuadPart ); return DTS_INVALID_PARAMETER; } // // MapViewOfFile() 함수의 dwFileOffsetLow 파라미터는 // SYSTEM_INFO::dwAllocationGranularity 값의 배수이어야 한다. // static DWORD AllocationGranularity = 0; if (0 == AllocationGranularity) { SYSTEM_INFO si = { 0 }; GetSystemInfo(&si); AllocationGranularity = si.dwAllocationGranularity; } DWORD AdjustMask = AllocationGranularity - 1; LARGE_INTEGER AdjustOffset = { 0 }; AdjustOffset.HighPart = Offset.HighPart; // AllocationGranularity 이하의 값을 버림 // AdjustOffset.LowPart = (Offset.LowPart & ~AdjustMask); // 버려진 값만큼 매핑할 사이즈를 증가 // DWORD BytesToMap = (Offset.LowPart & AdjustMask) + Size; mFileView = (PUCHAR)MapViewOfFile( mFileMap, (TRUE == ReadOnly) ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE, AdjustOffset.HighPart, AdjustOffset.LowPart, BytesToMap ); if (NULL == mFileView) { printf( "MapViewOfFile(high=0x%08x, log=0x%08x, bytes to map=%u) failed, gle=0x%08x", AdjustOffset.HighPart, AdjustOffset.LowPart, BytesToMap, GetLastError() ); return DTS_WINAPI_FAILED; } ReferencedPointer = &mFileView[Offset.LowPart & AdjustMask]; return DTS_SUCCESS; }