blob: 192b164d5c46a1fd30cc49eca7fd4e209f961938 [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 */
53 unsigned size; /* buffer size, zero if not allocated yet */
54 unsigned want; /* requested buffer size, default is BUFSIZ */
55 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 */
58 unsigned have; /* amount of output data unused at next */
59 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
203xz_load(xz_statep state, unsigned char *buf, unsigned len, unsigned *have)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200204{
205 int ret;
206
207 *have = 0;
208 do {
209 ret = read(state->fd, buf + *have, len - *have);
210 if (ret <= 0)
211 break;
212 *have += ret;
213 } while (*have < len);
214 if (ret < 0) {
215 xz_error(state, -1, strerror(errno));
216 return -1;
217 }
218 if (ret == 0)
219 state->eof = 1;
220 return 0;
221}
222
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800223static int
224xz_avail(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200225{
226 lzma_stream *strm = &(state->strm);
227
228 if (state->err != LZMA_OK)
229 return -1;
230 if (state->eof == 0) {
231 if (xz_load(state, state->in, state->size,
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800232 (unsigned *) &(strm->avail_in)) == -1)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200233 return -1;
234 strm->next_in = state->in;
235 }
236 return 0;
237}
238
239static int
240is_format_xz(xz_statep state)
241{
242 lzma_stream *strm = &(state->strm);
243
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800244 return strm->avail_in >= 6 && memcmp(state->in, "\3757zXZ", 6) == 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200245}
246
247static int
248is_format_lzma(xz_statep state)
249{
250 lzma_stream *strm = &(state->strm);
251
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800252 lzma_filter filter;
253 lzma_options_lzma *opt;
254 uint32_t dict_size;
255 uint64_t uncompressed_size;
256 size_t i;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200257
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800258 if (strm->avail_in < 13)
259 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200260
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800261 filter.id = LZMA_FILTER_LZMA1;
262 if (lzma_properties_decode(&filter, NULL, state->in, 5) != LZMA_OK)
263 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200264
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800265 opt = filter.options;
266 dict_size = opt->dict_size;
267 free(opt);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200268
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800269 /* A hack to ditch tons of false positives: We allow only dictionary
270 * sizes that are 2^n or 2^n + 2^(n-1) or UINT32_MAX. LZMA_Alone
271 * created only files with 2^n, but accepts any dictionary size.
272 * If someone complains, this will be reconsidered.
273 */
274 if (dict_size != UINT32_MAX) {
275 uint32_t d = dict_size - 1;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200276
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800277 d |= d >> 2;
278 d |= d >> 3;
279 d |= d >> 4;
280 d |= d >> 8;
281 d |= d >> 16;
282 ++d;
283 if (d != dict_size || dict_size == 0)
284 return 0;
285 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200286
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800287 /* Another hack to ditch false positives: Assume that if the
288 * uncompressed size is known, it must be less than 256 GiB.
289 * Again, if someone complains, this will be reconsidered.
290 */
291 uncompressed_size = 0;
292 for (i = 0; i < 8; ++i)
293 uncompressed_size |= (uint64_t) (state->in[5 + i]) << (i * 8);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200294
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800295 if (uncompressed_size != UINT64_MAX
296 && uncompressed_size > (UINT64_C(1) << 38))
297 return 0;
298
299 return 1;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200300}
301
302#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800303
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200304/* Get next byte from input, or -1 if end or error. */
305#define NEXT() ((strm->avail_in == 0 && xz_avail(state) == -1) ? -1 : \
306 (strm->avail_in == 0 ? -1 : \
307 (strm->avail_in--, *(strm->next_in)++)))
308
309/* Get a four-byte little-endian integer and return 0 on success and the value
310 in *ret. Otherwise -1 is returned and *ret is not modified. */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800311static int
312gz_next4(xz_statep state, unsigned long *ret)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200313{
314 int ch;
315 unsigned long val;
316 z_streamp strm = &(state->zstrm);
317
318 val = NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800319 val += (unsigned) NEXT() << 8;
320 val += (unsigned long) NEXT() << 16;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200321 ch = NEXT();
322 if (ch == -1)
323 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800324 val += (unsigned long) ch << 24;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200325 *ret = val;
326 return 0;
327}
328#endif
329
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800330static int
331xz_head(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200332{
333 lzma_stream *strm = &(state->strm);
334 lzma_stream init = LZMA_STREAM_INIT;
335 int flags;
336 unsigned len;
337
338 /* allocate read buffers and inflate memory */
339 if (state->size == 0) {
340 /* allocate buffers */
341 state->in = malloc(state->want);
342 state->out = malloc(state->want << 1);
343 if (state->in == NULL || state->out == NULL) {
344 if (state->out != NULL)
345 free(state->out);
346 if (state->in != NULL)
347 free(state->in);
348 xz_error(state, LZMA_MEM_ERROR, "out of memory");
349 return -1;
350 }
351 state->size = state->want;
352
353 /* allocate decoder memory */
354 state->strm = init;
355 state->strm.avail_in = 0;
356 state->strm.next_in = NULL;
357 if (lzma_auto_decoder(&state->strm, UINT64_MAX, 0) != LZMA_OK) {
358 free(state->out);
359 free(state->in);
360 state->size = 0;
361 xz_error(state, LZMA_MEM_ERROR, "out of memory");
362 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800363 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200364#ifdef HAVE_ZLIB_H
365 /* allocate inflate memory */
366 state->zstrm.zalloc = Z_NULL;
367 state->zstrm.zfree = Z_NULL;
368 state->zstrm.opaque = Z_NULL;
369 state->zstrm.avail_in = 0;
370 state->zstrm.next_in = Z_NULL;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800371 if (inflateInit2(&(state->zstrm), -15) != Z_OK) { /* raw inflate */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200372 free(state->out);
373 free(state->in);
374 state->size = 0;
375 xz_error(state, LZMA_MEM_ERROR, "out of memory");
376 return -1;
377 }
378#endif
379 }
380
381 /* get some data in the input buffer */
382 if (strm->avail_in == 0) {
383 if (xz_avail(state) == -1)
384 return -1;
385 if (strm->avail_in == 0)
386 return 0;
387 }
388
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800389 /* look for the xz magic header bytes */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200390 if (is_format_xz(state) || is_format_lzma(state)) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800391 state->how = LZMA;
392 state->direct = 0;
393 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200394 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200395#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800396 /* look for the gzip magic header bytes 31 and 139 */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200397 if (strm->next_in[0] == 31) {
398 strm->avail_in--;
399 strm->next_in++;
400 if (strm->avail_in == 0 && xz_avail(state) == -1)
401 return -1;
402 if (strm->avail_in && strm->next_in[0] == 139) {
403 /* we have a gzip header, woo hoo! */
404 strm->avail_in--;
405 strm->next_in++;
406
407 /* skip rest of header */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800408 if (NEXT() != 8) { /* compression method */
409 xz_error(state, LZMA_DATA_ERROR,
410 "unknown compression method");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200411 return -1;
412 }
413 flags = NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800414 if (flags & 0xe0) { /* reserved flag bits */
415 xz_error(state, LZMA_DATA_ERROR,
416 "unknown header flags set");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200417 return -1;
418 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800419 NEXT(); /* modification time */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200420 NEXT();
421 NEXT();
422 NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800423 NEXT(); /* extra flags */
424 NEXT(); /* operating system */
425 if (flags & 4) { /* extra field */
426 len = (unsigned) NEXT();
427 len += (unsigned) NEXT() << 8;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200428 while (len--)
429 if (NEXT() < 0)
430 break;
431 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800432 if (flags & 8) /* file name */
433 while (NEXT() > 0) ;
434 if (flags & 16) /* comment */
435 while (NEXT() > 0) ;
436 if (flags & 2) { /* header crc */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200437 NEXT();
438 NEXT();
439 }
440 /* an unexpected end of file is not checked for here -- it will be
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800441 * noticed on the first request for uncompressed data */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200442
443 /* set up for decompression */
444 inflateReset(&state->zstrm);
445 state->zstrm.adler = crc32(0L, Z_NULL, 0);
446 state->how = GZIP;
447 state->direct = 0;
448 return 0;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800449 } else {
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200450 /* not a gzip file -- save first byte (31) and fall to raw i/o */
451 state->out[0] = 31;
452 state->have = 1;
453 }
454 }
455#endif
456
457 /* doing raw i/o, save start of raw data for seeking, copy any leftover
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800458 * input to output -- this assumes that the output buffer is larger than
459 * the input buffer, which also assures space for gzungetc() */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200460 state->raw = state->pos;
461 state->next = state->out;
462 if (strm->avail_in) {
463 memcpy(state->next + state->have, strm->next_in, strm->avail_in);
464 state->have += strm->avail_in;
465 strm->avail_in = 0;
466 }
467 state->how = COPY;
468 state->direct = 1;
469 return 0;
470}
471
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800472static int
473xz_decomp(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200474{
475 int ret;
476 unsigned had;
477 unsigned long crc, len;
478 lzma_stream *strm = &(state->strm);
479
480 lzma_action action = LZMA_RUN;
481
482 /* fill output buffer up to end of deflate stream */
483 had = strm->avail_out;
484 do {
485 /* get more input for inflate() */
486 if (strm->avail_in == 0 && xz_avail(state) == -1)
487 return -1;
488 if (strm->avail_in == 0) {
489 xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
490 return -1;
491 }
492 if (state->eof)
493 action = LZMA_FINISH;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800494
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200495 /* decompress and handle errors */
496#ifdef HAVE_ZLIB_H
497 if (state->how == GZIP) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800498 state->zstrm.avail_in = (uInt) state->strm.avail_in;
499 state->zstrm.next_in = (Bytef *) state->strm.next_in;
500 state->zstrm.avail_out = (uInt) state->strm.avail_out;
501 state->zstrm.next_out = (Bytef *) state->strm.next_out;
502 ret = inflate(&state->zstrm, Z_NO_FLUSH);
503 if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
504 xz_error(state, Z_STREAM_ERROR,
505 "internal error: inflate stream corrupt");
506 return -1;
507 }
508 if (ret == Z_MEM_ERROR)
509 ret = LZMA_MEM_ERROR;
510 if (ret == Z_DATA_ERROR)
511 ret = LZMA_DATA_ERROR;
512 if (ret == Z_STREAM_END)
513 ret = LZMA_STREAM_END;
514 state->strm.avail_in = state->zstrm.avail_in;
515 state->strm.next_in = state->zstrm.next_in;
516 state->strm.avail_out = state->zstrm.avail_out;
517 state->strm.next_out = state->zstrm.next_out;
518 } else /* state->how == LZMA */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200519#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800520 ret = lzma_code(strm, action);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200521 if (ret == LZMA_MEM_ERROR) {
522 xz_error(state, LZMA_MEM_ERROR, "out of memory");
523 return -1;
524 }
525 if (ret == LZMA_DATA_ERROR) {
526 xz_error(state, LZMA_DATA_ERROR, "compressed data error");
527 return -1;
528 }
529 } while (strm->avail_out && ret != LZMA_STREAM_END);
530
531 /* update available output and crc check value */
532 state->have = had - strm->avail_out;
533 state->next = strm->next_out - state->have;
534#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800535 state->zstrm.adler =
536 crc32(state->zstrm.adler, state->next, state->have);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200537#endif
538
539 if (ret == LZMA_STREAM_END) {
540#ifdef HAVE_ZLIB_H
541 if (state->how == GZIP) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800542 if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) {
543 xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
544 return -1;
545 }
546 if (crc != state->zstrm.adler) {
547 xz_error(state, LZMA_DATA_ERROR, "incorrect data check");
548 return -1;
549 }
550 if (len != (state->zstrm.total_out & 0xffffffffL)) {
551 xz_error(state, LZMA_DATA_ERROR, "incorrect length check");
552 return -1;
553 }
554 state->strm.avail_in = 0;
555 state->strm.next_in = NULL;
556 state->strm.avail_out = 0;
557 state->strm.next_out = NULL;
558 } else
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200559#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800560 if (strm->avail_in != 0 || !state->eof) {
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200561 xz_error(state, LZMA_DATA_ERROR, "trailing garbage");
562 return -1;
563 }
564 state->how = LOOK; /* ready for next stream, once have is 0 (leave
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800565 * state->direct unchanged to remember how) */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200566 }
567
568 /* good decompression */
569 return 0;
570}
571
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800572static int
573xz_make(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200574{
575 lzma_stream *strm = &(state->strm);
576
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800577 if (state->how == LOOK) { /* look for lzma / gzip header */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200578 if (xz_head(state) == -1)
579 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800580 if (state->have) /* got some data from xz_head() */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200581 return 0;
582 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800583 if (state->how == COPY) { /* straight copy */
584 if (xz_load(state, state->out, state->size << 1, &(state->have)) ==
585 -1)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200586 return -1;
587 state->next = state->out;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800588 } else if (state->how == LZMA || state->how == GZIP) { /* decompress */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200589 strm->avail_out = state->size << 1;
590 strm->next_out = state->out;
591 if (xz_decomp(state) == -1)
592 return -1;
593 }
594 return 0;
595}
596
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800597static int
598xz_skip(xz_statep state, uint64_t len)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200599{
600 unsigned n;
601
602 /* skip over len bytes or reach end-of-file, whichever comes first */
603 while (len)
604 /* skip over whatever is in output buffer */
605 if (state->have) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800606 n = (uint64_t) state->have > len ?
607 (unsigned) len : state->have;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200608 state->have -= n;
609 state->next += n;
610 state->pos += n;
611 len -= n;
612 }
613
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800614 /* output buffer empty -- return if we're at the end of the input */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200615 else if (state->eof && state->strm.avail_in == 0)
616 break;
617
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800618 /* need more data to skip -- load up output buffer */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200619 else {
620 /* get more output, looking for header if required */
621 if (xz_make(state) == -1)
622 return -1;
623 }
624 return 0;
625}
626
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800627int
628__libxml2_xzread(xzFile file, void *buf, unsigned len)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200629{
630 unsigned got, n;
631 xz_statep state;
632 lzma_stream *strm;
633
634 /* get internal structure */
635 if (file == NULL)
636 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800637 state = (xz_statep) file;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200638 strm = &(state->strm);
639
640 /* check that we're reading and that there's no error */
641 if (state->err != LZMA_OK)
642 return -1;
643
644 /* since an int is returned, make sure len fits in one, otherwise return
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800645 * with an error (this avoids the flaw in the interface) */
646 if ((int) len < 0) {
647 xz_error(state, LZMA_BUF_ERROR,
648 "requested length does not fit in int");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200649 return -1;
650 }
651
652 /* if len is zero, avoid unnecessary operations */
653 if (len == 0)
654 return 0;
655
656 /* process a skip request */
657 if (state->seek) {
658 state->seek = 0;
659 if (xz_skip(state, state->skip) == -1)
660 return -1;
661 }
662
663 /* get len bytes to buf, or less than len if at the end */
664 got = 0;
665 do {
666 /* first just try copying data from the output buffer */
667 if (state->have) {
668 n = state->have > len ? len : state->have;
669 memcpy(buf, state->next, n);
670 state->next += n;
671 state->have -= n;
672 }
673
674 /* output buffer empty -- return if we're at the end of the input */
675 else if (state->eof && strm->avail_in == 0)
676 break;
677
678 /* need output data -- for small len or new stream load up our output
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800679 * buffer */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200680 else if (state->how == LOOK || len < (state->size << 1)) {
681 /* get more output, looking for header if required */
682 if (xz_make(state) == -1)
683 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800684 continue; /* no progress yet -- go back to memcpy() above */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200685 /* the copy above assures that we will leave with space in the
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800686 * output buffer, allowing at least one gzungetc() to succeed */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200687 }
688
689 /* large len -- read directly into user buffer */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800690 else if (state->how == COPY) { /* read directly */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200691 if (xz_load(state, buf, len, &n) == -1)
692 return -1;
693 }
694
695 /* large len -- decompress directly into user buffer */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800696 else { /* state->how == LZMA */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200697 strm->avail_out = len;
698 strm->next_out = buf;
699 if (xz_decomp(state) == -1)
700 return -1;
701 n = state->have;
702 state->have = 0;
703 }
704
705 /* update progress */
706 len -= n;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800707 buf = (char *) buf + n;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200708 got += n;
709 state->pos += n;
710 } while (len);
711
712 /* return number of bytes read into user buffer (will fit in int) */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800713 return (int) got;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200714}
715
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800716int
717__libxml2_xzclose(xzFile file)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200718{
719 int ret;
720 xz_statep state;
721
722 /* get internal structure */
723 if (file == NULL)
724 return LZMA_DATA_ERROR;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800725 state = (xz_statep) file;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200726
727 /* free memory and close file */
728 if (state->size) {
729 lzma_end(&(state->strm));
730#ifdef HAVE_LIBZ_H
731 inflateEnd(&(state->zstrm));
732#endif
733 free(state->out);
734 free(state->in);
735 }
736 free(state->path);
737 ret = close(state->fd);
738 free(state);
739 return ret ? ret : LZMA_OK;
740}
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800741#endif /* HAVE_LZMA_H */