blob: f9f16fc3ea056faab0eb965cb68d76fa1d1ca1c2 [file] [log] [blame]
Daniel Veillardadf5ec92012-01-26 16:56:22 +08001/**
2 * xzlib.c: front end for the transparent suport of lzma compression
3 * at the I/O layer, based on an example file from lzma project
4 *
5 * See Copyright for the status of this software.
6 *
7 * Anders F Bjorklund <afb@users.sourceforge.net>
8 */
9#define IN_LIBXML
10#include "libxml.h"
11#ifdef HAVE_LZMA_H
12
13#include <string.h>
14#ifdef HAVE_ERRNO_H
15#include <errno.h>
16#endif
17
18
19#ifdef HAVE_SYS_TYPES_H
20#include <sys/types.h>
21#endif
22#ifdef HAVE_SYS_STAT_H
23#include <sys/stat.h>
24#endif
25#ifdef HAVE_FCNTL_H
26#include <fcntl.h>
27#endif
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
31#ifdef HAVE_STDLIB_H
32#include <stdlib.h>
33#endif
34#ifdef HAVE_ZLIB_H
35#include <zlib.h>
36#endif
37#include <lzma.h>
38
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020039#include "xzlib.h"
40
41/* values for xz_state how */
Daniel Veillardadf5ec92012-01-26 16:56:22 +080042#define LOOK 0 /* look for a gzip/lzma header */
43#define COPY 1 /* copy input directly */
44#define GZIP 2 /* decompress a gzip stream */
45#define LZMA 3 /* decompress a lzma stream */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020046
47/* internal lzma file state data structure */
48typedef struct {
Daniel Veillardadf5ec92012-01-26 16:56:22 +080049 int mode; /* see lzma modes above */
50 int fd; /* file descriptor */
51 char *path; /* path or fd for error messages */
52 uint64_t pos; /* current position in uncompressed data */
Daniel Veillard72789ef2012-04-02 17:52:20 +080053 unsigned int size; /* buffer size, zero if not allocated yet */
54 unsigned int want; /* requested buffer size, default is BUFSIZ */
Daniel Veillardadf5ec92012-01-26 16:56:22 +080055 unsigned char *in; /* input buffer */
56 unsigned char *out; /* output buffer (double-sized when reading) */
57 unsigned char *next; /* next output data to deliver or write */
Daniel Veillard72789ef2012-04-02 17:52:20 +080058 unsigned int have; /* amount of output data unused at next */
Daniel Veillardadf5ec92012-01-26 16:56:22 +080059 int eof; /* true if end of input file reached */
60 uint64_t start; /* where the lzma data started, for rewinding */
61 uint64_t raw; /* where the raw data started, for seeking */
62 int how; /* 0: get header, 1: copy, 2: decompress */
63 int direct; /* true if last read direct, false if lzma */
64 /* seek request */
65 uint64_t skip; /* amount to skip (already rewound if backwards) */
66 int seek; /* true if seek request pending */
67 /* error information */
68 int err; /* error code */
69 char *msg; /* error message */
70 /* lzma stream */
71 lzma_stream strm; /* stream structure in-place (not a pointer) */
72 char padding1[32]; /* padding allowing to cope with possible
73 extensions of above structure without
74 too much side effect */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020075#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +080076 /* zlib inflate or deflate stream */
77 z_stream zstrm; /* stream structure in-place (not a pointer) */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020078#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +080079 char padding2[32]; /* padding allowing to cope with possible
80 extensions of above structure without
81 too much side effect */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020082} xz_state, *xz_statep;
83
Daniel Veillardadf5ec92012-01-26 16:56:22 +080084static void
85xz_error(xz_statep state, int err, const char *msg)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020086{
87 /* free previously allocated message and clear */
88 if (state->msg != NULL) {
89 if (state->err != LZMA_MEM_ERROR)
90 free(state->msg);
91 state->msg = NULL;
92 }
93
94 /* set error code, and if no message, then done */
95 state->err = err;
96 if (msg == NULL)
97 return;
98
99 /* for an out of memory error, save as static string */
100 if (err == LZMA_MEM_ERROR) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800101 state->msg = (char *) msg;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200102 return;
103 }
104
105 /* construct error message with path */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800106 if ((state->msg =
107 malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200108 state->err = LZMA_MEM_ERROR;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800109 state->msg = (char *) "out of memory";
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200110 return;
111 }
112 strcpy(state->msg, state->path);
113 strcat(state->msg, ": ");
114 strcat(state->msg, msg);
115 return;
116}
117
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800118static void
119xz_reset(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200120{
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800121 state->have = 0; /* no output data available */
122 state->eof = 0; /* not at end of file */
123 state->how = LOOK; /* look for gzip header */
124 state->direct = 1; /* default for empty file */
125 state->seek = 0; /* no seek request pending */
126 xz_error(state, LZMA_OK, NULL); /* clear error */
127 state->pos = 0; /* no uncompressed data yet */
128 state->strm.avail_in = 0; /* no input data yet */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200129#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800130 state->zstrm.avail_in = 0; /* no input data yet */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200131#endif
132}
133
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800134static xzFile
135xz_open(const char *path, int fd, const char *mode ATTRIBUTE_UNUSED)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200136{
137 xz_statep state;
138
139 /* allocate xzFile structure to return */
140 state = malloc(sizeof(xz_state));
141 if (state == NULL)
142 return NULL;
143 state->size = 0; /* no buffers allocated yet */
144 state->want = BUFSIZ; /* requested buffer size */
145 state->msg = NULL; /* no error message yet */
146
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800147 /* save the path name for error messages */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200148 state->path = malloc(strlen(path) + 1);
149 if (state->path == NULL) {
150 free(state);
151 return NULL;
152 }
153 strcpy(state->path, path);
154
155 /* open the file with the appropriate mode (or just use fd) */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800156 state->fd = fd != -1 ? fd : open(path,
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200157#ifdef O_LARGEFILE
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800158 O_LARGEFILE |
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200159#endif
160#ifdef O_BINARY
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800161 O_BINARY |
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200162#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800163 O_RDONLY, 0666);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200164 if (state->fd == -1) {
165 free(state->path);
166 free(state);
167 return NULL;
168 }
169
170 /* save the current position for rewinding (only if reading) */
171 state->start = lseek(state->fd, 0, SEEK_CUR);
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800172 if (state->start == (uint64_t) - 1)
173 state->start = 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200174
175 /* initialize stream */
176 xz_reset(state);
177
178 /* return stream */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800179 return (xzFile) state;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200180}
181
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800182xzFile
183__libxml2_xzopen(const char *path, const char *mode)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200184{
185 return xz_open(path, -1, mode);
186}
187
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800188xzFile
189__libxml2_xzdopen(int fd, const char *mode)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200190{
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800191 char *path; /* identifier for error messages */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200192 xzFile xz;
193
194 if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
195 return NULL;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800196 sprintf(path, "<fd:%d>", fd); /* for debugging */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200197 xz = xz_open(path, fd, mode);
198 free(path);
199 return xz;
200}
201
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800202static int
Daniel Veillard72789ef2012-04-02 17:52:20 +0800203xz_load(xz_statep state, unsigned char *buf, unsigned int len,
204 unsigned int *have)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200205{
206 int ret;
207
208 *have = 0;
209 do {
210 ret = read(state->fd, buf + *have, len - *have);
211 if (ret <= 0)
212 break;
213 *have += ret;
214 } while (*have < len);
215 if (ret < 0) {
216 xz_error(state, -1, strerror(errno));
217 return -1;
218 }
219 if (ret == 0)
220 state->eof = 1;
221 return 0;
222}
223
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800224static int
225xz_avail(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200226{
227 lzma_stream *strm = &(state->strm);
228
229 if (state->err != LZMA_OK)
230 return -1;
231 if (state->eof == 0) {
232 if (xz_load(state, state->in, state->size,
Daniel Veillard72789ef2012-04-02 17:52:20 +0800233 (unsigned int *) &(strm->avail_in)) == -1)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200234 return -1;
235 strm->next_in = state->in;
236 }
237 return 0;
238}
239
240static int
241is_format_xz(xz_statep state)
242{
243 lzma_stream *strm = &(state->strm);
244
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800245 return strm->avail_in >= 6 && memcmp(state->in, "\3757zXZ", 6) == 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200246}
247
248static int
249is_format_lzma(xz_statep state)
250{
251 lzma_stream *strm = &(state->strm);
252
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800253 lzma_filter filter;
254 lzma_options_lzma *opt;
255 uint32_t dict_size;
256 uint64_t uncompressed_size;
257 size_t i;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200258
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800259 if (strm->avail_in < 13)
260 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200261
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800262 filter.id = LZMA_FILTER_LZMA1;
263 if (lzma_properties_decode(&filter, NULL, state->in, 5) != LZMA_OK)
264 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200265
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800266 opt = filter.options;
267 dict_size = opt->dict_size;
268 free(opt);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200269
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800270 /* A hack to ditch tons of false positives: We allow only dictionary
271 * sizes that are 2^n or 2^n + 2^(n-1) or UINT32_MAX. LZMA_Alone
272 * created only files with 2^n, but accepts any dictionary size.
273 * If someone complains, this will be reconsidered.
274 */
275 if (dict_size != UINT32_MAX) {
276 uint32_t d = dict_size - 1;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200277
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800278 d |= d >> 2;
279 d |= d >> 3;
280 d |= d >> 4;
281 d |= d >> 8;
282 d |= d >> 16;
283 ++d;
284 if (d != dict_size || dict_size == 0)
285 return 0;
286 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200287
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800288 /* Another hack to ditch false positives: Assume that if the
289 * uncompressed size is known, it must be less than 256 GiB.
290 * Again, if someone complains, this will be reconsidered.
291 */
292 uncompressed_size = 0;
293 for (i = 0; i < 8; ++i)
294 uncompressed_size |= (uint64_t) (state->in[5 + i]) << (i * 8);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200295
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800296 if (uncompressed_size != UINT64_MAX
297 && uncompressed_size > (UINT64_C(1) << 38))
298 return 0;
299
300 return 1;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200301}
302
303#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800304
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200305/* Get next byte from input, or -1 if end or error. */
306#define NEXT() ((strm->avail_in == 0 && xz_avail(state) == -1) ? -1 : \
307 (strm->avail_in == 0 ? -1 : \
308 (strm->avail_in--, *(strm->next_in)++)))
309
310/* Get a four-byte little-endian integer and return 0 on success and the value
311 in *ret. Otherwise -1 is returned and *ret is not modified. */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800312static int
313gz_next4(xz_statep state, unsigned long *ret)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200314{
315 int ch;
316 unsigned long val;
317 z_streamp strm = &(state->zstrm);
318
319 val = NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800320 val += (unsigned) NEXT() << 8;
321 val += (unsigned long) NEXT() << 16;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200322 ch = NEXT();
323 if (ch == -1)
324 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800325 val += (unsigned long) ch << 24;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200326 *ret = val;
327 return 0;
328}
329#endif
330
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800331static int
332xz_head(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200333{
334 lzma_stream *strm = &(state->strm);
335 lzma_stream init = LZMA_STREAM_INIT;
336 int flags;
337 unsigned len;
338
339 /* allocate read buffers and inflate memory */
340 if (state->size == 0) {
341 /* allocate buffers */
342 state->in = malloc(state->want);
343 state->out = malloc(state->want << 1);
344 if (state->in == NULL || state->out == NULL) {
345 if (state->out != NULL)
346 free(state->out);
347 if (state->in != NULL)
348 free(state->in);
349 xz_error(state, LZMA_MEM_ERROR, "out of memory");
350 return -1;
351 }
352 state->size = state->want;
353
354 /* allocate decoder memory */
355 state->strm = init;
356 state->strm.avail_in = 0;
357 state->strm.next_in = NULL;
358 if (lzma_auto_decoder(&state->strm, UINT64_MAX, 0) != LZMA_OK) {
359 free(state->out);
360 free(state->in);
361 state->size = 0;
362 xz_error(state, LZMA_MEM_ERROR, "out of memory");
363 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800364 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200365#ifdef HAVE_ZLIB_H
366 /* allocate inflate memory */
367 state->zstrm.zalloc = Z_NULL;
368 state->zstrm.zfree = Z_NULL;
369 state->zstrm.opaque = Z_NULL;
370 state->zstrm.avail_in = 0;
371 state->zstrm.next_in = Z_NULL;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800372 if (inflateInit2(&(state->zstrm), -15) != Z_OK) { /* raw inflate */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200373 free(state->out);
374 free(state->in);
375 state->size = 0;
376 xz_error(state, LZMA_MEM_ERROR, "out of memory");
377 return -1;
378 }
379#endif
380 }
381
382 /* get some data in the input buffer */
383 if (strm->avail_in == 0) {
384 if (xz_avail(state) == -1)
385 return -1;
386 if (strm->avail_in == 0)
387 return 0;
388 }
389
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800390 /* look for the xz magic header bytes */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200391 if (is_format_xz(state) || is_format_lzma(state)) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800392 state->how = LZMA;
393 state->direct = 0;
394 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200395 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200396#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800397 /* look for the gzip magic header bytes 31 and 139 */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200398 if (strm->next_in[0] == 31) {
399 strm->avail_in--;
400 strm->next_in++;
401 if (strm->avail_in == 0 && xz_avail(state) == -1)
402 return -1;
403 if (strm->avail_in && strm->next_in[0] == 139) {
404 /* we have a gzip header, woo hoo! */
405 strm->avail_in--;
406 strm->next_in++;
407
408 /* skip rest of header */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800409 if (NEXT() != 8) { /* compression method */
410 xz_error(state, LZMA_DATA_ERROR,
411 "unknown compression method");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200412 return -1;
413 }
414 flags = NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800415 if (flags & 0xe0) { /* reserved flag bits */
416 xz_error(state, LZMA_DATA_ERROR,
417 "unknown header flags set");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200418 return -1;
419 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800420 NEXT(); /* modification time */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200421 NEXT();
422 NEXT();
423 NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800424 NEXT(); /* extra flags */
425 NEXT(); /* operating system */
426 if (flags & 4) { /* extra field */
427 len = (unsigned) NEXT();
428 len += (unsigned) NEXT() << 8;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200429 while (len--)
430 if (NEXT() < 0)
431 break;
432 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800433 if (flags & 8) /* file name */
434 while (NEXT() > 0) ;
435 if (flags & 16) /* comment */
436 while (NEXT() > 0) ;
437 if (flags & 2) { /* header crc */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200438 NEXT();
439 NEXT();
440 }
441 /* an unexpected end of file is not checked for here -- it will be
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800442 * noticed on the first request for uncompressed data */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200443
444 /* set up for decompression */
445 inflateReset(&state->zstrm);
446 state->zstrm.adler = crc32(0L, Z_NULL, 0);
447 state->how = GZIP;
448 state->direct = 0;
449 return 0;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800450 } else {
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200451 /* not a gzip file -- save first byte (31) and fall to raw i/o */
452 state->out[0] = 31;
453 state->have = 1;
454 }
455 }
456#endif
457
458 /* doing raw i/o, save start of raw data for seeking, copy any leftover
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800459 * input to output -- this assumes that the output buffer is larger than
460 * the input buffer, which also assures space for gzungetc() */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200461 state->raw = state->pos;
462 state->next = state->out;
463 if (strm->avail_in) {
464 memcpy(state->next + state->have, strm->next_in, strm->avail_in);
465 state->have += strm->avail_in;
466 strm->avail_in = 0;
467 }
468 state->how = COPY;
469 state->direct = 1;
470 return 0;
471}
472
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800473static int
474xz_decomp(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200475{
476 int ret;
477 unsigned had;
478 unsigned long crc, len;
479 lzma_stream *strm = &(state->strm);
480
481 lzma_action action = LZMA_RUN;
482
483 /* fill output buffer up to end of deflate stream */
484 had = strm->avail_out;
485 do {
486 /* get more input for inflate() */
487 if (strm->avail_in == 0 && xz_avail(state) == -1)
488 return -1;
489 if (strm->avail_in == 0) {
490 xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
491 return -1;
492 }
493 if (state->eof)
494 action = LZMA_FINISH;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800495
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200496 /* decompress and handle errors */
497#ifdef HAVE_ZLIB_H
498 if (state->how == GZIP) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800499 state->zstrm.avail_in = (uInt) state->strm.avail_in;
500 state->zstrm.next_in = (Bytef *) state->strm.next_in;
501 state->zstrm.avail_out = (uInt) state->strm.avail_out;
502 state->zstrm.next_out = (Bytef *) state->strm.next_out;
503 ret = inflate(&state->zstrm, Z_NO_FLUSH);
504 if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
505 xz_error(state, Z_STREAM_ERROR,
506 "internal error: inflate stream corrupt");
507 return -1;
508 }
509 if (ret == Z_MEM_ERROR)
510 ret = LZMA_MEM_ERROR;
511 if (ret == Z_DATA_ERROR)
512 ret = LZMA_DATA_ERROR;
513 if (ret == Z_STREAM_END)
514 ret = LZMA_STREAM_END;
515 state->strm.avail_in = state->zstrm.avail_in;
516 state->strm.next_in = state->zstrm.next_in;
517 state->strm.avail_out = state->zstrm.avail_out;
518 state->strm.next_out = state->zstrm.next_out;
519 } else /* state->how == LZMA */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200520#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800521 ret = lzma_code(strm, action);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200522 if (ret == LZMA_MEM_ERROR) {
523 xz_error(state, LZMA_MEM_ERROR, "out of memory");
524 return -1;
525 }
526 if (ret == LZMA_DATA_ERROR) {
527 xz_error(state, LZMA_DATA_ERROR, "compressed data error");
528 return -1;
529 }
530 } while (strm->avail_out && ret != LZMA_STREAM_END);
531
532 /* update available output and crc check value */
533 state->have = had - strm->avail_out;
534 state->next = strm->next_out - state->have;
535#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800536 state->zstrm.adler =
537 crc32(state->zstrm.adler, state->next, state->have);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200538#endif
539
540 if (ret == LZMA_STREAM_END) {
541#ifdef HAVE_ZLIB_H
542 if (state->how == GZIP) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800543 if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) {
544 xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
545 return -1;
546 }
547 if (crc != state->zstrm.adler) {
548 xz_error(state, LZMA_DATA_ERROR, "incorrect data check");
549 return -1;
550 }
551 if (len != (state->zstrm.total_out & 0xffffffffL)) {
552 xz_error(state, LZMA_DATA_ERROR, "incorrect length check");
553 return -1;
554 }
555 state->strm.avail_in = 0;
556 state->strm.next_in = NULL;
557 state->strm.avail_out = 0;
558 state->strm.next_out = NULL;
559 } else
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200560#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800561 if (strm->avail_in != 0 || !state->eof) {
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200562 xz_error(state, LZMA_DATA_ERROR, "trailing garbage");
563 return -1;
564 }
565 state->how = LOOK; /* ready for next stream, once have is 0 (leave
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800566 * state->direct unchanged to remember how) */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200567 }
568
569 /* good decompression */
570 return 0;
571}
572
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800573static int
574xz_make(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200575{
576 lzma_stream *strm = &(state->strm);
577
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800578 if (state->how == LOOK) { /* look for lzma / gzip header */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200579 if (xz_head(state) == -1)
580 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800581 if (state->have) /* got some data from xz_head() */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200582 return 0;
583 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800584 if (state->how == COPY) { /* straight copy */
585 if (xz_load(state, state->out, state->size << 1, &(state->have)) ==
586 -1)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200587 return -1;
588 state->next = state->out;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800589 } else if (state->how == LZMA || state->how == GZIP) { /* decompress */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200590 strm->avail_out = state->size << 1;
591 strm->next_out = state->out;
592 if (xz_decomp(state) == -1)
593 return -1;
594 }
595 return 0;
596}
597
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800598static int
599xz_skip(xz_statep state, uint64_t len)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200600{
601 unsigned n;
602
603 /* skip over len bytes or reach end-of-file, whichever comes first */
604 while (len)
605 /* skip over whatever is in output buffer */
606 if (state->have) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800607 n = (uint64_t) state->have > len ?
608 (unsigned) len : state->have;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200609 state->have -= n;
610 state->next += n;
611 state->pos += n;
612 len -= n;
613 }
614
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800615 /* output buffer empty -- return if we're at the end of the input */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200616 else if (state->eof && state->strm.avail_in == 0)
617 break;
618
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800619 /* need more data to skip -- load up output buffer */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200620 else {
621 /* get more output, looking for header if required */
622 if (xz_make(state) == -1)
623 return -1;
624 }
625 return 0;
626}
627
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800628int
629__libxml2_xzread(xzFile file, void *buf, unsigned len)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200630{
631 unsigned got, n;
632 xz_statep state;
633 lzma_stream *strm;
634
635 /* get internal structure */
636 if (file == NULL)
637 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800638 state = (xz_statep) file;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200639 strm = &(state->strm);
640
641 /* check that we're reading and that there's no error */
642 if (state->err != LZMA_OK)
643 return -1;
644
645 /* since an int is returned, make sure len fits in one, otherwise return
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800646 * with an error (this avoids the flaw in the interface) */
647 if ((int) len < 0) {
648 xz_error(state, LZMA_BUF_ERROR,
649 "requested length does not fit in int");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200650 return -1;
651 }
652
653 /* if len is zero, avoid unnecessary operations */
654 if (len == 0)
655 return 0;
656
657 /* process a skip request */
658 if (state->seek) {
659 state->seek = 0;
660 if (xz_skip(state, state->skip) == -1)
661 return -1;
662 }
663
664 /* get len bytes to buf, or less than len if at the end */
665 got = 0;
666 do {
667 /* first just try copying data from the output buffer */
668 if (state->have) {
669 n = state->have > len ? len : state->have;
670 memcpy(buf, state->next, n);
671 state->next += n;
672 state->have -= n;
673 }
674
675 /* output buffer empty -- return if we're at the end of the input */
676 else if (state->eof && strm->avail_in == 0)
677 break;
678
679 /* need output data -- for small len or new stream load up our output
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800680 * buffer */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200681 else if (state->how == LOOK || len < (state->size << 1)) {
682 /* get more output, looking for header if required */
683 if (xz_make(state) == -1)
684 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800685 continue; /* no progress yet -- go back to memcpy() above */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200686 /* the copy above assures that we will leave with space in the
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800687 * output buffer, allowing at least one gzungetc() to succeed */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200688 }
689
690 /* large len -- read directly into user buffer */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800691 else if (state->how == COPY) { /* read directly */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200692 if (xz_load(state, buf, len, &n) == -1)
693 return -1;
694 }
695
696 /* large len -- decompress directly into user buffer */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800697 else { /* state->how == LZMA */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200698 strm->avail_out = len;
699 strm->next_out = buf;
700 if (xz_decomp(state) == -1)
701 return -1;
702 n = state->have;
703 state->have = 0;
704 }
705
706 /* update progress */
707 len -= n;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800708 buf = (char *) buf + n;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200709 got += n;
710 state->pos += n;
711 } while (len);
712
713 /* return number of bytes read into user buffer (will fit in int) */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800714 return (int) got;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200715}
716
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800717int
718__libxml2_xzclose(xzFile file)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200719{
720 int ret;
721 xz_statep state;
722
723 /* get internal structure */
724 if (file == NULL)
725 return LZMA_DATA_ERROR;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800726 state = (xz_statep) file;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200727
728 /* free memory and close file */
729 if (state->size) {
730 lzma_end(&(state->strm));
731#ifdef HAVE_LIBZ_H
732 inflateEnd(&(state->zstrm));
733#endif
734 free(state->out);
735 free(state->in);
736 }
737 free(state->path);
738 ret = close(state->fd);
739 free(state);
740 return ret ? ret : LZMA_OK;
741}
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800742#endif /* HAVE_LZMA_H */