Coverage Report

Created: 2023-12-08 06:59

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