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 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)
129 if (pimpl_->cd == invalid_cd) {
134 char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(buf);
135 size_t inbytesleft = buflen;
136 size_t outbytesleft = maxoutsize;
138 int res = iconv(pimpl_->cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
140 // flush out remaining data. This is needed because iconv sometimes
141 // holds back chars in the stream, waiting for a combination character
142 // (see e.g. http://sources.redhat.com/bugzilla/show_bug.cgi?id=1124)
143 iconv(pimpl_->cd, NULL, NULL, &outbuf, &outbytesleft);
146 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
147 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
150 // Everything went well.
151 return maxoutsize - outbytesleft;
153 // There are some errors in the conversion
154 lyxerr << "Error returned from iconv" << endl;
157 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
160 lyxerr << "EILSEQ An invalid multibyte sequence"
161 << " has been encountered in the input.\n"
162 << "When converting from " << pimpl_->fromcode_
163 << " to " << pimpl_->tocode_ << ".\n";
164 lyxerr << "Input:" << hex;
165 for (size_t i = 0; i < buflen; ++i) {
166 // char may be signed, avoid output of
167 // something like 0xffffffc2
168 boost::uint32_t const b =
169 *reinterpret_cast<unsigned char const *>(buf + i);
170 lyxerr << " 0x" << (unsigned int)b;
172 lyxerr << dec << endl;
175 lyxerr << "EINVAL An incomplete multibyte sequence"
176 << " has been encountered in the input.\n"
177 << "When converting from " << pimpl_->fromcode_
178 << " to " << pimpl_->tocode_ << ".\n";
179 lyxerr << "Input:" << hex;
180 for (size_t i = 0; i < buflen; ++i) {
181 // char may be signed, avoid output of
182 // something like 0xffffffc2
183 boost::uint32_t const b =
184 *reinterpret_cast<unsigned char const *>(buf + i);
185 lyxerr << " 0x" << (unsigned int)b;
187 lyxerr << dec << endl;
190 lyxerr << "\tSome other error: " << errno << endl;
193 // We got an error so we close down the conversion engine
194 if (iconv_close(pimpl_->cd) == -1) {
195 lyxerr << "Error returned from iconv_close("
196 << errno << ")" << endl;
198 pimpl_->cd = invalid_cd;
203 std::string IconvProcessor::from() const
205 return pimpl_->fromcode_;
209 std::string IconvProcessor::to() const
211 return pimpl_->tocode_;
218 template<typename RetType, typename InType>
220 iconv_convert(IconvProcessor & processor, InType const * buf, size_t buflen)
223 return vector<RetType>();
225 char const * inbuf = reinterpret_cast<char const *>(buf);
226 size_t inbytesleft = buflen * sizeof(InType);
228 static QThreadStorage<std::vector<char> *> static_outbuf;
229 if (!static_outbuf.hasLocalData())
230 static_outbuf.setLocalData(new std::vector<char>(32768));
231 std::vector<char> & outbuf = *static_outbuf.localData();
232 // The number of UCS4 code points in buf is at most inbytesleft.
233 // The output encoding will use at most
234 // max_encoded_bytes(pimpl_->tocode_) per UCS4 code point.
235 size_t maxoutbufsize = max_encoded_bytes(processor.to()) * inbytesleft;
236 if (outbuf.size() < maxoutbufsize)
237 outbuf.resize(maxoutbufsize);
239 int bytes = processor.convert(inbuf, inbytesleft, &outbuf[0], outbuf.size());
242 // FIXME Maybe throw an exception and handle that in the caller?
243 return vector<RetType>();
245 RetType const * tmp = reinterpret_cast<RetType const *>(&outbuf[0]);
246 return vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
252 IconvProcessor & utf8ToUcs4()
254 static QThreadStorage<IconvProcessor *> processor;
255 if (!processor.hasLocalData())
256 processor.setLocalData(new IconvProcessor(ucs4_codeset, "UTF-8"));
257 return *processor.localData();
261 vector<char_type> utf8_to_ucs4(vector<char> const & utf8str)
264 return vector<char_type>();
266 return utf8_to_ucs4(&utf8str[0], utf8str.size());
271 utf8_to_ucs4(char const * utf8str, size_t ls)
273 return iconv_convert<char_type>(utf8ToUcs4(), utf8str, ls);
278 utf16_to_ucs4(unsigned short const * s, size_t ls)
280 static QThreadStorage<IconvProcessor *> processor;
281 if (!processor.hasLocalData())
282 processor.setLocalData(new IconvProcessor(ucs4_codeset, utf16_codeset));
283 return iconv_convert<char_type>(*processor.localData(), s, ls);
287 vector<unsigned short>
288 ucs4_to_utf16(char_type const * s, size_t ls)
290 static QThreadStorage<IconvProcessor *> processor;
291 if (!processor.hasLocalData())
292 processor.setLocalData(new IconvProcessor(utf16_codeset, ucs4_codeset));
293 return iconv_convert<unsigned short>(*processor.localData(), s, ls);
297 IconvProcessor & ucs4ToUtf8()
299 static QThreadStorage<IconvProcessor *> processor;
300 if (!processor.hasLocalData())
301 processor.setLocalData(new IconvProcessor("UTF-8", ucs4_codeset));
302 return *processor.localData();
307 ucs4_to_utf8(char_type c)
309 return iconv_convert<char>(ucs4ToUtf8(), &c, 1);
314 ucs4_to_utf8(vector<char_type> const & ucs4str)
317 return vector<char>();
319 return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
324 ucs4_to_utf8(char_type const * ucs4str, size_t ls)
326 return iconv_convert<char>(ucs4ToUtf8(), ucs4str, ls);
331 eightbit_to_ucs4(char const * s, size_t ls, string const & encoding)
333 static QThreadStorage<map<string, IconvProcessor> *> static_processors;
334 if (!static_processors.hasLocalData())
335 static_processors.setLocalData(new map<string, IconvProcessor>);
336 map<string, IconvProcessor> & processors = *static_processors.localData();
337 if (processors.find(encoding) == processors.end()) {
338 IconvProcessor processor(ucs4_codeset, encoding.c_str());
339 processors.insert(make_pair(encoding, processor));
341 return iconv_convert<char_type>(processors[encoding], s, ls);
347 map<string, IconvProcessor> & ucs4To8bitProcessors()
349 static QThreadStorage<map<string, IconvProcessor> *> processors;
350 if (!processors.hasLocalData())
351 processors.setLocalData(new map<string, IconvProcessor>);
352 return *processors.localData();
359 ucs4_to_eightbit(char_type const * ucs4str, size_t ls, string const & encoding)
361 map<string, IconvProcessor> & processors(ucs4To8bitProcessors());
362 if (processors.find(encoding) == processors.end()) {
363 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
364 processors.insert(make_pair(encoding, processor));
366 return iconv_convert<char>(processors[encoding], ucs4str, ls);
370 char ucs4_to_eightbit(char_type ucs4, string const & encoding)
372 map<string, IconvProcessor> & processors(ucs4To8bitProcessors());
373 map<string, IconvProcessor>::iterator it = processors.find(encoding);
374 if (it == processors.end()) {
375 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
376 it = processors.insert(make_pair(encoding, processor)).first;
380 int const bytes = it->second.convert((char *)(&ucs4), 4, &out, 1);
387 void ucs4_to_multibytes(char_type ucs4, vector<char> & out,
388 string const & encoding)
390 static QThreadStorage<map<string, IconvProcessor> *> static_processors;
391 if (!static_processors.hasLocalData())
392 static_processors.setLocalData(new map<string, IconvProcessor>);
393 map<string, IconvProcessor> & processors = *static_processors.localData();
394 map<string, IconvProcessor>::iterator it = processors.find(encoding);
395 if (it == processors.end()) {
396 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
397 it = processors.insert(make_pair(encoding, processor)).first;
401 int bytes = it->second.convert((char *)(&ucs4), 4, &out[0], 4);
408 int max_encoded_bytes(std::string const & encoding)
410 // FIXME: this information should be transferred to lib/encodings
411 // UTF8 uses at most 4 bytes to represent one UCS4 code point
412 // (see RFC 3629). RFC 2279 specifies 6 bytes, but that
413 // information is outdated, and RFC 2279 has been superseded by
415 // The CJK encodings use (different) multibyte representation as well.
416 // All other encodings encode one UCS4 code point in one byte
417 // (and can therefore only encode a subset of UCS4)
418 // Furthermore, all encodings that use shifting (like SJIS) do not work with
419 // iconv_codecvt_facet.
420 if (encoding == "UTF-8" ||
422 encoding == "EUC-TW")
424 else if (encoding == "EUC-JP")
426 else if (encoding == "ISO-2022-JP")
428 else if (encoding == "BIG5" ||
429 encoding == "EUC-KR" ||
430 encoding == "EUC-CN" ||
431 encoding == "SJIS" ||