Coverage Report

Created: 2026-08-31 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/c-blosc2/_deps/zlib_ng-src/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
#include "zbuild.h"
7
#include "zutil.h"
8
9
/* ===========================================================================
10
     Decompresses the source buffer into the destination buffer.  *sourceLen is
11
   the byte length of the source buffer. Upon entry, *destLen is the total size
12
   of the destination buffer, which must be large enough to hold the entire
13
   uncompressed data. (The size of the uncompressed data must have been saved
14
   previously by the compressor and transmitted to the decompressor by some
15
   mechanism outside the scope of this compression library.) Upon exit,
16
   *destLen is the size of the decompressed data and *sourceLen is the number
17
   of source bytes consumed. Upon return, source + *sourceLen points to the
18
   first unused input byte.
19
20
     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
21
   memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
22
   Z_DATA_ERROR if the input data was corrupted, including if the input data is
23
   an incomplete zlib stream.
24
*/
25
8.28k
z_int32_t Z_EXPORT PREFIX(uncompress2)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t *sourceLen) {
26
8.28k
    PREFIX3(stream) stream;
27
8.28k
    int err;
28
8.28k
    const unsigned int max = (unsigned int)-1;
29
8.28k
    z_uintmax_t len, left;
30
8.28k
    unsigned char buf[1];    /* for detection of incomplete stream when *destLen == 0 */
31
32
8.28k
    len = *sourceLen;
33
8.28k
    if (*destLen) {
34
8.28k
        left = *destLen;
35
8.28k
        *destLen = 0;
36
8.28k
    } else {
37
0
        left = 1;
38
0
        dest = buf;
39
0
    }
40
41
8.28k
    stream.next_in = (z_const unsigned char *)source;
42
8.28k
    stream.avail_in = 0;
43
8.28k
    stream.zalloc = NULL;
44
8.28k
    stream.zfree = NULL;
45
8.28k
    stream.opaque = NULL;
46
47
8.28k
    err = PREFIX(inflateInit)(&stream);
48
8.28k
    if (err != Z_OK) return err;
49
50
8.28k
    stream.next_out = dest;
51
8.28k
    stream.avail_out = 0;
52
53
9.02k
    do {
54
9.02k
        if (stream.avail_out == 0) {
55
8.58k
            stream.avail_out = left > (unsigned long)max ? max : (unsigned int)left;
56
8.58k
            left -= stream.avail_out;
57
8.58k
        }
58
9.02k
        if (stream.avail_in == 0) {
59
8.75k
            stream.avail_in = len > (unsigned long)max ? max : (unsigned int)len;
60
8.75k
            len -= stream.avail_in;
61
8.75k
        }
62
9.02k
        err = PREFIX(inflate)(&stream, Z_NO_FLUSH);
63
9.02k
    } while (err == Z_OK);
64
65
8.28k
    *sourceLen -= len + stream.avail_in;
66
8.28k
    if (dest != buf)
67
8.28k
        *destLen = (z_size_t)stream.total_out;
68
0
    else if (stream.total_out && err == Z_BUF_ERROR)
69
0
        left = 1;
70
71
8.28k
    PREFIX(inflateEnd)(&stream);
72
8.28k
    return err == Z_STREAM_END ? Z_OK :
73
8.28k
           err == Z_NEED_DICT ? Z_DATA_ERROR  :
74
1.66k
           err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
75
1.66k
           err;
76
8.28k
}
77
78
8.28k
z_int32_t Z_EXPORT PREFIX(uncompress)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t sourceLen) {
79
8.28k
    return PREFIX(uncompress2)(dest, destLen, source, &sourceLen);
80
8.28k
}