Coverage Report

Created: 2025-06-20 06:08

/src/c-blosc2/internal-complibs/zlib-ng-2.0.7/uncompr.c
Line
Count
Source (jump to first uncovered line)
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
0
int Z_EXPORT PREFIX(uncompress2)(unsigned char *dest, z_size_t *destLen, const unsigned char *source, z_size_t *sourceLen) {
31
0
    PREFIX3(stream) stream;
32
0
    int err;
33
0
    const unsigned int max = (unsigned int)-1;
34
0
    z_size_t len, left;
35
0
    unsigned char buf[1];    /* for detection of incomplete stream when *destLen == 0 */
36
37
0
    len = *sourceLen;
38
0
    if (*destLen) {
39
0
        left = *destLen;
40
0
        *destLen = 0;
41
0
    } else {
42
0
        left = 1;
43
0
        dest = buf;
44
0
    }
45
46
0
    stream.next_in = (z_const unsigned char *)source;
47
0
    stream.avail_in = 0;
48
0
    stream.zalloc = NULL;
49
0
    stream.zfree = NULL;
50
0
    stream.opaque = NULL;
51
52
0
    err = PREFIX(inflateInit)(&stream);
53
0
    if (err != Z_OK) return err;
54
55
0
    stream.next_out = dest;
56
0
    stream.avail_out = 0;
57
58
0
    do {
59
0
        if (stream.avail_out == 0) {
60
0
            stream.avail_out = left > (unsigned long)max ? max : (unsigned int)left;
61
0
            left -= stream.avail_out;
62
0
        }
63
0
        if (stream.avail_in == 0) {
64
0
            stream.avail_in = len > (unsigned long)max ? max : (unsigned int)len;
65
0
            len -= stream.avail_in;
66
0
        }
67
0
        err = PREFIX(inflate)(&stream, Z_NO_FLUSH);
68
0
    } while (err == Z_OK);
69
70
0
    *sourceLen -= len + stream.avail_in;
71
0
    if (dest != buf)
72
0
        *destLen = (z_size_t)stream.total_out;
73
0
    else if (stream.total_out && err == Z_BUF_ERROR)
74
0
        left = 1;
75
76
0
    PREFIX(inflateEnd)(&stream);
77
0
    return err == Z_STREAM_END ? Z_OK :
78
0
           err == Z_NEED_DICT ? Z_DATA_ERROR  :
79
0
           err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
80
0
           err;
81
0
}
82
83
0
int Z_EXPORT PREFIX(uncompress)(unsigned char *dest, z_size_t *destLen, const unsigned char *source, z_size_t sourceLen) {
84
0
    return PREFIX(uncompress2)(dest, destLen, source, &sourceLen);
85
0
}