Coverage Report

Created: 2025-12-14 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/Pl_Flate.cc
Line
Count
Source
1
#include <qpdf/Pl_Flate.hh>
2
3
#include <climits>
4
#include <cstring>
5
#include <zlib.h>
6
7
#include <qpdf/QIntC.hh>
8
#include <qpdf/QUtil.hh>
9
#include <qpdf/Util.hh>
10
#include <qpdf/qpdf-config.h>
11
12
#ifdef ZOPFLI
13
# include <zopfli.h>
14
#endif
15
16
using namespace qpdf;
17
18
namespace
19
{
20
    unsigned long long memory_limit_{0};
21
} // namespace
22
23
int Pl_Flate::compression_level = Z_DEFAULT_COMPRESSION;
24
25
Pl_Flate::Members::Members(size_t out_bufsize, action_e action) :
26
14.2k
    out_bufsize(out_bufsize),
27
14.2k
    action(action),
28
14.2k
    initialized(false),
29
14.2k
    zdata(nullptr)
30
14.2k
{
31
14.2k
    this->outbuf = QUtil::make_shared_array<unsigned char>(out_bufsize);
32
    // Indirect through zdata to reach the z_stream so we don't have to include zlib.h in
33
    // Pl_Flate.hh.  This means people using shared library versions of qpdf don't have to have zlib
34
    // development files available, which particularly helps in a Windows environment.
35
14.2k
    zdata = new z_stream;
36
37
14.2k
    util::no_ci_rt_error_if(
38
14.2k
        out_bufsize > UINT_MAX,
39
14.2k
        "Pl_Flate: zlib doesn't support buffer sizes larger than unsigned int");
40
41
14.2k
    z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
42
14.2k
    zstream.zalloc = nullptr;
43
14.2k
    zstream.zfree = nullptr;
44
14.2k
    zstream.opaque = nullptr;
45
14.2k
    zstream.next_in = nullptr;
46
14.2k
    zstream.avail_in = 0;
47
14.2k
    zstream.next_out = this->outbuf.get();
48
14.2k
    zstream.avail_out = QIntC::to_uint(out_bufsize);
49
50
14.2k
    if (action == a_deflate && Pl_Flate::zopfli_enabled()) {
51
0
        zopfli_buf = std::make_unique<std::string>();
52
0
    }
53
14.2k
}
54
55
Pl_Flate::Members::~Members()
56
14.2k
{
57
14.2k
    if (initialized) {
58
3.59k
        z_stream& zstream = *(static_cast<z_stream*>(zdata));
59
3.59k
        if (action == a_deflate) {
60
0
            deflateEnd(&zstream);
61
3.59k
        } else {
62
3.59k
            inflateEnd(&zstream);
63
3.59k
        }
64
3.59k
    }
65
66
14.2k
    delete static_cast<z_stream*>(this->zdata);
67
14.2k
    zdata = nullptr;
68
14.2k
}
69
70
Pl_Flate::Pl_Flate(
71
    char const* identifier, Pipeline* next, action_e action, unsigned int out_bufsize_int) :
72
14.2k
    Pipeline(identifier, next),
73
14.2k
    m(std::make_unique<Members>(QIntC::to_size(out_bufsize_int), action))
74
14.2k
{
75
14.2k
    util::assertion(next, "Attempt to create Pl_Flate with nullptr as next");
76
14.2k
}
77
78
// Must be explicit and not inline -- see QPDF_DLL_CLASS in README-maintainer
79
14.2k
Pl_Flate::~Pl_Flate() = default;
80
81
unsigned long long
82
Pl_Flate::memory_limit()
83
4.69k
{
84
4.69k
    return memory_limit_;
85
4.69k
}
86
87
void
88
Pl_Flate::memory_limit(unsigned long long limit)
89
24.4k
{
90
24.4k
    memory_limit_ = limit;
91
24.4k
}
92
93
void
94
Pl_Flate::setWarnCallback(std::function<void(char const*, int)> callback)
95
14.8k
{
96
14.8k
    m->callback = callback;
97
14.8k
}
98
99
void
100
Pl_Flate::warn(char const* msg, int code)
101
2.92k
{
102
2.92k
    if (m->callback) {
103
2.92k
        m->callback(msg, code);
104
2.92k
    }
105
2.92k
}
106
107
void
108
Pl_Flate::write(unsigned char const* data, size_t len)
109
236k
{
110
236k
    util::assertion(
111
236k
        m->outbuf.get(), identifier + ": Pl_Flate: write() called after finish() called");
112
236k
    if (m->zopfli_buf) {
113
0
        m->zopfli_buf->append(reinterpret_cast<char const*>(data), len);
114
0
        return;
115
0
    }
116
117
    // Write in chunks in case len is too big to fit in an int. Assume int is at least 32 bits.
118
236k
    static size_t const max_bytes = 1 << 30;
119
236k
    size_t bytes_left = len;
120
236k
    unsigned char const* buf = data;
121
473k
    while (bytes_left > 0) {
122
236k
        size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left);
123
236k
        handleData(buf, bytes, (m->action == a_inflate ? Z_SYNC_FLUSH : Z_NO_FLUSH));
124
236k
        bytes_left -= bytes;
125
236k
        buf += bytes;
126
236k
    }
