Coverage Report

Created: 2024-09-06 07:53

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