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