HRESULT ScreenshotCapture::GetBase64Data(std::wstring& data) { if (m_pImage == NULL) { // CImage was not initialized. return E_POINTER; } CComPtr<IStream> stream; HRESULT hr = ::CreateStreamOnHGlobal(NULL, TRUE, &stream); if (FAILED(hr)) { LOG(WARN) << "Error creating IStream" << hr; return hr; } hr = m_pImage->Save(stream, Gdiplus::ImageFormatPNG); if (FAILED(hr)) { LOG(WARN) << "Saving image failed" << hr; return hr; } // Get the size of the stream. STATSTG statstg; hr = stream->Stat(&statstg, STATFLAG_DEFAULT); if (FAILED(hr)) { LOG(WARN) << "No stat on stream" << hr; return hr; } HGLOBAL hGlobal = NULL; hr = ::GetHGlobalFromStream(stream, &hGlobal); if (FAILED(hr)) { LOG(WARN) << "No HGlobal in stream" << hr; return hr; } // TODO: What if the file is bigger than max_int? LOG(INFO) << "Size of stream: " << statstg.cbSize.QuadPart; int length = Base64EncodeGetRequiredLength( statstg.cbSize.QuadPart, ATL_BASE64_FLAG_NOCRLF); if (length <= 0) { LOG(WARN) << "Got zero or negative length from base64 required length"; return E_FAIL; } char *array = new char[length + 1]; if (!::Base64Encode( reinterpret_cast<BYTE*>(::GlobalLock(hGlobal)), statstg.cbSize.QuadPart, array, &length, ATL_BASE64_FLAG_NOCRLF)) { delete array; ::GlobalUnlock(hGlobal); LOG(WARN) << "Failure encoding to base64"; return E_FAIL; } array[length] = '\0'; data = CA2W(array); delete array; ::GlobalUnlock(hGlobal); return S_OK; }
/// <summary> /// base64 encodes a string /// </summary> /// <param name="bytes">String to be encoded</param> /// <param name="byteLength">Length of the string in bytes</param> /// <see cref="http://weblogs.asp.net/kennykerr/visual-c-in-short-encoding-and-decoding-with-base64"/> /// <returns>base64 encoded string</returns> CStringA Uploader::ToBase64(const void* bytes, int byteLength) { ASSERT(0 != bytes); CStringA base64; int base64Length = Base64EncodeGetRequiredLength(byteLength); VERIFY(Base64Encode(static_cast<const BYTE*>(bytes), byteLength, base64.GetBufferSetLength(base64Length), &base64Length)); base64.ReleaseBufferSetLength(base64Length); // URLEncode escape sequences, we'll need this. base64.Replace("\r\n", ""); // Also remove carriage returns. base64.Replace("+", "%2B"); base64.Replace("/", "%2F"); base64.Replace("=", "%3D"); return base64; }
///////////////////////////////////////////////////////////////////// // // Function: // // Description: // ///////////////////////////////////////////////////////////////////// UINT CACreateClientAuthFile::OnExecution() { tstring strDataDirectory; tstring strEnableProtectedApplicationExecution; tstring strBOINCProjectAccountUsername; tstring strBOINCProjectAccountPassword; tstring strClientAuthFile; tstring strVersionNT; struct _stat buf; TCHAR szMessage[2048]; UINT uiReturnValue; uiReturnValue = GetProperty( _T("DATADIR"), strDataDirectory ); if ( uiReturnValue ) return uiReturnValue; uiReturnValue = GetProperty( _T("ENABLEPROTECTEDAPPLICATIONEXECUTION3"), strEnableProtectedApplicationExecution ); if ( uiReturnValue ) return uiReturnValue; uiReturnValue = GetProperty( _T("BOINC_PROJECT_ISUSERNAME"), strBOINCProjectAccountUsername ); if ( uiReturnValue ) return uiReturnValue; uiReturnValue = GetProperty( _T("BOINC_PROJECT_PASSWORD"), strBOINCProjectAccountPassword ); if ( uiReturnValue ) return uiReturnValue; uiReturnValue = GetProperty( _T("VersionNT"), strVersionNT ); if ( uiReturnValue ) return uiReturnValue; // The client_auth.xml file is stored in the data directory. // strClientAuthFile = strDataDirectory + _T("\\client_auth.xml"); // If we are not installing in protected mode, there may not // be a valid 'boinc_project' account, so delete the // client_auth.xml file if it exists. // // NOTE: Windows 2000 or older requires the SeTcbPrivilege // user right, which makes the account the equiv of an // administrator on the system. Disable the use of // 'boinc_project' on Windows 2000 or older // if ((_T("1") != strEnableProtectedApplicationExecution) || _T("500") >= strVersionNT) { if (0 == _tstat(strClientAuthFile.c_str(), &buf)) { if (DeleteFile(strClientAuthFile.c_str())) { LogMessage( INSTALLMESSAGE_INFO, NULL, NULL, NULL, NULL, _T("The client_auth.xml file was successfully deleted.") ); } else { LogMessage( INSTALLMESSAGE_FATALEXIT, NULL, NULL, NULL, NULL, _T("The client_auth.xml could not be deleted from the data direvtory. ") _T("Please delete the file and rerun setup.") ); return ERROR_INSTALL_FAILURE; } } } else { // We are installing in protected mode, which means the 'boinc_project' // account password has been changed, so we need to write out the new // username and password to the client_auth.xml file. DWORD dwSize = Base64EncodeGetRequiredLength((int)strBOINCProjectAccountPassword.length()); LPSTR szBuffer = (LPSTR)malloc(dwSize*sizeof(TCHAR)); if (!szBuffer) { LogMessage( INSTALLMESSAGE_FATALEXIT, NULL, NULL, NULL, NULL, _T("Not enough memory could be allocated to complete the requested action. ") _T("Please shutdown any running applications or reboot the computer and rerun ") _T("setup to complete installation.") ); return ERROR_INSTALL_FAILURE; } memset(szBuffer, '\0', (dwSize*sizeof(TCHAR))); // Base 64 encode the 'boinc_project' account password // CW2A pszASCIIDecodedPassword( strBOINCProjectAccountPassword.c_str() ); if (!Base64Encode( (const BYTE*)((LPSTR)pszASCIIDecodedPassword), (int)strlen(pszASCIIDecodedPassword), szBuffer, (int*)&dwSize, 0) ) { LogMessage( INSTALLMESSAGE_FATALEXIT, NULL, NULL, NULL, NULL, _T("The 'boinc_project' account password failed to be encoded.") ); return ERROR_INSTALL_FAILURE; } CA2W pszUnicodeEncodedPassword( szBuffer ); _sntprintf( szMessage, sizeof(szMessage), _T("(Unicode) Base64 Encoded String: '%s'"), pszUnicodeEncodedPassword.m_psz ); LogMessage( INSTALLMESSAGE_INFO, NULL, NULL, NULL, NULL, szMessage ); FILE* fClientAuthFile = NULL; fClientAuthFile = _tfopen(strClientAuthFile.c_str(), _T("w")); _ftprintf( fClientAuthFile, _T("<client_authorization>\n") _T(" <boinc_project>\n") _T(" <username>%s</username>\n") _T(" <password>%s</password>\n") _T(" </boinc_project>\n") _T("</client_authorization>\n"), strBOINCProjectAccountUsername.c_str(), pszUnicodeEncodedPassword.m_psz ); fclose(fClientAuthFile); free(szBuffer); } return ERROR_SUCCESS; }