-
Notifications
You must be signed in to change notification settings - Fork 4
/
compress_lzma.c
341 lines (310 loc) · 8.51 KB
/
compress_lzma.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
/*
FuseCompress
Copyright (C) 2006 Milan Svoboda <milan.svoboda@centrum.cz>
LZMA module (C) 2008 Ulrich Hecht <uli@suse.de>
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdint.h>
#include <lzma.h>
#if LZMA_VERSION <= UINT32_C(49990030)
#define LZMA_EASY_ENCODER(a,b) lzma_easy_encoder_single(a,b)
#elif LZMA_VERSION <= UINT32_C(49990050)
#define LZMA_EASY_ENCODER(a,b) lzma_easy_encoder(a,b)
#else
#define LZMA_EASY_ENCODER(a,b) lzma_easy_encoder(a,b,LZMA_CHECK_CRC32)
#endif
#include "structs.h"
#include "globals.h"
#include "file.h"
#include "log.h"
#include "compress.h"
#define BUF_SIZE 4096
static const lzma_stream lzma_stream_init = LZMA_STREAM_INIT;
/**
*
*
* @param fd_source Source file descriptor
* @param fd_dest Destination file descriptor
* @return Number of bytes read from fd_source or (off_t)-1 on error
*/
static off_t lzmaCompress(void *cancel_cookie, int fd_source, int fd_dest)
{
DEBUG_("lzmaCompress started");
unsigned char bufin[BUF_SIZE]; /* data read from file */
unsigned char bufout[BUF_SIZE]; /* compressed data */
unsigned int wr; /* bytes written to compressed file */
int rd; /* bytes read from uncompressed file */
off_t size = 0; /* total size read (uncompressed) */
int ret;
int dup_fd = dup(fd_dest);
if (dup_fd == -1)
{
return (off_t) FAIL;
}
/* init LZMA encoder */
lzma_stream lstr = lzma_stream_init;
ret = LZMA_EASY_ENCODER(&lstr, compresslevel[2] - '0');
if(ret != LZMA_OK) {
ERR_("LZMA_EASY_ENCODER failed: %d",ret);
close(dup_fd);
return (off_t)FAIL;
}
while ((rd = read(fd_source, bufin, sizeof(bufin))) > 0)
{
lstr.next_in = (const uint8_t*)bufin;
lstr.avail_in = rd;
while(lstr.avail_in) {
lstr.next_out = (uint8_t*)bufout;
lstr.avail_out = BUF_SIZE;
ret = lzma_code(&lstr, LZMA_RUN);
if(ret != LZMA_OK) {
lzma_end(&lstr);
ERR_("lzma_code failed");
close(dup_fd);
return (off_t)FAIL;
}
//assert(lstr.avail_in == 0);
wr = write(dup_fd, bufout, BUF_SIZE - lstr.avail_out);
DEBUG_("lzmaCompress %d bytes written",wr);
if (wr < BUF_SIZE - lstr.avail_out)
{ /* unable to write all compressed data */
lzma_end(&lstr);
ERR_("write failed");
close(dup_fd);
return (off_t) FAIL;
}
}
size += rd;
if (compress_testcancel(cancel_cookie))
{ /* we're told to cancel compression */
break;
}
}
if (rd < 0)
{
lzma_end(&lstr);
ERR_("read error");
close(dup_fd);
return (off_t) FAIL;
}
/* compress remaining data in input buffer */
for(;;) {
lstr.next_out = bufout;
lstr.avail_out = BUF_SIZE;
ret = lzma_code(&lstr, LZMA_FINISH);
DEBUG_("LZMA_FINISHED out'ed %zd bytes",BUF_SIZE - lstr.avail_out);
if(ret != LZMA_OK && ret != LZMA_STREAM_END) {
lzma_end(&lstr);
ERR_("LZMA_FINISH failed: %d",ret);
close(dup_fd);
return (off_t)FAIL;
}
if(write(dup_fd, bufout, BUF_SIZE - lstr.avail_out) < 0) {
ERR_("writing tail failed");
close(dup_fd);
return ret;
}
if(ret == LZMA_STREAM_END) break;
}
lzma_end(&lstr);
DEBUG_("returned size %ld",size);
close(dup_fd);
return size;
}
/**
* Decompress data from fd_source into fd_dest.
*
* @param fd_source Source file descriptor
* @param fd_dest Destination file descriptor
* @return Number of bytes written to fd_dest or (off_t)-1 on error
*/
static off_t lzmaDecompress(int fd_source, int fd_dest)
{
DEBUG_("lzmaDecompress started");
unsigned char bufin[BUF_SIZE]; /* compressed input buffer */
unsigned char bufout[BUF_SIZE]; /* uncompressed output buffer */
int wr; /* uncompressed bytes written */
int rd; /* compressed bytes read */
off_t size = 0; /* total uncompressed bytes written */
int ret;
int dup_fd = dup(fd_source);
if(dup_fd == -1) return (off_t)FAIL;
/* init LZMA decoder */
lzma_stream lstr = lzma_stream_init;
#if LZMA_VERSION <= UINT32_C(49990030)
ret = lzma_auto_decoder(&lstr, NULL, NULL);
#else
ret = lzma_auto_decoder(&lstr, -1, 0);
#endif
if(ret != LZMA_OK) {
ERR_("lzma_auto_decoder failed");
close(dup_fd);
return (off_t)FAIL;
}
while ((rd = read(dup_fd, bufin, BUF_SIZE)) > 0)
{
lstr.next_in = bufin;
lstr.avail_in = rd;
while(lstr.avail_in) {
lstr.next_out = bufout;
lstr.avail_out = BUF_SIZE;
ret = lzma_code(&lstr, LZMA_RUN);
if(ret != LZMA_OK && ret != LZMA_STREAM_END) { /* decompression error */
lzma_end(&lstr);
ERR_("lzma_code failed: %d",ret);
close(dup_fd);
return (off_t)FAIL;
}
wr = write(fd_dest, bufout, BUF_SIZE - lstr.avail_out);
if (wr == -1) {
lzma_end(&lstr);
ERR_("write failed");
close(dup_fd);
return (off_t) FAIL;
}
size += wr;
if(ret == LZMA_STREAM_END) break;
}
if (ret == LZMA_STREAM_END) break;
}
lzma_end(&lstr);
close(dup_fd);
if (rd == -1)
{
ERR_("read error");
return (off_t) FAIL;
}
return size;
}
struct lzmafile {
lzma_stream str; /* codec stream descriptor */
int fd; /* backing file descriptor */
char mode; /* access mode ('r' or 'w') */
unsigned char rdbuf[BUF_SIZE]; /* read buffer used by lzmaRead */
};
void* lzmaOpen(int fd, const char* mode)
{
DEBUG_("lzmaOpen started");
int ret;
/* initialize LZMA stream */
struct lzmafile* lf = malloc(sizeof(struct lzmafile));
if(!lf) return NULL;
lf->fd = fd;
lf->str = lzma_stream_init;
lf->mode = mode[0];
if(mode[0] == 'r') {
#if LZMA_VERSION <= UINT32_C(49990030)
ret = lzma_auto_decoder(&lf->str, NULL, NULL);
#else
ret = lzma_auto_decoder(&lf->str, -1, 0);
#endif
lf->str.avail_in = 0;
}
else {
ret = LZMA_EASY_ENCODER(&lf->str, mode[2] - '0');
}
if(ret != LZMA_OK) return NULL;
return (void*)lf;
}
int lzmaClose(struct lzmafile* file)
{
DEBUG_("lzmaClose started");
int fd;
int ret;
unsigned char buf[BUF_SIZE]; /* buffer used when flushing remaining
output data in write mode */
if(!file) return -1;
fd = file->fd;
if(file->mode == 'w') {
/* flush LZMA output buffer */
for(;;) {
file->str.next_out = buf;
file->str.avail_out = BUF_SIZE;
ret = lzma_code(&file->str, LZMA_FINISH);
if(ret != LZMA_STREAM_END && ret != LZMA_OK) {
lzma_end(&file->str);
return -1;
}
int outsize = BUF_SIZE - file->str.avail_out;
DEBUG_("LZMA_FINISH out'ed %d bytes",outsize);
if(write(fd, buf, outsize) != outsize) {
lzma_end(&file->str);
return -1;
}
if(ret == LZMA_STREAM_END) break;
}
}
DEBUG_("lzmaClose total out %ld bytes",file->str.total_out);
lzma_end(&file->str);
free(file);
return close(fd);
}
int lzmaWrite(struct lzmafile* file, void* buf, unsigned int len)
{
DEBUG_("lzmaWrite started");
int ret;
lzma_stream* lstr = &file->str;
unsigned char bufout[len]; /* compressed output buffer */
if(file->mode != 'w') return -1;
lstr->next_in = buf;
lstr->avail_in = len;
while(lstr->avail_in) {
lstr->next_out = bufout;
lstr->avail_out = len;
ret = lzma_code(lstr, LZMA_RUN);
DEBUG_("lzma_code in %d out %ld ret %d",len,len-lstr->avail_out, ret);
if(ret != LZMA_OK) return -1;
DEBUG_("writing at %zd",lseek(file->fd,0,SEEK_CUR));
ret = write(file->fd, bufout, len-lstr->avail_out);
if(ret != len-lstr->avail_out) return -1;
}
return len;
}
int lzmaRead(struct lzmafile* file, void* buf, unsigned int len)
{
DEBUG_("lzmaRead started at %zd", lseek(file->fd, 0, SEEK_CUR));
int ret;
if(file->mode != 'r') return -1;
lzma_stream* lstr = &file->str;
lstr->next_out = buf;
lstr->avail_out = len;
/* decompress until EOF or output buffer is full */
while(lstr->avail_out) {
if(lstr->avail_in == 0) {
/* refill input buffer */
if((ret = read(file->fd, file->rdbuf, BUF_SIZE)) < 0)
return ret;
DEBUG_("read %d bytes",ret);
if(ret == 0) break; /* EOF */
lstr->next_in = file->rdbuf;
lstr->avail_in = ret;
}
DEBUG_("avail_in %zd avail_out %zd",lstr->avail_in,lstr->avail_out);
ret = lzma_code(lstr, LZMA_RUN);
if(ret != LZMA_OK && ret != LZMA_STREAM_END) {
ERR_("decoding failed: %d",ret);
return -1;
}
if(ret == LZMA_STREAM_END) break;
}
DEBUG_("decoded to %zd bytes", len - lstr->avail_out);
return len - lstr->avail_out;
}
compressor_t module_lzma = {
.type = 0x04,
.name = "lzma",
.compress = lzmaCompress,
.decompress = lzmaDecompress,
.open = (void *(*) (int fd, const char *mode)) lzmaOpen,
.write = (int (*)(void *file, void *buf, unsigned int len)) lzmaWrite,
.read = (int (*)(void *file, void *buf, unsigned int len)) lzmaRead,
.close = (int (*)(void *file)) lzmaClose,
};