Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /**  | 
2  |  |  * xzlib.c: front end for the transparent support 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 LIBXML_LZMA_ENABLED  | 
12  |  |  | 
13  |  | #include <string.h>  | 
14  |  | #include <stdlib.h>  | 
15  |  | #include <errno.h>  | 
16  |  |  | 
17  |  | #ifdef HAVE_SYS_STAT_H  | 
18  |  | #include <sys/stat.h>  | 
19  |  | #endif  | 
20  |  | #ifdef HAVE_FCNTL_H  | 
21  |  | #include <fcntl.h>  | 
22  |  | #endif  | 
23  |  | #ifdef HAVE_UNISTD_H  | 
24  |  | #include <unistd.h>  | 
25  |  | #elif defined (_WIN32)  | 
26  |  | #include <io.h>  | 
27  |  | #endif  | 
28  |  | #ifdef LIBXML_ZLIB_ENABLED  | 
29  |  | #include <zlib.h>  | 
30  |  | #endif  | 
31  |  | #ifdef LIBXML_LZMA_ENABLED  | 
32  |  | #include <lzma.h>  | 
33  |  | #endif  | 
34  |  |  | 
35  |  | #include "private/xzlib.h"  | 
36  |  | #include <libxml/xmlmemory.h>  | 
37  |  |  | 
38  |  | /* values for xz_state how */  | 
39  | 0  | #define LOOK 0                  /* look for a gzip/lzma header */  | 
40  | 0  | #define COPY 1                  /* copy input directly */  | 
41  | 0  | #define GZIP 2                  /* decompress a gzip stream */  | 
42  | 0  | #define LZMA 3                  /* decompress a lzma stream */  | 
43  |  |  | 
44  |  | /* internal lzma file state data structure */  | 
45  |  | typedef struct { | 
46  |  |     int mode;                   /* see lzma modes above */  | 
47  |  |     int fd;                     /* file descriptor */  | 
48  |  |     char *path;                 /* path or fd for error messages */  | 
49  |  |     uint64_t pos;               /* current position in uncompressed data */  | 
50  |  |     unsigned int size;          /* buffer size, zero if not allocated yet */  | 
51  |  |     unsigned int want;          /* requested buffer size, default is BUFSIZ */  | 
52  |  |     unsigned char *in;          /* input buffer */  | 
53  |  |     unsigned char *out;         /* output buffer (double-sized when reading) */  | 
54  |  |     unsigned char *next;        /* next output data to deliver or write */  | 
55  |  |     unsigned int have;          /* amount of output data unused at next */  | 
56  |  |     int eof;                    /* true if end of input file reached */  | 
57  |  |     uint64_t start;             /* where the lzma data started, for rewinding */  | 
58  |  |     uint64_t raw;               /* where the raw data started, for seeking */  | 
59  |  |     int how;                    /* 0: get header, 1: copy, 2: decompress */  | 
60  |  |     int direct;                 /* true if last read direct, false if lzma */  | 
61  |  |     /* seek request */  | 
62  |  |     uint64_t skip;              /* amount to skip (already rewound if backwards) */  | 
63  |  |     int seek;                   /* true if seek request pending */  | 
64  |  |     /* error information */  | 
65  |  |     int err;                    /* error code */  | 
66  |  |     char *msg;                  /* error message */  | 
67  |  |     /* lzma stream */  | 
68  |  |     int init;                   /* is the inflate stream initialized */  | 
69  |  |     lzma_stream strm;           /* stream structure in-place (not a pointer) */  | 
70  |  |     char padding1[32];          /* padding allowing to cope with possible  | 
71  |  |                                    extensions of above structure without  | 
72  |  |            too much side effect */  | 
73  |  | #ifdef LIBXML_ZLIB_ENABLED  | 
74  |  |     /* zlib inflate or deflate stream */  | 
75  |  |     z_stream zstrm;             /* stream structure in-place (not a pointer) */  | 
76  |  | #endif  | 
77  |  |     char padding2[32];          /* padding allowing to cope with possible  | 
78  |  |                                    extensions of above structure without  | 
79  |  |            too much side effect */  | 
80  |  | } xz_state, *xz_statep;  | 
81  |  |  | 
82  |  | static void  | 
83  |  | xz_error(xz_statep state, int err, const char *msg)  | 
84  | 0  | { | 
85  |  |     /* free previously allocated message and clear */  | 
86  | 0  |     if (state->msg != NULL) { | 
87  | 0  |         if (state->err != LZMA_MEM_ERROR)  | 
88  | 0  |             xmlFree(state->msg);  | 
89  | 0  |         state->msg = NULL;  | 
90  | 0  |     }  | 
91  |  |  | 
92  |  |     /* set error code, and if no message, then done */  | 
93  | 0  |     state->err = err;  | 
94  | 0  |     if (msg == NULL)  | 
95  | 0  |         return;  | 
96  |  |  | 
97  |  |     /* for an out of memory error, save as static string */  | 
98  | 0  |     if (err == LZMA_MEM_ERROR) { | 
99  | 0  |         state->msg = (char *) msg;  | 
100  | 0  |         return;  | 
101  | 0  |     }  | 
102  |  |  | 
103  |  |     /* construct error message with path */  | 
104  | 0  |     if ((state->msg =  | 
105  | 0  |          xmlMalloc(strlen(state->path) + strlen(msg) + 3)) == NULL) { | 
106  | 0  |         state->err = LZMA_MEM_ERROR;  | 
107  | 0  |         state->msg = (char *) "out of memory";  | 
108  | 0  |         return;  | 
109  | 0  |     }  | 
110  | 0  |     strcpy(state->msg, state->path);  | 
111  | 0  |     strcat(state->msg, ": ");  | 
112  | 0  |     strcat(state->msg, msg);  | 
113  | 0  |     return;  | 
114  | 0  | }  | 
115  |  |  | 
116  |  | static void  | 
117  |  | xz_reset(xz_statep state)  | 
118  | 0  | { | 
119  | 0  |     state->have = 0;            /* no output data available */  | 
120  | 0  |     state->eof = 0;             /* not at end of file */  | 
121  | 0  |     state->how = LOOK;          /* look for gzip header */  | 
122  | 0  |     state->direct = 1;          /* default for empty file */  | 
123  | 0  |     state->seek = 0;            /* no seek request pending */  | 
124  | 0  |     xz_error(state, LZMA_OK, NULL);     /* clear error */  | 
125  | 0  |     state->pos = 0;             /* no uncompressed data yet */  | 
126  | 0  |     state->strm.avail_in = 0;   /* no input data yet */  | 
127  | 0  | #ifdef LIBXML_ZLIB_ENABLED  | 
128  | 0  |     state->zstrm.avail_in = 0;  /* no input data yet */  | 
129  | 0  | #endif  | 
130  | 0  | }  | 
131  |  |  | 
132  |  | static xzFile  | 
133  |  | xz_open(const char *path, int fd, const char *mode ATTRIBUTE_UNUSED)  | 
134  | 0  | { | 
135  | 0  |     xz_statep state;  | 
136  |  |  | 
137  |  |     /* allocate xzFile structure to return */  | 
138  | 0  |     state = xmlMalloc(sizeof(xz_state));  | 
139  | 0  |     if (state == NULL)  | 
140  | 0  |         return NULL;  | 
141  | 0  |     state->size = 0;            /* no buffers allocated yet */  | 
142  | 0  |     state->want = BUFSIZ;       /* requested buffer size */  | 
143  | 0  |     state->msg = NULL;          /* no error message yet */  | 
144  | 0  |     state->init = 0;            /* initialization of zlib data */  | 
145  |  |  | 
146  |  |     /* save the path name for error messages */  | 
147  | 0  |     state->path = xmlMalloc(strlen(path) + 1);  | 
148  | 0  |     if (state->path == NULL) { | 
149  | 0  |         xmlFree(state);  | 
150  | 0  |         return NULL;  | 
151  | 0  |     }  | 
152  | 0  |     strcpy(state->path, path);  | 
153  |  |  | 
154  |  |     /* open the file with the appropriate mode (or just use fd) */  | 
155  | 0  |     state->fd = fd != -1 ? fd : open(path,  | 
156  |  | #ifdef O_LARGEFILE  | 
157  |  |                                      O_LARGEFILE |  | 
158  |  | #endif  | 
159  |  | #ifdef O_BINARY  | 
160  |  |                                      O_BINARY |  | 
161  |  | #endif  | 
162  | 0  |                                      O_RDONLY, 0666);  | 
163  | 0  |     if (state->fd == -1) { | 
164  | 0  |         xmlFree(state->path);  | 
165  | 0  |         xmlFree(state);  | 
166  | 0  |         return NULL;  | 
167  | 0  |     }  | 
168  |  |  | 
169  |  |     /* save the current position for rewinding (only if reading) */  | 
170  | 0  |     state->start = lseek(state->fd, 0, SEEK_CUR);  | 
171  | 0  |     if (state->start == (uint64_t) - 1)  | 
172  | 0  |         state->start = 0;  | 
173  |  |  | 
174  |  |     /* initialize stream */  | 
175  | 0  |     xz_reset(state);  | 
176  |  |  | 
177  |  |     /* return stream */  | 
178  | 0  |     return (xzFile) state;  | 
179  | 0  | }  | 
180  |  |  | 
181  |  | static int  | 
182  | 0  | xz_compressed(xzFile f) { | 
183  | 0  |     xz_statep state;  | 
184  |  | 
  | 
185  | 0  |     if (f == NULL)  | 
186  | 0  |         return(-1);  | 
187  | 0  |     state = (xz_statep) f;  | 
188  | 0  |     if (state->init <= 0)  | 
189  | 0  |         return(-1);  | 
190  |  |  | 
191  | 0  |     switch (state->how) { | 
192  | 0  |         case COPY:  | 
193  | 0  |       return(0);  | 
194  | 0  |   case GZIP:  | 
195  | 0  |   case LZMA:  | 
196  | 0  |       return(1);  | 
197  | 0  |     }  | 
198  | 0  |     return(-1);  | 
199  | 0  | }  | 
200  |  |  | 
201  |  | xzFile  | 
202  |  | __libxml2_xzopen(const char *path, const char *mode)  | 
203  | 0  | { | 
204  | 0  |     return xz_open(path, -1, mode);  | 
205  | 0  | }  | 
206  |  |  | 
207  |  | int  | 
208  | 0  | __libxml2_xzcompressed(xzFile f) { | 
209  | 0  |     return xz_compressed(f);  | 
210  | 0  | }  | 
211  |  |  | 
212  |  | xzFile  | 
213  |  | __libxml2_xzdopen(int fd, const char *mode)  | 
214  | 0  | { | 
215  | 0  |     char *path;                 /* identifier for error messages */  | 
216  | 0  |     xzFile xz;  | 
217  |  | 
  | 
218  | 0  |     if (fd == -1 || (path = xmlMalloc(7 + 3 * sizeof(int))) == NULL)  | 
219  | 0  |         return NULL;  | 
220  | 0  |     sprintf(path, "<fd:%d>", fd);       /* for debugging */  | 
221  | 0  |     xz = xz_open(path, fd, mode);  | 
222  | 0  |     xmlFree(path);  | 
223  | 0  |     return xz;  | 
224  | 0  | }  | 
225  |  |  | 
226  |  | static int  | 
227  |  | xz_load(xz_statep state, unsigned char *buf, unsigned int len,  | 
228  |  |         unsigned int *have)  | 
229  | 0  | { | 
230  | 0  |     int ret;  | 
231  |  | 
  | 
232  | 0  |     *have = 0;  | 
233  | 0  |     do { | 
234  | 0  |         ret = read(state->fd, buf + *have, len - *have);  | 
235  | 0  |         if (ret <= 0)  | 
236  | 0  |             break;  | 
237  | 0  |         *have += ret;  | 
238  | 0  |     } while (*have < len);  | 
239  | 0  |     if (ret < 0) { | 
240  | 0  |         xz_error(state, -1, strerror(errno));  | 
241  | 0  |         return -1;  | 
242  | 0  |     }  | 
243  | 0  |     if (ret == 0)  | 
244  | 0  |         state->eof = 1;  | 
245  | 0  |     return 0;  | 
246  | 0  | }  | 
247  |  |  | 
248  |  | static int  | 
249  |  | xz_avail(xz_statep state)  | 
250  | 0  | { | 
251  | 0  |     lzma_stream *strm = &(state->strm);  | 
252  |  | 
  | 
253  | 0  |     if (state->err != LZMA_OK)  | 
254  | 0  |         return -1;  | 
255  | 0  |     if (state->eof == 0) { | 
256  |  |         /* avail_in is size_t, which is not necessary sizeof(unsigned) */  | 
257  | 0  |         unsigned tmp = strm->avail_in;  | 
258  |  | 
  | 
259  | 0  |         if (xz_load(state, state->in, state->size, &tmp) == -1) { | 
260  | 0  |             strm->avail_in = tmp;  | 
261  | 0  |             return -1;  | 
262  | 0  |         }  | 
263  | 0  |         strm->avail_in = tmp;  | 
264  | 0  |         strm->next_in = state->in;  | 
265  | 0  |     }  | 
266  | 0  |     return 0;  | 
267  | 0  | }  | 
268  |  |  | 
269  |  | #ifdef LIBXML_ZLIB_ENABLED  | 
270  |  | static int  | 
271  |  | xz_avail_zstrm(xz_statep state)  | 
272  | 0  | { | 
273  | 0  |     int ret;  | 
274  | 0  |     state->strm.avail_in = state->zstrm.avail_in;  | 
275  | 0  |     state->strm.next_in = state->zstrm.next_in;  | 
276  | 0  |     ret = xz_avail(state);  | 
277  | 0  |     state->zstrm.avail_in = (uInt) state->strm.avail_in;  | 
278  | 0  |     state->zstrm.next_in = (Bytef *) state->strm.next_in;  | 
279  | 0  |     return ret;  | 
280  | 0  | }  | 
281  |  | #endif  | 
282  |  |  | 
283  |  | static int  | 
284  |  | is_format_xz(xz_statep state)  | 
285  | 0  | { | 
286  | 0  |     lzma_stream *strm = &(state->strm);  | 
287  |  | 
  | 
288  | 0  |     return strm->avail_in >= 6 && memcmp(state->in, "\3757zXZ", 6) == 0;  | 
289  | 0  | }  | 
290  |  |  | 
291  |  | static int  | 
292  |  | is_format_lzma(xz_statep state)  | 
293  | 0  | { | 
294  | 0  |     lzma_stream *strm = &(state->strm);  | 
295  |  | 
  | 
296  | 0  |     lzma_filter filter;  | 
297  | 0  |     lzma_options_lzma *opt;  | 
298  | 0  |     uint32_t dict_size;  | 
299  | 0  |     uint64_t uncompressed_size;  | 
300  | 0  |     size_t i;  | 
301  |  | 
  | 
302  | 0  |     if (strm->avail_in < 13)  | 
303  | 0  |         return 0;  | 
304  |  |  | 
305  | 0  |     filter.id = LZMA_FILTER_LZMA1;  | 
306  | 0  |     if (lzma_properties_decode(&filter, NULL, state->in, 5) != LZMA_OK)  | 
307  | 0  |         return 0;  | 
308  |  |  | 
309  | 0  |     opt = filter.options;  | 
310  | 0  |     dict_size = opt->dict_size;  | 
311  | 0  |     free(opt); /* we can't use xmlFree on a string returned by zlib */  | 
312  |  |  | 
313  |  |     /* A hack to ditch tons of false positives: We allow only dictionary  | 
314  |  |      * sizes that are 2^n or 2^n + 2^(n-1) or UINT32_MAX. LZMA_Alone  | 
315  |  |      * created only files with 2^n, but accepts any dictionary size.  | 
316  |  |      * If someone complains, this will be reconsidered.  | 
317  |  |      */  | 
318  | 0  |     if (dict_size != UINT32_MAX) { | 
319  | 0  |         uint32_t d = dict_size - 1;  | 
320  |  | 
  | 
321  | 0  |         d |= d >> 2;  | 
322  | 0  |         d |= d >> 3;  | 
323  | 0  |         d |= d >> 4;  | 
324  | 0  |         d |= d >> 8;  | 
325  | 0  |         d |= d >> 16;  | 
326  | 0  |         ++d;  | 
327  | 0  |         if (d != dict_size || dict_size == 0)  | 
328  | 0  |             return 0;  | 
329  | 0  |     }  | 
330  |  |  | 
331  |  |     /* Another hack to ditch false positives: Assume that if the  | 
332  |  |      * uncompressed size is known, it must be less than 256 GiB.  | 
333  |  |      * Again, if someone complains, this will be reconsidered.  | 
334  |  |      */  | 
335  | 0  |     uncompressed_size = 0;  | 
336  | 0  |     for (i = 0; i < 8; ++i)  | 
337  | 0  |         uncompressed_size |= (uint64_t) (state->in[5 + i]) << (i * 8);  | 
338  |  | 
  | 
339  | 0  |     if (uncompressed_size != UINT64_MAX  | 
340  | 0  |         && uncompressed_size > (UINT64_C(1) << 38))  | 
341  | 0  |         return 0;  | 
342  |  |  | 
343  | 0  |     return 1;  | 
344  | 0  | }  | 
345  |  |  | 
346  |  | #ifdef LIBXML_ZLIB_ENABLED  | 
347  |  |  | 
348  |  | /* Get next byte from input, or -1 if end or error. */  | 
349  | 0  | #define NEXT() ((strm->avail_in == 0 && xz_avail(state) == -1) ? -1 : \  | 
350  | 0  |                 (strm->avail_in == 0 ? -1 : \  | 
351  | 0  |                  (strm->avail_in--, *(strm->next_in)++)))  | 
352  |  | /* Same thing, but from zstrm */  | 
353  | 0  | #define NEXTZ() ((strm->avail_in == 0 && xz_avail_zstrm(state) == -1) ? -1 : \  | 
354  | 0  |                 (strm->avail_in == 0 ? -1 : \  | 
355  | 0  |                  (strm->avail_in--, *(strm->next_in)++)))  | 
356  |  |  | 
357  |  | /* Get a four-byte little-endian integer and return 0 on success and the value  | 
358  |  |    in *ret.  Otherwise -1 is returned and *ret is not modified. */  | 
359  |  | static int  | 
360  |  | gz_next4(xz_statep state, unsigned long *ret)  | 
361  | 0  | { | 
362  | 0  |     int ch;  | 
363  | 0  |     unsigned long val;  | 
364  | 0  |     z_streamp strm = &(state->zstrm);  | 
365  |  | 
  | 
366  | 0  |     val = NEXTZ();  | 
367  | 0  |     val += (unsigned) NEXTZ() << 8;  | 
368  | 0  |     val += (unsigned long) NEXTZ() << 16;  | 
369  | 0  |     ch = NEXTZ();  | 
370  | 0  |     if (ch == -1)  | 
371  | 0  |         return -1;  | 
372  | 0  |     val += (unsigned long) ch << 24;  | 
373  | 0  |     *ret = val;  | 
374  | 0  |     return 0;  | 
375  | 0  | }  | 
376  |  | #endif  | 
377  |  |  | 
378  |  | static int  | 
379  |  | xz_head(xz_statep state)  | 
380  | 0  | { | 
381  | 0  |     lzma_stream *strm = &(state->strm);  | 
382  | 0  |     lzma_stream init = LZMA_STREAM_INIT;  | 
383  | 0  |     int flags;  | 
384  | 0  |     unsigned len;  | 
385  |  |  | 
386  |  |     /* Avoid unused variable warning if features are disabled. */  | 
387  | 0  |     (void) flags;  | 
388  | 0  |     (void) len;  | 
389  |  |  | 
390  |  |     /* allocate read buffers and inflate memory */  | 
391  | 0  |     if (state->size == 0) { | 
392  |  |         /* allocate buffers */  | 
393  | 0  |         state->in = xmlMalloc(state->want);  | 
394  | 0  |         state->out = xmlMalloc(state->want << 1);  | 
395  | 0  |         if (state->in == NULL || state->out == NULL) { | 
396  | 0  |             if (state->out != NULL)  | 
397  | 0  |                 xmlFree(state->out);  | 
398  | 0  |             if (state->in != NULL)  | 
399  | 0  |                 xmlFree(state->in);  | 
400  | 0  |             xz_error(state, LZMA_MEM_ERROR, "out of memory");  | 
401  | 0  |             return -1;  | 
402  | 0  |         }  | 
403  | 0  |         state->size = state->want;  | 
404  |  |  | 
405  |  |         /* allocate decoder memory */  | 
406  | 0  |         state->strm = init;  | 
407  | 0  |         state->strm.avail_in = 0;  | 
408  | 0  |         state->strm.next_in = NULL;  | 
409  | 0  |         if (lzma_auto_decoder(&state->strm, 100000000, 0) != LZMA_OK) { | 
410  | 0  |             xmlFree(state->out);  | 
411  | 0  |             xmlFree(state->in);  | 
412  | 0  |             state->size = 0;  | 
413  | 0  |             xz_error(state, LZMA_MEM_ERROR, "out of memory");  | 
414  | 0  |             return -1;  | 
415  | 0  |         }  | 
416  | 0  | #ifdef LIBXML_ZLIB_ENABLED  | 
417  |  |         /* allocate inflate memory */  | 
418  | 0  |         state->zstrm.zalloc = Z_NULL;  | 
419  | 0  |         state->zstrm.zfree = Z_NULL;  | 
420  | 0  |         state->zstrm.opaque = Z_NULL;  | 
421  | 0  |         state->zstrm.avail_in = 0;  | 
422  | 0  |         state->zstrm.next_in = Z_NULL;  | 
423  | 0  |         if (state->init == 0) { | 
424  | 0  |             if (inflateInit2(&(state->zstrm), -15) != Z_OK) {/* raw inflate */ | 
425  | 0  |                 xmlFree(state->out);  | 
426  | 0  |                 xmlFree(state->in);  | 
427  | 0  |                 state->size = 0;  | 
428  | 0  |                 xz_error(state, LZMA_MEM_ERROR, "out of memory");  | 
429  | 0  |                 return -1;  | 
430  | 0  |             }  | 
431  | 0  |             state->init = 1;  | 
432  | 0  |         }  | 
433  | 0  | #endif  | 
434  | 0  |     }  | 
435  |  |  | 
436  |  |     /* get some data in the input buffer */  | 
437  | 0  |     if (strm->avail_in == 0) { | 
438  | 0  |         if (xz_avail(state) == -1)  | 
439  | 0  |             return -1;  | 
440  | 0  |         if (strm->avail_in == 0)  | 
441  | 0  |             return 0;  | 
442  | 0  |     }  | 
443  |  |  | 
444  |  |     /* look for the xz magic header bytes */  | 
445  | 0  |     if (is_format_xz(state) || is_format_lzma(state)) { | 
446  | 0  |         state->how = LZMA;  | 
447  | 0  |         state->direct = 0;  | 
448  | 0  |         return 0;  | 
449  | 0  |     }  | 
450  | 0  | #ifdef LIBXML_ZLIB_ENABLED  | 
451  |  |     /* look for the gzip magic header bytes 31 and 139 */  | 
452  | 0  |     if (strm->next_in[0] == 31) { | 
453  | 0  |         strm->avail_in--;  | 
454  | 0  |         strm->next_in++;  | 
455  | 0  |         if (strm->avail_in == 0 && xz_avail(state) == -1)  | 
456  | 0  |             return -1;  | 
457  | 0  |         if (strm->avail_in && strm->next_in[0] == 139) { | 
458  |  |             /* we have a gzip header, woo hoo! */  | 
459  | 0  |             strm->avail_in--;  | 
460  | 0  |             strm->next_in++;  | 
461  |  |  | 
462  |  |             /* skip rest of header */  | 
463  | 0  |             if (NEXT() != 8) {  /* compression method */ | 
464  | 0  |                 xz_error(state, LZMA_DATA_ERROR,  | 
465  | 0  |                          "unknown compression method");  | 
466  | 0  |                 return -1;  | 
467  | 0  |             }  | 
468  | 0  |             flags = NEXT();  | 
469  | 0  |             if (flags & 0xe0) { /* reserved flag bits */ | 
470  | 0  |                 xz_error(state, LZMA_DATA_ERROR,  | 
471  | 0  |                          "unknown header flags set");  | 
472  | 0  |                 return -1;  | 
473  | 0  |             }  | 
474  | 0  |             NEXT();             /* modification time */  | 
475  | 0  |             NEXT();  | 
476  | 0  |             NEXT();  | 
477  | 0  |             NEXT();  | 
478  | 0  |             NEXT();             /* extra flags */  | 
479  | 0  |             NEXT();             /* operating system */  | 
480  | 0  |             if (flags & 4) {    /* extra field */ | 
481  | 0  |                 len = (unsigned) NEXT();  | 
482  | 0  |                 len += (unsigned) NEXT() << 8;  | 
483  | 0  |                 while (len--)  | 
484  | 0  |                     if (NEXT() < 0)  | 
485  | 0  |                         break;  | 
486  | 0  |             }  | 
487  | 0  |             if (flags & 8)      /* file name */  | 
488  | 0  |                 while (NEXT() > 0) ;  | 
489  | 0  |             if (flags & 16)     /* comment */  | 
490  | 0  |                 while (NEXT() > 0) ;  | 
491  | 0  |             if (flags & 2) {    /* header crc */ | 
492  | 0  |                 NEXT();  | 
493  | 0  |                 NEXT();  | 
494  | 0  |             }  | 
495  |  |             /* an unexpected end of file is not checked for here -- it will be  | 
496  |  |              * noticed on the first request for uncompressed data */  | 
497  |  |  | 
498  |  |             /* set up for decompression */  | 
499  | 0  |             inflateReset(&state->zstrm);  | 
500  | 0  |             state->zstrm.adler = crc32(0L, Z_NULL, 0);  | 
501  | 0  |             state->how = GZIP;  | 
502  | 0  |             state->direct = 0;  | 
503  | 0  |             return 0;  | 
504  | 0  |         } else { | 
505  |  |             /* not a gzip file -- save first byte (31) and fall to raw i/o */  | 
506  | 0  |             state->out[0] = 31;  | 
507  | 0  |             state->have = 1;  | 
508  | 0  |         }  | 
509  | 0  |     }  | 
510  | 0  | #endif  | 
511  |  |  | 
512  |  |     /* doing raw i/o, save start of raw data for seeking, copy any leftover  | 
513  |  |      * input to output -- this assumes that the output buffer is larger than  | 
514  |  |      * the input buffer, which also assures space for gzungetc() */  | 
515  | 0  |     state->raw = state->pos;  | 
516  | 0  |     state->next = state->out;  | 
517  | 0  |     if (strm->avail_in) { | 
518  | 0  |         memcpy(state->next + state->have, strm->next_in, strm->avail_in);  | 
519  | 0  |         state->have += strm->avail_in;  | 
520  | 0  |         strm->avail_in = 0;  | 
521  | 0  |     }  | 
522  | 0  |     state->how = COPY;  | 
523  | 0  |     state->direct = 1;  | 
524  | 0  |     return 0;  | 
525  | 0  | }  | 
526  |  |  | 
527  |  | static int  | 
528  |  | xz_decomp(xz_statep state)  | 
529  | 0  | { | 
530  | 0  |     int ret;  | 
531  | 0  |     unsigned had;  | 
532  | 0  |     unsigned long crc, len;  | 
533  | 0  |     lzma_stream *strm = &(state->strm);  | 
534  |  | 
  | 
535  | 0  |     lzma_action action = LZMA_RUN;  | 
536  |  |  | 
537  |  |     /* Avoid unused variable warning if features are disabled. */  | 
538  | 0  |     (void) crc;  | 
539  | 0  |     (void) len;  | 
540  |  |  | 
541  |  |     /* fill output buffer up to end of deflate stream */  | 
542  | 0  |     had = strm->avail_out;  | 
543  | 0  |     do { | 
544  |  |         /* get more input for inflate() */  | 
545  | 0  |         if (strm->avail_in == 0 && xz_avail(state) == -1)  | 
546  | 0  |             return -1;  | 
547  | 0  |         if (strm->avail_in == 0) { | 
548  | 0  |             xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");  | 
549  | 0  |             return -1;  | 
550  | 0  |         }  | 
551  | 0  |         if (state->eof)  | 
552  | 0  |             action = LZMA_FINISH;  | 
553  |  |  | 
554  |  |         /* decompress and handle errors */  | 
555  | 0  | #ifdef LIBXML_ZLIB_ENABLED  | 
556  | 0  |         if (state->how == GZIP) { | 
557  | 0  |             state->zstrm.avail_in = (uInt) state->strm.avail_in;  | 
558  | 0  |             state->zstrm.next_in = (Bytef *) state->strm.next_in;  | 
559  | 0  |             state->zstrm.avail_out = (uInt) state->strm.avail_out;  | 
560  | 0  |             state->zstrm.next_out = (Bytef *) state->strm.next_out;  | 
561  | 0  |             ret = inflate(&state->zstrm, Z_NO_FLUSH);  | 
562  | 0  |             if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) { | 
563  | 0  |                 xz_error(state, Z_STREAM_ERROR,  | 
564  | 0  |                          "internal error: inflate stream corrupt");  | 
565  | 0  |                 return -1;  | 
566  | 0  |             }  | 
567  |  |             /*  | 
568  |  |              * FIXME: Remapping a couple of error codes and falling through  | 
569  |  |              * to the LZMA error handling looks fragile.  | 
570  |  |              */  | 
571  | 0  |             if (ret == Z_MEM_ERROR)  | 
572  | 0  |                 ret = LZMA_MEM_ERROR;  | 
573  | 0  |             if (ret == Z_DATA_ERROR)  | 
574  | 0  |                 ret = LZMA_DATA_ERROR;  | 
575  | 0  |             if (ret == Z_STREAM_END)  | 
576  | 0  |                 ret = LZMA_STREAM_END;  | 
577  | 0  |             state->strm.avail_in = state->zstrm.avail_in;  | 
578  | 0  |             state->strm.next_in = state->zstrm.next_in;  | 
579  | 0  |             state->strm.avail_out = state->zstrm.avail_out;  | 
580  | 0  |             state->strm.next_out = state->zstrm.next_out;  | 
581  | 0  |         } else                  /* state->how == LZMA */  | 
582  | 0  | #endif  | 
583  | 0  |             ret = lzma_code(strm, action);  | 
584  | 0  |         if (ret == LZMA_MEM_ERROR) { | 
585  | 0  |             xz_error(state, LZMA_MEM_ERROR, "out of memory");  | 
586  | 0  |             return -1;  | 
587  | 0  |         }  | 
588  | 0  |         if (ret == LZMA_DATA_ERROR) { | 
589  | 0  |             xz_error(state, LZMA_DATA_ERROR, "compressed data error");  | 
590  | 0  |             return -1;  | 
591  | 0  |         }  | 
592  | 0  |         if (ret == LZMA_PROG_ERROR) { | 
593  | 0  |             xz_error(state, LZMA_PROG_ERROR, "compression error");  | 
594  | 0  |             return -1;  | 
595  | 0  |         }  | 
596  | 0  |         if ((state->how != GZIP) &&  | 
597  | 0  |             (ret != LZMA_OK) && (ret != LZMA_STREAM_END)) { | 
598  | 0  |             xz_error(state, ret, "lzma error");  | 
599  | 0  |             return -1;  | 
600  | 0  |         }  | 
601  | 0  |     } while (strm->avail_out && ret != LZMA_STREAM_END);  | 
602  |  |  | 
603  |  |     /* update available output and crc check value */  | 
604  | 0  |     state->have = had - strm->avail_out;  | 
605  | 0  |     state->next = strm->next_out - state->have;  | 
606  | 0  | #ifdef LIBXML_ZLIB_ENABLED  | 
607  | 0  |     state->zstrm.adler =  | 
608  | 0  |         crc32(state->zstrm.adler, state->next, state->have);  | 
609  | 0  | #endif  | 
610  |  | 
  | 
