//********************************************************************* // // Lempel-Ziv based data uncompression // Copyright Ver 1.0 // ECE of UMass-Amherst March 10, 98 // //********************************************************************* #include #define L_WIN 128 #define L_LOOK 64 #define L L_WIN+L_LOOK main(int argc, char *argv[]) { unsigned char x[L+1]; // use last one to save at all-matching unsigned pointer=0, matchLength=0, lastSymbol=0; int i, j, gameOver=0; FILE *p1, *p2; if(argc < 3){ printf("Usage: unlz77 zipfile outputfile\n"); exit(1); } if( (p1=fopen(argv[1], "r")) == NULL){ printf("\nThe file %s not exist!\n", argv[1]); exit(0); } if( (p2=fopen(argv[2], "w")) == NULL){ printf("\nThe file %s can't be opened!\n", argv[2]); exit(0); } // initialize sliding window & look-ahead buffer for(i=1; i<=L+1; i++) x[i] = 0; do{ fread( &pointer, 1, 1, p1); // pointer, matchLength must be initialized!! fread( &matchLength, 1, 1, p1); fread( &lastSymbol, 1, 1, p1); if(feof(p1)) gameOver = 1; if(matchLength == 0){ x[L_WIN+1] = lastSymbol; if(!gameOver) fwrite( &lastSymbol, 1, 1, p2); } else { for(i=1; i<=matchLength; i++){ x[L_WIN+i] = x[pointer+i-1]; fwrite( &x[L_WIN+i],1,1, p2); x[L_WIN+matchLength+1] = lastSymbol; } if(!gameOver) fwrite( &lastSymbol, 1, 1,p2); } for(i=1; i<=L_WIN; i++) // shift x[i] = x[i+matchLength+1]; }while( !gameOver ); fclose(p1); fclose(p2); }