blob: 150e80332a9183a7a996e2db3aa56c383195c17d [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"
Daniel Veillard9f3cdef2012-05-15 09:38:13 +080040#include <libxml/xmlmemory.h>
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020041
42/* values for xz_state how */
Daniel Veillardadf5ec92012-01-26 16:56:22 +080043#define LOOK 0 /* look for a gzip/lzma header */
44#define COPY 1 /* copy input directly */
45#define GZIP 2 /* decompress a gzip stream */
46#define LZMA 3 /* decompress a lzma stream */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020047
48/* internal lzma file state data structure */
49typedef struct {
Daniel Veillardadf5ec92012-01-26 16:56:22 +080050 int mode; /* see lzma modes above */
51 int fd; /* file descriptor */
52 char *path; /* path or fd for error messages */
53 uint64_t pos; /* current position in uncompressed data */
Daniel Veillard72789ef2012-04-02 17:52:20 +080054 unsigned int size; /* buffer size, zero if not allocated yet */
55 unsigned int want; /* requested buffer size, default is BUFSIZ */
Daniel Veillardadf5ec92012-01-26 16:56:22 +080056 unsigned char *in; /* input buffer */
57 unsigned char *out; /* output buffer (double-sized when reading) */
58 unsigned char *next; /* next output data to deliver or write */
Daniel Veillard72789ef2012-04-02 17:52:20 +080059 unsigned int have; /* amount of output data unused at next */
Daniel Veillardadf5ec92012-01-26 16:56:22 +080060 int eof; /* true if end of input file reached */
61 uint64_t start; /* where the lzma data started, for rewinding */
62 uint64_t raw; /* where the raw data started, for seeking */
63 int how; /* 0: get header, 1: copy, 2: decompress */
64 int direct; /* true if last read direct, false if lzma */
65 /* seek request */
66 uint64_t skip; /* amount to skip (already rewound if backwards) */
67 int seek; /* true if seek request pending */
68 /* error information */
69 int err; /* error code */
70 char *msg; /* error message */
71 /* lzma stream */
Daniel Veillard9f3cdef2012-05-15 09:38:13 +080072 int init; /* is the iniflate stream initialized */
Daniel Veillardadf5ec92012-01-26 16:56:22 +080073 lzma_stream strm; /* stream structure in-place (not a pointer) */
74 char padding1[32]; /* padding allowing to cope with possible
75 extensions of above structure without
76 too much side effect */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020077#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +080078 /* zlib inflate or deflate stream */
79 z_stream zstrm; /* stream structure in-place (not a pointer) */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020080#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +080081 char padding2[32]; /* padding allowing to cope with possible
82 extensions of above structure without
83 too much side effect */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020084} xz_state, *xz_statep;
85
Daniel Veillardadf5ec92012-01-26 16:56:22 +080086static void
87xz_error(xz_statep state, int err, const char *msg)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020088{
89 /* free previously allocated message and clear */
90 if (state->msg != NULL) {
91 if (state->err != LZMA_MEM_ERROR)
Daniel Veillard9f3cdef2012-05-15 09:38:13 +080092 xmlFree(state->msg);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +020093 state->msg = NULL;
94 }
95
96 /* set error code, and if no message, then done */
97 state->err = err;
98 if (msg == NULL)
99 return;
100
101 /* for an out of memory error, save as static string */
102 if (err == LZMA_MEM_ERROR) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800103 state->msg = (char *) msg;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200104 return;
105 }
106
107 /* construct error message with path */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800108 if ((state->msg =
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800109 xmlMalloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200110 state->err = LZMA_MEM_ERROR;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800111 state->msg = (char *) "out of memory";
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200112 return;
113 }
114 strcpy(state->msg, state->path);
115 strcat(state->msg, ": ");
116 strcat(state->msg, msg);
117 return;
118}
119
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800120static void
121xz_reset(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200122{
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800123 state->have = 0; /* no output data available */
124 state->eof = 0; /* not at end of file */
125 state->how = LOOK; /* look for gzip header */
126 state->direct = 1; /* default for empty file */
127 state->seek = 0; /* no seek request pending */
128 xz_error(state, LZMA_OK, NULL); /* clear error */
129 state->pos = 0; /* no uncompressed data yet */
130 state->strm.avail_in = 0; /* no input data yet */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200131#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800132 state->zstrm.avail_in = 0; /* no input data yet */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200133#endif
134}
135
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800136static xzFile
137xz_open(const char *path, int fd, const char *mode ATTRIBUTE_UNUSED)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200138{
139 xz_statep state;
140
141 /* allocate xzFile structure to return */
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800142 state = xmlMalloc(sizeof(xz_state));
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200143 if (state == NULL)
144 return NULL;
145 state->size = 0; /* no buffers allocated yet */
146 state->want = BUFSIZ; /* requested buffer size */
147 state->msg = NULL; /* no error message yet */
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800148 state->init = 0; /* initialization of zlib data */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200149
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800150 /* save the path name for error messages */
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800151 state->path = xmlMalloc(strlen(path) + 1);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200152 if (state->path == NULL) {
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800153 xmlFree(state);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200154 return NULL;
155 }
156 strcpy(state->path, path);
157
158 /* open the file with the appropriate mode (or just use fd) */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800159 state->fd = fd != -1 ? fd : open(path,
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200160#ifdef O_LARGEFILE
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800161 O_LARGEFILE |
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200162#endif
163#ifdef O_BINARY
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800164 O_BINARY |
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200165#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800166 O_RDONLY, 0666);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200167 if (state->fd == -1) {
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800168 xmlFree(state->path);
169 xmlFree(state);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200170 return NULL;
171 }
172
173 /* save the current position for rewinding (only if reading) */
174 state->start = lseek(state->fd, 0, SEEK_CUR);
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800175 if (state->start == (uint64_t) - 1)
176 state->start = 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200177
178 /* initialize stream */
179 xz_reset(state);
180
181 /* return stream */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800182 return (xzFile) state;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200183}
184
Daniel Veillard63588f42013-05-10 14:01:46 +0800185static int
186xz_compressed(xzFile f) {
187 xz_statep state;
188
189 if (f == NULL)
190 return(-1);
191 state = (xz_statep) f;
192 if (state->init <= 0)
193 return(-1);
194
195 switch (state->how) {
196 case COPY:
197 return(0);
198 case GZIP:
199 case LZMA:
200 return(1);
201 }
202 return(-1);
203}
204
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800205xzFile
206__libxml2_xzopen(const char *path, const char *mode)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200207{
208 return xz_open(path, -1, mode);
209}
210
Daniel Veillard63588f42013-05-10 14:01:46 +0800211int
212__libxml2_xzcompressed(xzFile f) {
213 return xz_compressed(f);
214}
215
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800216xzFile
217__libxml2_xzdopen(int fd, const char *mode)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200218{
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800219 char *path; /* identifier for error messages */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200220 xzFile xz;
221
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800222 if (fd == -1 || (path = xmlMalloc(7 + 3 * sizeof(int))) == NULL)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200223 return NULL;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800224 sprintf(path, "<fd:%d>", fd); /* for debugging */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200225 xz = xz_open(path, fd, mode);
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800226 xmlFree(path);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200227 return xz;
228}
229
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800230static int
Daniel Veillard72789ef2012-04-02 17:52:20 +0800231xz_load(xz_statep state, unsigned char *buf, unsigned int len,
232 unsigned int *have)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200233{
234 int ret;
235
236 *have = 0;
237 do {
238 ret = read(state->fd, buf + *have, len - *have);
239 if (ret <= 0)
240 break;
241 *have += ret;
242 } while (*have < len);
243 if (ret < 0) {
244 xz_error(state, -1, strerror(errno));
245 return -1;
246 }
247 if (ret == 0)
248 state->eof = 1;
249 return 0;
250}
251
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800252static int
253xz_avail(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200254{
255 lzma_stream *strm = &(state->strm);
256
257 if (state->err != LZMA_OK)
258 return -1;
259 if (state->eof == 0) {
Marcus Meissner99644922012-05-07 18:41:42 +0800260 /* avail_in is size_t, which is not necessary sizeof(unsigned) */
261 unsigned tmp = strm->avail_in;
262
263 if (xz_load(state, state->in, state->size, &tmp) == -1) {
264 strm->avail_in = tmp;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200265 return -1;
Marcus Meissner99644922012-05-07 18:41:42 +0800266 }
267 strm->avail_in = tmp;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200268 strm->next_in = state->in;
269 }
270 return 0;
271}
272
273static int
274is_format_xz(xz_statep state)
275{
276 lzma_stream *strm = &(state->strm);
277
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800278 return strm->avail_in >= 6 && memcmp(state->in, "\3757zXZ", 6) == 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200279}
280
281static int
282is_format_lzma(xz_statep state)
283{
284 lzma_stream *strm = &(state->strm);
285
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800286 lzma_filter filter;
287 lzma_options_lzma *opt;
288 uint32_t dict_size;
289 uint64_t uncompressed_size;
290 size_t i;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200291
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800292 if (strm->avail_in < 13)
293 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200294
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800295 filter.id = LZMA_FILTER_LZMA1;
296 if (lzma_properties_decode(&filter, NULL, state->in, 5) != LZMA_OK)
297 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200298
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800299 opt = filter.options;
300 dict_size = opt->dict_size;
Daniel Veillard94431ec2012-05-15 10:45:05 +0800301 free(opt); /* we can't use xmlFree on a string returned by zlib */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200302
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800303 /* A hack to ditch tons of false positives: We allow only dictionary
304 * sizes that are 2^n or 2^n + 2^(n-1) or UINT32_MAX. LZMA_Alone
305 * created only files with 2^n, but accepts any dictionary size.
306 * If someone complains, this will be reconsidered.
307 */
308 if (dict_size != UINT32_MAX) {
309 uint32_t d = dict_size - 1;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200310
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800311 d |= d >> 2;
312 d |= d >> 3;
313 d |= d >> 4;
314 d |= d >> 8;
315 d |= d >> 16;
316 ++d;
317 if (d != dict_size || dict_size == 0)
318 return 0;
319 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200320
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800321 /* Another hack to ditch false positives: Assume that if the
322 * uncompressed size is known, it must be less than 256 GiB.
323 * Again, if someone complains, this will be reconsidered.
324 */
325 uncompressed_size = 0;
326 for (i = 0; i < 8; ++i)
327 uncompressed_size |= (uint64_t) (state->in[5 + i]) << (i * 8);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200328
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800329 if (uncompressed_size != UINT64_MAX
330 && uncompressed_size > (UINT64_C(1) << 38))
331 return 0;
332
333 return 1;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200334}
335
336#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800337
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200338/* Get next byte from input, or -1 if end or error. */
339#define NEXT() ((strm->avail_in == 0 && xz_avail(state) == -1) ? -1 : \
340 (strm->avail_in == 0 ? -1 : \
341 (strm->avail_in--, *(strm->next_in)++)))
342
343/* Get a four-byte little-endian integer and return 0 on success and the value
344 in *ret. Otherwise -1 is returned and *ret is not modified. */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800345static int
346gz_next4(xz_statep state, unsigned long *ret)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200347{
348 int ch;
349 unsigned long val;
350 z_streamp strm = &(state->zstrm);
351
352 val = NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800353 val += (unsigned) NEXT() << 8;
354 val += (unsigned long) NEXT() << 16;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200355 ch = NEXT();
356 if (ch == -1)
357 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800358 val += (unsigned long) ch << 24;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200359 *ret = val;
360 return 0;
361}
362#endif
363
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800364static int
365xz_head(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200366{
367 lzma_stream *strm = &(state->strm);
368 lzma_stream init = LZMA_STREAM_INIT;
369 int flags;
370 unsigned len;
371
372 /* allocate read buffers and inflate memory */
373 if (state->size == 0) {
374 /* allocate buffers */
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800375 state->in = xmlMalloc(state->want);
376 state->out = xmlMalloc(state->want << 1);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200377 if (state->in == NULL || state->out == NULL) {
378 if (state->out != NULL)
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800379 xmlFree(state->out);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200380 if (state->in != NULL)
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800381 xmlFree(state->in);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200382 xz_error(state, LZMA_MEM_ERROR, "out of memory");
383 return -1;
384 }
385 state->size = state->want;
386
387 /* allocate decoder memory */
388 state->strm = init;
389 state->strm.avail_in = 0;
390 state->strm.next_in = NULL;
391 if (lzma_auto_decoder(&state->strm, UINT64_MAX, 0) != LZMA_OK) {
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800392 xmlFree(state->out);
393 xmlFree(state->in);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200394 state->size = 0;
395 xz_error(state, LZMA_MEM_ERROR, "out of memory");
396 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800397 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200398#ifdef HAVE_ZLIB_H
399 /* allocate inflate memory */
400 state->zstrm.zalloc = Z_NULL;
401 state->zstrm.zfree = Z_NULL;
402 state->zstrm.opaque = Z_NULL;
403 state->zstrm.avail_in = 0;
404 state->zstrm.next_in = Z_NULL;
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800405 if (state->init == 0) {
406 if (inflateInit2(&(state->zstrm), -15) != Z_OK) {/* raw inflate */
407 xmlFree(state->out);
408 xmlFree(state->in);
409 state->size = 0;
410 xz_error(state, LZMA_MEM_ERROR, "out of memory");
411 return -1;
412 }
413 state->init = 1;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200414 }
415#endif
416 }
417
418 /* get some data in the input buffer */
419 if (strm->avail_in == 0) {
420 if (xz_avail(state) == -1)
421 return -1;
422 if (strm->avail_in == 0)
423 return 0;
424 }
425
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800426 /* look for the xz magic header bytes */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200427 if (is_format_xz(state) || is_format_lzma(state)) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800428 state->how = LZMA;
429 state->direct = 0;
430 return 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200431 }
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200432#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800433 /* look for the gzip magic header bytes 31 and 139 */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200434 if (strm->next_in[0] == 31) {
435 strm->avail_in--;
436 strm->next_in++;
437 if (strm->avail_in == 0 && xz_avail(state) == -1)
438 return -1;
439 if (strm->avail_in && strm->next_in[0] == 139) {
440 /* we have a gzip header, woo hoo! */
441 strm->avail_in--;
442 strm->next_in++;
443
444 /* skip rest of header */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800445 if (NEXT() != 8) { /* compression method */
446 xz_error(state, LZMA_DATA_ERROR,
447 "unknown compression method");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200448 return -1;
449 }
450 flags = NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800451 if (flags & 0xe0) { /* reserved flag bits */
452 xz_error(state, LZMA_DATA_ERROR,
453 "unknown header flags set");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200454 return -1;
455 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800456 NEXT(); /* modification time */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200457 NEXT();
458 NEXT();
459 NEXT();
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800460 NEXT(); /* extra flags */
461 NEXT(); /* operating system */
462 if (flags & 4) { /* extra field */
463 len = (unsigned) NEXT();
464 len += (unsigned) NEXT() << 8;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200465 while (len--)
466 if (NEXT() < 0)
467 break;
468 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800469 if (flags & 8) /* file name */
470 while (NEXT() > 0) ;
471 if (flags & 16) /* comment */
472 while (NEXT() > 0) ;
473 if (flags & 2) { /* header crc */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200474 NEXT();
475 NEXT();
476 }
477 /* an unexpected end of file is not checked for here -- it will be
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800478 * noticed on the first request for uncompressed data */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200479
480 /* set up for decompression */
481 inflateReset(&state->zstrm);
482 state->zstrm.adler = crc32(0L, Z_NULL, 0);
483 state->how = GZIP;
484 state->direct = 0;
485 return 0;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800486 } else {
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200487 /* not a gzip file -- save first byte (31) and fall to raw i/o */
488 state->out[0] = 31;
489 state->have = 1;
490 }
491 }
492#endif
493
494 /* doing raw i/o, save start of raw data for seeking, copy any leftover
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800495 * input to output -- this assumes that the output buffer is larger than
496 * the input buffer, which also assures space for gzungetc() */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200497 state->raw = state->pos;
498 state->next = state->out;
499 if (strm->avail_in) {
500 memcpy(state->next + state->have, strm->next_in, strm->avail_in);
501 state->have += strm->avail_in;
502 strm->avail_in = 0;
503 }
504 state->how = COPY;
505 state->direct = 1;
506 return 0;
507}
508
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800509static int
510xz_decomp(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200511{
512 int ret;
513 unsigned had;
514 unsigned long crc, len;
515 lzma_stream *strm = &(state->strm);
516
517 lzma_action action = LZMA_RUN;
518
519 /* fill output buffer up to end of deflate stream */
520 had = strm->avail_out;
521 do {
522 /* get more input for inflate() */
523 if (strm->avail_in == 0 && xz_avail(state) == -1)
524 return -1;
525 if (strm->avail_in == 0) {
526 xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
527 return -1;
528 }
529 if (state->eof)
530 action = LZMA_FINISH;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800531
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200532 /* decompress and handle errors */
533#ifdef HAVE_ZLIB_H
534 if (state->how == GZIP) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800535 state->zstrm.avail_in = (uInt) state->strm.avail_in;
536 state->zstrm.next_in = (Bytef *) state->strm.next_in;
537 state->zstrm.avail_out = (uInt) state->strm.avail_out;
538 state->zstrm.next_out = (Bytef *) state->strm.next_out;
539 ret = inflate(&state->zstrm, Z_NO_FLUSH);
540 if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
541 xz_error(state, Z_STREAM_ERROR,
542 "internal error: inflate stream corrupt");
543 return -1;
544 }
545 if (ret == Z_MEM_ERROR)
546 ret = LZMA_MEM_ERROR;
547 if (ret == Z_DATA_ERROR)
548 ret = LZMA_DATA_ERROR;
549 if (ret == Z_STREAM_END)
550 ret = LZMA_STREAM_END;
551 state->strm.avail_in = state->zstrm.avail_in;
552 state->strm.next_in = state->zstrm.next_in;
553 state->strm.avail_out = state->zstrm.avail_out;
554 state->strm.next_out = state->zstrm.next_out;
555 } else /* state->how == LZMA */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200556#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800557 ret = lzma_code(strm, action);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200558 if (ret == LZMA_MEM_ERROR) {
559 xz_error(state, LZMA_MEM_ERROR, "out of memory");
560 return -1;
561 }
562 if (ret == LZMA_DATA_ERROR) {
563 xz_error(state, LZMA_DATA_ERROR, "compressed data error");
564 return -1;
565 }
566 } while (strm->avail_out && ret != LZMA_STREAM_END);
567
568 /* update available output and crc check value */
569 state->have = had - strm->avail_out;
570 state->next = strm->next_out - state->have;
571#ifdef HAVE_ZLIB_H
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800572 state->zstrm.adler =
573 crc32(state->zstrm.adler, state->next, state->have);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200574#endif
575
576 if (ret == LZMA_STREAM_END) {
577#ifdef HAVE_ZLIB_H
578 if (state->how == GZIP) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800579 if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) {
580 xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
581 return -1;
582 }
583 if (crc != state->zstrm.adler) {
584 xz_error(state, LZMA_DATA_ERROR, "incorrect data check");
585 return -1;
586 }
587 if (len != (state->zstrm.total_out & 0xffffffffL)) {
588 xz_error(state, LZMA_DATA_ERROR, "incorrect length check");
589 return -1;
590 }
591 state->strm.avail_in = 0;
592 state->strm.next_in = NULL;
593 state->strm.avail_out = 0;
594 state->strm.next_out = NULL;
595 } else
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200596#endif
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800597 if (strm->avail_in != 0 || !state->eof) {
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200598 xz_error(state, LZMA_DATA_ERROR, "trailing garbage");
599 return -1;
600 }
601 state->how = LOOK; /* ready for next stream, once have is 0 (leave
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800602 * state->direct unchanged to remember how) */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200603 }
604
605 /* good decompression */
606 return 0;
607}
608
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800609static int
610xz_make(xz_statep state)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200611{
612 lzma_stream *strm = &(state->strm);
613
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800614 if (state->how == LOOK) { /* look for lzma / gzip header */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200615 if (xz_head(state) == -1)
616 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800617 if (state->have) /* got some data from xz_head() */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200618 return 0;
619 }
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800620 if (state->how == COPY) { /* straight copy */
621 if (xz_load(state, state->out, state->size << 1, &(state->have)) ==
622 -1)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200623 return -1;
624 state->next = state->out;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800625 } else if (state->how == LZMA || state->how == GZIP) { /* decompress */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200626 strm->avail_out = state->size << 1;
627 strm->next_out = state->out;
628 if (xz_decomp(state) == -1)
629 return -1;
630 }
631 return 0;
632}
633
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800634static int
635xz_skip(xz_statep state, uint64_t len)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200636{
637 unsigned n;
638
639 /* skip over len bytes or reach end-of-file, whichever comes first */
640 while (len)
641 /* skip over whatever is in output buffer */
642 if (state->have) {
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800643 n = (uint64_t) state->have > len ?
644 (unsigned) len : state->have;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200645 state->have -= n;
646 state->next += n;
647 state->pos += n;
648 len -= n;
649 }
650
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800651 /* output buffer empty -- return if we're at the end of the input */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200652 else if (state->eof && state->strm.avail_in == 0)
653 break;
654
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800655 /* need more data to skip -- load up output buffer */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200656 else {
657 /* get more output, looking for header if required */
658 if (xz_make(state) == -1)
659 return -1;
660 }
661 return 0;
662}
663
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800664int
665__libxml2_xzread(xzFile file, void *buf, unsigned len)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200666{
667 unsigned got, n;
668 xz_statep state;
669 lzma_stream *strm;
670
671 /* get internal structure */
672 if (file == NULL)
673 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800674 state = (xz_statep) file;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200675 strm = &(state->strm);
676
677 /* check that we're reading and that there's no error */
678 if (state->err != LZMA_OK)
679 return -1;
680
681 /* since an int is returned, make sure len fits in one, otherwise return
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800682 * with an error (this avoids the flaw in the interface) */
683 if ((int) len < 0) {
684 xz_error(state, LZMA_BUF_ERROR,
685 "requested length does not fit in int");
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200686 return -1;
687 }
688
689 /* if len is zero, avoid unnecessary operations */
690 if (len == 0)
691 return 0;
692
693 /* process a skip request */
694 if (state->seek) {
695 state->seek = 0;
696 if (xz_skip(state, state->skip) == -1)
697 return -1;
698 }
699
700 /* get len bytes to buf, or less than len if at the end */
701 got = 0;
702 do {
703 /* first just try copying data from the output buffer */
704 if (state->have) {
705 n = state->have > len ? len : state->have;
706 memcpy(buf, state->next, n);
707 state->next += n;
708 state->have -= n;
709 }
710
711 /* output buffer empty -- return if we're at the end of the input */
712 else if (state->eof && strm->avail_in == 0)
713 break;
714
715 /* need output data -- for small len or new stream load up our output
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800716 * buffer */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200717 else if (state->how == LOOK || len < (state->size << 1)) {
718 /* get more output, looking for header if required */
719 if (xz_make(state) == -1)
720 return -1;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800721 continue; /* no progress yet -- go back to memcpy() above */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200722 /* the copy above assures that we will leave with space in the
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800723 * output buffer, allowing at least one gzungetc() to succeed */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200724 }
725
726 /* large len -- read directly into user buffer */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800727 else if (state->how == COPY) { /* read directly */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200728 if (xz_load(state, buf, len, &n) == -1)
729 return -1;
730 }
731
732 /* large len -- decompress directly into user buffer */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800733 else { /* state->how == LZMA */
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200734 strm->avail_out = len;
735 strm->next_out = buf;
736 if (xz_decomp(state) == -1)
737 return -1;
738 n = state->have;
739 state->have = 0;
740 }
741
742 /* update progress */
743 len -= n;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800744 buf = (char *) buf + n;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200745 got += n;
746 state->pos += n;
747 } while (len);
748
749 /* return number of bytes read into user buffer (will fit in int) */
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800750 return (int) got;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200751}
752
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800753int
754__libxml2_xzclose(xzFile file)
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200755{
756 int ret;
757 xz_statep state;
758
759 /* get internal structure */
760 if (file == NULL)
761 return LZMA_DATA_ERROR;
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800762 state = (xz_statep) file;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200763
764 /* free memory and close file */
765 if (state->size) {
766 lzma_end(&(state->strm));
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800767#ifdef HAVE_ZLIB_H
768 if (state->init == 1)
769 inflateEnd(&(state->zstrm));
770 state->init = 0;
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200771#endif
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800772 xmlFree(state->out);
773 xmlFree(state->in);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200774 }
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800775 xmlFree(state->path);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200776 ret = close(state->fd);
Daniel Veillard9f3cdef2012-05-15 09:38:13 +0800777 xmlFree(state);
Anders F Bjorklund6bdc7742011-09-19 09:53:20 +0200778 return ret ? ret : LZMA_OK;
779}
Daniel Veillardadf5ec92012-01-26 16:56:22 +0800780#endif /* HAVE_LZMA_H */