Coverage Report

Created: 2026-12-01 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/include/qpdf/global.hh
Line
Count
Source
1
// Copyright (c) 2005-2021 Jay Berkenbilt
2
// Copyright (c) 2022-2026 Jay Berkenbilt and Manfred Holger
3
//
4
// This file is part of qpdf.
5
//
6
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7
// in compliance with the License. You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software distributed under the License
12
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13
// or implied. See the License for the specific language governing permissions and limitations under
14
// the License.
15
//
16
// Versions of qpdf prior to version 7 were released under the terms of version 2.0 of the Artistic
17
// License. At your option, you may continue to consider qpdf to be licensed under those terms.
18
// Please see the manual for additional information.
19
20
#ifndef GLOBAL_HH
21
#define GLOBAL_HH
22
23
#include <qpdf/Constants.h>
24
25
#include <qpdf/QUtil.hh>
26
#include <qpdf/qpdf-c.h>
27
28
#include <cstdint>
29
30
namespace qpdf::global
31
{
32
    /// Helper function to translate result codes into C++ exceptions - for qpdf internal use only.
33
    inline void
34
    handle_result(qpdf_result_e result)
35
8.12k
    {
36
8.12k
        if (result != qpdf_r_ok) {
37
0
            QUtil::handle_result_code(result, "qpdf::global");
38
0
        }
39
8.12k
    }
40
41
    /// Helper function to wrap calls to qpdf_global_get_uint32 - for qpdf internal use only.
42
    inline uint32_t
43
    get_uint32(qpdf_param_e param)
44
0
    {
45
0
        uint32_t value;
46
0
        handle_result(qpdf_global_get_uint32(param, &value));
47
0
        return value;
48
0
    }
49
50
    /// Helper function to wrap calls to qpdf_global_set_uint32 - for qpdf internal use only.
51
    inline void
52
    set_uint32(qpdf_param_e param, uint32_t value)
53
8.12k
    {
54
8.12k
        handle_result(qpdf_global_set_uint32(param, value));
55
8.12k
    }
56
57
    /// @brief Retrieves the number of limit errors.
58
    ///
59
    /// Returns the number of times a global limit was exceeded. This item is read only.
60
    ///
61
    /// @return The number of limit errors.
62
    ///
63
    /// @since 12.3
64
    inline uint32_t
65
    limit_errors()
66
0
    {
67
0
        return get_uint32(qpdf_p_limit_errors);
68
0
    }
69
70
    namespace options
71
    {
72
        /// @brief  Retrieves whether inspection mode is set.
73
        ///
74
        /// @return True if inspection mode is set.
75
        ///
76
        /// @since 12.3
77
        inline bool
78
        inspection_mode()
79
0
        {
80
0
            return get_uint32(qpdf_p_inspection_mode) != 0;
81
0
        }
82
83
        /// @brief  Set inspection mode if `true` is passed.
84
        ///
85
        /// This function enables restrictive inspection mode if `true` is passed. Inspection mode
86
        /// must be enabled before a QPDF object is created. By default inspection mode is off.
87
        /// Calling `inspection_mode(false)` is not supported and currently is a no-op.
88
        ///
89
        /// @param value A boolean indicating whether to enable (true) inspection mode.
90
        ///
91
        /// @since 12.3
92
        inline void
93
        inspection_mode(bool value)
94
0
        {
95
0
            set_uint32(qpdf_p_inspection_mode, value ? QPDF_TRUE : QPDF_FALSE);
96
0
        }
97
98
        /// @brief  Retrieves whether fuzz mode is set.
99
        ///
100
        /// Fuzz mode is intended for use when building fuzzers and sets various global limits with
101
        /// the aim of avoiding spurious time-outs and out-of-memory errors as well as limiting the
102
        /// time spent fuzzing qpdf's dependencies instead of fuzzing qpdf itself. These limits
103
        /// cannot be imposed on qpdf during normal operation since legitimate PDF files can be very
104
        /// large and complex and as result may require more memory and longer runtimes than is
105
        /// allowed during fuzzing. By default fuzz mode is off.
106
        ///
107
        /// @return True if fuzz mode is set.
108
        ///
109
        /// @since 12.4
110
        inline bool
111
        fuzz_mode()
112
0
        {
113
0
            return get_uint32(qpdf_p_fuzz_mode) != 0;
114
0
        }
115
116
        /// @brief  Set fuzz mode if `true` is passed.
117
        ///
118
        /// This function enables fuzz mode if `true` is passed. Fuzz mode is intended for use when
119
        /// building fuzzers and sets various global limits with the aim of avoiding spurious
120
        /// time-outs and out-of-memory errors as well as limiting the time spent fuzzing qpdf's
121
        /// dependencies instead of fuzzing qpdf itself. These limits cannot be imposed on qpdf
122
        /// during normal operation since legitimate PDF files can be very large and complex and as
123
        /// result may require more memory and longer runtimes than is allowed during fuzzing. By
124
        /// default fuzz mode is off. Calling `fuzz_mode(false)` is not supported and currently is a
125
        /// no-op.
126
        ///
127
        /// @param value A boolean indicating whether to enable (true) fuzz mode.
128
        ///
129
        /// @since 12.4
130
        inline void
131
        fuzz_mode(bool value)
132
8.12k
        {
133
8.12k
            set_uint32(qpdf_p_fuzz_mode, value ? QPDF_TRUE : QPDF_FALSE);
134
8.12k
        }
135
136
        /// @brief  Retrieves whether DCT throw-on-corrupt-data option is set.
137
        ///
138
        /// When enabled, DCT decompression will treat corrupt data as an error and throw an
139
        /// exception. By default this option is enabled.
140
        ///
141
        /// @return True if DCT throw-on-corrupt-data is set.
142
        ///
143
        /// @since 12.4
144
        inline bool
145
        dct_throw_on_corrupt_data()
146
0
        {
147
0
            return get_uint32(qpdf_p_dct_throw_on_corrupt_data) != 0;
148
0
        }
149
150
        /// @brief  Set the DCT throw-on-corrupt-data option.
151
        ///
152
        /// When enabled, DCT decompression will treat corrupt data as an error and throw an
153
        /// exception. Passing `false` will cause the decompressor to attempt to continue on
154
        /// corrupt JPEG data where possible.
155
        ///
156
        /// @param value A boolean indicating whether to enable (true) or disable (false) the
157
        ///              behavior.
158
        ///
159
        /// @since 12.4
160
        inline void
161
        dct_throw_on_corrupt_data(bool value)
162
0
        {
163
0
            set_uint32(qpdf_p_dct_throw_on_corrupt_data, value ? QPDF_TRUE : QPDF_FALSE);
164
0
        }
165
166
        /// @brief  Retrieves whether default limits are enabled.
167
        ///
168
        /// @return True if default limits are enabled.
169
        ///
170
        /// @since 12.3
171
        inline bool
172
        default_limits()
173
0
        {
174
0
            return get_uint32(qpdf_p_default_limits) != 0;
175
0
        }
176
177
        /// @brief  Disable all optional default limits if `false` is passed.
178
        ///
179
        /// This function disables all optional default limits if `false` is passed. Once default
180
        /// values have been disabled they cannot be re-enabled. Passing `true` has no effect. This
181
        /// function will leave any limits that have been explicitly set unchanged. Some limits,
182
        /// such as limits imposed to avoid stack overflows, cannot be disabled but can be changed.
183
        ///
184
        /// @param value A boolean indicating whether to disable (false) the default limits.
185
        ///
186
        /// @since 12.3
187
        inline void
188
        default_limits(bool value)
189
0
        {
190
0
            set_uint32(qpdf_p_default_limits, value ? QPDF_TRUE : QPDF_FALSE);
191
0
        }
192
193
    } // namespace options
194
195
    namespace limits
196
    {
197
        /// @brief Retrieves the default maximum number of warnings accepted per document.
198
        ///
199
        /// This value provides a default for `QPDF::setMaxWarnings`. A value of 0 indicates no
200
        /// limit. The default is 0.
201
        ///
202
        /// @return The configured default maximum warnings per document.
203
        ///
204
        /// @since 12.4
205
        inline uint32_t
206
        doc_max_warnings()
207
0
        {
208
0
            return get_uint32(qpdf_p_doc_max_warnings);
209
0
        }
210
211
        /// @brief Sets the default maximum number of warnings accepted per document.
212
        ///
213
        /// This value is used as the default in newly-created `QPDF` objects as the initial
214
        /// setting for `QPDF::setMaxWarnings`.
215
        ///
216
        /// @param val The maximum warnings per document to set. A value of 0 disables the limit.
217
        ///
218
        /// @since 12.4
219
        inline void
220
        doc_max_warnings(uint32_t val)
221
0
        {
222
0
            set_uint32(qpdf_p_doc_max_warnings, val);
223
0
        }
224
225
        /// @brief Retrieves the maximum nesting level while parsing objects.
226
        ///
227
        /// @return The maximum nesting level while parsing objects.
228
        ///
229
        /// @note The maximum nesting level cannot be disabled by calling `default_limits(false)`.
230
        ///
231
        /// @since 12.3
232
        inline uint32_t
233
        parser_max_nesting()
234
0
        {
235
0
            return get_uint32(qpdf_p_parser_max_nesting);
236
0
        }
237
238
        /// @brief Sets the maximum nesting level while parsing objects.
239
        ///
240
        /// @param value The maximum nesting level to set.
241
        ///
242
        /// @note The maximum nesting level cannot be disabled by calling `default_limits(false)`.
243
        ///
244
        /// @since 12.3
245
        inline void
246
        parser_max_nesting(uint32_t value)
247
0
        {
248
0
            set_uint32(qpdf_p_parser_max_nesting, value);
249
0
        }
250
251
        /// @brief Retrieves the maximum number of errors allowed while parsing objects.
252
        ///
253
        /// A value of 0 means that there is no maximum imposed.
254
        ///
255
        /// @return The maximum number of errors allowed while parsing objects.
256
        ///
257
        /// @since 12.3
258
        inline uint32_t
259
        parser_max_errors()
260
0
        {
261
0
            return get_uint32(qpdf_p_parser_max_errors);
262
0
        }
263
264
        /// Sets the maximum number of errors allowed while parsing objects.
265
        ///
266
        /// A value of 0 means that there is no maximum imposed.
267
        ///
268
        /// @param value The maximum number of errors allowed while parsing objects to set.
269
        ///
270
        /// @since 12.3
271
        inline void
272
        parser_max_errors(uint32_t value)
273
0
        {
274
0
            set_uint32(qpdf_p_parser_max_errors, value);
275
0
        }
276
277
        /// @brief Retrieves the maximum number of top-level objects allowed in a container while
278
        ///        parsing.
279
        ///
280
        /// The limit applies when the PDF document's xref table is undamaged and the object itself
281
        /// can be parsed without errors. The default limit is 4,294,967,295.
282
        ///
283
        /// @return The maximum number of top-level objects allowed in a container while parsing
284
        ///         objects.
285
        ///
286
        /// @since 12.3
287
        inline uint32_t
288
        parser_max_container_size()
289
0
        {
290
0
            return get_uint32(qpdf_p_parser_max_container_size);
291
0
        }
292
293
        /// @brief Sets the maximum number of top-level objects allowed in a container while
294
        ///        parsing.
295
        ///
296
        /// The limit applies when the PDF document's xref table is undamaged and the object itself
297
        /// can be parsed without errors. The default limit is 4,294,967,295.
298
        ///
299
        /// @param value  The maximum number of top-level objects allowed in a container while
300
        ///               parsing objects to set.
301
        ///
302
        /// @since 12.3
303
        inline void
304
        parser_max_container_size(uint32_t value)
305
0
        {
306
0
            set_uint32(qpdf_p_parser_max_container_size, value);
307
0
        }
308
309
        /// @brief Retrieves the maximum number of top-level objects allowed in a container while
310
        ///        parsing objects.
311
        ///
312
        /// The limit applies when the PDF document's xref table is damaged or the object itself is
313
        /// damaged. The limit also applies when parsing xref streams. The default limit is 5,000.
314
        ///
315
        /// @return The maximum number of top-level objects allowed in a container while parsing
316
        ///         objects.
317
        ///
318
        /// @since 12.3
319
        inline uint32_t
320
        parser_max_container_size_damaged()
321
0
        {
322
0
            return get_uint32(qpdf_p_parser_max_container_size_damaged);
323
0
        }
324
325
        /// @brief Sets the maximum number of top-level objects allowed in a container while
326
        ///        parsing.
327
        ///
328
        /// The limit applies when the PDF document's xref table is damaged or the object itself is
329
        /// damaged. The limit also applies when parsing trailer dictionaries and xref streams. The
330
        /// default limit is 5,000.
331
        ///
332
        /// @param value  The maximum number of top-level objects allowed in a container while
333
        ///               parsing objects to set.
334
        ///
335
        /// @since 12.3
336
        inline void
337
        parser_max_container_size_damaged(uint32_t value)
338
0
        {
339
0
            set_uint32(qpdf_p_parser_max_container_size_damaged, value);
340
0
        }
341
342
        /// @brief Retrieves the maximum number of filters allowed when filtering streams.
343
        ///
344
        /// An excessive number of stream filters is usually a sign that a file is damaged or
345
        /// specially constructed. If the maximum is exceeded for a stream the stream is treated as
346
        /// unfilterable. The default maximum is 25.
347
        ///
348
        /// @return The maximum number of filters allowed when filtering streams.
349
        ///
350
        /// @since 12.3
351
        inline uint32_t
352
        max_stream_filters()
353
0
        {
354
0
            return get_uint32(qpdf_p_max_stream_filters);
355
0
        }
356
357
        /// @brief Sets the maximum number of filters allowed when filtering streams.
358
        ///
359
        /// An excessive number of stream filters is usually a sign that a file is damaged or
360
        /// specially constructed. If the maximum is exceeded for a stream the stream is treated as
361
        /// unfilterable. The default maximum is 25.
362
        ///
363
        /// @param value  The maximum number of filters allowed when filtering streams to set.
364
        ///
365
        /// @since 12.3
366
        inline void
367
        max_stream_filters(uint32_t value)
368
0
        {
369
0
            set_uint32(qpdf_p_max_stream_filters, value);
370
0
        }
371
372
        /// @brief  Retrieves the maximum amount of memory in bytes allowed for DCT (JPEG)
373
        ///         decompression.
374
        ///
375
        /// Streams requiring memory in excess of the limit for decompression are treated as
376
        /// unfilterable. A value of 0 indicates no limit. The default is 0.
377
        ///
378
        /// @return The configured maximum memory in bytes for DCT decompression.
379
        ///
380
        /// @since 12.4
381
        inline uint32_t
382
        dct_max_memory()
383
0
        {
384
0
            return get_uint32(qpdf_p_dct_max_memory);
385
0
        }
386
387
        /// @brief  Sets the maximum amount of memory in bytes allowed for DCT (JPEG) decompression.
388
        ///
389
        /// Streams requiring memory in excess of the limit for decompression are treated as
390
        /// unfilterable. A value of 0 indicates no limit. The default is 0.
391
        ///
392
        /// @param val The maximum memory in bytes to allow for DCT decompression.
393
        ///
394
        /// @since 12.4
395
        inline void
396
        dct_max_memory(uint32_t val)
397
0
        {
398
0
            set_uint32(qpdf_p_dct_max_memory, val);
399
0
        }
400
401
        /// @brief  Retrieves the maximum number of progressive scans allowed for DCT (JPEG)
402
        ///         decompression.
403
        ///
404
        /// Streams exceeding the limit are treated as unfilterable. A value of 0 indicates no
405
        /// limit. The default is 0.
406
        ///
407
        /// @return The configured maximum progressive scans for DCT decompression.
408
        ///
409
        /// @since 12.4
410
        inline uint32_t
411
        dct_max_progressive_scans()
412
0
        {
413
0
            return get_uint32(qpdf_p_dct_max_progressive_scans);
414
0
        }
415
416
        /// @brief  Sets the maximum number of progressive scans allowed for DCT (JPEG)
417
        ///         decompression.
418
        ///
419
        /// Streams exceeding the limit are treated as unfilterable. A value of 0 indicates no
420
        /// limit. The default is 0.
421
        ///
422
        /// @param val The maximum progressive scans to allow for DCT operations.
423
        ///
424
        /// @since 12.4
425
        inline void
426
        dct_max_progressive_scans(uint32_t val)
427
0
        {
428
0
            set_uint32(qpdf_p_dct_max_progressive_scans, val);
429
0
        }
430
431
        /// @brief  Retrieves the maximum amount of memory in bytes allowed for Flate (zlib)
432
        ///         decompression/processing.
433
        ///
434
        /// Streams requiring memory in excess of the limit for Flate processing are treated as
435
        /// unfilterable. A value of 0 indicates no limit. The default is 0.
436
        ///
437
        /// @return The configured maximum memory in bytes for Flate processing.
438
        ///
439
        /// @since 12.4
440
        inline uint32_t
441
        flate_max_memory()
442
0
        {
443
0
            return get_uint32(qpdf_p_flate_max_memory);
444
0
        }
445
446
        /// @brief  Sets the maximum amount of memory in bytes allowed for Flate (zlib)
447
        ///         decompression/processing.
448
        ///
449
        /// Streams requiring memory in excess of the limit for processing are treated as
450
        /// unfilterable. A value of 0 indicates no limit. The default is 0.
451
        ///
452
        /// @param val The maximum memory in bytes to allow for Flate processing.
453
        ///
454
        /// @since 12.4
455
        inline void
456
        flate_max_memory(uint32_t val)
457
0
        {
458
0
            set_uint32(qpdf_p_flate_max_memory, val);
459
0
        }
460
461
        /// @brief  Retrieves the maximum amount of memory in bytes allowed for PNG filter
462
        ///         processing.
463
        ///
464
        /// Streams requiring memory in excess of the limit are treated as unfilterable. A value of
465
        /// 0 indicates no limit. The default is 0.
466
        ///
467
        /// @return The configured maximum memory in bytes for PNG processing.
468
        ///
469
        /// @since 12.4
470
        inline uint32_t
471
        png_max_memory()
472
0
        {
473
0
            return get_uint32(qpdf_p_png_max_memory);
474
0
        }
475
476
        /// @brief  Sets the maximum amount of memory in bytes allowed for PNG filter processing.
477
        ///
478
        /// Streams requiring memory in excess of the limit are treated as unfilterable. A value of
479
        /// 0 indicates no limit. The default is 0.
480
        ///
481
        /// @param val The maximum memory in bytes to allow for PNG processing.
482
        ///
483
        /// @since 12.4
484
        inline void
485
        png_max_memory(uint32_t val)
486
0
        {
487
0
            set_uint32(qpdf_p_png_max_memory, val);
488
0
        }
489
490
        /// @brief  Retrieves the maximum amount of memory in bytes allowed for RunLength
491
        ///         decoding/processing.
492
        ///
493
        /// Streams requiring memory in excess of the limit for run-length processing are treated
494
        /// as unfilterable. A value of 0 indicates no limit. The default is 0.
495
        ///
496
        /// @return The configured maximum memory in bytes for run-length processing.
497
        ///
498
        /// @since 12.4
499
        inline uint32_t
500
        run_length_max_memory()
501
0
        {
502
0
            return get_uint32(qpdf_p_run_length_max_memory);
503
0
        }
504
505
        /// @brief  Sets the maximum amount of memory in bytes allowed for RunLength
506
        ///         decoding/processing.
507
        ///
508
        /// Streams requiring memory in excess of the limit for processing are treated as
509
        /// unfilterable. A value of 0 indicates no limit. The default is 0.
510
        ///
511
        /// @param val The maximum memory in bytes to allow for run-length processing.
512
        ///
513
        /// @since 12.4
514
        inline void
515
        run_length_max_memory(uint32_t val)
516
0
        {
517
0
            set_uint32(qpdf_p_run_length_max_memory, val);
518
0
        }
519
520
        /// @brief  Retrieves the maximum amount of memory in bytes allowed for TIFF processing.
521
        ///
522
        /// Streams requiring memory in excess of the limit for TIFF processing are treated as
523
        /// unfilterable. A value of 0 indicates no limit. The default is 0.
524
        ///
525
        /// @return The configured maximum memory in bytes for TIFF processing.
526
        ///
527
        /// @since 12.4
528
        inline uint32_t
529
        tiff_max_memory()
530
0
        {
531
0
            return get_uint32(qpdf_p_tiff_max_memory);
532
0
        }
533
534
        /// @brief  Sets the maximum amount of memory in bytes allowed for TIFF processing.
535
        ///
536
        /// Streams requiring memory in excess of the limit for processing are treated as
537
        /// unfilterable. A value of 0 indicates no limit. The default is 0.
538
        ///
539
        /// @param val The maximum memory in bytes to allow for TIFF processing.
540
        ///
541
        /// @since 12.4
542
        inline void
543
        tiff_max_memory(uint32_t val)
544
0
        {
545
0
            set_uint32(qpdf_p_tiff_max_memory, val);
546
0
        }
547
548
    } // namespace limits
549
550
} // namespace qpdf::global
551
552
#endif // GLOBAL_HH