try
This commit is contained in:
		
							parent
							
								
									1cd0cc8fc0
								
							
						
					
					
						commit
						cfbcf91b0d
					
				
							
								
								
									
										11
									
								
								a2/Makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								a2/Makefile
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
			
		||||
# variables
 | 
			
		||||
CC=gcc
 | 
			
		||||
CFLAGS=-std=c11 -Wall -Werror
 | 
			
		||||
LDLIBS=-lm
 | 
			
		||||
OUTPUT=$@
 | 
			
		||||
 | 
			
		||||
# targets
 | 
			
		||||
%: %.c
 | 
			
		||||
	$(CC) $(CFLAGS) $@.c $(LDLIBS) -o $(OUTPUT)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										0
									
								
								a2/program.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								a2/program.c
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										16
									
								
								sk2a/Makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								sk2a/Makefile
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,16 @@
 | 
			
		||||
CC = gcc
 | 
			
		||||
CFLAGS = -g -Wall -Werror
 | 
			
		||||
 | 
			
		||||
all: program
 | 
			
		||||
 | 
			
		||||
program: compressor.o main.o
 | 
			
		||||
	$(CC) $(CFLAGS) compressor.o main.o -o program
 | 
			
		||||
 | 
			
		||||
compressor.o: compressor.c
 | 
			
		||||
	$(CC) $(CFLAGS) -c compressor.c -o compressor.o
 | 
			
		||||
 | 
			
		||||
main.o: main.c
 | 
			
		||||
	$(CC) $(CFLAGS) -c main.c -o main.o
 | 
			
		||||
 | 
			
		||||
clean:
 | 
			
		||||
	rm -rf program *.o *.exe *.exe.stackdump
 | 
			
		||||
							
								
								
									
										81
									
								
								sk2a/compressor.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								sk2a/compressor.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,81 @@
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include "compressor.h"
 | 
			
		||||
#define BUFSIZE 1024
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int ByteRLEEncode(char* bufferIn, int size, char* bufferOut) {
 | 
			
		||||
    int posIn = 0, count = 1, posOut = 0;
 | 
			
		||||
    char byteCurrent, byteRLE = bufferIn[posIn++];
 | 
			
		||||
 | 
			
		||||
    while (posIn != size) {
 | 
			
		||||
        byteCurrent = bufferIn[posIn++];
 | 
			
		||||
 | 
			
		||||
        if (byteCurrent == byteRLE) {
 | 
			
		||||
            count++;
 | 
			
		||||
 | 
			
		||||
            if (count == 256) {
 | 
			
		||||
                bufferOut[posOut++] = byteRLE;
 | 
			
		||||
                bufferOut[posOut++] = 255;
 | 
			
		||||
 | 
			
		||||
                count = 1;
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            bufferOut[posOut++] = byteRLE;
 | 
			
		||||
            bufferOut[posOut++] = count;
 | 
			
		||||
 | 
			
		||||
            byteRLE = byteCurrent;
 | 
			
		||||
            count = 1;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bufferOut[posOut++] = byteRLE;
 | 
			
		||||
    bufferOut[posOut++] = count;
 | 
			
		||||
 | 
			
		||||
    return posOut;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int mycompress(char* bufferIn, int size, char* bufferOut){
 | 
			
		||||
    return ByteRLEEncode(bufferIn, size, bufferOut);;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void compress(FILE *infile, FILE *outfile){
 | 
			
		||||
    char buffer[BUFSIZE];
 | 
			
		||||
    memset(buffer,0,BUFSIZE);
 | 
			
		||||
    while(1){
 | 
			
		||||
        int insize = fread(buffer,sizeof(char),BUFSIZE,infile);
 | 
			
		||||
        if (insize == 0){
 | 
			
		||||
            if (feof(infile)){
 | 
			
		||||
                // end of file
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
            assert(!ferror(infile));
 | 
			
		||||
        }
 | 
			
		||||
        char outbuf[BUFSIZE*2];
 | 
			
		||||
        // Doplnte implementaciu kompresie. 
 | 
			
		||||
        int outsize = mycompress(buffer,insize,outbuf);
 | 
			
		||||
        if (outsize   > 0){
 | 
			
		||||
            fwrite(outbuf,sizeof(char),outsize,outfile);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void decompress(FILE *infile, FILE *outfile){
 | 
			
		||||
    unsigned char RLEMetadata[2];
 | 
			
		||||
    char RLEData[255];
 | 
			
		||||
 | 
			
		||||
    while (fread(RLEMetadata, 2, 1, infile)) {
 | 
			
		||||
        printf("%c %d\n", RLEMetadata[0], RLEMetadata[1]);
 | 
			
		||||
 | 
			
		||||
        for (int i = 0; i < RLEMetadata[1]; i++) {
 | 
			
		||||
            RLEData[i] = RLEMetadata[0];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        fwrite(RLEData, RLEMetadata[1], 1, outfile);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										18
									
								
								sk2a/compressor.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								sk2a/compressor.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,18 @@
 | 
			
		||||
#ifndef _COMPRESSORH
 | 
			
		||||
#define _COMPRESSORH
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Skomprimuje súbor in a zapíše do súboru out.
 | 
			
		||||
 * @arg in smerník na otvorený vstupný súbor (na čítanie)
 | 
			
		||||
 * @arg out smerník na otvorený výstupný súbor (na zápis)
 | 
			
		||||
 */
 | 
			
		||||
void compress(FILE* in, FILE* out);
 | 
			
		||||
/**
 | 
			
		||||
 * Dekomprimuje súbor in a zapíše do súboru out.
 | 
			
		||||
 * @arg in smerník na otvorený vstupný súbor (na čítanie)
 | 
			
		||||
 * @arg out smerník na otvorený výstupný súbor (na zápis)
 | 
			
		||||
 */
 | 
			
		||||
void decompress(FILE* in, FILE* out);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										24
									
								
								sk2a/main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								sk2a/main.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
			
		||||
#include "compressor.h"
 | 
			
		||||
 | 
			
		||||
int main(int argc,char** argv){
 | 
			
		||||
    if (argc != 4 || (argv[1][1] != 'c' && argv[1][1] != 'd')){
 | 
			
		||||
        printf("Usage: \n");
 | 
			
		||||
        printf("  Compress ./compress -c infile.txt outfile.compress\n");
 | 
			
		||||
        printf("  decompress ./compress -d outfile.compress infile.txt\n");
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    char* action = argv[1];
 | 
			
		||||
    char* infile = argv[2];
 | 
			
		||||
    char* outfile = argv[3];
 | 
			
		||||
    FILE* inf = fopen(infile,"r");
 | 
			
		||||
    FILE* outf = fopen(outfile,"w");
 | 
			
		||||
    if (action[1] == 'c'){
 | 
			
		||||
        compress(inf,outf);
 | 
			
		||||
    }
 | 
			
		||||
    else if (action[1] == 'd'){
 | 
			
		||||
        decompress(inf,outf);
 | 
			
		||||
    }
 | 
			
		||||
    fclose(inf);
 | 
			
		||||
    fclose(outf);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user