main.c
// This method contains the logic of the compression algorithm.
// Is invoked when "-c" option is specified in launch command, followed by file name.
This method contains the logic of the compression algorithm.
Is invoked when "-c" option is specified in launch command, followed by file name.
This method contains the logic of the compression algorithm . Is invoked when " - c " option is specified in launch command followed by file name .
int compress(char* inputPath) {
    FILE *fileInput;
    FILE *fileOutput;
    bool last = false;
    int inputLength = 0;
    int outputLength = 0;
    int endOffset = 0;
    int pos = -1;
    int i, size, shift, c_in;
    size_t bytesRead = (size_t) -1;
    unsigned char c;
    unsigned char array[arraySize];
    unsigned char window[windowSize];
    unsigned char buffer[bufferSize];
    unsigned char loadBuffer[bufferSize];
    unsigned char str[bufferSize];
    char path[30] = "input/";
    strcat(path, inputPath);
    fileInput = fopen(path, "rb");
    fileOutput = fopen("output/output.lz77", "wb");
    if (!fileInput) {
        fprintf(stderr, "Unable to open fileInput %s", inputPath);
        return 0;
    }
    fseek(fileInput, 0, SEEK_END);
    inputLength = ftell(fileInput);
    fseek(fileInput, 0, SEEK_SET);
    fprintf(stdout, "Input file size: %d bytes", inputLength);
    if (inputLength == 0)
        return 3;
    if (inputLength < arraySize)
        return 2;
    fread(array, 1, arraySize, fileInput);
    fwrite(array, 1, windowSize, fileOutput);
    while (true) {
        if ((c_in = fgetc(fileInput)) == EOF)
            last = true;
        else
            c = (unsigned char) c_in;
        for (int k = 0; k < windowSize; k++)
            window[k] = array[k];
        for (int k = windowSize, j = 0; k < arraySize; k++, j++) {
            buffer[j] = array[k];
            str[j] = array[k];
        }
        if (endOffset != 0) {
            size = bufferSize - endOffset;
            if (endOffset == bufferSize)
                break;
        }
        else {
            size = bufferSize;
        }
        pos = -1;
        for (i = size; i > 0; i--) {
            pos = findMatch(window, str, i);
            if (pos != -1)
                break;
        }
        if (pos == -1) {
            fputc(255, fileOutput);
            fputc(buffer[0], fileOutput);
            shift = 1;
        }
        else {
            fputc(windowSize - pos, fileOutput);
            fputc(i, fileOutput);
            if (i == bufferSize) {
                shift = bufferSize + 1;
                if (!last)
                    fputc(c, fileOutput);
                else
                    endOffset = 1;
            }
            else {
                if (i + endOffset != bufferSize)
                    fputc(buffer[i], fileOutput);
                else
                    break;
                shift = i + 1;
            }
        }
        for (int j = 0; j < arraySize - shift; j++)
            array[j] = array[j + shift];
        if (!last)
            array[arraySize - shift] = c;
        if (shift == 1 && last)
            endOffset++;
        if (shift != 1) {
            bytesRead = fread(loadBuffer, 1, (size_t) shift - 1, fileInput);
            for (int k = 0, l = arraySize - shift + 1; k < shift - 1; k++, l++)
                array[l] = loadBuffer[k];
            if (last) {
                endOffset += shift;
                continue;
            }
            if (bytesRead < shift - 1)
                endOffset = shift - 1 - bytesRead;
        }
    }
    fseek(fileOutput, 0, SEEK_END);
    outputLength = ftell(fileOutput);
    fseek(fileOutput, 0, SEEK_SET);
    fprintf(stdout, "\nOutput file size: %d bytes\n", outputLength);
    fclose(fileInput);
    fclose(fileOutput);
    return 1;
}
int compress ( char * inputPath ) { FILE * fileInput ; FILE * fileOutput ; bool last = false ; int inputLength = 0 ; int outputLength = 0 ; int endOffset = 0 ; int pos = -1 ; int i , size , shift , c_in ; size_t bytesRead = ( size_t ) -1 ; unsigned char c ; unsigned char array [ arraySize ] ; unsigned char window [ windowSize ] ; unsigned char buffer [ bufferSize ] ; unsigned char loadBuffer [ bufferSize ] ; unsigned char str [ bufferSize ] ; char path [ 30 ] = " " ; strcat ( path , inputPath ) ; fileInput = fopen ( path , " " ) ; fileOutput = fopen ( " " , " " ) ; if ( ! fileInput ) { fprintf ( stderr , " " , inputPath ) ; return 0 ; } fseek ( fileInput , 0 , SEEK_END ) ; inputLength = ftell ( fileInput ) ; fseek ( fileInput , 0 , SEEK_SET ) ; fprintf ( stdout , " " , inputLength ) ; if ( inputLength == 0 ) return 3 ; if ( inputLength < arraySize ) return 2 ; fread ( array , 1 , arraySize , fileInput ) ; fwrite ( array , 1 , windowSize , fileOutput ) ; while ( true ) { if ( ( c_in = fgetc ( fileInput ) ) == EOF ) last = true ; else c = ( unsigned char ) c_in ; for ( int k = 0 ; k < windowSize ; k ++ ) window [ k ] = array [ k ] ; for ( int k = windowSize , j = 0 ; k < arraySize ; k ++ , j ++ ) { buffer [ j ] = array [ k ] ; str [ j ] = array [ k ] ; } if ( endOffset != 0 ) { size = bufferSize - endOffset ; if ( endOffset == bufferSize ) break ; } else { size = bufferSize ; } pos = -1 ; for ( i = size ; i > 0 ; i -- ) { pos = findMatch ( window , str , i ) ; if ( pos != -1 ) break ; } if ( pos == -1 ) { fputc ( 255 , fileOutput ) ; fputc ( buffer [ 0 ] , fileOutput ) ; shift = 1 ; } else { fputc ( windowSize - pos , fileOutput ) ; fputc ( i , fileOutput ) ; if ( i == bufferSize ) { shift = bufferSize + 1 ; if ( ! last ) fputc ( c , fileOutput ) ; else endOffset = 1 ; } else { if ( i + endOffset != bufferSize ) fputc ( buffer [ i ] , fileOutput ) ; else break ; shift = i + 1 ; } } for ( int j = 0 ; j < arraySize - shift ; j ++ ) array [ j ] = array [ j + shift ] ; if ( ! last ) array [ arraySize - shift ] = c ; if ( shift == 1 && last ) endOffset ++ ; if ( shift != 1 ) { bytesRead = fread ( loadBuffer , 1 , ( size_t ) shift - 1 , fileInput ) ; for ( int k = 0 , l = arraySize - shift + 1 ; k < shift - 1 ; k ++ , l ++ ) array [ l ] = loadBuffer [ k ] ; if ( last ) { endOffset += shift ; continue ; } if ( bytesRead < shift - 1 ) endOffset = shift - 1 - bytesRead ; } } fseek ( fileOutput , 0 , SEEK_END ) ; outputLength = ftell ( fileOutput ) ; fseek ( fileOutput , 0 , SEEK_SET ) ; fprintf ( stdout , " \n \n " , outputLength ) ; fclose ( fileInput ) ; fclose ( fileOutput ) ; return 1 ; }
This method contains the logic of the compression algorithm.
This method contains the logic of the compression algorithm .
// Open I/O files // If unable to open file, return alert // Get fileInput length // If file is empty, return alert // If file length is smaller than arraySize, not worth processing // Load array with initial bytes // Write the first bytes to output file // LZ77 logic beginning // Load window (dictionary) // Load buffer (lookahead) // Search for longest match in window // No match found // Write only one byte instead of two // 255 -> offset = 0, match = 0 // Found match // offset = windowSize - position of match // i = number of match bytes // endOffset = number of bytes in lookahead buffer not to be considered (EOF) // Shift buffers // If (shift != 1) -> read more bytes from file // Load loadBuffer with new bytes // Load array with new bytes // Shift bytes in array, then splitted into window[] and buffer[] during next iteration // Get fileOutput length // Close I/O files