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.

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