Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib/uncompr.c
Line
Count
Source
1
/* uncompr.c -- decompress a memory buffer
2
 * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
 */
5
6
/* @(#) $Id$ */
7
8
#define ZLIB_INTERNAL
9
#include "zlib.h"
10
11
/* ===========================================================================
12
     Decompresses the source buffer into the destination buffer.  *sourceLen is
13
   the byte length of the source buffer. Upon entry, *destLen is the total size
14
   of the destination buffer, which must be large enough to hold the entire
15
   uncompressed data. (The size of the uncompressed data must have been saved
16
   previously by the compressor and transmitted to the decompressor by some
17
   mechanism outside the scope of this compression library.) Upon exit,
18
   *destLen is the size of the decompressed data and *sourceLen is the number
19
   of source bytes consumed. Upon return, source + *sourceLen points to the
20
   first unused input byte.
21
22
     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
23
   memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
24
   Z_DATA_ERROR if the input data was corrupted, including if the input data is
25
   an incomplete zlib stream.
26
27
     The _z versions of the functions take size_t length arguments.
28
*/
29
int ZEXPORT uncompress2_z(Bytef *dest, z_size_t *destLen, const Bytef *source,
30
884k
                          z_size_t *sourceLen) {
31
884k
    z_stream stream;
32
884k
    int err;
33
884k
    const uInt max = (uInt)-1;
34
884k
    z_size_t len, left;
35
36
884k
    len = *sourceLen;
37
884k
    left = *destLen;
38
884k
    if (left == 0 && dest == Z_NULL)
39
0
        dest = (Bytef *)&stream.reserved;       /* next_out cannot be NULL */
40
41
884k
    stream.next_in = (z_const Bytef *)source;
42
884k
    stream.avail_in = 0;
43
884k
    stream.zalloc = (alloc_func)0;
44
884k
    stream.zfree = (free_func)0;
45
884k
    stream.opaque = (voidpf)0;
46
47
884k
    err = inflateInit(&stream);
48
884k
    if (err != Z_OK) return err;
49
50
884k
    stream.next_out = dest;
51
884k
    stream.avail_out = 0;
52
53
1.11M
    do {
54
1.11M
        if (stream.avail_out == 0) {
55
928k
            stream.avail_out = left > (z_size_t)max ? max : (uInt)left;
56
928k
            left -= stream.avail_out;
57
928k
        }
58
1.11M
        if (stream.avail_in == 0) {
59
1.07M
            stream.avail_in = len > (z_size_t)max ? max : (uInt)len;
60
1.07M
            len -= stream.avail_in;
61
1.07M
        }
62
1.11M
        err = inflate(&stream, Z_NO_FLUSH);
63
1.11M
    } while (err == Z_OK);
64
65
    /* Set len and left to the unused input data and unused output space. Set
66
       *sourceLen to the amount of input consumed. Set *destLen to the amount
67
       of data produced. */
68
884k
    len += stream.avail_in;
69
884k
    left += stream.avail_out;
70
884k
    *sourceLen -= len;
71
884k
    *destLen -= left;
72
73
884k
    inflateEnd(&stream);
74
884k
    return err == Z_STREAM_END ? Z_OK :
75
884k
           err == Z_NEED_DICT ? Z_DATA_ERROR  :
76
631k
           err == Z_BUF_ERROR && len == 0 ? Z_DATA_ERROR :
77
626k
           err;
78
884k
}
79
int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
80
884k
                        uLong *sourceLen) {
81
884k
    int ret;
82
884k
    z_size_t got = *destLen, used = *sourceLen;
83
884k
    ret = uncompress2_z(dest, &got, source, &used);
84
884k
    *sourceLen = (uLong)used;
85
884k
    *destLen = (uLong)got;
86
884k
    return ret;
87
884k
}
88
int ZEXPORT uncompress_z(Bytef *dest, z_size_t *destLen, const Bytef *source,
89
0
                         z_size_t sourceLen) {
90
0
    z_size_t used = sourceLen;
91
0
    return uncompress2_z(dest, destLen, source, &used);
92
0
}
93
int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
94
884k
                       uLong sourceLen) {
95
884k
    uLong used = sourceLen;
96
884k
    return uncompress2(dest, destLen, source, &used);
97
884k
}