Coverage Report

Created: 2026-10-17 07:02

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
95.9M
{
13
95.9M
    return (ch >= 32 && ch <= 126) || static_cast<unsigned char>(ch) >= 160;
14
95.9M
}
15
16
void
17
QPDF_String::writeJSON(int json_version, JSON::Writer& p)
18
108k
{
19
108k
    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
108k
    if (util::is_utf16(val)) {
35
5.49k
        p << "\"u:" << JSON::Writer::encode_string(QUtil::utf16_to_utf8(val)) << "\"";
36
5.49k
        return;
37
5.49k
    }
38
    // See if we can unambiguously represent as Unicode.
39
103k
    if (util::is_explicit_utf8(val)) {
40
4.33k
        p << "\"u:" << JSON::Writer::encode_string(val.substr(3)) << "\"";
41
4.33k
        return;
42
4.33k
    }
43
98.6k
    if (!useHexString()) {
44
58.0k
        auto candidate = QUtil::pdf_doc_to_utf8(val);
45
58.0k
        std::string test;
46
58.0k
        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
57.0k
            p << "\"u:" << JSON::Writer::encode_string(candidate) << "\"";
49
57.0k
            return;
50
57.0k
        }
51
58.0k
    }
52
41.6k
    p << "\"b:" << QUtil::hex_encode(val) << "\"";
53
41.6k
}
54
55
bool
56
QPDF_String::useHexString() const
57
1.07M
{
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
1.07M
    unsigned int non_ascii = 0;
62
231M
    for (auto const ch: val) {
63
231M
        if (ch > 126) {
64
42.6M
            ++non_ascii;
65
189M
        } else if (ch >= 32) {
66
118M
            continue;
67
118M
        } else if (ch < 0 || ch >= 24) {
68
15.1M
            ++non_ascii;
69
55.2M
        } else if (!(ch == '\n' || ch == '\r' || ch == '\t' || ch == '\b' || ch == '\f')) {
70
211k
            return true;
71
211k
        }
72
231M
    }
73
861k
    return 5 * non_ascii > val.length();
74
1.07M
}
75
76
std::string
77
QPDF_String::unparse(bool force_binary)
78
1.46M
{
79
1.46M
    bool use_hexstring = force_binary || useHexString();
80
1.46M
    std::string result;
81
1.46M
    if (use_hexstring) {
82
785k
        static auto constexpr hexchars = "0123456789abcdef";
83
785k
        result.reserve(2 * val.length() + 2);
84
785k
        result += '<';
85
508M
        for (const char c: val) {
86
508M
            result += hexchars[static_cast<unsigned char>(c) >> 4];
87
508M
            result += hexchars[c & 0x0f];
88
508M
        }
89
785k
        result += '>';
90
785k
    } else {
91
676k
        result += "(";
92
140M
        for (unsigned int i = 0; i < val.length(); ++i) {
93
139M
            char ch = val.at(i);
94
139M
            switch (ch) {
95
6.35M
            case '\n':
96
6.35M
                result += "\\n";
97
6.35M
                break;
98
99
210k
            case '\r':
100
210k
                result += "\\r";
101
210k
                break;
102
103
19.4M
            case '\t':
104
19.4M
                result += "\\t";
105
19.4M
                break;
106
107
5.89M
            case '\b':
108
5.89M
                result += "\\b";
109
5.89M
                break;
110
111
10.9M
            case '\f':
112
10.9M
                result += "\\f";
113
10.9M
                break;
114
115
307k
            case '(':
116
307k
                result += "\\(";
117
307k
                break;
118
119
306k
            case ')':
120
306k
                result += "\\)";
121
306k
                break;
122
123
109k
            case '\\':
124
109k
                result += "\\\\";
125
109k
                break;
126
127
95.9M
            default:
128
95.9M
                if (is_iso_latin1_printable(ch)) {
129
89.8M
                    result += val.at(i);
130
89.8M
                } else {
131
6.10M
                    result += "\\" +
132
6.10M
                        QUtil::int_to_string_base(
133
6.10M
                                  static_cast<int>(static_cast<unsigned char>(ch)), 8, 3);
134
6.10M
                }
135
95.9M
                break;
136
139M
            }
137
139M
        }
138
676k
        result += ")";
139
676k
    }
140
141
1.46M
    return result;
142
1.46M
}