Coverage Report

Created: 2026-03-26 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/c-blosc/internal-complibs/zlib-1.3.1/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
int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
28
3.33k
                        uLong *sourceLen) {
29
3.33k
    z_stream stream;
30
3.33k
    int err;
31
3.33k
    const uInt max = (uInt)-1;
32
3.33k
    uLong len, left;
33
3.33k
    Byte buf[1];    /* for detection of incomplete stream when *destLen == 0 */
34
35
3.33k
    len = *sourceLen;
36
3.33k
    if (*destLen) {
37
3.33k
        left = *destLen;
38
3.33k
        *destLen = 0;
39
3.33k
    }
40
0
    else {
41
0
        left = 1;
42
0
        dest = buf;
43
0
    }
44
45
3.33k
    stream.next_in = (z_const Bytef *)source;
46
3.33k
    stream.avail_in = 0;
47
3.33k
    stream.zalloc = (alloc_func)0;
48
3.33k
    stream.zfree = (free_func)0;
49
3.33k
    stream.opaque = (voidpf)0;
50
51
3.33k
    err = inflateInit(&stream);
52
3.33k
    if (err != Z_OK) return err;
53
54
3.33k
    stream.next_out = dest;
55
3.33k
    stream.avail_out = 0;
56
57
3.33k
    do {
58
3.33k
        if (stream.avail_out == 0) {
59
3.33k
            stream.avail_out = left > (uLong)max ? max : (uInt)left;
60
3.33k
            left -= stream.avail_out;
61
3.33k
        }
62
3.33k
        if (stream.avail_in == 0) {
63
3.33k
            stream.avail_in = len > (uLong)max ? max : (uInt)len;
64
3.33k
            len -= stream.avail_in;
65
3.33k
        }
66
3.33k
        err = inflate(&stream, Z_NO_FLUSH);
67
3.33k
    } while (err == Z_OK);
68
69
3.33k
    *sourceLen -= len + stream.avail_in;
70
3.33k
    if (dest != buf)
71
3.33k
        *destLen = stream.total_out;
72
0
    else if (stream.total_out && err == Z_BUF_ERROR)
73
0
        left = 1;
74
75
3.33k
    inflateEnd(&stream);
76
3.33k
    return err == Z_STREAM_END ? Z_OK :
77
3.33k
           err == Z_NEED_DICT ? Z_DATA_ERROR  :
78
0
           err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
79
0
           err;
80
3.33k
}
81
82
int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
83
3.33k
                       uLong sourceLen) {
84
3.33k
    return uncompress2(dest, destLen, source, &sourceLen);
85
3.33k
}