Coverage Report

Created: 2026-11-12 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/QPDF_String.cc
Line
Count
Source
1
#include <qpdf/QPDFObject_private.hh>
2
3
#include <qpdf/QPDFObjectHandle_private.hh>
4
#include <qpdf/QUtil.hh>
5
#include <qpdf/Util.hh>
6
7
// DO NOT USE ctype -- it is locale dependent for some things, and it's not worth the risk of
8
// including it in case it may accidentally be used.
9
10
static bool
11
is_iso_latin1_printable(char ch)
12
4.34M
{
13
4.34M
    return (ch >= 32 && ch <= 126) || static_cast<unsigned char>(ch) >= 160;
14
4.34M
}
15
16
void
17
QPDF_String::writeJSON(int json_version, JSON::Writer& p)
18
22.4k
{
19
22.4k
    if (json_version == 1) {
20
0
        if (util::is_utf16(val)) {
21
0
            p << "\"" << JSON::Writer::encode_string(QUtil::utf16_to_utf8(val)) << "\"";
22
0
            return;
23
0
        }
24
0
        if (util::is_explicit_utf8(val)) {
25
            // PDF 2.0 allows UTF-8 strings when explicitly prefixed with the three-byte
26
            // representation of U+FEFF.
27
0
            p << "\"" << JSON::Writer::encode_string(val.substr(3)) << "\"";
28
0
            return;
29
0
        }
30
0
        p << "\"" << JSON::Writer::encode_string(QUtil::pdf_doc_to_utf8(val)) << "\"";
31
0
        return;
32
0
    }
33
    // See if we can unambiguously represent as Unicode.
34
22.4k
    if (util::is_utf16(val)) {
35
1.53k
        p << "\"u:" << JSON::Writer::encode_string(QUtil::utf16_to_utf8(val)) << "\"";
36
1.53k
        return;
37
1.53k
    }
38
    // See if we can unambiguously represent as Unicode.
39
20.8k
    if (util::is_explicit_utf8(val)) {
40
1.10k
        p << "\"u:" << JSON::Writer::encode_string(val.substr(3)) << "\"";
41
1.10k
        return;
42
1.10k
    }
43
19.7k
    if (!useHexString()) {
44
9.25k
        auto candidate = QUtil::pdf_doc_to_utf8(val);
45
9.25k
        std::string test;
46
9.25k
        if (QUtil::utf8_to_pdf_doc(candidate, test, '?') && test == val) {
47
            // This is a PDF-doc string that can be losslessly encoded as Unicode.
48
8.96k
            p << "\"u:" << JSON::Writer::encode_string(candidate) << "\"";
49
8.96k
            return;
50
8.96k
        }
51
9.25k
    }
52
10.7k
    p << "\"b:" << QUtil::hex_encode(val) << "\"";
53
10.7k
}
54
55
bool
56
QPDF_String::useHexString() const
57
153k
{
58
    // Heuristic: use the hexadecimal representation of a string if there are any non-printable (in
59
    // PDF Doc encoding) characters or if too large of a proportion of the string consists of
60
    // non-ASCII characters.
61
153k
    unsigned int non_ascii = 0;
62
41.5M
    for (auto const ch: val) {
63
41.5M
        if (ch > 126) {
64
91.2k
            ++non_ascii;
65
41.4M
        } else if (ch >= 32) {
66
5.64M
            continue;
67
35.8M
        } else if (ch < 0 || ch >= 24) {
68
3.38M
            ++non_ascii;
69
32.4M
        } else if (!(ch == '\n' || ch == '\r' || ch == '\t' || ch == '\b' || ch == '\f')) {
70
54.8k
            return true;
71
54.8k
        }
72
41.5M
    }
73
98.2k
    return 5 * non_ascii > val.length();
74
153k
}
75
76
std::string
77
QPDF_String::unparse(bool force_binary)
78
133k
{
79
133k
    bool use_hexstring = force_binary || useHexString();
80
133k
    std::string result;
81
133k
    if (use_hexstring) {
82
60.4k
        static auto constexpr hexchars = "0123456789abcdef";
83
60.4k
        result.reserve(2 * val.length() + 2);
84
60.4k
        result += '<';
85
35.3M
        for (const char c: val) {
86
35.3M
            result += hexchars[static_cast<unsigned char>(c) >> 4];
87
35.3M
            result += hexchars[c & 0x0f];
88
35.3M
        }
89
60.4k
        result += '>';
90
72.8k
    } else {
91
72.8k
        result += "(";
92
29.4M
        for (unsigned int i = 0; i < val.length(); ++i) {
93
29.4M
            char ch = val.at(i);
94
29.4M
            switch (ch) {
95
189k
            case '\n':
96
189k
                result += "\\n";
97
189k
                break;
98
99
3.79k
            case '\r':
100
3.79k
                result += "\\r";
101
3.79k
                break;
102
103
24.7M
            case '\t':
104
24.7M
                result += "\\t";
105
24.7M
                break;
106
107
21.0k
            case '\b':
108
21.0k
                result += "\\b";
109
21.0k
                break;
110
111
114k
            case '\f':
112
114k
                result += "\\f";
113
114k
                break;
114
115
8.89k
            case '(':
116
8.89k
                result += "\\(";
117
8.89k
                break;
118
119
8.97k
            case ')':
120
8.97k
                result += "\\)";
121
8.97k
                break;
122
123
13.3k
            case '\\':
124
13.3k
                result += "\\\\";
125
13.3k
                break;
126
127
4.34M
            default:
128
4.34M
                if (is_iso_latin1_printable(ch)) {
129
3.02M
                    result += val.at(i);
130
3.02M
                } else {
131
1.32M
                    result += "\\" +
132
1.32M
                        QUtil::int_to_string_base(
133
1.32M
                                  static_cast<int>(static_cast<unsigned char>(ch)), 8, 3);
134
1.32M
                }
135
4.34M
                break;
136
29.4M
            }
137
29.4M
        }
138
72.8k
        result += ")";
139
72.8k
    }
140
141
133k
    return result;
142
133k
}