3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
10 * A collection of unicode conversion functions, using iconv.
15 #include "support/unicode.h"
16 #include "support/debug.h"
17 #include "support/mutex.h"
21 #include <boost/cstdint.hpp>
33 #ifdef WORDS_BIGENDIAN
34 char const * utf16_codeset = "UTF16-BE";
36 char const * utf16_codeset = "UTF16-LE";
44 #ifdef WORDS_BIGENDIAN
45 char const * ucs4_codeset = "UCS-4BE";
47 char const * ucs4_codeset = "UCS-4LE";
50 static const iconv_t invalid_cd = (iconv_t)(-1);
53 struct IconvProcessor::Impl
55 Impl(string const & to, string const & from)
56 : cd(invalid_cd), tocode_(to), fromcode_(from)
61 if (cd != invalid_cd && iconv_close(cd) == -1)
62 LYXERR0("Error returned from iconv_close(" << errno << ")");
69 Mutex mutex_; // iconv() is not thread save, see #7240
73 IconvProcessor::IconvProcessor(char const * tocode, char const * fromcode)
74 : pimpl_(new IconvProcessor::Impl(tocode, fromcode))
79 IconvProcessor::IconvProcessor(IconvProcessor const & other)
80 : pimpl_(new IconvProcessor::Impl(other.pimpl_->tocode_, other.pimpl_->fromcode_))
85 IconvProcessor::~IconvProcessor()
91 void IconvProcessor::operator=(IconvProcessor const & other)
94 pimpl_ = new Impl(other.pimpl_->tocode_, other.pimpl_->fromcode_);
98 bool IconvProcessor::init()
100 if (pimpl_->cd != invalid_cd)
103 pimpl_->cd = iconv_open(pimpl_->tocode_.c_str(), pimpl_->fromcode_.c_str());
104 if (pimpl_->cd != invalid_cd)
107 lyxerr << "Error returned from iconv_open" << endl;
110 lyxerr << "EINVAL The conversion from " << pimpl_->fromcode_
111 << " to " << pimpl_->tocode_
112 << " is not supported by the implementation."
116 lyxerr << "\tSome other error: " << errno << endl;
123 int IconvProcessor::convert(char const * buf, size_t buflen,
124 char * outbuf, size_t maxoutsize)
126 Mutex::Locker lock(&pimpl_->mutex_);
131 if (pimpl_->cd == invalid_cd) {
136 char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(buf);
137 size_t inbytesleft = buflen;
138 size_t outbytesleft = maxoutsize;
140 int res = iconv(pimpl_->cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
142 // flush out remaining data. This is needed because iconv sometimes
143 // holds back chars in the stream, waiting for a combination character
144 // (see e.g. http://sources.redhat.com/bugzilla/show_bug.cgi?id=1124)
145 iconv(pimpl_->cd, NULL, NULL, &outbuf, &outbytesleft);
148 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
149 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
152 // Everything went well.
153 return maxoutsize - outbytesleft;
155 // There are some errors in the conversion
156 lyxerr << "Error returned from iconv" << endl;
159 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
162 lyxerr << "EILSEQ An invalid multibyte sequence"
163 << " has been encountered in the input.\n"
164 << "When converting from " << pimpl_->fromcode_
165 << " to " << pimpl_->tocode_ << ".\n";
166 lyxerr << "Input:" << hex;
167 for (size_t i = 0; i < buflen; ++i) {
168 // char may be signed, avoid output of
169 // something like 0xffffffc2
170 boost::uint32_t const b =
171 *reinterpret_cast<unsigned char const *>(buf + i);
172 lyxerr << " 0x" << (unsigned int)b;
174 lyxerr << dec << endl;
177 lyxerr << "EINVAL An incomplete multibyte sequence"
178 << " has been encountered in the input.\n"
179 << "When converting from " << pimpl_->fromcode_
180 << " to " << pimpl_->tocode_ << ".\n";
181 lyxerr << "Input:" << hex;
182 for (size_t i = 0; i < buflen; ++i) {
183 // char may be signed, avoid output of
184 // something like 0xffffffc2
185 boost::uint32_t const b =
186 *reinterpret_cast<unsigned char const *>(buf + i);
187 lyxerr << " 0x" << (unsigned int)b;
189 lyxerr << dec << endl;
192 lyxerr << "\tSome other error: " << errno << endl;
195 // We got an error so we close down the conversion engine
196 if (iconv_close(pimpl_->cd) == -1) {
197 lyxerr << "Error returned from iconv_close("
198 << errno << ")" << endl;
200 pimpl_->cd = invalid_cd;
205 std::string IconvProcessor::from() const
207 return pimpl_->fromcode_;
211 std::string IconvProcessor::to() const
213 return pimpl_->tocode_;
220 template<typename RetType, typename InType>
222 iconv_convert(IconvProcessor & processor, InType const * buf, size_t buflen)
225 return vector<RetType>();
227 char const * inbuf = reinterpret_cast<char const *>(buf);
228 size_t inbytesleft = buflen * sizeof(InType);
230 static std::vector<char> outbuf(32768);
231 // The number of UCS4 code points in buf is at most inbytesleft.
232 // The output encoding will use at most
233 // max_encoded_bytes(pimpl_->tocode_) per UCS4 code point.
234 size_t maxoutbufsize = max_encoded_bytes(processor.to()) * inbytesleft;
235 if (outbuf.size() < maxoutbufsize)
236 outbuf.resize(maxoutbufsize);
238 int bytes = processor.convert(inbuf, inbytesleft, &outbuf[0], outbuf.size());
241 // FIXME Maybe throw an exception and handle that in the caller?
242 return vector<RetType>();
244 RetType const * tmp = reinterpret_cast<RetType const *>(&outbuf[0]);
245 return vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
251 vector<char_type> utf8_to_ucs4(vector<char> const & utf8str)
254 return vector<char_type>();
256 return utf8_to_ucs4(&utf8str[0], utf8str.size());
261 utf8_to_ucs4(char const * utf8str, size_t ls)
263 static IconvProcessor processor(ucs4_codeset, "UTF-8");
264 return iconv_convert<char_type>(processor, utf8str, ls);
269 utf16_to_ucs4(unsigned short const * s, size_t ls)
271 static IconvProcessor processor(ucs4_codeset, utf16_codeset);
272 return iconv_convert<char_type>(processor, s, ls);
276 vector<unsigned short>
277 ucs4_to_utf16(char_type const * s, size_t ls)
279 static IconvProcessor processor(utf16_codeset, ucs4_codeset);
280 return iconv_convert<unsigned short>(processor, s, ls);
285 ucs4_to_utf8(char_type c)
287 static IconvProcessor processor("UTF-8", ucs4_codeset);
288 return iconv_convert<char>(processor, &c, 1);
293 ucs4_to_utf8(vector<char_type> const & ucs4str)
296 return vector<char>();
298 return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
303 ucs4_to_utf8(char_type const * ucs4str, size_t ls)
305 static IconvProcessor processor("UTF-8", ucs4_codeset);
306 return iconv_convert<char>(processor, ucs4str, ls);
311 eightbit_to_ucs4(char const * s, size_t ls, string const & encoding)
313 static map<string, IconvProcessor> processors;
314 if (processors.find(encoding) == processors.end()) {
315 IconvProcessor processor(ucs4_codeset, encoding.c_str());
316 processors.insert(make_pair(encoding, processor));
318 return iconv_convert<char_type>(processors[encoding], s, ls);
323 ucs4_to_eightbit(char_type const * ucs4str, size_t ls, string const & encoding)
325 static map<string, IconvProcessor> processors;
326 if (processors.find(encoding) == processors.end()) {
327 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
328 processors.insert(make_pair(encoding, processor));
330 return iconv_convert<char>(processors[encoding], ucs4str, ls);
334 char ucs4_to_eightbit(char_type ucs4, string const & encoding)
336 static map<string, IconvProcessor> processors;
337 map<string, IconvProcessor>::iterator it = processors.find(encoding);
338 if (it == processors.end()) {
339 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
340 it = processors.insert(make_pair(encoding, processor)).first;
344 int const bytes = it->second.convert((char *)(&ucs4), 4, &out, 1);
351 void ucs4_to_multibytes(char_type ucs4, vector<char> & out,
352 string const & encoding)
354 static map<string, IconvProcessor> processors;
355 map<string, IconvProcessor>::iterator it = processors.find(encoding);
356 if (it == processors.end()) {
357 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
358 it = processors.insert(make_pair(encoding, processor)).first;
362 int bytes = it->second.convert((char *)(&ucs4), 4, &out[0], 4);
369 int max_encoded_bytes(std::string const & encoding)
371 // FIXME: this information should be transferred to lib/encodings
372 // UTF8 uses at most 4 bytes to represent one UCS4 code point
373 // (see RFC 3629). RFC 2279 specifies 6 bytes, but that
374 // information is outdated, and RFC 2279 has been superseded by
376 // The CJK encodings use (different) multibyte representation as well.
377 // All other encodings encode one UCS4 code point in one byte
378 // (and can therefore only encode a subset of UCS4)
379 // Note that BIG5 and SJIS do not work with LaTeX (see lib/encodings).
380 // Furthermore, all encodings that use shifting (like SJIS) do not work with
381 // iconv_codecvt_facet.
382 if (encoding == "UTF-8" ||
384 encoding == "EUC-TW")
386 else if (encoding == "EUC-JP")
388 else if (encoding == "ISO-2022-JP")
390 else if (encoding == "BIG5" ||
391 encoding == "EUC-KR" ||
392 encoding == "EUC-CN" ||
393 encoding == "SJIS" ||