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.

309 lines
6.2 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. utilities.h
  5. Abstract:
  6. SIS Groveler general utilities include file
  7. Authors:
  8. Cedric Krumbein, 1998
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. /************************ General-purpose definitions ************************/
  14. typedef DWORDLONG Signature;
  15. typedef LONGLONG PerfTime;
  16. #define Clear(OBJECT) \
  17. memset(&(OBJECT), 0, sizeof(OBJECT))
  18. #define IsSet(EVENT) \
  19. ((EVENT) != NULL && WaitForSingleObject(EVENT, 0) == WAIT_OBJECT_0)
  20. #define IsReset(EVENT) \
  21. ((EVENT) != NULL && WaitForSingleObject(EVENT, 0) == WAIT_TIMEOUT)
  22. #define ROTATE_LEFT(DATA, NUM_BITS) \
  23. ((DATA) << (NUM_BITS) | (DATA) >> (sizeof(DATA)*8 - (NUM_BITS)))
  24. #define ROTATE_RIGHT(DATA, NUM_BITS) \
  25. ((DATA) >> (NUM_BITS) | (DATA) << (sizeof(DATA)*8 - (NUM_BITS)))
  26. /************************ Utility function prototypes ************************/
  27. #define PERF_TIME_TO_MSEC(VALUE) PerformanceTimeToMSec(VALUE)
  28. #define PERF_TIME_TO_USEC(VALUE) PerformanceTimeToUSec(VALUE)
  29. PerfTime GetPerformanceTime();
  30. DWORD PerformanceTimeToMSec(PerfTime timeInterval);
  31. LONGLONG PerformanceTimeToUSec(PerfTime timeInterval);
  32. DWORDLONG GetTime();
  33. TCHAR *PrintTime(TCHAR *string,
  34. DWORDLONG time);
  35. DWORDLONG GetFileID(const TCHAR *fileName);
  36. BOOL GetCSIndex(HANDLE fileHandle,
  37. CSID *csIndex);
  38. //
  39. // A class to handle arbitrary length pathnames, as returned by NtQueryInformationFile()
  40. //
  41. class TFileName {
  42. public:
  43. ULONG nameLenMax; // maximum length of name
  44. ULONG nameLen; // actual length of name (not including NULL terminator)
  45. TCHAR *name; // file name (ptr to nameInfo->FileName)
  46. FILE_NAME_INFORMATION *nameInfo; // required by NtQueryInformationFile
  47. ULONG nameInfoSize; // sizeof nameInfo buffer
  48. TFileName(void) : nameLenMax(0), nameLen(0), name(NULL), nameInfo(NULL), nameInfoSize(0) {}
  49. ~TFileName() {
  50. if (nameInfo)
  51. delete[] nameInfo;
  52. }
  53. void resize(int size = 900) {
  54. if (nameInfo)
  55. delete[] nameInfo;
  56. allocBuf(size);
  57. }
  58. void append(const TCHAR *s, int c = -1) {
  59. int slen;
  60. int n;
  61. if (0 == c || NULL == s)
  62. return;
  63. slen = _tcslen(s);
  64. if (-1 == c)
  65. n = slen;
  66. else
  67. n = min(slen, c);
  68. // If the combined size of the two strings is larger than our buffer,
  69. // realloc the buffer.
  70. if (nameLen + n + 1 > nameLenMax) {
  71. FILE_NAME_INFORMATION *ni = nameInfo;
  72. allocBuf(nameLen + n + 1 + 512);
  73. if (ni) {
  74. _tcsncpy(name, ni->FileName, nameLen);
  75. delete[] ni;
  76. }
  77. }
  78. _tcsncpy(&name[nameLen], s, n);
  79. nameLen += n;
  80. name[nameLen] = _T('\0');
  81. }
  82. void assign(const TCHAR *s, int c = -1) {
  83. if (nameLenMax > 0) {
  84. nameLen = 0;
  85. name[0] = _T('\0');
  86. }
  87. append(s, c);
  88. }
  89. private:
  90. // Allocate a buffer for nameInfo of the specified size. Note that name will
  91. // point into this buffer.
  92. void allocBuf(int size) {
  93. ASSERT(size >= 0);
  94. nameLenMax = size;
  95. nameLen = 0;
  96. if (size > 0) {
  97. nameInfoSize = size * sizeof(TCHAR) + sizeof(ULONG);
  98. nameInfo = (PFILE_NAME_INFORMATION) new BYTE[nameInfoSize + sizeof FILE_NAME_INFORMATION]; // conservative size
  99. ASSERT(nameInfo); // new_handler should raise exception on out of memory
  100. ASSERT((((ULONG_PTR) nameInfo) % sizeof(ULONG)) == 0); // make sure alignment is correct
  101. name = (TCHAR *) nameInfo->FileName;
  102. name[0] = _T('\0');
  103. ASSERT(((UINT_PTR) &nameInfo->FileName[size] - (UINT_PTR) nameInfo) == nameInfoSize);
  104. } else {
  105. nameInfo = NULL;
  106. name = NULL;
  107. nameInfoSize = 0;
  108. }
  109. }
  110. };
  111. BOOL GetFileName(
  112. HANDLE fileHandle,
  113. TFileName *tFileName);
  114. BOOL GetFileName(
  115. HANDLE volumeHandle,
  116. DWORDLONG fileID,
  117. TFileName *tFileName);
  118. TCHAR *GetCSName(CSID *csIndex);
  119. VOID FreeCSName(TCHAR *rpcStr);
  120. Signature Checksum(
  121. const VOID *buffer,
  122. DWORD bufferLen,
  123. DWORDLONG offset,
  124. Signature firstWord);
  125. /*********************** Hash table class declaration ************************/
  126. #define TABLE_MIN_LOAD 4
  127. #define TABLE_MAX_LOAD 5
  128. #define TABLE_RANDOM_CONSTANT 314159269
  129. #define TABLE_RANDOM_PRIME 1000000007
  130. #define TABLE_DIR_SIZE 256
  131. #define TABLE_SEGMENT_BITS 8
  132. #define TABLE_SEGMENT_SIZE (1U << TABLE_SEGMENT_BITS)
  133. #define TABLE_SEGMENT_MASK (TABLE_SEGMENT_SIZE - 1U)
  134. class Table {
  135. private:
  136. struct TableEntry {
  137. TableEntry *prevEntry,
  138. *nextEntry,
  139. *prevChain,
  140. *nextChain;
  141. DWORD hashValue,
  142. keyLen;
  143. VOID *data;
  144. } *firstEntry,
  145. *lastEntry;
  146. DWORD numBuckets,
  147. dirSize,
  148. expandIndex,
  149. level,
  150. numEntries;
  151. struct TableSegment {
  152. TableEntry *slot[TABLE_SEGMENT_SIZE];
  153. } **directory;
  154. DWORD Hash(const VOID *key,
  155. DWORD keyLen) const;
  156. DWORD BucketNum(DWORD hashValue) const;
  157. VOID Expand();
  158. VOID Contract();
  159. public:
  160. Table();
  161. ~Table();
  162. BOOL Put(
  163. VOID *data,
  164. DWORD keyLen);
  165. VOID *Get(const VOID *key,
  166. DWORD keyLen,
  167. BOOL erase = FALSE);
  168. VOID *GetFirst(DWORD *keyLen = NULL,
  169. BOOL erase = TRUE);
  170. DWORD Number() const;
  171. };
  172. /************************** FIFO class declaration ***************************/
  173. class FIFO {
  174. private:
  175. struct FIFOEntry {
  176. FIFOEntry *next;
  177. DWORD size;
  178. VOID *data;
  179. } *head, *tail;
  180. DWORD numEntries;
  181. public:
  182. FIFO();
  183. ~FIFO();
  184. VOID Put(VOID *data);
  185. VOID *Get();
  186. DWORD Number() const;
  187. };
  188. /************************** LIFO class declaration ***************************/
  189. class LIFO {
  190. private:
  191. struct LIFOEntry {
  192. LIFOEntry *next;
  193. DWORD size;
  194. VOID *data;
  195. } *top;
  196. DWORD numEntries;
  197. public:
  198. LIFO();
  199. ~LIFO();
  200. VOID Put(VOID *data);
  201. VOID *Get();
  202. DWORD Number() const;
  203. };
  204. BOOL GetParentName(const TCHAR *fileName,
  205. TFileName *parentName);