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"
18 #include <QThreadStorage>
22 #include <boost/cstdint.hpp>
35 #ifdef WORDS_BIGENDIAN
36 char const * utf16_codeset = "UTF16-BE";
38 char const * utf16_codeset = "UTF16-LE";
46 #ifdef WORDS_BIGENDIAN
47 char const * ucs4_codeset = "UCS-4BE";
49 char const * ucs4_codeset = "UCS-4LE";
52 static const iconv_t invalid_cd = (iconv_t)(-1);
55 struct IconvProcessor::Impl
57 Impl(string const & to, string const & from)
58 : cd(invalid_cd), tocode_(to), fromcode_(from)
63 if (cd != invalid_cd && iconv_close(cd) == -1)
64 LYXERR0("Error returned from iconv_close(" << errno << ')');
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 IconvProcessor & IconvProcessor::operator=(IconvProcessor const & other)
95 pimpl_ = new Impl(other.pimpl_->tocode_, other.pimpl_->fromcode_);
101 bool IconvProcessor::init()
103 if (pimpl_->cd != invalid_cd)
106 pimpl_->cd = iconv_open(pimpl_->tocode_.c_str(), pimpl_->fromcode_.c_str());
107 if (pimpl_->cd != invalid_cd)
110 lyxerr << "Error returned from iconv_open" << endl;
113 lyxerr << "EINVAL The conversion from " << pimpl_->fromcode_
114 << " to " << pimpl_->tocode_
115 << " is not supported by the implementation."
119 lyxerr << "\tSome other error: " << errno << endl;
126 int IconvProcessor::convert(char const * buf, size_t buflen,
127 char * outbuf, size_t maxoutsize)
132 if (pimpl_->cd == invalid_cd) {
137 char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(buf);
138 size_t inbytesleft = buflen;
139 size_t outbytesleft = maxoutsize;
141 int res = iconv(pimpl_->cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
143 // flush out remaining data. This is needed because iconv sometimes
144 // holds back chars in the stream, waiting for a combination character
145 // (see e.g. http://sources.redhat.com/bugzilla/show_bug.cgi?id=1124)
146 iconv(pimpl_->cd, NULL, NULL, &outbuf, &outbytesleft);
149 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
150 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
153 // Everything went well.
154 return maxoutsize - outbytesleft;
156 // There are some errors in the conversion
157 lyxerr << "Error returned from iconv" << endl;
160 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
163 lyxerr << "EILSEQ An invalid multibyte sequence"
164 << " has been encountered in the input.\n"
165 << "When converting from " << pimpl_->fromcode_
166 << " to " << pimpl_->tocode_ << ".\n";
167 lyxerr << "Input:" << hex;
168 for (size_t i = 0; i < buflen; ++i) {
169 // char may be signed, avoid output of
170 // something like 0xffffffc2
171 boost::uint32_t const b =
172 *reinterpret_cast<unsigned char const *>(buf + i);
173 lyxerr << " 0x" << (unsigned int)b;
175 lyxerr << dec << endl;
178 lyxerr << "EINVAL An incomplete multibyte sequence"
179 << " has been encountered in the input.\n"
180 << "When converting from " << pimpl_->fromcode_
181 << " to " << pimpl_->tocode_ << ".\n";
182 lyxerr << "Input:" << hex;
183 for (size_t i = 0; i < buflen; ++i) {
184 // char may be signed, avoid output of
185 // something like 0xffffffc2
186 boost::uint32_t const b =
187 *reinterpret_cast<unsigned char const *>(buf + i);
188 lyxerr << " 0x" << (unsigned int)b;
190 lyxerr << dec << endl;
193 lyxerr << "\tSome other error: " << errno << endl;
196 // We got an error so we close down the conversion engine
197 if (iconv_close(pimpl_->cd) == -1) {
198 lyxerr << "Error returned from iconv_close("
199 << errno << ")" << endl;
201 pimpl_->cd = invalid_cd;
206 std::string IconvProcessor::from() const
208 return pimpl_->fromcode_;
212 std::string IconvProcessor::to() const
214 return pimpl_->tocode_;
221 template<typename RetType, typename InType>
223 iconv_convert(IconvProcessor & processor, InType const * buf, size_t buflen)
226 return vector<RetType>();
228 char const * inbuf = reinterpret_cast<char const *>(buf);
229 size_t inbytesleft = buflen * sizeof(InType);
231 static QThreadStorage<std::vector<char> *> static_outbuf;
232 if (!static_outbuf.hasLocalData())
233 static_outbuf.setLocalData(new std::vector<char>(32768));
234 std::vector<char> & outbuf = *static_outbuf.localData();
235 // The number of UCS4 code points in buf is at most inbytesleft.
236 // The output encoding will use at most
237 // max_encoded_bytes(pimpl_->tocode_) per UCS4 code point.
238 size_t maxoutbufsize = max_encoded_bytes(processor.to()) * inbytesleft;
239 if (outbuf.size() < maxoutbufsize)
240 outbuf.resize(maxoutbufsize);
242 int bytes = processor.convert(inbuf, inbytesleft, &outbuf[0], outbuf.size());
245 // FIXME Maybe throw an exception and handle that in the caller?
246 return vector<RetType>();
248 RetType const * tmp = reinterpret_cast<RetType const *>(&outbuf[0]);
249 return vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
255 IconvProcessor & utf8ToUcs4()
257 static QThreadStorage<IconvProcessor *> processor;
258 if (!processor.hasLocalData())
259 processor.setLocalData(new IconvProcessor(ucs4_codeset, "UTF-8"));
260 return *processor.localData();
264 vector<char_type> utf8_to_ucs4(vector<char> const & utf8str)
267 return vector<char_type>();
269 return utf8_to_ucs4(&utf8str[0], utf8str.size());
274 utf8_to_ucs4(char const * utf8str, size_t ls)
276 return iconv_convert<char_type>(utf8ToUcs4(), utf8str, ls);
281 utf16_to_ucs4(unsigned short const * s, size_t ls)
283 static QThreadStorage<IconvProcessor *> processor;
284 if (!processor.hasLocalData())
285 processor.setLocalData(new IconvProcessor(ucs4_codeset, utf16_codeset));
286 return iconv_convert<char_type>(*processor.localData(), s, ls);
290 vector<unsigned short>
291 ucs4_to_utf16(char_type const * s, size_t ls)
293 static QThreadStorage<IconvProcessor *> processor;
294 if (!processor.hasLocalData())
295 processor.setLocalData(new IconvProcessor(utf16_codeset, ucs4_codeset));
296 return iconv_convert<unsigned short>(*processor.localData(), s, ls);
300 IconvProcessor & ucs4ToUtf8()
302 static QThreadStorage<IconvProcessor *> processor;
303 if (!processor.hasLocalData())
304 processor.setLocalData(new IconvProcessor("UTF-8", ucs4_codeset));
305 return *processor.localData();
310 ucs4_to_utf8(char_type c)
312 return iconv_convert<char>(ucs4ToUtf8(), &c, 1);
317 ucs4_to_utf8(vector<char_type> const & ucs4str)
320 return vector<char>();
322 return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
327 ucs4_to_utf8(char_type const * ucs4str, size_t ls)
329 return iconv_convert<char>(ucs4ToUtf8(), ucs4str, ls);
334 eightbit_to_ucs4(char const * s, size_t ls, string const & encoding)
336 static QThreadStorage<map<string, IconvProcessor> *> static_processors;
337 if (!static_processors.hasLocalData())
338 static_processors.setLocalData(new map<string, IconvProcessor>);
339 map<string, IconvProcessor> & processors = *static_processors.localData();
340 if (processors.find(encoding) == processors.end()) {
341 IconvProcessor processor(ucs4_codeset, encoding.c_str());
342 processors.insert(make_pair(encoding, processor));
344 return iconv_convert<char_type>(processors[encoding], s, ls);
350 map<string, IconvProcessor> & ucs4To8bitProcessors()
352 static QThreadStorage<map<string, IconvProcessor> *> processors;
353 if (!processors.hasLocalData())
354 processors.setLocalData(new map<string, IconvProcessor>);
355 return *processors.localData();
362 ucs4_to_eightbit(char_type const * ucs4str, size_t ls, string const & encoding)
364 map<string, IconvProcessor> & processors(ucs4To8bitProcessors());
365 if (processors.find(encoding) == processors.end()) {
366 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
367 processors.insert(make_pair(encoding, processor));
369 return iconv_convert<char>(processors[encoding], ucs4str, ls);
373 char ucs4_to_eightbit(char_type ucs4, string const & encoding)
375 map<string, IconvProcessor> & processors(ucs4To8bitProcessors());
376 map<string, IconvProcessor>::iterator it = processors.find(encoding);
377 if (it == processors.end()) {
378 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
379 it = processors.insert(make_pair(encoding, processor)).first;
383 int const bytes = it->second.convert((char *)(&ucs4), 4, &out, 1);
390 void ucs4_to_multibytes(char_type ucs4, vector<char> & out,
391 string const & encoding)
393 static QThreadStorage<map<string, IconvProcessor> *> static_processors;
394 if (!static_processors.hasLocalData())
395 static_processors.setLocalData(new map<string, IconvProcessor>);
396 map<string, IconvProcessor> & processors = *static_processors.localData();
397 map<string, IconvProcessor>::iterator it = processors.find(encoding);
398 if (it == processors.end()) {
399 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
400 it = processors.insert(make_pair(encoding, processor)).first;
404 int bytes = it->second.convert((char *)(&ucs4), 4, &out[0], 4);
411 int max_encoded_bytes(std::string const & encoding)
413 // FIXME: this information should be transferred to lib/encodings
414 // UTF8 uses at most 4 bytes to represent one UCS4 code point
415 // (see RFC 3629). RFC 2279 specifies 6 bytes, but that
416 // information is outdated, and RFC 2279 has been superseded by
418 // The CJK encodings use (different) multibyte representation as well.
419 // All other encodings encode one UCS4 code point in one byte
420 // (and can therefore only encode a subset of UCS4)
421 // Furthermore, all encodings that use shifting (like SJIS) do not work with
422 // iconv_codecvt_facet.
423 if (encoding == "UTF-8" ||
425 encoding == "EUC-TW")
427 else if (encoding == "EUC-JP")
429 else if (encoding == "ISO-2022-JP")
431 else if (encoding == "BIG5" ||
432 encoding == "EUC-KR" ||
433 encoding == "EUC-CN" ||
434 encoding == "SJIS" ||