blob: f1f50e569e70b46d661da71c435463efac795fac [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) {
Marcus Meissner99644922012-05-07 18:41:42 +0800232 /* avail_in is size_t, which is not necessary sizeof(unsigned) */
233 unsigned tmp = strm->avail_in;
234
235 if (xz_load(state, state->in, state->size, &tmp) == -1) {
236 strm->avail_in = tmp;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200237 return -1;
Marcus Meissner99644922012-05-07 18:41:42 +0800238 }
239 strm->avail_in = tmp;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200240 strm->next_in = state->in;
241 }
242 return 0;
243}
244
245static int
246is_format_xz(xz_statep state)
247{
248 lzma_stream *strm = &(state->strm);
249
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800250 return strm->avail_in >= 6 && memcmp(state->in, "\3757zXZ", 6) == 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200251}
252
253static int
254is_format_lzma(xz_statep state)
255{
256 lzma_stream *strm = &(state->strm);
257
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800258 lzma_filter filter;
259 lzma_options_lzma *opt;
260 uint32_t dict_size;
261 uint64_t uncompressed_size;
262 size_t i;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200263
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800264 if (strm->avail_in < 13)
265 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200266
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800267 filter.id = LZMA_FILTER_LZMA1;
268 if (lzma_properties_decode(&filter, NULL, state->in, 5) != LZMA_OK)
269 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200270
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800271 opt = filter.options;
272 dict_size = opt->dict_size;
273 free(opt);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200274
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800275 /* A hack to ditch tons of false positives: We allow only dictionary
276 * sizes that are 2^n or 2^n + 2^(n-1) or UINT32_MAX. LZMA_Alone
277 * created only files with 2^n, but accepts any dictionary size.
278 * If someone complains, this will be reconsidered.
279 */
280 if (dict_size != UINT32_MAX) {
281 uint32_t d = dict_size - 1;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200282
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800283 d |= d >> 2;
284 d |= d >> 3;
285 d |= d >> 4;
286 d |= d >> 8;
287 d |= d >> 16;
288 ++d;
289 if (d != dict_size || dict_size == 0)
290 return 0;
291 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200292
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800293 /* Another hack to ditch false positives: Assume that if the
294 * uncompressed size is known, it must be less than 256 GiB.
295 * Again, if someone complains, this will be reconsidered.
296 */
297 uncompressed_size = 0;
298 for (i = 0; i < 8; ++i)
299 uncompressed_size |= (uint64_t) (state->in[5 + i]) << (i * 8);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200300
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800301 if (uncompressed_size != UINT64_MAX
302 && uncompressed_size > (UINT64_C(1) << 38))
303 return 0;
304
305 return 1;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200306}
307
308#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800309
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200310/* Get next byte from input, or -1 if end or error. */
311#define NEXT() ((strm->avail_in == 0 && xz_avail(state) == -1) ? -1 : \
312 (strm->avail_in == 0 ? -1 : \
313 (strm->avail_in--, *(strm->next_in)++)))
314
315/* Get a four-byte little-endian integer and return 0 on success and the value
316 in *ret. Otherwise -1 is returned and *ret is not modified. */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800317static int
318gz_next4(xz_statep state, unsigned long *ret)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200319{
320 int ch;
321 unsigned long val;
322 z_streamp strm = &(state->zstrm);
323
324 val = NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800325 val += (unsigned) NEXT() << 8;
326 val += (unsigned long) NEXT() << 16;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200327 ch = NEXT();
328 if (ch == -1)
329 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800330 val += (unsigned long) ch << 24;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200331 *ret = val;
332 return 0;
333}
334#endif
335
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800336static int
337xz_head(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200338{
339 lzma_stream *strm = &(state->strm);
340 lzma_stream init = LZMA_STREAM_INIT;
341 int flags;
342 unsigned len;
343
344 /* allocate read buffers and inflate memory */
345 if (state->size == 0) {
346 /* allocate buffers */
347 state->in = malloc(state->want);
348 state->out = malloc(state->want << 1);
349 if (state->in == NULL || state->out == NULL) {
350 if (state->out != NULL)
351 free(state->out);
352 if (state->in != NULL)
353 free(state->in);
354 xz_error(state, LZMA_MEM_ERROR, "out of memory");
355 return -1;
356 }
357 state->size = state->want;
358
359 /* allocate decoder memory */
360 state->strm = init;
361 state->strm.avail_in = 0;
362 state->strm.next_in = NULL;
363 if (lzma_auto_decoder(&state->strm, UINT64_MAX, 0) != LZMA_OK) {
364 free(state->out);
365 free(state->in);
366 state->size = 0;
367 xz_error(state, LZMA_MEM_ERROR, "out of memory");
368 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800369 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200370#ifdef HAVE_ZLIB_H
371 /* allocate inflate memory */
372 state->zstrm.zalloc = Z_NULL;
373 state->zstrm.zfree = Z_NULL;
374 state->zstrm.opaque = Z_NULL;
375 state->zstrm.avail_in = 0;
376 state->zstrm.next_in = Z_NULL;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800377 if (inflateInit2(&(state->zstrm), -15) != Z_OK) { /* raw inflate */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200378 free(state->out);
379 free(state->in);
380 state->size = 0;
381 xz_error(state, LZMA_MEM_ERROR, "out of memory");
382 return -1;
383 }
384#endif
385 }
386
387 /* get some data in the input buffer */
388 if (strm->avail_in == 0) {
389 if (xz_avail(state) == -1)
390 return -1;
391 if (strm->avail_in == 0)
392 return 0;
393 }
394
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800395 /* look for the xz magic header bytes */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200396 if (is_format_xz(state) || is_format_lzma(state)) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800397 state->how = LZMA;
398 state->direct = 0;
399 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200400 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200401#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800402 /* look for the gzip magic header bytes 31 and 139 */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200403 if (strm->next_in[0] == 31) {
404 strm->avail_in--;
405 strm->next_in++;
406 if (strm->avail_in == 0 && xz_avail(state) == -1)
407 return -1;
408 if (strm->avail_in && strm->next_in[0] == 139) {
409 /* we have a gzip header, woo hoo! */
410 strm->avail_in--;
411 strm->next_in++;
412
413 /* skip rest of header */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800414 if (NEXT() != 8) { /* compression method */
415 xz_error(state, LZMA_DATA_ERROR,
416 "unknown compression method");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200417 return -1;
418 }
419 flags = NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800420 if (flags & 0xe0) { /* reserved flag bits */
421 xz_error(state, LZMA_DATA_ERROR,
422 "unknown header flags set");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200423 return -1;
424 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800425 NEXT(); /* modification time */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200426 NEXT();
427 NEXT();
428 NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800429 NEXT(); /* extra flags */
430 NEXT(); /* operating system */
431 if (flags & 4) { /* extra field */
432 len = (unsigned) NEXT();
433 len += (unsigned) NEXT() << 8;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200434 while (len--)
435 if (NEXT() < 0)
436 break;
437 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800438 if (flags & 8) /* file name */
439 while (NEXT() > 0) ;
440 if (flags & 16) /* comment */
441 while (NEXT() > 0) ;
442 if (flags & 2) { /* header crc */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200443 NEXT();
444 NEXT();
445 }
446 /* an unexpected end of file is not checked for here -- it will be
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800447 * noticed on the first request for uncompressed data */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200448
449 /* set up for decompression */
450 inflateReset(&state->zstrm);
451 state->zstrm.adler = crc32(0L, Z_NULL, 0);
452 state->how = GZIP;
453 state->direct = 0;
454 return 0;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800455 } else {
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200456 /* not a gzip file -- save first byte (31) and fall to raw i/o */
457 state->out[0] = 31;
458 state->have = 1;
459 }
460 }
461#endif
462
463 /* doing raw i/o, save start of raw data for seeking, copy any leftover
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800464 * input to output -- this assumes that the output buffer is larger than
465 * the input buffer, which also assures space for gzungetc() */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200466 state->raw = state->pos;
467 state->next = state->out;
468 if (strm->avail_in) {
469 memcpy(state->next + state->have, strm->next_in, strm->avail_in);
470 state->have += strm->avail_in;
471 strm->avail_in = 0;
472 }
473 state->how = COPY;
474 state->direct = 1;
475 return 0;
476}
477
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800478static int
479xz_decomp(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200480{
481 int ret;
482 unsigned had;
483 unsigned long crc, len;
484 lzma_stream *strm = &(state->strm);
485
486 lzma_action action = LZMA_RUN;
487
488 /* fill output buffer up to end of deflate stream */
489 had = strm->avail_out;
490 do {
491 /* get more input for inflate() */
492 if (strm->avail_in == 0 && xz_avail(state) == -1)
493 return -1;
494 if (strm->avail_in == 0) {
495 xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
496 return -1;
497 }
498 if (state->eof)
499 action = LZMA_FINISH;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800500
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200501 /* decompress and handle errors */
502#ifdef HAVE_ZLIB_H
503 if (state->how == GZIP) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800504 state->zstrm.avail_in = (uInt) state->strm.avail_in;
505 state->zstrm.next_in = (Bytef *) state->strm.next_in;
506 state->zstrm.avail_out = (uInt) state->strm.avail_out;
507 state->zstrm.next_out = (Bytef *) state->strm.next_out;
508 ret = inflate(&state->zstrm, Z_NO_FLUSH);
509 if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
510 xz_error(state, Z_STREAM_ERROR,
511 "internal error: inflate stream corrupt");
512 return -1;
513 }
514 if (ret == Z_MEM_ERROR)
515 ret = LZMA_MEM_ERROR;
516 if (ret == Z_DATA_ERROR)
517 ret = LZMA_DATA_ERROR;
518 if (ret == Z_STREAM_END)
519 ret = LZMA_STREAM_END;
520 state->strm.avail_in = state->zstrm.avail_in;
521 state->strm.next_in = state->zstrm.next_in;
522 state->strm.avail_out = state->zstrm.avail_out;
523 state->strm.next_out = state->zstrm.next_out;
524 } else /* state->how == LZMA */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200525#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800526 ret = lzma_code(strm, action);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200527 if (ret == LZMA_MEM_ERROR) {
528 xz_error(state, LZMA_MEM_ERROR, "out of memory");
529 return -1;
530 }
531 if (ret == LZMA_DATA_ERROR) {
532 xz_error(state, LZMA_DATA_ERROR, "compressed data error");
533 return -1;
534 }
535 } while (strm->avail_out && ret != LZMA_STREAM_END);
536
537 /* update available output and crc check value */
538 state->have = had - strm->avail_out;
539 state->next = strm->next_out - state->have;
540#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800541 state->zstrm.adler =
542 crc32(state->zstrm.adler, state->next, state->have);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200543#endif
544
545 if (ret == LZMA_STREAM_END) {
546#ifdef HAVE_ZLIB_H
547 if (state->how == GZIP) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800548 if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) {
549 xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
550 return -1;
551 }
552 if (crc != state->zstrm.adler) {
553 xz_error(state, LZMA_DATA_ERROR, "incorrect data check");
554 return -1;
555 }
556 if (len != (state->zstrm.total_out & 0xffffffffL)) {
557 xz_error(state, LZMA_DATA_ERROR, "incorrect length check");
558 return -1;
559 }
560 state->strm.avail_in = 0;
561 state->strm.next_in = NULL;
562 state->strm.avail_out = 0;
563 state->strm.next_out = NULL;
564 } else
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200565#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800566 if (strm->avail_in != 0 || !state->eof) {
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200567 xz_error(state, LZMA_DATA_ERROR, "trailing garbage");
568 return -1;
569 }
570 state->how = LOOK; /* ready for next stream, once have is 0 (leave
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800571 * state->direct unchanged to remember how) */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200572 }
573
574 /* good decompression */
575 return 0;
576}
577
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800578static int
579xz_make(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200580{
581 lzma_stream *strm = &(state->strm);
582
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800583 if (state->how == LOOK) { /* look for lzma / gzip header */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200584 if (xz_head(state) == -1)
585 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800586 if (state->have) /* got some data from xz_head() */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200587 return 0;
588 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800589 if (state->how == COPY) { /* straight copy */
590 if (xz_load(state, state->out, state->size << 1, &(state->have)) ==
591 -1)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200592 return -1;
593 state->next = state->out;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800594 } else if (state->how == LZMA || state->how == GZIP) { /* decompress */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200595 strm->avail_out = state->size << 1;
596 strm->next_out = state->out;
597 if (xz_decomp(state) == -1)
598 return -1;
599 }
600 return 0;
601}
602
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800603static int
604xz_skip(xz_statep state, uint64_t len)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200605{
606 unsigned n;
607
608 /* skip over len bytes or reach end-of-file, whichever comes first */
609 while (len)
610 /* skip over whatever is in output buffer */
611 if (state->have) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800612 n = (uint64_t) state->have > len ?
613 (unsigned) len : state->have;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200614 state->have -= n;
615 state->next += n;
616 state->pos += n;
617 len -= n;
618 }
619
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800620 /* output buffer empty -- return if we're at the end of the input */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200621 else if (state->eof && state->strm.avail_in == 0)
622 break;
623
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800624 /* need more data to skip -- load up output buffer */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200625 else {
626 /* get more output, looking for header if required */
627 if (xz_make(state) == -1)
628 return -1;
629 }
630 return 0;
631}
632
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800633int
634__libxml2_xzread(xzFile file, void *buf, unsigned len)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200635{
636 unsigned got, n;
637 xz_statep state;
638 lzma_stream *strm;
639
640 /* get internal structure */
641 if (file == NULL)
642 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800643 state = (xz_statep) file;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200644 strm = &(state->strm);
645
646 /* check that we're reading and that there's no error */
647 if (state->err != LZMA_OK)
648 return -1;
649
650 /* since an int is returned, make sure len fits in one, otherwise return
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800651 * with an error (this avoids the flaw in the interface) */
652 if ((int) len < 0) {
653 xz_error(state, LZMA_BUF_ERROR,
654 "requested length does not fit in int");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200655 return -1;
656 }
657
658 /* if len is zero, avoid unnecessary operations */
659 if (len == 0)
660 return 0;
661
662 /* process a skip request */
663 if (state->seek) {
664 state->seek = 0;
665 if (xz_skip(state, state->skip) == -1)
666 return -1;
667 }
668
669 /* get len bytes to buf, or less than len if at the end */
670 got = 0;
671 do {
672 /* first just try copying data from the output buffer */
673 if (state->have) {
674 n = state->have > len ? len : state->have;
675 memcpy(buf, state->next, n);
676 state->next += n;
677 state->have -= n;
678 }
679
680 /* output buffer empty -- return if we're at the end of the input */
681 else if (state->eof && strm->avail_in == 0)
682 break;
683
684 /* need output data -- for small len or new stream load up our output
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800685 * buffer */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200686 else if (state->how == LOOK || len < (state->size << 1)) {
687 /* get more output, looking for header if required */
688 if (xz_make(state) == -1)
689 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800690 continue; /* no progress yet -- go back to memcpy() above */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200691 /* the copy above assures that we will leave with space in the
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800692 * output buffer, allowing at least one gzungetc() to succeed */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200693 }
694
695 /* large len -- read directly into user buffer */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800696 else if (state->how == COPY) { /* read directly */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200697 if (xz_load(state, buf, len, &n) == -1)
698 return -1;
699 }
700
701 /* large len -- decompress directly into user buffer */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800702 else { /* state->how == LZMA */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200703 strm->avail_out = len;
704 strm->next_out = buf;
705 if (xz_decomp(state) == -1)
706 return -1;
707 n = state->have;
708 state->have = 0;
709 }
710
711 /* update progress */
712 len -= n;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800713 buf = (char *) buf + n;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200714 got += n;
715 state->pos += n;
716 } while (len);
717
718 /* return number of bytes read into user buffer (will fit in int) */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800719 return (int) got;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200720}
721
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800722int
723__libxml2_xzclose(xzFile file)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200724{
725 int ret;
726 xz_statep state;
727
728 /* get internal structure */
729 if (file == NULL)
730 return LZMA_DATA_ERROR;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800731 state = (xz_statep) file;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200732
733 /* free memory and close file */
734 if (state->size) {
735 lzma_end(&(state->strm));
736#ifdef HAVE_LIBZ_H
737 inflateEnd(&(state->zstrm));
738#endif
739 free(state->out);
740 free(state->in);
741 }
742 free(state->path);
743 ret = close(state->fd);
744 free(state);
745 return ret ? ret : LZMA_OK;
746}
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800747#endif /* HAVE_LZMA_H */