-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdehuff.c
More file actions
135 lines (114 loc) · 3.53 KB
/
dehuff.c
File metadata and controls
135 lines (114 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "bitreader.h"
#include "bitwriter.h"
#include "node.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//format
//shitformat3
#define OPTIONS "i:o:h"
void dehuff_decompress_file(FILE *fout, BitReader *inbuf) {
// Read header information
uint8_t type1 = bit_read_uint8(inbuf);
uint8_t type2 = bit_read_uint8(inbuf);
uint32_t filesize = bit_read_uint32(inbuf);
uint16_t num_leaves = bit_read_uint16(inbuf);
// Verify header information
assert(type1 == 'H');
assert(type2 == 'C');
// Calculate the number of nodes in the code tree
uint32_t num_nodes = 2 * num_leaves - 1;
// Initialize stack and top-of-stack pointer
Node *stack[64]; // Assuming 64 is the maximum number of nodes
int top = -1;
// Build the code tree
for (uint32_t i = 0; i < num_nodes; i++) {
uint8_t bit = bit_read_bit(inbuf);
Node *node;
if (bit == 1) {
uint8_t symbol = bit_read_uint8(inbuf);
node = node_create(symbol, 0);
} else {
node = node_create(0, 0);
node->right = stack[top--];
node->left = stack[top--];
}
stack[++top] = node; // Push the node onto the stack
}
Node *code_tree = stack[top--]; // Pop the code tree from the stack
// Decompress the file
for (uint32_t i = 0; i < filesize; i++) {
Node *node = code_tree;
// Traverse the code tree until reaching a leaf node
while (1) {
uint8_t bit = bit_read_bit(inbuf);
if (bit == 0) {
node = node->left;
} else {
node = node->right;
}
if (node->left == NULL && node->right == NULL) {
// Leaf node reached, write the symbol to the output file
fputc(node->symbol, fout);
break;
}
}
}
}
int main(int argc, char **argv) {
int opt = 0;
char *infile = NULL;
char *outfile = NULL;
// Parse command line options
while ((opt = getopt(argc, argv, OPTIONS)) != -1) {
switch (opt) {
case 'i': infile = optarg; break;
case 'o': outfile = optarg; break;
case 'h':
// Display help message and exit
printf("dehuff: -i option is required\nUsage: dehuff -i infile -o outfile\n "
"dehuff -v -i infile -o outfile\n dehuff -h\n");
return 0;
}
}
// Check if input and output files are provided
if (infile == NULL || outfile == NULL) {
printf("dehuff: -i option is required.\n");
printf("Usage: dehuff -i infile -o outfile\n");
printf(" dehuff -v -i infile -o outfile\n");
printf(" dehuff -h\n");
return 1;
}
// Open input file
FILE *fin = fopen(infile, "rb");
if (!fin) {
printf("Failed to open input file: %s\n", infile);
return 1;
}
// Open output file
FILE *fout = fopen(outfile, "wb");
if (!fout) {
fclose(fin);
printf("Failed to open output file: %s\n", outfile);
return 1;
}
// Open input buffer
BitReader *inbuf = bit_read_open(infile);
if (!inbuf) {
fclose(fin);
fclose(fout);
printf("Failed to open input buffer.\n");
return 1;
}
// Decompress the file
dehuff_decompress_file(fout, inbuf);
// Clean up
bit_read_close(&inbuf);
//free(&node);
fclose(fin);
fclose(fout);
return 0;
}