-
Notifications
You must be signed in to change notification settings - Fork 7
/
wrapper.c
291 lines (241 loc) · 7.58 KB
/
wrapper.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
/*
* Basic implementation of LZ78 compression algorithm
*
* Copyright (C) 2010 evilaliv3 <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "wrapper.h"
/* Structure representing the type of algorithm */
struct __algorithm {
char* name; /* String representing the name */
uint8_t type; /* Constant representing the type */
};
/* Opaque type representing the type of algorithm */
typedef struct __algorithm algorithm;
/* Struct of available algorithms */
const algorithm algo_list[] = {
{"lz78", LZ78_ALGORITHM},
{NULL, UNKNOWN_ALGORITHM}
};
/* Struct representing the wrapper used for compression or decompression */
struct __wrapper {
uint8_t type; /* Algorithm used to compress or decompress data */
uint8_t mode; /* Flag indicating compress/decompress mode */
void* data; /* Opaque structure representing the algorithm */
};
/* Global variable representing the current error stored */
uint8_t wrapper_cur_err = WRAPPER_SUCCESS;
/* Associate an algorithm-dependent error to a wrapper-generic error */
uint8_t wrapper_return(uint8_t code) {
wrapper_cur_err = code;
switch (code) {
case LZ78_SUCCESS:
return WRAPPER_SUCCESS;
case LZ78_ERROR_READ:
return WRAPPER_ERROR_READ;
case LZ78_ERROR_WRITE:
return WRAPPER_ERROR_WRITE;
case LZ78_ERROR_EAGAIN:
return WRAPPER_ERROR_EAGAIN;
case LZ78_ERROR_COMPRESS:
return WRAPPER_ERROR_COMPRESS;
case LZ78_ERROR_DECOMPRESS:
return WRAPPER_ERROR_DECOMPRESS;
case LZ78_ERROR_DICTIONARY:
case LZ78_ERROR_INITIALIZATION:
case LZ78_ERROR_MODE:
return WRAPPER_ERROR_GENERIC;
}
return code;
}
uint8_t get_algorithm(char* type) {
uint8_t i = 0;
while (algo_list[i].name != NULL) {
if (strcmp(type, algo_list[i].name) == 0)
return algo_list[i].type;
++i;
}
return UNKNOWN_ALGORITHM;
}
int byte_size(char* size) {
int n;
if (size == NULL)
return 0;
n = atoi(size);
switch (size[strlen(size) - 1]) {
case 'K':
n <<= 10;
break;
case 'M':
n <<= 20;
break;
}
return (n < 0) ? 0 : n;
}
void wrapper_perror() {
switch (wrapper_cur_err) {
case WRAPPER_SUCCESS:
break;
case WRAPPER_ERROR_ALGORITHM:
fprintf(stderr, "Unrecognized compression algorithm\n");
break;
case WRAPPER_ERROR_FILE_IN:
fprintf(stderr, "Unable to read input file\n");
break;
case WRAPPER_ERROR_FILE_OUT:
fprintf(stderr, "Unable to write output file\n");
break;
case LZ78_SUCCESS:
break;
case LZ78_ERROR_DICTIONARY:
fprintf(stderr, "LZ78: unable to allocate dictionaries\n");
break;
case LZ78_ERROR_INITIALIZATION:
fprintf(stderr, "LZ78: bad initialization\n");
break;
case LZ78_ERROR_MODE:
fprintf(stderr, "LZ78: wrong compression/decompression mode\n");
break;
case LZ78_ERROR_READ:
fprintf(stderr, "LZ78: unable to read input data\n");
break;
case LZ78_ERROR_WRITE:
fprintf(stderr, "LZ78: unable to write output data\n");
break;
case LZ78_ERROR_EAGAIN:
fprintf(stderr, "LZ78: I/O operation would block: retry...\n");
break;
case LZ78_ERROR_COMPRESS:
fprintf(stderr, "LZ78: unable to compress input data\n");
break;
case LZ78_ERROR_DECOMPRESS:
fprintf(stderr, "LZ78: unable to decompress input data\n");
break;
default:
fprintf(stderr, "Unhandled error code %d\n", wrapper_cur_err);
}
}
wrapper* wrapper_new(uint8_t w_mode, uint8_t w_type, char* argv) {
wrapper* w = malloc(sizeof(wrapper));
if (w == NULL)
return NULL;
w->type = w_type;
w->mode = w_mode;
switch (w->type) {
case LZ78_ALGORITHM:
w->data = lz78_new(w_mode, byte_size(argv));
break;
default:
free(w);
return NULL;
}
if (w->data)
return w;
else {
free(w);
return NULL;
}
}
void wrapper_destroy(wrapper* w) {
if (w == NULL)
return;
switch (w->type) {
case LZ78_ALGORITHM:
lz78_destroy(w->data);
break;
default:
return;
}
free(w);
}
uint8_t wrapper_compress(wrapper* w, char* input, char* output) {
uint8_t ret;
int fd_in;
int fd_out;
switch (w->type) {
case LZ78_ALGORITHM:
if (input == NULL) {
fd_in = STDIN_FILENO;
} else {
fd_in = open(input, ACCESS_READ);
if (fd_in == -1)
return wrapper_return(WRAPPER_ERROR_FILE_IN);
}
if (output == NULL) {
fd_out = STDOUT_FILENO;
} else {
fd_out = open(output, ACCESS_WRITE, 0644);
if (fd_out == -1) {
close(fd_in);
return wrapper_return(WRAPPER_ERROR_FILE_OUT);
}
}
ret = lz78_compress(w->data, fd_in, fd_out);
close(fd_in);
close(fd_out);
return wrapper_return(ret);
default:
return wrapper_return(WRAPPER_ERROR_ALGORITHM);
}
}
uint8_t wrapper_decompress(wrapper* w, char* input, char* output) {
uint8_t ret;
int fd_in;
int fd_out;
switch (w->type) {
case LZ78_ALGORITHM:
if (input == NULL) {
fd_in = STDIN_FILENO;
} else {
fd_in = open(input, ACCESS_READ);
if (fd_in == -1)
return wrapper_return(WRAPPER_ERROR_FILE_IN);
}
if (output == NULL) {
fd_out = STDOUT_FILENO;
} else {
fd_out = open(output, ACCESS_WRITE, 0644);
if (fd_out == -1) {
close(fd_in);
return wrapper_return(WRAPPER_ERROR_FILE_OUT);
}
}
ret = lz78_decompress(w->data, fd_in, fd_out);
close(fd_in);
close(fd_out);
return wrapper_return(ret);
default:
return wrapper_return(WRAPPER_ERROR_ALGORITHM);
}
}
uint8_t wrapper_exec(wrapper* w, char* input, char* output) {
uint8_t ret;
if (w->mode == WRAPPER_MODE_COMPRESS) {
for (;;) {
ret = wrapper_compress(w, input, output);
if (ret != WRAPPER_ERROR_EAGAIN)
return ret;
}
} else {
for (;;) {
ret = wrapper_decompress(w, input, output);
if (ret != WRAPPER_ERROR_EAGAIN)
return ret;
}
}
}