void * __cdecl _expand_base (void * pBlock, size_t newsize) { void * pvReturn; /* validation section */ _VALIDATE_RETURN(pBlock != NULL, EINVAL, NULL); if (newsize > _HEAP_MAXREQ) { errno = ENOMEM; return NULL; } #ifndef _WIN64 if ( __active_heap == __V6_HEAP ) { PHEADER pHeader; _mlock( _HEAP_LOCK ); __try { // if allocation block lies within the small-block heap, // try to resize it there if ((pHeader = __sbh_find_block(pBlock)) != NULL) { pvReturn = NULL; if ( (newsize <= __sbh_threshold) && __sbh_resize_block(pHeader, pBlock, (int)newsize) ) pvReturn = pBlock; } } __finally { _munlock( _HEAP_LOCK ); } if ( pHeader == NULL ) { // force nonzero size and round up to next paragraph if (newsize == 0) newsize = 1; newsize = (newsize + BYTES_PER_PARA - 1) & ~(BYTES_PER_PARA - 1); pvReturn = HeapReAlloc(_crtheap, HEAP_REALLOC_IN_PLACE_ONLY, pBlock, newsize); if (pvReturn == NULL) { errno = _get_errno_from_oserr(GetLastError()); } } }
unsigned __cdecl _getdiskfree ( unsigned uDrive, struct _diskfree_t * pdf ) { char Root[4]; char * pRoot; _VALIDATE_RETURN((pdf != NULL), EINVAL, ERROR_INVALID_PARAMETER); _VALIDATE_RETURN(( uDrive <= 26 ), EINVAL, ERROR_INVALID_PARAMETER); memset( pdf, 0, sizeof( struct _diskfree_t ) ); if ( uDrive == 0 ) { pRoot = NULL; } else { pRoot = &Root[0]; Root[0] = (char)uDrive + (char)('A' - 1); Root[1] = ':'; Root[2] = '\\'; Root[3] = '\0'; } if ( !GetDiskFreeSpace( pRoot, (LPDWORD)&(pdf->sectors_per_cluster), (LPDWORD)&(pdf->bytes_per_sector), (LPDWORD)&(pdf->avail_clusters), (LPDWORD)&(pdf->total_clusters)) ) { int err = GetLastError(); errno = _get_errno_from_oserr(err); return ( err ); } return ( 0 ); }
errno_t __cdecl rand_s ( unsigned int *_RandomValue ) { PGENRANDOM pfnRtlGenRandom = (PGENRANDOM) DecodePointer(g_pfnRtlGenRandom); _VALIDATE_RETURN_ERRCODE( _RandomValue != NULL, EINVAL ); *_RandomValue = 0; // Review : better value to initialize it to? if ( pfnRtlGenRandom == NULL ) { PGENRANDOM encoded; void* enull; #ifdef _CORESYS #define RAND_DLL L"cryptbase.dll" #else /* _CORESYS */ #define RAND_DLL L"ADVAPI32.DLL" #endif /* _CORESYS */ // advapi32.dll/cryptbase.dll is unloaded when the App exits. HMODULE hRandDll = LoadLibraryExW(RAND_DLL, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); #ifndef _CORESYS if (!hRandDll && GetLastError() == ERROR_INVALID_PARAMETER) { // LOAD_LIBRARY_SEARCH_SYSTEM32 is not supported on this platfrom, // try one more time using default options hRandDll = LoadLibraryExW(RAND_DLL, NULL, 0); } #endif /* _CORESYS */ if (!hRandDll) { _VALIDATE_RETURN_ERRCODE(("rand_s is not available on this platform", 0), EINVAL); } pfnRtlGenRandom = ( PGENRANDOM ) GetProcAddress( hRandDll, _TO_STR( RtlGenRandom ) ); if ( pfnRtlGenRandom == NULL ) { _VALIDATE_RETURN_ERRCODE(("rand_s is not available on this platform", 0), _get_errno_from_oserr(GetLastError())); } encoded = (PGENRANDOM) EncodePointer(pfnRtlGenRandom); enull = EncodePointer(NULL); #ifdef _M_IX86 if ( (void*)(LONG_PTR)InterlockedExchange( ( LONG* )&g_pfnRtlGenRandom, ( LONG )( LONG_PTR )encoded) != enull ) #else /* _M_IX86 */ if ( InterlockedExchangePointer( ( void** )&g_pfnRtlGenRandom, ( void* )encoded) != enull ) #endif /* _M_IX86 */ { /* A different thread has already loaded advapi32.dll/cryptbase.dll. */ FreeLibrary( hRandDll ); } } if ( !(*pfnRtlGenRandom)( _RandomValue, ( ULONG )sizeof( unsigned int ) ) ) { errno = ENOMEM; return errno; } return 0; }
_CRTIMP void __cdecl _dosmaperr( unsigned long oserrno ) { _doserrno = oserrno; /* set _doserrno */ errno = _get_errno_from_oserr(oserrno); }
errno_t __cdecl rand_s ( unsigned int* _RandomValue ) { PGENRANDOM pfnRtlGenRandom = _decode_pointer(g_pfnRtlGenRandom); _VALIDATE_RETURN_ERRCODE(_RandomValue != NULL, EINVAL); *_RandomValue = 0; // Review : better value to initialize it to? if (pfnRtlGenRandom == NULL) { PGENRANDOM encoded; void* enull; // Advapi32.dll is unloaded when the App exits. HMODULE hAdvApi32 = LoadLibrary("ADVAPI32.DLL"); if (!hAdvApi32) { _VALIDATE_RETURN_ERRCODE(("rand_s is not available on this platform", 0), EINVAL); } pfnRtlGenRandom = (PGENRANDOM) GetProcAddress(hAdvApi32, _TO_STR(RtlGenRandom)); if (pfnRtlGenRandom == NULL) { _VALIDATE_RETURN_ERRCODE(("rand_s is not available on this platform", 0), _get_errno_from_oserr(GetLastError())); } encoded = (PGENRANDOM) _encode_pointer(pfnRtlGenRandom); enull = _encoded_null(); #ifdef _M_IX86 if ((void*)(LONG_PTR)InterlockedExchange( (LONG*)&g_pfnRtlGenRandom, (LONG)(LONG_PTR)encoded) != enull) #else /* _M_IX86 */ if (InterlockedExchangePointer( (void**)&g_pfnRtlGenRandom, (void*)encoded) != enull) #endif /* _M_IX86 */ { /* A different thread has already loaded advapi32.dll. */ FreeLibrary(hAdvApi32); } } if (!(*pfnRtlGenRandom)(_RandomValue, (ULONG)sizeof(unsigned int))) { errno = ENOMEM; return errno; } return 0; }