Using Vice without Steam

From Valve Developer Community
Revision as of 04:01, 29 January 2006 by Scottloyd (talk | contribs)
Jump to navigation Jump to search

What is this?

This is a *port* of vice that is built to not depend on Steam.

Usage

  • First you need to create a new Empty VisualC++ Console Project that will compile this code into an executable.
  • Add public/IceKey.cpp into your project
  • Setup the include paths in your settings to include public/ (for the header file)
  • Copy the code below into main.cpp, and compile.

main.cpp

//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: Standalone utility to encrypt files with ice encryption, that doesn't
//			 depend on Steam.
//
// Author: Valve Software and Scott Loyd (scottloyd@gmail.com).
//			Tested and proofread by Me2.
// Depends on: Just needs public/IceKey.cpp to be compiled/linked with it
// $NoKeywords: $
//
//=============================================================================//

#include <stdio.h>
#include "IceKey.H"

#ifdef WIN32
 #include "windows.h"
#else
#endif

// Globals
static bool g_Encrypt = true; //By default we encrypt

#define MAX_ICE_KEY 8
#define MAX_EXTENTION 16
static char g_ICEKey[MAX_ICE_KEY];
static char g_Extension[MAX_EXTENTION];

void Usage( void );
void Exit(const char *msg);
void SetExtention(const char *dest, int length, const char *ext);


int main(int argc, char* argv[])
{
	if(argc < 2)
	{
		Usage();
		exit( 0 );
	}
	
	//By default we output .ctx
	strncpy( g_Extension, ".ctx",MAX_EXTENTION );
	memset(g_ICEKey,NULL,MAX_ICE_KEY);

	int i = 1;
	while( i < argc )
	{
		if( stricmp( argv[i], "-h" ) == 0 )
		{
			Usage();
			exit( 0 );
		} 
		else if( stricmp( argv[i], "-d" ) == 0 )
		{
			g_Encrypt = false;
		} 
		else if( stricmp( argv[i], "-x" ) == 0 )
		{
			//Extention
			i++;

			if ( strlen( argv[i] ) > MAX_EXTENTION )
			{
				Exit("Your Extention is too big.\n");
			}

			strncpy( g_Extension, argv[i], MAX_EXTENTION );
		}
		else if( stricmp( argv[i], "-k" ) == 0 )
		{
			//Key
			i++;

			if ( strlen( argv[i] ) != MAX_ICE_KEY )
			{
				Exit("Your ICE key needs to be 8 characters long.\n");
			}

			strncpy( g_ICEKey, argv[i], MAX_ICE_KEY );
		}
		else 
		{
			break;
		}
		i++;
	}

	if(g_ICEKey[0] == '\0') {
		Exit("You need to specify a key.\n");
	}
	//Parse files starting from current arg position
	if(argv[i] == NULL && (strlen(argv[i]) < 1))
		Exit("Was not about to find a file to parse\n");

	//Open allocate/read a file into memory
	FILE *pFile;
	pFile = fopen (argv[i], "rb");
	if(! pFile)
		Exit("Failed to open input file\n");

	long lFileSize; //Size of input file
	unsigned char *pBuff; //Holds the input file contents
	unsigned char *pOutBuff; //Holds the output goodies

	// obtain file size.
	fseek (pFile , 0 , SEEK_END);
	lFileSize= ftell (pFile);
	rewind (pFile);

	// allocate memory to contain the whole file.
	pBuff = (unsigned char*) malloc (lFileSize);
	pOutBuff = (unsigned char*) malloc (lFileSize);

	if (pBuff == NULL || pOutBuff == NULL)
	{
		fclose(pFile);
		Exit("Could not allocate buffer\n");;
	}

	// copy the file into the buffer.
	fread (pBuff,1,lFileSize,pFile);
	
	//clean the output buffer
	memset(pOutBuff,NULL,lFileSize);

	fclose(pFile);

	//Lets start the ice goodies!
	IceKey ice( 0 ); // level 0 = 64bit key
	ice.set( (unsigned char*) g_ICEKey ); // set key

	int blockSize = ice.blockSize();

	unsigned char *p1 = pBuff;
	unsigned char *p2 = pOutBuff;

	// encrypt data in 8 byte blocks
	int bytesLeft = lFileSize;

	while ( bytesLeft >= blockSize )
	{
		if ( g_Encrypt )
			ice.encrypt( p1, p2 );
		else
			ice.decrypt( p1, p2 );

		bytesLeft -= blockSize;
		p1+=blockSize;
		p2+=blockSize;
	}

	//The end chunk doesn't get an encryption?  that sux...
	memcpy( p2, p1, bytesLeft );

	int outLength = strlen(argv[i]) + MAX_EXTENTION + 1;
	char *pOutPath = (char *)malloc(outLength);
	strncpy(pOutPath,argv[i],outLength);

	SetExtention(pOutPath, outLength, g_Extension);

	pFile = fopen (pOutPath , "w");
	if(pFile == NULL)
	{
		fprintf( stderr, "Was not able to open output file for writing.\n" );
		goto dealloc;
	}

	fwrite (pOutBuff , 1 , lFileSize , pFile);
	fclose (pFile);

dealloc:
	free(pBuff);
	free(pOutBuff);
	free(pOutPath);
}




//----- Helpers -----
static void Usage( void )
{
	fprintf( stdout, "Usage: ice <-d> <-x ext> [-k IceKey(8 bit)] [file]\n" );
	fprintf( stdout, "Default action: Encrypt. \n" );
	fprintf( stdout, "-d : Decrypt a file. \n" );
	fprintf( stdout, "-x : Extention to use as output. \n" );
	fprintf( stdout, "-k : You need to specify your 8 bit Ice Encryption Key. \n" );
	fprintf( stdout, "-h : Print the help menu, and stop.\n" );
}

static void Exit(const char *msg)
{
	fprintf( stderr, msg );
	exit( 1 );
}

static void SetExtention(const char *dest, int length, const char *ext)
{
	//Start at the end till we hit a .
	//if we reach 0 without a .  just append the extension; one must not have existed.
	int mover = length;

	while( (*(dest + mover) != '.') && (mover > 0))
		mover--;

	if(mover == 0)
		strcat((char *)dest,ext);
	else
	{
		strcpy((char *)(dest + mover),ext);
	}
}