/* DwordArray.cpp */ #include "StdAfx.h" #include #include "DwordArray.h" CDwordArray::CDwordArray(void) { m_Address = AllocAddress = NULL; m_RecordCount = sztAllocSize = 0; m_Status = 0; } CDwordArray::~CDwordArray(void) { Release(); } void CDwordArray::Release(void) { if( AllocAddress ) { VirtualFree( AllocAddress, sztAllocSize, MEM_DECOMMIT ); VirtualFree( AllocAddress, 0, MEM_RELEASE ); } m_Address = AllocAddress = NULL; m_RecordCount = 0; sztAllocSize = 0; m_Status = 0; } DWORD CDwordArray::SetData(DWORD pDataType, size_t pRecordCount) { size_t sztPtr; DWORD dwResult; Release(); sztAllocSize = sizeof( SortRecStruct ) * pRecordCount; m_Address = AllocAddress = ( SortRecStruct* ) VirtualAlloc( NULL, sztAllocSize, MEM_COMMIT, PAGE_READWRITE ); if( AllocAddress == NULL ) dwResult = GetLastError(); else { switch( pDataType ) { case 0: // Random32 SetRandom32( pRecordCount ); break; case 1: // Random32R SetRandom32R( pRecordCount ); break; case 2: // Random15 for( dwRndValue = 1, sztPtr = 0; sztPtr < pRecordCount; sztPtr++ ) { AllocAddress[ sztPtr ].Key = RndShortMax(); *( ( size_t* ) &AllocAddress[ sztPtr ].Value ) = ( sztPtr + 1 ); } break; case 3: // Random15R for( dwRndValue = 1, sztPtr = 0; sztPtr < pRecordCount; sztPtr++ ) { AllocAddress[ pRecordCount - sztPtr - 1 ].Key = RndShortMax(); *( ( size_t* ) &AllocAddress[ sztPtr ].Value ) = ( sztPtr + 1 ); } break; case 4: // Forward for( sztPtr = 0; sztPtr < pRecordCount; sztPtr++ ) { AllocAddress[ sztPtr ].Key = ( DWORD ) ( sztPtr + 1 ); *( ( size_t* ) &AllocAddress[ sztPtr ].Value ) = ( sztPtr + 1 ); } break; case 5: // Reverse for( sztPtr = 0; sztPtr < pRecordCount; sztPtr++ ) { AllocAddress[ sztPtr ].Key = ( DWORD ) ( pRecordCount - sztPtr ); *( ( size_t* ) &AllocAddress[ sztPtr ].Value ) = ( sztPtr + 1 ); } break; case 6: // Constant for( sztPtr = 0; sztPtr < pRecordCount; sztPtr++ ) { AllocAddress[ sztPtr ].Key = 9625; *( ( size_t* ) &AllocAddress[ sztPtr ].Value ) = ( sztPtr + 1 ); } break; case 7: // Med Killer for( sztPtr = 0; sztPtr < pRecordCount / 2; ++sztPtr) { AllocAddress[ sztPtr ].Key = ( DWORD ) sztPtr; } for( sztPtr = pRecordCount / 2; sztPtr < pRecordCount; ++sztPtr ) { AllocAddress[ sztPtr ].Key = ( DWORD ) ( pRecordCount - sztPtr ); } break; } m_RecordCount = pRecordCount; dwResult = 0; } return dwResult; } DWORD CDwordArray::RndShortMax(void) { dwRndValue = dwRndValue * 214013 + 2531011; return ( dwRndValue >> 16 ) & 0x7fff;; } void CDwordArray::GetBinPath(TCHAR* pPath) { SHGetSpecialFolderPath( NULL, pPath, CSIDL_COMMON_APPDATA, FALSE ); lstrcat( pPath, TEXT( "\\RandData.bin" ) ); } void CDwordArray::SetRandom32(size_t pRecCount) { TCHAR tcBin[ MAX_PATH ]; size_t sztPtr, sztAllocSize2; HANDLE fBin; DWORD *dwBin, dwCount; GetBinPath( tcBin ); sztAllocSize2 = sizeof( DWORD ) * pRecCount; dwBin = ( DWORD* ) VirtualAlloc( NULL, sztAllocSize2, MEM_COMMIT, PAGE_READWRITE ); if( dwBin == NULL ) { m_Status = GetLastError(); return; } fBin = CreateFile( tcBin, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if( fBin == INVALID_HANDLE_VALUE ) { m_Status = GetLastError(); return; } ReadFile( fBin, dwBin, ( DWORD ) sztAllocSize2, &dwCount, NULL ); CloseHandle( fBin ); for( sztPtr = 0; sztPtr < pRecCount; sztPtr++ ) { AllocAddress[ sztPtr ].Key = dwBin[ sztPtr ]; *( ( size_t* ) &AllocAddress[ sztPtr ].Value ) = ( sztPtr + 1 ); } VirtualFree( dwBin, sztAllocSize2, MEM_DECOMMIT ); VirtualFree( dwBin, 0, MEM_RELEASE ); } void CDwordArray::SetRandom32R(size_t pRecCount) { TCHAR tcBin[ MAX_PATH ]; size_t sztPtr, sztAllocSize2; HANDLE fBin; DWORD *dwBin, dwCount; GetBinPath( tcBin ); sztAllocSize2 = sizeof( DWORD ) * pRecCount; dwBin = ( DWORD* ) VirtualAlloc( NULL, sztAllocSize2, MEM_COMMIT, PAGE_READWRITE ); if( dwBin == NULL ) { m_Status = GetLastError(); return; } fBin = CreateFile( tcBin, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if( fBin == INVALID_HANDLE_VALUE ) { m_Status = GetLastError(); return; } ReadFile( fBin, dwBin, ( DWORD ) sztAllocSize2, &dwCount, NULL ); CloseHandle( fBin ); for( sztPtr = 0; sztPtr < pRecCount; sztPtr++ ) { AllocAddress[ pRecCount - sztPtr - 1 ].Key = dwBin[ sztPtr ]; *( ( size_t* ) &AllocAddress[ sztPtr ].Value ) = ( sztPtr + 1 ); } VirtualFree( dwBin, sztAllocSize2, MEM_DECOMMIT ); VirtualFree( dwBin, 0, MEM_RELEASE ); }