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.

215 lines
5.3 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. reglist.c
  5. Abstract:
  6. This module contains routine for list manipulation
  7. Author:
  8. Dragos C. Sambotin (dragoss) 30-Dec-1998
  9. Revision History:
  10. --*/
  11. #include "chkreg.h"
  12. extern PUCHAR Base;
  13. extern FILE *OutputFile;
  14. extern BOOLEAN FixHive;
  15. extern UNKNOWN_LIST LostCells[];
  16. extern BOOLEAN LostSpace;
  17. VOID AddCellToUnknownList(HCELL_INDEX cellindex)
  18. /*
  19. Routine Description:
  20. Adds a cell to the list (pseudo) of the unknown cells.
  21. The list of unknown cells is in fact a primitive two-dimensional hash table.
  22. Always add at the begining. The caller is responsible not to add twice the same cell.
  23. Arguments:
  24. cellindex - supplies the cell index of the cell assumed as unknown.
  25. Return Value:
  26. NONE.
  27. */
  28. {
  29. if(LostSpace) {
  30. // only if we are interested in the lost space
  31. ULONG WhatList = (ULONG)cellindex % FRAGMENTATION;
  32. ULONG WhatIndex = (ULONG)cellindex % SUBLISTS;
  33. PUNKNOWN_CELL Tmp;
  34. Tmp = (PUNKNOWN_CELL)malloc(sizeof(UNKNOWN_CELL));
  35. if(Tmp) {
  36. Tmp->Cell = cellindex;
  37. Tmp->Next = LostCells[WhatList].List[WhatIndex];
  38. LostCells[WhatList].List[WhatIndex] = Tmp;
  39. LostCells[WhatList].Count++;
  40. }
  41. }
  42. }
  43. VOID RemoveCellFromUnknownList(HCELL_INDEX cellindex)
  44. /*
  45. Routine Description:
  46. Walk through the list and remove the specified cell.
  47. Free the storage too.
  48. Arguments:
  49. cellindex - supplies the cell index of the cell assumed as unknown.
  50. Return Value:
  51. NONE.
  52. */
  53. {
  54. if(LostSpace) {
  55. // only if we are interested in the lost space
  56. ULONG WhatList = (ULONG)cellindex % FRAGMENTATION;
  57. ULONG WhatIndex = (ULONG)cellindex % SUBLISTS;
  58. PUNKNOWN_CELL Prev;
  59. PUNKNOWN_CELL Tmp;
  60. Prev = NULL;
  61. Tmp = LostCells[WhatList].List[WhatIndex];
  62. fprintf(stdout,"Verifying Cell %8lx \r",cellindex,LostCells[WhatList].Count);
  63. while(Tmp) {
  64. if( Tmp->Cell == cellindex ) {
  65. // found it!
  66. if(Prev) {
  67. ASSERT(Prev->Next == Tmp);
  68. Prev->Next = Tmp->Next;
  69. } else {
  70. // no predecessor ==> Tmp is the entry ==> update it:
  71. LostCells[WhatList].List[WhatIndex] = Tmp->Next;
  72. }
  73. LostCells[WhatList].Count--;
  74. // free the space and break the loop
  75. free(Tmp);
  76. break;
  77. }
  78. Prev = Tmp;
  79. Tmp = Tmp->Next;
  80. }
  81. }
  82. }
  83. VOID FreeUnknownList()
  84. /*
  85. Routine Description:
  86. Free the storage for all elements.
  87. Arguments:
  88. None
  89. Return Value:
  90. NONE.
  91. */
  92. {
  93. if(LostSpace) {
  94. // only if we are interested in the lost space
  95. PUNKNOWN_CELL Tmp;
  96. ULONG i,j;
  97. for( i=0;i<FRAGMENTATION;i++) {
  98. for( j=0;j<SUBLISTS;j++) {
  99. while(LostCells[i].List[j]) {
  100. Tmp = LostCells[i].List[j];
  101. LostCells[i].List[j] = LostCells[i].List[j]->Next;
  102. free(Tmp);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. VOID DumpUnknownList()
  109. /*
  110. Routine Description:
  111. Dumps all the elements in the unknown list.
  112. Free the lost cells. Lost cells are cells that are marked as used,
  113. but are never referenced within the hive.
  114. Arguments:
  115. None
  116. Return Value:
  117. NONE.
  118. */
  119. {
  120. if(LostSpace) {
  121. // only if we are interested in the lost space
  122. ULONG Count = 0,i;
  123. for( i=0;i<FRAGMENTATION;i++) {
  124. ASSERT((LONG)(LostCells[i].Count) >= 0);
  125. Count += LostCells[i].Count;
  126. }
  127. fprintf(OutputFile,"\nLost Cells Count = %8lu \n",Count);
  128. if(Count && FixHive) {
  129. int chFree,j;
  130. PUNKNOWN_CELL Tmp;
  131. PHCELL pcell;
  132. USHORT Sig;
  133. fprintf(stdout,"Do you want to free the lost cells space ?(y/n)");
  134. fflush(stdin);
  135. chFree = getchar();
  136. if( (chFree != 'y') && (chFree != 'Y') ) {
  137. // the lost cells will remain lost
  138. return;
  139. }
  140. for( i=0;i<FRAGMENTATION;i++) {
  141. if(LostCells[i].Count > 0) {
  142. for( j=0;j<SUBLISTS;j++) {
  143. Tmp = LostCells[i].List[j];
  144. while(Tmp) {
  145. fprintf(stdout,"Marking cell 0x%lx as free ...");
  146. // free the cell only if it is not a security cell !
  147. pcell = (PHCELL)(Base + Tmp->Cell);
  148. Sig=(USHORT) pcell->u.NewCell.u.UserData;
  149. // don't mess with security cells !
  150. if(Sig != CM_KEY_SECURITY_SIGNATURE) {
  151. FreeCell(Tmp->Cell);
  152. }
  153. fprintf(stdout,"OK\n");
  154. Tmp = Tmp->Next;
  155. }
  156. }
  157. }
  158. }
  159. fprintf(stdout,"\n");
  160. }
  161. }
  162. }