Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
11 KiB

  1. // File: util.h
  2. //
  3. // Contents: Utility classes.
  4. //
  5. // Classes: CRefCount
  6. //
  7. // Functions://
  8. // History:
  9. //
  10. //----------------------------------------------------------------------------
  11. #ifndef _URLZONE_UTIL_H_
  12. #define _URLZONE_UTIL_H_
  13. // Declarations for global variables.
  14. extern BOOL g_bUseHKLMOnly;
  15. extern BOOL g_bInit;
  16. extern BOOL IsZonesInitialized();
  17. extern HINSTANCE g_hInst;
  18. // Cache of drive letter to drive type.
  19. extern DWORD GetDriveTypeFromCacheA(LPCSTR psz);
  20. // Is this file under the Cache directory.
  21. extern BOOL IsFileInCacheDir(LPCWSTR pszFile);
  22. // Is this file under the Cookie directory.
  23. extern BOOL IsFileInCookieDir(LPCWSTR pszFile);
  24. // Is urlmon currently loaded in GUI-mode setup?
  25. extern BOOL IsInGUIModeSetup();
  26. // Replacement for ultoa, works with wide-chars.
  27. extern BOOL DwToWchar (DWORD dw, LPWSTR lpwsz, int radix);
  28. #ifdef unix
  29. #undef offsetof
  30. #endif /* unix */
  31. #define offsetof(s,m) ( (SIZE_T) &( ((s*)NULL)->m) )
  32. #define GETPPARENT(pmemb, struc, membname) ((struc*)(((char*)(pmemb))-offsetof(struc, membname)))
  33. // Does this file bear the Mark of the Web
  34. extern BOOL FileBearsMarkOfTheWeb(LPCTSTR pszFile, LPWSTR *ppszURLMark);
  35. EXTERN_C HRESULT ZonesDllInstall(BOOL bInstall, LPCWSTR pwStr);
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CRegKey
  38. class CRegKey
  39. {
  40. public:
  41. CRegKey(BOOL bHKLMOnly);
  42. CRegKey();
  43. ~CRegKey();
  44. // Attributes
  45. public:
  46. operator HUSKEY() const;
  47. HUSKEY m_hKey;
  48. BOOL m_bHKLMOnly;
  49. // Operations
  50. public:
  51. LONG SetValue(DWORD dwValue, LPCTSTR lpszValueName); // DWORD
  52. LONG SetValue(LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL); // STRING
  53. LONG SetBinaryValue(const BYTE *pb, LPCTSTR lpszValueName, DWORD dwCount); // BINARY
  54. LONG SetValueOfType(const BYTE *pb, LPCTSTR lpszValueName, DWORD dwCount, DWORD dwType); // ANY TYPE
  55. LONG QueryValue(DWORD* pdwValue, LPCTSTR lpszValueName);
  56. LONG QueryValue(LPTSTR szValue, LPCTSTR lpszValueName, DWORD* pdwCount);
  57. LONG QueryBinaryValue(LPBYTE pb, LPCTSTR lpszValueName, DWORD *pdwCount);
  58. LONG QueryValueOrWild (DWORD* pdwValue, LPCTSTR lpszValueName)
  59. {
  60. if (ERROR_SUCCESS == QueryValue (pdwValue, lpszValueName))
  61. return ERROR_SUCCESS;
  62. else
  63. return QueryValue (pdwValue, TEXT("*"));
  64. }
  65. LONG SetKeyValue(LPCTSTR lpszKeyName, LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL);
  66. static LONG WINAPI SetValue(HUSKEY hKeyParent, LPCTSTR lpszKeyName,
  67. LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL, BOOL bHKLMOnly = FALSE);
  68. inline LONG QuerySubKeyInfo (DWORD* pdwNumKeys, DWORD* pdwMaxLen, DWORD *pdwNumValues);
  69. LONG EnumKey(DWORD dwIndex, LPTSTR lpszName, DWORD* pcchName);
  70. LONG EnumValue(DWORD dwIndex, LPTSTR pszValueName, LPDWORD pcchValueNameLen,
  71. LPDWORD pdwType, LPVOID pvData, LPDWORD pcbData);
  72. LONG Create(
  73. HUSKEY hKeyParent, // OPTIONAL
  74. LPCTSTR lpszKeyName,
  75. REGSAM samDesired);
  76. LONG Open(
  77. HUSKEY hKeyParent, // OPTIONAL
  78. LPCTSTR lpszKeyName,
  79. REGSAM samDesired);
  80. LONG Close();
  81. HUSKEY Detach();
  82. void Attach(HUSKEY hKey);
  83. LONG DeleteValue(LPCTSTR lpszValue);
  84. LONG DeleteEmptyKey(LPCTSTR pszSubKey);
  85. private:
  86. inline DWORD RegSetFlags() const
  87. { return m_bHKLMOnly ? SHREGSET_FORCE_HKLM : SHREGSET_FORCE_HKCU; }
  88. inline SHREGDEL_FLAGS RegDelFlags() const
  89. { return m_bHKLMOnly ? SHREGDEL_HKLM : SHREGDEL_HKCU; }
  90. inline SHREGENUM_FLAGS RegEnumFlags() const
  91. { return m_bHKLMOnly ? SHREGENUM_HKLM : SHREGENUM_DEFAULT; }
  92. };
  93. inline CRegKey::CRegKey(BOOL bHKLMOnly)
  94. {m_hKey = NULL; m_bHKLMOnly = bHKLMOnly;}
  95. inline CRegKey::CRegKey()
  96. {
  97. TransAssert(g_bInit);
  98. m_bHKLMOnly = g_bUseHKLMOnly;
  99. m_hKey = NULL;
  100. }
  101. inline CRegKey::~CRegKey()
  102. {Close();}
  103. inline CRegKey::operator HUSKEY() const
  104. {return m_hKey;}
  105. inline LONG CRegKey::SetValue(DWORD dwValue, LPCTSTR lpszValueName)
  106. {
  107. TransAssert(m_hKey != NULL);
  108. return SHRegWriteUSValue(m_hKey, lpszValueName, REG_DWORD,
  109. (LPVOID)&dwValue, sizeof(DWORD), RegSetFlags());
  110. }
  111. inline LONG CRegKey::SetValue(LPCTSTR lpszValue, LPCTSTR lpszValueName)
  112. {
  113. TransAssert(lpszValue != NULL);
  114. TransAssert(m_hKey != NULL);
  115. return SHRegWriteUSValue(m_hKey, lpszValueName, REG_SZ,
  116. (LPVOID)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR), RegSetFlags());
  117. }
  118. inline LONG CRegKey::SetBinaryValue(const BYTE *pb, LPCTSTR lpszValueName, DWORD dwCount)
  119. {
  120. TransAssert(pb != NULL);
  121. TransAssert(m_hKey != NULL);
  122. return SHRegWriteUSValue(m_hKey, lpszValueName, REG_BINARY,
  123. (LPVOID)pb, dwCount, RegSetFlags());
  124. }
  125. inline LONG CRegKey::SetValueOfType(const BYTE *pb, LPCTSTR lpszValueName, DWORD dwCount, DWORD dwType)
  126. {
  127. TransAssert(pb != NULL);
  128. TransAssert(m_hKey != NULL);
  129. return SHRegWriteUSValue(m_hKey, lpszValueName, dwType,
  130. (LPVOID)pb, dwCount, RegSetFlags());
  131. }
  132. inline LONG CRegKey::EnumKey(DWORD dwIndex, LPTSTR lpszName, LPDWORD pcchName)
  133. {
  134. TransAssert(pcchName != NULL);
  135. return SHRegEnumUSKey(m_hKey, dwIndex, lpszName, pcchName, RegEnumFlags());
  136. }
  137. inline LONG CRegKey::EnumValue(DWORD dwIndex, LPTSTR pszValueName, LPDWORD pcchValueNameLen,
  138. LPDWORD pdwType, LPVOID pvData, LPDWORD pcbData)
  139. {
  140. // If these counts are all NULL, you will not get anything useful back.
  141. TransAssert(pcchValueNameLen != NULL || pdwType != NULL || pcbData != NULL);
  142. return SHRegEnumUSValue(m_hKey, dwIndex, pszValueName, pcchValueNameLen, pdwType,
  143. pvData, pcbData, RegEnumFlags());
  144. }
  145. inline LONG CRegKey::QuerySubKeyInfo(DWORD *pdwNumKeys, DWORD *pdwMaxLen, DWORD *pdwNumValues)
  146. {
  147. return SHRegQueryInfoUSKey (m_hKey, pdwNumKeys, pdwMaxLen,
  148. pdwNumValues, NULL, RegEnumFlags());
  149. }
  150. inline HUSKEY CRegKey::Detach()
  151. {
  152. HUSKEY hKey = m_hKey;
  153. m_hKey = NULL;
  154. return hKey;
  155. }
  156. inline void CRegKey::Attach(HUSKEY hKey)
  157. {
  158. TransAssert(m_hKey == NULL);
  159. m_hKey = hKey;
  160. }
  161. inline LONG CRegKey::DeleteValue(LPCTSTR lpszValue)
  162. {
  163. TransAssert(m_hKey != NULL);
  164. return SHRegDeleteUSValue(m_hKey, lpszValue, RegDelFlags());
  165. }
  166. inline LONG CRegKey::DeleteEmptyKey(LPCTSTR pszSubKey)
  167. {
  168. TransAssert(m_hKey != NULL);
  169. return SHRegDeleteEmptyUSKey(m_hKey, pszSubKey, RegDelFlags());
  170. }
  171. // Simple helper class to get an exclusive lock for the duration of a function.
  172. // WILL NOT BLOCK IF THE HANDLE PASSED IS NULL OR INVALID.
  173. class CExclusiveLock
  174. {
  175. public:
  176. CExclusiveLock(HANDLE hMutex); // pass in a handle to a mutex.
  177. ~CExclusiveLock();
  178. private:
  179. HANDLE m_hMutex;
  180. BOOL fOk;
  181. };
  182. inline CExclusiveLock::CExclusiveLock( HANDLE hMutex )
  183. {
  184. fOk = FALSE;
  185. if ( hMutex )
  186. {
  187. m_hMutex = hMutex;
  188. DWORD dw = WaitForSingleObject(hMutex, INFINITE);
  189. if ( (dw == WAIT_OBJECT_0) || (dw == WAIT_ABANDONED) )
  190. {
  191. // fix IE bug 18152
  192. fOk = TRUE;
  193. }
  194. else
  195. {
  196. TransAssert(FALSE); //shouldn't be anything else
  197. }
  198. }
  199. }
  200. inline CExclusiveLock::~CExclusiveLock( )
  201. {
  202. if ( fOk )
  203. {
  204. ReleaseMutex(m_hMutex);
  205. }
  206. }
  207. // Helper class to create a shared memory object to share between processes.
  208. class CSharedMem
  209. {
  210. public:
  211. CSharedMem() { m_hFileMapping = NULL; m_lpVoidShared = NULL ; m_dwSize = 0; };
  212. ~CSharedMem( ) { Release( ); }
  213. BOOL Init(LPCSTR pszNamePrefix, DWORD dwSize);
  214. VOID Release( );
  215. // Offset into the shared memory section.
  216. // Always check return value since this can return NULL.
  217. LPVOID GetPtr (DWORD dwOffset);
  218. private:
  219. HANDLE m_hFileMapping ;
  220. LPVOID m_lpVoidShared;
  221. DWORD m_dwSize;
  222. };
  223. extern CSharedMem g_SharedMem;
  224. // Shared memory related constants
  225. #define SM_REGZONECHANGE_COUNTER 0 // Dword at offset 0
  226. #define SM_SECMGRCHANGE_COUNTER 4 // Dword at offset 4
  227. #define SM_SECTION_SIZE 8 // Total size of shared memory section.
  228. #define SM_SECTION_NAME "UrlZonesSM_"
  229. // registry key paths - absolute
  230. #define SZROOT TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\")
  231. #define SZZONES SZROOT TEXT("Zones\\")
  232. #define SZTEMPLATE SZROOT TEXT("TemplatePolicies\\")
  233. #define SZZONEMAP SZROOT TEXT("ZoneMap\\")
  234. #define SZCACHE SZROOT TEXT("Cache")
  235. #define SZPOLICIES TEXT("Software\\Policies\\Microsoft\\Windows\\CurrentVersion\\Internet Settings")
  236. #define SZHKLMONLY TEXT("Security_HKLM_only")
  237. // Entries to figure out if per user cache is allowed.
  238. #define SZLOGON TEXT("Network\\Logon")
  239. #define SZUSERPROFILES TEXT("UserProfiles")
  240. // Cache location if per user cache is allowed.
  241. #define SZSHELLFOLDER TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders")
  242. #define SZTIFS TEXT("Cache")
  243. // cache location if global
  244. #define SZCACHECONTENT SZROOT TEXT("Cache\\Content")
  245. #define SZCACHEPATH TEXT("CachePath")
  246. // registry key paths - relative to SZZONES
  247. #define SZZONESTANDARD TEXT("Standard\\")
  248. #define SZZONEUSERDEFINED TEXT("User-Defined\\")
  249. // registry key paths - relative to SZZONEMAP
  250. #define SZDOMAINS TEXT("Domains\\")
  251. #define SZHARDENEDDOMAINS TEXT("EscDomains\\")
  252. #define SZRANGES TEXT("Ranges\\")
  253. #define SZESCRANGES TEXT("EscRanges\\")
  254. #define SZPROTOCOLS TEXT("ProtocolDefaults\\")
  255. // registry value names
  256. #define SZINTRANETNAME TEXT("IntranetName")
  257. #define SZUNCASINTRANET TEXT("UNCAsIntranet")
  258. #define SZPROXYBYPASS TEXT("ProxyBypass")
  259. #define SZRANGE TEXT(":Range")
  260. #define SZRANGEPREFIX TEXT("Range")
  261. // Attributes to deal with "High", "Med", "Low" template policies
  262. #define SZMINLEVEL __TEXT("MinLevel")
  263. #define SZRECLEVEL __TEXT("RecommendedLevel")
  264. #define SZCURRLEVEL __TEXT("CurrentLevel")
  265. // registry key path for allowed activex controls list; relative to HKEY_LOCAL_MACHINE _or_ HKEY_CURRENT_USER
  266. #define ALLOWED_CONTROLS_KEY TEXT("SOFTWARE\\Policies\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\AllowedControls")
  267. #define CSTRLENW(str) (sizeof(str)/sizeof(TCHAR) - 1)
  268. #define MARK_PREFIX_SIZE 30
  269. #define MARK_SUFFIX_SIZE 10
  270. #define EXTRA_BUFFER_SIZE 1024
  271. #define UNICODE_HEADER_SIZE 2
  272. #endif // _URLZONE_UTIL_H_