skuska
This commit is contained in:
parent
38f65ca4e9
commit
790aa2149c
110
sk1/compressor.c
110
sk1/compressor.c
@ -266,21 +266,107 @@ int compress_2(const char* input_file_name, const char* output_file_name) {
|
||||
unsigned char* data = read_file(input_file_name, &size);
|
||||
if (!data) return -1;
|
||||
|
||||
printf("Compressing using Algorithm 2 (LZ77)...\n");
|
||||
|
||||
int result = write_file(output_file_name, data, size);
|
||||
FILE* output_file = fopen(output_file_name, "wb");
|
||||
if (!output_file) {
|
||||
perror("Error opening output file");
|
||||
free(data);
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t search_buffer_size = 1024; // Veľkosť search bufferu
|
||||
size_t lookahead_buffer_size = 16; // Veľkosť lookahead bufferu
|
||||
|
||||
size_t pos = 0;
|
||||
while (pos < size) {
|
||||
size_t best_match_offset = 0;
|
||||
size_t best_match_length = 0;
|
||||
|
||||
// Vyhľadaj najlepšiu zhodu v search bufferi
|
||||
for (size_t i = (pos > search_buffer_size ? pos - search_buffer_size : 0); i < pos; i++) {
|
||||
size_t match_length = 0;
|
||||
while (match_length < lookahead_buffer_size &&
|
||||
pos + match_length < size &&
|
||||
data[i + match_length] == data[pos + match_length]) {
|
||||
match_length++;
|
||||
}
|
||||
if (match_length > best_match_length) {
|
||||
best_match_length = match_length;
|
||||
best_match_offset = pos - i;
|
||||
}
|
||||
}
|
||||
|
||||
// Ulož trojicu (offset, length, next_symbol)
|
||||
unsigned char offset_high = (best_match_offset >> 8) & 0xFF;
|
||||
unsigned char offset_low = best_match_offset & 0xFF;
|
||||
unsigned char length = best_match_length;
|
||||
unsigned char next_symbol = (pos + best_match_length < size) ? data[pos + best_match_length] : 0;
|
||||
|
||||
fputc(offset_high, output_file);
|
||||
fputc(offset_low, output_file);
|
||||
fputc(length, output_file);
|
||||
fputc(next_symbol, output_file);
|
||||
|
||||
// Posuň pozíciu v dátach
|
||||
pos += best_match_length + 1;
|
||||
}
|
||||
|
||||
fclose(output_file);
|
||||
free(data);
|
||||
return result;
|
||||
|
||||
printf("Compression using LZ77 completed successfully.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int decompress_2(const char* input_file_name, const char* output_file_name) {
|
||||
size_t size;
|
||||
unsigned char* data = read_file(input_file_name, &size);
|
||||
if (!data) return -1;
|
||||
FILE* input_file = fopen(input_file_name, "rb");
|
||||
if (!input_file) {
|
||||
perror("Error opening input file");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Decompressing using Algorithm 2 (LZ77)...\n");
|
||||
FILE* output_file = fopen(output_file_name, "wb");
|
||||
if (!output_file) {
|
||||
perror("Error opening output file");
|
||||
fclose(input_file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int result = write_file(output_file_name, data, size);
|
||||
free(data);
|
||||
return result;
|
||||
}
|
||||
unsigned char* output_buffer = (unsigned char*)malloc(10 * 1024 * 1024); // Pre alokáciu 10 MB
|
||||
if (!output_buffer) {
|
||||
perror("Memory allocation error");
|
||||
fclose(input_file);
|
||||
fclose(output_file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t pos = 0;
|
||||
while (1) {
|
||||
int offset_high = fgetc(input_file);
|
||||
int offset_low = fgetc(input_file);
|
||||
int length = fgetc(input_file);
|
||||
int next_symbol = fgetc(input_file);
|
||||
|
||||
if (offset_high == EOF || offset_low == EOF || length == EOF || next_symbol == EOF) break;
|
||||
|
||||
size_t offset = (offset_high << 8) | offset_low;
|
||||
|
||||
// Skopíruj zhodu z histórie
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
output_buffer[pos] = output_buffer[pos - offset];
|
||||
pos++;
|
||||
}
|
||||
|
||||
// Pridaj ďalší symbol
|
||||
output_buffer[pos++] = next_symbol;
|
||||
}
|
||||
|
||||
fwrite(output_buffer, 1, pos, output_file);
|
||||
|
||||
free(output_buffer);
|
||||
fclose(input_file);
|
||||
fclose(output_file);
|
||||
|
||||
printf("Decompression using LZ77 completed successfully.\n");
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user