From 317af25699d28ecc87e160b2fc0be68bb192df53 Mon Sep 17 00:00:00 2001 From: vj586da Date: Sun, 23 Jan 2022 23:01:39 +0100 Subject: [PATCH] add all --- sk2a/Makefile | 8 +++ sk2a/compressor.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++ sk2a/compressor.h | 18 ++++++ sk2a/main.c | 24 ++++++++ 4 files changed, 186 insertions(+) create mode 100644 sk2a/Makefile create mode 100644 sk2a/compressor.c create mode 100644 sk2a/compressor.h create mode 100644 sk2a/main.c diff --git a/sk2a/Makefile b/sk2a/Makefile new file mode 100644 index 0000000..f451ff9 --- /dev/null +++ b/sk2a/Makefile @@ -0,0 +1,8 @@ +all: program + +program: compressor.c main.c + gcc -Wall -g compressor.c main.c -o program + +clean: + rm program + diff --git a/sk2a/compressor.c b/sk2a/compressor.c new file mode 100644 index 0000000..d38206e --- /dev/null +++ b/sk2a/compressor.c @@ -0,0 +1,136 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include "compressor.h" +#define BUFSIZE 2000000 +#define BSIZE 9 + +int mycompress(char* buff,int size,char* outbuf){ + //memcpy(outbuf,buff,size); + int k = 0; + int head = 0; + while(head < size && buff[head] > 0 ){ + + int bufferSize = BSIZE; + if (head < bufferSize){ + bufferSize = head; + } + + char* searchBuffer = buff + head - bufferSize; + + char* window = buff + head; + + int lookAheadSize = BSIZE; + if((size - head) < lookAheadSize){ + lookAheadSize = BSIZE - head; + } + //printf("Buffer: %s\n",buff); + //printf("window: %s\n", window); + //printf("head: %d\n", head); + int wsz = BSIZE; + int msz = wsz; + if (bufferSize < wsz){ + msz = bufferSize; + } + int offset = -1; + + int size = -1; + + for(int i = 1; i 0){ + offset = bufferSize - ( r - searchBuffer ); + size = i+1; + }else { + break; + } + } + + + if(size > 0){ + //printf("%d",offset); + //printf("%d",size-1); + //printf("%c\n",buff[head+size-1]); + sprintf(&outbuf[k],"%d%d%c",offset,size-1,buff[head+size-1]); + k+=3; + head+=size; + + }else { + //printf("00%c\n",buff[head]); + sprintf(&outbuf[k],"00%c",buff[head]); + k+=3; + head+=1; + } + + } + + + return k; +} + + +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){ + char code[3]; + char buffer[BUFSIZE]; + int size = 0; + int k = 0; + fseek(infile, 0, SEEK_END); + long fileSize = ftell(infile); + fseek(infile, 0, SEEK_SET); + printf("size: %ld\n", fileSize); + char letter; +while(!feof(infile)){ + size = 0; + memset(buffer,0,BUFSIZE); + for(int j = 0; j < 4096; j++){ + fread(code, sizeof(char), 3, infile); + int offset = atoi(code); + int length = atoi(&code[1]); + letter = code[2]; + offset=(offset-length)/10; + if(offset == 0){ + buffer[size] = letter; + size++; + }else { + for(int i = 0; i < length; i++){ + buffer[size] = buffer[size-offset]; + size++; + } + buffer[size] = letter; + size++; + } + printf("%d\n",k); + k+=3; +} + +fwrite(buffer, sizeof(char), size-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); +} +