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.

231 lines
5.5 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. REGCRC.CPP
  5. Abstract:
  6. History:
  7. --*/
  8. #include "precomp.h"
  9. #include "regcrc.h"
  10. HRESULT CRegCRC::ComputeValueCRC(HKEY hKey, LPCTSTR szValueName,
  11. DWORD dwPrevCRC, DWORD& dwNewCRC)
  12. {
  13. dwNewCRC = dwPrevCRC;
  14. // Get the size of the value
  15. // =========================
  16. DWORD dwSize = 0;
  17. long lRes = RegQueryValueEx(hKey, szValueName, NULL, NULL, NULL, &dwSize);
  18. if(lRes)
  19. {
  20. return S_FALSE;
  21. }
  22. // Get the actual value
  23. // ====================
  24. BYTE* pBuffer = new BYTE[dwSize];
  25. if ( pBuffer == NULL )
  26. return WBEM_E_OUT_OF_MEMORY;
  27. DWORD dwType;
  28. lRes = RegQueryValueEx(hKey, szValueName, NULL, &dwType,
  29. pBuffer, &dwSize);
  30. if(lRes)
  31. {
  32. return S_FALSE;
  33. }
  34. // Hash the type
  35. // =============
  36. dwNewCRC = UpdateCRC32((BYTE*)&dwType, sizeof(DWORD), dwNewCRC);
  37. // Hash the data
  38. // =============
  39. dwNewCRC = UpdateCRC32(pBuffer, dwSize, dwNewCRC);
  40. delete [] pBuffer;
  41. return S_OK;
  42. }
  43. HRESULT CRegCRC::ComputeKeyValuesCRC(HKEY hKey, DWORD dwPrevCRC,
  44. DWORD& dwNewCRC)
  45. {
  46. dwNewCRC = dwPrevCRC;
  47. // Get maximum value length
  48. // ========================
  49. DWORD dwNumValues, dwMaxValueLen;
  50. long lRes = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
  51. &dwNumValues, &dwMaxValueLen, NULL, NULL, NULL);
  52. if(lRes && lRes != ERROR_INSUFFICIENT_BUFFER)
  53. {
  54. return E_FAIL;
  55. }
  56. // Enuremate all the values
  57. // ========================
  58. for(DWORD dwIndex = 0; dwIndex < dwNumValues; dwIndex++)
  59. {
  60. TCHAR* szName = new TCHAR[dwMaxValueLen + 1];
  61. if ( szName == NULL )
  62. return WBEM_E_OUT_OF_MEMORY;
  63. DWORD dwLen = dwMaxValueLen + 1;
  64. long lRes = RegEnumValue(hKey, dwIndex, szName, &dwLen, NULL,
  65. NULL, NULL, NULL);
  66. if(lRes)
  67. {
  68. delete [] szName;
  69. continue;
  70. }
  71. // Hash the name
  72. // =============
  73. dwNewCRC = UpdateCRC32((LPBYTE)szName, lstrlen(szName), dwNewCRC);
  74. // Hash the value
  75. // ==============
  76. ComputeValueCRC(hKey, szName, dwNewCRC, dwNewCRC);
  77. delete [] szName;
  78. }
  79. return S_OK;
  80. }
  81. HRESULT CRegCRC::ComputeKeyCRC(HKEY hKey, DWORD dwPrevCRC,
  82. DWORD& dwNewCRC)
  83. {
  84. HRESULT hres = ComputeKeyValuesCRC(hKey, dwPrevCRC, dwNewCRC);
  85. // Get maximum subkey length
  86. // =========================
  87. DWORD dwNumKeys, dwMaxKeyLen;
  88. long lRes = RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwNumKeys,
  89. &dwMaxKeyLen, NULL, NULL,
  90. NULL, NULL, NULL, NULL);
  91. if(lRes && lRes != ERROR_INSUFFICIENT_BUFFER)
  92. {
  93. return E_FAIL;
  94. }
  95. // Enuremate all the subkeys
  96. // =========================
  97. for(DWORD dwIndex = 0; dwIndex < dwNumKeys; dwIndex++)
  98. {
  99. TCHAR* szName = new TCHAR[dwMaxKeyLen + 1];
  100. if ( szName == NULL )
  101. return WBEM_E_OUT_OF_MEMORY;
  102. DWORD dwLen = dwMaxKeyLen + 1;
  103. long lRes = RegEnumKeyEx(hKey, dwIndex, szName, &dwLen, NULL,
  104. NULL, NULL, NULL);
  105. if(lRes)
  106. {
  107. delete [] szName;
  108. continue;
  109. }
  110. // Hash the name
  111. // =============
  112. dwNewCRC = UpdateCRC32((LPBYTE)szName, lstrlen(szName), dwNewCRC);
  113. delete [] szName;
  114. }
  115. return S_OK;
  116. }
  117. HRESULT CRegCRC::ComputeTreeCRC(HKEY hKey, DWORD dwPrevCRC, DWORD& dwNewCRC)
  118. {
  119. dwNewCRC = dwPrevCRC;
  120. // Compute this key's CRC
  121. // ======================
  122. HRESULT hres = ComputeKeyValuesCRC(hKey, dwNewCRC, dwNewCRC);
  123. if(FAILED(hres)) return hres;
  124. // Get maximum subkey length
  125. // =========================
  126. DWORD dwNumKeys, dwMaxKeyLen;
  127. long lRes = RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwNumKeys,
  128. &dwMaxKeyLen, NULL, NULL,
  129. NULL, NULL, NULL, NULL);
  130. if(lRes && lRes != ERROR_INSUFFICIENT_BUFFER)
  131. {
  132. return E_FAIL;
  133. }
  134. // Enuremate all the subkeys
  135. // =========================
  136. for(DWORD dwIndex = 0; dwIndex < dwNumKeys; dwIndex++)
  137. {
  138. TCHAR* szName = new TCHAR[dwMaxKeyLen + 1];
  139. if ( szName == NULL )
  140. return WBEM_E_OUT_OF_MEMORY;
  141. DWORD dwLen = dwMaxKeyLen + 1;
  142. long lRes = RegEnumKeyEx(hKey, dwIndex, szName, &dwLen, NULL,
  143. NULL, NULL, NULL);
  144. if(lRes)
  145. {
  146. delete [] szName;
  147. continue;
  148. }
  149. // Hash the name
  150. // =============
  151. dwNewCRC = UpdateCRC32((LPBYTE)szName, lstrlen(szName), dwNewCRC);
  152. // Open the subkey
  153. // ===============
  154. HKEY hChild;
  155. lRes = RegOpenKeyEx(hKey, szName, 0, KEY_READ, &hChild);
  156. delete [] szName;
  157. if(lRes)
  158. {
  159. continue;
  160. }
  161. else
  162. {
  163. // Hash the value
  164. // ==============
  165. ComputeTreeCRC(hChild, dwNewCRC, dwNewCRC);
  166. RegCloseKey(hChild);
  167. }
  168. }
  169. return S_OK;
  170. }