diff options
| author | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
|---|---|---|
| committer | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
| commit | 3bf9df6b2785fa6d951086978a3e66f49427166a (patch) | |
| tree | 2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /utils/newdat | |
| download | archived-source-engine-2018-hl2-src-master.tar.xz archived-source-engine-2018-hl2-src-master.zip | |
Diffstat (limited to 'utils/newdat')
| -rw-r--r-- | utils/newdat/newdat.cpp | 184 | ||||
| -rw-r--r-- | utils/newdat/newdat.vpc | 33 |
2 files changed, 217 insertions, 0 deletions
diff --git a/utils/newdat/newdat.cpp b/utils/newdat/newdat.cpp new file mode 100644 index 0000000..d03439a --- /dev/null +++ b/utils/newdat/newdat.cpp @@ -0,0 +1,184 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: Makes .DAT files +// +// $Workfile: $ +// $Date: $ +// +//----------------------------------------------------------------------------- +// $Log: $ +// +// $NoKeywords: $ +//=============================================================================// + +#include <stdio.h> +#include <string.h> +#include <stdarg.h> +#include <stdlib.h> +#include "basetypes.h" +#include "checksum_md5.h" +#include "tier1/strtools.h" + +void Sys_Error( char *fmt, ... ); +extern void Con_Printf( char *fmt, ... ); + +// So we can link CRC +int LittleLongFn( int l ); +int (*LittleLong)(int l) = LittleLongFn; + + +// So we can link CRC +void Sys_Error( char *fmt, ... ) +{ + va_list args; + va_start( args, fmt ); + vprintf( fmt, args ); + exit(0); +} + +// So we can link CRC +int COM_FindFile( char *filename, FILE **file ) +{ + if ( file ) + { + *file = fopen( filename, "rb" ); + if ( *file ) + return 1; + } + return 0; +} + +// So we can link CRC +int LittleLongFn( int l ) +{ + return l; +} + +// So we can link CRC +void Con_Printf( char *fmt, ... ) +{ + va_list args; + va_start( args, fmt ); + vprintf( fmt, args ); +} + +bool MD5_Hash_File(unsigned char digest[16], char *pszFileName, bool bUsefopen /* = FALSE */, bool bSeed /* = FALSE */, unsigned int seed[4] /* = NULL */) +{ + FILE *fp; + byte chunk[1024]; + int nBytesRead; + MD5Context_t ctx; + + int nSize; + + if (!bUsefopen) + { + nSize = COM_FindFile(pszFileName, &fp); + if ( !fp || ( nSize == -1 ) ) + return false; + } + else + { + fp = fopen( pszFileName, "rb" ); + if ( !fp ) + return false; + + fseek ( fp, 0, SEEK_END ); + nSize = ftell ( fp ); + fseek ( fp, 0, SEEK_SET ); + + if ( nSize <= 0 ) + { + fclose ( fp ); + return false; + } + } + + memset(&ctx, 0, sizeof(MD5Context_t)); + + MD5Init(&ctx); + + if (bSeed) + { + // Seed the hash with the seed value + MD5Update( &ctx, (const unsigned char *)&seed[0], 16 ); + } + + // Now read in 1K chunks + while (nSize > 0) + { + if (nSize > 1024) + nBytesRead = fread(chunk, 1, 1024, fp); + else + nBytesRead = fread(chunk, 1, nSize, fp); + + // If any data was received, CRC it. + if (nBytesRead > 0) + { + nSize -= nBytesRead; + MD5Update(&ctx, chunk, nBytesRead); + } + + // We we are end of file, break loop and return + if ( feof( fp ) ) + { + fclose( fp ); + fp = NULL; + break; + } + // If there was a disk error, indicate failure. + else if ( ferror(fp) ) + { + if ( fp ) + fclose(fp); + return FALSE; + } + } + + if ( fp ) + fclose(fp); + + MD5Final(digest, &ctx); + + return TRUE; +} + +//----------------------------------------------------------------------------- +// Purpose: newdat.exe - makes the .DAT signature for file / virus checking +// Input : argc - std args +// *argv[] - +// Output : int 0 == success. 1 == failure +//----------------------------------------------------------------------------- +int main( int argc, char *argv[] ) +{ + char out[512], datFile[512]; + unsigned char digest[16]; + + if ( argc < 2 ) + { + printf("USAGE: newdat <filename>\n" ); + return 1; + } + + + // Get the filename without the extension + Q_StripExtension( argv[1], out, sizeof( out ) ); + sprintf( datFile, "%s.dat", out ); + + // Build the MD5 hash for the .EXE file + MD5_Hash_File( digest, argv[1], TRUE, FALSE, NULL ); + + // Write the first 4 bytes of the MD5 hash as the signature ".dat" file + FILE *fp = fopen( datFile, "wb" ); + if ( fp ) + { + fwrite( digest, sizeof(int), 1, fp ); + fclose( fp ); + printf("Wrote %s\n", datFile ); + return 0; + } + else + printf("Can't open %s\n", datFile ); + + return 1; +} diff --git a/utils/newdat/newdat.vpc b/utils/newdat/newdat.vpc new file mode 100644 index 0000000..20db5ee --- /dev/null +++ b/utils/newdat/newdat.vpc @@ -0,0 +1,33 @@ +//----------------------------------------------------------------------------- +// NEWDAT.VPC +// +// Project Script +//----------------------------------------------------------------------------- + +$Macro SRCDIR "..\.." +$Macro OUTBINDIR "$SRCDIR\..\game\bin" + +$Include "$SRCDIR\vpc_scripts\source_exe_con_base.vpc" + +$Configuration +{ + $Compiler + { + $PreprocessorDefinitions "$BASE;PROTECTED_THINGS_DISABLE" + } +} + +$Project "Newdat" +{ + $Folder "Source Files" + { + $File "newdat.cpp" + } + + $Folder "Header Files" + { + $File "$SRCDIR\public\tier0\basetypes.h" + $File "$SRCDIR\public\tier1\checksum_md5.h" + $File "$SRCDIR\public\tier0\commonmacros.h" + } +} |