-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecompress.c
404 lines (336 loc) · 10.3 KB
/
decompress.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/**
* @file decompress.c
* @brief Decompress functions.
* @date 2014-2015
* @author Fabio Santos <ffsantos92@gmail.com>
* @author Eurico Sousa <2110133@my.ipleiria.pt>
*/
#include "decompress.h"
#include "common.h"
/* External variables */
extern int got_signal;
/**
* Decompress a given palz file.
* @param source_filename
* @param dictionary
* @return compress ratio
* @see compress_ratio()
*/
float decompress_file(TDictionary **dictionary, char *source_filename){
TDictionary *aux = NULL;
aux = *dictionary;
size_t len = 0;
char *final_filename = NULL;
char *line = NULL;
char *element = NULL;
int read;
int val = 0;
int nbytes = 0;
int bytesForInt = 0;
unsigned int last_element = 0;
float source_file_size = 0;
float final_file_size = 0;
FILE *fpTempFile = NULL;
FILE *fpSourceFile = NULL;
FILE *fpFinalFile = NULL;
TDictionary *dictWords = NULL;
dictWords = MALLOC(sizeof(TDictionary));
dictWords->nElements = 0;
/* Open .palz file */
if ((fpSourceFile = fopen(source_filename, "r")) == NULL) {
return ERR_FOPEN;
}
/* Get first line */
if (getline(&line, &len, fpSourceFile) == -1) {
return ERR_PALZCORRUPTED;
}
if (!is_header_PALZ(line)) {
return ERR_PALZEXTENSION;
}
/* Get second line */
if (getline(&line, &len, fpSourceFile) == -1) {
return ERR_PALZCORRUPTED;
}
if ((val = is_valid_size(line)) == -1) {
return ERR_PALZCORRUPTED;
}
if (bytes_for_int(val+14) == -1) {
return ERR_PALZBIGDICTIONARY;
}
while (val != 0) {
nbytes = getline(&line, &len, fpSourceFile);
if ((element = malloc(sizeof(char)*nbytes)) == NULL) {
return ERR_PALZCORRUPTED;
}
strncpy(element, line, nbytes-1);
element[nbytes-1] = '\0';
dictionary_add_element(&dictWords, &element, nbytes);
FREE(element);
val--;
}
FREE(line);
bytesForInt = bytes_for_int(dictWords->nElements + 14);
unsigned char elementN[bytesForInt];
fpTempFile = tmpfile();
while (!feof(fpSourceFile)) {
while (fread(&elementN, 1, bytesForInt, fpSourceFile)) {
/**
* Check if number read from binary code is greater than dictionary entries
* or less than zero.
*/
if (*(int *)elementN > (dictWords->nElements + 14) ||
*(int *)elementN < 0) {
return ERR_PALZCORRUPTED;
}
/* Check for repetition */
if (*(int *)elementN == 0) {
/**
* We can't start with a repetition. So in this case,
* we verify if last_element exists to validate the repetition.
*/
if (last_element == 0) {
return ERR_PALZCORRUPTED;
}
/* Check how many times the last_element must be repeated */
if (fread(&elementN, 1, bytesForInt, fpSourceFile)
!= (unsigned int)bytesForInt) {
return ERR_PALZCORRUPTED;
}
/* last_element can't be repeated zero or less times */
if(*(int *)elementN <= 0) {
return ERR_PALZCORRUPTED;
}
/* Repeat for elementN times */
while(*(int *)elementN != 0) {
if(last_element < 15){
fputs(aux->element[last_element-1].element, fpTempFile);
}else{
fputs(dictWords->element[last_element-15].element, fpTempFile);
}
*(int *)elementN -= 1;
}
}else{
if(*(int *)elementN < 15){
fputs(aux->element[*(int *)elementN-1].element, fpTempFile);
}else{
fputs(dictWords->element[*(int *)elementN - 15].element, fpTempFile);
}
last_element = *(int *)elementN;
}
}
}
fclose(fpSourceFile);
int i;
for(i = 0; i< dictWords->nElements; i++){
FREE(dictWords->element[i].element);
}
FREE(dictWords->element);
FREE(dictWords);
if ((source_file_size = get_size(source_filename))==-1) {
return ERR_FSTATUS;
}
/*
* Sets the file position indicator for the stream
* pointed to by stream to the beginning of the file.
*/
rewind(fpTempFile);
if (is_dot_palz(source_filename)) {
final_filename = remove_dot_palz(source_filename);
} else {
final_filename = source_filename;
}
fpFinalFile = fopen(final_filename, "w");
/* Copy data from tmpfile to destination file */
while((read = fgetc(fpTempFile)) != EOF) {
fputc(read, fpFinalFile);
}
fclose(fpTempFile);
fclose(fpFinalFile);
if ((final_file_size = get_size(final_filename))==-1) {
return ERR_FSTATUS;
}
fprintf(stderr,"Compression ratio: %s ", final_filename);
return compress_ratio(source_file_size, final_file_size);
}
/**
* Check if header_first_row contains "PALZ\n".
* @param header_first_row first row of file
* @return 1 if true, 0 if false
*/
int is_header_PALZ(const char *header_first_row){
if (strcmp(header_first_row,MAGIC_PALZ) == 0) {
return 1;
}
return 0;
}
/**
* Check if a given string is a valid integer. Based on strtol page from manual.
* @param size_str second row of file
* @return val value represented on string or -1 if string is a non-valid integer
*/
int is_valid_size(const char *size_str){
int base = 10;
char *first_failure;
long val;
errno = 0;
val = strtol(size_str, &first_failure, base);
/* Check for various possible errors */
if (val < 0 || (errno != 0 && val == 0)) {
return -1;
}
if (first_failure != NULL && (*first_failure != '\0')
& (*first_failure != '\n')) {
return -1;
}
return val;
}
/**
* Search for palz files in a given folder and sub-folders. For every palz file
* found, call decompress_file() function.
* @param directory main directory where to start
* @param dictionary
* @return 0 at end
* @see decompress_file()
*/
int decompress_folder(TDictionary **dictionary, const char *directory){
TDictionary *aux = NULL;
aux = *dictionary;
DIR *dir = NULL;
struct dirent *dirent = NULL;
char *nextDirent = NULL;
float output = 0;
/* Open directory */
if ((dir = opendir(directory)) == NULL) {
return ERR_FOPEN;
}
/* While readdir returns a pointer to a dirent structure */
while (!got_signal && (dirent = readdir(dir)) != NULL) {
nextDirent = MALLOC(sizeof(char)*(strlen(dirent->d_name)
+ strlen (directory) + 2));
strcpy(nextDirent, directory);
/* Appends d_name */
strcat(nextDirent, dirent->d_name);
/* DT_DIR = directory */
if (dirent->d_type == DT_DIR && strcmp(dirent->d_name, ".") != 0
&& strcmp(dirent->d_name, "..") != 0) {
strcat(nextDirent, "/");
decompress_folder(&aux, nextDirent);
/* DT_REG = regular file */
} else if (dirent->d_type == DT_REG) {
if (is_dot_palz(dirent->d_name)) {
dictionary_restart(&aux);
if ((output = decompress_file(&aux, nextDirent)) < 0) {
get_error_msg(output, nextDirent);
} else {
fprintf(stderr,"%.2f %%\n", output);
}
}
}
FREE(nextDirent);
}
closedir(dir);
return 0;
}
/**
* Search for .palz files in a given folder and sub-folders. For every .palz file
* found, call decompress_file() function using threads. Each file must be
* decompressed by one thread only.
* @param directory main directory where to start
* @param max_threads maximum number of threads
* @return 0 at end
* @see decompress_file()
*/
int parallel_folder_decompress(TDictionary **dictionary, char *directory,
int max_threads){
char **files_to_decompress = NULL;
int amount = 0;
float output = 0;
pthread_t thr[max_threads];
PARAM_T param;
/* Initialize the mutex */
if ((errno = pthread_mutex_init(¶m.mutex, NULL)) != 0) {
ERROR(C_ERRO_MUTEX_INIT, "pthread_mutex_init() failed!");
}
/* Initialize the condition variable */
if ((errno = pthread_cond_init(¶m.cond, NULL)) != 0) {
ERROR(C_ERRO_CONDITION_INIT, "pthread_cond_init() failed!");
}
/* Initialize other parameters */
param.buffer = MALLOC(max_threads*sizeof(char*));
param.index_reading = 0;
param.index_writing = 0;
param.total = 0;
param.stop = 0;
param.max = max_threads;
param.mode = DECOMPRESS_MODE;
param.dictionary = dictionary;
int i;
for(i=0; i<param.max; i++){
param.buffer[i] = NULL;
}
/* Create the threads */
for(i=0; i<max_threads; i++){
if ((errno = pthread_create(&thr[i], NULL, consumer, ¶m) != 0)) {
ERROR(C_ERRO_PTHREAD_CREATE, "pthread_create() failed!");
}
}
/* Search for all non-palz files */
if ((output = get_files_from_dir(directory, &files_to_decompress, &amount,
DECOMPRESS_MODE)) < 0) {
get_error_msg(output, NULL);
}
/* Give all files to compress to write on buffer */
for(i = 0; i<amount; i++){
write_on_buffer(files_to_decompress[i], ¶m);
FREE(files_to_decompress[i]);
}
FREE(files_to_decompress);
/* Enters critical section */
if ((errno = pthread_mutex_lock(&(param.mutex))) != 0) {
WARNING("pthread_mutex_lock() failed\n");
return 0;
}
/* Stop production */
param.stop = 1;
/* Notify all threads */
if ((errno = pthread_cond_broadcast(&(param.cond))) != 0) {
WARNING("pthread_cond_broadcast() failed");
return 0;
}
/* Exits critical section */
if ((errno = pthread_mutex_unlock(&(param.mutex))) != 0) {
WARNING("pthread_mutex_unlock() failed");
return 0;
}
/* Wait for the threads to finish */
for(i=0; i<max_threads; i++) {
if ((errno = pthread_join(thr[i], NULL)) != 0) {
ERROR(C_ERRO_PTHREAD_JOIN, "pthread_join() failed!");
}
}
/* Free the mutex */
if ((errno = pthread_mutex_destroy(¶m.mutex)) != 0) {
ERROR(C_ERRO_MUTEX_DESTROY, "pthread_mutex_destroy() failed!");
}
/* Free consumer condition variable */
if ((errno = pthread_cond_destroy(¶m.cond)) != 0) {
ERROR(C_ERRO_CONDITION_DESTROY, "pthread_cond_destroy() failed!");
}
/* Free buffer */
for (i=0; i<param.max; i++){
FREE(param.buffer[i]);
}
FREE(param.buffer);
return 0;
}
/**
* Remove .palz from a certain filename.
* @param filename
* @return new_filename without .palz
*/
char* remove_dot_palz(const char *source_filename){
char *new_filename = (char*)source_filename;
/* replace dot with \0 */
new_filename[strlen(new_filename)-5] = '\0';
return new_filename;
}