main.c
// This method contains the logic of the inverse algorithm, used to decompress.
// Is invoked when "-d" option is specified in launch command.
This method contains the logic of the inverse algorithm, used to decompress.
Is invoked when "-d" option is specified in launch command.
This method contains the logic of the inverse algorithm used to decompress . Is invoked when " - d " option is specified in launch command .
int decompress() {
    FILE *fileInput;
    FILE *fileOutput;
    int shift, offset, match, c_in;
    bool done = false;
    unsigned char c;
    unsigned char window[windowSize];
    unsigned char writeBuffer[windowSize];
    unsigned char readBuffer[2];
    fileInput = fopen("output/output.lz77", "rb");
    fileOutput = fopen("output/file", "wb");
    if (!fileInput) {
        fprintf(stderr, "Unable to open fileInput %s", "output.lz77");
        return 0;
    }
    fread(window, 1, windowSize, fileInput);
    fwrite(window, 1, windowSize, fileOutput);
    while (true) {
        size_t bytesRead = fread(readBuffer, 1, 2, fileInput);
        if (bytesRead >= 2) {
            offset = (int) readBuffer[0];
            match = (int) readBuffer[1];
            if (offset == 255) {
                offset = 0;
                c = (unsigned char) match;
                match = 0;
                shift = match + 1;
            }
            else {
                shift = match + 1;
                c_in = fgetc(fileInput);
                if (c_in == EOF)
                    done = true;
                else
                    c = (unsigned char) c_in;
            }
            for (int i = 0, j = windowSize - offset; i < match; i++, j++)
                writeBuffer[i] = window[j];
            fwrite(writeBuffer, 1, (size_t) match, fileOutput);
            if (!done)
                fputc(c, fileOutput);
            for (int i = 0; i < windowSize - shift; i++)
                window[i] = window[i + shift];
            for (int i = 0, j = windowSize - shift; i < match; i++, j++)
                window[j] = writeBuffer[i];
            window[windowSize - 1] = c;
        }
        else {
            break;
        }
    }
    fclose(fileInput);
    fclose(fileOutput);
    return 1;
}
int decompress ( ) { FILE * fileInput ; FILE * fileOutput ; int shift , offset , match , c_in ; bool done = false ; unsigned char c ; unsigned char window [ windowSize ] ; unsigned char writeBuffer [ windowSize ] ; unsigned char readBuffer [ 2 ] ; fileInput = fopen ( " " , " " ) ; fileOutput = fopen ( " " , " " ) ; if ( ! fileInput ) { fprintf ( stderr , " " , " " ) ; return 0 ; } fread ( window , 1 , windowSize , fileInput ) ; fwrite ( window , 1 , windowSize , fileOutput ) ; while ( true ) { size_t bytesRead = fread ( readBuffer , 1 , 2 , fileInput ) ; if ( bytesRead >= 2 ) { offset = ( int ) readBuffer [ 0 ] ; match = ( int ) readBuffer [ 1 ] ; if ( offset == 255 ) { offset = 0 ; c = ( unsigned char ) match ; match = 0 ; shift = match + 1 ; } else { shift = match + 1 ; c_in = fgetc ( fileInput ) ; if ( c_in == EOF ) done = true ; else c = ( unsigned char ) c_in ; } for ( int i = 0 , j = windowSize - offset ; i < match ; i ++ , j ++ ) writeBuffer [ i ] = window [ j ] ; fwrite ( writeBuffer , 1 , ( size_t ) match , fileOutput ) ; if ( ! done ) fputc ( c , fileOutput ) ; for ( int i = 0 ; i < windowSize - shift ; i ++ ) window [ i ] = window [ i + shift ] ; for ( int i = 0 , j = windowSize - shift ; i < match ; i ++ , j ++ ) window [ j ] = writeBuffer [ i ] ; window [ windowSize - 1 ] = c ; } else { break ; } } fclose ( fileInput ) ; fclose ( fileOutput ) ; return 1 ; }
This method contains the logic of the inverse algorithm, used to decompress.
This method contains the logic of the inverse algorithm used to decompress .
// Open I/O files // Load array with initial bytes and write to file // Inverse algorithm beginning // Read file by couples/triads to reconstruct original file // If first byte of readBuffer is 255 -> offset = 0, match = 0 // Load and write occurrence to file // Shift window // Close I/O files