Coverage Report

Created: 2026-01-25 07:18

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