/* TIMZip V0.1B (c) F Javier Ventoso Reigosa TIM Compression Utility for Net Yaroze */ #include "stdio.h" #include "dos.h" #define TIM_4BITS 0 #define TIM_8BITS 1 #define TIM_16BITS 2 #define TIM_24BITS 3 #define LENGTH_4 32+16*2 #define LENGTH_8 32+256*2 typedef struct { char version; char id; short int pad; // flags char flags; char pad2; short int pad3; } TIM_HEADER; FILE *pInputFile; char *cTimName; TIM_HEADER tim_header; long length_input = 0; long length_output = 0; // change big endian by little or viceversa ( 2 bytes long ) unsigned short usBigEndian2LittleEndian( unsigned short bigendian ) { unsigned short littleendian = bigendian << 8; littleendian += (bigendian & 0xFF00) >> 8; return( littleendian ); } char params( char *param, int argc, char *argv[] ) { int ncon; for( ncon = 1; ncon < argc; ncon++ ) { if( strstr( strlwr( argv[ncon] ), strlwr( param ) ) ) return( ncon ); } return( 0 ); } int OpenInputFile( int argc, char *argv[] ) { if( argc > 1 ) { if( params( "-i", argc, argv ) ) { cTimName = argv[params( "-i", argc, argv )] + 2; if( !(pInputFile = fopen( cTimName, "rb" )) ) { printf( "ž Error opening %s \n", cTimName ); return( 0 ); } return( 1 ); } else { printf( "ž You must include -i \n" ); return( 0 ); } } return( 0 ); } int CheckTim( void ) { // read tim header fread( &tim_header, sizeof( tim_header ), 1, pInputFile ); // correct tim ? if( ( tim_header.version != 0x10 ) || ( tim_header.id != 0x00 ) ) { printf( "ž %s isn't a TIM file... \n", cTimName ); fclose( pInputFile ); return( 0 ); } return( 1 ); } int SaveCompressFile( int argc, char *argv[] ) { FILE *pOutputFile; char *cOutputFile; long ncon; unsigned char byte_read; unsigned char nrep; unsigned char *buffer; unsigned char bufftemp[LENGTH_8]; // open output file if( params( "-o", argc, argv ) ) { // use -o name cOutputFile = argv[params( "-o", argc, argv )] + 2; } else { // use output.tiz cOutputFile = "output.tiz"; } if( !(pOutputFile = fopen( cOutputFile, "w+b" )) ) { printf( "ž Error creating %s \n", cOutputFile ); fclose( pInputFile ); return( 0 ); } // get length of input file fseek( pInputFile, 0L, SEEK_END ); fgetpos( pInputFile, &length_input ); // retore file pointer to top rewind( pInputFile ); // reserve memory if( !(buffer = ( char * ) malloc( length_input+1 )) ) { printf( "ž Sorry, there's no memory... \n" ); fclose( pInputFile ); fclose( pOutputFile ); return( 0 ); } // save tim header as it is if( (tim_header.flags & 7) == TIM_4BITS ) { fread( bufftemp, LENGTH_4, 1, pInputFile ); fwrite( bufftemp, LENGTH_4, 1, pOutputFile ); length_input -= LENGTH_4; fseek( pInputFile, LENGTH_4, SEEK_SET ); } else if( (tim_header.flags & 7) == TIM_8BITS ) { fread( bufftemp, LENGTH_8, 1, pInputFile ); fwrite( bufftemp, LENGTH_8, 1, pOutputFile ); length_input -= LENGTH_8; fseek( pInputFile, LENGTH_8, SEEK_SET ); } // copy the rest of input file into memory buffer ncon = 0; for( ;; ) { buffer[ncon] = fgetc( pInputFile ); ncon++; if( feof( pInputFile ) ) { break; } } // compress for( ncon = 0; ncon < length_input; ) { byte_read = buffer[ncon]; nrep = 193; while( (byte_read == buffer[++ncon]) && (ncon < length_input) ) { nrep++; if( nrep > 254 ) { ncon++; break; } } if( (nrep == 193) && (byte_read < 193) ) { fputc( byte_read, pOutputFile ); length_output++; } else { fputc( nrep, pOutputFile ); fputc( byte_read, pOutputFile ); length_output += 2; } } fclose( pInputFile ); fclose( pOutputFile ); free( buffer ); return( 1 ); } int ExpandCompressFile( int argc, char *argv[] ) { FILE *pOutputFile; char *cOutputFile; long ncon; unsigned char byte_read; unsigned char nrep; unsigned char *buffer; unsigned char bufftemp[LENGTH_8]; // open output file if( params( "-o", argc, argv ) ) { // use -o name cOutputFile = argv[params( "-o", argc, argv )] + 2; } else { // use output.tiz cOutputFile = "output.tiz"; } if( !(pOutputFile = fopen( cOutputFile, "w+b" )) ) { printf( "ž Error creating %s \n", cOutputFile ); fclose( pInputFile ); return( 0 ); } // get length of input file fseek( pInputFile, 0L, SEEK_END ); fgetpos( pInputFile, &length_input ); // retore file pointer to top rewind( pInputFile ); // reserve memory if( !(buffer = ( char * ) malloc( length_input+1 )) ) { printf( "ž Sorry, there's no memory... \n" ); fclose( pInputFile ); fclose( pOutputFile ); return( 0 ); } // restore tim header as it is if( (tim_header.flags & 7) == TIM_4BITS ) { fread( bufftemp, LENGTH_4, 1, pInputFile ); fwrite( bufftemp, LENGTH_4, 1, pOutputFile ); length_input -= LENGTH_4; } else if( (tim_header.flags & 7) == TIM_8BITS ) { fread( bufftemp, LENGTH_8, 1, pInputFile ); fwrite( bufftemp, LENGTH_8, 1, pOutputFile ); length_input -= LENGTH_8; } // copy input file into memory buffer ncon = 0; for( ;; ) { buffer[ncon] = fgetc( pInputFile ); ncon++; if( feof( pInputFile ) ) { break; } } // expand for( ncon = 0; ncon < length_input; ) { byte_read = buffer[ncon]; if( byte_read > 192 ) { nrep = byte_read - 192; ncon++; byte_read = buffer[ncon]; while( nrep ) { fputc( byte_read, pOutputFile ); length_output++; nrep--; } } else { fputc( byte_read, pOutputFile ); length_output++; } ncon++; } fclose( pInputFile ); fclose( pOutputFile ); free( buffer ); return( 1 ); } int CheckInputOutputNames( int argc, char *argv[] ) { if( !params( "-o", argc, argv ) ) { if( !strcmp( cTimName, "output.tiz" ) ) { printf( "ž Input file is equal to Output file! \n" ); fclose( pInputFile ); return( 0 ); } printf( "ž Using default output name 'output.tiz'... \n" ); } else if( !strcmp( cTimName, argv[params( "-o", argc, argv )] + 2 ) ) { printf( "ž Input file is equal to Output file! \n" ); fclose( pInputFile ); return( 0 ); } return( 1 ); } int main( int argc, char *argv[] ) { long porcentage; if( argc == 1 ) { clrscr(); printf( "ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n" ); printf( "ÄÄÄÄ RLE encoding TimZip V0.1įeta GNU 32 bits ÄÄÄÄ \n" ); printf( "ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n" ); printf( "ÄÄÄÄ F Javier Ventoso Reigosa 23/IX/1998 ÄÄÄÄ \n" ); printf( "ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n" ); printf( "³\n" ); printf( "³ Use: -i -o[output_file] -x ( eXpand ) \n" ); printf( "³ Example: TimZip -iMenu.tim -oMenu.tiz \n" ); printf( "³ TimZip -iMenu.tiz -oMenu.tim -x ( expand a compress file ) \n" ); printf( "³\n" ); printf( "³ This program is a įeta version, only works with 4 and 8 bits TIM files. \n" ); printf( "³\n" ); printf( "³\n" ); printf( "³ Next updates: Full TIM/VAB/VAG support \n" ); printf( "³ Huffman/LZW+RLE encoding \n" ); printf( "³ or Fractal/JPG encoding \n" ); printf( "³\n" ); printf( "³\n" ); printf( "³ Please, if you found any bug send me an e-mail to: \n" ); printf( "³\n" ); printf( "³ javier0003@mundivia.es or javier_ventoso@iname.com \n" ); printf( "³\n" ); printf( "\n" ); } else { printf( "ž TimZip V0.1į ( compress PSX TIM files ) \n" ); } if( !OpenInputFile( argc, argv ) ) { return( 1 ); } if( !CheckInputOutputNames( argc, argv ) ) { return( 1 ); } if( !CheckTim() ) { return( 1 ); } // expand ? if( params( "-x", argc, argv ) && params( "-i", argc, argv ) ) { printf( "ž Expanding... \n" ); ExpandCompressFile( argc, argv ); return( 0 ); } if( (tim_header.flags & 7) == TIM_4BITS || (tim_header.flags & 7) == TIM_8BITS ) { printf( "ž Compressing " ); if( (tim_header.flags & 7) == TIM_4BITS ) { printf( "4 bits TIM... " ); } else { printf( "8 bits TIM... " ); } SaveCompressFile( argc, argv ); porcentage = length_output * 100 / length_input; printf( "%d%% ", porcentage ); if( length_output > length_input ) { printf( "\nž Atchung! Compressed file is bigger than original TIM! \n" ); } else if( length_output == length_input ) { printf( "\nž Atchung! Compressed file is equal to original TIM! \n" ); } else { printf( "\n" ); } } if( (tim_header.flags & 7) == TIM_16BITS || (tim_header.flags & 7) == TIM_24BITS ) { printf( "ž Sorry, this program is a įeta version, \n only works with 4 & 8 bits TIM formats...\n" ); //Save16CompressFile( argc, argv ); } }