Coverage Report

Created: 2026-02-26 06:38

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