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.

196 lines
6.2 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. checkfix.c
  5. Abstract:
  6. This module recomputes the checksum for an image file.
  7. Author:
  8. Steven R. Wood (stevewo) 4-May-1993
  9. Revision History:
  10. --*/
  11. #include <private.h>
  12. void Usage()
  13. {
  14. fprintf( stderr, "usage: CHECKFIX [-?] [-v] [-q] image-names...\n" );
  15. fprintf( stderr, " [-?] display this message\n" );
  16. fprintf( stderr, " [-v] verbose output\n" );
  17. fprintf( stderr, " [-q] quiet on failure\n" );
  18. exit( 1 );
  19. }
  20. int
  21. __cdecl
  22. main(
  23. int argc,
  24. char *argv[]
  25. )
  26. {
  27. HANDLE FileHandle;
  28. HANDLE MappingHandle;
  29. PIMAGE_NT_HEADERS NtHeaders;
  30. PVOID BaseAddress;
  31. ULONG CheckSum;
  32. ULONG FileLength;
  33. ULONG HeaderSum;
  34. ULONG OldCheckSum;
  35. LPSTR ImageName;
  36. BOOLEAN fVerbose = FALSE;
  37. BOOLEAN fQuiet = FALSE;
  38. LPSTR s;
  39. UCHAR c;
  40. if (argc <= 1) {
  41. Usage();
  42. }
  43. while (--argc) {
  44. s = *++argv;
  45. if ( *s == '-' ) {
  46. while (c=*++s) {
  47. switch (c) {
  48. case 'q':
  49. case 'Q':
  50. fQuiet = TRUE;
  51. break;
  52. case 'v':
  53. case 'V':
  54. fVerbose=TRUE;
  55. break;
  56. case 'h':
  57. case 'H':
  58. case '?':
  59. Usage();
  60. default:
  61. fprintf( stderr, "VERFIX: illegal option /%c\n", c );
  62. Usage();
  63. }
  64. }
  65. }
  66. else {
  67. ImageName = s;
  68. FileHandle = CreateFile( ImageName,
  69. GENERIC_READ | GENERIC_WRITE,
  70. FILE_SHARE_READ,
  71. NULL,
  72. OPEN_EXISTING,
  73. 0,
  74. NULL
  75. );
  76. if (FileHandle == INVALID_HANDLE_VALUE) {
  77. if (!fQuiet) {
  78. fprintf( stderr, "VERFIX: Unable to open %s (%u) - skipping\n", ImageName, GetLastError() );
  79. }
  80. }
  81. else {
  82. }
  83. MappingHandle = CreateFileMapping( FileHandle,
  84. NULL,
  85. PAGE_READWRITE,
  86. 0,
  87. 0,
  88. NULL
  89. );
  90. if (MappingHandle == NULL) {
  91. CloseHandle( FileHandle );
  92. if (!fQuiet) {
  93. fprintf( stderr, "VERFIX: Unable to create mapping object for file %s (%u) - skipping\n", ImageName, GetLastError() );
  94. }
  95. }
  96. else {
  97. BaseAddress = MapViewOfFile( MappingHandle,
  98. FILE_MAP_READ | FILE_MAP_WRITE,
  99. 0,
  100. 0,
  101. 0
  102. );
  103. CloseHandle( MappingHandle );
  104. if (BaseAddress == NULL) {
  105. CloseHandle( FileHandle );
  106. if (!fQuiet ) {
  107. fprintf( stderr, "VERFIX: Unable to map view of file %s (%u) - skipping\n", ImageName, GetLastError() );
  108. }
  109. }
  110. else {
  111. //
  112. // Get the length of the file in bytes and compute the checksum.
  113. //
  114. FileLength = GetFileSize( FileHandle, NULL );
  115. //
  116. // Obtain a pointer to the header information.
  117. //
  118. NtHeaders = ImageNtHeader( BaseAddress );
  119. if (NtHeaders == NULL) {
  120. CloseHandle( FileHandle );
  121. UnmapViewOfFile( BaseAddress );
  122. if (!fQuiet) {
  123. fprintf( stderr, "VERFIX: %s is not a valid image file - skipping\n", ImageName, GetLastError() );
  124. }
  125. }
  126. else {
  127. //
  128. // Recompute and reset the checksum of the modified file.
  129. //
  130. OldCheckSum = NtHeaders->OptionalHeader.CheckSum;
  131. (VOID) CheckSumMappedFile( BaseAddress,
  132. FileLength,
  133. &HeaderSum,
  134. &CheckSum
  135. );
  136. NtHeaders->OptionalHeader.CheckSum = CheckSum;
  137. if (!FlushViewOfFile( BaseAddress, FileLength )) {
  138. if (!fQuiet) {
  139. fprintf( stderr,
  140. "VERFIX: Flush of %s failed (%u)\n",
  141. ImageName,
  142. GetLastError()
  143. );
  144. }
  145. }
  146. if (NtHeaders->OptionalHeader.CheckSum != OldCheckSum) {
  147. if (!TouchFileTimes( FileHandle, NULL )) {
  148. if (!fQuiet) {
  149. fprintf( stderr, "VERFIX: Unable to touch file %s (%u)\n", ImageName, GetLastError() );
  150. }
  151. }
  152. else
  153. if (fVerbose) {
  154. printf( "%s - Old Checksum: %x", ImageName, OldCheckSum );
  155. printf( " New Checksum: %x\n", NtHeaders->OptionalHeader.CheckSum );
  156. }
  157. }
  158. UnmapViewOfFile( BaseAddress );
  159. CloseHandle( FileHandle );
  160. }
  161. }
  162. }
  163. }
  164. }
  165. exit( 0 );
  166. return 0;
  167. }