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 */ |
Daniel Veillard | 72789ef | 2012-04-02 17:52:20 +0800 | [diff] [blame] | 53 | unsigned int size; /* buffer size, zero if not allocated yet */ |
| 54 | unsigned int want; /* requested buffer size, default is BUFSIZ */ |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 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 */ |
Daniel Veillard | 72789ef | 2012-04-02 17:52:20 +0800 | [diff] [blame] | 58 | unsigned int have; /* amount of output data unused at next */ |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 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 |
Daniel Veillard | 72789ef | 2012-04-02 17:52:20 +0800 | [diff] [blame] | 203 | xz_load(xz_statep state, unsigned char *buf, unsigned int len, |
| 204 | unsigned int *have) |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 205 | { |
| 206 | int ret; |
| 207 | |
| 208 | *have = 0; |
| 209 | do { |
| 210 | ret = read(state->fd, buf + *have, len - *have); |
| 211 | if (ret <= 0) |
| 212 | break; |
| 213 | *have += ret; |
| 214 | } while (*have < len); |
| 215 | if (ret < 0) { |
| 216 | xz_error(state, -1, strerror(errno)); |
| 217 | return -1; |
| 218 | } |
| 219 | if (ret == 0) |
| 220 | state->eof = 1; |
| 221 | return 0; |
| 222 | } |
| 223 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 224 | static int |
| 225 | xz_avail(xz_statep state) |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 226 | { |
| 227 | lzma_stream *strm = &(state->strm); |
| 228 | |
| 229 | if (state->err != LZMA_OK) |
| 230 | return -1; |
| 231 | if (state->eof == 0) { |
Marcus Meissner | 9964492 | 2012-05-07 18:41:42 +0800 | [diff] [blame^] | 232 | /* avail_in is size_t, which is not necessary sizeof(unsigned) */ |
| 233 | unsigned tmp = strm->avail_in; |
| 234 | |
| 235 | if (xz_load(state, state->in, state->size, &tmp) == -1) { |
| 236 | strm->avail_in = tmp; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 237 | return -1; |
Marcus Meissner | 9964492 | 2012-05-07 18:41:42 +0800 | [diff] [blame^] | 238 | } |
| 239 | strm->avail_in = tmp; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 240 | strm->next_in = state->in; |
| 241 | } |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int |
| 246 | is_format_xz(xz_statep state) |
| 247 | { |
| 248 | lzma_stream *strm = &(state->strm); |
| 249 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 250 | return strm->avail_in >= 6 && memcmp(state->in, "\3757zXZ", 6) == 0; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static int |
| 254 | is_format_lzma(xz_statep state) |
| 255 | { |
| 256 | lzma_stream *strm = &(state->strm); |
| 257 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 258 | lzma_filter filter; |
| 259 | lzma_options_lzma *opt; |
| 260 | uint32_t dict_size; |
| 261 | uint64_t uncompressed_size; |
| 262 | size_t i; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 263 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 264 | if (strm->avail_in < 13) |
| 265 | return 0; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 266 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 267 | filter.id = LZMA_FILTER_LZMA1; |
| 268 | if (lzma_properties_decode(&filter, NULL, state->in, 5) != LZMA_OK) |
| 269 | return 0; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 270 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 271 | opt = filter.options; |
| 272 | dict_size = opt->dict_size; |
| 273 | free(opt); |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 274 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 275 | /* A hack to ditch tons of false positives: We allow only dictionary |
| 276 | * sizes that are 2^n or 2^n + 2^(n-1) or UINT32_MAX. LZMA_Alone |
| 277 | * created only files with 2^n, but accepts any dictionary size. |
| 278 | * If someone complains, this will be reconsidered. |
| 279 | */ |
| 280 | if (dict_size != UINT32_MAX) { |
| 281 | uint32_t d = dict_size - 1; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 282 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 283 | d |= d >> 2; |
| 284 | d |= d >> 3; |
| 285 | d |= d >> 4; |
| 286 | d |= d >> 8; |
| 287 | d |= d >> 16; |
| 288 | ++d; |
| 289 | if (d != dict_size || dict_size == 0) |
| 290 | return 0; |
| 291 | } |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 292 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 293 | /* Another hack to ditch false positives: Assume that if the |
| 294 | * uncompressed size is known, it must be less than 256 GiB. |
| 295 | * Again, if someone complains, this will be reconsidered. |
| 296 | */ |
| 297 | uncompressed_size = 0; |
| 298 | for (i = 0; i < 8; ++i) |
| 299 | uncompressed_size |= (uint64_t) (state->in[5 + i]) << (i * 8); |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 300 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 301 | if (uncompressed_size != UINT64_MAX |
| 302 | && uncompressed_size > (UINT64_C(1) << 38)) |
| 303 | return 0; |
| 304 | |
| 305 | return 1; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 309 | |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 310 | /* Get next byte from input, or -1 if end or error. */ |
| 311 | #define NEXT() ((strm->avail_in == 0 && xz_avail(state) == -1) ? -1 : \ |
| 312 | (strm->avail_in == 0 ? -1 : \ |
| 313 | (strm->avail_in--, *(strm->next_in)++))) |
| 314 | |
| 315 | /* Get a four-byte little-endian integer and return 0 on success and the value |
| 316 | in *ret. Otherwise -1 is returned and *ret is not modified. */ |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 317 | static int |
| 318 | gz_next4(xz_statep state, unsigned long *ret) |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 319 | { |
| 320 | int ch; |
| 321 | unsigned long val; |
| 322 | z_streamp strm = &(state->zstrm); |
| 323 | |
| 324 | val = NEXT(); |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 325 | val += (unsigned) NEXT() << 8; |
| 326 | val += (unsigned long) NEXT() << 16; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 327 | ch = NEXT(); |
| 328 | if (ch == -1) |
| 329 | return -1; |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 330 | val += (unsigned long) ch << 24; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 331 | *ret = val; |
| 332 | return 0; |
| 333 | } |
| 334 | #endif |
| 335 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 336 | static int |
| 337 | xz_head(xz_statep state) |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 338 | { |
| 339 | lzma_stream *strm = &(state->strm); |
| 340 | lzma_stream init = LZMA_STREAM_INIT; |
| 341 | int flags; |
| 342 | unsigned len; |
| 343 | |
| 344 | /* allocate read buffers and inflate memory */ |
| 345 | if (state->size == 0) { |
| 346 | /* allocate buffers */ |
| 347 | state->in = malloc(state->want); |
| 348 | state->out = malloc(state->want << 1); |
| 349 | if (state->in == NULL || state->out == NULL) { |
| 350 | if (state->out != NULL) |
| 351 | free(state->out); |
| 352 | if (state->in != NULL) |
| 353 | free(state->in); |
| 354 | xz_error(state, LZMA_MEM_ERROR, "out of memory"); |
| 355 | return -1; |
| 356 | } |
| 357 | state->size = state->want; |
| 358 | |
| 359 | /* allocate decoder memory */ |
| 360 | state->strm = init; |
| 361 | state->strm.avail_in = 0; |
| 362 | state->strm.next_in = NULL; |
| 363 | if (lzma_auto_decoder(&state->strm, UINT64_MAX, 0) != LZMA_OK) { |
| 364 | free(state->out); |
| 365 | free(state->in); |
| 366 | state->size = 0; |
| 367 | xz_error(state, LZMA_MEM_ERROR, "out of memory"); |
| 368 | return -1; |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 369 | } |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 370 | #ifdef HAVE_ZLIB_H |
| 371 | /* allocate inflate memory */ |
| 372 | state->zstrm.zalloc = Z_NULL; |
| 373 | state->zstrm.zfree = Z_NULL; |
| 374 | state->zstrm.opaque = Z_NULL; |
| 375 | state->zstrm.avail_in = 0; |
| 376 | state->zstrm.next_in = Z_NULL; |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 377 | if (inflateInit2(&(state->zstrm), -15) != Z_OK) { /* raw inflate */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 378 | free(state->out); |
| 379 | free(state->in); |
| 380 | state->size = 0; |
| 381 | xz_error(state, LZMA_MEM_ERROR, "out of memory"); |
| 382 | return -1; |
| 383 | } |
| 384 | #endif |
| 385 | } |
| 386 | |
| 387 | /* get some data in the input buffer */ |
| 388 | if (strm->avail_in == 0) { |
| 389 | if (xz_avail(state) == -1) |
| 390 | return -1; |
| 391 | if (strm->avail_in == 0) |
| 392 | return 0; |
| 393 | } |
| 394 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 395 | /* look for the xz magic header bytes */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 396 | if (is_format_xz(state) || is_format_lzma(state)) { |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 397 | state->how = LZMA; |
| 398 | state->direct = 0; |
| 399 | return 0; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 400 | } |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 401 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 402 | /* look for the gzip magic header bytes 31 and 139 */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 403 | if (strm->next_in[0] == 31) { |
| 404 | strm->avail_in--; |
| 405 | strm->next_in++; |
| 406 | if (strm->avail_in == 0 && xz_avail(state) == -1) |
| 407 | return -1; |
| 408 | if (strm->avail_in && strm->next_in[0] == 139) { |
| 409 | /* we have a gzip header, woo hoo! */ |
| 410 | strm->avail_in--; |
| 411 | strm->next_in++; |
| 412 | |
| 413 | /* skip rest of header */ |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 414 | if (NEXT() != 8) { /* compression method */ |
| 415 | xz_error(state, LZMA_DATA_ERROR, |
| 416 | "unknown compression method"); |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 417 | return -1; |
| 418 | } |
| 419 | flags = NEXT(); |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 420 | if (flags & 0xe0) { /* reserved flag bits */ |
| 421 | xz_error(state, LZMA_DATA_ERROR, |
| 422 | "unknown header flags set"); |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 423 | return -1; |
| 424 | } |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 425 | NEXT(); /* modification time */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 426 | NEXT(); |
| 427 | NEXT(); |
| 428 | NEXT(); |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 429 | NEXT(); /* extra flags */ |
| 430 | NEXT(); /* operating system */ |
| 431 | if (flags & 4) { /* extra field */ |
| 432 | len = (unsigned) NEXT(); |
| 433 | len += (unsigned) NEXT() << 8; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 434 | while (len--) |
| 435 | if (NEXT() < 0) |
| 436 | break; |
| 437 | } |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 438 | if (flags & 8) /* file name */ |
| 439 | while (NEXT() > 0) ; |
| 440 | if (flags & 16) /* comment */ |
| 441 | while (NEXT() > 0) ; |
| 442 | if (flags & 2) { /* header crc */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 443 | NEXT(); |
| 444 | NEXT(); |
| 445 | } |
| 446 | /* 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] | 447 | * noticed on the first request for uncompressed data */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 448 | |
| 449 | /* set up for decompression */ |
| 450 | inflateReset(&state->zstrm); |
| 451 | state->zstrm.adler = crc32(0L, Z_NULL, 0); |
| 452 | state->how = GZIP; |
| 453 | state->direct = 0; |
| 454 | return 0; |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 455 | } else { |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 456 | /* not a gzip file -- save first byte (31) and fall to raw i/o */ |
| 457 | state->out[0] = 31; |
| 458 | state->have = 1; |
| 459 | } |
| 460 | } |
| 461 | #endif |
| 462 | |
| 463 | /* 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] | 464 | * input to output -- this assumes that the output buffer is larger than |
| 465 | * the input buffer, which also assures space for gzungetc() */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 466 | state->raw = state->pos; |
| 467 | state->next = state->out; |
| 468 | if (strm->avail_in) { |
| 469 | memcpy(state->next + state->have, strm->next_in, strm->avail_in); |
| 470 | state->have += strm->avail_in; |
| 471 | strm->avail_in = 0; |
| 472 | } |
| 473 | state->how = COPY; |
| 474 | state->direct = 1; |
| 475 | return 0; |
| 476 | } |
| 477 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 478 | static int |
| 479 | xz_decomp(xz_statep state) |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 480 | { |
| 481 | int ret; |
| 482 | unsigned had; |
| 483 | unsigned long crc, len; |
| 484 | lzma_stream *strm = &(state->strm); |
| 485 | |
| 486 | lzma_action action = LZMA_RUN; |
| 487 | |
| 488 | /* fill output buffer up to end of deflate stream */ |
| 489 | had = strm->avail_out; |
| 490 | do { |
| 491 | /* get more input for inflate() */ |
| 492 | if (strm->avail_in == 0 && xz_avail(state) == -1) |
| 493 | return -1; |
| 494 | if (strm->avail_in == 0) { |
| 495 | xz_error(state, LZMA_DATA_ERROR, "unexpected end of file"); |
| 496 | return -1; |
| 497 | } |
| 498 | if (state->eof) |
| 499 | action = LZMA_FINISH; |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 500 | |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 501 | /* decompress and handle errors */ |
| 502 | #ifdef HAVE_ZLIB_H |
| 503 | if (state->how == GZIP) { |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 504 | state->zstrm.avail_in = (uInt) state->strm.avail_in; |
| 505 | state->zstrm.next_in = (Bytef *) state->strm.next_in; |
| 506 | state->zstrm.avail_out = (uInt) state->strm.avail_out; |
| 507 | state->zstrm.next_out = (Bytef *) state->strm.next_out; |
| 508 | ret = inflate(&state->zstrm, Z_NO_FLUSH); |
| 509 | if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) { |
| 510 | xz_error(state, Z_STREAM_ERROR, |
| 511 | "internal error: inflate stream corrupt"); |
| 512 | return -1; |
| 513 | } |
| 514 | if (ret == Z_MEM_ERROR) |
| 515 | ret = LZMA_MEM_ERROR; |
| 516 | if (ret == Z_DATA_ERROR) |
| 517 | ret = LZMA_DATA_ERROR; |
| 518 | if (ret == Z_STREAM_END) |
| 519 | ret = LZMA_STREAM_END; |
| 520 | state->strm.avail_in = state->zstrm.avail_in; |
| 521 | state->strm.next_in = state->zstrm.next_in; |
| 522 | state->strm.avail_out = state->zstrm.avail_out; |
| 523 | state->strm.next_out = state->zstrm.next_out; |
| 524 | } else /* state->how == LZMA */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 525 | #endif |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 526 | ret = lzma_code(strm, action); |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 527 | if (ret == LZMA_MEM_ERROR) { |
| 528 | xz_error(state, LZMA_MEM_ERROR, "out of memory"); |
| 529 | return -1; |
| 530 | } |
| 531 | if (ret == LZMA_DATA_ERROR) { |
| 532 | xz_error(state, LZMA_DATA_ERROR, "compressed data error"); |
| 533 | return -1; |
| 534 | } |
| 535 | } while (strm->avail_out && ret != LZMA_STREAM_END); |
| 536 | |
| 537 | /* update available output and crc check value */ |
| 538 | state->have = had - strm->avail_out; |
| 539 | state->next = strm->next_out - state->have; |
| 540 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 541 | state->zstrm.adler = |
| 542 | crc32(state->zstrm.adler, state->next, state->have); |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 543 | #endif |
| 544 | |
| 545 | if (ret == LZMA_STREAM_END) { |
| 546 | #ifdef HAVE_ZLIB_H |
| 547 | if (state->how == GZIP) { |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 548 | if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) { |
| 549 | xz_error(state, LZMA_DATA_ERROR, "unexpected end of file"); |
| 550 | return -1; |
| 551 | } |
| 552 | if (crc != state->zstrm.adler) { |
| 553 | xz_error(state, LZMA_DATA_ERROR, "incorrect data check"); |
| 554 | return -1; |
| 555 | } |
| 556 | if (len != (state->zstrm.total_out & 0xffffffffL)) { |
| 557 | xz_error(state, LZMA_DATA_ERROR, "incorrect length check"); |
| 558 | return -1; |
| 559 | } |
| 560 | state->strm.avail_in = 0; |
| 561 | state->strm.next_in = NULL; |
| 562 | state->strm.avail_out = 0; |
| 563 | state->strm.next_out = NULL; |
| 564 | } else |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 565 | #endif |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 566 | if (strm->avail_in != 0 || !state->eof) { |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 567 | xz_error(state, LZMA_DATA_ERROR, "trailing garbage"); |
| 568 | return -1; |
| 569 | } |
| 570 | state->how = LOOK; /* ready for next stream, once have is 0 (leave |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 571 | * state->direct unchanged to remember how) */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* good decompression */ |
| 575 | return 0; |
| 576 | } |
| 577 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 578 | static int |
| 579 | xz_make(xz_statep state) |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 580 | { |
| 581 | lzma_stream *strm = &(state->strm); |
| 582 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 583 | if (state->how == LOOK) { /* look for lzma / gzip header */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 584 | if (xz_head(state) == -1) |
| 585 | return -1; |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 586 | if (state->have) /* got some data from xz_head() */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 587 | return 0; |
| 588 | } |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 589 | if (state->how == COPY) { /* straight copy */ |
| 590 | if (xz_load(state, state->out, state->size << 1, &(state->have)) == |
| 591 | -1) |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 592 | return -1; |
| 593 | state->next = state->out; |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 594 | } else if (state->how == LZMA || state->how == GZIP) { /* decompress */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 595 | strm->avail_out = state->size << 1; |
| 596 | strm->next_out = state->out; |
| 597 | if (xz_decomp(state) == -1) |
| 598 | return -1; |
| 599 | } |
| 600 | return 0; |
| 601 | } |
| 602 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 603 | static int |
| 604 | xz_skip(xz_statep state, uint64_t len) |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 605 | { |
| 606 | unsigned n; |
| 607 | |
| 608 | /* skip over len bytes or reach end-of-file, whichever comes first */ |
| 609 | while (len) |
| 610 | /* skip over whatever is in output buffer */ |
| 611 | if (state->have) { |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 612 | n = (uint64_t) state->have > len ? |
| 613 | (unsigned) len : state->have; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 614 | state->have -= n; |
| 615 | state->next += n; |
| 616 | state->pos += n; |
| 617 | len -= n; |
| 618 | } |
| 619 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 620 | /* 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] | 621 | else if (state->eof && state->strm.avail_in == 0) |
| 622 | break; |
| 623 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 624 | /* need more data to skip -- load up output buffer */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 625 | else { |
| 626 | /* get more output, looking for header if required */ |
| 627 | if (xz_make(state) == -1) |
| 628 | return -1; |
| 629 | } |
| 630 | return 0; |
| 631 | } |
| 632 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 633 | int |
| 634 | __libxml2_xzread(xzFile file, void *buf, unsigned len) |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 635 | { |
| 636 | unsigned got, n; |
| 637 | xz_statep state; |
| 638 | lzma_stream *strm; |
| 639 | |
| 640 | /* get internal structure */ |
| 641 | if (file == NULL) |
| 642 | return -1; |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 643 | state = (xz_statep) file; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 644 | strm = &(state->strm); |
| 645 | |
| 646 | /* check that we're reading and that there's no error */ |
| 647 | if (state->err != LZMA_OK) |
| 648 | return -1; |
| 649 | |
| 650 | /* 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] | 651 | * with an error (this avoids the flaw in the interface) */ |
| 652 | if ((int) len < 0) { |
| 653 | xz_error(state, LZMA_BUF_ERROR, |
| 654 | "requested length does not fit in int"); |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 655 | return -1; |
| 656 | } |
| 657 | |
| 658 | /* if len is zero, avoid unnecessary operations */ |
| 659 | if (len == 0) |
| 660 | return 0; |
| 661 | |
| 662 | /* process a skip request */ |
| 663 | if (state->seek) { |
| 664 | state->seek = 0; |
| 665 | if (xz_skip(state, state->skip) == -1) |
| 666 | return -1; |
| 667 | } |
| 668 | |
| 669 | /* get len bytes to buf, or less than len if at the end */ |
| 670 | got = 0; |
| 671 | do { |
| 672 | /* first just try copying data from the output buffer */ |
| 673 | if (state->have) { |
| 674 | n = state->have > len ? len : state->have; |
| 675 | memcpy(buf, state->next, n); |
| 676 | state->next += n; |
| 677 | state->have -= n; |
| 678 | } |
| 679 | |
| 680 | /* output buffer empty -- return if we're at the end of the input */ |
| 681 | else if (state->eof && strm->avail_in == 0) |
| 682 | break; |
| 683 | |
| 684 | /* 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] | 685 | * buffer */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 686 | else if (state->how == LOOK || len < (state->size << 1)) { |
| 687 | /* get more output, looking for header if required */ |
| 688 | if (xz_make(state) == -1) |
| 689 | return -1; |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 690 | continue; /* no progress yet -- go back to memcpy() above */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 691 | /* the copy above assures that we will leave with space in the |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 692 | * output buffer, allowing at least one gzungetc() to succeed */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /* large len -- read directly into user buffer */ |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 696 | else if (state->how == COPY) { /* read directly */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 697 | if (xz_load(state, buf, len, &n) == -1) |
| 698 | return -1; |
| 699 | } |
| 700 | |
| 701 | /* large len -- decompress directly into user buffer */ |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 702 | else { /* state->how == LZMA */ |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 703 | strm->avail_out = len; |
| 704 | strm->next_out = buf; |
| 705 | if (xz_decomp(state) == -1) |
| 706 | return -1; |
| 707 | n = state->have; |
| 708 | state->have = 0; |
| 709 | } |
| 710 | |
| 711 | /* update progress */ |
| 712 | len -= n; |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 713 | buf = (char *) buf + n; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 714 | got += n; |
| 715 | state->pos += n; |
| 716 | } while (len); |
| 717 | |
| 718 | /* return number of bytes read into user buffer (will fit in int) */ |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 719 | return (int) got; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 720 | } |
| 721 | |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 722 | int |
| 723 | __libxml2_xzclose(xzFile file) |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 724 | { |
| 725 | int ret; |
| 726 | xz_statep state; |
| 727 | |
| 728 | /* get internal structure */ |
| 729 | if (file == NULL) |
| 730 | return LZMA_DATA_ERROR; |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 731 | state = (xz_statep) file; |
Anders F Bjorklund | 6bdc774 | 2011-09-19 09:53:20 +0200 | [diff] [blame] | 732 | |
| 733 | /* free memory and close file */ |
| 734 | if (state->size) { |
| 735 | lzma_end(&(state->strm)); |
| 736 | #ifdef HAVE_LIBZ_H |
| 737 | inflateEnd(&(state->zstrm)); |
| 738 | #endif |
| 739 | free(state->out); |
| 740 | free(state->in); |
| 741 | } |
| 742 | free(state->path); |
| 743 | ret = close(state->fd); |
| 744 | free(state); |
| 745 | return ret ? ret : LZMA_OK; |
| 746 | } |
Daniel Veillard | adf5ec9 | 2012-01-26 16:56:22 +0800 | [diff] [blame] | 747 | #endif /* HAVE_LZMA_H */ |