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