+++ /dev/null
-/*++\r
-\r
-Copyright (c) 2004-2006 Intel Corporation. All rights reserved\r
-This program and the accompanying materials are licensed and made available\r
-under the terms and conditions of the BSD License which accompanies this\r
-distribution. The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-\r
-Module Name:\r
-\r
- ZeroDebugData.c\r
-\r
-Abstract:\r
-\r
- Zero the Debug Data Fields of Portable Executable (PE) format file.\r
-\r
---*/\r
-\r
-#include <stdio.h>\r
-#include <string.h>\r
-#include <stdlib.h>\r
-\r
-#define UTILITY_NAME "GenTEImage"\r
-#define UTILITY_MAJOR_VERSION 0\r
-#define UTILITY_MINOR_VERSION 1\r
-\r
-void\r
-Version (\r
- void\r
- )\r
-/*++\r
-Routine Description:\r
- print version information for this utility\r
-\r
-Arguments:\r
- None\r
-\r
-Returns:\r
- None\r
---*/\r
-// GC_TODO: void - add argument and description to function comment\r
-{\r
- //\r
- // print usage of command\r
- //\r
- printf ("%s v%d.%d -Utility to zero the Debug Data Fields of Portable Executable (PE) format file.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);\r
- printf ("Copyright (c) 1999-2007 Intel Corporation. All rights reserved.\n");\r
-}\r
-\r
-void\r
-Usage (\r
- void\r
- )\r
-/*++\r
-Routine Description:\r
- print usage of ZeroDebugData command\r
-\r
-Arguments:\r
- None\r
-\r
-Returns:\r
- None\r
---*/\r
-// GC_TODO: void - add argument and description to function comment\r
-{\r
- Version();\r
- //\r
- // print usage of command\r
- //\r
- printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");\r
-}\r
-\r
-int\r
-ReadFromFile (\r
- FILE *fp,\r
- long offset,\r
- void *buffer,\r
- int size\r
- )\r
-/*++\r
-Routine Description:\r
- read data from a specified location of file\r
-\r
-Arguments:\r
- fp - file pointer\r
- offset - number of bytes from beginning of file\r
- buffer - buffer used to store data\r
- size - size of buffer\r
-\r
-Returns:\r
- = 0 - Success\r
- = -1 - Failed\r
---*/\r
-{\r
- //\r
- // set file pointer to the specified location of file\r
- //\r
- if (fseek (fp, offset, SEEK_SET) != 0) {\r
- printf ("Error: Cannot move the current location of the file.\n");\r
- return -1;\r
- }\r
- //\r
- // read data from the file\r
- //\r
- if (fread (buffer, size, 1, fp) != 1) {\r
- printf ("Error: Cannot read data from the file.\n");\r
- return -1;\r
- }\r
-\r
- return 0;\r
-}\r
-\r
-int\r
-ZeroDebugData (\r
- FILE *fp,\r
- FILE *fpData\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Zero the debug data fields of the file\r
-\r
-Arguments:\r
-\r
- fp - file pointer\r
- fpData - pointer to output file that ZeroDebugData progress is written to\r
-\r
-Returns:\r
-\r
- = 0 - Success\r
- = -1 - Failed\r
-\r
---*/\r
-{\r
- unsigned char header[4];\r
- unsigned long offset;\r
- unsigned long NumberOfRvaAndSizes;\r
- unsigned int nvalue;\r
- unsigned long lvalue;\r
- unsigned long Size;\r
- unsigned long Pointer;\r
- unsigned char *Buffer;\r
- unsigned long Index;\r
-\r
- //\r
- // read the header of file\r
- //\r
- if (ReadFromFile (fp, 0, header, 2) != 0) {\r
- printf ("Error: open image file\n");\r
- return -1;\r
- }\r
- //\r
- // "MZ" -- the header of image file (PE)\r
- //\r
- if (strncmp ((char *) header, "MZ", 2) != 0) {\r
- printf ("Error: Invalid Image file.\n");\r
- return -1;\r
- }\r
- //\r
- // At location 0x3C, the stub has the file offset to the\r
- // PE signature.\r
- //\r
- if (ReadFromFile (fp, 0x3C, &offset, 4) != 0) {\r
- return -1;\r
- }\r
- //\r
- // read the header of optional\r
- //\r
- if (ReadFromFile (fp, offset, header, 4) != 0) {\r
- return -1;\r
- }\r
- //\r
- // "PE\0\0" -- the signature of optional header\r
- //\r
- if (strncmp ((char *) header, "PE\0\0", 4) != 0) {\r
- printf ("Error: Invalid PE format file.\n");\r
- return -1;\r
- }\r
- //\r
- // Add 16 to skip COFF file header, and get to optional header.\r
- //\r
- offset += 24;\r
-\r
- //\r
- // Check the magic field, 0x10B for PE32 and 0x20B for PE32+\r
- //\r
- if (ReadFromFile (fp, offset, &nvalue, 2) != 0) {\r
- return -1;\r
- }\r
- //\r
- // If this is PE32 image file, offset of NumberOfRvaAndSizes is 92.\r
- // Else it is 108.\r
- //\r
- switch (nvalue & 0xFFFF) {\r
- case 0x10B:\r
- offset += 92;\r
- printf ("Info: Image is PE32. ");\r
- break;\r
-\r
- case 0x20B:\r
- offset += 108;\r
- printf ("Info: Image is PE32+. ");\r
- break;\r
-\r
- default:\r
- printf ("Error: Magic value is unknown.\n");\r
- return -1;\r
- }\r
- //\r
- // get the value of NumberOfRvaAndSizes\r
- //\r
- if (ReadFromFile (fp, offset, &NumberOfRvaAndSizes, 4) != 0) {\r
- printf ("Error: read NumberOfRvaAndSizes error.\n");\r
- return -1;\r
- }\r
- //\r
- // printf ("Info: NumberOfRvaAndSizes = %d\n", NumberOfRvaAndSizes);\r
- //\r
- //\r
- // Finding Debug Table, offset of Debug Table\r
- // is 4 + 6 * 8 = 52.\r
- //\r
- if (NumberOfRvaAndSizes >= 7) {\r
- if (ReadFromFile (fp, offset + 52, &lvalue, 4) != 0) {\r
- return -1;\r
- }\r
- //\r
- // Read the SizeOfData(16) and PointerToRawData(24)\r
- //\r
- if (ReadFromFile (fp, lvalue + 16, &Size, 4) != 0) {\r
- printf ("error: Size = %d\n", Size);\r
- return -1;\r
- }\r
-\r
- printf ("Debug data: size = %xh, ", Size);\r
- fprintf (fpData, "Debug data: size = %xh, ", Size);\r
-\r
- if (ReadFromFile (fp, lvalue + 20, &Pointer, 4) != 0) {\r
- printf ("error: LoadOffset = %xh\n", Pointer);\r
- return -1;\r
- }\r
- //\r
- // printf ("LoadOffset = %xh, ", Pointer);\r
- //\r
- fprintf (fpData, "LoadOffset = %xh, ", Pointer);\r
-\r
- if (ReadFromFile (fp, lvalue + 24, &Pointer, 4) != 0) {\r
- printf ("error: FileOffset = %xh\n", Pointer);\r
- return -1;\r
- }\r
-\r
- printf ("FileOffset = %xh, ", Pointer);\r
- fprintf (fpData, "FileOffset = %xh, \n", Pointer);\r
-\r
- if ((lvalue != 0) && (Pointer != 0)) {\r
- //\r
- // prepare buffer\r
- //\r
- Buffer = malloc (Size + 1);\r
- if (Buffer == NULL) {\r
- printf ("Error: Cannot allocate memory.\n");\r
- return -1;\r
- }\r
- //\r
- // set file pointer to the specified location of file\r
- //\r
- if (fseek (fp, Pointer, SEEK_SET) != 0) {\r
- printf ("Error: Cannot move the current location of the file.\n");\r
- free (Buffer);\r
- return -1;\r
- }\r
- //\r
- // read data from PE file\r
- //\r
- if (fread (Buffer, Size, 1, fp) != 1) {\r
- printf ("Error: Cannot read data from the file.\n");\r
- free (Buffer);\r
- return -1;\r
- }\r
- //\r
- // write to data file\r
- //\r
- for (Index = 0; Index < Size;) {\r
- fprintf (fpData, "%02x ", Buffer[Index]);\r
-\r
- Index++;\r
- if (Index % 8 == 0) {\r
- fprintf (fpData, "\n");\r
- }\r
- }\r
-\r
- fprintf (fpData, "\n");\r
-\r
- //\r
- // zero buffer and write back to PE file\r
- //\r
- if (fseek (fp, Pointer, SEEK_SET) != 0) {\r
- printf ("Error: Cannot move the current location of the file.\n");\r
- free (Buffer);\r
- return -1;\r
- }\r
-\r
- memset (Buffer, 0, Size);\r
- if (fwrite (Buffer, Size, 1, fp) != 1) {\r
- perror ("Error: Cannot write zero to the file.\n");\r
- free (Buffer);\r
- return -1;\r
- }\r
- //\r
- // set file pointer to the specified location of file\r
- //\r
- if (fseek (fp, lvalue + 4, SEEK_SET) != 0) {\r
- printf ("Error: Cannot move the current location of the file.\n");\r
- free (Buffer);\r
- return -1;\r
- }\r
-\r
- if (fwrite (Buffer, 4, 1, fp) != 1) {\r
- perror ("Error: Cannot write zero to the file.\n");\r
- free (Buffer);\r
- return -1;\r
- }\r
-\r
- free (Buffer);\r
- }\r
- }\r
-\r
- return 0;\r
-}\r
-\r
-int\r
-main (\r
- int argc,\r
- char *argv[]\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Prints the zero debug data of the PE file to the DebugData file.\r
- Executes the ZeroDebugData function.\r
-\r
-Arguments:\r
-\r
- argc - Standard C argument, number of command line arguments.\r
- argv[] - Standard C argument, array of pointers to the input files,\r
- such as the PE and DebugData files.\r
-\r
-Returns:\r
-\r
- zero - success\r
- nonzero - failure\r
-\r
---*/\r
-{\r
- FILE *fp;\r
- FILE *fpData;\r
- char DataFile[1024] = "";\r
-\r
- if (argc == 1) {\r
- Usage();\r
- return -1;\r
- }\r
- \r
- if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0) ||\r
- (strcmp(argv[1], "-?") == 0) || (strcmp(argv[1], "/?") == 0)) {\r
- Usage();\r
- return -1;\r
- }\r
- \r
- if ((strcmp(argv[1], "-V") == 0) || (strcmp(argv[1], "--version") == 0)) {\r
- Version();\r
- return -1;\r
- }\r
-\r
- if (argc == 2) {\r
- Usage();\r
- return -1;\r
- }\r
-\r
- //\r
- // open the DebugData file, if not exists, return\r
- //\r
- if (argc >= 3) {\r
- strcpy (DataFile, argv[2]);\r
- } else {\r
- strcpy (DataFile, "DebugData.dat");\r
- }\r
-\r
- fpData = fopen (DataFile, "a+");\r
- if (fpData == NULL) {\r
- fpData = fopen (DataFile, "w");\r
- if (fpData == NULL) {\r
- printf ("Error: Cannot open the data file!\n");\r
- return -1;\r
- }\r
- }\r
- //\r
- // open the PE file\r
- //\r
- fp = fopen (argv[1], "r+b");\r
- if (fp == NULL) {\r
- printf ("Error: Cannot open the PE file!\n");\r
- return -1;\r
- }\r
- //\r
- // Zero the Debug Data to the PE file\r
- //\r
- printf ("Zero Debug Data to file %s:\n", argv[1]);\r
- fprintf (fpData, "\nZero Debug Data to file %s:\n", argv[1]);\r
- if ((int *) ZeroDebugData (fp, fpData) != 0) {\r
- printf ("Error: Zero Debug Data PE file\n");\r
- fclose (fp);\r
- return -1;\r
- }\r
-\r
- printf (" success\n");\r
-\r
- //\r
- // close the PE file\r
- //\r
- fflush (fpData);\r
- fflush (fp);\r
- fclose (fpData);\r
- fclose (fp);\r
-\r
- return 0;\r
-}\r