Using Vice without Steam

From Valve Developer Community
Revision as of 17:38, 31 January 2006 by Scottloyd (talk | contribs) (Cleaned it up a little more, and added linux support.)
Jump to navigation Jump to search

What is this?

This is a Steam port of VICE that is built to not depend on Steam. Works on windows and linux.

Compiling on Windows

  • 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.

Compiling on Linux

  • Copy the makefile into a file called Makefile
  • edit the Makefile to refleft the path to your SDK_ROOT/public/ directory
  • make

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 <stdlib.h>
#include <string.h>
#include "IceKey.H"

// 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];

#ifdef WIN32
#define STRING_CMP stricmp
#else
#define STRING_CMP strcasecmp
#undef NULL
#define NULL 0
#endif


//----- Helpers -----
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" );
}

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

void SetExtention(char *dest, size_t 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.
	size_t mover = length;

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

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

/*
The entry point, see usage for I/O
*/
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,0,MAX_ICE_KEY);

	int i = 1;
	while( i < argc )
	{
		if( STRING_CMP( argv[i], "-h" ) == 0 )
		{
			Usage();
			exit( 0 );
		} 
		else if( STRING_CMP( argv[i], "-d" ) == 0 )
		{
			g_Encrypt = false;
		} 
		else if( STRING_CMP( 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( STRING_CMP( 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 );

	size_t 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);
}

Linux Makefile

#Simple Makefile for compiling on linux
CPP = g++
PUBLIC=../../public
CPPFLAGS = ${PUBLIC}

ice:
	${CPP} -o $@ main.cpp ${PUBLIC}/IceKey.cpp -I${PUBLIC}

clean:
	-rm -f ice

See Also