Source code of Windows XP (NT5)
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
8.1 KiB

  1. /*
  2. ** wcsutil.h
  3. ** Sean P. Nolan
  4. **
  5. ** server-side utils
  6. */
  7. #ifndef _WCSUTIL_H_
  8. #define _WCSUTIL_H_
  9. #include <windows.h>
  10. #include <ole2.h>
  11. #include <winsock.h>
  12. #include <httpext.h>
  13. #include "wcguids.h"
  14. /*--------------------------------------------------------------------------+
  15. | Utilities Provided by the Framework |
  16. +--------------------------------------------------------------------------*/
  17. extern LPSTR g_szSSOProgID;
  18. // Logging to application event log -- see ReportEvent() in win32 sdk
  19. // documentation for information on wType and dwEventID.
  20. void LogEvent(WORD wType, DWORD dwEventID, char *sz);
  21. // Asserts
  22. #ifdef DEBUG
  23. extern void AssertProc(PCSTR szFile, DWORD dwLine, PCSTR szMsg, DWORD dwFlags);
  24. #define AssertSzFlg(f, sz, dwFlag) ( (f) ? 0 : AssertProc(__FILE__, __LINE__, sz, dwFlag))
  25. #else // !DEBUG
  26. #define AssertSzFlg(f, sz, dwFlg)
  27. #endif // !DEBUG
  28. #define AP_GETLASTERROR 0x00000001
  29. // under non-debug, AssertSzFlg is defined away, so these #defines don't
  30. // have to be #ifdef DEBUG
  31. #define AssertSz(f, sz) AssertSzFlg(f, sz, 0)
  32. #define AssertEx(f) AssertSz((f), "!(" #f ")")
  33. #define Assert(f) AssertEx(f)
  34. #define AssertSzGLE(f, sz) AssertSzFlg(f, sz, AP_GETLASTERROR)
  35. #define AssertExGLE(f) AssertSzGLE((f), "!(" #f ")")
  36. // Debug Printing
  37. #ifdef DEBUG
  38. int dbgprintf(PCSTR pFormat, ...);
  39. #else
  40. #define dbgprintf()
  41. #endif
  42. // Memory Mgmt
  43. LPVOID _MsnAlloc(DWORD cb);
  44. LPVOID _MsnRealloc(LPVOID pv, DWORD cb);
  45. void _MsnFree(LPVOID pv);
  46. // Data Munging
  47. LPSTR _SzFromVariant(VARIANT *pvar);
  48. BSTR _BstrFromVariant(VARIANT *pvar, BOOL *pfFree);
  49. BOOL _FIntFromVariant(VARIANT *pvar, int *pi);
  50. BSTR BstrQuoteHtml(WCHAR *wsz);
  51. BSTR BstrQuoteForUrl(WCHAR *wsz);
  52. void UnquoteHtml(WCHAR *wszSrc, WCHAR *rgchDst, int cchDst);
  53. // String Manipulation / Parsing
  54. void _AnsiStringFromGuid(REFGUID rguid, LPSTR sz);
  55. LPSTR _SkipWhiteSpace(LPSTR sz);
  56. LPSTR _FindEndOfLine(LPSTR sz);
  57. DWORD _atoi(LPSTR sz);
  58. // URL Fiddling
  59. int UrlType(char *szUrl);
  60. #define URL_TYPE_ABSOLUTE 0 // e.g. http://foo/bar or just //foo.com/bar.htm
  61. #define URL_TYPE_LOCAL_ABSOLUTE 1 // e.g. /foo/bar/baz.htm
  62. #define URL_TYPE_RELATIVE 2 // e.g. xyzzy/plugh.html
  63. // IIS Hacks
  64. #ifdef _HTTPEXT_H_
  65. BOOL _FTranslateVirtualRoot(EXTENSION_CONTROL_BLOCK *pecb, LPSTR szPathIn,
  66. LPSTR szPathTranslated, DWORD cbPathTranslated);
  67. #endif // _HTTPEXT_H_
  68. // Critical Sections
  69. class CCritSec
  70. {
  71. private:
  72. CRITICAL_SECTION m_cs;
  73. public:
  74. CCritSec(void) { ::InitializeCriticalSection(&m_cs); }
  75. ~CCritSec(void) { ::DeleteCriticalSection(&m_cs); }
  76. void Lock(void) { ::EnterCriticalSection(&m_cs); }
  77. void Unlock(void) { ::LeaveCriticalSection(&m_cs); }
  78. };
  79. /*--------------------------------------------------------------------------+
  80. | CThingWatcher/CFileWatcher/CRegKeyWatcher |
  81. +--------------------------------------------------------------------------*/
  82. typedef void (*PFNFILECHANGED)(LPSTR szFile, LONG lUser);
  83. typedef void (*PFNREGKEYCHANGED)(HKEY hkey, LONG lUser);
  84. typedef int (__stdcall *PFNCLOSEHEVTNOTIFY)(HANDLE hevtNotify);
  85. class CThingWatcher
  86. {
  87. friend DWORD ThingWatcherThread(CThingWatcher *pfw);
  88. public:
  89. CThingWatcher(PFNCLOSEHEVTNOTIFY pfnCloseHevtNotify);
  90. ~CThingWatcher();
  91. BOOL FWatchHandle(HANDLE hevtNotify);
  92. private:
  93. virtual BOOL FireChange(DWORD dwWait) = 0;
  94. HANDLE m_rghWait[2];
  95. HINSTANCE m_hModule;
  96. PFNCLOSEHEVTNOTIFY m_pfnCloseHevtNotify;
  97. };
  98. class CFileWatcher : public CThingWatcher
  99. {
  100. private:
  101. char m_szPath[MAX_PATH];
  102. FILETIME m_ftLastWrite;
  103. LONG m_lUser;
  104. PFNFILECHANGED m_pfnChanged;
  105. BOOL m_fDirectory;
  106. HANDLE m_hevtNotify;
  107. public:
  108. CFileWatcher(void);
  109. ~CFileWatcher(void);
  110. BOOL FStartWatching(LPSTR szPath, LONG lUser, PFNFILECHANGED pfnChanged);
  111. private:
  112. virtual BOOL FireChange(DWORD dwWait);
  113. };
  114. class CRegKeyWatcher : public CThingWatcher
  115. {
  116. public:
  117. CRegKeyWatcher();
  118. ~CRegKeyWatcher();
  119. BOOL FStartWatching(HKEY hkey, BOOL fSubTree, DWORD dwNotifyFilter, LONG lUser, PFNREGKEYCHANGED pfnChanged);
  120. private:
  121. virtual BOOL FireChange(DWORD dwWait);
  122. HKEY m_hkey;
  123. LONG m_lUser;
  124. PFNREGKEYCHANGED m_pfnChanged;
  125. HANDLE m_hevtNotify;
  126. };
  127. /*--------------------------------------------------------------------------+
  128. | CDataFile |
  129. +--------------------------------------------------------------------------*/
  130. class CDataFile
  131. {
  132. friend class CDataFileGroup;
  133. friend void DataFileChanged(LPSTR szFile, LONG lUser);
  134. private:
  135. ULONG m_cRef;
  136. CDataFile *m_pdfNext;
  137. CDataFile *m_pdfPrev;
  138. CDataFileGroup *m_pfg;
  139. protected:
  140. CCritSec m_cs;
  141. char m_szDataPath[MAX_PATH];
  142. CFileWatcher m_fw;
  143. public:
  144. CDataFile(LPSTR szDataPath, CDataFileGroup *pfg);
  145. ~CDataFile(void);
  146. ULONG AddRef(void);
  147. ULONG Release(void);
  148. BOOL FWatchFile(PFNFILECHANGED pfnChanged = NULL);
  149. virtual void FreeDataFile(void) { delete this; }
  150. private:
  151. ULONG GetRefCount(void);
  152. BOOL FMatch(LPSTR szDataPath);
  153. CDataFile *GetNext(void) { return(m_pdfNext); }
  154. CDataFile *GetPrev(void) { return(m_pdfPrev); }
  155. void SetNext(CDataFile *pdf) { m_pdfNext = pdf; }
  156. void SetPrev(CDataFile *pdf) { m_pdfPrev = pdf; }
  157. };
  158. /*--------------------------------------------------------------------------+
  159. | CDataFileGroup |
  160. +--------------------------------------------------------------------------*/
  161. #define NUM_GROUP_BUCKETS (1024)
  162. typedef struct _HashBucket
  163. {
  164. CDataFile *pdfHead;
  165. CDataFile *pdfTail;
  166. }
  167. HB;
  168. class CDataFileGroup
  169. {
  170. private:
  171. CCritSec m_cs;
  172. HB m_rghb[NUM_GROUP_BUCKETS];
  173. public:
  174. CDataFileGroup(void);
  175. ~CDataFileGroup(void);
  176. CDataFile *GetDataFile(LPSTR szDataPath);
  177. void ForgetDataFile(CDataFile *pdf);
  178. virtual CDataFile *CreateDataFile(LPSTR szDataPath) = 0;
  179. private:
  180. void RememberDataFile(CDataFile *pdf, HB *phb = NULL);
  181. HB *GetHashBucket(LPSTR szDataPath);
  182. };
  183. /*--------------------------------------------------------------------------+
  184. | CGenericHash |
  185. +--------------------------------------------------------------------------*/
  186. typedef struct _HashItem
  187. {
  188. BSTR bstrName;
  189. LPVOID pvData;
  190. struct _HashItem *phiNext;
  191. struct _HashItem *phiPrev;
  192. }
  193. HITEM;
  194. class CGenericHash
  195. {
  196. protected:
  197. DWORD m_chi;
  198. HITEM **m_rgphi;
  199. CCritSec m_cs;
  200. public:
  201. CGenericHash(DWORD cBuckets);
  202. ~CGenericHash(void);
  203. LPVOID PvFind(OLECHAR *wszName);
  204. BOOL FAdd(OLECHAR *wszName, LPVOID pv);
  205. void Remove(OLECHAR *wszName);
  206. void RemoveAll(void);
  207. void Lock(void) { m_cs.Lock(); }
  208. void Unlock(void) { m_cs.Unlock(); }
  209. virtual DWORD GetHashValue(OLECHAR *wsz);
  210. virtual void FreeHashData(LPVOID pv);
  211. private:
  212. HITEM *FindItem(OLECHAR *wszName, HITEM ***ppphiHead);
  213. void RemoveItem(HITEM **pphiHead, HITEM *phi);
  214. BOOL FEnsureBuckets(void);
  215. };
  216. /*--------------------------------------------------------------------------+
  217. | CResourceCollection |
  218. +--------------------------------------------------------------------------*/
  219. typedef struct _resource
  220. {
  221. BOOL fInUse;
  222. BOOL fValid;
  223. PVOID pv;
  224. } RS, *PRS;
  225. typedef PRS HRS;
  226. #define hrsNil NULL
  227. class CResourceCollection
  228. {
  229. public:
  230. CResourceCollection();
  231. ~CResourceCollection();
  232. BOOL FInit(int cRsrc);
  233. BOOL FTerm();
  234. HRS HrsGetResource();
  235. void ReleaseResource(BOOL fReset, HRS hrs);
  236. void CleanupAll(PVOID pvNil);
  237. BOOL FValid(HRS hrs);
  238. private:
  239. PRS PrsFree();
  240. void WaitForRs();
  241. CCritSec m_cs;
  242. HANDLE m_hsem;
  243. int m_crs;
  244. PRS m_rgrs;
  245. virtual BOOL FInitResource(PRS prs) = 0;
  246. virtual void CleanupResource(PRS prs) = 0;
  247. };
  248. /*--------------------------------------------------------------------------+
  249. | CSocketCollection |
  250. +--------------------------------------------------------------------------*/
  251. class CSocketCollection : public CResourceCollection
  252. {
  253. public:
  254. CSocketCollection() : CResourceCollection() {};
  255. ~CSocketCollection() {};
  256. BOOL FInit(int cRsrc, char *szServer, USHORT usPort); // usPort should be in host byte order
  257. BOOL FReinit(char *szServer, USHORT usPort);
  258. SOCKET ScFromHrs(HRS hrs);
  259. private:
  260. char m_szServer[MAX_PATH];
  261. int m_usPort;
  262. SOCKADDR_IN m_sin;
  263. virtual BOOL FInitResource(PRS prs);
  264. virtual void CleanupResource(PRS prs);
  265. };
  266. #endif // _WCSUTIL_H_