Compare commits

..

No commits in common. "b3f3cd05265d66b07d359f5ddd600bd2d3278121" and "cac4809a6428a9689a3ba1ca772973bd65400ee2" have entirely different histories.

11 changed files with 77 additions and 110 deletions

View File

View File

View File

@ -1,18 +0,0 @@
CC = gcc
CFLAGS = -Wall -Wextra -std=c11
TARGET = compressor
all: $(TARGET)
$(TARGET): main.o compressor.o
$(CC) $(CFLAGS) -o $(TARGET) main.o compressor.o
main.o: main.c compressor.h
$(CC) $(CFLAGS) -c main.c
compressor.o: compressor.c compressor.h
$(CC) $(CFLAGS) -c compressor.c
clean:
rm -f *.o $(TARGET)

View File

@ -1,11 +0,0 @@
0-A
0-n
0-t
0-o
2-
0-K
4-z
0-a
0-r
0-

Binary file not shown.

View File

@ -1,64 +1,43 @@
#include "compressor.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
// Trie (prefix tree) structure for LZ78 compression
struct dict {
int index;
struct dict* characters[LZ78_DICT_SIZE];
};
#define BUFFER_SIZE 1024
#define LZ78_DICT_SIZE 256
// Free the dictionary trie
void delete_dict(struct dict* tree) {
if (!tree) {
return;
}
for (int i = 0; i < LZ78_DICT_SIZE; i++) {
delete_dict(tree->characters[i]);
}
free(tree);
// Function prototypes
int compress(const char* input_file_name, const char* output_file_name);
int decompress(const char* input_file_name, const char* output_file_name);
void lz78_compress(FILE* in, FILE* out);
void lz78_decompress(FILE* in, FILE* out);
void print_usage() {
printf("Usage:\n");
printf(" ./compressor -c infile outfile Compress infile into outfile\n");
printf(" ./compressor -d compressed uncompressed Decompress compressed into uncompressed\n");
}
// LZ78 Compression
void lz78_compress(FILE* in, FILE* out) {
struct dict* root = calloc(1, sizeof(struct dict));
struct dict* current = root;
int index = 1;
int c;
while ((c = fgetc(in)) != EOF) {
if (current->characters[c]) {
current = current->characters[c];
} else {
current->characters[c] = calloc(1, sizeof(struct dict));
current->characters[c]->index = index++;
fprintf(out, "%d-%c\n", current->index, c);
current = root;
}
int main(int argc, char* argv[]) {
if (argc != 4) {
print_usage();
return 1;
}
delete_dict(root);
}
const char* mode = argv[1];
const char* infile = argv[2];
const char* outfile = argv[3];
// LZ78 Decompression
void lz78_decompress(FILE* in, FILE* out) {
char buffer[LZ78_DICT_SIZE][BUFFER_SIZE] = {""};
int index, dict_index = 1;
char c;
while (fscanf(in, "%d-%c\n", &index, &c) == 2) {
if (index > 0) {
fprintf(out, "%s", buffer[index]);
}
fputc(c, out);
snprintf(buffer[dict_index], BUFFER_SIZE, "%s%c", buffer[index], c);
dict_index++;
if (strcmp(mode, "-c") == 0) {
return compress(infile, outfile);
} else if (strcmp(mode, "-d") == 0) {
return decompress(infile, outfile);
} else {
print_usage();
return 1;
}
}
// Compress function
int compress(const char* input_file_name, const char* output_file_name) {
FILE* input_file = fopen(input_file_name, "rb");
if (!input_file) {
@ -82,7 +61,6 @@ int compress(const char* input_file_name, const char* output_file_name) {
return 0;
}
// Decompress function
int decompress(const char* input_file_name, const char* output_file_name) {
FILE* input_file = fopen(input_file_name, "rb");
if (!input_file) {
@ -106,3 +84,53 @@ int decompress(const char* input_file_name, const char* output_file_name) {
return 0;
}
// LZ78 compression
struct dict {
int index;
struct dict* characters[LZ78_DICT_SIZE];
};
void delete_dict(struct dict* tree) {
if (!tree) {
return;
}
for (int i = 0; i < LZ78_DICT_SIZE; i++) {
delete_dict(tree->characters[i]);
}
free(tree);
}
void lz78_compress(FILE* in, FILE* out) {
struct dict* root = calloc(1, sizeof(struct dict));
struct dict* current = root;
int index = 1;
int c;
while ((c = fgetc(in)) != EOF) {
if (current->characters[c]) {
current = current->characters[c];
} else {
current->characters[c] = calloc(1, sizeof(struct dict));
current->characters[c]->index = index++;
fprintf(out, "%d-%c\n", current->index, c);
current = root;
}
}
delete_dict(root);
}
// LZ78 decompression
void lz78_decompress(FILE* in, FILE* out) {
char buffer[LZ78_DICT_SIZE][BUFFER_SIZE] = {""};
int index;
char c;
while (fscanf(in, "%d-%c\n", &index, &c) == 2) {
if (index > 0) {
fprintf(out, "%s", buffer[index]);
}
fputc(c, out);
snprintf(buffer[index], BUFFER_SIZE, "%s%c", buffer[index], c);
}
}

Binary file not shown.

View File

@ -1 +0,0 @@
Anton Kozar

View File

@ -1,30 +0,0 @@
#include "compressor.h"
#include <stdio.h>
#include <string.h>
void print_usage() {
printf("Usage:\n");
printf(" ./compressor -c infile outfile Compress infile into outfile\n");
printf(" ./compressor -d compressed uncompressed Decompress compressed into uncompressed\n");
}
int main(int argc, char* argv[]) {
if (argc != 4) {
print_usage();
return 1;
}
const char* mode = argv[1];
const char* infile = argv[2];
const char* outfile = argv[3];
if (strcmp(mode, "-c") == 0) {
return compress(infile, outfile);
} else if (strcmp(mode, "-d") == 0) {
return decompress(infile, outfile);
} else {
print_usage();
return 1;
}
}

Binary file not shown.

View File

@ -1 +0,0 @@
Anton Kozar