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.

391 lines
12 KiB

  1. //+----------------------------------------------------------------------------
  2. // File: util.hxx
  3. //
  4. // Synopsis: This file contains utility definitions
  5. //
  6. //-----------------------------------------------------------------------------
  7. #ifndef _UTIL_HXX
  8. #define _UTIL_HXX
  9. // Globals --------------------------------------------------------------------
  10. extern const UCHAR SZ_NEWLINE[];
  11. const ULONG CB_NEWLINE = (2 * sizeof(UCHAR));
  12. // Prototypes -----------------------------------------------------------------
  13. #ifdef _NOCRT
  14. extern "C" size_t __cdecl _tcslen(const TCHAR *);
  15. extern "C" int __cdecl memcmp(const void *, const void *, size_t);
  16. extern "C" void * __cdecl memcpy(void *, const void *, size_t);
  17. extern "C" void * __cdecl memset(void *, int, size_t);
  18. extern "C" void * __cdecl memmove(void *, const void *, size_t);
  19. #endif
  20. inline void * _cdecl operator new(size_t cb) { return HeapAlloc(g_heap, 0, cb); }
  21. inline void * _cdecl operator new(size_t , void * pv) { return pv; }
  22. inline void _cdecl operator delete(void * pv) { HeapFree(g_heap, 0, pv); }
  23. inline SIZE_T MemSize(void * pv) { return HeapSize(g_heap, 0, pv); }
  24. inline void * MemReAlloc(void * pv, SIZE_T cb) { return HeapReAlloc(g_heap, 0, pv, cb); }
  25. void * CoAlloc(ULONG cb);
  26. void CoFree(void * pv);
  27. ULONG CoGetSize(void * pv);
  28. BOOL CoDidAlloc(void * pv);
  29. HRESULT CopyStream(IStream * pstmDest, IStream * pstmSrc, ULARGE_INTEGER cb,
  30. ULARGE_INTEGER * pcbRead, ULARGE_INTEGER * pcbWritten);
  31. //+----------------------------------------------------------------------------
  32. // Class: CAry
  33. //
  34. // Synopsis:
  35. //
  36. //-----------------------------------------------------------------------------
  37. template <class TYPE> class CAry
  38. {
  39. public:
  40. CAry();
  41. CAry(int cItems, TYPE * pItems);
  42. ~CAry();
  43. operator TYPE *() { return _pItems; }
  44. HRESULT Append(TYPE * pitem);
  45. void Delete(int iItem);
  46. void DeleteByValue(TYPE item);
  47. HRESULT EnsureSize(int cItems);
  48. int Find(TYPE item);
  49. HRESULT Insert(int iItem, TYPE item);
  50. HRESULT SetSize(int cItems);
  51. int Size() { return _cItems; }
  52. protected:
  53. int _cItems;
  54. TYPE * _pItems;
  55. };
  56. #define DEFINE_ARY(x) typedef CAry<x> CAry##x
  57. //+----------------------------------------------------------------------------
  58. // Class: CMemoryStream
  59. //
  60. // Synopsis:
  61. //
  62. //-----------------------------------------------------------------------------
  63. class CMemoryStream : public CComponent,
  64. public IStream
  65. {
  66. public:
  67. CMemoryStream() { _cbSize = 0; _ibPos = 0; _pbData = NULL; }
  68. ~CMemoryStream() { delete [] _pbData; }
  69. // IUnknown methods
  70. DEFINE_IUNKNOWN_METHODS;
  71. // IStream methods
  72. STDMETHOD(Read)(void * pv, ULONG cb, ULONG * pcbRead);
  73. STDMETHOD(Write)(const void * pv, ULONG cb, ULONG * pcbWritten);
  74. STDMETHOD(Seek)(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER * plibNewPosition);
  75. STDMETHOD(SetSize)(ULARGE_INTEGER libNewSize);
  76. STDMETHOD(CopyTo)(IStream * pstm, ULARGE_INTEGER cb, ULARGE_INTEGER * pcbRead, ULARGE_INTEGER * pcbWritten);
  77. STDMETHOD(Commit)(DWORD) { return E_NOTIMPL; }
  78. STDMETHOD(Revert)() { return E_NOTIMPL; }
  79. STDMETHOD(LockRegion)(ULARGE_INTEGER, ULARGE_INTEGER, DWORD) { return E_NOTIMPL; }
  80. STDMETHOD(UnlockRegion)(ULARGE_INTEGER, ULARGE_INTEGER, DWORD) { return E_NOTIMPL; }
  81. STDMETHOD(Stat)(STATSTG *, DWORD) { return E_NOTIMPL; }
  82. STDMETHOD(Clone)(IStream **) { return E_NOTIMPL; }
  83. private:
  84. ULONG _cbSize;
  85. ULONG _ibPos;
  86. BYTE * _pbData;
  87. };
  88. //+----------------------------------------------------------------------------
  89. // Class: CBufferedStream
  90. //
  91. // Synopsis:
  92. //
  93. //-----------------------------------------------------------------------------
  94. class CBufferedStream : public CComponent,
  95. public IStream
  96. {
  97. public:
  98. CBufferedStream(IStream * pstm, ULONG cbNewLine = 0, BOOL fRead = TRUE);
  99. ~CBufferedStream();
  100. HRESULT Flush(ULONG * pcbWritten = NULL);
  101. ULONG GetTotalWritten() { return _cbTotal; }
  102. HRESULT Load();
  103. HRESULT SetBufferSize(ULONG cb);
  104. // IUnknown methods
  105. DEFINE_IUNKNOWN_METHODS;
  106. // IStream methods
  107. STDMETHOD(Read)(void * pv, ULONG cb, ULONG * pcbRead);
  108. STDMETHOD(Write)(const void * pv, ULONG cb, ULONG * pcbWritten);
  109. STDMETHOD(Seek)(LARGE_INTEGER , DWORD , ULARGE_INTEGER *) { return E_NOTIMPL; }
  110. STDMETHOD(SetSize)(ULARGE_INTEGER) { return E_NOTIMPL; }
  111. STDMETHOD(CopyTo)(IStream *, ULARGE_INTEGER, ULARGE_INTEGER *, ULARGE_INTEGER *) { return E_NOTIMPL; }
  112. STDMETHOD(Commit)(DWORD) { return E_NOTIMPL; }
  113. STDMETHOD(Revert)() { return E_NOTIMPL; }
  114. STDMETHOD(LockRegion)(ULARGE_INTEGER, ULARGE_INTEGER, DWORD) { return E_NOTIMPL; }
  115. STDMETHOD(UnlockRegion)(ULARGE_INTEGER, ULARGE_INTEGER, DWORD) { return E_NOTIMPL; }
  116. STDMETHOD(Stat)(STATSTG *, DWORD) { return E_NOTIMPL; }
  117. STDMETHOD(Clone)(IStream **) { return E_NOTIMPL; }
  118. private:
  119. BOOL _fRead;
  120. BYTE * _pb;
  121. ULONG _cb;
  122. ULONG _ib;
  123. ULONG _cbLine;
  124. ULONG _cbNewLine;
  125. ULONG _cbTotal;
  126. IStream * _pstm;
  127. };
  128. //+----------------------------------------------------------------------------
  129. // Class: CFileStream
  130. //
  131. // Synopsis:
  132. //
  133. //-----------------------------------------------------------------------------
  134. class CFileStream : public CComponent,
  135. public IStream
  136. {
  137. typedef CComponent parent;
  138. public:
  139. CFileStream();
  140. ~CFileStream();
  141. HRESULT Init(LPCWSTR szFileName,
  142. DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE, // Access (read-write) mode
  143. DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE, // Share mode (default to exclusive)
  144. LPSECURITY_ATTRIBUTES pSecurityAttributes = NULL, // Pointer to security descriptor
  145. DWORD dwCreationDistribution = OPEN_ALWAYS, // How to create
  146. DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL, // File attributes
  147. HANDLE hTemplateFile = NULL); // Handle of template file
  148. HRESULT GetFileSize(ULONG * pcbSize);
  149. // IUnknown methods
  150. DEFINE_IUNKNOWN_METHODS;
  151. // IStream methods
  152. STDMETHOD(Read)(void * pv, ULONG cb, ULONG * pcbRead);
  153. STDMETHOD(Write)(const void * pv, ULONG cb, ULONG * pcbWritten);
  154. STDMETHOD(Seek)(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER * plibNewPosition);
  155. STDMETHOD(SetSize)(ULARGE_INTEGER libNewSize);
  156. STDMETHOD(CopyTo)(IStream * pstm, ULARGE_INTEGER cb, ULARGE_INTEGER * pcbRead, ULARGE_INTEGER * pcbWritten);
  157. STDMETHOD(Commit)(DWORD) { return E_NOTIMPL; }
  158. STDMETHOD(Revert)() { return E_NOTIMPL; }
  159. STDMETHOD(LockRegion)(ULARGE_INTEGER, ULARGE_INTEGER, DWORD) { return E_NOTIMPL; }
  160. STDMETHOD(UnlockRegion)(ULARGE_INTEGER, ULARGE_INTEGER, DWORD) { return E_NOTIMPL; }
  161. STDMETHOD(Stat)(STATSTG *, DWORD) { return E_NOTIMPL; }
  162. STDMETHOD(Clone)(IStream **) { return E_NOTIMPL; }
  163. private:
  164. HANDLE _hFile;
  165. HRESULT PrivateQueryInterface(REFIID riid, void ** ppvObj);
  166. };
  167. // Inlines --------------------------------------------------------------------
  168. //+----------------------------------------------------------------------------
  169. // Function: CAry<TYPE>
  170. //
  171. // Synopsis:
  172. //
  173. //-----------------------------------------------------------------------------
  174. template <class TYPE>
  175. CAry<TYPE>::CAry()
  176. {
  177. _cItems = 0;
  178. _pItems = NULL;
  179. }
  180. template <class TYPE>
  181. CAry<TYPE>::CAry(
  182. int cItems,
  183. TYPE * pItems)
  184. {
  185. SetSize(cItems);
  186. if (pItems)
  187. {
  188. ::memcpy(_pItems, pItems, (cItems)*sizeof(TYPE));
  189. _cItems = cItems;
  190. }
  191. }
  192. template <class TYPE>
  193. CAry<TYPE>::~CAry()
  194. {
  195. // Delete the array memory
  196. // (The check for NULL is necessary since the "real" pointer is one word
  197. // before the address kept in _pItems)
  198. if (_pItems)
  199. {
  200. delete [] (((INT_PTR *)_pItems)-1);
  201. }
  202. }
  203. //+----------------------------------------------------------------------------
  204. // Function: Append
  205. //
  206. // Synopsis:
  207. //
  208. //-----------------------------------------------------------------------------
  209. template <class TYPE> HRESULT
  210. CAry<TYPE>::Append(
  211. TYPE * pitem)
  212. {
  213. HRESULT hr = EnsureSize(_cItems);
  214. if (!hr)
  215. {
  216. _pItems[_cItems++] = *pitem;
  217. }
  218. return hr;
  219. }
  220. //+----------------------------------------------------------------------------
  221. // Function: Delete
  222. //
  223. // Synopsis:
  224. //
  225. //-----------------------------------------------------------------------------
  226. template <class TYPE> void
  227. CAry<TYPE>::Delete(
  228. int iItem)
  229. {
  230. Assert(iItem < _cItems);
  231. _cItems--;
  232. ::memmove(_pItems[iItem], _pItems[iItem+1], (_cItems-iItem)*sizeof(TYPE));
  233. }
  234. //+----------------------------------------------------------------------------
  235. // Function: DeleteByValue
  236. //
  237. // Synopsis:
  238. //
  239. //-----------------------------------------------------------------------------
  240. template <class TYPE> void
  241. CAry<TYPE>::DeleteByValue(
  242. TYPE item)
  243. {
  244. int iItem = Find(item);
  245. if (iItem >= 0)
  246. Delete(iItem);
  247. }
  248. //+----------------------------------------------------------------------------
  249. // Function: EnsureSize
  250. //
  251. // Synopsis:
  252. //
  253. //-----------------------------------------------------------------------------
  254. template <class TYPE> HRESULT
  255. CAry<TYPE>::EnsureSize(
  256. int cItems)
  257. {
  258. int cItemsMax = (int)(!_pItems
  259. ? 0
  260. : *(((INT_PTR *)_pItems)-1));
  261. if (cItems > cItemsMax)
  262. {
  263. TYPE * pItems = new TYPE[cItems+1];
  264. if (!pItems)
  265. return E_OUTOFMEMORY;
  266. if (_pItems)
  267. {
  268. ::memcpy(((INT_PTR *)pItems)+1, _pItems, (cItemsMax)*sizeof(TYPE));
  269. delete [] (((INT_PTR *)_pItems)-1);
  270. }
  271. *((INT_PTR *)pItems) = cItems;
  272. _pItems = (TYPE *)(((INT_PTR *)pItems)+1);
  273. }
  274. return S_OK;
  275. }
  276. //+----------------------------------------------------------------------------
  277. // Function: Find
  278. //
  279. // Synopsis:
  280. //
  281. //-----------------------------------------------------------------------------
  282. template <class TYPE> int
  283. CAry<TYPE>::Find(
  284. TYPE item)
  285. {
  286. int iItem;
  287. for (iItem=_cItems-1; iItem >= 0; iItem--)
  288. {
  289. if (_pItems[iItem] == item)
  290. break;
  291. }
  292. return iItem;
  293. }
  294. //+----------------------------------------------------------------------------
  295. // Function: Insert
  296. //
  297. // Synopsis:
  298. //
  299. //-----------------------------------------------------------------------------
  300. template <class TYPE> HRESULT
  301. CAry<TYPE>::Insert(
  302. int iItem,
  303. TYPE item)
  304. {
  305. HRESULT hr = EnsureSize(_cItems);
  306. if (!hr)
  307. {
  308. if (iItem < _cItems)
  309. {
  310. ::memmove(_pItems[iItem+1], _pItems[iItem], (_cItems-iItem)*sizeof(TYPE));
  311. }
  312. _cItems++;
  313. _pItems[iItem] = item;
  314. }
  315. return hr;
  316. }
  317. //+----------------------------------------------------------------------------
  318. // Function: SetSize
  319. //
  320. // Synopsis:
  321. //
  322. //-----------------------------------------------------------------------------
  323. template <class TYPE> HRESULT
  324. CAry<TYPE>::SetSize(
  325. int cItems)
  326. {
  327. HRESULT hr = EnsureSize(cItems);
  328. if (hr)
  329. return hr;
  330. _cItems = cItems;
  331. return S_OK;
  332. }
  333. #endif // _UTIL_HXX