Coverage Report

Created: 2024-01-18 09:16

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