blob: 8bafbd3ac7457e332c6bc16ef694b13acae1c7a7 [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"
Daniel Veillard18b89882015-11-03 15:46:29 +080011#ifdef LIBXML_LZMA_ENABLED
Daniel Veillardadf5ec92012-01-26 16:56:22 +080012
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
Daniel Veillard18b89882015-11-03 15:46:29 +080037#ifdef HAVE_LZMA_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +080038#include <lzma.h>
Daniel Veillard18b89882015-11-03 15:46:29 +080039#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +080040
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020041#include "xzlib.h"
Daniel Veillard9f3cdef2012-05-15 09:38:13 +080042#include <libxml/xmlmemory.h>
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020043
44/* values for xz_state how */
Daniel Veillardadf5ec92012-01-26 16:56:22 +080045#define LOOK 0 /* look for a gzip/lzma header */
46#define COPY 1 /* copy input directly */
47#define GZIP 2 /* decompress a gzip stream */
48#define LZMA 3 /* decompress a lzma stream */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020049
50/* internal lzma file state data structure */
51typedef struct {
Daniel Veillardadf5ec92012-01-26 16:56:22 +080052 int mode; /* see lzma modes above */
53 int fd; /* file descriptor */
54 char *path; /* path or fd for error messages */
55 uint64_t pos; /* current position in uncompressed data */
Daniel Veillard72789ef2012-04-02 17:52:20 +080056 unsigned int size; /* buffer size, zero if not allocated yet */
57 unsigned int want; /* requested buffer size, default is BUFSIZ */
Daniel Veillardadf5ec92012-01-26 16:56:22 +080058 unsigned char *in; /* input buffer */
59 unsigned char *out; /* output buffer (double-sized when reading) */
60 unsigned char *next; /* next output data to deliver or write */
Daniel Veillard72789ef2012-04-02 17:52:20 +080061 unsigned int have; /* amount of output data unused at next */
Daniel Veillardadf5ec92012-01-26 16:56:22 +080062 int eof; /* true if end of input file reached */
63 uint64_t start; /* where the lzma data started, for rewinding */
64 uint64_t raw; /* where the raw data started, for seeking */
65 int how; /* 0: get header, 1: copy, 2: decompress */
66 int direct; /* true if last read direct, false if lzma */
67 /* seek request */
68 uint64_t skip; /* amount to skip (already rewound if backwards) */
69 int seek; /* true if seek request pending */
70 /* error information */
71 int err; /* error code */
72 char *msg; /* error message */
73 /* lzma stream */
Daniel Veillard9f3cdef2012-05-15 09:38:13 +080074 int init; /* is the iniflate stream initialized */
Daniel Veillardadf5ec92012-01-26 16:56:22 +080075 lzma_stream strm; /* stream structure in-place (not a pointer) */
76 char padding1[32]; /* padding allowing to cope with possible
77 extensions of above structure without
78 too much side effect */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020079#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +080080 /* zlib inflate or deflate stream */
81 z_stream zstrm; /* stream structure in-place (not a pointer) */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020082#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +080083 char padding2[32]; /* padding allowing to cope with possible
84 extensions of above structure without
85 too much side effect */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020086} xz_state, *xz_statep;
87
Daniel Veillardadf5ec92012-01-26 16:56:22 +080088static void
89xz_error(xz_statep state, int err, const char *msg)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020090{
91 /* free previously allocated message and clear */
92 if (state->msg != NULL) {
93 if (state->err != LZMA_MEM_ERROR)
Daniel Veillard9f3cdef2012-05-15 09:38:13 +080094 xmlFree(state->msg);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020095 state->msg = NULL;
96 }
97
98 /* set error code, and if no message, then done */
99 state->err = err;
100 if (msg == NULL)
101 return;
102
103 /* for an out of memory error, save as static string */
104 if (err == LZMA_MEM_ERROR) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800105 state->msg = (char *) msg;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200106 return;
107 }
108
109 /* construct error message with path */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800110 if ((state->msg =
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800111 xmlMalloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200112 state->err = LZMA_MEM_ERROR;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800113 state->msg = (char *) "out of memory";
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200114 return;
115 }
116 strcpy(state->msg, state->path);
117 strcat(state->msg, ": ");
118 strcat(state->msg, msg);
119 return;
120}
121
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800122static void
123xz_reset(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200124{
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800125 state->have = 0; /* no output data available */
126 state->eof = 0; /* not at end of file */
127 state->how = LOOK; /* look for gzip header */
128 state->direct = 1; /* default for empty file */
129 state->seek = 0; /* no seek request pending */
130 xz_error(state, LZMA_OK, NULL); /* clear error */
131 state->pos = 0; /* no uncompressed data yet */
132 state->strm.avail_in = 0; /* no input data yet */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200133#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800134 state->zstrm.avail_in = 0; /* no input data yet */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200135#endif
136}
137
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800138static xzFile
139xz_open(const char *path, int fd, const char *mode ATTRIBUTE_UNUSED)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200140{
141 xz_statep state;
142
143 /* allocate xzFile structure to return */
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800144 state = xmlMalloc(sizeof(xz_state));
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200145 if (state == NULL)
146 return NULL;
147 state->size = 0; /* no buffers allocated yet */
148 state->want = BUFSIZ; /* requested buffer size */
149 state->msg = NULL; /* no error message yet */
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800150 state->init = 0; /* initialization of zlib data */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200151
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800152 /* save the path name for error messages */
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800153 state->path = xmlMalloc(strlen(path) + 1);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200154 if (state->path == NULL) {
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800155 xmlFree(state);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200156 return NULL;
157 }
158 strcpy(state->path, path);
159
160 /* open the file with the appropriate mode (or just use fd) */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800161 state->fd = fd != -1 ? fd : open(path,
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200162#ifdef O_LARGEFILE
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800163 O_LARGEFILE |
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200164#endif
165#ifdef O_BINARY
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800166 O_BINARY |
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200167#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800168 O_RDONLY, 0666);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200169 if (state->fd == -1) {
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800170 xmlFree(state->path);
171 xmlFree(state);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200172 return NULL;
173 }
174
175 /* save the current position for rewinding (only if reading) */
176 state->start = lseek(state->fd, 0, SEEK_CUR);
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800177 if (state->start == (uint64_t) - 1)
178 state->start = 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200179
180 /* initialize stream */
181 xz_reset(state);
182
183 /* return stream */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800184 return (xzFile) state;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200185}
186
Daniel Veillard63588f42013-05-10 14:01:46 +0800187static int
188xz_compressed(xzFile f) {
189 xz_statep state;
190
191 if (f == NULL)
192 return(-1);
193 state = (xz_statep) f;
194 if (state->init <= 0)
195 return(-1);
196
197 switch (state->how) {
198 case COPY:
199 return(0);
200 case GZIP:
201 case LZMA:
202 return(1);
203 }
204 return(-1);
205}
206
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800207xzFile
208__libxml2_xzopen(const char *path, const char *mode)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200209{
210 return xz_open(path, -1, mode);
211}
212
Daniel Veillard63588f42013-05-10 14:01:46 +0800213int
214__libxml2_xzcompressed(xzFile f) {
215 return xz_compressed(f);
216}
217
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800218xzFile
219__libxml2_xzdopen(int fd, const char *mode)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200220{
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800221 char *path; /* identifier for error messages */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200222 xzFile xz;
223
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800224 if (fd == -1 || (path = xmlMalloc(7 + 3 * sizeof(int))) == NULL)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200225 return NULL;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800226 sprintf(path, "<fd:%d>", fd); /* for debugging */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200227 xz = xz_open(path, fd, mode);
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800228 xmlFree(path);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200229 return xz;
230}
231
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800232static int
Daniel Veillard72789ef2012-04-02 17:52:20 +0800233xz_load(xz_statep state, unsigned char *buf, unsigned int len,
234 unsigned int *have)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200235{
236 int ret;
237
238 *have = 0;
239 do {
240 ret = read(state->fd, buf + *have, len - *have);
241 if (ret <= 0)
242 break;
243 *have += ret;
244 } while (*have < len);
245 if (ret < 0) {
246 xz_error(state, -1, strerror(errno));
247 return -1;
248 }
249 if (ret == 0)
250 state->eof = 1;
251 return 0;
252}
253
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800254static int
255xz_avail(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200256{
257 lzma_stream *strm = &(state->strm);
258
259 if (state->err != LZMA_OK)
260 return -1;
261 if (state->eof == 0) {
Marcus Meissner99644922012-05-07 18:41:42 +0800262 /* avail_in is size_t, which is not necessary sizeof(unsigned) */
263 unsigned tmp = strm->avail_in;
264
265 if (xz_load(state, state->in, state->size, &tmp) == -1) {
266 strm->avail_in = tmp;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200267 return -1;
Marcus Meissner99644922012-05-07 18:41:42 +0800268 }
269 strm->avail_in = tmp;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200270 strm->next_in = state->in;
271 }
272 return 0;
273}
274
Mike Alexandera1313a62013-11-28 23:21:23 +0800275#ifdef HAVE_ZLIB_H
276static int
277xz_avail_zstrm(xz_statep state)
278{
279 int ret;
280 state->strm.avail_in = state->zstrm.avail_in;
281 state->strm.next_in = state->zstrm.next_in;
282 ret = xz_avail(state);
283 state->zstrm.avail_in = (uInt) state->strm.avail_in;
284 state->zstrm.next_in = (Bytef *) state->strm.next_in;
285 return ret;
286}
287#endif
288
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200289static int
290is_format_xz(xz_statep state)
291{
292 lzma_stream *strm = &(state->strm);
293
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800294 return strm->avail_in >= 6 && memcmp(state->in, "\3757zXZ", 6) == 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200295}
296
297static int
298is_format_lzma(xz_statep state)
299{
300 lzma_stream *strm = &(state->strm);
301
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800302 lzma_filter filter;
303 lzma_options_lzma *opt;
304 uint32_t dict_size;
305 uint64_t uncompressed_size;
306 size_t i;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200307
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800308 if (strm->avail_in < 13)
309 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200310
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800311 filter.id = LZMA_FILTER_LZMA1;
312 if (lzma_properties_decode(&filter, NULL, state->in, 5) != LZMA_OK)
313 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200314
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800315 opt = filter.options;
316 dict_size = opt->dict_size;
Daniel Veillard94431ec2012-05-15 10:45:05 +0800317 free(opt); /* we can't use xmlFree on a string returned by zlib */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200318
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800319 /* A hack to ditch tons of false positives: We allow only dictionary
320 * sizes that are 2^n or 2^n + 2^(n-1) or UINT32_MAX. LZMA_Alone
321 * created only files with 2^n, but accepts any dictionary size.
322 * If someone complains, this will be reconsidered.
323 */
324 if (dict_size != UINT32_MAX) {
325 uint32_t d = dict_size - 1;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200326
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800327 d |= d >> 2;
328 d |= d >> 3;
329 d |= d >> 4;
330 d |= d >> 8;
331 d |= d >> 16;
332 ++d;
333 if (d != dict_size || dict_size == 0)
334 return 0;
335 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200336
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800337 /* Another hack to ditch false positives: Assume that if the
338 * uncompressed size is known, it must be less than 256 GiB.
339 * Again, if someone complains, this will be reconsidered.
340 */
341 uncompressed_size = 0;
342 for (i = 0; i < 8; ++i)
343 uncompressed_size |= (uint64_t) (state->in[5 + i]) << (i * 8);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200344
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800345 if (uncompressed_size != UINT64_MAX
346 && uncompressed_size > (UINT64_C(1) << 38))
347 return 0;
348
349 return 1;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200350}
351
352#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800353
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200354/* Get next byte from input, or -1 if end or error. */
355#define NEXT() ((strm->avail_in == 0 && xz_avail(state) == -1) ? -1 : \
356 (strm->avail_in == 0 ? -1 : \
357 (strm->avail_in--, *(strm->next_in)++)))
Mike Alexandera1313a62013-11-28 23:21:23 +0800358/* Same thing, but from zstrm */
359#define NEXTZ() ((strm->avail_in == 0 && xz_avail_zstrm(state) == -1) ? -1 : \
360 (strm->avail_in == 0 ? -1 : \
361 (strm->avail_in--, *(strm->next_in)++)))
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200362
363/* Get a four-byte little-endian integer and return 0 on success and the value
364 in *ret. Otherwise -1 is returned and *ret is not modified. */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800365static int
366gz_next4(xz_statep state, unsigned long *ret)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200367{
368 int ch;
369 unsigned long val;
370 z_streamp strm = &(state->zstrm);
371
Mike Alexandera1313a62013-11-28 23:21:23 +0800372 val = NEXTZ();
373 val += (unsigned) NEXTZ() << 8;
374 val += (unsigned long) NEXTZ() << 16;
375 ch = NEXTZ();
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200376 if (ch == -1)
377 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800378 val += (unsigned long) ch << 24;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200379 *ret = val;
380 return 0;
381}
382#endif
383
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800384static int
385xz_head(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200386{
387 lzma_stream *strm = &(state->strm);
388 lzma_stream init = LZMA_STREAM_INIT;
389 int flags;
390 unsigned len;
391
392 /* allocate read buffers and inflate memory */
393 if (state->size == 0) {
394 /* allocate buffers */
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800395 state->in = xmlMalloc(state->want);
396 state->out = xmlMalloc(state->want << 1);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200397 if (state->in == NULL || state->out == NULL) {
398 if (state->out != NULL)
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800399 xmlFree(state->out);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200400 if (state->in != NULL)
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800401 xmlFree(state->in);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200402 xz_error(state, LZMA_MEM_ERROR, "out of memory");
403 return -1;
404 }
405 state->size = state->want;
406
407 /* allocate decoder memory */
408 state->strm = init;
409 state->strm.avail_in = 0;
410 state->strm.next_in = NULL;
Nick Wellnhofere2a91222017-09-07 18:36:01 +0200411 if (lzma_auto_decoder(&state->strm, 100000000, 0) != LZMA_OK) {
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800412 xmlFree(state->out);
413 xmlFree(state->in);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200414 state->size = 0;
415 xz_error(state, LZMA_MEM_ERROR, "out of memory");
416 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800417 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200418#ifdef HAVE_ZLIB_H
419 /* allocate inflate memory */
420 state->zstrm.zalloc = Z_NULL;
421 state->zstrm.zfree = Z_NULL;
422 state->zstrm.opaque = Z_NULL;
423 state->zstrm.avail_in = 0;
424 state->zstrm.next_in = Z_NULL;
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800425 if (state->init == 0) {
426 if (inflateInit2(&(state->zstrm), -15) != Z_OK) {/* raw inflate */
427 xmlFree(state->out);
428 xmlFree(state->in);
429 state->size = 0;
430 xz_error(state, LZMA_MEM_ERROR, "out of memory");
431 return -1;
432 }
433 state->init = 1;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200434 }
435#endif
436 }
437
438 /* get some data in the input buffer */
439 if (strm->avail_in == 0) {
440 if (xz_avail(state) == -1)
441 return -1;
442 if (strm->avail_in == 0)
443 return 0;
444 }
445
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800446 /* look for the xz magic header bytes */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200447 if (is_format_xz(state) || is_format_lzma(state)) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800448 state->how = LZMA;
449 state->direct = 0;
450 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200451 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200452#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800453 /* look for the gzip magic header bytes 31 and 139 */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200454 if (strm->next_in[0] == 31) {
455 strm->avail_in--;
456 strm->next_in++;
457 if (strm->avail_in == 0 && xz_avail(state) == -1)
458 return -1;
459 if (strm->avail_in && strm->next_in[0] == 139) {
460 /* we have a gzip header, woo hoo! */
461 strm->avail_in--;
462 strm->next_in++;
463
464 /* skip rest of header */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800465 if (NEXT() != 8) { /* compression method */
466 xz_error(state, LZMA_DATA_ERROR,
467 "unknown compression method");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200468 return -1;
469 }
470 flags = NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800471 if (flags & 0xe0) { /* reserved flag bits */
472 xz_error(state, LZMA_DATA_ERROR,
473 "unknown header flags set");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200474 return -1;
475 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800476 NEXT(); /* modification time */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200477 NEXT();
478 NEXT();
479 NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800480 NEXT(); /* extra flags */
481 NEXT(); /* operating system */
482 if (flags & 4) { /* extra field */
483 len = (unsigned) NEXT();
484 len += (unsigned) NEXT() << 8;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200485 while (len--)
486 if (NEXT() < 0)
487 break;
488 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800489 if (flags & 8) /* file name */
490 while (NEXT() > 0) ;
491 if (flags & 16) /* comment */
492 while (NEXT() > 0) ;
493 if (flags & 2) { /* header crc */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200494 NEXT();
495 NEXT();
496 }
497 /* an unexpected end of file is not checked for here -- it will be
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800498 * noticed on the first request for uncompressed data */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200499
500 /* set up for decompression */
501 inflateReset(&state->zstrm);
502 state->zstrm.adler = crc32(0L, Z_NULL, 0);
503 state->how = GZIP;
504 state->direct = 0;
505 return 0;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800506 } else {
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200507 /* not a gzip file -- save first byte (31) and fall to raw i/o */
508 state->out[0] = 31;
509 state->have = 1;
510 }
511 }
512#endif
513
514 /* doing raw i/o, save start of raw data for seeking, copy any leftover
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800515 * input to output -- this assumes that the output buffer is larger than
516 * the input buffer, which also assures space for gzungetc() */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200517 state->raw = state->pos;
518 state->next = state->out;
519 if (strm->avail_in) {
520 memcpy(state->next + state->have, strm->next_in, strm->avail_in);
521 state->have += strm->avail_in;
522 strm->avail_in = 0;
523 }
524 state->how = COPY;
525 state->direct = 1;
526 return 0;
527}
528
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800529static int
530xz_decomp(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200531{
532 int ret;
533 unsigned had;
534 unsigned long crc, len;
535 lzma_stream *strm = &(state->strm);
536
537 lzma_action action = LZMA_RUN;
538
539 /* fill output buffer up to end of deflate stream */
540 had = strm->avail_out;
541 do {
542 /* get more input for inflate() */
543 if (strm->avail_in == 0 && xz_avail(state) == -1)
544 return -1;
545 if (strm->avail_in == 0) {
546 xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
547 return -1;
548 }
549 if (state->eof)
550 action = LZMA_FINISH;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800551
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200552 /* decompress and handle errors */
553#ifdef HAVE_ZLIB_H
554 if (state->how == GZIP) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800555 state->zstrm.avail_in = (uInt) state->strm.avail_in;
556 state->zstrm.next_in = (Bytef *) state->strm.next_in;
557 state->zstrm.avail_out = (uInt) state->strm.avail_out;
558 state->zstrm.next_out = (Bytef *) state->strm.next_out;
559 ret = inflate(&state->zstrm, Z_NO_FLUSH);
560 if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
561 xz_error(state, Z_STREAM_ERROR,
562 "internal error: inflate stream corrupt");
563 return -1;
564 }
565 if (ret == Z_MEM_ERROR)
566 ret = LZMA_MEM_ERROR;
567 if (ret == Z_DATA_ERROR)
568 ret = LZMA_DATA_ERROR;
569 if (ret == Z_STREAM_END)
570 ret = LZMA_STREAM_END;
571 state->strm.avail_in = state->zstrm.avail_in;
572 state->strm.next_in = state->zstrm.next_in;
573 state->strm.avail_out = state->zstrm.avail_out;
574 state->strm.next_out = state->zstrm.next_out;
575 } else /* state->how == LZMA */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200576#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800577 ret = lzma_code(strm, action);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200578 if (ret == LZMA_MEM_ERROR) {
579 xz_error(state, LZMA_MEM_ERROR, "out of memory");
580 return -1;
581 }
582 if (ret == LZMA_DATA_ERROR) {
583 xz_error(state, LZMA_DATA_ERROR, "compressed data error");
584 return -1;
585 }
Daniel Veillardf0709e32015-11-03 15:31:25 +0800586 if (ret == LZMA_PROG_ERROR) {
587 xz_error(state, LZMA_PROG_ERROR, "compression error");
588 return -1;
589 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200590 } while (strm->avail_out && ret != LZMA_STREAM_END);
591
592 /* update available output and crc check value */
593 state->have = had - strm->avail_out;
594 state->next = strm->next_out - state->have;
595#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800596 state->zstrm.adler =
597 crc32(state->zstrm.adler, state->next, state->have);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200598#endif
599
600 if (ret == LZMA_STREAM_END) {
601#ifdef HAVE_ZLIB_H
602 if (state->how == GZIP) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800603 if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) {
604 xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
605 return -1;
606 }
607 if (crc != state->zstrm.adler) {
608 xz_error(state, LZMA_DATA_ERROR, "incorrect data check");
609 return -1;
610 }
611 if (len != (state->zstrm.total_out & 0xffffffffL)) {
612 xz_error(state, LZMA_DATA_ERROR, "incorrect length check");
613 return -1;
614 }
615 state->strm.avail_in = 0;
616 state->strm.next_in = NULL;
617 state->strm.avail_out = 0;
618 state->strm.next_out = NULL;
619 } else
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200620#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800621 if (strm->avail_in != 0 || !state->eof) {
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200622 xz_error(state, LZMA_DATA_ERROR, "trailing garbage");
623 return -1;
624 }
625 state->how = LOOK; /* ready for next stream, once have is 0 (leave
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800626 * state->direct unchanged to remember how) */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200627 }
628
629 /* good decompression */
630 return 0;
631}
632
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800633static int
634xz_make(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200635{
636 lzma_stream *strm = &(state->strm);
637
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800638 if (state->how == LOOK) { /* look for lzma / gzip header */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200639 if (xz_head(state) == -1)
640 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800641 if (state->have) /* got some data from xz_head() */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200642 return 0;
643 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800644 if (state->how == COPY) { /* straight copy */
645 if (xz_load(state, state->out, state->size << 1, &(state->have)) ==
646 -1)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200647 return -1;
648 state->next = state->out;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800649 } else if (state->how == LZMA || state->how == GZIP) { /* decompress */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200650 strm->avail_out = state->size << 1;
651 strm->next_out = state->out;
652 if (xz_decomp(state) == -1)
653 return -1;
654 }
655 return 0;
656}
657
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800658static int
659xz_skip(xz_statep state, uint64_t len)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200660{
661 unsigned n;
662
663 /* skip over len bytes or reach end-of-file, whichever comes first */
664 while (len)
665 /* skip over whatever is in output buffer */
666 if (state->have) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800667 n = (uint64_t) state->have > len ?
668 (unsigned) len : state->have;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200669 state->have -= n;
670 state->next += n;
671 state->pos += n;
672 len -= n;
673 }
674
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800675 /* output buffer empty -- return if we're at the end of the input */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200676 else if (state->eof && state->strm.avail_in == 0)
677 break;
678
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800679 /* need more data to skip -- load up output buffer */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200680 else {
681 /* get more output, looking for header if required */
682 if (xz_make(state) == -1)
683 return -1;
684 }
685 return 0;
686}
687
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800688int
689__libxml2_xzread(xzFile file, void *buf, unsigned len)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200690{
691 unsigned got, n;
692 xz_statep state;
693 lzma_stream *strm;
694
695 /* get internal structure */
696 if (file == NULL)
697 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800698 state = (xz_statep) file;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200699 strm = &(state->strm);
700
701 /* check that we're reading and that there's no error */
702 if (state->err != LZMA_OK)
703 return -1;
704
705 /* since an int is returned, make sure len fits in one, otherwise return
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800706 * with an error (this avoids the flaw in the interface) */
707 if ((int) len < 0) {
708 xz_error(state, LZMA_BUF_ERROR,
709 "requested length does not fit in int");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200710 return -1;
711 }
712
713 /* if len is zero, avoid unnecessary operations */
714 if (len == 0)
715 return 0;
716
717 /* process a skip request */
718 if (state->seek) {
719 state->seek = 0;
720 if (xz_skip(state, state->skip) == -1)
721 return -1;
722 }
723
724 /* get len bytes to buf, or less than len if at the end */
725 got = 0;
726 do {
727 /* first just try copying data from the output buffer */
728 if (state->have) {
729 n = state->have > len ? len : state->have;
730 memcpy(buf, state->next, n);
731 state->next += n;
732 state->have -= n;
733 }
734
735 /* output buffer empty -- return if we're at the end of the input */
736 else if (state->eof && strm->avail_in == 0)
737 break;
738
739 /* need output data -- for small len or new stream load up our output
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800740 * buffer */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200741 else if (state->how == LOOK || len < (state->size << 1)) {
742 /* get more output, looking for header if required */
743 if (xz_make(state) == -1)
744 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800745 continue; /* no progress yet -- go back to memcpy() above */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200746 /* the copy above assures that we will leave with space in the
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800747 * output buffer, allowing at least one gzungetc() to succeed */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200748 }
749
750 /* large len -- read directly into user buffer */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800751 else if (state->how == COPY) { /* read directly */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200752 if (xz_load(state, buf, len, &n) == -1)
753 return -1;
754 }
755
756 /* large len -- decompress directly into user buffer */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800757 else { /* state->how == LZMA */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200758 strm->avail_out = len;
759 strm->next_out = buf;
760 if (xz_decomp(state) == -1)
761 return -1;
762 n = state->have;
763 state->have = 0;
764 }
765
766 /* update progress */
767 len -= n;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800768 buf = (char *) buf + n;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200769 got += n;
770 state->pos += n;
771 } while (len);
772
773 /* return number of bytes read into user buffer (will fit in int) */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800774 return (int) got;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200775}
776
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800777int
778__libxml2_xzclose(xzFile file)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200779{
780 int ret;
781 xz_statep state;
782
783 /* get internal structure */
784 if (file == NULL)
785 return LZMA_DATA_ERROR;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800786 state = (xz_statep) file;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200787
788 /* free memory and close file */
789 if (state->size) {
790 lzma_end(&(state->strm));
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800791#ifdef HAVE_ZLIB_H
792 if (state->init == 1)
793 inflateEnd(&(state->zstrm));
794 state->init = 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200795#endif
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800796 xmlFree(state->out);
797 xmlFree(state->in);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200798 }
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800799 xmlFree(state->path);
Nick Wellnhofer07e227e2017-09-07 18:55:46 +0200800 if ((state->msg != NULL) && (state->err != LZMA_MEM_ERROR))
801 xmlFree(state->msg);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200802 ret = close(state->fd);
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800803 xmlFree(state);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200804 return ret ? ret : LZMA_OK;
805}
Daniel Veillard18b89882015-11-03 15:46:29 +0800806#endif /* LIBXML_LZMA_ENABLED */