Coverage Report

Created: 2026-08-31 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ots/subprojects/zlib-1.3.2/uncompr.c
Line
Count
Source
1
/* uncompr.c -- decompress a memory buffer
2
 * Copyright (C) 1995-2026 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.70k
                          z_size_t *sourceLen) {
31
1.70k
    z_stream stream;
32
1.70k
    int err;
33
1.70k
    const uInt max = (uInt)-1;
34
1.70k
    z_size_t len, left;
35
36
1.70k
    if (sourceLen == NULL || (*sourceLen > 0 && source == NULL) ||
37
1.70k
        destLen == NULL || (*destLen > 0 && dest == NULL))
38
0
        return Z_STREAM_ERROR;
39
40
1.70k
    len = *sourceLen;
41
1.70k
    left = *destLen;
42
1.70k
    if (left == 0 && dest == Z_NULL)
43
0
        dest = (Bytef *)&stream.reserved;       /* next_out cannot be NULL */
44
45
1.70k
    stream.next_in = (z_const Bytef *)source;
46
1.70k
    stream.avail_in = 0;
47
1.70k
    stream.zalloc = (alloc_func)0;
48
1.70k
    stream.zfree = (free_func)0;
49
1.70k
    stream.opaque = (voidpf)0;
50
51
1.70k
    err = inflateInit(&stream);
52
1.70k
    if (err != Z_OK) return err;
53
54
1.70k
    stream.next_out = dest;
55
1.70k
    stream.avail_out = 0;
56
57
1.82k
    do {
58
1.82k
        if (stream.avail_out == 0) {
59
1.76k
            stream.avail_out = left > (z_size_t)max ? max : (uInt)left;
60
1.76k
            left -= stream.avail_out;
61
1.76k
        }
62
1.82k
        if (stream.avail_in == 0) {
63
1.76k
            stream.avail_in = len > (z_size_t)max ? max : (uInt)len;
64
1.76k
            len -= stream.avail_in;
65
1.76k
        }
66
1.82k
        err = inflate(&stream, Z_NO_FLUSH);
67
1.82k
    } while (err == Z_OK);
68
69
    /* Set len and left to the unused input data and unused output space. Set
70
       *sourceLen to the amount of input consumed. Set *destLen to the amount
71
       of data produced. */
72
1.70k
    len += stream.avail_in;
73
1.70k
    left += stream.avail_out;
74
1.70k
    *sourceLen -= len;
75
1.70k
    *destLen -= left;
76
77
1.70k
    inflateEnd(&stream);
78
1.70k
    return err == Z_STREAM_END ? Z_OK :
79
1.70k
           err == Z_NEED_DICT ? Z_DATA_ERROR  :
80
345
           err == Z_BUF_ERROR && len == 0 ? Z_DATA_ERROR :
81
344
           err;
82
1.70k
}
83
int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
84
1.70k
                        uLong *sourceLen) {
85
1.70k
    int ret;
86
1.70k
    z_size_t got = *destLen, used = *sourceLen;
87
1.70k
    ret = uncompress2_z(dest, &got, source, &used);
88
1.70k
    *sourceLen = (uLong)used;
89
1.70k
    *destLen = (uLong)got;
90
1.70k
    return ret;
91
1.70k
}
92
int ZEXPORT uncompress_z(Bytef *dest, z_size_t *destLen, const Bytef *source,
93
0
                         z_size_t sourceLen) {
94
0
    z_size_t used = sourceLen;
95
0
    return uncompress2_z(dest, destLen, source, &used);
96
0
}
97
int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
98
1.70k
                       uLong sourceLen) {
99
1.70k
    uLong used = sourceLen;
100
1.70k
    return uncompress2(dest, destLen, source, &used);
101
1.70k
}