// Converts from Ascii or Hex to Decimal integer int Strtoi(const char * szStr) { UINT retVal = 0; bool fNegative=false; if( szStr && *szStr) { char ch; if(*szStr=='-') { fNegative=true; szStr++; } else if(*szStr == '+') szStr++; // Check if it is a hex number if( ('0' == szStr[0]) && (('X' == szStr[1]) || ('x' == szStr[1])) ) { szStr+=2; while( ch = *szStr++) { if(ch >= '0' && ch <='9' ) ch-='0'; else if( ch >= 'A' && ch<='F' ) ch=ch-'A' + 10; else if( ch >= 'a' && ch<='f') ch=ch-'a'+10; else return 0; //invalid string if (FAILED (UIntMult(retVal, 16, &retVal))) return 0; if (FAILED (UIntAdd(retVal, (UINT)ch, &retVal))) return 0; } } // Otherwise its an int else { while( ch = *szStr++) { if(ch >= '0' && ch <='9' ) ch-='0'; else return 0; //invalid string if (FAILED (UIntMult(retVal, 10, &retVal))) return 0; if (FAILED (UIntAdd(retVal, (UINT)ch, &retVal))) return 0; } } } Assert (INT_MAX > retVal); return ( fNegative ? -((int)retVal) : (int)retVal ); }
BOOL HashTbl::Init(uint cidHash) { // cidHash must be a power of two Assert(cidHash > 0 && 0 == (cidHash & (cidHash - 1))); long cb; /* Allocate and clear the hash bucket table */ m_luMask = cidHash - 1; m_luCount = 0; // (Bug 1117873 - Windows OS Bugs) // Prefast: Verify that cidHash * sizeof(Ident *) does not cause an integer overflow // NoReleaseAllocator( ) takes long - so check for LONG_MAX // Win8 730594 - Use intsafe function to check for overflow. uint cbTemp; if (FAILED(UIntMult(cidHash, sizeof(Ident *), &cbTemp)) || cbTemp > LONG_MAX) return FALSE; cb = cbTemp; if (nullptr == (m_prgpidName = (Ident **)m_noReleaseAllocator.Alloc(cb))) return FALSE; memset(m_prgpidName, 0, cb); #if PROFILE_DICTIONARY stats = DictionaryStats::Create(typeid(this).name(), cidHash); #endif return TRUE; }
void HashTbl::Grow() { // Grow the bucket size by grow factor // Has the side-effect of inverting the order the pids appear in their respective buckets. uint cidHash = m_luMask + 1; uint n_cidHash = cidHash * GrowFactor; Assert(n_cidHash > 0 && 0 == (n_cidHash & (n_cidHash - 1))); // Win8 730594 - Use intsafe function to check for overflow. uint cbTemp; if (FAILED(UIntMult(n_cidHash, sizeof(Ident *), &cbTemp)) || cbTemp > LONG_MAX) // It is fine to exit early here, we will just have a potentially densely populated hash table return; long cb = cbTemp; uint n_luMask = n_cidHash - 1; IdentPtr *n_prgpidName = (IdentPtr *)m_noReleaseAllocator.Alloc(cb); if (n_prgpidName == nullptr) // It is fine to exit early here, we will just have a potentially densely populated hash table return; // Clear the array memset(n_prgpidName, 0, cb); // Place each entry its new bucket. for (uint i = 0; i < cidHash; i++) { for (IdentPtr pid = m_prgpidName[i], next = pid ? pid->m_pidNext : nullptr; pid; pid = next, next = pid ? pid->m_pidNext : nullptr) { ulong luHash = pid->m_luHash; ulong luIndex = luHash & n_luMask; pid->m_pidNext = n_prgpidName[luIndex]; n_prgpidName[luIndex] = pid; } } Assert(CountAndVerifyItems(n_prgpidName, n_cidHash, n_luMask) == m_luCount); // Update the table fields. m_prgpidName = n_prgpidName; m_luMask= n_luMask; #if PROFILE_DICTIONARY if(stats) { int emptyBuckets = 0; for (uint i = 0; i < n_cidHash; i++) { if(m_prgpidName[i] == nullptr) { emptyBuckets++; } } stats->Resize(n_cidHash, emptyBuckets); } #endif }
/****************************************************************** * Creates a DIB Section from the converted IWICBitmapSource * ******************************************************************/ HRESULT UserImageFactory_CreateDIBSectionFromBitmapSource( IWICBitmapSource *pToRenderBitmapSource, HBITMAP& hBitmapInOut ) { HRESULT hr = S_OK; UINT nWidth = 0; UINT nHeight = 0; void *pvImageBits = NULL; BITMAPINFO bminfo; HWND hWindow = NULL; HDC hdcScreen = NULL; WICPixelFormatGUID pixelFormat; UINT cbStride = 0; UINT cbImage = 0; // Check BitmapSource format //hr = IWICBitmapSource_GetPixelFormat( pToRenderBitmapSource, &pixelFormat ); hr = pToRenderBitmapSource->GetPixelFormat( &pixelFormat ); if( SUCCEEDED( hr ) ) { //hr = IWICBitmapSource_GetSize( pToRenderBitmapSource, &nWidth, &nHeight ); hr = pToRenderBitmapSource->GetSize( &nWidth, &nHeight ); } // Create a DIB section based on Bitmap Info // BITMAPINFO Struct must first be setup before a DIB can be created. // Note that the height is negative for top-down bitmaps if( SUCCEEDED( hr ) ) { ZeroMemory( &bminfo, sizeof( bminfo ) ); bminfo.bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); bminfo.bmiHeader.biWidth = nWidth; bminfo.bmiHeader.biHeight = -(LONG)nHeight; bminfo.bmiHeader.biPlanes = 1; bminfo.bmiHeader.biBitCount = 32; bminfo.bmiHeader.biCompression = BI_RGB; hWindow = GetActiveWindow(); while( GetParent( hWindow ) != NULL ) hWindow = GetParent( hWindow ); // Get a DC for the full screen hdcScreen = GetDC( hWindow ); hr = hdcScreen ? S_OK : E_FAIL; // Release the previously allocated bitmap if( SUCCEEDED( hr ) ) { if( hBitmapInOut ) { DeleteObject( hBitmapInOut ); } // TBD: check this. As a handle this should just be as-is, right? hBitmapInOut = CreateDIBSection( hdcScreen, &bminfo, DIB_RGB_COLORS, &pvImageBits, NULL, 0 ); ReleaseDC( NULL, hdcScreen ); hr = hBitmapInOut ? S_OK : E_FAIL; } } if( SUCCEEDED( hr ) ) { // Size of a scan line represented in bytes: 4 bytes each pixel hr = UIntMult( nWidth, sizeof( ARGB ), &cbStride ); } if( SUCCEEDED( hr ) ) { // Size of the image, represented in bytes hr = UIntMult( cbStride, nHeight, &cbImage ); } // Extract the image into the HBITMAP if( SUCCEEDED( hr ) ) { hr = pToRenderBitmapSource->CopyPixels( //hr = IWICBitmapSource_CopyPixels( pToRenderBitmapSource, NULL, cbStride, cbImage, (BYTE*)pvImageBits ); } // Image Extraction failed, clear allocated memory if( FAILED( hr ) ) { DeleteObject( hBitmapInOut ); hBitmapInOut = NULL; } return hr; }
HRESULT touchmind::util::BitmapHelper::CreateBitmapFromWICBitmapSource(IN IWICBitmapSource *pBitmapSource, OUT HBITMAP *pHBitmap) { HRESULT hr = S_OK; UINT width = 0; UINT height = 0; void *pvImageBits = nullptr; WICPixelFormatGUID pixelFormat = {0}; hr = pBitmapSource->GetPixelFormat(&pixelFormat); if (SUCCEEDED(hr)) { hr = (pixelFormat == GUID_WICPixelFormat32bppPBGRA) ? S_OK : E_FAIL; } if (SUCCEEDED(hr)) { hr = pBitmapSource->GetSize(&width, &height); } if (SUCCEEDED(hr)) { BITMAPINFO bminfo; ZeroMemory(&bminfo, sizeof(bminfo)); bminfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bminfo.bmiHeader.biWidth = width; bminfo.bmiHeader.biHeight = -(LONG)height; bminfo.bmiHeader.biPlanes = 1; bminfo.bmiHeader.biBitCount = 32; bminfo.bmiHeader.biCompression = BI_RGB; HDC hdcScreen = GetDC(nullptr); hr = hdcScreen ? S_OK : E_FAIL; if (SUCCEEDED(hr)) { if (pHBitmap) { DeleteObject(pHBitmap); } *pHBitmap = CreateDIBSection(hdcScreen, &bminfo, DIB_RGB_COLORS, &pvImageBits, nullptr, 0); ReleaseDC(nullptr, hdcScreen); hr = *pHBitmap ? S_OK : E_FAIL; } } UINT cbStride = 0; if (SUCCEEDED(hr)) { hr = UIntMult(width, sizeof(ARGB), &cbStride); } UINT cbImage = 0; if (SUCCEEDED(hr)) { hr = UIntMult(cbStride, height, &cbImage); } if (SUCCEEDED(hr)) { hr = pBitmapSource->CopyPixels(nullptr, cbStride, cbImage, reinterpret_cast<BYTE *>(pvImageBits)); } if (FAILED(hr)) { DeleteObject(*pHBitmap); *pHBitmap = nullptr; } return hr; }