Counter Strike : Global Offensive Source Code
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.

368 lines
9.8 KiB

  1. #include "crccheck_shared.h"
  2. #include "tier1/checksum_crc.h"
  3. #include "tier1/strtools.h"
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdarg.h>
  7. #ifdef _WIN32
  8. #include <process.h>
  9. #else
  10. #include <stdlib.h>
  11. #define stricmp strcasecmp
  12. #endif
  13. #pragma warning( disable : 4996 )
  14. #pragma warning( disable : 4127 )
  15. #define MAX_INCLUDE_STACK_DEPTH 10
  16. //-----------------------------------------------------------------------------
  17. // Sys_Error
  18. //
  19. //-----------------------------------------------------------------------------
  20. void Sys_Error( const char* format, ... )
  21. {
  22. va_list argptr;
  23. va_start( argptr,format );
  24. vfprintf( stderr, format, argptr );
  25. va_end( argptr );
  26. exit( 1 );
  27. }
  28. void SafeSnprintf( char *pOut, int nOutLen, const char *pFormat, ... )
  29. {
  30. va_list marker;
  31. va_start( marker, pFormat );
  32. V_vsnprintf( pOut, nOutLen, pFormat, marker );
  33. va_end( marker );
  34. pOut[nOutLen-1] = 0;
  35. }
  36. // for linked lists of strings
  37. struct StringNode_t
  38. {
  39. StringNode_t *m_pNext;
  40. char m_Text[1]; // the string data
  41. };
  42. static StringNode_t *MakeStrNode( char const *pStr )
  43. {
  44. size_t nLen = strlen( pStr );
  45. StringNode_t *nRet = ( StringNode_t * ) new unsigned char[sizeof( StringNode_t ) + nLen ];
  46. strcpy( nRet->m_Text, pStr );
  47. return nRet;
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Sys_LoadFile
  51. //-----------------------------------------------------------------------------
  52. int Sys_LoadTextFileWithIncludes( const char* filename, char** bufferptr )
  53. {
  54. FILE *pFileStack[MAX_INCLUDE_STACK_DEPTH];
  55. int nSP = MAX_INCLUDE_STACK_DEPTH;
  56. StringNode_t *pFileLines = NULL; // tail ptr for fast adds
  57. size_t nTotalFileBytes = 0;
  58. FILE *handle = fopen( filename, "r" );
  59. if ( !handle )
  60. return -1;
  61. pFileStack[--nSP] = handle; // push
  62. while ( nSP < MAX_INCLUDE_STACK_DEPTH )
  63. {
  64. // read lines
  65. for (;;)
  66. {
  67. char lineBuffer[2048];
  68. char *ln = fgets( lineBuffer, sizeof( lineBuffer ), pFileStack[nSP] );
  69. if ( !ln )
  70. break; // out of text
  71. ln += strspn( ln, "\t " ); // skip white space
  72. if ( memcmp( ln, "#include", 8 ) == 0 )
  73. {
  74. // omg, an include
  75. ln += 8;
  76. ln += strspn( ln, " \t\"<" ); // skip whitespace, ", and <
  77. size_t nPathNameLength = strcspn( ln, " \t\">\n" );
  78. if ( !nPathNameLength )
  79. {
  80. Sys_Error( "bad include %s via %s\n", lineBuffer, filename );
  81. }
  82. ln[nPathNameLength] = 0; // kill everything after end of filename
  83. FILE *inchandle = fopen( ln, "r" );
  84. if ( !inchandle )
  85. {
  86. Sys_Error( "can't open #include of %s\n", ln );
  87. }
  88. if ( !nSP )
  89. {
  90. Sys_Error( "include nesting too deep via %s", filename );
  91. }
  92. pFileStack[--nSP] = inchandle;
  93. }
  94. else
  95. {
  96. size_t nLen = strlen( ln );
  97. nTotalFileBytes += nLen;
  98. StringNode_t *pNewLine = MakeStrNode( ln );
  99. pNewLine->m_pNext = pFileLines;
  100. pFileLines = pNewLine;
  101. }
  102. }
  103. fclose( pFileStack[nSP] );
  104. nSP++; // pop stack
  105. }
  106. // Reverse the pFileLines list so it goes the right way.
  107. StringNode_t *pPrev = NULL;
  108. StringNode_t *pCur;
  109. for( pCur = pFileLines; pCur; )
  110. {
  111. StringNode_t *pNext = pCur->m_pNext;
  112. pCur->m_pNext = pPrev;
  113. pPrev = pCur;
  114. pCur = pNext;
  115. }
  116. pFileLines = pPrev;
  117. // Now dump all the lines out into a single buffer.
  118. char *buffer = new char[nTotalFileBytes + 1]; // and null
  119. *bufferptr = buffer; // tell caller
  120. // copy all strings and null terminate
  121. int nLine = 0;
  122. StringNode_t *pNext;
  123. for( pCur=pFileLines; pCur; pCur=pNext )
  124. {
  125. pNext = pCur->m_pNext;
  126. size_t nLen = strlen( pCur->m_Text );
  127. memcpy( buffer, pCur->m_Text, nLen );
  128. buffer += nLen;
  129. nLine++;
  130. // Cleanup the line..
  131. //delete [] (unsigned char*)pCur;
  132. }
  133. *( buffer++ ) = 0; // null
  134. return (int)nTotalFileBytes;
  135. }
  136. // Just like fgets() but it removes trailing newlines.
  137. char* ChompLineFromFile( char *pOut, int nOutBytes, FILE *fp )
  138. {
  139. char *pReturn = fgets( pOut, nOutBytes, fp );
  140. if ( pReturn )
  141. {
  142. int len = (int)strlen( pReturn );
  143. if ( len > 0 && pReturn[len-1] == '\n' )
  144. {
  145. pReturn[len-1] = 0;
  146. if ( len > 1 && pReturn[len-2] == '\r' )
  147. pReturn[len-2] = 0;
  148. }
  149. }
  150. return pReturn;
  151. }
  152. bool CheckSupplementalString( const char *pSupplementalString, const char *pReferenceSupplementalString )
  153. {
  154. // The supplemental string is only checked while VPC is determining if a project file is stale or not.
  155. // It's not used by the pre-build event's CRC check.
  156. // The supplemental string contains various options that tell how the project was built. It's generated in VPC_GenerateCRCOptionString.
  157. //
  158. // If there's no reference supplemental string (which is the case if we're running vpccrccheck.exe), then we ignore it and continue.
  159. if ( !pReferenceSupplementalString )
  160. return true;
  161. return ( pSupplementalString && pReferenceSupplementalString && stricmp( pSupplementalString, pReferenceSupplementalString ) == 0 );
  162. }
  163. bool VPC_CheckProjectDependencyCRCs( const char *pProjectFilename, const char *pReferenceSupplementalString, char *pErrorString, int nErrorStringLength )
  164. {
  165. // Build the xxxxx.vcproj.vpc_crc filename
  166. char szFilename[512];
  167. SafeSnprintf( szFilename, sizeof( szFilename ), "%s.%s", pProjectFilename, VPCCRCCHECK_FILE_EXTENSION );
  168. // Open it up.
  169. FILE *fp = fopen( szFilename, "rt" );
  170. if ( !fp )
  171. {
  172. SafeSnprintf( pErrorString, nErrorStringLength, "Unable to load %s to check CRC strings", szFilename );
  173. return false;
  174. }
  175. bool bReturnValue = false;
  176. char lineBuffer[2048];
  177. // Check the version of the CRC file.
  178. const char *pVersionString = ChompLineFromFile( lineBuffer, sizeof( lineBuffer ), fp );
  179. if ( pVersionString && stricmp( pVersionString, VPCCRCCHECK_FILE_VERSION_STRING ) == 0 )
  180. {
  181. // Check the supplemental CRC string.
  182. const char *pSupplementalString = ChompLineFromFile( lineBuffer, sizeof( lineBuffer ), fp );
  183. if ( CheckSupplementalString( pSupplementalString, pReferenceSupplementalString ) )
  184. {
  185. // Now read each line. Each line has a CRC and a filename on it.
  186. while ( 1 )
  187. {
  188. char *pLine = ChompLineFromFile( lineBuffer, sizeof( lineBuffer ), fp );
  189. if ( !pLine )
  190. {
  191. // We got all the way through the file without a CRC error, so all's well.
  192. bReturnValue = true;
  193. break;
  194. }
  195. char *pSpace = strchr( pLine, ' ' );
  196. if ( !pSpace )
  197. {
  198. SafeSnprintf( pErrorString, nErrorStringLength, "Invalid line ('%s') in %s", pLine, szFilename );
  199. break;
  200. }
  201. // Null-terminate it so we have the CRC by itself and the filename follows the space.
  202. *pSpace = 0;
  203. const char *pVPCFilename = pSpace + 1;
  204. // Parse the CRC out.
  205. unsigned int nReferenceCRC;
  206. sscanf( pLine, "%x", &nReferenceCRC );
  207. // Calculate the CRC from the contents of the file.
  208. char *pBuffer;
  209. int nTotalFileBytes = Sys_LoadTextFileWithIncludes( pVPCFilename, &pBuffer );
  210. if ( nTotalFileBytes == -1 )
  211. {
  212. SafeSnprintf( pErrorString, nErrorStringLength, "Unable to load %s for CRC comparison.", pVPCFilename );
  213. break;
  214. }
  215. CRC32_t nCRCFromTextContents = CRC32_ProcessSingleBuffer( pBuffer, nTotalFileBytes );
  216. delete [] pBuffer;
  217. // Compare them.
  218. if ( nCRCFromTextContents != nReferenceCRC )
  219. {
  220. SafeSnprintf( pErrorString, nErrorStringLength, "This VCPROJ is out of sync with its VPC scripts.\n %s mismatches (0x%x vs 0x%x).\n Please use VPC to re-generate!\n \n", pVPCFilename, nReferenceCRC, nCRCFromTextContents );
  221. break;
  222. }
  223. }
  224. }
  225. else
  226. {
  227. SafeSnprintf( pErrorString, nErrorStringLength, "Supplemental string mismatch." );
  228. }
  229. }
  230. else
  231. {
  232. SafeSnprintf( pErrorString, nErrorStringLength, "CRC file %s has an invalid version string ('%s')", szFilename, pVersionString ? pVersionString : "[null]" );
  233. }
  234. fclose( fp );
  235. return bReturnValue;
  236. }
  237. int VPC_OldeStyleCRCChecks( int argc, char **argv )
  238. {
  239. for ( int i=1; (i+2) < argc; )
  240. {
  241. const char *pTestArg = argv[i];
  242. if ( stricmp( pTestArg, "-crc" ) != 0 )
  243. {
  244. ++i;
  245. continue;
  246. }
  247. const char *pVPCFilename = argv[i+1];
  248. // Get the CRC value on the command line.
  249. const char *pTestCRC = argv[i+2];
  250. unsigned int nCRCFromCommandLine;
  251. sscanf( pTestCRC, "%x", &nCRCFromCommandLine );
  252. // Calculate the CRC from the contents of the file.
  253. char *pBuffer;
  254. int nTotalFileBytes = Sys_LoadTextFileWithIncludes( pVPCFilename, &pBuffer );
  255. if ( nTotalFileBytes == -1 )
  256. {
  257. Sys_Error( "Unable to load %s for CRC comparison.", pVPCFilename );
  258. }
  259. CRC32_t nCRCFromTextContents = CRC32_ProcessSingleBuffer( pBuffer, nTotalFileBytes );
  260. delete [] pBuffer;
  261. // Compare them.
  262. if ( nCRCFromTextContents != nCRCFromCommandLine )
  263. {
  264. Sys_Error( " \n This VCPROJ is out of sync with its VPC scripts.\n %s mismatches (0x%x vs 0x%x).\n Please use VPC to re-generate!\n \n", pVPCFilename, nCRCFromCommandLine, nCRCFromTextContents );
  265. }
  266. i += 2;
  267. }
  268. return 0;
  269. }
  270. int VPC_CommandLineCRCChecks( int argc, char **argv )
  271. {
  272. if ( argc < 2 )
  273. {
  274. fprintf( stderr, "Invalid arguments to " VPCCRCCHECK_EXE_FILENAME ". Format: " VPCCRCCHECK_EXE_FILENAME " [project filename]\n" );
  275. return 1;
  276. }
  277. const char *pFirstCRC = argv[1];
  278. // If the first argument starts with -crc but is not -crc2, then this is an old CRC check command line with all the CRCs and filenames
  279. // directly on the command line. The new format puts all that in a separate file.
  280. if ( pFirstCRC[0] == '-' && pFirstCRC[1] == 'c' && pFirstCRC[2] == 'r' && pFirstCRC[3] == 'c' && pFirstCRC[4] != '2' )
  281. {
  282. return VPC_OldeStyleCRCChecks( argc, argv );
  283. }
  284. if ( stricmp( pFirstCRC, "-crc2" ) != 0 )
  285. {
  286. fprintf( stderr, "Missing -crc2 parameter on vpc CRC check command line." );
  287. return 1;
  288. }
  289. const char *pProjectFilename = argv[2];
  290. char errorString[1024];
  291. bool bCRCsValid = VPC_CheckProjectDependencyCRCs( pProjectFilename, NULL, errorString, sizeof( errorString ) );
  292. if ( bCRCsValid )
  293. {
  294. return 0;
  295. }
  296. else
  297. {
  298. fprintf( stderr, "%s", errorString );
  299. return 1;
  300. }
  301. }