Обновить sk1/compressor.c

This commit is contained in:
Yevhen Kozirovskyi 2025-01-19 17:43:47 +00:00
parent 4f7f2b98c9
commit 7830f11bdf

View File

@ -123,6 +123,8 @@ struct MinHeapNode* buildHuffmanTree(unsigned char data[], int freq[], int size)
// Print Huffman Codes to a map // Print Huffman Codes to a map
void storeCodes(struct MinHeapNode* root, char** codes, char* currentCode, int top) { void storeCodes(struct MinHeapNode* root, char** codes, char* currentCode, int top) {
if (!root) return;
if (root->left) { if (root->left) {
currentCode[top] = '0'; currentCode[top] = '0';
storeCodes(root->left, codes, currentCode, top + 1); storeCodes(root->left, codes, currentCode, top + 1);
@ -133,11 +135,11 @@ void storeCodes(struct MinHeapNode* root, char** codes, char* currentCode, int t
} }
if (!(root->left) && !(root->right)) { if (!(root->left) && !(root->right)) {
currentCode[top] = '\0'; currentCode[top] = '\0';
codes[root->data] = strdup(currentCode); codes[root->data] = strdup(currentCode); // Ensure memory is allocated
} }
} }
// Compress the input file using Huffman Coding // Updated compressFile function
int compressFile(const char* input_file_name, const char* output_file_name) { int compressFile(const char* input_file_name, const char* output_file_name) {
FILE* inputFile = fopen(input_file_name, "rb"); FILE* inputFile = fopen(input_file_name, "rb");
if (!inputFile) { if (!inputFile) {
@ -166,7 +168,7 @@ int compressFile(const char* input_file_name, const char* output_file_name) {
struct MinHeapNode* root = buildHuffmanTree(data, frequencies, size); struct MinHeapNode* root = buildHuffmanTree(data, frequencies, size);
char* codes[256] = {0}; char* codes[256] = {0};
char currentCode[MAX_TREE_HT]; char currentCode[MAX_TREE_HT] = {0}; // Initialize to avoid garbage values
storeCodes(root, codes, currentCode, 0); storeCodes(root, codes, currentCode, 0);
FILE* outputFile = fopen(output_file_name, "wb"); FILE* outputFile = fopen(output_file_name, "wb");
@ -183,6 +185,12 @@ int compressFile(const char* input_file_name, const char* output_file_name) {
int bitCount = 0; int bitCount = 0;
while (fread(&buffer, sizeof(unsigned char), 1, inputFile)) { while (fread(&buffer, sizeof(unsigned char), 1, inputFile)) {
char* code = codes[buffer]; char* code = codes[buffer];
if (!code) {
fprintf(stderr, "Error: Undefined code for byte %u\n", buffer);
fclose(inputFile);
fclose(outputFile);
return -1;
}
for (int i = 0; code[i] != '\0'; i++) { for (int i = 0; code[i] != '\0'; i++) {
byte = (byte << 1) | (code[i] - '0'); byte = (byte << 1) | (code[i] - '0');
bitCount++; bitCount++;
@ -200,9 +208,15 @@ int compressFile(const char* input_file_name, const char* output_file_name) {
fclose(inputFile); fclose(inputFile);
fclose(outputFile); fclose(outputFile);
// Free memory for codes
for (int i = 0; i < 256; i++) {
if (codes[i]) free(codes[i]);
}
return 0; return 0;
} }
// Decompress the file // Decompress the file
int decompressFile(const char* input_file_name, const char* output_file_name) { int decompressFile(const char* input_file_name, const char* output_file_name) {
FILE* inputFile = fopen(input_file_name, "rb"); FILE* inputFile = fopen(input_file_name, "rb");