Coverage Report

Created: 2022-05-03 06:10

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