Coverage Report

Created: 2026-12-01 06:45

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
// PDF 2.0 allows UTF-8 strings when explicitly prefixed with the three-byte representation of
11
// U+FEFF. A string with that prefix whose remainder is not valid UTF-8 is treated like any other
12
// string.
13
static bool
14
is_valid_explicit_utf8(std::string const& val)
15
0
{
16
0
    return util::is_explicit_utf8(val) &&
17
0
        qpdf::Name::analyzeJSONEncoding(std::string_view(val).substr(3)).first;
18
0
}
19
20
static bool
21
is_iso_latin1_printable(char ch)
22
12.9M
{
23
12.9M
    return (ch >= 32 && ch <= 126) || static_cast<unsigned char>(ch) >= 160;
24
12.9M
}
25
26
void
27
QPDF_String::writeJSON(int json_version, JSON::Writer& p)
28
0
{
29
0
    if (json_version == 1) {
30
0
        if (util::is_utf16(val)) {
31
0
            p << "\"" << JSON::Writer::encode_string(QUtil::utf16_to_utf8(val)) << "\"";
32
0
            return;
33
0
        }
34
0
        if (is_valid_explicit_utf8(val)) {
35
0
            p << "\"" << JSON::Writer::encode_string(val.substr(3)) << "\"";
36
0
            return;
37
0
        }
38
0
        p << "\"" << JSON::Writer::encode_string(QUtil::pdf_doc_to_utf8(val)) << "\"";
39
0
        return;
40
0
    }
41
    // See if we can unambiguously represent as Unicode.
42
0
    if (util::is_utf16(val)) {
43
0
        p << "\"u:" << JSON::Writer::encode_string(QUtil::utf16_to_utf8(val)) << "\"";
44
0
        return;
45
0
    }
46
    // See if we can unambiguously represent as Unicode.
47
0
    if (is_valid_explicit_utf8(val)) {
48
0
        p << "\"u:" << JSON::Writer::encode_string(val.substr(3)) << "\"";
49
0
        return;
50
0
    }
51
0
    if (!useHexString()) {
52
0
        auto candidate = QUtil::pdf_doc_to_utf8(val);
53
0
        std::string test;
54
0
        if (QUtil::utf8_to_pdf_doc(candidate, test, '?') && test == val) {
55
            // This is a PDF-doc string that can be losslessly encoded as Unicode.
56
0
            p << "\"u:" << JSON::Writer::encode_string(candidate) << "\"";
57
0
            return;
58
0
        }
59
0
    }
60
0
    p << "\"b:" << QUtil::hex_encode(val) << "\"";
61
0
}
62
63
bool
64
QPDF_String::useHexString() const
65
77.0k
{
66
    // Heuristic: use the hexadecimal representation of a string if there are any non-printable (in
67
    // PDF Doc encoding) characters or if too large of a proportion of the string consists of
68
    // non-ASCII characters.
69
77.0k
    unsigned int non_ascii = 0;
70
20.1M
    for (auto const ch: val) {
71
20.1M
        if (ch > 126) {
72
1.43M
            ++non_ascii;
73
18.7M
        } else if (ch >= 32) {
74
16.8M
            continue;
75
16.8M
        } else if (ch < 0 || ch >= 24) {
76
273k
            ++non_ascii;
77
1.62M
        } else if (!(ch == '\n' || ch == '\r' || ch == '\t' || ch == '\b' || ch == '\f')) {
78
10.4k
            return true;
79
10.4k
        }
80
20.1M
    }
81
66.6k
    return 5 * non_ascii > val.length();
82
77.0k
}
83
84
std::string
85
QPDF_String::unparse(bool force_binary)
86
103k
{
87
103k
    bool use_hexstring = force_binary || useHexString();
88
103k
    std::string result;
89
103k
    if (use_hexstring) {
90
46.8k
        static auto constexpr hexchars = "0123456789abcdef";
91
46.8k
        result.reserve(2 * val.length() + 2);
92
46.8k
        result += '<';
93
89.2M
        for (const char c: val) {
94
89.2M
            result += hexchars[static_cast<unsigned char>(c) >> 4];
95
89.2M
            result += hexchars[c & 0x0f];
96
89.2M
        }
97
46.8k
        result += '>';
98
56.7k
    } else {
99
56.7k
        result += "(";
100
14.2M
        for (unsigned int i = 0; i < val.length(); ++i) {
101
14.2M
            char ch = val.at(i);
102
14.2M
            switch (ch) {
103
1.03M
            case '\n':
104
1.03M
                result += "\\n";
105
1.03M
                break;
106
107
11.7k
            case '\r':
108
11.7k
                result += "\\r";
109
11.7k
                break;
110
111
87.3k
            case '\t':
112
87.3k
                result += "\\t";
113
87.3k
                break;
114
115
36.7k
            case '\b':
116
36.7k
                result += "\\b";
117
36.7k
                break;
118
119
50.5k
            case '\f':
120
50.5k
                result += "\\f";
121
50.5k
                break;
122
123
32.8k
            case '(':
124
32.8k
                result += "\\(";
125
32.8k
                break;
126
127
32.9k
            case ')':
128
32.9k
                result += "\\)";
129
32.9k
                break;
130
131
19.9k
            case '\\':
132
19.9k
                result += "\\\\";
133
19.9k
                break;
134
135
12.9M
            default:
136
12.9M
                if (is_iso_latin1_printable(ch)) {
137
12.7M
                    result += val.at(i);
138
12.7M
                } else {
139
189k
                    result += "\\" +
140
189k
                        QUtil::int_to_string_base(
141
189k
                                  static_cast<int>(static_cast<unsigned char>(ch)), 8, 3);
142
189k
                }
143
12.9M
                break;
144
14.2M
            }
145
14.2M
        }
146
56.7k
        result += ")";
147
56.7k
    }
148
149
103k
    return result;
150
103k
}