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.

183 lines
3.4 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Makes .DAT files
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //-----------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdarg.h>
  16. #include <stdlib.h>
  17. #include "basetypes.h"
  18. #include "checksum_md5.h"
  19. #include "tier1/strtools.h"
  20. void Sys_Error( char *fmt, ... );
  21. extern void Con_Printf( char *fmt, ... );
  22. // So we can link CRC
  23. int LittleLongFn( int l );
  24. // So we can link CRC
  25. void Sys_Error( char *fmt, ... )
  26. {
  27. va_list args;
  28. va_start( args, fmt );
  29. vprintf( fmt, args );
  30. exit(0);
  31. }
  32. // So we can link CRC
  33. int COM_FindFile( char *filename, FILE **file )
  34. {
  35. if ( file )
  36. {
  37. *file = fopen( filename, "rb" );
  38. if ( *file )
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. // So we can link CRC
  44. int LittleLongFn( int l )
  45. {
  46. return l;
  47. }
  48. // So we can link CRC
  49. void Con_Printf( char *fmt, ... )
  50. {
  51. va_list args;
  52. va_start( args, fmt );
  53. vprintf( fmt, args );
  54. }
  55. bool MD5_Hash_File(unsigned char digest[16], char *pszFileName, bool bUsefopen /* = FALSE */, bool bSeed /* = FALSE */, unsigned int seed[4] /* = NULL */)
  56. {
  57. FILE *fp;
  58. byte chunk[1024];
  59. int nBytesRead;
  60. MD5Context_t ctx;
  61. int nSize;
  62. if (!bUsefopen)
  63. {
  64. nSize = COM_FindFile(pszFileName, &fp);
  65. if ( !fp || ( nSize == -1 ) )
  66. return false;
  67. }
  68. else
  69. {
  70. fp = fopen( pszFileName, "rb" );
  71. if ( !fp )
  72. return false;
  73. fseek ( fp, 0, SEEK_END );
  74. nSize = ftell ( fp );
  75. fseek ( fp, 0, SEEK_SET );
  76. if ( nSize <= 0 )
  77. {
  78. fclose ( fp );
  79. return false;
  80. }
  81. }
  82. memset(&ctx, 0, sizeof(MD5Context_t));
  83. MD5Init(&ctx);
  84. if (bSeed)
  85. {
  86. // Seed the hash with the seed value
  87. MD5Update( &ctx, (const unsigned char *)&seed[0], 16 );
  88. }
  89. // Now read in 1K chunks
  90. while (nSize > 0)
  91. {
  92. if (nSize > 1024)
  93. nBytesRead = fread(chunk, 1, 1024, fp);
  94. else
  95. nBytesRead = fread(chunk, 1, nSize, fp);
  96. // If any data was received, CRC it.
  97. if (nBytesRead > 0)
  98. {
  99. nSize -= nBytesRead;
  100. MD5Update(&ctx, chunk, nBytesRead);
  101. }
  102. // We we are end of file, break loop and return
  103. if ( feof( fp ) )
  104. {
  105. fclose( fp );
  106. fp = NULL;
  107. break;
  108. }
  109. // If there was a disk error, indicate failure.
  110. else if ( ferror(fp) )
  111. {
  112. if ( fp )
  113. fclose(fp);
  114. return FALSE;
  115. }
  116. }
  117. if ( fp )
  118. fclose(fp);
  119. MD5Final(digest, &ctx);
  120. return TRUE;
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Purpose: newdat.exe - makes the .DAT signature for file / virus checking
  124. // Input : argc - std args
  125. // *argv[] -
  126. // Output : int 0 == success. 1 == failure
  127. //-----------------------------------------------------------------------------
  128. int main( int argc, char *argv[] )
  129. {
  130. char out[512], datFile[512];
  131. unsigned char digest[16];
  132. if ( argc < 2 )
  133. {
  134. printf("USAGE: newdat <filename>\n" );
  135. return 1;
  136. }
  137. // Get the filename without the extension
  138. Q_StripExtension( argv[1], out, sizeof( out ) );
  139. sprintf( datFile, "%s.dat", out );
  140. // Build the MD5 hash for the .EXE file
  141. MD5_Hash_File( digest, argv[1], TRUE, FALSE, NULL );
  142. // Write the first 4 bytes of the MD5 hash as the signature ".dat" file
  143. FILE *fp = fopen( datFile, "wb" );
  144. if ( fp )
  145. {
  146. fwrite( digest, sizeof(int), 1, fp );
  147. fclose( fp );
  148. printf("Wrote %s\n", datFile );
  149. return 0;
  150. }
  151. else
  152. printf("Can't open %s\n", datFile );
  153. return 1;
  154. }