Coverage Report

Created: 2026-02-26 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/qpdf/Util.hh
Line
Count
Source
1
#ifndef UTIL_HH
2
#define UTIL_HH
3
4
#include <qpdf/assert_debug.h>
5
6
#include <stdexcept>
7
#include <string>
8
#include <utility>
9
10
using namespace std::literals;
11
12
namespace qpdf::util
13
{
14
    // qpdf::util is a collection of useful utility functions for qpdf internal use. It includes
15
    // inline functions, some of which are exposed as regular functions in QUtil. Implementations
16
    // are in QUtil.cc.
17
18
    // Throw a logic_error if 'cond' does not hold.
19
    //
20
    // DO NOT USE unless it is impractical or unnecessary to cover violations during CI Testing.
21
    template <typename T>
22
    inline void
23
    assertion(bool cond, T&& msg)
24
179
    {
25
179
        if (!cond) {
26
0
            throw std::logic_error(std::forward<T>(msg));
27
0
        }
28
179
    }
void qpdf::util::assertion<char const (&) [52]>(bool, char const (&) [52])
Line
Count
Source
24
179
    {
25
179
        if (!cond) {
26
0
            throw std::logic_error(std::forward<T>(msg));
27
0
        }
28
179
    }
Unexecuted instantiation: void qpdf::util::assertion<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&&)
29
30
    template <typename T>
31
    inline void
32
    internal_error_if(bool cond, T&& msg)
33
    {
34
        if (cond) {
35
            throw std::logic_error("INTERNAL ERROR: "s.append(std::forward<T>(msg))
36
                                       .append(
37
                                           "\nThis is a qpdf bug. Please report at "
38
                                           "https://github.com/qpdf/qpdf/issues"));
39
        }
40
    }
41
42
    template <typename T>
43
    inline void
44
    no_ci_rt_error_if(bool cond, T&& msg)
45
716
    {
46
716
        if (cond) {
47
0
            throw std::runtime_error(std::forward<T>(msg));
48
0
        }
49
716
    }
void qpdf::util::no_ci_rt_error_if<char const (&) [49]>(bool, char const (&) [49])
Line
Count
Source
45
179
    {
46
179
        if (cond) {
47
0
            throw std::runtime_error(std::forward<T>(msg));
48
0
        }
49
179
    }
void qpdf::util::no_ci_rt_error_if<char const (&) [69]>(bool, char const (&) [69])
Line
Count
Source
45
179
    {
46
179
        if (cond) {
47
0
            throw std::runtime_error(std::forward<T>(msg));
48
0
        }
49
179
    }
void qpdf::util::no_ci_rt_error_if<char const (&) [45]>(bool, char const (&) [45])
Line
Count
Source
45
179
    {
46
179
        if (cond) {
47
0
            throw std::runtime_error(std::forward<T>(msg));
48
0
        }
49
179
    }
void qpdf::util::no_ci_rt_error_if<char const (&) [32]>(bool, char const (&) [32])
Line
Count
Source
45
179
    {
46
179
        if (cond) {
47
0
            throw std::runtime_error(std::forward<T>(msg));
48
0
        }
49
179
    }
50
51
    inline constexpr char
52
    hex_decode_char(char digit)
53
0
    {
54
0
        return digit <= '9' && digit >= '0'
55
0
            ? char(digit - '0')
56
0
            : (digit >= 'a' ? char(digit - 'a' + 10)
57
0
                            : (digit >= 'A' ? char(digit - 'A' + 10) : '\20'));
58
0
    }
59
60
    inline constexpr bool
61
    is_hex_digit(char ch)
62
0
    {
63
0
        return hex_decode_char(ch) < '\20';
64
0
    }
65
66
    inline constexpr bool
67
    is_space(char ch)
68
0
    {
69
0
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\f' || ch == '\v';
70
0
    }
71
72
    inline bool
73
    is_digit(char ch)
74
0
    {
75
0
        return (ch >= '0' && ch <= '9');
76
0
    }
77
78
    // Returns lower-case hex-encoded version of the char including a leading "#".
79
    inline std::string
80
    hex_encode_char(char c)
81
0
    {
82
0
        static auto constexpr hexchars = "0123456789abcdef";
83
0
        return {'#', hexchars[static_cast<unsigned char>(c) >> 4], hexchars[c & 0x0f]};
84
0
    }
85
86
    // Numerically increment a digit string. Ignore the last 'tail' characters.
87
    inline void
88
    increment(std::string& s, int tail = 0)
89
0
    {
90
0
        auto end = s.rend();
91
0
        for (auto it = s.rbegin() + tail; it != end; ++it) {
92
0
            ++*it;
93
0
            if (*it != ':') {
94
0
                return;
95
0
            }
96
0
            *it = '0';
97
0
        }
98
0
        s.insert(0, 1, '1');
99
0
    }
100
101
    inline bool
102
    is_utf16(std::string const& str)
103
0
    {
104
0
        return str.starts_with("\xfe\xff") || str.starts_with("\xff\xfe");
105
0
    }
106
107
    inline bool
108
    is_explicit_utf8(std::string const& str)
109
0
    {
110
0
        // QPDF_String.cc knows that this is a 3-byte sequence.
111
0
        return str.starts_with("\xef\xbb\xbf");
112
0
    }
113
114
    std::string random_string(size_t len);
115
116
} // namespace qpdf::util
117
118
#endif // UTIL_HH