This commit is contained in:
Bohdan Kapliuk 2025-01-12 16:13:30 +02:00
parent 6c4d7d0fef
commit f191d7b7b4

View File

@ -88,7 +88,7 @@ unsigned char* decompress_1_buffer(const unsigned char* data, size_t size, size_
int decompress_1(const char* input_file_name, const char* output_file_name) { int decompress_1(const char* input_file_name, const char* output_file_name) {
size_t size; size_t size;
unsigned char* data; unsigned char* data;
if (read_file(input_file_name, &data, &size) != 0) return -1; if (read_file(output_file_name, &data, &size) != 0) return -1;
size_t out_size; size_t out_size;
unsigned char* decompressed = decompress_1_buffer(data, size, &out_size); unsigned char* decompressed = decompress_1_buffer(data, size, &out_size);
@ -96,7 +96,7 @@ int decompress_1(const char* input_file_name, const char* output_file_name) {
free(data); free(data);
return -1; return -1;
} }
int result = write_file(output_file_name, decompressed, out_size); int result = write_file(input_file_name, decompressed, out_size);
free(data); free(data);
free(decompressed); free(decompressed);
return result == 0 ? (int)out_size : -1; return result == 0 ? (int)out_size : -1;
@ -109,17 +109,21 @@ int compress_2(const char* input_file_name, const char* output_file_name) {
if (read_file(input_file_name, &data, &size) != 0) { if (read_file(input_file_name, &data, &size) != 0) {
return -1; return -1;
} }
FILE* output = fopen(output_file_name, "w"); FILE* output = fopen(output_file_name, "w");
if (!output) { if (!output) {
free(data); free(data);
return -1; return -1;
} }
unsigned char* dictionary[MAX_DICT_SIZE] = { NULL }; unsigned char* dictionary[MAX_DICT_SIZE] = { NULL };
size_t dict_size = 0; size_t dict_size = 0;
size_t current_index = 0; size_t current_index = 0;
while (current_index < size) { while (current_index < size) {
size_t match_index = 0; size_t match_index = 0;
size_t match_length = 0; size_t match_length = 0;
for (size_t i = 0; i < dict_size; i++) { for (size_t i = 0; i < dict_size; i++) {
size_t len = 0; size_t len = 0;
while (data[current_index + len] != '\0' && while (data[current_index + len] != '\0' &&
@ -132,9 +136,11 @@ int compress_2(const char* input_file_name, const char* output_file_name) {
match_index = i + 1; match_index = i + 1;
} }
} }
if (current_index + match_length < size) { if (current_index + match_length < size) {
fprintf(output, "%zu%c", match_index, data[current_index + match_length]); fprintf(output, "%zu%c", match_index, data[current_index + match_length]);
} }
if (dict_size < MAX_DICT_SIZE) { if (dict_size < MAX_DICT_SIZE) {
dictionary[dict_size] = malloc(match_length + 2); dictionary[dict_size] = malloc(match_length + 2);
if (!dictionary[dict_size]) { if (!dictionary[dict_size]) {
@ -152,6 +158,7 @@ int compress_2(const char* input_file_name, const char* output_file_name) {
dictionary[dict_size][match_length + 1] = '\0'; dictionary[dict_size][match_length + 1] = '\0';
dict_size++; dict_size++;
} }
current_index += match_length + 1; current_index += match_length + 1;
} }
@ -159,40 +166,47 @@ int compress_2(const char* input_file_name, const char* output_file_name) {
free(data); free(data);
for (size_t i = 0; i < dict_size; i++) { for (size_t i = 0; i < dict_size; i++) {
free(dictionary[i]); free(dictionary[i]);
} }
return 0; return 0;
} }
int decompress_2(const char* input_file_name, const char* output_file_name) { int decompress_2(const char* input_file_name, const char* output_file_name) {
unsigned char* data; unsigned char* data;
size_t size; size_t size;
if (read_file(input_file_name, &data, &size) != 0) { if (read_file(output_file_name, &data, &size) != 0) {
return -1; return -1;
} }
FILE* output = fopen(output_file_name, "w");
FILE* output = fopen(input_file_name, "w");
if (!output) { if (!output) {
free(data); free(data);
return -1; return -1;
} }
unsigned char* dictionary[MAX_DICT_SIZE] = { NULL }; unsigned char* dictionary[MAX_DICT_SIZE] = { NULL };
size_t dict_size = 0; size_t dict_size = 0;
size_t current_index = 0; size_t current_index = 0;
while (current_index < size) { while (current_index < size) {
unsigned short index = 0; unsigned short index = 0;
unsigned char ch = '\0'; unsigned char ch = '\0';
while (current_index < size && isdigit(data[current_index])) { while (current_index < size && isdigit(data[current_index])) {
index = index * 10 + (data[current_index] - '0'); index = index * 10 + (data[current_index] - '0');
current_index++; current_index++;
} }
if (current_index < size) { if (current_index < size) {
ch = data[current_index++]; ch = data[current_index++];
} }
if (index == 0) { if (index == 0) {
fputc(ch, output); fputc(ch, output);
} else { } else {
fwrite(dictionary[index - 1], 1, strlen((char*)dictionary[index - 1]), output); fwrite(dictionary[index - 1], 1, strlen((char*)dictionary[index - 1]), output);
fputc(ch, output); fputc(ch, output);
} }
if (dict_size < MAX_DICT_SIZE) { if (dict_size < MAX_DICT_SIZE) {
size_t length = (index > 0) ? strlen((char*)dictionary[index - 1]) : 0; size_t length = (index > 0) ? strlen((char*)dictionary[index - 1]) : 0;
dictionary[dict_size] = malloc(length + 2); dictionary[dict_size] = malloc(length + 2);