diff --git a/a2/Makefile b/a2/Makefile new file mode 100644 index 0000000..f78f641 --- /dev/null +++ b/a2/Makefile @@ -0,0 +1,11 @@ +# variables +CC=gcc +CFLAGS=-std=c11 -Wall -Werror +LDLIBS=-lm +OUTPUT=$@ + +# targets +%: %.c + $(CC) $(CFLAGS) $@.c $(LDLIBS) -o $(OUTPUT) + + diff --git a/a2/program.c b/a2/program.c new file mode 100644 index 0000000..e69de29 diff --git a/sk2a/Makefile b/sk2a/Makefile new file mode 100644 index 0000000..dd4b40f --- /dev/null +++ b/sk2a/Makefile @@ -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 \ No newline at end of file diff --git a/sk2a/compressor.c b/sk2a/compressor.c new file mode 100644 index 0000000..fc8d1c5 --- /dev/null +++ b/sk2a/compressor.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#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); + } +} + diff --git a/sk2a/compressor.h b/sk2a/compressor.h new file mode 100644 index 0000000..489a5f1 --- /dev/null +++ b/sk2a/compressor.h @@ -0,0 +1,18 @@ +#ifndef _COMPRESSORH +#define _COMPRESSORH +#include + +/** + * 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 diff --git a/sk2a/main.c b/sk2a/main.c new file mode 100644 index 0000000..c4d3bc0 --- /dev/null +++ b/sk2a/main.c @@ -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); +} +