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.

182 lines
4.2 KiB

  1. #ifndef _WIN32_WINNT
  2. #define _WIN32_WINNT 0x0400
  3. #endif
  4. #ifndef WIN32
  5. #define WIN32 0x0400
  6. #endif
  7. #pragma warning( disable: 4001 4035 4115 4200 4201 4204 4209 4214 4514 4699 )
  8. #include <windows.h>
  9. #pragma warning( disable: 4001 4035 4115 4200 4201 4204 4209 4214 4514 4699 )
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include "patchapi.h"
  13. #include "patchprv.h"
  14. #include <ntverp.h>
  15. #include <common.ver>
  16. typedef
  17. BOOL
  18. PATCHAPI
  19. FN_ApplyPatchToFileExA(
  20. IN LPCSTR PatchFileName,
  21. IN LPCSTR OldFileName,
  22. OUT LPCSTR NewFileName,
  23. IN ULONG ApplyOptionFlags,
  24. IN PPATCH_PROGRESS_CALLBACK ProgressCallback,
  25. IN PVOID CallbackContext
  26. );
  27. typedef FN_ApplyPatchToFileExA *PFN_ApplyPatchToFileExA;
  28. FN_ApplyPatchToFileExA ApplyPatchToFileExA; // assert typedef correctness
  29. void CopyRight( void ) {
  30. printf(
  31. "\n"
  32. "APATCH " VER_PRODUCTVERSION_STR " Patch Application Utility\n"
  33. VER_LEGALCOPYRIGHT_STR
  34. "\n\n"
  35. );
  36. }
  37. void Usage( void ) {
  38. printf(
  39. "Usage: APATCH PatchFile OldFile TargetNewFile\n\n"
  40. );
  41. exit( 1 );
  42. }
  43. BOOL
  44. CALLBACK
  45. MyProgressCallback(
  46. PVOID CallbackContext,
  47. ULONG CurrentPosition,
  48. ULONG MaximumPosition
  49. )
  50. {
  51. UNREFERENCED_PARAMETER( CallbackContext );
  52. if ( CurrentPosition & 0xFF000000 ) {
  53. CurrentPosition >>= 8;
  54. MaximumPosition >>= 8;
  55. }
  56. if ( MaximumPosition != 0 ) {
  57. fprintf( stderr, "\r%3.1f%% complete", ( CurrentPosition * 100.0 ) / MaximumPosition );
  58. }
  59. return TRUE;
  60. }
  61. void __cdecl main( int argc, char *argv[] ) {
  62. LPSTR OldFileName = NULL;
  63. LPSTR PatchFileName = NULL;
  64. LPSTR NewFileName = NULL;
  65. LPSTR DllFileName = NULL;
  66. BOOL Success;
  67. LPSTR arg;
  68. int i;
  69. CHAR LoadedFileName[ MAX_PATH + 1 ];
  70. HMODULE hLib;
  71. PFN_ApplyPatchToFileExA pApplyPatchToFileExA = ApplyPatchToFileExA;
  72. SetErrorMode( SEM_FAILCRITICALERRORS );
  73. #ifndef DEBUG
  74. SetErrorMode( SEM_NOALIGNMENTFAULTEXCEPT | SEM_FAILCRITICALERRORS );
  75. #endif
  76. CopyRight();
  77. for ( i = 1; i < argc; i++ ) {
  78. arg = argv[ i ];
  79. if ( strchr( arg, '?' )) {
  80. Usage();
  81. }
  82. if ( _strnicmp( arg, "/dll:", 5 ) == 0 ) {
  83. DllFileName = arg + 5;
  84. }
  85. else if ( PatchFileName == NULL ) {
  86. PatchFileName = arg;
  87. }
  88. else if ( OldFileName == NULL ) {
  89. OldFileName = arg;
  90. }
  91. else if ( NewFileName == NULL ) {
  92. NewFileName = arg;
  93. }
  94. else {
  95. Usage();
  96. }
  97. }
  98. if (( OldFileName == NULL ) || ( NewFileName == NULL ) || ( PatchFileName == NULL )) {
  99. Usage();
  100. }
  101. if ( DllFileName != NULL ) {
  102. hLib = LoadLibrary( DllFileName );
  103. if ( hLib == NULL ) {
  104. printf( "Unable to load DLL '%s'\n", DllFileName );
  105. exit( 1 );
  106. }
  107. if ( GetModuleFileName( hLib, LoadedFileName, sizeof( LoadedFileName ))) {
  108. printf( "Loaded DLL '%s'\n", LoadedFileName );
  109. }
  110. pApplyPatchToFileExA = (PFN_ApplyPatchToFileExA) GetProcAddress( hLib, "ApplyPatchToFileExA" );
  111. if ( pApplyPatchToFileExA == NULL ) {
  112. printf( "Unable to import ApplyPatchToFileExA from DLL\n" );
  113. exit( 1 );
  114. }
  115. }
  116. DeleteFile( NewFileName );
  117. Success = pApplyPatchToFileExA(
  118. PatchFileName,
  119. OldFileName,
  120. NewFileName,
  121. 0,
  122. MyProgressCallback,
  123. NULL
  124. );
  125. printf( "\n\n" );
  126. if ( ! Success ) {
  127. CHAR ErrorText[ 16 ];
  128. ULONG ErrorCode = GetLastError();
  129. sprintf( ErrorText, ( ErrorCode < 0x10000000 ) ? "%d" : "%X", ErrorCode );
  130. printf( "Failed to create file from patch (%s)\n", ErrorText );
  131. exit( 1 );
  132. }
  133. printf( "OK\n" );
  134. exit( 0 );
  135. }