Coverage Report

Created: 2026-01-17 06:22

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
1.43k
                          z_size_t *sourceLen) {
31
1.43k
    z_stream stream;
32
1.43k
    int err;
33
1.43k
    const uInt max = (uInt)-1;
34
1.43k
    z_size_t len, left;
35
36
1.43k
    len = *sourceLen;
37
1.43k
    left = *destLen;
38
1.43k
    if (left == 0 && dest == Z_NULL)
39
0
        dest = (Bytef *)&stream.reserved;       /* next_out cannot be NULL */
40
41
1.43k
    stream.next_in = (z_const Bytef *)source;
42
1.43k
    stream.avail_in = 0;
43
1.43k
    stream.zalloc = (alloc_func)0;
44
1.43k
    stream.zfree = (free_func)0;
45
1.43k
    stream.opaque = (voidpf)0;
46
47
1.43k
    err = inflateInit(&stream);
48
1.43k
    if (err != Z_OK) return err;
49
50
1.43k
    stream.next_out = dest;
51
1.43k
    stream.avail_out = 0;
52
53
2.30k
    do {
54
2.30k
        if (stream.avail_out == 0) {
55
1.57k
            stream.avail_out = left > (z_size_t)max ? max : (uInt)left;
56
1.57k
            left -= stream.avail_out;
57
1.57k
        }
58
2.30k
        if (stream.avail_in == 0) {
59
2.18k
            stream.avail_in = len > (z_size_t)max ? max : (uInt)len;
60
2.18k
            len -= stream.avail_in;
61
2.18k
        }
62
2.30k
        err = inflate(&stream, Z_NO_FLUSH);
63
2.30k
    } 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
1.43k
    len += stream.avail_in;
69
1.43k
    left += stream.avail_out;
70
1.43k
    *sourceLen -= len;
71
1.43k
    *destLen -= left;
72
73
1.43k
    inflateEnd(&stream);
74
1.43k
    return err == Z_STREAM_END ? Z_OK :
75
1.43k
           err == Z_NEED_DICT ? Z_DATA_ERROR  :
76
1.43k
           err == Z_BUF_ERROR && len == 0 ? Z_DATA_ERROR :
77
1.42k
           err;
78
1.43k
}
79
int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
80
1.43k
                        uLong *sourceLen) {
81
1.43k
    int ret;
82
1.43k
    z_size_t got = *destLen, used = *sourceLen;
83
1.43k
    ret = uncompress2_z(dest, &got, source, &used);
84
1.43k
    *sourceLen = (uLong)used;
85
1.43k
    *destLen = (uLong)got;
86
1.43k
    return ret;
87
1.43k
}
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
1.43k
                       uLong sourceLen) {
95
1.43k
    uLong used = sourceLen;
96
1.43k
    return uncompress2(dest, destLen, source, &used);
97
1.43k
}