/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 | 0 | { |
23 | 0 | return (ch >= 32 && ch <= 126) || static_cast<unsigned char>(ch) >= 160; |
24 | 0 | } |
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 | 0 | { |
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 | 0 | unsigned int non_ascii = 0; |
70 | 0 | for (auto const ch: val) { |
71 | 0 | if (ch > 126) { |
72 | 0 | ++non_ascii; |
73 | 0 | } else if (ch >= 32) { |
74 | 0 | continue; |
75 | 0 | } else if (ch < 0 || ch >= 24) { |
76 | 0 | ++non_ascii; |
77 | 0 | } else if (!(ch == '\n' || ch == '\r' || ch == '\t' || ch == '\b' || ch == '\f')) { |
78 | 0 | return true; |
79 | 0 | } |
80 | 0 | } |
81 | 0 | return 5 * non_ascii > val.length(); |
82 | 0 | } |
83 | | |
84 | | std::string |
85 | | QPDF_String::unparse(bool force_binary) |
86 | 0 | { |
87 | 0 | bool use_hexstring = force_binary || useHexString(); |
88 | 0 | std::string result; |
89 | 0 | if (use_hexstring) { |
90 | 0 | static auto constexpr hexchars = "0123456789abcdef"; |
91 | 0 | result.reserve(2 * val.length() + 2); |
92 | 0 | result += '<'; |
93 | 0 | for (const char c: val) { |
94 | 0 | result += hexchars[static_cast<unsigned char>(c) >> 4]; |
95 | 0 | result += hexchars[c & 0x0f]; |
96 | 0 | } |
97 | 0 | result += '>'; |
98 | 0 | } else { |
99 | 0 | result += "("; |
100 | 0 | for (unsigned int i = 0; i < val.length(); ++i) { |
101 | 0 | char ch = val.at(i); |
102 | 0 | switch (ch) { |
103 | 0 | case '\n': |
104 | 0 | result += "\\n"; |
105 | 0 | break; |
106 | | |
107 | 0 | case '\r': |
108 | 0 | result += "\\r"; |
109 | 0 | break; |
110 | | |
111 | 0 | case '\t': |
112 | 0 | result += "\\t"; |
113 | 0 | break; |
114 | | |
115 | 0 | case '\b': |
116 | 0 | result += "\\b"; |
117 | 0 | break; |
118 | | |
119 | 0 | case '\f': |
120 | 0 | result += "\\f"; |
121 | 0 | break; |
122 | | |
123 | 0 | case '(': |
124 | 0 | result += "\\("; |
125 | 0 | break; |
126 | | |
127 | 0 | case ')': |
128 | 0 | result += "\\)"; |
129 | 0 | break; |
130 | | |
131 | 0 | case '\\': |
132 | 0 | result += "\\\\"; |
133 | 0 | break; |
134 | | |
135 | 0 | default: |
136 | 0 | if (is_iso_latin1_printable(ch)) { |
137 | 0 | result += val.at(i); |
138 | 0 | } else { |
139 | 0 | result += "\\" + |
140 | 0 | QUtil::int_to_string_base( |
141 | 0 | static_cast<int>(static_cast<unsigned char>(ch)), 8, 3); |
142 | 0 | } |
143 | 0 | break; |
144 | 0 | } |
145 | 0 | } |
146 | 0 | result += ")"; |
147 | 0 | } |
148 | | |
149 | 0 | return result; |
150 | 0 | } |