⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 patch.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <windows.h>
#include <conio.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

/** DEFINES *******************************************************************/

#define PATCH_BUFFER_SIZE           4096    /* Maximum size of a patch */
#define PATCH_BUFFER_MAGIC          "\xde\xad\xbe\xef MaGiC MaRk "
#define SIZEOF_PATCH_BUFFER_MAGIC   (sizeof (PATCH_BUFFER_MAGIC) - 1)

/** TYPES *********************************************************************/

typedef struct _PatchedByte
{
   int            offset;    /*!< File offset of the patched byte. */
   unsigned char  expected;  /*!< Expected (original) value of the byte. */
   unsigned char  patched;   /*!< Patched (new) value for the byte. */
} PatchedByte;

typedef struct _PatchedFile
{
   const char  *name;        /*!< Name of the file to be patched. */
   int          fileSize;    /*!< Size of the file in bytes. */
   int          patchCount;  /*!< Number of patches for the file. */
   PatchedByte *patches;     /*!< Patches for the file. */
} PatchedFile;

typedef struct _Patch
{
   const char   *name;       /*!< Name of the patch. */
   int           fileCount;  /*!< Number of files in the patch. */
   PatchedFile  *files;      /*!< Files for the patch. */
} Patch;

/** FUNCTION PROTOTYPES *******************************************************/

static void printUsage();

/** GLOBALS *******************************************************************/

static Patch m_patch = { NULL, 0, NULL };
static int m_argc = 0;
static char **m_argv = NULL;

/* patch buffer where we put the patch info into */
static char m_patchBuffer[SIZEOF_PATCH_BUFFER_MAGIC + PATCH_BUFFER_SIZE] =
   PATCH_BUFFER_MAGIC;

/** HELPER FUNCTIONS **********************************************************/

static void *
loadFile(const char *fileName, int *fileSize_)
{
   FILE *f;
   struct stat sb;
   int fileSize;
   void *p;
   
   /* Open the file */
   f = fopen(fileName, "rb");
   if (f == NULL)
   {
      printf("Couldn't open file %s for reading!\n", fileName);
      return NULL;
   }
   
   /* Get file size */
   if (fstat(fileno(f), &sb) < 0)
   {
      fclose(f);
      printf("Couldn't get size of file %s!\n", fileName);
      return NULL;
   }
   fileSize = sb.st_size;
   
   /* Load file */
   p = malloc(fileSize);
   if (p == NULL)
   {
      fclose(f);
      printf("Couldn't allocate %d bytes for file %s!\n", fileSize, fileName);
      return NULL;
   }
   
   if (fread(p, fileSize, 1, f) != 1)
   {
      fclose(f);
      free(p);
      printf("Couldn't read file %s into memory!\n", fileName);
      return NULL;
   }
   
   /* Close file */
   fclose(f);
   
   *fileSize_ = fileSize;
   return p;
}


static int
saveFile(const char *fileName, void *file, int fileSize)
{
   FILE *f;

   /* Open the file */
   f = fopen(fileName, "wb");
   if (f == NULL)
   {
      printf("Couldn't open file %s for writing!\n", fileName);
      return -1;
   }

   /* Write file */
   if (fwrite(file, fileSize, 1, f) != 1)
   {
      fclose(f);
      printf("Couldn't write file %s!\n", fileName);
      return -1;
   }

   /* Close file */
   fclose(f);
   return 0;
}


static int
compareFiles(
   PatchedFile *patchedFile,
   const char *originalFileName)
{
   const char *patchedFileName = patchedFile->name;
   unsigned char *origChunk, *patchedChunk;
   int origSize, patchedSize, i, patchCount;
   PatchedByte *patches = NULL;
   int patchesArrayCount = 0;
   
   /* Load both files */
   origChunk = loadFile(originalFileName, &origSize);
   if (origChunk == NULL)
      return -1;
   patchedChunk = loadFile(patchedFileName, &patchedSize);
   if (patchedChunk == NULL)
   {
      free(origChunk);
      return -1;
   }
   if (origSize != patchedSize)
   {
      free(origChunk);
      free(patchedChunk);
      printf("File size of %s and %s differs (%d != %d)\n",
             originalFileName, patchedFileName,
             origSize, patchedSize);
      return -1;
   }
   
   /* Compare the files and record any differences */
   printf("Comparing %s to %s", originalFileName, patchedFileName);
   for (i = 0, patchCount = 0; i < origSize; i++)
   {
      if (origChunk[i] != patchedChunk[i])
      {
         patchCount++;
         
         /* Resize patches array if needed */
         if (patchesArrayCount < patchCount)
         {
            PatchedByte *newPatches;
            newPatches = realloc(patches, patchCount * sizeof (PatchedByte));
            if (newPatches == NULL)
            {
              if (patches != NULL)
                free(patches);
              free(origChunk);
              free(patchedChunk);
              printf("\nOut of memory (tried to allocated %d bytes)\n",
                     patchCount * sizeof (PatchedByte));
              return -1;
            }
            patches = newPatches;
         }
         
         /* Fill in patch info */
         patches[patchCount - 1].offset = i;
         patches[patchCount - 1].expected = origChunk[i];
         patches[patchCount - 1].patched = patchedChunk[i];
      }
      if ((i % (origSize / 40)) == 0)
         printf(".");
   }
   printf(" %d changed bytes found.\n", patchCount);
   
   /* Unload the files */
   free(origChunk);
   free(patchedChunk);
   
   /* Save patch info */
   patchedFile->fileSize = patchedSize;
   patchedFile->patchCount = patchCount;
   patchedFile->patches = patches;
   
   return 0;
}