127
236k
}
128
129
void
130
Pl_Flate::handleData(unsigned char const* data, size_t len, int flush)
131
249k
{
132
249k
    util::no_ci_rt_error_if(
133
249k
        len > UINT_MAX, "Pl_Flate: zlib doesn't support data blocks larger than int");
134
249k
    z_stream& zstream = *(static_cast<z_stream*>(m->zdata));
135
    // zlib is known not to modify the data pointed to by next_in but doesn't declare the field
136
    // value const unless compiled to do so.
137
249k
    zstream.next_in = const_cast<unsigned char*>(data);
138
249k
    zstream.avail_in = QIntC::to_uint(len);
139
140
249k
    if (!m->initialized) {
141
13.2k
        int err = Z_OK;
142
143
        // deflateInit and inflateInit are macros that use old-style casts.
144
13.2k
#if ((defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406) || defined(__clang__))
145
13.2k
# pragma GCC diagnostic push
146
13.2k
# pragma GCC diagnostic ignored "-Wold-style-cast"
147
13.2k
#endif
148
13.2k
        if (m->action == a_deflate) {
149
0
            err = deflateInit(&zstream, compression_level);
150
13.2k
        } else {
151
13.2k
            err = inflateInit(&zstream);
152
13.2k
        }
153
13.2k
#if ((defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406) || defined(__clang__))
154
13.2k
# pragma GCC diagnostic pop
155
13.2k
#endif
156
157
13.2k
        checkError("Init", err);
158
13.2k
        m->initialized = true;
159
13.2k
    }
160
161
249k
    int err = Z_OK;
162
163
249k
    bool done = false;
164
492k
    while (!done) {
165
249k
        if (m->action == a_deflate) {
166
0
            err = deflate(&zstream, flush);
167
249k
        } else {
168
249k
            err = inflate(&zstream, flush);
169
249k
        }
170
249k
        if ((m->action == a_inflate) && (err != Z_OK) && zstream.msg &&
171
9.02k
            (strcmp(zstream.msg, "incorrect data check") == 0)) {
172
            // Other PDF readers ignore this specific error. Combining this with Z_SYNC_FLUSH
173
            // enables qpdf to handle some broken zlib streams without losing data.
174
2.87k
            err = Z_STREAM_END;
175
2.87k
        }
176
249k
        switch (err) {
177
2.92k
        case Z_BUF_ERROR:
178
            // Probably shouldn't be able to happen, but possible as a boundary condition: if the
179
            // last call to inflate exactly filled the output buffer, it's possible that the next
180
            // call to inflate could have nothing to do. There are PDF files in the wild that have
181
            // this error (including at least one in qpdf's test suite). In some cases, we want to
182
            // know about this, because it indicates incorrect compression, so call a callback if
183
            // provided.
184
2.92k
            warn("input stream is complete but output may still be valid", err);
185
2.92k
            done = true;
186
2.92k
            break;
187
188
14.1k
        case Z_STREAM_END:
189
14.1k
            done = true;
190
            // fall through
191
192
240k
        case Z_OK:
193
240k
            {
194
240k
                if ((zstream.avail_in == 0) && (zstream.avail_out > 0)) {
195
                    // There is nothing left to read, and there was sufficient buffer space to write
196
                    // everything we needed, so we're done for now.
197
238k
                    done = true;
198
238k
                }
199
240k
                uLong ready = QIntC::to_ulong(m->out_bufsize - zstream.avail_out);
200
240k
                if (ready > 0) {
201
211k
                    if (memory_limit_ && m->action != a_deflate) {
202
211k
                        m->written += ready;
203
211k
                        if (m->written > memory_limit_) {
204
54
                            throw std::runtime_error("PL_Flate memory limit exceeded");
205
54
                        }
206
211k
                    }
207
211k
                    next()->write(m->outbuf.get(), ready);
208
211k
                    zstream.next_out = m->outbuf.get();
209
211k
                    zstream.avail_out = QIntC::to_uint(m->out_bufsize);
210
211k
                }
211
240k
            }
212
240k
            break;
213
214
240k
        default:
215
6.55k
            checkError("data", err);
216
249k
        }
217
249k
    }
218
249k
}
219
220
void
221
Pl_Flate::finish()
222
13.3k
{
223
13.3k
    if (m->written > memory_limit_) {
224
49
        throw std::runtime_error("PL_Flate memory limit exceeded");
225
49
    }
226
13.3k
    try {
227
13.3k
        if (m->zopfli_buf) {
228
0
            finish_zopfli();
229
13.3k
        } else if (m->outbuf.get()) {
230
13.3k
            if (m->initialized) {
231
12.4k
                z_stream& zstream = *(static_cast<z_stream*>(m->zdata));
232
12.4k
                unsigned char buf[1];
233
12.4k
                buf[0] = '\0';
234
12.4k
                handleData(buf, 0, Z_FINISH);
235
12.4k
                int err = Z_OK;
236
12.4k
                if (m->action == a_deflate) {
237
0
                    err = deflateEnd(&zstream);
238
12.4k
                } else {
239
12.4k
                    err = inflateEnd(&zstream);
240
12.4k
                }
241
12.4k
                m->initialized = false;
242
12.4k
                checkError("End", err);
243
12.4k
            }
244
245
13.3k
            m->outbuf = nullptr;
246
13.3k
        }
247
13.3k
    } catch (std::exception& e) {
248
2.79k
        try {
249
2.79k
            next()->finish();
250
2.79k
        } catch (...) {
251
            // ignore secondary exception
252
7
        }
253
2.79k
        throw std::runtime_error(e.what());
254
2.79k
    }
255
10.5k
    next()->finish();
256
10.5k
}
257
258
void
259
Pl_Flate::setCompressionLevel(int level)
260
0
{
261
0
    compression_level = level;
262
0
}
263
264
void
265
Pl_Flate::checkError(char const* prefix, int error_code)
266
29.5k
{
267
29.5k
    z_stream& zstream = *(static_cast<z_stream*>(m->zdata));
268
29.5k
    if (error_code != Z_OK) {
269
6.55k
        char const* action_str = (m->action == a_deflate ? "deflate" : "inflate");
270
6.55k
        std::string msg = identifier + ": " + action_str + ": " + prefix + ": ";
271
272
6.55k
        if (zstream.msg) {
273
6.15k
            msg += zstream.msg;
274
6.15k
        } else {
275
402
            switch (error_code) {
276
0
            case Z_ERRNO:
277
0
                msg += "zlib system error";
278
0
                break;
279
280
0
            case Z_STREAM_ERROR:
281
0
                msg += "zlib stream error";
282
0
                break;
283
284
0
            case Z_DATA_ERROR:
285
0
                msg += "zlib data error";
286
0
                break;
287
288
0
            case Z_MEM_ERROR:
289
0
                msg += "zlib memory error";
290
0
                break;
291
292
0
            case Z_BUF_ERROR:
293
0
                msg += "zlib buffer error";
294
0
                break;
295
296
0
            case Z_VERSION_ERROR:
297
0
                msg += "zlib version error";
298
0
                break;
299
300
402
            default:
301
402
                msg += std::string("zlib unknown error (") + std::to_string(error_code) + ")";
302
402
                break;
303
402
            }
304
402
        }
305
306
6.55k
        throw std::runtime_error(msg);
307
6.55k
    }
308
29.5k
}
309
310
void
311
Pl_Flate::finish_zopfli()
312
0
{
313
#ifdef ZOPFLI
314
    if (!m->zopfli_buf) {
315
        return;
316
    }
317
    auto buf = std::move(*m->zopfli_buf.release());
318
    ZopfliOptions z_opt;
319
    ZopfliInitOptions(&z_opt);
320
    unsigned char* out{nullptr};
321
    size_t out_size{0};
322
    ZopfliCompress(
323
        &z_opt,
324
        ZOPFLI_FORMAT_ZLIB,
325
        reinterpret_cast<unsigned char const*>(buf.c_str()),
326
        buf.size(),
327
        &out,
328
        &out_size);
329
    std::unique_ptr<unsigned char, decltype(&free)> p(out, &free);
330
    next()->write(out, out_size);
331
    // next()->finish is called by finish()
332
#endif
333
0
}
334
335
bool
336
Pl_Flate::zopfli_supported()
337
0
{
338
#ifdef ZOPFLI
339
    return true;
340
#else
341
0
    return false;
342
0
#endif
343
0
}
344
345
bool
346
Pl_Flate::zopfli_enabled()
347
0
{
348
0
    if (zopfli_supported()) {
349
0
        std::string value;
350
0
        static bool enabled = QUtil::get_env("QPDF_ZOPFLI", &value) && value != "disabled";
351
0
        return enabled;
352
0
    } else {
353
0
        return false;
354
0
    }
355
0
}
356
357
bool
358
Pl_Flate::zopfli_check_env(QPDFLogger* logger)
359
0
{
360
0
    if (Pl_Flate::zopfli_supported()) {
361
0
        return true;
362
0
    }
363
0
    std::string value;
364
0
    auto is_set = QUtil::get_env("QPDF_ZOPFLI", &value);
365
0
    if (!is_set || value == "disabled" || value == "silent") {
366
0
        return true;
367
0
    }
368
0
    if (!logger) {
369
0
        logger = QPDFLogger::defaultLogger().get();
370
0
    }
371
372
    // This behavior is known in QPDFJob (for the --zopfli argument), Pl_Flate.hh, README.md,
373
    // and the manual. Do a case-insensitive search for zopfli if changing the behavior.
374
0
    if (value == "force") {
375
0
        throw std::runtime_error("QPDF_ZOPFLI=force, and zopfli support is not enabled");
376
0
    }
377
0
    logger->warn("QPDF_ZOPFLI is set, but libqpdf was not built with zopfli support\n");
378
0
    logger->warn(
379
0
        "Set QPDF_ZOPFLI=silent to suppress this warning and use zopfli when available.\n");
380
0
    return false;
381
0
}