Coverage Report

Created: 2026-01-10 07:00

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