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