codice:
int main( int argc, char **argv )
{
FILE *f;
unsigned char *in, *out, command, algo=0;
unsigned int insize, outsize=0;
char *inname, *outname;
/* Check arguments */
if( argc < 4 )//not enough arguments for the program to run properly
{
Help( argv[ 0 ] ); //just in case the program is named something else
return 0; //exit
}
/* Get command */
command = argv[1][0]; /*the first argument after the program name lets the
program know whether it is compressing or decompressing*/
if( (command != 'c') && (command != 'd') )
{
Help( argv[ 0 ] ); //if you didn't pick either c or d
return 0;
}
/* Get algo */
if( argc == 5 && command == 'c' ) /*if there are enough parms and you wanted
compression*/
{
algo = 0; //initializes algo
if( strcmp( argv[2], "h" ) == 0 ) algo = 2; //2 is for huffman
if( strcmp( argv[2], "l" ) == 0 ) algo = 9; //9 is for lz77
if( !algo ) //if algo has not changed
{
Help( argv[ 0 ] ); //displays help
return 0;
}
inname = argv[ 3 ]; //sets inname to the 3rd value in program call
outname = argv[ 4 ]; //sets outname to the 4th valu in program call
}
else if( argc == 4 && command == 'd' ) /*if there are the right number of
arguments and command =d*/
{
inname = argv[ 2 ]; //argument after d becomes inname
outname = argv[ 3 ]; //argument after that becomes outname
}
else //user doesn't know what's going on
{
Help( argv[ 0 ] );
return 0;
}
/* Open input file */
f = fopen( inname, "rb" ); /*open a file in read and binary mode and identify
it by f*/
if( !f ) //if file does not exist
{
printf( "Unable to open input file \"%s\".\n", inname );
return 0;
}
/* Get input file size */
insize = GetFileSize( f ); /*calls the GetFileSize function on f and puts
the value in insize*/
//Austin's Code /*Check File size. If greater than 2^32, abort*/
if (insize >(4000000))
{
printf ("File size is too large. Aborting... \n");
return 0;
}
/* Decompress? */
if( command == 'd' ) //if user wants to decompress
{
/* Read header */
algo = ReadWord32( f ); // dummy
algo = ReadWord32( f ); /*first word in a file compressed using this
algorithm determines the algo to decompress it*/
outsize = ReadWord32( f ); /*next word is the size of the original file*/
insize -= 12; //the first three words (12bytes) of the infile are gone
}
/* Print operation... */
switch( algo )
{
case 2: printf( "Huffman " ); break;
case 9: printf( "LZ77 " ); break;
}
switch( command )
{
case 'c': printf( "compress " ); break;
case 'd': printf( "decompress " ); break;
}
printf( "%s to %s...\n", inname, outname );
/* Read input file */
printf( "Input file: %d bytes\n", insize ); /*outputs name of file and the
number of bytes in it*/
in = (unsigned char *) malloc( insize ); /*puts a pointer to the newly
allocated chunk of memory the size of inzize in in*/
if( !in ) //if the memory was not allocated
{
printf( "Not enough memory\n" );
fclose( f );
return 0;
}
fread( in, insize, 1, f ); /*takes the file f and reads the entire thing
into the memory addressed by in one byte at a time*/
fclose( f ); //closes file f
/* Show output file size for decompression */
if( command == 'd' )
{
printf( "Output file: %d bytes\n", outsize );
//shows the outfile name and its size (calculated according to insize)
}
/* Open output file */
f = fopen( outname, "wb" ); /*opens the file called outname and calls it f
the file is writable and binary*/
if( !f ) //file did not open
{
printf( "Unable to open output file \"%s\".\n", outname );
free( in ); //frees the in memory and exits program
return 0;
}
/* Compress? */
if( command == 'c' )
{
/* Write header */
fwrite( "BCL1", 4, 1, f ); //writes the dummy header
WriteWord32( algo, f ); //writes the name of algo used
WriteWord32( insize, f ); //writes the size of the original file
/* Worst case buffer size */
outsize = (insize*104+50)/100 + 384; /*I don't know where these numbers
came from*/
}
/* Allocate memory for output buffer */
out = malloc( outsize ); /* makes outsize room in memory and puts the pointer
to it in out*/
if( !out )//memory was not allocated
{
printf( "Not enough memory\n" );
fclose( f ); //close file
free( in ); //free the memory addressed by in
return 0; //exit program
}
/* Compress or decompress */
if( command == 'c' ) //if user wanted to compress
{
switch( algo ) //algo is the index of the algorithms
{
case 2:
outsize = Huffman_Compress( in, out, insize ); /*compresses
in buffer, writes it to out buffer, and returns the size of the
out buffer*/
break;
case 9:
outsize = LZ_Compress( in, out, insize ); /*compresses
in buffer, writes it to out buffer, and returns the size of the
out buffer*/
break;
}
printf( "Output file: %d bytes (%.1f%%)\n", outsize,
100*(float)outsize/(float)insize );
}
else
{
switch( algo )
{
case 2:
Huffman_Uncompress( in, out, insize, outsize );/*uncompresses
in buffer and writes it to out buffer)*/
break;
case 9:
LZ_Uncompress( in, out, insize );/*uncompresses
in buffer and writes it to out buffer)*/
break;
}
}
/* Write output file */
fwrite( out, outsize, 1, f );/*writes outsize pieces of the out buffer
to f one byte at a time*/
fclose( f ); //closes f
/* Free memory */
free( in ); //deallocates buffer addressed by in
free( out ); //deallocates buffer addressed by out
return 0;
}