611  | 0  |     if (ret == LZMA_STREAM_END) { | 
612  | 0  | #ifdef LIBXML_ZLIB_ENABLED  | 
613  | 0  |         if (state->how == GZIP) { | 
614  | 0  |             if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) { | 
615  | 0  |                 xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");  | 
616  | 0  |                 return -1;  | 
617  | 0  |             }  | 
618  | 0  |             if (crc != state->zstrm.adler) { | 
619  | 0  |                 xz_error(state, LZMA_DATA_ERROR, "incorrect data check");  | 
620  | 0  |                 return -1;  | 
621  | 0  |             }  | 
622  | 0  |             if (len != (state->zstrm.total_out & 0xffffffffL)) { | 
623  | 0  |                 xz_error(state, LZMA_DATA_ERROR, "incorrect length check");  | 
624  | 0  |                 return -1;  | 
625  | 0  |             }  | 
626  | 0  |             state->strm.avail_in = 0;  | 
627  | 0  |             state->strm.next_in = NULL;  | 
628  | 0  |             state->strm.avail_out = 0;  | 
629  | 0  |             state->strm.next_out = NULL;  | 
630  | 0  |         } else  | 
631  | 0  | #endif  | 
632  | 0  |         if (strm->avail_in != 0 || !state->eof) { | 
633  | 0  |             xz_error(state, LZMA_DATA_ERROR, "trailing garbage");  | 
634  | 0  |             return -1;  | 
635  | 0  |         }  | 
636  | 0  |         state->how = LOOK;      /* ready for next stream, once have is 0 (leave  | 
637  |  |                                  * state->direct unchanged to remember how) */  | 
638  | 0  |     }  | 
639  |  |  | 
640  |  |     /* good decompression */  | 
641  | 0  |     return 0;  | 
642  | 0  | }  | 
643  |  |  | 
644  |  | static int  | 
645  |  | xz_make(xz_statep state)  | 
646  | 0  | { | 
647  | 0  |     lzma_stream *strm = &(state->strm);  | 
648  |  | 
  | 
649  | 0  |     if (state->how == LOOK) {   /* look for lzma / gzip header */ | 
650  | 0  |         if (xz_head(state) == -1)  | 
651  | 0  |             return -1;  | 
652  | 0  |         if (state->have)        /* got some data from xz_head() */  | 
653  | 0  |             return 0;  | 
654  | 0  |     }  | 
655  | 0  |     if (state->how == COPY) {   /* straight copy */ | 
656  | 0  |         if (xz_load(state, state->out, state->size << 1, &(state->have)) ==  | 
657  | 0  |             -1)  | 
658  | 0  |             return -1;  | 
659  | 0  |         state->next = state->out;  | 
660  | 0  |     } else if (state->how == LZMA || state->how == GZIP) {      /* decompress */ | 
661  | 0  |         strm->avail_out = state->size << 1;  | 
662  | 0  |         strm->next_out = state->out;  | 
663  | 0  |         if (xz_decomp(state) == -1)  | 
664  | 0  |             return -1;  | 
665  | 0  |     }  | 
666  | 0  |     return 0;  | 
667  | 0  | }  | 
668  |  |  | 
669  |  | static int  | 
670  |  | xz_skip(xz_statep state, uint64_t len)  | 
671  | 0  | { | 
672  | 0  |     unsigned n;  | 
673  |  |  | 
674  |  |     /* skip over len bytes or reach end-of-file, whichever comes first */  | 
675  | 0  |     while (len)  | 
676  |  |         /* skip over whatever is in output buffer */  | 
677  | 0  |         if (state->have) { | 
678  | 0  |             n = (uint64_t) state->have > len ?  | 
679  | 0  |                 (unsigned) len : state->have;  | 
680  | 0  |             state->have -= n;  | 
681  | 0  |             state->next += n;  | 
682  | 0  |             state->pos += n;  | 
683  | 0  |             len -= n;  | 
684  | 0  |         }  | 
685  |  |  | 
686  |  |     /* output buffer empty -- return if we're at the end of the input */  | 
687  | 0  |         else if (state->eof && state->strm.avail_in == 0)  | 
688  | 0  |             break;  | 
689  |  |  | 
690  |  |     /* need more data to skip -- load up output buffer */  | 
691  | 0  |         else { | 
692  |  |             /* get more output, looking for header if required */  | 
693  | 0  |             if (xz_make(state) == -1)  | 
694  | 0  |                 return -1;  | 
695  | 0  |         }  | 
696  | 0  |     return 0;  | 
697  | 0  | }  | 
698  |  |  | 
699  |  | int  | 
700  |  | __libxml2_xzread(xzFile file, void *buf, unsigned len)  | 
701  | 0  | { | 
702  | 0  |     unsigned got, n;  | 
703  | 0  |     xz_statep state;  | 
704  | 0  |     lzma_stream *strm;  | 
705  |  |  | 
706  |  |     /* get internal structure */  | 
707  | 0  |     if (file == NULL)  | 
708  | 0  |         return -1;  | 
709  | 0  |     state = (xz_statep) file;  | 
710  | 0  |     strm = &(state->strm);  | 
711  |  |  | 
712  |  |     /* check that we're reading and that there's no error */  | 
713  | 0  |     if (state->err != LZMA_OK)  | 
714  | 0  |         return -1;  | 
715  |  |  | 
716  |  |     /* since an int is returned, make sure len fits in one, otherwise return  | 
717  |  |      * with an error (this avoids the flaw in the interface) */  | 
718  | 0  |     if ((int) len < 0) { | 
719  | 0  |         xz_error(state, LZMA_BUF_ERROR,  | 
720  | 0  |                  "requested length does not fit in int");  | 
721  | 0  |         return -1;  | 
722  | 0  |     }  | 
723  |  |  | 
724  |  |     /* if len is zero, avoid unnecessary operations */  | 
725  | 0  |     if (len == 0)  | 
726  | 0  |         return 0;  | 
727  |  |  | 
728  |  |     /* process a skip request */  | 
729  | 0  |     if (state->seek) { | 
730  | 0  |         state->seek = 0;  | 
731  | 0  |         if (xz_skip(state, state->skip) == -1)  | 
732  | 0  |             return -1;  | 
733  | 0  |     }  | 
734  |  |  | 
735  |  |     /* get len bytes to buf, or less than len if at the end */  | 
736  | 0  |     got = 0;  | 
737  | 0  |     do { | 
738  |  |         /* first just try copying data from the output buffer */  | 
739  | 0  |         if (state->have) { | 
740  | 0  |             n = state->have > len ? len : state->have;  | 
741  | 0  |             memcpy(buf, state->next, n);  | 
742  | 0  |             state->next += n;  | 
743  | 0  |             state->have -= n;  | 
744  | 0  |         }  | 
745  |  |  | 
746  |  |         /* output buffer empty -- return if we're at the end of the input */  | 
747  | 0  |         else if (state->eof && strm->avail_in == 0)  | 
748  | 0  |             break;  | 
749  |  |  | 
750  |  |         /* need output data -- for small len or new stream load up our output  | 
751  |  |          * buffer */  | 
752  | 0  |         else if (state->how == LOOK || len < (state->size << 1)) { | 
753  |  |             /* get more output, looking for header if required */  | 
754  | 0  |             if (xz_make(state) == -1)  | 
755  | 0  |                 return -1;  | 
756  | 0  |             continue;           /* no progress yet -- go back to memcpy() above */  | 
757  |  |             /* the copy above assures that we will leave with space in the  | 
758  |  |              * output buffer, allowing at least one gzungetc() to succeed */  | 
759  | 0  |         }  | 
760  |  |  | 
761  |  |         /* large len -- read directly into user buffer */  | 
762  | 0  |         else if (state->how == COPY) {  /* read directly */ | 
763  | 0  |             if (xz_load(state, buf, len, &n) == -1)  | 
764  | 0  |                 return -1;  | 
765  | 0  |         }  | 
766  |  |  | 
767  |  |         /* large len -- decompress directly into user buffer */  | 
768  | 0  |         else {                  /* state->how == LZMA */ | 
769  | 0  |             strm->avail_out = len;  | 
770  | 0  |             strm->next_out = buf;  | 
771  | 0  |             if (xz_decomp(state) == -1)  | 
772  | 0  |                 return -1;  | 
773  | 0  |             n = state->have;  | 
774  | 0  |             state->have = 0;  | 
775  | 0  |         }  | 
776  |  |  | 
777  |  |         /* update progress */  | 
778  | 0  |         len -= n;  | 
779  | 0  |         buf = (char *) buf + n;  | 
780  | 0  |         got += n;  | 
781  | 0  |         state->pos += n;  | 
782  | 0  |     } while (len);  | 
783  |  |  | 
784  |  |     /* return number of bytes read into user buffer (will fit in int) */  | 
785  | 0  |     return (int) got;  | 
786  | 0  | }  | 
787  |  |  | 
788  |  | int  | 
789  |  | __libxml2_xzclose(xzFile file)  | 
790  | 0  | { | 
791  | 0  |     int ret;  | 
792  | 0  |     xz_statep state;  | 
793  |  |  | 
794  |  |     /* get internal structure */  | 
795  | 0  |     if (file == NULL)  | 
796  | 0  |         return LZMA_DATA_ERROR;  | 
797  | 0  |     state = (xz_statep) file;  | 
798  |  |  | 
799  |  |     /* free memory and close file */  | 
800  | 0  |     if (state->size) { | 
801  | 0  |         lzma_end(&(state->strm));  | 
802  | 0  | #ifdef LIBXML_ZLIB_ENABLED  | 
803  | 0  |         if (state->init == 1)  | 
804  | 0  |             inflateEnd(&(state->zstrm));  | 
805  | 0  |         state->init = 0;  | 
806  | 0  | #endif  | 
807  | 0  |         xmlFree(state->out);  | 
808  | 0  |         xmlFree(state->in);  | 
809  | 0  |     }  | 
810  | 0  |     xmlFree(state->path);  | 
811  | 0  |     if ((state->msg != NULL) && (state->err != LZMA_MEM_ERROR))  | 
812  | 0  |         xmlFree(state->msg);  | 
813  | 0  |     ret = close(state->fd);  | 
814  | 0  |     xmlFree(state);  | 
815  | 0  |     return ret ? ret : LZMA_OK;  | 
816  | 0  | }  | 
817  |  | #endif /* LIBXML_LZMA_ENABLED */  |