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.

255 lines
4.8 KiB

  1. /*
  2. * C R C S Z . H
  3. *
  4. * CRC Implementation
  5. *
  6. * Copyright 1986-1997 Microsoft Corporation, All Rights Reserved
  7. */
  8. #ifndef _CRCSZ_H_
  9. #define _CRCSZ_H_
  10. // Case Sensitive CRC'd string classes ---------------------------------------
  11. //
  12. // Encapsulation of a CRC'ed string. Use this class as the key type for
  13. // your cache class where a string key is called for. The benefit is
  14. // increased cache search performance because a full string compare
  15. // is done only when the CRCs match (which typically happens only
  16. // for the string you're looking for).
  17. //
  18. class CRCSz
  19. {
  20. public:
  21. DWORD m_dwCRC;
  22. LPCSTR m_lpsz;
  23. CRCSz(LPCSTR psz) :
  24. m_lpsz(psz),
  25. m_dwCRC(DwComputeCRC(0,
  26. const_cast<CHAR *>(psz),
  27. static_cast<UINT>(strlen(psz))))
  28. {
  29. }
  30. // operators for use with the hash cache
  31. //
  32. int hash( const int rhs ) const
  33. {
  34. return (m_dwCRC % rhs);
  35. }
  36. bool isequal( const CRCSz& rhs ) const
  37. {
  38. return ((m_dwCRC == rhs.m_dwCRC) &&
  39. !strcmp( m_lpsz, rhs.m_lpsz ));
  40. }
  41. };
  42. class CRCWszN
  43. {
  44. public:
  45. UINT m_cch;
  46. DWORD m_dwCRC;
  47. LPCWSTR m_pwsz;
  48. CRCWszN(LPCWSTR pwsz, UINT cch) :
  49. m_cch(cch),
  50. m_pwsz(pwsz),
  51. m_dwCRC(DwComputeCRC (0,
  52. const_cast<WCHAR *>(pwsz),
  53. cch * sizeof(WCHAR)))
  54. {
  55. }
  56. // operators for use with the hash cache
  57. //
  58. int hash( const int rhs ) const
  59. {
  60. return (m_dwCRC % rhs);
  61. }
  62. bool isequal( const CRCWszN& rhs ) const
  63. {
  64. return ((m_cch == rhs.m_cch) &&
  65. (m_dwCRC == rhs.m_dwCRC) &&
  66. !wcsncmp( m_pwsz, rhs.m_pwsz, m_cch ));
  67. }
  68. };
  69. class CRCWsz : public CRCWszN
  70. {
  71. // Not Implemented
  72. //
  73. CRCWsz();
  74. public:
  75. CRCWsz(LPCWSTR pwsz) :
  76. CRCWszN(pwsz, static_cast<UINT>(wcslen(pwsz)))
  77. {
  78. }
  79. CRCWsz( const CRCWszN& rhs ) :
  80. CRCWszN (rhs)
  81. {
  82. }
  83. };
  84. // Case Insensitive CRC'd string classes -------------------------------------
  85. //
  86. // Encapsulation of a CRC'ed string. Use this class as the key type for
  87. // your cache class where a string key is called for. The benefit is
  88. // increased cache search performance because a full string compare
  89. // is done only when the CRCs match (which typically happens only
  90. // for the string you're looking for).
  91. //
  92. class CRCSzi
  93. {
  94. public:
  95. DWORD m_dwCRC;
  96. LPCSTR m_lpsz;
  97. CRCSzi( LPCSTR lpsz ) :
  98. m_lpsz(lpsz)
  99. {
  100. UINT cch = static_cast<UINT>(strlen(lpsz));
  101. CHAR lpszLower[128];
  102. // Note that the CRC only is taken from the first 127 characters.
  103. //
  104. cch = (UINT)min(cch, sizeof(lpszLower) - 1);
  105. CopyMemory(lpszLower, lpsz, cch);
  106. lpszLower[cch] = 0;
  107. _strlwr(lpszLower);
  108. m_dwCRC = DwComputeCRC (0, const_cast<CHAR *>(lpszLower), cch);
  109. }
  110. // operators for use with the hash cache
  111. //
  112. int hash( const int rhs ) const
  113. {
  114. return (m_dwCRC % rhs);
  115. }
  116. bool isequal( const CRCSzi& rhs ) const
  117. {
  118. return ((m_dwCRC == rhs.m_dwCRC) &&
  119. !lstrcmpiA( m_lpsz, rhs.m_lpsz ));
  120. }
  121. };
  122. class CRCWsziN
  123. {
  124. public:
  125. UINT m_cch;
  126. DWORD m_dwCRC;
  127. LPCWSTR m_pwsz;
  128. CRCWsziN() :
  129. m_cch(0),
  130. m_dwCRC(0),
  131. m_pwsz(NULL)
  132. {}
  133. CRCWsziN(LPCWSTR pwsz, UINT cch) :
  134. m_cch(cch),
  135. m_pwsz(pwsz)
  136. {
  137. // Note that the CRC only is taken from the first 127 characters.
  138. //
  139. WCHAR pwszLower[128];
  140. UINT cb = sizeof(WCHAR) * min(cch, (sizeof(pwszLower)/sizeof(WCHAR)) - 1);
  141. CopyMemory(pwszLower, pwsz, cb);
  142. pwszLower[cb / sizeof(WCHAR)] = L'\0';
  143. _wcslwr(pwszLower);
  144. m_dwCRC = DwComputeCRC (0, const_cast<WCHAR *>(pwszLower), cb);
  145. }
  146. // operators for use with the hash cache
  147. //
  148. int hash( const int rhs ) const
  149. {
  150. return (m_dwCRC % rhs);
  151. }
  152. bool isequal( const CRCWsziN& rhs ) const
  153. {
  154. return ((m_cch == rhs.m_cch) &&
  155. (m_dwCRC == rhs.m_dwCRC) &&
  156. !_wcsnicmp( m_pwsz, rhs.m_pwsz, m_cch ));
  157. }
  158. };
  159. class CRCWszi : public CRCWsziN
  160. {
  161. public:
  162. CRCWszi()
  163. {}
  164. CRCWszi( LPCWSTR pwsz ) :
  165. CRCWsziN (pwsz, static_cast<UINT>(wcslen(pwsz)))
  166. {
  167. }
  168. // operators for use with list::sort
  169. //
  170. bool operator<( const CRCWszi& rhs ) const
  171. {
  172. INT lret = 1;
  173. if (m_dwCRC < rhs.m_dwCRC)
  174. return true;
  175. if (m_dwCRC == rhs.m_dwCRC)
  176. {
  177. lret = _wcsnicmp(m_pwsz,
  178. rhs.m_pwsz,
  179. min(m_cch, rhs.m_cch));
  180. }
  181. return (lret ? (lret < 0) : (m_cch < rhs.m_cch));
  182. }
  183. // operators for use with list::unique
  184. //
  185. bool operator==( const CRCWszi& rhs ) const
  186. {
  187. return isequal(rhs);
  188. }
  189. };
  190. class CRCWsziLI : public CRCWszi
  191. {
  192. public:
  193. LARGE_INTEGER m_li;
  194. bool m_fFullCompare;
  195. CRCWsziLI( LPCWSTR pwsz, LARGE_INTEGER li, bool fFullCompare ) :
  196. CRCWszi (pwsz),
  197. m_li(li),
  198. m_fFullCompare(fFullCompare)
  199. {
  200. }
  201. bool isequal( const CRCWsziLI& rhs ) const
  202. {
  203. bool fIsEqual;
  204. fIsEqual = CRCWszi::isequal(rhs);
  205. if (fIsEqual)
  206. {
  207. if (m_fFullCompare)
  208. {
  209. fIsEqual = (m_li.QuadPart == rhs.m_li.QuadPart);
  210. }
  211. }
  212. return fIsEqual;
  213. }
  214. };
  215. #endif // _CRCSZ_H_