RBOOL addHitToNode ( PObsNode node, PObsSig sig ) { RBOOL isSuccess = FALSE; PObsSig* tmp = NULL; RU32 numHits = 0; if( rpal_memory_isValid( node ) && rpal_memory_isValid( sig ) ) { tmp = node->pSigsHit; if( NULL != tmp ) { while( NULL != *tmp ) { numHits++; tmp++; } } node->pSigsHit = rpal_memory_realloc( node->pSigsHit, ( ( numHits + 2 ) * sizeof( PObsSig ) ) ); tmp = node->pSigsHit; if( NULL != tmp ) { rpal_memory_zero( (RPU8)tmp + ( numHits * sizeof( PObsSig ) ), 2 * sizeof( PObsSig ) ); while( NULL != *tmp ) { tmp++; } *tmp = sig; isSuccess = TRUE; } } return isSuccess; }
static RBOOL getSnapshot ( processEntry* toSnapshot ) { RBOOL isSuccess = FALSE; RU32 i = 0; if( NULL != toSnapshot ) { rpal_memory_zero( toSnapshot, sizeof( g_snapshot_1 ) ); } if( NULL != toSnapshot ) { #ifdef RPAL_PLATFORM_WINDOWS HANDLE hSnapshot = NULL; PROCESSENTRY32W procEntry = { 0 }; procEntry.dwSize = sizeof( procEntry ); if( INVALID_HANDLE_VALUE != ( hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ) ) { if( Process32FirstW( hSnapshot, &procEntry ) ) { isSuccess = TRUE; do { if( 0 == procEntry.th32ProcessID ) { continue; } toSnapshot[ i ].pid = procEntry.th32ProcessID; toSnapshot[ i ].ppid = procEntry.th32ParentProcessID; i++; } while( Process32NextW( hSnapshot, &procEntry ) && MAX_SNAPSHOT_SIZE > i ); } CloseHandle( hSnapshot ); } #elif defined( RPAL_PLATFORM_LINUX ) RWCHAR procDir[] = _WCH( "/proc/" ); rDir hProcDir = NULL; rFileInfo finfo = {0}; if( rDir_open( (RPWCHAR)&procDir, &hProcDir ) ) { isSuccess = TRUE; while( rDir_next( hProcDir, &finfo ) && MAX_SNAPSHOT_SIZE > i ) { if( rpal_string_wtoi( (RPWCHAR)finfo.fileName, &( toSnapshot[ i ].pid ) ) && 0 != toSnapshot[ i ].pid ) { i++; } } rDir_close( hProcDir ); } #elif defined( RPAL_PLATFORM_MACOSX ) int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL }; struct kinfo_proc* infos = NULL; size_t size = 0; int ret = 0; int j = 0; if( 0 == ( ret = sysctl( mib, ARRAY_N_ELEM( mib ), infos, &size, NULL, 0 ) ) ) { if( NULL != ( infos = rpal_memory_alloc( size ) ) ) { while( 0 != ( ret = sysctl( mib, ARRAY_N_ELEM( mib ), infos, &size, NULL, 0 ) ) && ENOMEM == errno ) { if( NULL == ( infos = rpal_memory_realloc( infos, size ) ) ) { break; } } } } if( 0 == ret && NULL != infos ) { isSuccess = TRUE; size = size / sizeof( struct kinfo_proc ); for( j = 0; j < size; j++ ) { toSnapshot[ i ].pid = infos[ j ].kp_proc.p_pid; toSnapshot[ i ].ppid = infos[ j ].kp_eproc.e_ppid; i++; } if( NULL != infos ) { rpal_memory_free( infos ); infos = NULL; } } #endif } return isSuccess; }
static RBOOL postHttp ( RPCHAR location, RPCHAR params, RBOOL isEnforceCert, RPVOID* receivedData, RU32* receivedSize ) { RBOOL isSuccess = FALSE; DWORD timeout = ( 1000 * 10 ); DWORD bytesToRead = 0; DWORD bytesReceived = 0; DWORD bytesRead = 0; RPU8 pReceivedBuffer = NULL; URL_COMPONENTSA components = {0}; RBOOL isSecure = FALSE; RCHAR pUser[ 1024 ] = {0}; RCHAR pPass[ 1024 ] = {0}; RCHAR pUrl[ 1024 ] = {0}; RCHAR pPage[ 1024 ] = {0}; INTERNET_PORT port = 0; InternetCrackUrl_f pInternetCrackUrl = NULL; InternetOpen_f pInternetOpen = NULL; InternetConnect_f pInternetConnect = NULL; InternetCloseHandle_f pInternetCloseHandle = NULL; HttpOpenRequest_f pHttpOpenRequest = NULL; HttpSendRequest_f pHttpSendRequest = NULL; InternetQueryDataAvailable_f pInternetQueryDataAvailable = NULL; InternetReadFile_f pInternetReadFile = NULL; InternetSetOption_f pInternetSetOption = NULL; HMODULE hWininet = NULL; HINTERNET hInternet = NULL; HINTERNET hConnect = NULL; HINTERNET hHttp = NULL; DWORD flags = INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_NO_UI | INTERNET_FLAG_NO_CACHE_WRITE; RCHAR contentType[] = "Content-Type: application/x-www-form-urlencoded"; RCHAR userAgent[] = "rpHCP"; RCHAR method[] = "POST"; RCHAR importDll[] = "wininet.dll"; RCHAR import1[] = "InternetCrackUrlA"; RCHAR import2[] = "InternetOpenA"; RCHAR import3[] = "InternetConnectA"; RCHAR import4[] = "InternetCloseHandle"; RCHAR import5[] = "HttpOpenRequestA"; RCHAR import6[] = "HttpSendRequestA"; RCHAR import7[] = "InternetQueryDataAvailable"; RCHAR import8[] = "InternetReadFile"; RCHAR import9[] = "InternetSetOptionA"; if( NULL != location ) { rpal_debug_info( "posting to %s", location ); if( NULL != ( hWininet = GetModuleHandleA( (RPCHAR)&importDll ) ) || NULL != ( hWininet = LoadLibraryA( (RPCHAR)&importDll ) ) ) { pInternetCrackUrl = (InternetCrackUrl_f)GetProcAddress( hWininet, (RPCHAR)&import1 ); pInternetOpen = (InternetOpen_f)GetProcAddress( hWininet, (RPCHAR)&import2 ); pInternetConnect = (InternetConnect_f)GetProcAddress( hWininet, (RPCHAR)&import3 ); pInternetCloseHandle = (InternetCloseHandle_f)GetProcAddress( hWininet, (RPCHAR)&import4 ); pHttpOpenRequest = (HttpOpenRequest_f)GetProcAddress( hWininet, (RPCHAR)&import5 ); pHttpSendRequest = (HttpSendRequest_f)GetProcAddress( hWininet, (RPCHAR)&import6 ); pInternetQueryDataAvailable = (InternetQueryDataAvailable_f)GetProcAddress( hWininet, (RPCHAR)&import7 ); pInternetReadFile = (InternetReadFile_f)GetProcAddress( hWininet, (RPCHAR)&import8 ); pInternetSetOption = (InternetSetOption_f)GetProcAddress( hWininet, (RPCHAR)&import9 ); if( NULL != pInternetCrackUrl && NULL != pInternetOpen && NULL != pInternetConnect && NULL != pInternetCloseHandle && NULL != pHttpOpenRequest && NULL != pHttpSendRequest && NULL != pInternetQueryDataAvailable && NULL != pInternetReadFile && NULL != pInternetSetOption ) { components.lpszHostName = pUrl; components.dwHostNameLength = sizeof( pUrl ); components.lpszUrlPath = pPage; components.dwUrlPathLength = sizeof( pPage ); components.lpszUserName = pUser; components.dwUserNameLength = sizeof( pUser ); components.lpszPassword = pPass; components.dwPasswordLength = sizeof( pPass ); components.dwStructSize = sizeof( components ); if( !pInternetCrackUrl( location, 0, 0, &components ) ) { if( rpal_string_strlen( location ) < ARRAY_N_ELEM( pUrl ) ) { rpal_string_strcpya( pUrl, location ); } components.nPort = 80; components.nScheme = INTERNET_SCHEME_HTTP; } port = components.nPort; if( INTERNET_SCHEME_HTTPS == components.nScheme ) { isSecure = TRUE; } if( !isEnforceCert && isSecure ) { flags |= INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID; } if( isSecure ) { flags |= INTERNET_FLAG_SECURE; } hInternet = pInternetOpen( (RPCHAR)&userAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 ); if( NULL != hInternet ) { pInternetSetOption( hInternet, INTERNET_OPTION_CONNECT_TIMEOUT, &timeout, sizeof( timeout ) ); hConnect = pInternetConnect( hInternet, pUrl, port, pUser, pPass, INTERNET_SERVICE_HTTP, flags, (DWORD_PTR)NULL ); if( NULL != hConnect ) { hHttp = pHttpOpenRequest( hConnect, (RPCHAR)&method, pPage ? pPage : "", NULL, NULL, NULL, flags, (DWORD_PTR)NULL ); if( hHttp ) { if( pHttpSendRequest( hHttp, (RPCHAR)&contentType, (DWORD)(-1), params, (DWORD)rpal_string_strlen( params ) ) ) { isSuccess = TRUE; if( NULL != receivedData && NULL != receivedSize ) { while( pInternetQueryDataAvailable( hHttp, &bytesToRead, 0, 0 ) && 0 != bytesToRead ) { pReceivedBuffer = rpal_memory_realloc( pReceivedBuffer, bytesReceived + bytesToRead ); if( rpal_memory_isValid( pReceivedBuffer ) ) { if( pInternetReadFile( hHttp, pReceivedBuffer + bytesReceived, bytesToRead, &bytesRead ) ) { bytesReceived += bytesRead; bytesToRead = 0; bytesRead = 0; } else { rpal_memory_free( pReceivedBuffer ); pReceivedBuffer = NULL; bytesReceived = 0; break; } } else { pReceivedBuffer = NULL; bytesReceived = 0; isSuccess = FALSE; break; } } if( isSuccess && rpal_memory_isValid( pReceivedBuffer ) && 0 != bytesReceived ) { *receivedData = pReceivedBuffer; *receivedSize = bytesReceived; } } } pInternetCloseHandle( hHttp ); } pInternetCloseHandle( hConnect ); } pInternetCloseHandle( hInternet ); } } } } return isSuccess; }
PObsNode addTransition ( PObsNode parent, PObsNode node, PObsNode to, RU8 onValue ) { PObsNode retNode = NULL; RU32 currentNodeSize = 0; RU8 numElemToAdd = 0; RU8 indexToInsert = 0; PObsNode originalNode = node; RU32 i = 0; if( rpal_memory_isValid( node ) ) { currentNodeSize = sizeof( ObsNode ) + ( node->nElements * sizeof( RPVOID ) ); if( !IS_IN_RANGE( node, onValue ) ) { if( onValue >= node->startOffset + node->nElements ) { numElemToAdd = onValue - ( node->startOffset + node->nElements ) + 1; } else { numElemToAdd = node->startOffset - onValue; } if( node->nAllocated < node->nElements + numElemToAdd ) { node = rpal_memory_realloc( node, currentNodeSize + ( numElemToAdd * sizeof( RPVOID ) ) ); rpal_memory_zero( node->elements + node->nElements, numElemToAdd * sizeof( RPVOID ) ); node->nAllocated = node->nElements + numElemToAdd; } if( onValue < node->startOffset && 0 < node->nElements ) { rpal_memory_memmove( node->elements + numElemToAdd, node->elements, node->nElements * sizeof( RPVOID ) ); node->startOffset = node->startOffset - (RU8)numElemToAdd; } node->nElements += numElemToAdd; // The above realloc may have changed the pointer so we need // to update it in the parent, if it exists... if( NULL != parent && originalNode != node ) { for( i = 0; i < parent->nElements; i++ ) { if( originalNode == parent->elements[ i ] ) { parent->elements[ i ] = node; } } } } indexToInsert = EFFECTIVE_INDEX( node, onValue ); if( NULL == node->elements[ indexToInsert ] ) { node->elements[ indexToInsert ] = to; retNode = node; } } return retNode; }