/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 | 27.6M | { |
25 | 27.6M | if (!cond) { |
26 | 0 | throw std::logic_error(std::forward<T>(msg)); |
27 | 0 | } |
28 | 27.6M | } void qpdf::util::assertion<char const (&) [44]>(bool, char const (&) [44]) Line | Count | Source | 24 | 131k | { | 25 | 131k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 131k | } |
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> >&&) Line | Count | Source | 24 | 183k | { | 25 | 183k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 183k | } |
void qpdf::util::assertion<char const (&) [54]>(bool, char const (&) [54]) Line | Count | Source | 24 | 154k | { | 25 | 154k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 154k | } |
void qpdf::util::assertion<char const (&) [46]>(bool, char const (&) [46]) Line | Count | Source | 24 | 761 | { | 25 | 761 | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 761 | } |
void qpdf::util::assertion<char const (&) [48]>(bool, char const (&) [48]) Line | Count | Source | 24 | 12.9k | { | 25 | 12.9k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 12.9k | } |
void qpdf::util::assertion<char const (&) [52]>(bool, char const (&) [52]) Line | Count | Source | 24 | 25.9M | { | 25 | 25.9M | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 25.9M | } |
void qpdf::util::assertion<char const (&) [49]>(bool, char const (&) [49]) Line | Count | Source | 24 | 3.38k | { | 25 | 3.38k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 3.38k | } |
void qpdf::util::assertion<char const (&) [53]>(bool, char const (&) [53]) Line | Count | Source | 24 | 2.98k | { | 25 | 2.98k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 2.98k | } |
void qpdf::util::assertion<char const (&) [56]>(bool, char const (&) [56]) Line | Count | Source | 24 | 890 | { | 25 | 890 | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 890 | } |
Unexecuted instantiation: void qpdf::util::assertion<char const (&) [71]>(bool, char const (&) [71]) void qpdf::util::assertion<char const (&) [57]>(bool, char const (&) [57]) Line | Count | Source | 24 | 636 | { | 25 | 636 | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 636 | } |
void qpdf::util::assertion<char const (&) [51]>(bool, char const (&) [51]) Line | Count | Source | 24 | 1.66k | { | 25 | 1.66k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 1.66k | } |
Unexecuted instantiation: void qpdf::util::assertion<char const (&) [41]>(bool, char const (&) [41]) void qpdf::util::assertion<char const (&) [45]>(bool, char const (&) [45]) Line | Count | Source | 24 | 686k | { | 25 | 686k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 686k | } |
void qpdf::util::assertion<char const (&) [35]>(bool, char const (&) [35]) Line | Count | Source | 24 | 6.65k | { | 25 | 6.65k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 6.65k | } |
void qpdf::util::assertion<char const (&) [68]>(bool, char const (&) [68]) Line | Count | Source | 24 | 5.28k | { | 25 | 5.28k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 5.28k | } |
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> >&) void qpdf::util::assertion<char const (&) [83]>(bool, char const (&) [83]) Line | Count | Source | 24 | 99.5k | { | 25 | 99.5k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 99.5k | } |
void qpdf::util::assertion<char const (&) [65]>(bool, char const (&) [65]) Line | Count | Source | 24 | 3.02k | { | 25 | 3.02k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 3.02k | } |
void qpdf::util::assertion<char const (&) [43]>(bool, char const (&) [43]) Line | Count | Source | 24 | 61.4k | { | 25 | 61.4k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 61.4k | } |
void qpdf::util::assertion<char const (&) [50]>(bool, char const (&) [50]) Line | Count | Source | 24 | 252k | { | 25 | 252k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 252k | } |
void qpdf::util::assertion<char const (&) [58]>(bool, char const (&) [58]) Line | Count | Source | 24 | 319 | { | 25 | 319 | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 319 | } |
void qpdf::util::assertion<char const (&) [47]>(bool, char const (&) [47]) Line | Count | Source | 24 | 95.6k | { | 25 | 95.6k | if (!cond) { | 26 | 0 | throw std::logic_error(std::forward<T>(msg)); | 27 | 0 | } | 28 | 95.6k | } |
|
29 | | |
30 | | template <typename T> |
31 | | inline void |
32 | | internal_error_if(bool cond, T&& msg) |
33 | 9.15M | { |
34 | 9.15M | if (cond) { |
35 | 1 | throw std::logic_error("INTERNAL ERROR: "s.append(std::forward<T>(msg)) |
36 | 1 | .append( |
37 | 1 | "\nThis is a qpdf bug. Please report at " |
38 | 1 | "https://github.com/qpdf/qpdf/issues")); |
39 | 1 | } |
40 | 9.15M | } void qpdf::util::internal_error_if<char const (&) [29]>(bool, char const (&) [29]) Line | Count | Source | 33 | 5.07M | { | 34 | 5.07M | if (cond) { | 35 | 0 | throw std::logic_error("INTERNAL ERROR: "s.append(std::forward<T>(msg)) | 36 | 0 | .append( | 37 | 0 | "\nThis is a qpdf bug. Please report at " | 38 | 0 | "https://github.com/qpdf/qpdf/issues")); | 39 | 0 | } | 40 | 5.07M | } |
void qpdf::util::internal_error_if<char const (&) [28]>(bool, char const (&) [28]) Line | Count | Source | 33 | 1.66M | { | 34 | 1.66M | if (cond) { | 35 | 0 | throw std::logic_error("INTERNAL ERROR: "s.append(std::forward<T>(msg)) | 36 | 0 | .append( | 37 | 0 | "\nThis is a qpdf bug. Please report at " | 38 | 0 | "https://github.com/qpdf/qpdf/issues")); | 39 | 0 | } | 40 | 1.66M | } |
void qpdf::util::internal_error_if<char const (&) [34]>(bool, char const (&) [34]) Line | Count | Source | 33 | 2.40M | { | 34 | 2.40M | if (cond) { | 35 | 1 | throw std::logic_error("INTERNAL ERROR: "s.append(std::forward<T>(msg)) | 36 | 1 | .append( | 37 | 1 | "\nThis is a qpdf bug. Please report at " | 38 | 1 | "https://github.com/qpdf/qpdf/issues")); | 39 | 1 | } | 40 | 2.40M | } |
Unexecuted instantiation: void qpdf::util::internal_error_if<char const (&) [44]>(bool, char const (&) [44]) |
41 | | |
42 | | template <typename T> |
43 | | inline void |
44 | | no_ci_rt_error_if(bool cond, T&& msg) |
45 | 7.28M | { |
46 | 7.28M | if (cond) { |
47 | 102 | throw std::runtime_error(std::forward<T>(msg)); |
48 | 102 | } |
49 | 7.28M | } void qpdf::util::no_ci_rt_error_if<char const (&) [73]>(bool, char const (&) [73]) Line | Count | Source | 45 | 95 | { | 46 | 95 | if (cond) { | 47 | 0 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 0 | } | 49 | 95 | } |
void qpdf::util::no_ci_rt_error_if<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> >&&) Line | Count | Source | 45 | 58.4k | { | 46 | 58.4k | if (cond) { | 47 | 0 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 0 | } | 49 | 58.4k | } |
void qpdf::util::no_ci_rt_error_if<char const (&) [69]>(bool, char const (&) [69]) Line | Count | Source | 45 | 10.4k | { | 46 | 10.4k | if (cond) { | 47 | 28 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 28 | } | 49 | 10.4k | } |
void qpdf::util::no_ci_rt_error_if<char const (&) [59]>(bool, char const (&) [59]) Line | Count | Source | 45 | 96.4k | { | 46 | 96.4k | if (cond) { | 47 | 0 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 0 | } | 49 | 96.4k | } |
void qpdf::util::no_ci_rt_error_if<char const (&) [49]>(bool, char const (&) [49]) Line | Count | Source | 45 | 2.26k | { | 46 | 2.26k | if (cond) { | 47 | 3 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 3 | } | 49 | 2.26k | } |
void qpdf::util::no_ci_rt_error_if<char const (&) [45]>(bool, char const (&) [45]) Line | Count | Source | 45 | 1.35k | { | 46 | 1.35k | if (cond) { | 47 | 0 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 0 | } | 49 | 1.35k | } |
void qpdf::util::no_ci_rt_error_if<char const (&) [32]>(bool, char const (&) [32]) Line | Count | Source | 45 | 1.35k | { | 46 | 1.35k | if (cond) { | 47 | 0 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 0 | } | 49 | 1.35k | } |
void qpdf::util::no_ci_rt_error_if<char const (&) [35]>(bool, char const (&) [35]) Line | Count | Source | 45 | 44.1k | { | 46 | 44.1k | if (cond) { | 47 | 3 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 3 | } | 49 | 44.1k | } |
void qpdf::util::no_ci_rt_error_if<char const (&) [53]>(bool, char const (&) [53]) Line | Count | Source | 45 | 890 | { | 46 | 890 | if (cond) { | 47 | 3 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 3 | } | 49 | 890 | } |
void qpdf::util::no_ci_rt_error_if<char const (&) [51]>(bool, char const (&) [51]) Line | Count | Source | 45 | 3.17M | { | 46 | 3.17M | if (cond) { | 47 | 21 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 21 | } | 49 | 3.17M | } |
void qpdf::util::no_ci_rt_error_if<char const (&) [36]>(bool, char const (&) [36]) Line | Count | Source | 45 | 876 | { | 46 | 876 | if (cond) { | 47 | 3 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 3 | } | 49 | 876 | } |
void qpdf::util::no_ci_rt_error_if<char const (&) [23]>(bool, char const (&) [23]) Line | Count | Source | 45 | 3.84M | { | 46 | 3.84M | if (cond) { | 47 | 13 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 13 | } | 49 | 3.84M | } |
void qpdf::util::no_ci_rt_error_if<char const (&) [44]>(bool, char const (&) [44]) Line | Count | Source | 45 | 29.4k | { | 46 | 29.4k | if (cond) { | 47 | 28 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 28 | } | 49 | 29.4k | } |
void qpdf::util::no_ci_rt_error_if<char const (&) [42]>(bool, char const (&) [42]) Line | Count | Source | 45 | 29.1k | { | 46 | 29.1k | if (cond) { | 47 | 0 | throw std::runtime_error(std::forward<T>(msg)); | 48 | 0 | } | 49 | 29.1k | } |
|
50 | | |
51 | | inline constexpr char |
52 | | hex_decode_char(char digit) |
53 | 36.8M | { |
54 | 36.8M | return digit <= '9' && digit >= '0' |
55 | 36.8M | ? char(digit - '0') |
56 | 36.8M | : (digit >= 'a' ? char(digit - 'a' + 10) |
57 | 6.45M | : (digit >= 'A' ? char(digit - 'A' + 10) : '\20')); |
58 | 36.8M | } |
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 | 30.2M | { |
69 | 30.2M | return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\f' || ch == '\v'; |
70 | 30.2M | } |
71 | | |
72 | | inline bool |
73 | | is_digit(char ch) |
74 | 14.9M | { |
75 | 14.9M | return (ch >= '0' && ch <= '9'); |
76 | 14.9M | } |
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 | 52.8M | { |
82 | 52.8M | static auto constexpr hexchars = "0123456789abcdef"; |
83 | 52.8M | return {'#', hexchars[static_cast<unsigned char>(c) >> 4], hexchars[c & 0x0f]}; |
84 | 52.8M | } |
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 | 200k | { |
104 | 200k | return str.starts_with("\xfe\xff") || str.starts_with("\xff\xfe"); |
105 | 200k | } |
106 | | |
107 | | inline bool |
108 | | is_explicit_utf8(std::string const& str) |
109 | 135k | { |
110 | | // QPDF_String.cc knows that this is a 3-byte sequence. |
111 | 135k | return str.starts_with("\xef\xbb\xbf"); |
112 | 135k | } |
113 | | |
114 | | std::string random_string(size_t len); |
115 | | |
116 | | } // namespace qpdf::util |
117 | | |
118 | | #endif // UTIL_HH |