Team Fortress 2 Source Code as on 22/4/2020
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.

149 lines
3.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // test_binaries.cpp : test for debug section
  9. //
  10. // Adapted from PEDUMP, AUTHOR: Matt Pietrek - 1993
  11. //--------------------
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include "common.h"
  15. #include "strtools.h"
  16. bool HasSection( PIMAGE_SECTION_HEADER section, int numSections, const char *pSectionName )
  17. {
  18. for ( int i = 0; i < numSections; i++ )
  19. {
  20. if ( !strnicmp( (char *)section[i].Name, pSectionName, 8 ) )
  21. return true;
  22. }
  23. return false;
  24. }
  25. void TestExeFile( const char *pFilename, PIMAGE_DOS_HEADER dosHeader )
  26. {
  27. PIMAGE_NT_HEADERS pNTHeader;
  28. pNTHeader = MakePtr( PIMAGE_NT_HEADERS, dosHeader,
  29. dosHeader->e_lfanew );
  30. // First, verify that the e_lfanew field gave us a reasonable
  31. // pointer, then verify the PE signature.
  32. if ( IsBadReadPtr(pNTHeader, sizeof(IMAGE_NT_HEADERS)) ||
  33. pNTHeader->Signature != IMAGE_NT_SIGNATURE )
  34. {
  35. printf("Unhandled EXE type, or invalid .EXE (%s)\n", pFilename);
  36. return;
  37. }
  38. if ( HasSection( (PIMAGE_SECTION_HEADER)(pNTHeader+1), pNTHeader->FileHeader.NumberOfSections, "ValveDBG" ) )
  39. {
  40. printf("%s is a debug build\n", pFilename);
  41. }
  42. }
  43. //
  44. // Open up a file, memory map it, and call the appropriate dumping routine
  45. //
  46. void TestFile(const char *pFilename)
  47. {
  48. HANDLE hFile;
  49. HANDLE hFileMapping;
  50. LPVOID lpFileBase;
  51. PIMAGE_DOS_HEADER dosHeader;
  52. hFile = CreateFile(pFilename, GENERIC_READ, FILE_SHARE_READ, NULL,
  53. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  54. if ( hFile == INVALID_HANDLE_VALUE )
  55. {
  56. printf("Couldn't open file %s with CreateFile()\n", pFilename );
  57. return;
  58. }
  59. hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  60. if ( hFileMapping == 0 )
  61. {
  62. CloseHandle(hFile);
  63. printf("Couldn't open file mapping with CreateFileMapping()\n");
  64. return;
  65. }
  66. lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
  67. if ( lpFileBase == 0 )
  68. {
  69. CloseHandle(hFileMapping);
  70. CloseHandle(hFile);
  71. printf("Couldn't map view of file with MapViewOfFile()\n");
  72. return;
  73. }
  74. dosHeader = (PIMAGE_DOS_HEADER)lpFileBase;
  75. if ( dosHeader->e_magic == IMAGE_DOS_SIGNATURE )
  76. {
  77. TestExeFile( pFilename, dosHeader );
  78. }
  79. #if 0
  80. else if ( (dosHeader->e_magic == 0x014C) // Does it look like a i386
  81. && (dosHeader->e_sp == 0) ) // COFF OBJ file???
  82. {
  83. // The two tests above aren't what they look like. They're
  84. // really checking for IMAGE_FILE_HEADER.Machine == i386 (0x14C)
  85. // and IMAGE_FILE_HEADER.SizeOfOptionalHeader == 0;
  86. DumpObjFile( (PIMAGE_FILE_HEADER)lpFileBase );
  87. }
  88. #endif
  89. else
  90. printf("unrecognized file format\n");
  91. UnmapViewOfFile(lpFileBase);
  92. CloseHandle(hFileMapping);
  93. CloseHandle(hFile);
  94. }
  95. int main(int argc, char* argv[])
  96. {
  97. if ( argc < 2 )
  98. {
  99. printf("Usage: test_binaries <FILENAME>\n" );
  100. }
  101. else
  102. {
  103. char fileName[2048], dir[2048];
  104. if ( !Q_ExtractFilePath( argv[1], dir, sizeof( dir ) ) )
  105. {
  106. strcpy( dir, "" );
  107. }
  108. else
  109. {
  110. Q_FixSlashes( dir, '/' );
  111. int len = strlen(dir);
  112. if ( len && dir[len-1] !='/' )
  113. {
  114. strcat( dir, "/" );
  115. }
  116. }
  117. WIN32_FIND_DATA findData;
  118. HANDLE hFind = FindFirstFile( argv[1], &findData );
  119. if ( hFind == INVALID_HANDLE_VALUE )
  120. {
  121. printf("Can't find %s\n", argv[1] );
  122. }
  123. else
  124. {
  125. do
  126. {
  127. sprintf( fileName, "%s%s", dir, findData.cFileName );
  128. TestFile( fileName );
  129. } while ( FindNextFile( hFind, &findData ) );
  130. FindClose( hFind );
  131. }
  132. }
  133. return 0;
  134. }