// File: util.cxx // // Contents: Utility classes. // // Classes: CRefCount, CRegKey // // Functions:// // History: // //---------------------------------------------------------------------------- #include "zonepch.h" #include "advpub.h" #ifdef UNIX #include #endif /* UNIX */ #include "shfolder.h" // Misc utility functions and declarations. #define DRIVE_UNINIT 0xFFFF // Indicates that we haven't called GetDriveType yet. DWORD rgdwDriveTypeCache[26]; // one for each drive letter. BOOL g_bInit = FALSE; BOOL g_bUseHKLMOnly = FALSE; static LPWSTR s_pwzCacheDir; static HRESULT GetCacheDirectory( ); static LPWSTR s_pwzCookieDir = NULL; static CHAR *s_szMarkPrefix = "\r\n"; CSharedMem g_SharedMem; static CRITICAL_SECTION g_csect_GetCacheDir; BOOL IsZonesInitialized( ) { return g_bInit; } BOOL ZonesInit( ) { if (!g_bInit) { DWORD dwType; DWORD dwSize = sizeof(DWORD); DWORD dwDefault = FALSE ; // If no entry found default is use HKCU. g_bInit = TRUE; // Call the shlwapi wrapper function that directly reads in a value for us. // Note that if the call fails we have the right value in g_bHKLMOnly already. SHRegGetUSValue(SZPOLICIES, SZHKLMONLY, &dwType, &g_bUseHKLMOnly, &dwSize, TRUE, &dwDefault, sizeof(dwDefault)); InitializeCriticalSection(&CUrlZoneManager::s_csect); CUrlZoneManager::s_bcsectInit = TRUE; CSecurityManager::GlobalInit(); // Initialize the drive type cache. for ( int i = 0 ; i < ARRAYSIZE(rgdwDriveTypeCache) ; i++ ) rgdwDriveTypeCache[i] = DRIVE_UNINIT; //initialize critical section for GetCacheDirectory() InitializeCriticalSection(&g_csect_GetCacheDir); } return TRUE; } VOID ZonesUnInit ( ) { CUrlZoneManager::Cleanup( ); CSecurityManager::GlobalCleanup( ); g_SharedMem.Release(); // Free any memory allocated for the cache directory. delete [] s_pwzCacheDir; if(s_pwzCookieDir) delete [] s_pwzCookieDir; // Destroy the GetCacheDirectory() critsec. DeleteCriticalSection(&g_csect_GetCacheDir); } // IEAK calls this function so force us to re-read the global settings for // HKLM vs HKCU. STDAPI ZonesReInit(DWORD /* dwReserved */) { DWORD dwType; DWORD dwDefault = g_bUseHKLMOnly; DWORD dwSize = sizeof(DWORD); DWORD dwError = SHRegGetUSValue(SZPOLICIES, SZHKLMONLY, &dwType, &g_bUseHKLMOnly, &dwSize, TRUE, &dwDefault, sizeof(dwDefault)); return HRESULT_FROM_WIN32(dwError); } // SHDOCVW calls this during Thicket save to get the comment to flag the // saved file as STDAPI GetMarkOfTheWeb(LPCSTR pszURL, LPCSTR pszFile, DWORD dwFlags, LPSTR *ppszMark) { HRESULT hr = S_OK; int cchURL = lstrlenA(pszURL); // Note - the code assumes that lstrlen(IDS_MARK_PREFIX) // equals wsprintf(IDS_MARK_PREFIX, lstrlen(url) *ppszMark = (LPSTR)LocalAlloc( LMEM_FIXED, (lstrlenA(s_szMarkPrefix) + cchURL + lstrlenA(s_szMarkSuffix) + 1) * sizeof(CHAR) ); if ( *ppszMark ) { wsprintfA( *ppszMark, s_szMarkPrefix, cchURL ); lstrcatA( *ppszMark, pszURL ); lstrcatA( *ppszMark, s_szMarkSuffix ); } else hr = E_OUTOFMEMORY; return hr; } // The security manager calls this to sniff a file: URL for the Mark of the Web BOOL FileBearsMarkOfTheWeb(LPCTSTR pszFile, LPWSTR *ppszURLMark) { BOOL fMarked = FALSE; HANDLE hFile; CHAR szMarkPrefix[MARK_PREFIX_SIZE]; CHAR szMarkSuffix[MARK_SUFFIX_SIZE]; WCHAR wzMarkPrefix[MARK_PREFIX_SIZE]; WCHAR wzMarkSuffix[MARK_SUFFIX_SIZE]; BOOL fIsInUnicode = FALSE; DWORD cchReadBufLen = 0; DWORD cchURL; CHAR *szMarkHead = NULL; WCHAR *wzMarkHead = NULL; CHAR *szMarkSuf = NULL; WCHAR *wzMarkSuf = NULL; CHAR *pszURLMark = NULL; DWORD cchReadLen = 0; char *pszTmp = NULL; char *szReadBuf = NULL; int iIteration = 1; lstrcpyA(szMarkPrefix, s_szMarkPrefix); lstrcpyA(szMarkSuffix, s_szMarkSuffix); hFile = CreateFile( pszFile, GENERIC_READ, FILE_SHARE_READ , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if ( hFile != INVALID_HANDLE_VALUE ) { DWORD cchPrefix = lstrlenA(szMarkPrefix); DWORD dwRead; CHAR szHeader[UNICODE_HEADER_SIZE]; DWORD cchHeader = UNICODE_HEADER_SIZE; if (ReadFile(hFile, szHeader, cchHeader, &dwRead, NULL) && cchHeader == dwRead) { if ((BYTE)szHeader[0] == (BYTE)0xFF && (BYTE)szHeader[1] == (BYTE)0xFE) { fIsInUnicode = TRUE; } else { fIsInUnicode = FALSE; SetFilePointer(hFile, 0, NULL, FILE_BEGIN); TransAssert(GetLastError() == NO_ERROR); } } else { // Unable to sniff for header goto Exit; } // File pointer is not at "real" beginning of file, regardless of // whether we are reading a UNICODE or ANSI file. szReadBuf = NULL; cchReadBufLen = 0; iIteration = 1; // Anchor NULL to make szMarkPrefix: