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.

184 lines
3.5 KiB

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