static int OnDeltaPos(HWND hDlg, NMUPDOWN * pNMUpDown) { TFileTestData * pData = GetDialogData(hDlg); LONGLONG ByteOffset = 0; DWORD Length = 0; if(pNMUpDown->hdr.idFrom == IDC_BYTE_OFFSET_SPIN) { DlgText2Hex64(hDlg, IDC_BYTE_OFFSET, &ByteOffset); ByteOffset -= pNMUpDown->iDelta * 0x10000; if(ByteOffset < 0) ByteOffset = 0; Hex2DlgText64(hDlg, IDC_BYTE_OFFSET, ByteOffset); UpdateFileData(hDlg, NULL, pData->cbFileData, pData->FillPattern); } if(pNMUpDown->hdr.idFrom == IDC_LENGTH_SPIN) { DlgText2Hex32(hDlg, IDC_LENGTH, &Length); Length -= pNMUpDown->iDelta * 0x1000; if((int)Length < 0) Length = 0; Hex2DlgText32(hDlg, IDC_LENGTH, Length); UpdateFileData(hDlg, NULL, pData->cbFileData, pData->FillPattern); } return TRUE; }
static int OnDeltaPos(HWND hDlg, NMUPDOWN * pNMHDR) { LARGE_INTEGER Uint64; DWORD Uint32; // Get the proper edit box if(pNMHDR->hdr.idFrom == IDC_BYTE_OFFSET_SPIN) { DlgText2Hex64(hDlg, IDC_BYTE_OFFSET, &Uint64.QuadPart); if(pNMHDR->iDelta < 0) Uint64.QuadPart += 0x10000; else Uint64.QuadPart -= 0x10000; if(Uint64.HighPart & 0x80000000) Uint64.QuadPart = 0; Hex2DlgText64(hDlg, IDC_BYTE_OFFSET, Uint64.QuadPart); return TRUE; } if(pNMHDR->hdr.idFrom == IDC_LENGTH_SPIN) { DlgText2Hex32(hDlg, IDC_LENGTH, &Uint32); if(pNMHDR->iDelta < 0) Uint32 += 0x10000; else Uint32 -= 0x10000; if(Uint32 & 0x80000000) Uint32 = 0; Hex2DlgText32(hDlg, IDC_LENGTH, Uint32); return TRUE; } return FALSE; }
static int OnDeltaPos(HWND hDlg, NMUPDOWN * pNMUpDown) { UINT nIDCtrl = 0; bool bIsValuePointer = false; switch(pNMUpDown->hdr.idFrom) { case IDC_SECTION_SIZE_UPDOWN: nIDCtrl = IDC_SECTION_SIZE; break; case IDC_BASE_ADDRESS_UPDOWN: nIDCtrl = IDC_BASE_ADDRESS; bIsValuePointer = true; break; case IDC_COMMIT_SIZE_UPDOWN: nIDCtrl = IDC_COMMIT_SIZE; bIsValuePointer = true; break; case IDC_SECTION_OFFSET_UPDOWN: nIDCtrl = IDC_SECTION_OFFSET; break; case IDC_VIEW_SIZE_UPDOWN: nIDCtrl = IDC_VIEW_SIZE; bIsValuePointer = true; break; default: assert(false); return FALSE; } // If we have to set a pointer, do it if(bIsValuePointer == false) { ULONGLONG NewValue; ULONGLONG OldValue; DlgText2Hex64(hDlg, nIDCtrl, (PLONGLONG)&OldValue); NewValue = OldValue - (pNMUpDown->iDelta * 0x1000); if(pNMUpDown->iDelta > 0 && NewValue > OldValue) NewValue = 0; Hex2DlgText64(hDlg, nIDCtrl, NewValue); } else { DWORD_PTR NewValue; DWORD_PTR OldValue; DlgText2HexPtr(hDlg, nIDCtrl, &OldValue); NewValue = OldValue - (pNMUpDown->iDelta * 0x1000); if(pNMUpDown->iDelta > 0 && NewValue > OldValue) NewValue = 0; Hex2DlgTextPtr(hDlg, nIDCtrl, NewValue); } return TRUE; }
static int SaveDialog(HWND hDlg) { TFileTestData * pData = GetDialogData(hDlg); HWND hCombo = GetDlgItem(hDlg, IDC_CREATE_DISPOSITION); int nError; GetDlgItemText(hDlg, IDC_DIRECTORY_NAME, pData->szDirName, _maxchars(pData->szDirName)); GetDlgItemText(hDlg, IDC_FILE_NAME, pData->szFileName1, _maxchars(pData->szFileName1)); if((nError = DlgText2Hex32(hDlg, IDC_OBJ_ATTR_FLAGS, &pData->dwObjAttrFlags)) != ERROR_SUCCESS) return nError; if((nError = DlgText2Hex32(hDlg, IDC_DESIRED_ACCESS, &pData->dwDesiredAccess)) != ERROR_SUCCESS) return nError; if((nError = DlgText2Hex32(hDlg, IDC_FILE_ATTRIBUTES, &pData->dwFileAttributes)) != ERROR_SUCCESS) return nError; if((nError = DlgText2Hex32(hDlg, IDC_SHARE_ACCESS, &pData->dwShareAccess)) != ERROR_SUCCESS) return nError; if((nError = DlgText2Hex32(hDlg, IDC_CREATE_OPTIONS, &pData->dwCreateOptions)) != ERROR_SUCCESS) return nError; if((nError = DlgText2Hex64(hDlg, IDC_ALLOCATION_SIZE, &pData->AllocationSize)) != ERROR_SUCCESS) return nError; pData->dwCreateDisposition2 = ComboBox_GetCurSel(hCombo); pData->bUseTransaction = (IsDlgButtonChecked(hDlg, IDC_TRANSACTED) == BST_CHECKED); return ERROR_SUCCESS; }
static int OnDeltaPos(HWND hDlg, NMUPDOWN * pNMUpDown) { LONGLONG AllocationSize; DlgText2Hex64(hDlg, IDC_ALLOCATION_SIZE, &AllocationSize); AllocationSize -= pNMUpDown->iDelta * 0x100; if(AllocationSize < 0) AllocationSize = 0; Hex2DlgText64(hDlg, IDC_ALLOCATION_SIZE, AllocationSize); return TRUE; }
static int OnSetFilePointerClick(HWND hDlg) { TFileTestData * pData = GetDialogData(hDlg); LARGE_INTEGER ByteOffset; int nError = ERROR_SUCCESS; // Get the file offset from the dialog DlgText2Hex64(hDlg, IDC_BYTE_OFFSET, &ByteOffset.QuadPart); // Apply the file size SetLastError(ERROR_SUCCESS); SetFilePointer(pData->hFile, ByteOffset.LowPart, &ByteOffset.HighPart, FILE_BEGIN); nError = GetLastError(); SetResultInfo(hDlg, nError); return TRUE; }
static int SaveDialog1(HWND hDlg) { TFileTestData * pData = GetDialogData(hDlg); int nError; GetDlgItemText(hDlg, IDC_SECTION_NAME, pData->szSectionName, _tsize(pData->szSectionName)); if((nError = DlgText2Hex64(hDlg, IDC_SECTION_SIZE, &pData->MaximumSize.QuadPart)) != ERROR_SUCCESS) return nError; if((nError = DlgText2Hex32(hDlg, IDC_DESIRED_ACCESS, &pData->dwSectDesiredAccess)) != ERROR_SUCCESS) return nError; if((nError = DlgText2Hex32(hDlg, IDC_ALLOCATION_ATTRIBUTES, &pData->dwSectAllocAttributes)) != ERROR_SUCCESS) return nError; pData->dwSectPageProtection = PageProtection2Hex(hDlg, IDC_PAGE_PROTECTION); return ERROR_SUCCESS; }
static int SaveDialog2(HWND hDlg) { TFileTestData * pData = GetDialogData(hDlg); int nError; if((nError = DlgText2HexPtr(hDlg, IDC_BASE_ADDRESS, (PDWORD_PTR)(&pData->pvSectionMappedView))) != ERROR_SUCCESS) return nError; if((nError = DlgText2HexPtr(hDlg, IDC_COMMIT_SIZE, (PDWORD_PTR)(&pData->cbSectCommitSize))) != ERROR_SUCCESS) return nError; if((nError = DlgText2Hex64(hDlg, IDC_SECTION_OFFSET, &pData->SectionOffset.QuadPart)) != ERROR_SUCCESS) return nError; if((nError = DlgText2HexPtr(hDlg, IDC_VIEW_SIZE, (PDWORD_PTR)(&pData->cbSectViewSize))) != ERROR_SUCCESS) return nError; if((nError = DlgText2Hex32(hDlg, IDC_ALLOCATION_TYPE, &pData->dwSectAllocType)) != ERROR_SUCCESS) return nError; pData->dwSectWin32Protect = PageProtection2Hex(hDlg, IDC_WIN32_PROTECTION); return ERROR_SUCCESS; }
static int OnLockUnlockFile(HWND hDlg, int nReadWriteType) { TFileTestData * pData = GetDialogData(hDlg); TApcReadWrite * pApc; LARGE_INTEGER ByteOffset = {0}; LARGE_INTEGER LockLength = {0}; NTSTATUS Status; int nError = ERROR_SUCCESS; // Get the start position DlgText2Hex64(hDlg, IDC_BYTE_OFFSET, &ByteOffset.QuadPart); DlgText2Hex32(hDlg, IDC_LENGTH, &LockLength.LowPart); assert((LONGLONG)pData->cbFileData >= LockLength.QuadPart); // Create new APC entry pApc = (TApcReadWrite *)CreateApcEntry(pData, APC_TYPE_READ_WRITE, sizeof(TApcReadWrite)); if(pApc != NULL) { // Perform the I/O operation switch(nReadWriteType) { case IDC_LOCK_FILE: // Perform the lock operation using LockFile if(!LockFile(pData->hFile, ByteOffset.LowPart, ByteOffset.HighPart, LockLength.LowPart, LockLength.HighPart)) nError = GetLastError(); break; case IDC_UNLOCK_FILE: // Perform the unlock operation using UnlockFile if(!UnlockFile(pData->hFile, ByteOffset.LowPart, ByteOffset.HighPart, LockLength.LowPart, LockLength.HighPart)) nError = GetLastError(); break; case IDC_NTLOCK_FILE: // Remember that this is a native call pApc->bNativeCall = false; // Perform the lock operation using NtLockFile Status = NtLockFile(pData->hFile, pApc->hEvent, NULL, NULL, &pApc->IoStatus, &ByteOffset, &LockLength, 0, TRUE, TRUE); // If the lock operation ended with STATUS_PENDING, queue the APC if(Status == STATUS_PENDING) { SetResultInfo(hDlg, ERROR_IO_PENDING); InsertApcEntry(pData, pApc); return TRUE; } // Save the error nError = RtlNtStatusToDosError(Status); break; case IDC_NTUNLOCK_FILE: // Perform the unlock operation using NtUnlockFile Status = NtUnlockFile(pData->hFile, &pApc->IoStatus, &ByteOffset, &LockLength, 0); // Save the error assert(Status != STATUS_PENDING); nError = RtlNtStatusToDosError(Status); break; } // Set the information about the operation SetResultInfo(hDlg, nError); FreeApcEntry(pApc); } else { SetResultInfo(hDlg, GetLastError()); } return TRUE; }
static int OnReadWriteFile(HWND hDlg, int nIDCtrl) { IO_STATUS_BLOCK IoStatus = {0}; TFileTestData * pData = GetDialogData(hDlg); TApcReadWrite * pApc; LARGE_INTEGER ByteOffset = {0}; NTREADWRITE NtReadWrite; READWRITE ReadWrite; NTSTATUS Status; DWORD dwTransferred = 0; DWORD Length = 0; int nError = ERROR_SUCCESS; // Get the start position DlgText2Hex64(hDlg, IDC_BYTE_OFFSET, &ByteOffset.QuadPart); DlgText2Hex32(hDlg, IDC_LENGTH, &Length); assert(pData->cbFileData >= Length); // Create new APC entry pApc = (TApcReadWrite *)CreateApcEntry(pData, APC_TYPE_READ_WRITE, sizeof(TApcReadWrite)); if(pApc != NULL) { // Remember the state of "Increment position" pApc->ByteOffset = ByteOffset; pApc->bIncrementPosition = (IsDlgButtonChecked(hDlg, IDC_INCREASE_FILEPOS) == BST_CHECKED); // Perform the appropriate API switch(nIDCtrl) { case IDC_READ_FILE: case IDC_WRITE_FILE: // Get the pointer to the appropriate API ReadWrite = (nIDCtrl == IDC_READ_FILE) ? ReadFile : (READWRITE)WriteFile; pApc->bUpdateData = (nIDCtrl == IDC_READ_FILE); pApc->bNativeCall = false; // Prepare the OVERLAPPED structure in the APC pApc->Overlapped.OffsetHigh = ByteOffset.HighPart; pApc->Overlapped.Offset = ByteOffset.LowPart; // Perform the read operation using ReadFile if(!ReadWrite(pData->hFile, pData->pbFileData, Length, &dwTransferred, &pApc->Overlapped)) nError = GetLastError(); // If the read operation ended with ERROR_IO_PENDING, queue the APC if(nError == ERROR_IO_PENDING) { SetResultInfo(hDlg, ERROR_IO_PENDING); InsertApcEntry(pData, pApc); return TRUE; } break; case IDC_NTREAD_FILE: case IDC_NTWRITE_FILE: // Get the pointer to the appropriate API NtReadWrite = (nIDCtrl == IDC_NTREAD_FILE) ? NtReadFile : NtWriteFile; pApc->bUpdateData = (nIDCtrl == IDC_NTREAD_FILE); pApc->bNativeCall = true; // Perform the read/write operation Status = NtReadWrite(pData->hFile, pApc->hEvent, NULL, NULL, &pApc->IoStatus, pData->pbFileData, Length, &ByteOffset, NULL); // If the read operation ended with STATUS_PENDING, queue the APC if(Status == STATUS_PENDING) { SetResultInfo(hDlg, ERROR_IO_PENDING); InsertApcEntry(pData, pApc); return TRUE; } // Fill the number of bytes transferred dwTransferred = (DWORD)IoStatus.Information; nError = RtlNtStatusToDosError(Status); break; } // Complete the read/write operation CompleteReadWriteOperation(hDlg, pApc, nError, dwTransferred); FreeApcEntry(pApc); } else { SetResultInfo(hDlg, GetLastError()); } return TRUE; }
static int UpdateFileData( HWND hDlg, LPCSTR szDataToFill, size_t OffsetToFill, UINT FillPattern) { TFileTestData * pData = GetDialogData(hDlg); LONGLONG ByteOffset; LPBYTE WritePattern = (LPBYTE)"BAADF00D"; ULONG NewLength; ULONG i; size_t cchDataToFill = 0; HWND hWndChild = GetDlgItem(hDlg, IDC_FILE_DATA); // Get the byte offset DlgText2Hex64(hDlg, IDC_BYTE_OFFSET, &ByteOffset); DlgText2Hex32(hDlg, IDC_LENGTH, &NewLength); // Clear the file data from the data editor DataEditor_SetData(hWndChild, 0, NULL, 0); // Determine the new length of the data if(szDataToFill != NULL) { cchDataToFill = strlen(szDataToFill); NewLength = (ULONG)(OffsetToFill + cchDataToFill); } // If we need to increase the buffer size, do it if(NewLength > pData->cbFileDataMax) { pData->pbFileData = (LPBYTE)HeapReAlloc(g_hHeap, HEAP_ZERO_MEMORY, pData->pbFileData, NewLength); if(pData->pbFileData == NULL) return ERROR_NOT_ENOUGH_MEMORY; pData->cbFileDataMax = NewLength; } // If we shall fill the data with custom data, do it if(szDataToFill != NULL) { // Fill the gap after end of current data with zeros if(OffsetToFill > pData->cbFileData) memset(pData->pbFileData + pData->cbFileData, 0, OffsetToFill - pData->cbFileData); memcpy(pData->pbFileData + OffsetToFill, szDataToFill, cchDataToFill); } // If the caller required us to fill the data with pattern, do it else { // If the new pattern is zero, it means to fill the same pattern like before if(FillPattern == 0) FillPattern = pData->FillPattern; // If the pattern is the same like before, just fill the remaining part if(OffsetToFill < NewLength) { switch(FillPattern) { case IDC_FILL_DATA_ZEROS: memset(pData->pbFileData + OffsetToFill, 0, NewLength - OffsetToFill); break; case IDC_FILL_DATA_PATTERN: for(i = (ULONG)OffsetToFill; i < NewLength; i++) pData->pbFileData[i] = WritePattern[i % 8]; break; case IDC_FILL_DATA_RANDOM: srand(GetTickCount()); for(i = (ULONG)OffsetToFill; i < NewLength; i++) pData->pbFileData[i] = (BYTE)(rand() % 0x100); break; } } // Remember the current pattern pData->FillPattern = FillPattern; } // Remember the new data length if(NewLength != pData->cbFileData) { Hex2DlgText32(hDlg, IDC_LENGTH, NewLength); pData->cbFileData = NewLength; } // Apply the new file data DataEditor_SetData(hWndChild, ByteOffset, pData->pbFileData, pData->cbFileData); return ERROR_SUCCESS; }