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