//Naprogramuj nástroj na kompresiu a dekompresiu. Na kompresiu použite aspoň dva kompresné algoritmy, napr. : Huffmanovo kódovanie, LZ77, LZ78, Run Length kódovanie alebo iný.
insize = fread(buffer,sizeof(char),insize - 1,infile); //nacita obsah do bufferu
if (insize == 0){
assert(!ferror(infile));
}
char *tempbuf = NULL; // az vo funkcii to alokujem
int tempsize = myrlcompress(buffer,insize,&tempbuf); //vstupny buffer, velkost a adresu bufferu
char *outbuf = NULL;
int outsize = mylz78compress(tempbuf,tempsize,&outbuf);
if (outsize > 0){
fwrite(outbuf,sizeof(char),outsize,outfile); //napise sa do suboru, z akeho buffera do akeho suboru
}
}
int myrldecompress(char* buff,int size,char** outbuf){
*outbuf = calloc(size + 1,sizeof(char));
memset(*outbuf,0,size + 1);
int pos = 0;
for(int i = 0; i <size;i+=2){
char c = buff[i];
char length = buff[i + 1];
// ak nam nebude stacit miesto v *outbuf, treba allocovat viac
while((length + pos) > (size - 1)){
size *= 2;
char *tempbuf = calloc(size,sizeof(char));
memset(tempbuf,'\0',size);
strcpy(tempbuf,*outbuf);
*outbuf = tempbuf;
}
for (int j = 0; j <length;j++){//tolkokrathopridavam,kolkomamcisielko
(*outbuf)[pos] = c;
pos += 1;
}
}
return pos;
}
int mylz78decompress(char* buff,int size,char** outbuf){ //nepotrebujem pouzivat strom
*outbuf = calloc(size,sizeof(char)); //taka ista velkost ako vstup
memset(*outbuf,0,size);
char *words[MAX_WORDS]; //dvojrozmerne pole
words[0] = "";
char c;
int index = 1;
int number;
int pos = 0;
int bytes_read, bytes_read_all = 0; //citame vstupny buffer, necitame po bajtoch, viac ciferne cisla
while (sscanf(buff + bytes_read_all,"-%d-%c\n%n",&number,&c,&bytes_read) == 2){ //formatovacie citanie, pusaveme o bajty, ktore sa precitali ak vrati ine cislo ako 2 tak je nakocni
bytes_read_all += bytes_read; //o tolko sme sa posunuli
words[index] = calloc(strlen(words[number]) + 2,sizeof(char*)); //vo words sa vytvoria prefixi
strcpy(words[index],words[number]); //nakopirujeme words number a pridame nakoniec c
words[index][strlen(words[number])] = c;
// ak nam nebude stacit miesto v *outbuf, treba allocovat viac
while((strlen(words[index]) + pos) > (size - 1)){
size *= 2;
char *tempbuf = calloc(size,sizeof(char));
memset(tempbuf,'\0',size);
strcpy(tempbuf,*outbuf);
*outbuf = tempbuf;
}
strcpy(*outbuf + pos,words[index]); //nakopiruje najnovsie slovo