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.

503 lines
16 KiB

  1. #ifndef __UTILS_HXX__
  2. #define __UTILS_HXX__
  3. //+------------------------------------------------------------------------
  4. //
  5. // File : utils.hxx
  6. //
  7. // purpose : some useful helper functions
  8. //
  9. //-------------------------------------------------------------------------
  10. #define DATE_STR_LENGTH 30
  11. #define INTERNET_COOKIE_SIZE_LIMIT 4096
  12. #define MAX_SCRIPT_BLOCK_SIZE 4096
  13. #define VB_TRUE ((VARIANT_BOOL)-1) // TRUE for VARIANT_BOOL
  14. #define VB_FALSE ((VARIANT_BOOL)0) // FALSE for VARIANT_BOOL
  15. #define LCID_SCRIPTING 0x0409 // Mandatory VariantChangeTypeEx localeID
  16. #define ISSPACE(ch) (((ch) == _T(' ')) || ((unsigned)((ch) - 9)) <= 13 - 9)
  17. // Handy define stolen from Trident's core\include\cdutil.hxx (Used by rectpeer and tmpprint)
  18. #define MSOCMDSTATE_DOWN (MSOCMDF_SUPPORTED | MSOCMDF_ENABLED | MSOCMDF_LATCHED)
  19. // VARIANT conversion interface exposed by script engines (VBScript/JScript).
  20. EXTERN_C const GUID SID_VariantConversion;
  21. // Security function used by the print template objects: CLayoutRect, CTemplatePrinter
  22. BOOL TemplateAccessAllowed(IElementBehaviorSite *pISite);
  23. #define TEMPLATESECURITYCHECK() \
  24. if (!TemplateAccessAllowed(_pPeerSite)) \
  25. { \
  26. return E_ACCESSDENIED; \
  27. } \
  28. //+------------------------------------------------------------------------
  29. //
  30. // Interface and Class pointer manipulation
  31. //
  32. //-------------------------------------------------------------------------
  33. // Prototypes for actual out-of-line implementations of the
  34. // pointer management functions
  35. void ClearInterfaceFn(IUnknown ** ppUnk);
  36. void ReplaceInterfaceFn(IUnknown ** ppUnk, IUnknown * pUnk);
  37. void ReleaseInterface(IUnknown * pUnk);
  38. // Inline portions
  39. //+------------------------------------------------------------------------
  40. //
  41. // Function: ClearInterface
  42. //
  43. // Synopsis: Sets an interface pointer to NULL, after first calling
  44. // Release if the pointer was not NULL initially
  45. //
  46. // Arguments: [ppI] *ppI is cleared
  47. //
  48. //-------------------------------------------------------------------------
  49. #ifdef WIN16
  50. #define ClearInterface(p) ClearInterfaceFn((IUnknown **)p)
  51. #else
  52. template <class PI>
  53. inline void
  54. ClearInterface(PI * ppI)
  55. {
  56. #if DBG == 1
  57. IUnknown * pUnk = *ppI;
  58. #endif
  59. ClearInterfaceFn((IUnknown **) ppI);
  60. }
  61. #endif
  62. //+------------------------------------------------------------------------
  63. //
  64. // Function: ReplaceInterface
  65. //
  66. // Synopsis: Replaces an interface pointer with a new interface,
  67. // following proper ref counting rules:
  68. //
  69. // = *ppI is set to pI
  70. // = if pI is not NULL, it is AddRef'd
  71. // = if *ppI was not NULL initially, it is Release'd
  72. //
  73. // Effectively, this allows pointer assignment for ref-counted
  74. // pointers.
  75. //
  76. // Arguments: [ppI] Destination pointer in *ppI
  77. // [pI] Source pointer in pI
  78. //
  79. //-------------------------------------------------------------------------
  80. #ifdef WIN16
  81. #define ReplaceInterface(ppI, pI) ReplaceInterfaceFn((IUnknown **)ppI, pI)
  82. #else
  83. template <class PI>
  84. inline void
  85. ReplaceInterface(PI * ppI, PI pI)
  86. {
  87. #if DBG == 1
  88. IUnknown * pUnk = *ppI;
  89. #endif
  90. ReplaceInterfaceFn((IUnknown **) ppI, pI);
  91. }
  92. #endif
  93. //+------------------------------------------------------------------------
  94. //
  95. // CLASS: CBufferedStr - helper class
  96. //
  97. //-------------------------------------------------------------------------
  98. class CBufferedStr
  99. {
  100. public:
  101. CBufferedStr::CBufferedStr()
  102. {
  103. _pchBuf = NULL;
  104. _cchBufSize = 0;
  105. _cchIndex =0;
  106. }
  107. CBufferedStr::~CBufferedStr() { Free(); }
  108. //
  109. // Creates a buffer and initializes it with the supplied TCHAR*
  110. //---------------------------------------------------------------
  111. HRESULT Set( LPCTSTR pch = NULL, UINT cch=0 );
  112. void Free () { delete [] _pchBuf; }
  113. //
  114. // Adds at the end of the buffer, growing it it necessary.
  115. //---------------------------------------------------------------
  116. HRESULT QuickAppend ( const TCHAR* pch ) { return(QuickAppend(pch, _tcslen(pch))); }
  117. HRESULT QuickAppend ( const TCHAR* pch , ULONG cch );
  118. HRESULT QuickAppend ( long lValue );
  119. //
  120. // Returns current size of the buffer
  121. //---------------------------------------------------------------
  122. UINT Size() { return _pchBuf ? _cchBufSize : 0; }
  123. //
  124. // Returns current length of the buffer string
  125. //---------------------------------------------------------------
  126. UINT Length() { return _pchBuf ? _cchIndex : 0; }
  127. operator LPTSTR () const { return _pchBuf; }
  128. TCHAR * _pchBuf; // Actual buffer
  129. UINT _cchBufSize; // Size of _pchBuf
  130. UINT _cchIndex; // Length of _pchBuf
  131. };
  132. //+------------------------------------------------------------------------
  133. //
  134. // CLASS: CVariant - helper class
  135. //
  136. //-------------------------------------------------------------------------
  137. class CVariant : public VARIANT
  138. {
  139. public:
  140. CVariant() { ZeroVariant(); }
  141. CVariant(VARTYPE vt) { ZeroVariant(); V_VT(this) = vt; }
  142. ~CVariant() { Clear(); }
  143. void ZeroVariant()
  144. {
  145. ((DWORD *)this)[0] = 0; ((DWORD *)this)[1] = 0; ((DWORD *)this)[2] = 0;
  146. }
  147. HRESULT Copy(VARIANT *pVar)
  148. {
  149. return pVar ? VariantCopy(this, pVar) : E_POINTER ;
  150. }
  151. HRESULT Clear()
  152. {
  153. return VariantClear(this);
  154. }
  155. // Coerce from an arbitrary variant into this. (Copes with VATIANT BYREF's within VARIANTS).
  156. HRESULT CoerceVariantArg (VARIANT *pArgFrom, WORD wCoerceToType);
  157. // Coerce current variant into itself
  158. HRESULT CoerceVariantArg (WORD wCoerceToType);
  159. BOOL CoerceNumericToI4 ();
  160. BOOL IsEmpty() { return (vt == VT_NULL || vt == VT_EMPTY);}
  161. BOOL IsOptional() { return (IsEmpty() || vt == VT_ERROR);}
  162. };
  163. //+------------------------------------------------------------------------
  164. //
  165. // Class : CDataListEnumerator
  166. //
  167. // A simple parseing helper -- this returns a token separated by spaces
  168. // or by a separation character that is passed into the constructor.
  169. //
  170. //-------------------------------------------------------------------------
  171. class CDataListEnumerator
  172. {
  173. public:
  174. CDataListEnumerator ( LPCTSTR pszString, TCHAR ch=_T(' '))
  175. {
  176. Assert ( pszString );
  177. pStr = pszString;
  178. chSeparator = ch;
  179. };
  180. BOOL GetNext ( LPCTSTR *ppszNext, INT *pnLen );
  181. private:
  182. LPCTSTR pStr;
  183. LPCTSTR pStart;
  184. TCHAR chSeparator;
  185. };
  186. inline BOOL
  187. CDataListEnumerator::GetNext ( LPCTSTR *ppszNext, INT *pnLen )
  188. {
  189. // Find the start, skipping spaces and separators
  190. while ( ISSPACE(*pStr) || *pStr == chSeparator ) pStr++;
  191. pStart = pStr;
  192. if ( !*pStr )
  193. return FALSE;
  194. // Find the end of the next token
  195. for (;!(*pStr == _T('\0') || *pStr == chSeparator || ISSPACE(*pStr)); pStr++);
  196. *pnLen = (INT)(pStr - pStart);
  197. *ppszNext = pStart;
  198. return TRUE;
  199. }
  200. //-------------------------------------------------------------------------
  201. //
  202. // CDataObject -- This is our attrarray kind of value. IT is currently used
  203. // by rectpeer.hxx to manage the tag attributes. This is put into an
  204. // array and managed there.
  205. //
  206. //-------------------------------------------------------------------------
  207. class CDataObject
  208. {
  209. public:
  210. CDataObject()
  211. {
  212. _pstrPropertyName = NULL;
  213. V_VT(&_varValue) = VT_EMPTY;
  214. _fDirty = FALSE;
  215. };
  216. ~CDataObject()
  217. {
  218. ClearContents();
  219. };
  220. void ClearContents()
  221. {
  222. // don't clear the propertyName, it should be a static
  223. VariantClear(&_varValue);
  224. _fDirty = FALSE;
  225. _pstrPropertyName = NULL;
  226. };
  227. BOOL IsDirty () { return _fDirty; };
  228. //
  229. // Data Set'r functions
  230. //---------------------------------------
  231. HRESULT Set (BSTR bstrValue);
  232. HRESULT Set (VARIANT_BOOL bBool);
  233. HRESULT Set (IDispatch * pDisp);
  234. //
  235. // Data Get'r functions
  236. //---------------------------------------
  237. HRESULT GetAsBOOL (VARIANT_BOOL * pVB);
  238. HRESULT GetAsBSTR (BSTR * pbstr);
  239. HRESULT GetAsDispatch (IDispatch ** ppDisp);
  240. // Data Members
  241. //--------------------------------------------------------------
  242. const TCHAR * _pstrPropertyName; // this is the name of the property, don't free
  243. VARIANT _varValue; // the variant for the value
  244. BOOL _fDirty; // has the property been dirtied
  245. };
  246. //+------------------------------------------------------------------------
  247. //
  248. // Helper functions
  249. //
  250. //-------------------------------------------------------------------------
  251. HRESULT ConvertGmtTimeToString(FILETIME Time, TCHAR * pchDateStr, DWORD cchDateStr);
  252. HRESULT ParseDate(BSTR strDate, FILETIME * pftTime);
  253. int UnicodeFromMbcs(LPWSTR pwstr, int cwch, LPCSTR pstr, int cch=-1);
  254. int MbcsFromUnicode(LPSTR pstr, int cch, LPCWSTR pwstr, int cwch=-1);
  255. HRESULT VariantChangeTypeSpecial(VARIANT *pvargDest,
  256. VARIANT *pVArg,
  257. VARTYPE vt,
  258. IServiceProvider *pSrvProvider = NULL,
  259. WORD wFlags = 0);
  260. const TCHAR * __cdecl _tcsistr (const TCHAR * tcs1,const TCHAR * tcs2);
  261. HRESULT GetClientSiteWindow(IElementBehaviorSite *pSite, HWND *phWnd);
  262. STDMETHODIMP GetHTMLDocument(IElementBehaviorSite * pSite,
  263. IHTMLDocument2 **ppDoc);
  264. STDMETHODIMP GetHTMLWindow(IElementBehaviorSite * pSite,
  265. IHTMLWindow2 **ppWindow);
  266. BOOL AccessAllowed(BSTR bstrUrl, IUnknown * pUnkSite);
  267. STDMETHODIMP AppendChild(IHTMLElement *pOwner, IHTMLElement *pChild);
  268. BOOL IsSameObject(IUnknown *pUnkLeft, IUnknown *pUnkRight);
  269. HRESULT LoadLibrary(char *achLibraryName, HINSTANCE *hInst);
  270. //+------------------------------------------------------------------------
  271. //
  272. // behavior site / element / style access
  273. //
  274. //-------------------------------------------------------------------------
  275. //
  276. // NOTE: procedure for adding a new context element IFoo there:
  277. //
  278. // 1. create data member IFoo * _pFoo
  279. // 2. create an inline for access inline IFoo * Foo() { Assert (_pFoo && "_pFoo opened?"); return _pFoo; };
  280. // 3. create an enum value CA_FOO
  281. // 3a. choose the enum bit flag value so that it appears before any of the objects it depends on.and then
  282. // adjust the rest (sramani)
  283. // 4. in CContextAccess::Open, add the necessary code to retrieve the pointer.
  284. // 5. in CContextAccess::~CContextAccess, add the necessary code to release the pointer
  285. // 6. grab alexz for code review.
  286. //
  287. // thanks!
  288. //
  289. enum CONTEXT_ACCESS
  290. {
  291. CA_NONE = 0x0000,
  292. CA_SITEOM = 0x0001,
  293. CA_SITERENDER = 0x0002,
  294. CA_ELEM = 0x0004,
  295. CA_ELEM2 = 0x0008,
  296. CA_ELEM3 = 0x0010,
  297. CA_STYLE = 0x0020,
  298. CA_STYLE2 = 0x0040,
  299. CA_STYLE3 = 0x0080,
  300. CA_DEFAULTS = 0x0100,
  301. CA_DEFSTYLE = 0x0200,
  302. CA_DEFSTYLE2 = 0x0400,
  303. CA_DEFSTYLE3 = 0x0800,
  304. };
  305. //+------------------------------------------------------------------------
  306. //
  307. // behavior site / element / style access
  308. //
  309. //-------------------------------------------------------------------------
  310. class CContextAccess
  311. {
  312. public:
  313. //
  314. // methods
  315. //
  316. //
  317. // NOTE: please find procedure of adding new context IFoo above ^^^
  318. //
  319. CContextAccess(IElementBehaviorSite * pSite);
  320. CContextAccess(IHTMLElement * pElement);
  321. ~CContextAccess();
  322. HRESULT Open(DWORD dwAccess);
  323. inline IElementBehaviorSite * Site() { Assert (_pSite && "_pSite opened?"); return _pSite; };
  324. inline IElementBehaviorSiteOM * SiteOM() { Assert (_pSiteOM && "_pSiteOM opened?"); return _pSiteOM; };
  325. inline IElementBehaviorSiteRender * SiteRender() { Assert (_pSiteRender && "_pSiteRender opened?"); return _pSiteRender; };
  326. inline IHTMLElement * Elem() { Assert (_pElem && "_pElem opened?"); return _pElem; };
  327. inline IHTMLElement2 * Elem2() { Assert (_pElem2 && "_pElem2 opened?"); return _pElem2; };
  328. inline IHTMLElement3 * Elem3() { Assert (_pElem3 && "_pElem3 opened?"); return _pElem3; };
  329. inline IHTMLStyle * Style() { Assert (_pStyle && "_pStyle opened?"); return _pStyle; };
  330. inline IHTMLStyle2 * Style2() { Assert (_pStyle2 && "_pStyle2 opened?"); return _pStyle2; };
  331. inline IHTMLStyle3 * Style3() { Assert (_pStyle3 && "_pStyle3 opened?"); return _pStyle3; };
  332. inline IHTMLElementDefaults * Defaults() { Assert (_pDefaults && "_pDefaults opened?"); return _pDefaults; };
  333. inline IHTMLStyle * DefStyle() { Assert (_pDefStyle && "_pDefStyle opened?"); return _pDefStyle; };
  334. inline IHTMLStyle2 * DefStyle2() { Assert (_pDefStyle2 && "_pDefStyle2 opened?"); return _pDefStyle2; };
  335. inline IHTMLStyle3 * DefStyle3() { Assert (_pDefStyle3 && "_pDefStyle3 opened?"); return _pDefStyle3; };
  336. #if DBG == 1
  337. static HRESULT DbgTest(IElementBehaviorSite * pSite);
  338. #endif
  339. private:
  340. //
  341. // data
  342. //
  343. DWORD _dwAccess;
  344. IElementBehaviorSite * _pSite;
  345. IElementBehaviorSiteOM2 * _pSiteOM;
  346. IElementBehaviorSiteRender * _pSiteRender;
  347. IHTMLElement * _pElem;
  348. IHTMLElement2 * _pElem2;
  349. IHTMLElement3 * _pElem3;
  350. IHTMLStyle * _pStyle;
  351. IHTMLStyle2 * _pStyle2;
  352. IHTMLStyle3 * _pStyle3;
  353. IHTMLElementDefaults * _pDefaults;
  354. IHTMLStyle * _pDefStyle;
  355. IHTMLStyle2 * _pDefStyle2;
  356. IHTMLStyle3 * _pDefStyle3;
  357. };
  358. //+------------------------------------------------------------------------
  359. //
  360. // event object access
  361. //
  362. //-------------------------------------------------------------------------
  363. enum EVENT_OBJECT_ACCESS
  364. {
  365. EOA_EVENTOBJ = 0x01,
  366. EOA_EVENTOBJ2 = 0x02,
  367. };
  368. #define EVENT_LEFTBUTTON 0x01
  369. #define EVENT_MIDDLEBUTTON 0x02
  370. #define EVENT_RIGHTBUTTON 0x04
  371. #define EVENT_SHIFTKEY 0x01
  372. #define EVENT_CTRLKEY 0x02
  373. #define EVENT_ALTKEY 0x04
  374. class CEventObjectAccess
  375. {
  376. public:
  377. //
  378. // methods
  379. //
  380. CEventObjectAccess(DISPPARAMS * pDispParams);
  381. ~CEventObjectAccess();
  382. HRESULT Open(DWORD dwAccess);
  383. inline IHTMLEventObj * EventObj() { Assert (_pEventObj && "_pEventObj opened?"); return _pEventObj; };
  384. inline IHTMLEventObj2 * EventObj2() { Assert (_pEventObj2 && "_pEventObj opened?"); return _pEventObj2; };
  385. HRESULT GetScreenCoordinates(POINT * ppt);
  386. HRESULT GetWindowCoordinates(POINT * ppt);
  387. HRESULT GetParentCoordinates(POINT * ppt);
  388. HRESULT GetKeyCode(long * pl);
  389. HRESULT GetMouseButtons(long * pl);
  390. HRESULT GetKeyboardStatus(long * pl);
  391. HRESULT InitializeEventProperties();
  392. // NOTE: You should not access these directly. They may not be initialized.
  393. private:
  394. struct EventProperties
  395. {
  396. POINT ptScreen;
  397. POINT ptClient;
  398. POINT ptElem;
  399. long lKey;
  400. long lMouseButtons:3;
  401. long lKeys:3;
  402. } _EventProperties;
  403. BOOL _fEventPropertiesInitialized;
  404. DISPPARAMS * _pDispParams;
  405. IHTMLEventObj * _pEventObj;
  406. IHTMLEventObj2 * _pEventObj2;
  407. };
  408. #endif //__UTILS_HXX__