HRESULT CAssemblyStream::Init (LPOLESTR pszPath, DWORD dwFormat) { HRESULT hr = S_OK; DWORD cwPath; BOOL bRet; CCriticalSection cs(&g_csInitClb); ASSERT(pszPath); _dwFormat = dwFormat; cwPath = lstrlen(pszPath) + 1; ASSERT(cwPath < MAX_PATH); memcpy(_szPath, pszPath, sizeof(TCHAR) * cwPath); _hf = CreateFile(pszPath, GENERIC_WRITE, 0 /* no sharing */, NULL, CREATE_NEW, FILE_FLAG_SEQUENTIAL_SCAN, NULL); if (_hf == INVALID_HANDLE_VALUE) { hr = HRESULT_FROM_WIN32(GetLastError()); ReleaseParent(hr); goto Exit; } if (!g_hProv) { hr = cs.Lock(); if (FAILED(hr)) { goto Exit; } bRet=TRUE; if(!g_hProv) { bRet = WszCryptAcquireContext(&g_hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); } cs.Unlock(); if (!bRet) { hr = HRESULT_FROM_WIN32(GetLastError()); ReleaseParent(hr); goto Exit; } } bRet = CryptCreateHash(g_hProv, CALG_SHA1, 0, 0, &_hHash); if (!bRet) { hr = HRESULT_FROM_WIN32(GetLastError()); ReleaseParent(hr); goto Exit; } Exit: return hr; }
HRESULT CAssemblyStream::Init (LPCOLESTR pszPath, DWORD dwFormat) { HRESULT hr = S_OK; DWORD cwPath; BOOL bRet; _ASSERTE(pszPath); _dwFormat = dwFormat; cwPath = lstrlenW(pszPath) + 1; _ASSERTE(cwPath < MAX_PATH); memcpy(_szPath, pszPath, sizeof(TCHAR) * cwPath); _hf = WszCreateFile(pszPath, GENERIC_WRITE, 0 /* no sharing */, NULL, CREATE_NEW, FILE_FLAG_SEQUENTIAL_SCAN, NULL); if (_hf == INVALID_HANDLE_VALUE) { hr = HRESULT_FROM_WIN32(GetLastError()); ReleaseParent(hr); goto Exit; } if (!g_hProv) { HCRYPTPROV hProv; bRet = CryptAcquireContextA(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); if (!bRet) { hr = HRESULT_FROM_WIN32(GetLastError()); ReleaseParent(hr); goto Exit; } if (InterlockedCompareExchangePointer((void **)&g_hProv, (void *)hProv, 0)) { // Lost the race. Release our provider. CryptReleaseContext(hProv, 0); } } bRet = CryptCreateHash(g_hProv, CALG_SHA1, 0, 0, &_hHash); if (!bRet) { hr = HRESULT_FROM_WIN32(GetLastError()); ReleaseParent(hr); goto Exit; } Exit: return hr; }
BOOL CswFile::BindToParent (IN IUnknown* pUnkParent) { ReleaseParent (); m_pUnkParent = pUnkParent; m_pUnkParent->AddRef(); return TRUE; }
void CswFile::CleanUp () { ReleaseParent (); if (m_pStream) { m_pStream->Release (); Init (); } }
STDMETHODIMP CAssemblyStream::Write(VOID const *pv, ULONG cb, ULONG *pcbWritten) { BOOL fRet = WriteFile (_hf, pv, cb, pcbWritten, NULL); if (fRet) { CryptHashData(_hHash, (PBYTE) pv, *pcbWritten, 0); return S_OK; } else { // Report the error. HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); ReleaseParent (hr); return hr; } }
CAssemblyStream::~CAssemblyStream ( ) { if (_pParent) ReleaseParent (STG_E_ABNORMALAPIEXIT); _ASSERTE (_hf == INVALID_HANDLE_VALUE); }
STDMETHODIMP CAssemblyStream::Commit(DWORD dwCommitFlags) { BOOL fRet; HRESULT hr; IAssemblyName *pName = NULL; IAssemblyName *pNameCopy = NULL; IAssemblyManifestImport *pImport = NULL; if(FAILED(hr = AddSizeToItem())) goto exit; fRet = CloseHandle (_hf); _hf = INVALID_HANDLE_VALUE; hr = fRet? S_OK : HRESULT_FROM_WIN32(GetLastError()); if (FAILED(hr)) goto exit; // If this file contains a manifest extract the // name and set it on the parent cache item. switch (_dwFormat) { case STREAM_FORMAT_COMPLIB_MANIFEST: { // If a manifest interface has not already been // set on this item, construct one from path. if (!(pImport = _pParent->GetManifestInterface())) { if (FAILED(hr = CreateAssemblyManifestImport(_szPath, &pImport))) goto exit; if(FAILED(hr = _pParent->SetManifestInterface( pImport ))) goto exit; } // Get the read-only name def. if (FAILED(hr = pImport->GetAssemblyNameDef(&pName))) goto exit; // Make a writeable copy of the name def. if (FAILED(hr = pName->Clone(&pNameCopy))) goto exit; // Cache this on the parent cache item. if (FAILED(hr = _pParent->SetNameDef(pNameCopy))) goto exit; } break; case STREAM_FORMAT_COMPLIB_MODULE: { if( FAILED(hr = CheckHash()) ) goto exit; } break; } // end switch exit: SAFERELEASE(pImport); SAFERELEASE(pName); SAFERELEASE(pNameCopy); CryptDestroyHash(_hHash); _hHash = 0; ReleaseParent (hr); return hr; }