Coverage Report

Created: 2025-06-10 07:26

/src/ghostpdl/base/szlibd.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* zlib decoding (decompression) filter stream */
18
#include "memory_.h"
19
#include "std.h"
20
#include "strimpl.h"
21
#include "szlibxx.h"
22
23
/* Initialize the filter. */
24
static int
25
s_zlibD_init(stream_state * st)
26
265k
{
27
265k
    stream_zlib_state *const ss = (stream_zlib_state *)st;
28
265k
    int code = s_zlib_alloc_dynamic_state(ss);
29
30
265k
    if (code < 0)
31
0
        return ERRC;    /****** WRONG ******/
32
265k
    if (inflateInit2(&ss->dynamic->zstate,
33
265k
                     (ss->no_wrapper ? -ss->windowBits : ss->windowBits))
34
265k
        != Z_OK
35
265k
        ) {
36
0
        s_zlib_free_dynamic_state(ss);
37
0
        return ERRC;    /****** WRONG ******/
38
0
    }
39
265k
    st->min_left=1;
40
265k
    return 0;
41
265k
}
42
43
/* Reinitialize the filter. */
44
static int
45
s_zlibD_reset(stream_state * st)
46
0
{
47
0
    stream_zlib_state *const ss = (stream_zlib_state *)st;
48
49
0
    if (inflateReset(&ss->dynamic->zstate) != Z_OK)
50
0
        return ERRC;    /****** WRONG ******/
51
0
    return 0;
52
0
}
53
54
/* Process a buffer */
55
static int
56
s_zlibD_process(stream_state * st, stream_cursor_read * pr,
57
                stream_cursor_write * pw, bool ignore_last)
58
2.44M
{
59
2.44M
    stream_zlib_state *const ss = (stream_zlib_state *)st;
60
2.44M
    z_stream *zs = &ss->dynamic->zstate;
61
2.44M
    const byte *p = pr->ptr;
62
2.44M
    int status;
63
2.44M
    static const unsigned char jaws_empty[] = {0x58, 0x85, 1, 0, 0, 0, 0, 0, 1, 0x0A};
64
65
    /* Detect no input or full output so that we don't get */
66
    /* a Z_BUF_ERROR return. */
67
2.44M
    if (pw->ptr == pw->limit)
68
0
        return 1;
69
2.44M
    if (pr->ptr == pr->limit)
70
260k
        return 0;
71
2.18M
    zs->next_in = (Bytef *)p + 1;
72
2.18M
    zs->avail_in = pr->limit - p;
73
2.18M
    zs->next_out = pw->ptr + 1;
74
2.18M
    zs->avail_out = pw->limit - pw->ptr;
75
2.18M
    if (zs->total_in == 0 && zs->avail_in >= 10 && !memcmp(zs->next_in, jaws_empty, 10)) {
76
        /* JAWS PDF generator encodes empty stream as jaws_empty[].
77
         * The stream declares that the data block length is zero
78
         * but zlib routines regard a zero length data block to be an error.
79
         */
80
0
        pr->ptr += 10;
81
0
        return EOFC;
82
0
    }
83
2.18M
    status = inflate(zs, Z_PARTIAL_FLUSH);
84
2.18M
    pr->ptr = zs->next_in - 1;
85
2.18M
    pw->ptr = zs->next_out - 1;
86
2.18M
    switch (status) {
87
2.07M
        case Z_OK:
88
2.07M
            return (pw->ptr == pw->limit ? 1 : pr->ptr > p ? 0 : 1);
89
79.8k
        case Z_STREAM_END:
90
79.8k
            return EOFC;
91
32.2k
        default:
92
32.2k
            if (zs->msg && !strcmp("incorrect data check", zs->msg))
93
11.7k
            {
94
                /* Ignore errors when zlib streams fail on the checksum.
95
                 * Adobe, Apple and xpdf don't fail on pdf:s where this happens,
96
                 * so neither should we. fixes bug 688716.
97
                 */
98
11.7k
                dmprintf1(st->memory,
99
11.7k
                          "warning: ignoring zlib error: %s\n", zs->msg);
100
11.7k
                return EOFC;
101
11.7k
            }
102
20.4k
            return ERRC;
103
2.18M
    }
104
2.18M
}
105
106
/* Release the stream */
107
static void
108
s_zlibD_release(stream_state * st)
109
265k
{
110
265k
    stream_zlib_state *const ss = (stream_zlib_state *)st;
111
112
265k
    if (ss->dynamic != NULL)
113
265k
        inflateEnd(&ss->dynamic->zstate);
114
265k
    s_zlib_free_dynamic_state(ss);
115
265k
}
116
117
/* Stream template */
118
const stream_template s_zlibD_template = {
119
    &st_zlib_state, s_zlibD_init, s_zlibD_process, 1, 1, s_zlibD_release,
120
    s_zlib_set_defaults, s_zlibD_reset
121
};