summaryrefslogtreecommitdiff
path: root/utils/newdat/newdat.cpp
blob: d03439a1b867dce16d9e45fc31dfa87622ed791a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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;
}