static int
outputPatch(const char *outputFileName)
{
   char *patchExe, *patchBuffer = NULL;
   int i, size, patchExeSize, patchSize, stringSize, stringOffset, patchOffset;
   Patch *patch;
   PatchedFile *files;
   
   printf("Putting patch into %s...\n", outputFileName);
   
   /* Calculate size of the patch */
   patchSize = sizeof (Patch) + sizeof (PatchedFile) * m_patch.fileCount;
   stringSize = strlen(m_patch.name) + 1;
   for (i = 0; i < m_patch.fileCount; i++)
   {
      stringSize += strlen(m_patch.files[i].name) + 1;
      patchSize += sizeof (PatchedByte) * m_patch.files[i].patchCount;
   }
   if ((stringSize + patchSize) > PATCH_BUFFER_SIZE)
   {
      printf("Patch is too big - %d bytes maximum, %d bytes needed\n",
             PATCH_BUFFER_SIZE, stringSize + patchSize);
      return -1;
   }

   /* Load patch.exe file into memory... */
   patchExe = loadFile(m_argv[0], &patchExeSize);
   if (patchExe == NULL)
   {
      return -1;
   }
   
   /* Try to find the magic mark for the patch buffer */
   for (i = 0; i < (patchExeSize - SIZEOF_PATCH_BUFFER_MAGIC); i++)
   {
      if (memcmp(patchExe + i, m_patchBuffer, SIZEOF_PATCH_BUFFER_MAGIC) == 0)
      {
         patchBuffer = patchExe + i + SIZEOF_PATCH_BUFFER_MAGIC;
         
         break;
      }
   }
   if (!(i < (patchExeSize - SIZEOF_PATCH_BUFFER_MAGIC)))
   {
      free(patchExe);
      printf("Couldn't find patch buffer magic in file %s - this shouldn't happen!!!\n", m_argv[0]);
      return -1;
   }
   
   /* Pack patch together and replace string pointers by offsets */
   patch = (Patch *)patchBuffer;
   files = (PatchedFile *)(patchBuffer + sizeof (Patch));
   patchOffset = sizeof (Patch) + sizeof (PatchedFile) * m_patch.fileCount;
   stringOffset = patchSize;
   
   patch->fileCount = m_patch.fileCount;
   patch->files = (PatchedFile *)sizeof (Patch);

   patch->name = (const char *)stringOffset;
   strcpy(patchBuffer + stringOffset, m_patch.name);
   stringOffset += strlen(m_patch.name) + 1;
   
   for (i = 0; i < m_patch.fileCount; i++)
   {
      files[i].fileSize = m_patch.files[i].fileSize;
      files[i].patchCount = m_patch.files[i].patchCount;
      
      files[i].name = (const char *)stringOffset;
      strcpy(patchBuffer + stringOffset, m_patch.files[i].name);
      stringOffset += strlen(m_patch.files[i].name) + 1;
      
      size = files[i].patchCount * sizeof (PatchedByte);
      files[i].patches = (PatchedByte *)patchOffset;
      memcpy(patchBuffer + patchOffset, m_patch.files[i].patches, size);
      patchOffset += size;
   }
   size = patchSize + stringSize;
   memset(patchBuffer + size, 0, PATCH_BUFFER_SIZE - size);

   /* Save file */
   if (saveFile(outputFileName, patchExe, patchExeSize) < 0)
   {
      free(patchExe);
      return -1;
   }
   free(patchExe);

   printf("Patch saved!\n");
   return 0;
}


static int
loadPatch()
{
   char *p;
   Patch *patch;
   int i;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -