1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/strconv.cpp
3 // Purpose: Unicode conversion classes
4 // Author: Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik,
5 // Ryan Norton, Fredrik Roubert (UTF7)
9 // Copyright: (c) 1999 Ove Kaaven, Robert Roebling, Vaclav Slavik
10 // (c) 2000-2003 Vadim Zeitlin
11 // (c) 2004 Ryan Norton, Fredrik Roubert
12 // Licence: wxWindows licence
13 /////////////////////////////////////////////////////////////////////////////
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
26 #include "wx/hashmap.h"
29 #include "wx/strconv.h"
39 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
40 #include "wx/msw/private.h"
41 #include "wx/msw/missing.h"
42 #define wxHAVE_WIN32_MB2WC
47 #include "wx/thread.h"
50 #include "wx/encconv.h"
51 #include "wx/fontmap.h"
54 #include "wx/osx/core/private/strconv_cf.h"
55 #endif //def __DARWIN__
58 #define TRACE_STRCONV wxT("strconv")
60 // WC_UTF16 is defined only if sizeof(wchar_t) == 2, otherwise it's supposed to
62 #if SIZEOF_WCHAR_T == 2
67 // ============================================================================
69 // ============================================================================
71 // helper function of cMB2WC(): check if n bytes at this location are all NUL
72 static bool NotAllNULs(const char *p
, size_t n
)
74 while ( n
&& *p
++ == '\0' )
80 // ----------------------------------------------------------------------------
81 // UTF-16 en/decoding to/from UCS-4 with surrogates handling
82 // ----------------------------------------------------------------------------
84 static size_t encode_utf16(wxUint32 input
, wxUint16
*output
)
89 *output
= (wxUint16
) input
;
93 else if (input
>= 0x110000)
101 *output
++ = (wxUint16
) ((input
>> 10) + 0xd7c0);
102 *output
= (wxUint16
) ((input
& 0x3ff) + 0xdc00);
109 static size_t decode_utf16(const wxUint16
* input
, wxUint32
& output
)
111 if ((*input
< 0xd800) || (*input
> 0xdfff))
116 else if ((input
[1] < 0xdc00) || (input
[1] > 0xdfff))
119 return wxCONV_FAILED
;
123 output
= ((input
[0] - 0xd7c0) << 10) + (input
[1] - 0xdc00);
129 typedef wchar_t wxDecodeSurrogate_t
;
131 typedef wxUint16 wxDecodeSurrogate_t
;
132 #endif // WC_UTF16/!WC_UTF16
134 // returns the next UTF-32 character from the wchar_t buffer and advances the
135 // pointer to the character after this one
137 // if an invalid character is found, *pSrc is set to NULL, the caller must
139 static wxUint32
wxDecodeSurrogate(const wxDecodeSurrogate_t
**pSrc
)
143 n
= decode_utf16(reinterpret_cast<const wxUint16
*>(*pSrc
), out
);
144 if ( n
== wxCONV_FAILED
)
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
157 wxMBConv::ToWChar(wchar_t *dst
, size_t dstLen
,
158 const char *src
, size_t srcLen
) const
160 // although new conversion classes are supposed to implement this function
161 // directly, the existing ones only implement the old MB2WC() and so, to
162 // avoid to have to rewrite all conversion classes at once, we provide a
163 // default (but not efficient) implementation of this one in terms of the
164 // old function by copying the input to ensure that it's NUL-terminated and
165 // then using MB2WC() to convert it
167 // moreover, some conversion classes simply can't implement ToWChar()
168 // directly, the primary example is wxConvLibc: mbstowcs() only handles
169 // NUL-terminated strings
171 // the number of chars [which would be] written to dst [if it were not NULL]
172 size_t dstWritten
= 0;
174 // the number of NULs terminating this string
175 size_t nulLen
= 0; // not really needed, but just to avoid warnings
177 // if we were not given the input size we just have to assume that the
178 // string is properly terminated as we have no way of knowing how long it
179 // is anyhow, but if we do have the size check whether there are enough
183 if ( srcLen
!= wxNO_LEN
)
185 // we need to know how to find the end of this string
186 nulLen
= GetMBNulLen();
187 if ( nulLen
== wxCONV_FAILED
)
188 return wxCONV_FAILED
;
190 // if there are enough NULs we can avoid the copy
191 if ( srcLen
< nulLen
|| NotAllNULs(src
+ srcLen
- nulLen
, nulLen
) )
193 // make a copy in order to properly NUL-terminate the string
194 bufTmp
= wxCharBuffer(srcLen
+ nulLen
- 1 /* 1 will be added */);
195 char * const p
= bufTmp
.data();
196 memcpy(p
, src
, srcLen
);
197 for ( char *s
= p
+ srcLen
; s
< p
+ srcLen
+ nulLen
; s
++ )
203 srcEnd
= src
+ srcLen
;
205 else // quit after the first loop iteration
210 // the idea of this code is straightforward: it converts a NUL-terminated
211 // chunk of the string during each iteration and updates the output buffer
214 // all the complication come from the fact that this function, for
215 // historical reasons, must behave in 2 subtly different ways when it's
216 // called with a fixed number of characters and when it's called for the
217 // entire NUL-terminated string: in the former case (srcEnd != NULL) we
218 // must count all characters we convert, NUL or not; but in the latter we
219 // do not count the trailing NUL -- but still count all the NULs inside the
222 // so for the (simple) former case we just always count the trailing NUL,
223 // but for the latter we need to wait until we see if there is going to be
224 // another loop iteration and only count it then
227 // try to convert the current chunk
228 size_t lenChunk
= MB2WC(NULL
, src
, 0);
229 if ( lenChunk
== wxCONV_FAILED
)
230 return wxCONV_FAILED
;
232 dstWritten
+= lenChunk
;
238 // nothing left in the input string, conversion succeeded
244 if ( dstWritten
> dstLen
)
245 return wxCONV_FAILED
;
247 // +1 is for trailing NUL
248 if ( MB2WC(dst
, src
, lenChunk
+ 1) == wxCONV_FAILED
)
249 return wxCONV_FAILED
;
258 // we convert just one chunk in this case as this is the entire
259 // string anyhow (and we don't count the trailing NUL in this case)
263 // advance the input pointer past the end of this chunk: notice that we
264 // will always stop before srcEnd because we know that the chunk is
265 // always properly NUL-terminated
266 while ( NotAllNULs(src
, nulLen
) )
268 // notice that we must skip over multiple bytes here as we suppose
269 // that if NUL takes 2 or 4 bytes, then all the other characters do
270 // too and so if advanced by a single byte we might erroneously
271 // detect sequences of NUL bytes in the middle of the input
275 // if the buffer ends before this NUL, we shouldn't count it in our
276 // output so skip the code below
280 // do count this terminator as it's inside the buffer we convert
285 src
+= nulLen
; // skip the terminator itself
295 wxMBConv::FromWChar(char *dst
, size_t dstLen
,
296 const wchar_t *src
, size_t srcLen
) const
298 // the number of chars [which would be] written to dst [if it were not NULL]
299 size_t dstWritten
= 0;
301 // if we don't know its length we have no choice but to assume that it is
302 // NUL-terminated (notice that it can still be NUL-terminated even if
303 // explicit length is given but it doesn't change our return value)
304 const bool isNulTerminated
= srcLen
== wxNO_LEN
;
306 // make a copy of the input string unless it is already properly
308 wxWCharBuffer bufTmp
;
309 if ( isNulTerminated
)
311 srcLen
= wxWcslen(src
) + 1;
313 else if ( srcLen
!= 0 && src
[srcLen
- 1] != L
'\0' )
315 // make a copy in order to properly NUL-terminate the string
316 bufTmp
= wxWCharBuffer(srcLen
);
317 memcpy(bufTmp
.data(), src
, srcLen
* sizeof(wchar_t));
321 const size_t lenNul
= GetMBNulLen();
322 for ( const wchar_t * const srcEnd
= src
+ srcLen
;
324 src
++ /* skip L'\0' too */ )
326 // try to convert the current chunk
327 size_t lenChunk
= WC2MB(NULL
, src
, 0);
328 if ( lenChunk
== wxCONV_FAILED
)
329 return wxCONV_FAILED
;
331 dstWritten
+= lenChunk
;
333 const wchar_t * const
334 chunkEnd
= isNulTerminated
? srcEnd
- 1 : src
+ wxWcslen(src
);
336 // our return value accounts for the trailing NUL(s), unlike that of
337 // WC2MB(), however don't do it for the last NUL we artificially added
339 if ( chunkEnd
< srcEnd
)
340 dstWritten
+= lenNul
;
344 if ( dstWritten
> dstLen
)
345 return wxCONV_FAILED
;
347 // if we know that there is enough space in the destination buffer
348 // (because we accounted for lenNul in dstWritten above), we can
349 // convert directly in place -- but otherwise we need another
350 // temporary buffer to ensure that we don't overwrite the output
353 if ( chunkEnd
== srcEnd
)
355 dstBuf
= wxCharBuffer(lenChunk
+ lenNul
- 1);
356 dstTmp
= dstBuf
.data();
363 if ( WC2MB(dstTmp
, src
, lenChunk
+ lenNul
) == wxCONV_FAILED
)
364 return wxCONV_FAILED
;
368 // copy everything up to but excluding the terminating NUL(s)
369 // into the real output buffer
370 memcpy(dst
, dstTmp
, lenChunk
);
372 // micro-optimization: if dstTmp != dst it means that chunkEnd
373 // == srcEnd and so we're done, no need to update anything below
378 if ( chunkEnd
< srcEnd
)
388 size_t wxMBConv::MB2WC(wchar_t *outBuff
, const char *inBuff
, size_t outLen
) const
390 size_t rc
= ToWChar(outBuff
, outLen
, inBuff
);
391 if ( rc
!= wxCONV_FAILED
)
393 // ToWChar() returns the buffer length, i.e. including the trailing
394 // NUL, while this method doesn't take it into account
401 size_t wxMBConv::WC2MB(char *outBuff
, const wchar_t *inBuff
, size_t outLen
) const
403 size_t rc
= FromWChar(outBuff
, outLen
, inBuff
);
404 if ( rc
!= wxCONV_FAILED
)
412 wxMBConv::~wxMBConv()
414 // nothing to do here (necessary for Darwin linking probably)
417 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
421 // calculate the length of the buffer needed first
422 const size_t nLen
= ToWChar(NULL
, 0, psz
);
423 if ( nLen
!= wxCONV_FAILED
)
425 // now do the actual conversion
426 wxWCharBuffer
buf(nLen
- 1 /* +1 added implicitly */);
428 // +1 for the trailing NULL
429 if ( ToWChar(buf
.data(), nLen
, psz
) != wxCONV_FAILED
)
434 return wxWCharBuffer();
437 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *pwz
) const
441 const size_t nLen
= FromWChar(NULL
, 0, pwz
);
442 if ( nLen
!= wxCONV_FAILED
)
444 wxCharBuffer
buf(nLen
- 1);
445 if ( FromWChar(buf
.data(), nLen
, pwz
) != wxCONV_FAILED
)
450 return wxCharBuffer();
454 wxMBConv::cMB2WC(const char *inBuff
, size_t inLen
, size_t *outLen
) const
456 const size_t dstLen
= ToWChar(NULL
, 0, inBuff
, inLen
);
457 if ( dstLen
!= wxCONV_FAILED
)
459 // notice that we allocate space for dstLen+1 wide characters here
460 // because we want the buffer to always be NUL-terminated, even if the
461 // input isn't (as otherwise the caller has no way to know its length)
462 wxWCharBuffer
wbuf(dstLen
);
463 wbuf
.data()[dstLen
] = L
'\0';
464 if ( ToWChar(wbuf
.data(), dstLen
, inBuff
, inLen
) != wxCONV_FAILED
)
470 // we also need to handle NUL-terminated input strings
471 // specially: for them the output is the length of the string
472 // excluding the trailing NUL, however if we're asked to
473 // convert a specific number of characters we return the length
474 // of the resulting output even if it's NUL-terminated
475 if ( inLen
== wxNO_LEN
)
486 return wxWCharBuffer();
490 wxMBConv::cWC2MB(const wchar_t *inBuff
, size_t inLen
, size_t *outLen
) const
492 size_t dstLen
= FromWChar(NULL
, 0, inBuff
, inLen
);
493 if ( dstLen
!= wxCONV_FAILED
)
495 const size_t nulLen
= GetMBNulLen();
497 // as above, ensure that the buffer is always NUL-terminated, even if
499 wxCharBuffer
buf(dstLen
+ nulLen
- 1);
500 memset(buf
.data() + dstLen
, 0, nulLen
);
501 if ( FromWChar(buf
.data(), dstLen
, inBuff
, inLen
) != wxCONV_FAILED
)
507 if ( inLen
== wxNO_LEN
)
509 // in this case both input and output are NUL-terminated
510 // and we're not supposed to count NUL
522 return wxCharBuffer();
525 const wxWCharBuffer
wxMBConv::cMB2WC(const wxScopedCharBuffer
& buf
) const
527 const size_t srcLen
= buf
.length();
530 const size_t dstLen
= ToWChar(NULL
, 0, buf
, srcLen
);
531 if ( dstLen
!= wxCONV_FAILED
)
533 wxWCharBuffer
wbuf(dstLen
);
534 wbuf
.data()[dstLen
] = L
'\0';
535 if ( ToWChar(wbuf
.data(), dstLen
, buf
, srcLen
) != wxCONV_FAILED
)
540 return wxScopedWCharBuffer::CreateNonOwned(L
"", 0);
543 const wxCharBuffer
wxMBConv::cWC2MB(const wxScopedWCharBuffer
& wbuf
) const
545 const size_t srcLen
= wbuf
.length();
548 const size_t dstLen
= FromWChar(NULL
, 0, wbuf
, srcLen
);
549 if ( dstLen
!= wxCONV_FAILED
)
551 wxCharBuffer
buf(dstLen
);
552 buf
.data()[dstLen
] = '\0';
553 if ( FromWChar(buf
.data(), dstLen
, wbuf
, srcLen
) != wxCONV_FAILED
)
558 return wxScopedCharBuffer::CreateNonOwned("", 0);
561 // ----------------------------------------------------------------------------
563 // ----------------------------------------------------------------------------
565 size_t wxMBConvLibc::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
567 return wxMB2WC(buf
, psz
, n
);
570 size_t wxMBConvLibc::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
572 return wxWC2MB(buf
, psz
, n
);
575 // ----------------------------------------------------------------------------
576 // wxConvBrokenFileNames
577 // ----------------------------------------------------------------------------
581 wxConvBrokenFileNames::wxConvBrokenFileNames(const wxString
& charset
)
583 if ( wxStricmp(charset
, wxT("UTF-8")) == 0 ||
584 wxStricmp(charset
, wxT("UTF8")) == 0 )
585 m_conv
= new wxMBConvUTF8(wxMBConvUTF8::MAP_INVALID_UTF8_TO_PUA
);
587 m_conv
= new wxCSConv(charset
);
592 // ----------------------------------------------------------------------------
594 // ----------------------------------------------------------------------------
596 // Implementation (C) 2004 Fredrik Roubert
598 // Changes to work in streaming mode (C) 2008 Vadim Zeitlin
601 // BASE64 decoding table
603 static const unsigned char utf7unb64
[] =
605 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
606 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
607 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
608 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
609 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
610 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
611 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
612 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
613 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
614 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
615 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
616 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
617 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
618 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
619 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
620 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
621 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
622 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
623 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
624 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
625 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
626 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
627 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
628 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
629 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
630 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
631 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
632 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
633 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
634 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
635 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
636 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
639 size_t wxMBConvUTF7::ToWChar(wchar_t *dst
, size_t dstLen
,
640 const char *src
, size_t srcLen
) const
642 DecoderState stateOrig
,
644 if ( srcLen
== wxNO_LEN
)
646 // convert the entire string, up to and including the trailing NUL
647 srcLen
= strlen(src
) + 1;
649 // when working on the entire strings we don't update nor use the shift
650 // state from the previous call
651 statePtr
= &stateOrig
;
653 else // when working with partial strings we do use the shift state
655 statePtr
= const_cast<DecoderState
*>(&m_stateDecoder
);
657 // also save the old state to be able to rollback to it on error
658 stateOrig
= m_stateDecoder
;
661 // but to simplify the code below we use this variable in both cases
662 DecoderState
& state
= *statePtr
;
665 // number of characters [which would have been] written to dst [if it were
669 const char * const srcEnd
= src
+ srcLen
;
671 while ( (src
< srcEnd
) && (!dst
|| (len
< dstLen
)) )
673 const unsigned char cc
= *src
++;
675 if ( state
.IsShifted() )
677 const unsigned char dc
= utf7unb64
[cc
];
680 // end of encoded part, check that nothing was left: there can
681 // be up to 4 bits of 0 padding but nothing else (we also need
682 // to check isLSB as we count bits modulo 8 while a valid UTF-7
683 // encoded sequence must contain an integral number of UTF-16
685 if ( state
.isLSB
|| state
.bit
> 4 ||
686 (state
.accum
& ((1 << state
.bit
) - 1)) )
691 return wxCONV_FAILED
;
696 // re-parse this character normally below unless it's '-' which
697 // is consumed by the decoder
701 else // valid encoded character
703 // mini base64 decoder: each character is 6 bits
708 if ( state
.bit
>= 8 )
710 // got the full byte, consume it
712 unsigned char b
= (state
.accum
>> state
.bit
) & 0x00ff;
716 // we've got the full word, output it
718 *dst
++ = (state
.msb
<< 8) | b
;
724 // just store it while we wait for LSB
732 if ( state
.IsDirect() )
734 // start of an encoded segment?
739 // just the encoded plus sign, don't switch to shifted mode
745 else if ( utf7unb64
[(unsigned)*src
] == 0xff )
747 // empty encoded chunks are not allowed
751 return wxCONV_FAILED
;
753 else // base-64 encoded chunk follows
760 // only printable 7 bit ASCII characters (with the exception of
761 // NUL, TAB, CR and LF) can be used directly
762 if ( cc
>= 0x7f || (cc
< ' ' &&
763 !(cc
== '\0' || cc
== '\t' || cc
== '\r' || cc
== '\n')) )
764 return wxCONV_FAILED
;
775 // as we didn't read any characters we should be called with the same
776 // data (followed by some more new data) again later so don't save our
780 return wxCONV_FAILED
;
787 // BASE64 encoding table
789 static const unsigned char utf7enb64
[] =
791 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
792 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
793 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
794 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
795 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
796 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
797 'w', 'x', 'y', 'z', '0', '1', '2', '3',
798 '4', '5', '6', '7', '8', '9', '+', '/'
802 // UTF-7 encoding table
804 // 0 - Set D (directly encoded characters)
805 // 1 - Set O (optional direct characters)
806 // 2 - whitespace characters (optional)
807 // 3 - special characters
809 static const unsigned char utf7encode
[128] =
811 0, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3,
812 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
813 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 3,
814 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
815 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
816 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1,
817 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
818 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3
821 static inline bool wxIsUTF7Direct(wchar_t wc
)
823 return wc
< 0x80 && utf7encode
[wc
] < 1;
826 size_t wxMBConvUTF7::FromWChar(char *dst
, size_t dstLen
,
827 const wchar_t *src
, size_t srcLen
) const
829 EncoderState stateOrig
,
831 if ( srcLen
== wxNO_LEN
)
833 // we don't apply the stored state when operating on entire strings at
835 statePtr
= &stateOrig
;
837 srcLen
= wxWcslen(src
) + 1;
839 else // do use the mode we left the output in previously
841 stateOrig
= m_stateEncoder
;
842 statePtr
= const_cast<EncoderState
*>(&m_stateEncoder
);
845 EncoderState
& state
= *statePtr
;
850 const wchar_t * const srcEnd
= src
+ srcLen
;
851 while ( src
< srcEnd
&& (!dst
|| len
< dstLen
) )
854 if ( wxIsUTF7Direct(cc
) )
856 if ( state
.IsShifted() )
858 // pad with zeros the last encoded block if necessary
862 *dst
++ = utf7enb64
[((state
.accum
% 16) << (6 - state
.bit
)) % 64];
877 else if ( cc
== '+' && state
.IsDirect() )
888 else if (((wxUint32
)cc
) > 0xffff)
890 // no surrogate pair generation (yet?)
891 return wxCONV_FAILED
;
896 if ( state
.IsDirect() )
905 // BASE64 encode string
908 for ( unsigned lsb
= 0; lsb
< 2; lsb
++ )
911 state
.accum
+= lsb
? cc
& 0xff : (cc
& 0xff00) >> 8;
913 for (state
.bit
+= 8; state
.bit
>= 6; )
917 *dst
++ = utf7enb64
[(state
.accum
>> state
.bit
) % 64];
922 if ( src
== srcEnd
|| wxIsUTF7Direct(cc
= *src
) )
930 // we need to restore the original encoder state if we were called just to
931 // calculate the amount of space needed as we will presumably be called
932 // again to really convert the data now
939 // ----------------------------------------------------------------------------
941 // ----------------------------------------------------------------------------
943 static const wxUint32 utf8_max
[]=
944 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
946 // boundaries of the private use area we use to (temporarily) remap invalid
947 // characters invalid in a UTF-8 encoded string
948 const wxUint32 wxUnicodePUA
= 0x100000;
949 const wxUint32 wxUnicodePUAEnd
= wxUnicodePUA
+ 256;
951 // this table gives the length of the UTF-8 encoding from its first character:
952 const unsigned char tableUtf8Lengths
[256] = {
953 // single-byte sequences (ASCII):
954 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 00..0F
955 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 10..1F
956 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 20..2F
957 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 30..3F
958 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 40..4F
959 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 50..5F
960 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 60..6F
961 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 70..7F
963 // these are invalid:
964 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80..8F
965 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90..9F
966 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A0..AF
967 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // B0..BF
970 // two-byte sequences:
971 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // C2..CF
972 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // D0..DF
974 // three-byte sequences:
975 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // E0..EF
977 // four-byte sequences:
978 4, 4, 4, 4, 4, // F0..F4
980 // these are invalid again (5- or 6-byte
981 // sequences and sequences for code points
982 // above U+10FFFF, as restricted by RFC 3629):
983 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F5..FF
987 wxMBConvStrictUTF8::ToWChar(wchar_t *dst
, size_t dstLen
,
988 const char *src
, size_t srcLen
) const
990 wchar_t *out
= dstLen
? dst
: NULL
;
993 if ( srcLen
== wxNO_LEN
)
994 srcLen
= strlen(src
) + 1;
996 for ( const char *p
= src
; ; p
++ )
998 if ( (srcLen
== wxNO_LEN
? !*p
: !srcLen
) )
1000 // all done successfully, just add the trailing NULL if we are not
1001 // using explicit length
1002 if ( srcLen
== wxNO_LEN
)
1018 if ( out
&& !dstLen
-- )
1022 unsigned char c
= *p
;
1026 if ( srcLen
== 0 ) // the test works for wxNO_LEN too
1029 if ( srcLen
!= wxNO_LEN
)
1036 unsigned len
= tableUtf8Lengths
[c
];
1040 if ( srcLen
< len
) // the test works for wxNO_LEN too
1043 if ( srcLen
!= wxNO_LEN
)
1046 // Char. number range | UTF-8 octet sequence
1047 // (hexadecimal) | (binary)
1048 // ----------------------+----------------------------------------
1049 // 0000 0000 - 0000 007F | 0xxxxxxx
1050 // 0000 0080 - 0000 07FF | 110xxxxx 10xxxxxx
1051 // 0000 0800 - 0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
1052 // 0001 0000 - 0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1054 // Code point value is stored in bits marked with 'x',
1055 // lowest-order bit of the value on the right side in the diagram
1056 // above. (from RFC 3629)
1058 // mask to extract lead byte's value ('x' bits above), by sequence
1060 static const unsigned char leadValueMask
[] = { 0x7F, 0x1F, 0x0F, 0x07 };
1062 // mask and value of lead byte's most significant bits, by length:
1063 static const unsigned char leadMarkerMask
[] = { 0x80, 0xE0, 0xF0, 0xF8 };
1064 static const unsigned char leadMarkerVal
[] = { 0x00, 0xC0, 0xE0, 0xF0 };
1066 len
--; // it's more convenient to work with 0-based length here
1068 // extract the lead byte's value bits:
1069 if ( (c
& leadMarkerMask
[len
]) != leadMarkerVal
[len
] )
1072 code
= c
& leadValueMask
[len
];
1074 // all remaining bytes, if any, are handled in the same way
1075 // regardless of sequence's length:
1076 for ( ; len
; --len
)
1079 if ( (c
& 0xC0) != 0x80 )
1080 return wxCONV_FAILED
;
1088 // cast is ok because wchar_t == wxUint16 if WC_UTF16
1089 if ( encode_utf16(code
, (wxUint16
*)out
) == 2 )
1098 #endif // WC_UTF16/!WC_UTF16
1106 return wxCONV_FAILED
;
1110 wxMBConvStrictUTF8::FromWChar(char *dst
, size_t dstLen
,
1111 const wchar_t *src
, size_t srcLen
) const
1113 char *out
= dstLen
? dst
: NULL
;
1116 for ( const wchar_t *wp
= src
; ; wp
++ )
1118 if ( (srcLen
== wxNO_LEN
? !*wp
: !srcLen
) )
1120 // all done successfully, just add the trailing NULL if we are not
1121 // using explicit length
1122 if ( srcLen
== wxNO_LEN
)
1138 if ( srcLen
!= wxNO_LEN
)
1143 // cast is ok for WC_UTF16
1144 if ( decode_utf16((const wxUint16
*)wp
, code
) == 2 )
1146 // skip the next char too as we decoded a surrogate
1148 if ( srcLen
!= wxNO_LEN
)
1151 #else // wchar_t is UTF-32
1152 code
= *wp
& 0x7fffffff;
1164 out
[0] = (char)code
;
1167 else if ( code
<= 0x07FF )
1175 // NB: this line takes 6 least significant bits, encodes them as
1176 // 10xxxxxx and discards them so that the next byte can be encoded:
1177 out
[1] = 0x80 | (code
& 0x3F); code
>>= 6;
1178 out
[0] = 0xC0 | code
;
1181 else if ( code
< 0xFFFF )
1189 out
[2] = 0x80 | (code
& 0x3F); code
>>= 6;
1190 out
[1] = 0x80 | (code
& 0x3F); code
>>= 6;
1191 out
[0] = 0xE0 | code
;
1194 else if ( code
<= 0x10FFFF )
1202 out
[3] = 0x80 | (code
& 0x3F); code
>>= 6;
1203 out
[2] = 0x80 | (code
& 0x3F); code
>>= 6;
1204 out
[1] = 0x80 | (code
& 0x3F); code
>>= 6;
1205 out
[0] = 0xF0 | code
;
1210 wxFAIL_MSG( wxT("trying to encode undefined Unicode character") );
1223 // we only get here if an error occurs during decoding
1224 return wxCONV_FAILED
;
1227 size_t wxMBConvUTF8::ToWChar(wchar_t *buf
, size_t n
,
1228 const char *psz
, size_t srcLen
) const
1230 if ( m_options
== MAP_INVALID_UTF8_NOT
)
1231 return wxMBConvStrictUTF8::ToWChar(buf
, n
, psz
, srcLen
);
1235 // The length can be either given explicitly or computed implicitly for the
1236 // NUL-terminated strings.
1237 const bool isNulTerminated
= srcLen
== wxNO_LEN
;
1238 while ((isNulTerminated
? *psz
: srcLen
--) && ((!buf
) || (len
< n
)))
1240 const char *opsz
= psz
;
1241 bool invalid
= false;
1242 unsigned char cc
= *psz
++, fc
= cc
;
1244 for (cnt
= 0; fc
& 0x80; cnt
++)
1254 // escape the escape character for octal escapes
1255 if ((m_options
& MAP_INVALID_UTF8_TO_OCTAL
)
1256 && cc
== '\\' && (!buf
|| len
< n
))
1268 // invalid UTF-8 sequence
1273 unsigned ocnt
= cnt
- 1;
1274 wxUint32 res
= cc
& (0x3f >> cnt
);
1278 if ((cc
& 0xC0) != 0x80)
1280 // invalid UTF-8 sequence
1286 res
= (res
<< 6) | (cc
& 0x3f);
1289 if (invalid
|| res
<= utf8_max
[ocnt
])
1291 // illegal UTF-8 encoding
1294 else if ((m_options
& MAP_INVALID_UTF8_TO_PUA
) &&
1295 res
>= wxUnicodePUA
&& res
< wxUnicodePUAEnd
)
1297 // if one of our PUA characters turns up externally
1298 // it must also be treated as an illegal sequence
1299 // (a bit like you have to escape an escape character)
1305 // cast is ok because wchar_t == wxUint16 if WC_UTF16
1306 size_t pa
= encode_utf16(res
, (wxUint16
*)buf
);
1307 if (pa
== wxCONV_FAILED
)
1319 *buf
++ = (wchar_t)res
;
1321 #endif // WC_UTF16/!WC_UTF16
1327 if (m_options
& MAP_INVALID_UTF8_TO_PUA
)
1329 while (opsz
< psz
&& (!buf
|| len
< n
))
1332 // cast is ok because wchar_t == wxUuint16 if WC_UTF16
1333 size_t pa
= encode_utf16((unsigned char)*opsz
+ wxUnicodePUA
, (wxUint16
*)buf
);
1334 wxASSERT(pa
!= wxCONV_FAILED
);
1341 *buf
++ = (wchar_t)(wxUnicodePUA
+ (unsigned char)*opsz
);
1347 else if (m_options
& MAP_INVALID_UTF8_TO_OCTAL
)
1349 while (opsz
< psz
&& (!buf
|| len
< n
))
1351 if ( buf
&& len
+ 3 < n
)
1353 unsigned char on
= *opsz
;
1355 *buf
++ = (wchar_t)( L
'0' + on
/ 0100 );
1356 *buf
++ = (wchar_t)( L
'0' + (on
% 0100) / 010 );
1357 *buf
++ = (wchar_t)( L
'0' + on
% 010 );
1364 else // MAP_INVALID_UTF8_NOT
1366 return wxCONV_FAILED
;
1372 if ( isNulTerminated
)
1374 // Add the trailing NUL in this case if we have a large enough buffer.
1375 if ( buf
&& (len
< n
) )
1378 // And count it in any case.
1385 static inline bool isoctal(wchar_t wch
)
1387 return L
'0' <= wch
&& wch
<= L
'7';
1390 size_t wxMBConvUTF8::FromWChar(char *buf
, size_t n
,
1391 const wchar_t *psz
, size_t srcLen
) const
1393 if ( m_options
== MAP_INVALID_UTF8_NOT
)
1394 return wxMBConvStrictUTF8::FromWChar(buf
, n
, psz
, srcLen
);
1398 while ((srcLen
== wxNO_LEN
? *psz
: srcLen
--) && ((!buf
) || (len
< n
)))
1403 // cast is ok for WC_UTF16
1404 size_t pa
= decode_utf16((const wxUint16
*)psz
, cc
);
1405 psz
+= (pa
== wxCONV_FAILED
) ? 1 : pa
;
1407 cc
= (*psz
++) & 0x7fffffff;
1410 if ( (m_options
& MAP_INVALID_UTF8_TO_PUA
)
1411 && cc
>= wxUnicodePUA
&& cc
< wxUnicodePUAEnd
)
1414 *buf
++ = (char)(cc
- wxUnicodePUA
);
1417 else if ( (m_options
& MAP_INVALID_UTF8_TO_OCTAL
)
1418 && cc
== L
'\\' && psz
[0] == L
'\\' )
1425 else if ( (m_options
& MAP_INVALID_UTF8_TO_OCTAL
) &&
1427 isoctal(psz
[0]) && isoctal(psz
[1]) && isoctal(psz
[2]) )
1431 *buf
++ = (char) ((psz
[0] - L
'0') * 0100 +
1432 (psz
[1] - L
'0') * 010 +
1442 for (cnt
= 0; cc
> utf8_max
[cnt
]; cnt
++)
1458 *buf
++ = (char) ((-128 >> cnt
) | ((cc
>> (cnt
* 6)) & (0x3f >> cnt
)));
1460 *buf
++ = (char) (0x80 | ((cc
>> (cnt
* 6)) & 0x3f));
1466 if (srcLen
== wxNO_LEN
&& buf
&& (len
< n
))
1472 // ============================================================================
1474 // ============================================================================
1476 #ifdef WORDS_BIGENDIAN
1477 #define wxMBConvUTF16straight wxMBConvUTF16BE
1478 #define wxMBConvUTF16swap wxMBConvUTF16LE
1480 #define wxMBConvUTF16swap wxMBConvUTF16BE
1481 #define wxMBConvUTF16straight wxMBConvUTF16LE
1485 size_t wxMBConvUTF16Base::GetLength(const char *src
, size_t srcLen
)
1487 if ( srcLen
== wxNO_LEN
)
1489 // count the number of bytes in input, including the trailing NULs
1490 const wxUint16
*inBuff
= reinterpret_cast<const wxUint16
*>(src
);
1491 for ( srcLen
= 1; *inBuff
++; srcLen
++ )
1494 srcLen
*= BYTES_PER_CHAR
;
1496 else // we already have the length
1498 // we can only convert an entire number of UTF-16 characters
1499 if ( srcLen
% BYTES_PER_CHAR
)
1500 return wxCONV_FAILED
;
1506 // case when in-memory representation is UTF-16 too
1509 // ----------------------------------------------------------------------------
1510 // conversions without endianness change
1511 // ----------------------------------------------------------------------------
1514 wxMBConvUTF16straight::ToWChar(wchar_t *dst
, size_t dstLen
,
1515 const char *src
, size_t srcLen
) const
1517 // set up the scene for using memcpy() (which is presumably more efficient
1518 // than copying the bytes one by one)
1519 srcLen
= GetLength(src
, srcLen
);
1520 if ( srcLen
== wxNO_LEN
)
1521 return wxCONV_FAILED
;
1523 const size_t inLen
= srcLen
/ BYTES_PER_CHAR
;
1526 if ( dstLen
< inLen
)
1527 return wxCONV_FAILED
;
1529 memcpy(dst
, src
, srcLen
);
1536 wxMBConvUTF16straight::FromWChar(char *dst
, size_t dstLen
,
1537 const wchar_t *src
, size_t srcLen
) const
1539 if ( srcLen
== wxNO_LEN
)
1540 srcLen
= wxWcslen(src
) + 1;
1542 srcLen
*= BYTES_PER_CHAR
;
1546 if ( dstLen
< srcLen
)
1547 return wxCONV_FAILED
;
1549 memcpy(dst
, src
, srcLen
);
1555 // ----------------------------------------------------------------------------
1556 // endian-reversing conversions
1557 // ----------------------------------------------------------------------------
1560 wxMBConvUTF16swap::ToWChar(wchar_t *dst
, size_t dstLen
,
1561 const char *src
, size_t srcLen
) const
1563 srcLen
= GetLength(src
, srcLen
);
1564 if ( srcLen
== wxNO_LEN
)
1565 return wxCONV_FAILED
;
1567 srcLen
/= BYTES_PER_CHAR
;
1571 if ( dstLen
< srcLen
)
1572 return wxCONV_FAILED
;
1574 const wxUint16
*inBuff
= reinterpret_cast<const wxUint16
*>(src
);
1575 for ( size_t n
= 0; n
< srcLen
; n
++, inBuff
++ )
1577 *dst
++ = wxUINT16_SWAP_ALWAYS(*inBuff
);
1585 wxMBConvUTF16swap::FromWChar(char *dst
, size_t dstLen
,
1586 const wchar_t *src
, size_t srcLen
) const
1588 if ( srcLen
== wxNO_LEN
)
1589 srcLen
= wxWcslen(src
) + 1;
1591 srcLen
*= BYTES_PER_CHAR
;
1595 if ( dstLen
< srcLen
)
1596 return wxCONV_FAILED
;
1598 wxUint16
*outBuff
= reinterpret_cast<wxUint16
*>(dst
);
1599 for ( size_t n
= 0; n
< srcLen
; n
+= BYTES_PER_CHAR
, src
++ )
1601 *outBuff
++ = wxUINT16_SWAP_ALWAYS(*src
);
1608 #else // !WC_UTF16: wchar_t is UTF-32
1610 // ----------------------------------------------------------------------------
1611 // conversions without endianness change
1612 // ----------------------------------------------------------------------------
1615 wxMBConvUTF16straight::ToWChar(wchar_t *dst
, size_t dstLen
,
1616 const char *src
, size_t srcLen
) const
1618 srcLen
= GetLength(src
, srcLen
);
1619 if ( srcLen
== wxNO_LEN
)
1620 return wxCONV_FAILED
;
1622 const size_t inLen
= srcLen
/ BYTES_PER_CHAR
;
1625 // optimization: return maximal space which could be needed for this
1626 // string even if the real size could be smaller if the buffer contains
1632 const wxUint16
*inBuff
= reinterpret_cast<const wxUint16
*>(src
);
1633 for ( const wxUint16
* const inEnd
= inBuff
+ inLen
; inBuff
< inEnd
; )
1635 const wxUint32 ch
= wxDecodeSurrogate(&inBuff
);
1637 return wxCONV_FAILED
;
1639 if ( ++outLen
> dstLen
)
1640 return wxCONV_FAILED
;
1650 wxMBConvUTF16straight::FromWChar(char *dst
, size_t dstLen
,
1651 const wchar_t *src
, size_t srcLen
) const
1653 if ( srcLen
== wxNO_LEN
)
1654 srcLen
= wxWcslen(src
) + 1;
1657 wxUint16
*outBuff
= reinterpret_cast<wxUint16
*>(dst
);
1658 for ( size_t n
= 0; n
< srcLen
; n
++ )
1660 wxUint16 cc
[2] = { 0 };
1661 const size_t numChars
= encode_utf16(*src
++, cc
);
1662 if ( numChars
== wxCONV_FAILED
)
1663 return wxCONV_FAILED
;
1665 outLen
+= numChars
* BYTES_PER_CHAR
;
1668 if ( outLen
> dstLen
)
1669 return wxCONV_FAILED
;
1672 if ( numChars
== 2 )
1674 // second character of a surrogate
1683 // ----------------------------------------------------------------------------
1684 // endian-reversing conversions
1685 // ----------------------------------------------------------------------------
1688 wxMBConvUTF16swap::ToWChar(wchar_t *dst
, size_t dstLen
,
1689 const char *src
, size_t srcLen
) const
1691 srcLen
= GetLength(src
, srcLen
);
1692 if ( srcLen
== wxNO_LEN
)
1693 return wxCONV_FAILED
;
1695 const size_t inLen
= srcLen
/ BYTES_PER_CHAR
;
1698 // optimization: return maximal space which could be needed for this
1699 // string even if the real size could be smaller if the buffer contains
1705 const wxUint16
*inBuff
= reinterpret_cast<const wxUint16
*>(src
);
1706 for ( const wxUint16
* const inEnd
= inBuff
+ inLen
; inBuff
< inEnd
; )
1711 tmp
[0] = wxUINT16_SWAP_ALWAYS(*inBuff
);
1713 tmp
[1] = wxUINT16_SWAP_ALWAYS(*inBuff
);
1715 const size_t numChars
= decode_utf16(tmp
, ch
);
1716 if ( numChars
== wxCONV_FAILED
)
1717 return wxCONV_FAILED
;
1719 if ( numChars
== 2 )
1722 if ( ++outLen
> dstLen
)
1723 return wxCONV_FAILED
;
1733 wxMBConvUTF16swap::FromWChar(char *dst
, size_t dstLen
,
1734 const wchar_t *src
, size_t srcLen
) const
1736 if ( srcLen
== wxNO_LEN
)
1737 srcLen
= wxWcslen(src
) + 1;
1740 wxUint16
*outBuff
= reinterpret_cast<wxUint16
*>(dst
);
1741 for ( const wchar_t *srcEnd
= src
+ srcLen
; src
< srcEnd
; src
++ )
1743 wxUint16 cc
[2] = { 0 };
1744 const size_t numChars
= encode_utf16(*src
, cc
);
1745 if ( numChars
== wxCONV_FAILED
)
1746 return wxCONV_FAILED
;
1748 outLen
+= numChars
* BYTES_PER_CHAR
;
1751 if ( outLen
> dstLen
)
1752 return wxCONV_FAILED
;
1754 *outBuff
++ = wxUINT16_SWAP_ALWAYS(cc
[0]);
1755 if ( numChars
== 2 )
1757 // second character of a surrogate
1758 *outBuff
++ = wxUINT16_SWAP_ALWAYS(cc
[1]);
1766 #endif // WC_UTF16/!WC_UTF16
1769 // ============================================================================
1771 // ============================================================================
1773 #ifdef WORDS_BIGENDIAN
1774 #define wxMBConvUTF32straight wxMBConvUTF32BE
1775 #define wxMBConvUTF32swap wxMBConvUTF32LE
1777 #define wxMBConvUTF32swap wxMBConvUTF32BE
1778 #define wxMBConvUTF32straight wxMBConvUTF32LE
1782 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF32LE
) wxConvUTF32LE
;
1783 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF32BE
) wxConvUTF32BE
;
1786 size_t wxMBConvUTF32Base::GetLength(const char *src
, size_t srcLen
)
1788 if ( srcLen
== wxNO_LEN
)
1790 // count the number of bytes in input, including the trailing NULs
1791 const wxUint32
*inBuff
= reinterpret_cast<const wxUint32
*>(src
);
1792 for ( srcLen
= 1; *inBuff
++; srcLen
++ )
1795 srcLen
*= BYTES_PER_CHAR
;
1797 else // we already have the length
1799 // we can only convert an entire number of UTF-32 characters
1800 if ( srcLen
% BYTES_PER_CHAR
)
1801 return wxCONV_FAILED
;
1807 // case when in-memory representation is UTF-16
1810 // ----------------------------------------------------------------------------
1811 // conversions without endianness change
1812 // ----------------------------------------------------------------------------
1815 wxMBConvUTF32straight::ToWChar(wchar_t *dst
, size_t dstLen
,
1816 const char *src
, size_t srcLen
) const
1818 srcLen
= GetLength(src
, srcLen
);
1819 if ( srcLen
== wxNO_LEN
)
1820 return wxCONV_FAILED
;
1822 const wxUint32
*inBuff
= reinterpret_cast<const wxUint32
*>(src
);
1823 const size_t inLen
= srcLen
/ BYTES_PER_CHAR
;
1825 for ( size_t n
= 0; n
< inLen
; n
++ )
1827 wxUint16 cc
[2] = { 0 };
1828 const size_t numChars
= encode_utf16(*inBuff
++, cc
);
1829 if ( numChars
== wxCONV_FAILED
)
1830 return wxCONV_FAILED
;
1835 if ( outLen
> dstLen
)
1836 return wxCONV_FAILED
;
1839 if ( numChars
== 2 )
1841 // second character of a surrogate
1851 wxMBConvUTF32straight::FromWChar(char *dst
, size_t dstLen
,
1852 const wchar_t *src
, size_t srcLen
) const
1854 if ( srcLen
== wxNO_LEN
)
1855 srcLen
= wxWcslen(src
) + 1;
1859 // optimization: return maximal space which could be needed for this
1860 // string instead of the exact amount which could be less if there are
1861 // any surrogates in the input
1863 // we consider that surrogates are rare enough to make it worthwhile to
1864 // avoid running the loop below at the cost of slightly extra memory
1866 return srcLen
* BYTES_PER_CHAR
;
1869 wxUint32
*outBuff
= reinterpret_cast<wxUint32
*>(dst
);
1871 for ( const wchar_t * const srcEnd
= src
+ srcLen
; src
< srcEnd
; )
1873 const wxUint32 ch
= wxDecodeSurrogate(&src
);
1875 return wxCONV_FAILED
;
1877 outLen
+= BYTES_PER_CHAR
;
1879 if ( outLen
> dstLen
)
1880 return wxCONV_FAILED
;
1888 // ----------------------------------------------------------------------------
1889 // endian-reversing conversions
1890 // ----------------------------------------------------------------------------
1893 wxMBConvUTF32swap::ToWChar(wchar_t *dst
, size_t dstLen
,
1894 const char *src
, size_t srcLen
) const
1896 srcLen
= GetLength(src
, srcLen
);
1897 if ( srcLen
== wxNO_LEN
)
1898 return wxCONV_FAILED
;
1900 const wxUint32
*inBuff
= reinterpret_cast<const wxUint32
*>(src
);
1901 const size_t inLen
= srcLen
/ BYTES_PER_CHAR
;
1903 for ( size_t n
= 0; n
< inLen
; n
++, inBuff
++ )
1905 wxUint16 cc
[2] = { 0 };
1906 const size_t numChars
= encode_utf16(wxUINT32_SWAP_ALWAYS(*inBuff
), cc
);
1907 if ( numChars
== wxCONV_FAILED
)
1908 return wxCONV_FAILED
;
1913 if ( outLen
> dstLen
)
1914 return wxCONV_FAILED
;
1917 if ( numChars
== 2 )
1919 // second character of a surrogate
1929 wxMBConvUTF32swap::FromWChar(char *dst
, size_t dstLen
,
1930 const wchar_t *src
, size_t srcLen
) const
1932 if ( srcLen
== wxNO_LEN
)
1933 srcLen
= wxWcslen(src
) + 1;
1937 // optimization: return maximal space which could be needed for this
1938 // string instead of the exact amount which could be less if there are
1939 // any surrogates in the input
1941 // we consider that surrogates are rare enough to make it worthwhile to
1942 // avoid running the loop below at the cost of slightly extra memory
1944 return srcLen
*BYTES_PER_CHAR
;
1947 wxUint32
*outBuff
= reinterpret_cast<wxUint32
*>(dst
);
1949 for ( const wchar_t * const srcEnd
= src
+ srcLen
; src
< srcEnd
; )
1951 const wxUint32 ch
= wxDecodeSurrogate(&src
);
1953 return wxCONV_FAILED
;
1955 outLen
+= BYTES_PER_CHAR
;
1957 if ( outLen
> dstLen
)
1958 return wxCONV_FAILED
;
1960 *outBuff
++ = wxUINT32_SWAP_ALWAYS(ch
);
1966 #else // !WC_UTF16: wchar_t is UTF-32
1968 // ----------------------------------------------------------------------------
1969 // conversions without endianness change
1970 // ----------------------------------------------------------------------------
1973 wxMBConvUTF32straight::ToWChar(wchar_t *dst
, size_t dstLen
,
1974 const char *src
, size_t srcLen
) const
1976 // use memcpy() as it should be much faster than hand-written loop
1977 srcLen
= GetLength(src
, srcLen
);
1978 if ( srcLen
== wxNO_LEN
)
1979 return wxCONV_FAILED
;
1981 const size_t inLen
= srcLen
/BYTES_PER_CHAR
;
1984 if ( dstLen
< inLen
)
1985 return wxCONV_FAILED
;
1987 memcpy(dst
, src
, srcLen
);
1994 wxMBConvUTF32straight::FromWChar(char *dst
, size_t dstLen
,
1995 const wchar_t *src
, size_t srcLen
) const
1997 if ( srcLen
== wxNO_LEN
)
1998 srcLen
= wxWcslen(src
) + 1;
2000 srcLen
*= BYTES_PER_CHAR
;
2004 if ( dstLen
< srcLen
)
2005 return wxCONV_FAILED
;
2007 memcpy(dst
, src
, srcLen
);
2013 // ----------------------------------------------------------------------------
2014 // endian-reversing conversions
2015 // ----------------------------------------------------------------------------
2018 wxMBConvUTF32swap::ToWChar(wchar_t *dst
, size_t dstLen
,
2019 const char *src
, size_t srcLen
) const
2021 srcLen
= GetLength(src
, srcLen
);
2022 if ( srcLen
== wxNO_LEN
)
2023 return wxCONV_FAILED
;
2025 srcLen
/= BYTES_PER_CHAR
;
2029 if ( dstLen
< srcLen
)
2030 return wxCONV_FAILED
;
2032 const wxUint32
*inBuff
= reinterpret_cast<const wxUint32
*>(src
);
2033 for ( size_t n
= 0; n
< srcLen
; n
++, inBuff
++ )
2035 *dst
++ = wxUINT32_SWAP_ALWAYS(*inBuff
);
2043 wxMBConvUTF32swap::FromWChar(char *dst
, size_t dstLen
,
2044 const wchar_t *src
, size_t srcLen
) const
2046 if ( srcLen
== wxNO_LEN
)
2047 srcLen
= wxWcslen(src
) + 1;
2049 srcLen
*= BYTES_PER_CHAR
;
2053 if ( dstLen
< srcLen
)
2054 return wxCONV_FAILED
;
2056 wxUint32
*outBuff
= reinterpret_cast<wxUint32
*>(dst
);
2057 for ( size_t n
= 0; n
< srcLen
; n
+= BYTES_PER_CHAR
, src
++ )
2059 *outBuff
++ = wxUINT32_SWAP_ALWAYS(*src
);
2066 #endif // WC_UTF16/!WC_UTF16
2069 // ============================================================================
2070 // The classes doing conversion using the iconv_xxx() functions
2071 // ============================================================================
2075 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with
2076 // E2BIG if output buffer is _exactly_ as big as needed. Such case is
2077 // (unless there's yet another bug in glibc) the only case when iconv()
2078 // returns with (size_t)-1 (which means error) and says there are 0 bytes
2079 // left in the input buffer -- when _real_ error occurs,
2080 // bytes-left-in-input buffer is non-zero. Hence, this alternative test for
2082 // [This bug does not appear in glibc 2.2.]
2083 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
2084 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
2085 (errno != E2BIG || bufLeft != 0))
2087 #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
2090 #define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x))
2092 #define ICONV_T_INVALID ((iconv_t)-1)
2094 #if SIZEOF_WCHAR_T == 4
2095 #define WC_BSWAP wxUINT32_SWAP_ALWAYS
2096 #define WC_ENC wxFONTENCODING_UTF32
2097 #elif SIZEOF_WCHAR_T == 2
2098 #define WC_BSWAP wxUINT16_SWAP_ALWAYS
2099 #define WC_ENC wxFONTENCODING_UTF16
2100 #else // sizeof(wchar_t) != 2 nor 4
2101 // does this ever happen?
2102 #error "Unknown sizeof(wchar_t): please report this to wx-dev@lists.wxwindows.org"
2105 // ----------------------------------------------------------------------------
2106 // wxMBConv_iconv: encapsulates an iconv character set
2107 // ----------------------------------------------------------------------------
2109 class wxMBConv_iconv
: public wxMBConv
2112 wxMBConv_iconv(const char *name
);
2113 virtual ~wxMBConv_iconv();
2115 // implement base class virtual methods
2116 virtual size_t ToWChar(wchar_t *dst
, size_t dstLen
,
2117 const char *src
, size_t srcLen
= wxNO_LEN
) const;
2118 virtual size_t FromWChar(char *dst
, size_t dstLen
,
2119 const wchar_t *src
, size_t srcLen
= wxNO_LEN
) const;
2120 virtual size_t GetMBNulLen() const;
2122 #if wxUSE_UNICODE_UTF8
2123 virtual bool IsUTF8() const;
2126 virtual wxMBConv
*Clone() const
2128 wxMBConv_iconv
*p
= new wxMBConv_iconv(m_name
);
2129 p
->m_minMBCharWidth
= m_minMBCharWidth
;
2134 { return (m2w
!= ICONV_T_INVALID
) && (w2m
!= ICONV_T_INVALID
); }
2137 // the iconv handlers used to translate from multibyte
2138 // to wide char and in the other direction
2143 // guards access to m2w and w2m objects
2144 wxMutex m_iconvMutex
;
2148 // the name (for iconv_open()) of a wide char charset -- if none is
2149 // available on this machine, it will remain NULL
2150 static wxString ms_wcCharsetName
;
2152 // true if the wide char encoding we use (i.e. ms_wcCharsetName) has
2153 // different endian-ness than the native one
2154 static bool ms_wcNeedsSwap
;
2157 // name of the encoding handled by this conversion
2160 // cached result of GetMBNulLen(); set to 0 meaning "unknown"
2162 size_t m_minMBCharWidth
;
2165 // make the constructor available for unit testing
2166 WXDLLIMPEXP_BASE wxMBConv
* new_wxMBConv_iconv( const char* name
)
2168 wxMBConv_iconv
* result
= new wxMBConv_iconv( name
);
2169 if ( !result
->IsOk() )
2178 wxString
wxMBConv_iconv::ms_wcCharsetName
;
2179 bool wxMBConv_iconv::ms_wcNeedsSwap
= false;
2181 wxMBConv_iconv::wxMBConv_iconv(const char *name
)
2182 : m_name(wxStrdup(name
))
2184 m_minMBCharWidth
= 0;
2186 // check for charset that represents wchar_t:
2187 if ( ms_wcCharsetName
.empty() )
2189 wxLogTrace(TRACE_STRCONV
, wxT("Looking for wide char codeset:"));
2192 const wxChar
*const *names
= wxFontMapperBase::GetAllEncodingNames(WC_ENC
);
2193 #else // !wxUSE_FONTMAP
2194 static const wxChar
*const names_static
[] =
2196 #if SIZEOF_WCHAR_T == 4
2198 #elif SIZEOF_WCHAR_T == 2
2203 const wxChar
*const *names
= names_static
;
2204 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
2206 for ( ; *names
&& ms_wcCharsetName
.empty(); ++names
)
2208 const wxString
nameCS(*names
);
2210 // first try charset with explicit bytesex info (e.g. "UCS-4LE"):
2211 wxString
nameXE(nameCS
);
2213 #ifdef WORDS_BIGENDIAN
2214 nameXE
+= wxT("BE");
2215 #else // little endian
2216 nameXE
+= wxT("LE");
2219 wxLogTrace(TRACE_STRCONV
, wxT(" trying charset \"%s\""),
2222 m2w
= iconv_open(nameXE
.ToAscii(), name
);
2223 if ( m2w
== ICONV_T_INVALID
)
2225 // try charset w/o bytesex info (e.g. "UCS4")
2226 wxLogTrace(TRACE_STRCONV
, wxT(" trying charset \"%s\""),
2228 m2w
= iconv_open(nameCS
.ToAscii(), name
);
2230 // and check for bytesex ourselves:
2231 if ( m2w
!= ICONV_T_INVALID
)
2233 char buf
[2], *bufPtr
;
2242 outsz
= SIZEOF_WCHAR_T
* 2;
2243 char* wbufPtr
= (char*)wbuf
;
2247 m2w
, ICONV_CHAR_CAST(&bufPtr
), &insz
,
2250 if (ICONV_FAILED(res
, insz
))
2252 wxLogLastError(wxT("iconv"));
2253 wxLogError(_("Conversion to charset '%s' doesn't work."),
2256 else // ok, can convert to this encoding, remember it
2258 ms_wcCharsetName
= nameCS
;
2259 ms_wcNeedsSwap
= wbuf
[0] != (wchar_t)buf
[0];
2263 else // use charset not requiring byte swapping
2265 ms_wcCharsetName
= nameXE
;
2269 wxLogTrace(TRACE_STRCONV
,
2270 wxT("iconv wchar_t charset is \"%s\"%s"),
2271 ms_wcCharsetName
.empty() ? wxString("<none>")
2273 ms_wcNeedsSwap
? wxT(" (needs swap)")
2276 else // we already have ms_wcCharsetName
2278 m2w
= iconv_open(ms_wcCharsetName
.ToAscii(), name
);
2281 if ( ms_wcCharsetName
.empty() )
2283 w2m
= ICONV_T_INVALID
;
2287 w2m
= iconv_open(name
, ms_wcCharsetName
.ToAscii());
2288 if ( w2m
== ICONV_T_INVALID
)
2290 wxLogTrace(TRACE_STRCONV
,
2291 wxT("\"%s\" -> \"%s\" works but not the converse!?"),
2292 ms_wcCharsetName
.c_str(), name
);
2297 wxMBConv_iconv::~wxMBConv_iconv()
2299 free(const_cast<char *>(m_name
));
2301 if ( m2w
!= ICONV_T_INVALID
)
2303 if ( w2m
!= ICONV_T_INVALID
)
2308 wxMBConv_iconv::ToWChar(wchar_t *dst
, size_t dstLen
,
2309 const char *src
, size_t srcLen
) const
2311 if ( srcLen
== wxNO_LEN
)
2313 // find the string length: notice that must be done differently for
2314 // NUL-terminated strings and UTF-16/32 which are terminated with 2/4
2316 const size_t nulLen
= GetMBNulLen();
2320 return wxCONV_FAILED
;
2323 srcLen
= strlen(src
); // arguably more optimized than our version
2328 // for UTF-16/32 not only we need to have 2/4 consecutive NULs
2329 // but they also have to start at character boundary and not
2330 // span two adjacent characters
2332 for ( p
= src
; NotAllNULs(p
, nulLen
); p
+= nulLen
)
2338 // when we're determining the length of the string ourselves we count
2339 // the terminating NUL(s) as part of it and always NUL-terminate the
2344 // we express length in the number of (wide) characters but iconv always
2345 // counts buffer sizes it in bytes
2346 dstLen
*= SIZEOF_WCHAR_T
;
2349 // NB: iconv() is MT-safe, but each thread must use its own iconv_t handle.
2350 // Unfortunately there are a couple of global wxCSConv objects such as
2351 // wxConvLocal that are used all over wx code, so we have to make sure
2352 // the handle is used by at most one thread at the time. Otherwise
2353 // only a few wx classes would be safe to use from non-main threads
2354 // as MB<->WC conversion would fail "randomly".
2355 wxMutexLocker
lock(wxConstCast(this, wxMBConv_iconv
)->m_iconvMutex
);
2356 #endif // wxUSE_THREADS
2359 const char *pszPtr
= src
;
2363 char* bufPtr
= (char*)dst
;
2365 // have destination buffer, convert there
2366 size_t dstLenOrig
= dstLen
;
2368 ICONV_CHAR_CAST(&pszPtr
), &srcLen
,
2371 // convert the number of bytes converted as returned by iconv to the
2372 // number of (wide) characters converted that we need
2373 res
= (dstLenOrig
- dstLen
) / SIZEOF_WCHAR_T
;
2377 // convert to native endianness
2378 for ( unsigned i
= 0; i
< res
; i
++ )
2379 dst
[i
] = WC_BSWAP(dst
[i
]);
2382 else // no destination buffer
2384 // convert using temp buffer to calculate the size of the buffer needed
2390 char* bufPtr
= (char*)tbuf
;
2391 dstLen
= 8 * SIZEOF_WCHAR_T
;
2394 ICONV_CHAR_CAST(&pszPtr
), &srcLen
,
2397 res
+= 8 - (dstLen
/ SIZEOF_WCHAR_T
);
2399 while ((cres
== (size_t)-1) && (errno
== E2BIG
));
2402 if (ICONV_FAILED(cres
, srcLen
))
2404 //VS: it is ok if iconv fails, hence trace only
2405 wxLogTrace(TRACE_STRCONV
, wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
2406 return wxCONV_FAILED
;
2412 size_t wxMBConv_iconv::FromWChar(char *dst
, size_t dstLen
,
2413 const wchar_t *src
, size_t srcLen
) const
2416 // NB: explained in MB2WC
2417 wxMutexLocker
lock(wxConstCast(this, wxMBConv_iconv
)->m_iconvMutex
);
2420 if ( srcLen
== wxNO_LEN
)
2421 srcLen
= wxWcslen(src
) + 1;
2423 size_t inbuflen
= srcLen
* SIZEOF_WCHAR_T
;
2424 size_t outbuflen
= dstLen
;
2427 wchar_t *tmpbuf
= 0;
2431 // need to copy to temp buffer to switch endianness
2432 // (doing WC_BSWAP twice on the original buffer won't work, as it
2433 // could be in read-only memory, or be accessed in some other thread)
2434 tmpbuf
= (wchar_t *)malloc(inbuflen
);
2435 for ( size_t i
= 0; i
< srcLen
; i
++ )
2436 tmpbuf
[i
] = WC_BSWAP(src
[i
]);
2441 char* inbuf
= (char*)src
;
2444 // have destination buffer, convert there
2445 cres
= iconv(w2m
, ICONV_CHAR_CAST(&inbuf
), &inbuflen
, &dst
, &outbuflen
);
2447 res
= dstLen
- outbuflen
;
2449 else // no destination buffer
2451 // convert using temp buffer to calculate the size of the buffer needed
2457 outbuflen
= WXSIZEOF(tbuf
);
2459 cres
= iconv(w2m
, ICONV_CHAR_CAST(&inbuf
), &inbuflen
, &dst
, &outbuflen
);
2461 res
+= WXSIZEOF(tbuf
) - outbuflen
;
2463 while ((cres
== (size_t)-1) && (errno
== E2BIG
));
2471 if (ICONV_FAILED(cres
, inbuflen
))
2473 wxLogTrace(TRACE_STRCONV
, wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
2474 return wxCONV_FAILED
;
2480 size_t wxMBConv_iconv::GetMBNulLen() const
2482 if ( m_minMBCharWidth
== 0 )
2484 wxMBConv_iconv
* const self
= wxConstCast(this, wxMBConv_iconv
);
2487 // NB: explained in MB2WC
2488 wxMutexLocker
lock(self
->m_iconvMutex
);
2491 const wchar_t *wnul
= L
"";
2492 char buf
[8]; // should be enough for NUL in any encoding
2493 size_t inLen
= sizeof(wchar_t),
2494 outLen
= WXSIZEOF(buf
);
2495 char *inBuff
= (char *)wnul
;
2496 char *outBuff
= buf
;
2497 if ( iconv(w2m
, ICONV_CHAR_CAST(&inBuff
), &inLen
, &outBuff
, &outLen
) == (size_t)-1 )
2499 self
->m_minMBCharWidth
= (size_t)-1;
2503 self
->m_minMBCharWidth
= outBuff
- buf
;
2507 return m_minMBCharWidth
;
2510 #if wxUSE_UNICODE_UTF8
2511 bool wxMBConv_iconv::IsUTF8() const
2513 return wxStricmp(m_name
, "UTF-8") == 0 ||
2514 wxStricmp(m_name
, "UTF8") == 0;
2518 #endif // HAVE_ICONV
2521 // ============================================================================
2522 // Win32 conversion classes
2523 // ============================================================================
2525 #ifdef wxHAVE_WIN32_MB2WC
2529 extern WXDLLIMPEXP_BASE
long wxCharsetToCodepage(const char *charset
);
2530 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
);
2533 class wxMBConv_win32
: public wxMBConv
2538 m_CodePage
= CP_ACP
;
2539 m_minMBCharWidth
= 0;
2542 wxMBConv_win32(const wxMBConv_win32
& conv
)
2545 m_CodePage
= conv
.m_CodePage
;
2546 m_minMBCharWidth
= conv
.m_minMBCharWidth
;
2550 wxMBConv_win32(const char* name
)
2552 m_CodePage
= wxCharsetToCodepage(name
);
2553 m_minMBCharWidth
= 0;
2556 wxMBConv_win32(wxFontEncoding encoding
)
2558 m_CodePage
= wxEncodingToCodepage(encoding
);
2559 m_minMBCharWidth
= 0;
2561 #endif // wxUSE_FONTMAP
2563 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
2565 // note that we have to use MB_ERR_INVALID_CHARS flag as it without it
2566 // the behaviour is not compatible with the Unix version (using iconv)
2567 // and break the library itself, e.g. wxTextInputStream::NextChar()
2568 // wouldn't work if reading an incomplete MB char didn't result in an
2571 // Moreover, MB_ERR_INVALID_CHARS is only supported on Win 2K SP4 or
2572 // Win XP or newer and it is not supported for UTF-[78] so we always
2573 // use our own conversions in this case. See
2574 // http://blogs.msdn.com/michkap/archive/2005/04/19/409566.aspx
2575 // http://msdn.microsoft.com/library/en-us/intl/unicode_17si.asp
2576 if ( m_CodePage
== CP_UTF8
)
2578 return wxMBConvUTF8().MB2WC(buf
, psz
, n
);
2581 if ( m_CodePage
== CP_UTF7
)
2583 return wxMBConvUTF7().MB2WC(buf
, psz
, n
);
2587 if ( (m_CodePage
< 50000 && m_CodePage
!= CP_SYMBOL
) &&
2588 IsAtLeastWin2kSP4() )
2590 flags
= MB_ERR_INVALID_CHARS
;
2593 const size_t len
= ::MultiByteToWideChar
2595 m_CodePage
, // code page
2596 flags
, // flags: fall on error
2597 psz
, // input string
2598 -1, // its length (NUL-terminated)
2599 buf
, // output string
2600 buf
? n
: 0 // size of output buffer
2604 // function totally failed
2605 return wxCONV_FAILED
;
2608 // if we were really converting and didn't use MB_ERR_INVALID_CHARS,
2609 // check if we succeeded, by doing a double trip:
2610 if ( !flags
&& buf
)
2612 const size_t mbLen
= strlen(psz
);
2613 wxCharBuffer
mbBuf(mbLen
);
2614 if ( ::WideCharToMultiByte
2621 mbLen
+ 1, // size in bytes, not length
2625 strcmp(mbBuf
, psz
) != 0 )
2627 // we didn't obtain the same thing we started from, hence
2628 // the conversion was lossy and we consider that it failed
2629 return wxCONV_FAILED
;
2633 // note that it returns count of written chars for buf != NULL and size
2634 // of the needed buffer for buf == NULL so in either case the length of
2635 // the string (which never includes the terminating NUL) is one less
2639 virtual size_t WC2MB(char *buf
, const wchar_t *pwz
, size_t n
) const
2642 we have a problem here: by default, WideCharToMultiByte() may
2643 replace characters unrepresentable in the target code page with bad
2644 quality approximations such as turning "1/2" symbol (U+00BD) into
2645 "1" for the code pages which don't have it and we, obviously, want
2646 to avoid this at any price
2648 the trouble is that this function does it _silently_, i.e. it won't
2649 even tell us whether it did or not... Win98/2000 and higher provide
2650 WC_NO_BEST_FIT_CHARS but it doesn't work for the older systems and
2651 we have to resort to a round trip, i.e. check that converting back
2652 results in the same string -- this is, of course, expensive but
2653 otherwise we simply can't be sure to not garble the data.
2656 // determine if we can rely on WC_NO_BEST_FIT_CHARS: according to MSDN
2657 // it doesn't work with CJK encodings (which we test for rather roughly
2658 // here...) nor with UTF-7/8 nor, of course, with Windows versions not
2660 BOOL usedDef
wxDUMMY_INITIALIZE(false);
2663 if ( CanUseNoBestFit() && m_CodePage
< 50000 )
2665 // it's our lucky day
2666 flags
= WC_NO_BEST_FIT_CHARS
;
2667 pUsedDef
= &usedDef
;
2669 else // old system or unsupported encoding
2675 const size_t len
= ::WideCharToMultiByte
2677 m_CodePage
, // code page
2678 flags
, // either none or no best fit
2679 pwz
, // input string
2680 -1, // it is (wide) NUL-terminated
2681 buf
, // output buffer
2682 buf
? n
: 0, // and its size
2683 NULL
, // default "replacement" char
2684 pUsedDef
// [out] was it used?
2689 // function totally failed
2690 return wxCONV_FAILED
;
2693 // we did something, check if we really succeeded
2696 // check if the conversion failed, i.e. if any replacements
2699 return wxCONV_FAILED
;
2701 else // we must resort to double tripping...
2703 // first we need to ensure that we really have the MB data: this is
2704 // not the case if we're called with NULL buffer, in which case we
2705 // need to do the conversion yet again
2706 wxCharBuffer bufDef
;
2709 bufDef
= wxCharBuffer(len
);
2710 buf
= bufDef
.data();
2711 if ( !::WideCharToMultiByte(m_CodePage
, flags
, pwz
, -1,
2712 buf
, len
, NULL
, NULL
) )
2713 return wxCONV_FAILED
;
2718 wxWCharBuffer
wcBuf(n
);
2719 if ( MB2WC(wcBuf
.data(), buf
, n
+ 1) == wxCONV_FAILED
||
2720 wcscmp(wcBuf
, pwz
) != 0 )
2722 // we didn't obtain the same thing we started from, hence
2723 // the conversion was lossy and we consider that it failed
2724 return wxCONV_FAILED
;
2728 // see the comment above for the reason of "len - 1"
2732 virtual size_t GetMBNulLen() const
2734 if ( m_minMBCharWidth
== 0 )
2736 int len
= ::WideCharToMultiByte
2738 m_CodePage
, // code page
2740 L
"", // input string
2741 1, // translate just the NUL
2742 NULL
, // output buffer
2744 NULL
, // no replacement char
2745 NULL
// [out] don't care if it was used
2748 wxMBConv_win32
* const self
= wxConstCast(this, wxMBConv_win32
);
2752 wxLogDebug(wxT("Unexpected NUL length %d"), len
);
2753 self
->m_minMBCharWidth
= (size_t)-1;
2757 self
->m_minMBCharWidth
= (size_t)-1;
2763 self
->m_minMBCharWidth
= len
;
2768 return m_minMBCharWidth
;
2771 virtual wxMBConv
*Clone() const { return new wxMBConv_win32(*this); }
2773 bool IsOk() const { return m_CodePage
!= -1; }
2776 static bool CanUseNoBestFit()
2778 static int s_isWin98Or2k
= -1;
2780 if ( s_isWin98Or2k
== -1 )
2783 switch ( wxGetOsVersion(&verMaj
, &verMin
) )
2785 case wxOS_WINDOWS_9X
:
2786 s_isWin98Or2k
= verMaj
>= 4 && verMin
>= 10;
2789 case wxOS_WINDOWS_NT
:
2790 s_isWin98Or2k
= verMaj
>= 5;
2794 // unknown: be conservative by default
2799 wxASSERT_MSG( s_isWin98Or2k
!= -1, wxT("should be set above") );
2802 return s_isWin98Or2k
== 1;
2805 static bool IsAtLeastWin2kSP4()
2810 static int s_isAtLeastWin2kSP4
= -1;
2812 if ( s_isAtLeastWin2kSP4
== -1 )
2814 OSVERSIONINFOEX ver
;
2816 memset(&ver
, 0, sizeof(ver
));
2817 ver
.dwOSVersionInfoSize
= sizeof(ver
);
2818 GetVersionEx((OSVERSIONINFO
*)&ver
);
2820 s_isAtLeastWin2kSP4
=
2821 ((ver
.dwMajorVersion
> 5) || // Vista+
2822 (ver
.dwMajorVersion
== 5 && ver
.dwMinorVersion
> 0) || // XP/2003
2823 (ver
.dwMajorVersion
== 5 && ver
.dwMinorVersion
== 0 &&
2824 ver
.wServicePackMajor
>= 4)) // 2000 SP4+
2828 return s_isAtLeastWin2kSP4
== 1;
2833 // the code page we're working with
2836 // cached result of GetMBNulLen(), set to 0 initially meaning
2838 size_t m_minMBCharWidth
;
2841 #endif // wxHAVE_WIN32_MB2WC
2844 // ============================================================================
2845 // wxEncodingConverter based conversion classes
2846 // ============================================================================
2850 class wxMBConv_wxwin
: public wxMBConv
2855 // Refuse to use broken wxEncodingConverter code for Mac-specific encodings.
2856 // The wxMBConv_cf class does a better job.
2857 m_ok
= (m_enc
< wxFONTENCODING_MACMIN
|| m_enc
> wxFONTENCODING_MACMAX
) &&
2858 m2w
.Init(m_enc
, wxFONTENCODING_UNICODE
) &&
2859 w2m
.Init(wxFONTENCODING_UNICODE
, m_enc
);
2863 // temporarily just use wxEncodingConverter stuff,
2864 // so that it works while a better implementation is built
2865 wxMBConv_wxwin(const char* name
)
2868 m_enc
= wxFontMapperBase::Get()->CharsetToEncoding(name
, false);
2870 m_enc
= wxFONTENCODING_SYSTEM
;
2875 wxMBConv_wxwin(wxFontEncoding enc
)
2882 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t WXUNUSED(n
)) const
2884 size_t inbuf
= strlen(psz
);
2887 if (!m2w
.Convert(psz
, buf
))
2888 return wxCONV_FAILED
;
2893 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t WXUNUSED(n
)) const
2895 const size_t inbuf
= wxWcslen(psz
);
2898 if (!w2m
.Convert(psz
, buf
))
2899 return wxCONV_FAILED
;
2905 virtual size_t GetMBNulLen() const
2909 case wxFONTENCODING_UTF16BE
:
2910 case wxFONTENCODING_UTF16LE
:
2913 case wxFONTENCODING_UTF32BE
:
2914 case wxFONTENCODING_UTF32LE
:
2922 virtual wxMBConv
*Clone() const { return new wxMBConv_wxwin(m_enc
); }
2924 bool IsOk() const { return m_ok
; }
2927 wxFontEncoding m_enc
;
2928 wxEncodingConverter m2w
, w2m
;
2931 // were we initialized successfully?
2934 wxDECLARE_NO_COPY_CLASS(wxMBConv_wxwin
);
2937 // make the constructors available for unit testing
2938 WXDLLIMPEXP_BASE wxMBConv
* new_wxMBConv_wxwin( const char* name
)
2940 wxMBConv_wxwin
* result
= new wxMBConv_wxwin( name
);
2941 if ( !result
->IsOk() )
2950 #endif // wxUSE_FONTMAP
2952 // ============================================================================
2953 // wxCSConv implementation
2954 // ============================================================================
2956 void wxCSConv::Init()
2962 void wxCSConv::SetEncoding(wxFontEncoding encoding
)
2966 case wxFONTENCODING_MAX
:
2967 case wxFONTENCODING_SYSTEM
:
2970 // It's ok to not have encoding value if we have a name for it.
2971 m_encoding
= wxFONTENCODING_SYSTEM
;
2973 else // No name neither.
2975 // Fall back to the system default encoding in this case (not
2976 // sure how much sense does this make but this is how the old
2977 // code used to behave).
2979 m_encoding
= wxLocale::GetSystemEncoding();
2980 if ( m_encoding
== wxFONTENCODING_SYSTEM
)
2981 #endif // wxUSE_INTL
2982 m_encoding
= wxFONTENCODING_ISO8859_1
;
2986 case wxFONTENCODING_DEFAULT
:
2987 // wxFONTENCODING_DEFAULT is same as US-ASCII in this context
2988 m_encoding
= wxFONTENCODING_ISO8859_1
;
2992 // Just use the provided encoding.
2993 m_encoding
= encoding
;
2997 wxCSConv::wxCSConv(const wxString
& charset
)
3001 if ( !charset
.empty() )
3003 SetName(charset
.ToAscii());
3007 SetEncoding(wxFontMapperBase::GetEncodingFromName(charset
));
3009 SetEncoding(wxFONTENCODING_SYSTEM
);
3012 m_convReal
= DoCreate();
3015 wxCSConv::wxCSConv(wxFontEncoding encoding
)
3017 if ( encoding
== wxFONTENCODING_MAX
|| encoding
== wxFONTENCODING_DEFAULT
)
3019 wxFAIL_MSG( wxT("invalid encoding value in wxCSConv ctor") );
3021 encoding
= wxFONTENCODING_SYSTEM
;
3026 SetEncoding(encoding
);
3028 m_convReal
= DoCreate();
3031 wxCSConv::~wxCSConv()
3036 wxCSConv::wxCSConv(const wxCSConv
& conv
)
3041 SetName(conv
.m_name
);
3042 SetEncoding(conv
.m_encoding
);
3044 m_convReal
= DoCreate();
3047 wxCSConv
& wxCSConv::operator=(const wxCSConv
& conv
)
3051 SetName(conv
.m_name
);
3052 SetEncoding(conv
.m_encoding
);
3054 m_convReal
= DoCreate();
3059 void wxCSConv::Clear()
3064 wxDELETE(m_convReal
);
3067 void wxCSConv::SetName(const char *charset
)
3070 m_name
= wxStrdup(charset
);
3075 WX_DECLARE_HASH_MAP( wxFontEncoding
, wxString
, wxIntegerHash
, wxIntegerEqual
,
3076 wxEncodingNameCache
);
3078 static wxEncodingNameCache gs_nameCache
;
3081 wxMBConv
*wxCSConv::DoCreate() const
3084 wxLogTrace(TRACE_STRCONV
,
3085 wxT("creating conversion for %s"),
3087 : (const char*)wxFontMapperBase::GetEncodingName(m_encoding
).mb_str()));
3088 #endif // wxUSE_FONTMAP
3090 // check for the special case of ASCII or ISO8859-1 charset: as we have
3091 // special knowledge of it anyhow, we don't need to create a special
3092 // conversion object
3093 if ( m_encoding
== wxFONTENCODING_ISO8859_1
)
3095 // don't convert at all
3099 // we trust OS to do conversion better than we can so try external
3100 // conversion methods first
3102 // the full order is:
3103 // 1. OS conversion (iconv() under Unix or Win32 API)
3104 // 2. hard coded conversions for UTF
3105 // 3. wxEncodingConverter as fall back
3111 #endif // !wxUSE_FONTMAP
3114 wxFontEncoding
encoding(m_encoding
);
3119 wxMBConv_iconv
*conv
= new wxMBConv_iconv(m_name
);
3127 wxFontMapperBase::Get()->CharsetToEncoding(m_name
, false);
3128 #endif // wxUSE_FONTMAP
3132 const wxEncodingNameCache::iterator it
= gs_nameCache
.find(encoding
);
3133 if ( it
!= gs_nameCache
.end() )
3135 if ( it
->second
.empty() )
3138 wxMBConv_iconv
*conv
= new wxMBConv_iconv(it
->second
.ToAscii());
3145 const wxChar
* const* names
= wxFontMapperBase::GetAllEncodingNames(encoding
);
3146 // CS : in case this does not return valid names (eg for MacRoman)
3147 // encoding got a 'failure' entry in the cache all the same,
3148 // although it just has to be created using a different method, so
3149 // only store failed iconv creation attempts (or perhaps we
3150 // shoulnd't do this at all ?)
3151 if ( names
[0] != NULL
)
3153 for ( ; *names
; ++names
)
3155 // FIXME-UTF8: wxFontMapperBase::GetAllEncodingNames()
3156 // will need changes that will obsolete this
3157 wxString
name(*names
);
3158 wxMBConv_iconv
*conv
= new wxMBConv_iconv(name
.ToAscii());
3161 gs_nameCache
[encoding
] = *names
;
3168 gs_nameCache
[encoding
] = wxT(""); // cache the failure
3171 #endif // wxUSE_FONTMAP
3173 #endif // HAVE_ICONV
3175 #ifdef wxHAVE_WIN32_MB2WC
3178 wxMBConv_win32
*conv
= m_name
? new wxMBConv_win32(m_name
)
3179 : new wxMBConv_win32(m_encoding
);
3188 #endif // wxHAVE_WIN32_MB2WC
3192 // leave UTF16 and UTF32 to the built-ins of wx
3193 if ( m_name
|| ( m_encoding
< wxFONTENCODING_UTF16BE
||
3194 ( m_encoding
>= wxFONTENCODING_MACMIN
&& m_encoding
<= wxFONTENCODING_MACMAX
) ) )
3197 wxMBConv_cf
*conv
= m_name
? new wxMBConv_cf(m_name
)
3198 : new wxMBConv_cf(m_encoding
);
3200 wxMBConv_cf
*conv
= new wxMBConv_cf(m_encoding
);
3209 #endif // __DARWIN__
3212 wxFontEncoding enc
= m_encoding
;
3214 if ( enc
== wxFONTENCODING_SYSTEM
&& m_name
)
3216 // use "false" to suppress interactive dialogs -- we can be called from
3217 // anywhere and popping up a dialog from here is the last thing we want to
3219 enc
= wxFontMapperBase::Get()->CharsetToEncoding(m_name
, false);
3221 #endif // wxUSE_FONTMAP
3225 case wxFONTENCODING_UTF7
:
3226 return new wxMBConvUTF7
;
3228 case wxFONTENCODING_UTF8
:
3229 return new wxMBConvUTF8
;
3231 case wxFONTENCODING_UTF16BE
:
3232 return new wxMBConvUTF16BE
;
3234 case wxFONTENCODING_UTF16LE
:
3235 return new wxMBConvUTF16LE
;
3237 case wxFONTENCODING_UTF32BE
:
3238 return new wxMBConvUTF32BE
;
3240 case wxFONTENCODING_UTF32LE
:
3241 return new wxMBConvUTF32LE
;
3244 // nothing to do but put here to suppress gcc warnings
3251 wxMBConv_wxwin
*conv
= m_name
? new wxMBConv_wxwin(m_name
)
3252 : new wxMBConv_wxwin(m_encoding
);
3259 wxLogTrace(TRACE_STRCONV
,
3260 wxT("encoding \"%s\" is not supported by this system"),
3261 (m_name
? wxString(m_name
)
3262 : wxFontMapperBase::GetEncodingName(m_encoding
)));
3263 #endif // wxUSE_FONTMAP
3268 bool wxCSConv::IsOk() const
3270 // special case: no convReal created for wxFONTENCODING_ISO8859_1
3271 if ( m_encoding
== wxFONTENCODING_ISO8859_1
)
3272 return true; // always ok as we do it ourselves
3274 // m_convReal->IsOk() is called at its own creation, so we know it must
3275 // be ok if m_convReal is non-NULL
3276 return m_convReal
!= NULL
;
3279 size_t wxCSConv::ToWChar(wchar_t *dst
, size_t dstLen
,
3280 const char *src
, size_t srcLen
) const
3283 return m_convReal
->ToWChar(dst
, dstLen
, src
, srcLen
);
3286 if ( srcLen
== wxNO_LEN
)
3287 srcLen
= strlen(src
) + 1; // take trailing NUL too
3291 if ( dstLen
< srcLen
)
3292 return wxCONV_FAILED
;
3294 for ( size_t n
= 0; n
< srcLen
; n
++ )
3295 dst
[n
] = (unsigned char)(src
[n
]);
3301 size_t wxCSConv::FromWChar(char *dst
, size_t dstLen
,
3302 const wchar_t *src
, size_t srcLen
) const
3305 return m_convReal
->FromWChar(dst
, dstLen
, src
, srcLen
);
3308 if ( srcLen
== wxNO_LEN
)
3309 srcLen
= wxWcslen(src
) + 1;
3313 if ( dstLen
< srcLen
)
3314 return wxCONV_FAILED
;
3316 for ( size_t n
= 0; n
< srcLen
; n
++ )
3318 if ( src
[n
] > 0xFF )
3319 return wxCONV_FAILED
;
3321 dst
[n
] = (char)src
[n
];
3325 else // still need to check the input validity
3327 for ( size_t n
= 0; n
< srcLen
; n
++ )
3329 if ( src
[n
] > 0xFF )
3330 return wxCONV_FAILED
;
3337 size_t wxCSConv::GetMBNulLen() const
3340 return m_convReal
->GetMBNulLen();
3342 // otherwise, we are ISO-8859-1
3346 #if wxUSE_UNICODE_UTF8
3347 bool wxCSConv::IsUTF8() const
3350 return m_convReal
->IsUTF8();
3352 // otherwise, we are ISO-8859-1
3360 wxWCharBuffer
wxSafeConvertMB2WX(const char *s
)
3363 return wxWCharBuffer();
3365 wxWCharBuffer
wbuf(wxConvLibc
.cMB2WX(s
));
3367 wbuf
= wxMBConvUTF8().cMB2WX(s
);
3369 wbuf
= wxConvISO8859_1
.cMB2WX(s
);
3374 wxCharBuffer
wxSafeConvertWX2MB(const wchar_t *ws
)
3377 return wxCharBuffer();
3379 wxCharBuffer
buf(wxConvLibc
.cWX2MB(ws
));
3381 buf
= wxMBConvUTF8(wxMBConvUTF8::MAP_INVALID_UTF8_TO_OCTAL
).cWX2MB(ws
);
3386 #endif // wxUSE_UNICODE
3388 // ----------------------------------------------------------------------------
3390 // ----------------------------------------------------------------------------
3392 // NB: The reason why we create converted objects in this convoluted way,
3393 // using a factory function instead of global variable, is that they
3394 // may be used at static initialization time (some of them are used by
3395 // wxString ctors and there may be a global wxString object). In other
3396 // words, possibly _before_ the converter global object would be
3403 #undef wxConvISO8859_1
3405 #define WX_DEFINE_GLOBAL_CONV2(klass, impl_klass, name, ctor_args) \
3406 WXDLLIMPEXP_DATA_BASE(klass*) name##Ptr = NULL; \
3407 WXDLLIMPEXP_BASE klass* wxGet_##name##Ptr() \
3409 static impl_klass name##Obj ctor_args; \
3410 return &name##Obj; \
3412 /* this ensures that all global converter objects are created */ \
3413 /* by the time static initialization is done, i.e. before any */ \
3414 /* thread is launched: */ \
3415 static klass* gs_##name##instance = wxGet_##name##Ptr()
3417 #define WX_DEFINE_GLOBAL_CONV(klass, name, ctor_args) \
3418 WX_DEFINE_GLOBAL_CONV2(klass, klass, name, ctor_args)
3421 // disable warning "variable 'xxx' was declared but never referenced"
3422 #pragma warning(disable: 177)
3426 WX_DEFINE_GLOBAL_CONV2(wxMBConv
, wxMBConv_win32
, wxConvLibc
, wxEMPTY_PARAMETER_VALUE
);
3427 #elif 0 // defined(__WXOSX__)
3428 WX_DEFINE_GLOBAL_CONV2(wxMBConv
, wxMBConv_cf
, wxConvLibc
, (wxFONTENCODING_UTF8
));
3430 WX_DEFINE_GLOBAL_CONV2(wxMBConv
, wxMBConvLibc
, wxConvLibc
, wxEMPTY_PARAMETER_VALUE
);
3433 // NB: we can't use wxEMPTY_PARAMETER_VALUE as final argument here because it's
3434 // passed to WX_DEFINE_GLOBAL_CONV2 after a macro expansion and so still
3435 // provokes an error message about "not enough macro parameters"; and we
3436 // can't use "()" here as the name##Obj declaration would be parsed as a
3437 // function declaration then, so use a semicolon and live with an extra
3438 // empty statement (and hope that no compilers warns about this)
3439 WX_DEFINE_GLOBAL_CONV(wxMBConvStrictUTF8
, wxConvUTF8
, ;);
3440 WX_DEFINE_GLOBAL_CONV(wxMBConvUTF7
, wxConvUTF7
, ;);
3442 WX_DEFINE_GLOBAL_CONV(wxCSConv
, wxConvLocal
, (wxFONTENCODING_SYSTEM
));
3443 WX_DEFINE_GLOBAL_CONV(wxCSConv
, wxConvISO8859_1
, (wxFONTENCODING_ISO8859_1
));
3445 WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvCurrent
= wxGet_wxConvLibcPtr();
3446 WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvUI
= wxGet_wxConvLocalPtr();
3449 // It is important to use this conversion object under Darwin as it ensures
3450 // that Unicode strings are (re)composed correctly even though xnu kernel uses
3451 // decomposed form internally (at least for the file names).
3452 static wxMBConv_cf
wxConvMacUTF8DObj(wxFONTENCODING_UTF8
);
3455 WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvFileName
=
3458 #else // !__DARWIN__
3459 wxGet_wxConvLibcPtr();
3460 #endif // __DARWIN__/!__DARWIN__