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.

277 lines
5.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // vice.cpp : Defines the entry point for the console application.
  9. //
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <malloc.h>
  13. #include <string.h>
  14. #include "tier1/strtools.h"
  15. #include <sys/stat.h>
  16. #include "conio.h"
  17. #include <direct.h>
  18. #include <io.h>
  19. #include "UtlBuffer.h"
  20. #include "tier0/dbg.h"
  21. #include "cmdlib.h"
  22. #include "tier0/icommandline.h"
  23. #include "windows.h"
  24. #include "mathlib/IceKey.h"
  25. #include <filesystem_tools.h>
  26. #define FF_TRYAGAIN 1
  27. #define FF_DONTPROCESS 2
  28. #undef GetCurrentDirectory
  29. static bool g_NoPause = false;
  30. static bool g_Quiet = false;
  31. static bool g_Encrypt = false;
  32. static bool g_Decrypt = false;
  33. static char g_ICEKey[16];
  34. static char g_Extension[16];
  35. static void Pause( void )
  36. {
  37. if( !g_NoPause )
  38. {
  39. printf( "Hit a key to continue\n" );
  40. getch();
  41. }
  42. }
  43. static void Exit(const char *msg)
  44. {
  45. fprintf( stderr, "%s", msg );
  46. Pause();
  47. exit( -1 );
  48. }
  49. static void Usage( void )
  50. {
  51. fprintf( stderr, "Usage: vice [-quiet] [-nopause] [-encrypt key] [-decrypt key] [-newext name] file file2 . . .\n" );
  52. fprintf( stderr, "-quiet : don't print anything out, don't pause for input\n" );
  53. fprintf( stderr, "-nopause : don't pause for input\n" );
  54. fprintf( stderr, "-encrypt : encrypt files with given key\n" );
  55. fprintf( stderr, "-decrypt : decypt files with given key\n" );
  56. fprintf( stderr, "-newext : new output file extension\n" );
  57. Pause();
  58. exit( -1 );
  59. }
  60. bool Process_File( char *pInputBaseName, int maxlen )
  61. {
  62. Q_FixSlashes( pInputBaseName, '/' );
  63. // Q_StripExtension( pInputBaseName, pInputBaseName, maxlen );
  64. if( !g_Quiet )
  65. {
  66. printf( "input file: %s\n", pInputBaseName );
  67. }
  68. FileHandle_t f = g_pFullFileSystem->Open(pInputBaseName, "rb", "vice" );
  69. if (!f)
  70. Error("Could not open input file");
  71. int fileSize = g_pFullFileSystem->Size(f);
  72. unsigned char *buffer = (unsigned char*)_alloca(fileSize);
  73. g_pFullFileSystem->Read(buffer, fileSize, f); // read into local buffer
  74. g_pFullFileSystem->Close( f ); // close file after reading
  75. IceKey ice( 0 ); // level 0 = 64bit key
  76. ice.set( (unsigned char*) g_ICEKey ); // set key
  77. int blockSize = ice.blockSize();
  78. unsigned char *temp = (unsigned char *)_alloca( fileSize );
  79. unsigned char *p1 = buffer;
  80. unsigned char *p2 = temp;
  81. // encrypt data in 8 byte blocks
  82. int bytesLeft = fileSize;
  83. while ( bytesLeft >= blockSize )
  84. {
  85. if ( g_Encrypt )
  86. {
  87. ice.encrypt( p1, p2 );
  88. }
  89. else if ( g_Decrypt )
  90. {
  91. ice.decrypt( p1, p2 );
  92. }
  93. else
  94. {
  95. memcpy( p2, p1, blockSize );
  96. }
  97. bytesLeft -= blockSize;
  98. p1+=blockSize;
  99. p2+=blockSize;
  100. }
  101. memcpy( p2, p1, bytesLeft );
  102. Q_SetExtension( pInputBaseName, g_Extension, maxlen );
  103. if( !g_Quiet )
  104. {
  105. printf( "output file: %s\n", pInputBaseName );
  106. }
  107. f = g_pFullFileSystem->Open(pInputBaseName, "wb", "vice" );
  108. if (!f)
  109. Exit("Could not open output file");
  110. g_pFullFileSystem->Write( temp, fileSize, f ); // read into local buffer
  111. g_pFullFileSystem->Close( f ); // close file after reading
  112. return TRUE;
  113. }
  114. int main(int argc, char* argv[])
  115. {
  116. CommandLine()->CreateCmdLine( argc, argv );
  117. if( argc < 2 )
  118. {
  119. Usage();
  120. }
  121. char *pInputBaseName = NULL;
  122. int i = 1;
  123. strcpy( g_Extension, ".dat" );
  124. while( i < argc )
  125. {
  126. if( stricmp( argv[i], "-quiet" ) == 0 )
  127. {
  128. i++;
  129. g_Quiet = true;
  130. g_NoPause = true; // no point in pausing if we aren't going to print anything out.
  131. }
  132. if( stricmp( argv[i], "-nopause" ) == 0 )
  133. {
  134. i++;
  135. g_NoPause = true;
  136. }
  137. if( stricmp( argv[i], "-encrypt" ) == 0 )
  138. {
  139. g_Encrypt = true;
  140. i++;
  141. if ( strlen( argv[i] ) != 8 )
  142. {
  143. Exit("Error - ICE key must be a 8 char text.\n");
  144. }
  145. Q_strncpy( g_ICEKey, argv[i], sizeof(g_ICEKey) );
  146. i++;
  147. }
  148. if( stricmp( argv[i], "-decrypt" ) == 0 )
  149. {
  150. g_Decrypt = true;
  151. i++;
  152. if ( strlen( argv[i] ) != 8 )
  153. {
  154. Exit("Error - ICE key must be a 8 char text.\n");
  155. }
  156. Q_strncpy( g_ICEKey, argv[i], sizeof(g_ICEKey) );
  157. i++;
  158. }
  159. if( stricmp( argv[i], "-newext" ) == 0 )
  160. {
  161. i++;
  162. if ( strlen( argv[i] ) > 5 )
  163. {
  164. Exit("Error - extension must be smaller than 4 chars.\n");
  165. }
  166. Q_strncpy( g_Extension, argv[i], sizeof(g_Extension) );
  167. i++;
  168. }
  169. else
  170. {
  171. break;
  172. }
  173. }
  174. if ( i >= argc )
  175. {
  176. Exit("Error - missing files in commandline.\n");
  177. }
  178. CmdLib_InitFileSystem( argv[i] );
  179. g_pFullFileSystem->GetCurrentDirectory( gamedir, sizeof(gamedir) );
  180. g_pFullFileSystem->AddSearchPath( gamedir, "vice" );
  181. Q_FixSlashes( gamedir, '/' );
  182. for( ; i < argc; i++ )
  183. {
  184. pInputBaseName = argv[i];
  185. int maxlen = Q_strlen( pInputBaseName ) + 1;
  186. if ( strstr( pInputBaseName, "*.") )
  187. {
  188. char search[ MAX_PATH ];
  189. char fname[ MAX_PATH ];
  190. char ext[_MAX_EXT];
  191. _splitpath( pInputBaseName, NULL, NULL, fname, ext ); //find extension wanted
  192. fname[strlen(fname)-1] = 0; // remove *
  193. sprintf( search, "%s\\*%s", gamedir, ext );
  194. Q_FixSlashes( search, '/' );
  195. WIN32_FIND_DATA wfd;
  196. HANDLE hResult;
  197. memset(&wfd, 0, sizeof(WIN32_FIND_DATA));
  198. hResult = FindFirstFile( search, &wfd );
  199. while ( hResult != INVALID_HANDLE_VALUE )
  200. {
  201. if ( !strnicmp( fname, wfd.cFileName, strlen(fname) ) )
  202. {
  203. if ( !Process_File( wfd.cFileName, sizeof( wfd.cFileName ) ) )
  204. break;
  205. }
  206. if ( !FindNextFile( hResult, &wfd) )
  207. break;
  208. }
  209. FindClose( hResult );
  210. }
  211. else
  212. {
  213. Process_File( pInputBaseName, maxlen );
  214. }
  215. }
  216. Pause();
  217. return 0;
  218. }