-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoding.c
More file actions
282 lines (250 loc) · 7.39 KB
/
decoding.c
File metadata and controls
282 lines (250 loc) · 7.39 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//Huffman coding
//Adam Rachuba
//Decoding program
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define MAX_CODE_BYTE 4
int symbols_number=0;
int bytes_number=0;
struct code_table
{
int symbol;
unsigned char code[MAX_CODE_BYTE];
unsigned char length;
};
struct node
{
int value;
int symbol;
struct node *right;
struct node *left;
};
char *new_extension(const char file[], const char new_extension[])
{
int n = strlen(file);
int i = n - 1;
while (i >= 0 && file[i] != '.') i--;
char *new_file = malloc((n + strlen(new_extension) + 2) * sizeof(char));
if (new_file == NULL)
{
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
if (i > 0)
{
strncpy(new_file, file, i + 1);
new_file[i + 1] = '\0';
}
else if(i==0) strcpy(new_file, file);
strcat(new_file, new_extension);
return new_file;
}
struct code_table* load_code_table(char name_file[])
{
FILE* file = fopen(name_file, "rb");
if (file == NULL)
{
fprintf(stderr, "Unable to open file %s\n", name_file);
exit(EXIT_FAILURE);
}
char ch;
while ((ch = fgetc(file)) != EOF)
{
if (ch == '\n') symbols_number++;
}
rewind(file);
struct code_table* code_tab = malloc(symbols_number * sizeof(struct code_table));
if (code_tab == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
int i;
for (i = 0; i < symbols_number; i++)
{
if (fscanf(file, "%d", &code_tab[i].symbol) != 1)
{
fprintf(stderr, "Invalid file format\n");
exit(EXIT_FAILURE);
}
int j=0;
for (j = 0; j < MAX_CODE_BYTE; j++)
{
if (fscanf(file, "%hhu", &code_tab[i].code[j]) != 1)
{
fprintf(stderr, "Invalid file format\n");
exit(EXIT_FAILURE);
}
}
if (fscanf(file, "%hhu", &code_tab[i].length) != 1)
{
fprintf(stderr, "Invalid file format\n");
exit(EXIT_FAILURE);
}
}
fclose(file);
return code_tab;
}
int load_bytes_number(const char* name_file)
{
FILE* file = fopen(name_file, "r");
if (file == NULL)
{
fprintf(stderr, "Unable to open file %s\n", name_file);
exit(EXIT_FAILURE);
}
int bytes;
if (fscanf(file, "%d", &bytes) != 1)
{
fprintf(stderr, "Invalid file format\n");
exit(EXIT_FAILURE);
}
fclose(file);
return bytes;
}
struct node* recreate_binary_tree(struct code_table code_tab[])
{
if (symbols_number == 1)
{
struct node* single_node = malloc(sizeof(struct node));
if (single_node == NULL)
{
fprintf(stderr, "Memory allocation failed for single node.\n");
exit(EXIT_FAILURE);
}
single_node->symbol = code_tab[0].symbol;
single_node->left = NULL;
single_node->right = NULL;
return single_node;
}
struct node *root = malloc(sizeof(struct node));
if (root == NULL)
{
fprintf(stderr, "Memory allocation failed for root node.\n");
exit(EXIT_FAILURE);
}
root->value = -1;
root->symbol = -1;
root->left = NULL;
root->right = NULL;
int i=0;
for (i = 0; i < symbols_number; i++)
{
struct node *current = root;
int j=0;
for (j = 0; j < code_tab[i].length; j++)
{
if (((code_tab[i].code[j / 8]) >> (7 - (j % 8))) & 1)
{
if (current->right == NULL)
{
current->right = malloc(sizeof(struct node));
if (current->right == NULL)
{
fprintf(stderr, "Memory allocation failed for node.\n");
exit(EXIT_FAILURE);
}
current->right->value = -1;
current->right->symbol = -1;
current->right->left = NULL;
current->right->right = NULL;
}
current = current->right;
}
else
{
if (current->left == NULL)
{
current->left = malloc(sizeof(struct node));
if (current->left == NULL)
{
fprintf(stderr, "Memory allocation failed for node.\n");
exit(EXIT_FAILURE);
}
current->left->value = -1;
current->left->symbol = -1;
current->left->left = NULL;
current->left->right = NULL;
}
current = current->left;
}
}
if (current->left != NULL || current->right != NULL)
{
fprintf(stderr, "Invalid code table. Tree structure is not correct.\n");
exit(EXIT_FAILURE);
}
current->symbol = code_tab[i].symbol;
}
return root;
}
void decode_huffman(struct node* root, const char* encoded_file, const char* decoded_file)
{
FILE* input_file = fopen(encoded_file, "rb");
if (input_file == NULL)
{
fprintf(stderr, "Unable to open input file %s\n", encoded_file);
exit(EXIT_FAILURE);
}
FILE* output_file = fopen(decoded_file, "wb");
if (output_file == NULL)
{
fprintf(stderr, "Unable to open output file %s\n", decoded_file);
exit(EXIT_FAILURE);
}
struct node* current = root;
int bit_index = 0;
unsigned char byte;
int bytes_decoded = 0;
while (bytes_decoded < bytes_number && fread(&byte, sizeof(unsigned char), 1, input_file) > 0)
{
int i;
for (i = 7; i >= 0; --i)
{
if (((byte >> i) & 1) == 1)
{
current = current->right;
}
else
{
current = current->left;
}
if (current->left == NULL && current->right == NULL)
{
fwrite(&(current->symbol), sizeof(unsigned char), 1, output_file);
current = root;
bytes_decoded++;
if (bytes_decoded == bytes_number) break;
}
}
}
fclose(input_file);
fclose(output_file);
}
void set_filenames(char* filenames[], char *argv[])
{
filenames[0] = strdup(new_extension(argv[1], "codeTable"));
filenames[1] = strdup(new_extension(argv[1], "bytes"));
filenames[2] = strdup(new_extension(argv[1], "huffman"));
filenames[3] = strdup(new_extension(argv[1], "decoded"));
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Give name of file as parameter!\n");
exit(EXIT_FAILURE);
}
char *filenames[4];
set_filenames(filenames, argv);
struct code_table* code_tab = load_code_table(filenames[0]);
bytes_number=load_bytes_number(filenames[1]);
struct node* root=recreate_binary_tree(code_tab);
decode_huffman(root, filenames[2], filenames[3]);
free(code_tab);
int i;
for (i = 0; i < 4; i++) free(filenames[i]);
return 0;
}