1 // Copyright 2017 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fpdfapi/font/cpdf_tounicodemap.h"
8
9 #include <set>
10 #include <utility>
11
12 #include "core/fpdfapi/font/cpdf_cid2unicodemap.h"
13 #include "core/fpdfapi/font/cpdf_fontglobals.h"
14 #include "core/fpdfapi/parser/cpdf_simple_parser.h"
15 #include "core/fpdfapi/parser/cpdf_stream.h"
16 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
17 #include "core/fxcrt/fx_extension.h"
18 #include "core/fxcrt/fx_safe_types.h"
19 #include "third_party/base/containers/contains.h"
20 #include "third_party/base/numerics/safe_conversions.h"
21
22 namespace {
23
StringDataAdd(WideString str)24 WideString StringDataAdd(WideString str) {
25 WideString ret;
26 wchar_t value = 1;
27 for (size_t i = str.GetLength(); i > 0; --i) {
28 wchar_t ch = str[i - 1] + value;
29 if (ch < str[i - 1]) {
30 ret.InsertAtFront(0);
31 } else {
32 ret.InsertAtFront(ch);
33 value = 0;
34 }
35 }
36 if (value)
37 ret.InsertAtFront(value);
38 return ret;
39 }
40
41 } // namespace
42
CPDF_ToUnicodeMap(RetainPtr<const CPDF_Stream> pStream)43 CPDF_ToUnicodeMap::CPDF_ToUnicodeMap(RetainPtr<const CPDF_Stream> pStream) {
44 Load(std::move(pStream));
45 }
46
47 CPDF_ToUnicodeMap::~CPDF_ToUnicodeMap() = default;
48
Lookup(uint32_t charcode) const49 WideString CPDF_ToUnicodeMap::Lookup(uint32_t charcode) const {
50 auto it = m_Multimap.find(charcode);
51 if (it == m_Multimap.end()) {
52 if (!m_pBaseMap)
53 return WideString();
54 return WideString(
55 m_pBaseMap->UnicodeFromCID(static_cast<uint16_t>(charcode)));
56 }
57
58 uint32_t value = *it->second.begin();
59 wchar_t unicode = static_cast<wchar_t>(value & 0xffff);
60 if (unicode != 0xffff)
61 return WideString(unicode);
62
63 size_t index = value >> 16;
64 return index < m_MultiCharVec.size() ? m_MultiCharVec[index] : WideString();
65 }
66
ReverseLookup(wchar_t unicode) const67 uint32_t CPDF_ToUnicodeMap::ReverseLookup(wchar_t unicode) const {
68 for (const auto& pair : m_Multimap) {
69 if (pdfium::Contains(pair.second, static_cast<uint32_t>(unicode)))
70 return pair.first;
71 }
72 return 0;
73 }
74
GetUnicodeCountByCharcodeForTesting(uint32_t charcode) const75 size_t CPDF_ToUnicodeMap::GetUnicodeCountByCharcodeForTesting(
76 uint32_t charcode) const {
77 auto it = m_Multimap.find(charcode);
78 return it != m_Multimap.end() ? it->second.size() : 0u;
79 }
80
81 // static
StringToCode(ByteStringView input)82 absl::optional<uint32_t> CPDF_ToUnicodeMap::StringToCode(ByteStringView input) {
83 // Ignore whitespaces within `input`. See https://crbug.com/pdfium/2065.
84 std::set<char> seen_whitespace_chars;
85 for (char c : input) {
86 if (PDFCharIsWhitespace(c)) {
87 seen_whitespace_chars.insert(c);
88 }
89 }
90 ByteString str_without_whitespace_chars; // Must outlive `str`.
91 ByteStringView str;
92 if (seen_whitespace_chars.empty()) {
93 str = input;
94 } else {
95 str_without_whitespace_chars.Reserve(input.GetLength());
96 for (char c : input) {
97 if (!pdfium::Contains(seen_whitespace_chars, c)) {
98 str_without_whitespace_chars += c;
99 }
100 }
101 str = str_without_whitespace_chars.AsStringView();
102 }
103
104 size_t len = str.GetLength();
105 if (len <= 2 || str[0] != '<' || str[len - 1] != '>')
106 return absl::nullopt;
107
108 FX_SAFE_UINT32 code = 0;
109 for (char c : str.Substr(1, len - 2)) {
110 if (!FXSYS_IsHexDigit(c))
111 return absl::nullopt;
112
113 code = code * 16 + FXSYS_HexCharToInt(c);
114 if (!code.IsValid())
115 return absl::nullopt;
116 }
117 return absl::optional<uint32_t>(code.ValueOrDie());
118 }
119
120 // static
StringToWideString(ByteStringView str)121 WideString CPDF_ToUnicodeMap::StringToWideString(ByteStringView str) {
122 size_t len = str.GetLength();
123 if (len <= 2 || str[0] != '<' || str[len - 1] != '>')
124 return WideString();
125
126 WideString result;
127 int byte_pos = 0;
128 wchar_t ch = 0;
129 for (char c : str.Substr(1, len - 2)) {
130 if (!FXSYS_IsHexDigit(c))
131 break;
132
133 ch = ch * 16 + FXSYS_HexCharToInt(c);
134 byte_pos++;
135 if (byte_pos == 4) {
136 result += ch;
137 byte_pos = 0;
138 ch = 0;
139 }
140 }
141 return result;
142 }
143
Load(RetainPtr<const CPDF_Stream> pStream)144 void CPDF_ToUnicodeMap::Load(RetainPtr<const CPDF_Stream> pStream) {
145 CIDSet cid_set = CIDSET_UNKNOWN;
146 auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(std::move(pStream));
147 pAcc->LoadAllDataFiltered();
148 CPDF_SimpleParser parser(pAcc->GetSpan());
149 while (true) {
150 ByteStringView word = parser.GetWord();
151 if (word.IsEmpty())
152 break;
153
154 if (word == "beginbfchar")
155 HandleBeginBFChar(&parser);
156 else if (word == "beginbfrange")
157 HandleBeginBFRange(&parser);
158 else if (word == "/Adobe-Korea1-UCS2")
159 cid_set = CIDSET_KOREA1;
160 else if (word == "/Adobe-Japan1-UCS2")
161 cid_set = CIDSET_JAPAN1;
162 else if (word == "/Adobe-CNS1-UCS2")
163 cid_set = CIDSET_CNS1;
164 else if (word == "/Adobe-GB1-UCS2")
165 cid_set = CIDSET_GB1;
166 }
167 if (cid_set != CIDSET_UNKNOWN) {
168 m_pBaseMap = CPDF_FontGlobals::GetInstance()->GetCID2UnicodeMap(cid_set);
169 }
170 }
171
HandleBeginBFChar(CPDF_SimpleParser * pParser)172 void CPDF_ToUnicodeMap::HandleBeginBFChar(CPDF_SimpleParser* pParser) {
173 while (true) {
174 ByteStringView word = pParser->GetWord();
175 if (word.IsEmpty() || word == "endbfchar")
176 return;
177
178 absl::optional<uint32_t> code = StringToCode(word);
179 if (!code.has_value())
180 return;
181
182 SetCode(code.value(), StringToWideString(pParser->GetWord()));
183 }
184 }
185
HandleBeginBFRange(CPDF_SimpleParser * pParser)186 void CPDF_ToUnicodeMap::HandleBeginBFRange(CPDF_SimpleParser* pParser) {
187 while (true) {
188 ByteStringView lowcode_str = pParser->GetWord();
189 if (lowcode_str.IsEmpty() || lowcode_str == "endbfrange")
190 return;
191
192 absl::optional<uint32_t> lowcode_opt = StringToCode(lowcode_str);
193 if (!lowcode_opt.has_value())
194 return;
195
196 ByteStringView highcode_str = pParser->GetWord();
197 absl::optional<uint32_t> highcode_opt = StringToCode(highcode_str);
198 if (!highcode_opt.has_value())
199 return;
200
201 uint32_t lowcode = lowcode_opt.value();
202 uint32_t highcode = (lowcode & 0xffffff00) | (highcode_opt.value() & 0xff);
203
204 ByteStringView start = pParser->GetWord();
205 if (start == "[") {
206 for (FX_SAFE_UINT32 code = lowcode;
207 code.IsValid() && code.ValueOrDie() <= highcode; code++) {
208 SetCode(code.ValueOrDie(), StringToWideString(pParser->GetWord()));
209 }
210 pParser->GetWord();
211 continue;
212 }
213
214 WideString destcode = StringToWideString(start);
215 if (destcode.GetLength() == 1) {
216 absl::optional<uint32_t> value_or_error = StringToCode(start);
217 if (!value_or_error.has_value())
218 return;
219
220 uint32_t value = value_or_error.value();
221 for (FX_SAFE_UINT32 code = lowcode;
222 code.IsValid() && code.ValueOrDie() <= highcode; code++) {
223 InsertIntoMultimap(code.ValueOrDie(), value++);
224 }
225 } else {
226 for (FX_SAFE_UINT32 code = lowcode;
227 code.IsValid() && code.ValueOrDie() <= highcode; code++) {
228 uint32_t code_value = code.ValueOrDie();
229 WideString retcode =
230 code_value == lowcode ? destcode : StringDataAdd(destcode);
231 InsertIntoMultimap(code_value, GetMultiCharIndexIndicator());
232 m_MultiCharVec.push_back(retcode);
233 destcode = std::move(retcode);
234 }
235 }
236 }
237 }
238
GetMultiCharIndexIndicator() const239 uint32_t CPDF_ToUnicodeMap::GetMultiCharIndexIndicator() const {
240 FX_SAFE_UINT32 uni = m_MultiCharVec.size();
241 uni = uni * 0x10000 + 0xffff;
242 return uni.ValueOrDefault(0);
243 }
244
SetCode(uint32_t srccode,WideString destcode)245 void CPDF_ToUnicodeMap::SetCode(uint32_t srccode, WideString destcode) {
246 size_t len = destcode.GetLength();
247 if (len == 0)
248 return;
249
250 if (len == 1) {
251 InsertIntoMultimap(srccode, destcode[0]);
252 } else {
253 InsertIntoMultimap(srccode, GetMultiCharIndexIndicator());
254 m_MultiCharVec.push_back(destcode);
255 }
256 }
257
InsertIntoMultimap(uint32_t code,uint32_t destcode)258 void CPDF_ToUnicodeMap::InsertIntoMultimap(uint32_t code, uint32_t destcode) {
259 auto it = m_Multimap.find(code);
260 if (it == m_Multimap.end()) {
261 m_Multimap.emplace(code, std::set<uint32_t>{destcode});
262 return;
263 }
264
265 it->second.emplace(destcode);
266 }
267