+ // when working on the entire strings we don't update nor use the shift
+ // state from the previous call
+ statePtr = &stateOrig;
+ }
+ else // when working with partial strings we do use the shift state
+ {
+ statePtr = const_cast<DecoderState *>(&m_stateDecoder);
+
+ // also save the old state to be able to rollback to it on error
+ stateOrig = m_stateDecoder;
+ }
+
+ // but to simplify the code below we use this variable in both cases
+ DecoderState& state = *statePtr;
+
+
+ // number of characters [which would have been] written to dst [if it were
+ // not NULL]
+ size_t len = 0;
+
+ const char * const srcEnd = src + srcLen;
+
+ while ( (src < srcEnd) && (!dst || (len < dstLen)) )
+ {
+ const unsigned char cc = *src++;
+
+ if ( state.IsShifted() )
+ {
+ const unsigned char dc = utf7unb64[cc];
+ if ( dc == 0xff )
+ {
+ // end of encoded part, check that nothing was left: there can
+ // be up to 4 bits of 0 padding but nothing else (we also need
+ // to check isLSB as we count bits modulo 8 while a valid UTF-7
+ // encoded sequence must contain an integral number of UTF-16
+ // characters)
+ if ( state.isLSB || state.bit > 4 ||
+ (state.accum & ((1 << state.bit) - 1)) )
+ {
+ if ( !len )
+ state = stateOrig;
+
+ return wxCONV_FAILED;
+ }
+
+ state.ToDirect();
+
+ // re-parse this character normally below unless it's '-' which
+ // is consumed by the decoder
+ if ( cc == '-' )
+ continue;
+ }
+ else // valid encoded character
+ {
+ // mini base64 decoder: each character is 6 bits
+ state.bit += 6;
+ state.accum <<= 6;
+ state.accum += dc;
+
+ if ( state.bit >= 8 )
+ {
+ // got the full byte, consume it
+ state.bit -= 8;
+ unsigned char b = (state.accum >> state.bit) & 0x00ff;
+
+ if ( state.isLSB )
+ {
+ // we've got the full word, output it
+ if ( dst )
+ *dst++ = (state.msb << 8) | b;
+ len++;
+ state.isLSB = false;
+ }
+ else // MSB
+ {
+ // just store it while we wait for LSB
+ state.msb = b;
+ state.isLSB = true;
+ }
+ }
+ }
+ }
+
+ if ( state.IsDirect() )
+ {
+ // start of an encoded segment?
+ if ( cc == '+' )
+ {
+ if ( *src == '-' )
+ {
+ // just the encoded plus sign, don't switch to shifted mode
+ if ( dst )
+ *dst++ = '+';
+ len++;
+ src++;
+ }
+ else if ( utf7unb64[(unsigned)*src] == 0xff )
+ {
+ // empty encoded chunks are not allowed
+ if ( !len )
+ state = stateOrig;
+
+ return wxCONV_FAILED;
+ }
+ else // base-64 encoded chunk follows
+ {
+ state.ToShifted();
+ }
+ }
+ else // not '+'
+ {
+ // only printable 7 bit ASCII characters (with the exception of
+ // NUL, TAB, CR and LF) can be used directly
+ if ( cc >= 0x7f || (cc < ' ' &&
+ !(cc == '\0' || cc == '\t' || cc == '\r' || cc == '\n')) )
+ return wxCONV_FAILED;
+
+ if ( dst )
+ *dst++ = cc;
+ len++;
+ }
+ }
+ }
+
+ if ( !len )
+ {
+ // as we didn't read any characters we should be called with the same
+ // data (followed by some more new data) again later so don't save our
+ // state
+ state = stateOrig;
+
+ return wxCONV_FAILED;
+ }
+
+ return len;
+}
+
+//
+// BASE64 encoding table
+//
+static const unsigned char utf7enb64[] =
+{
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+ 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+ 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+ 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+ 'w', 'x', 'y', 'z', '0', '1', '2', '3',
+ '4', '5', '6', '7', '8', '9', '+', '/'
+};
+
+//
+// UTF-7 encoding table
+//
+// 0 - Set D (directly encoded characters)
+// 1 - Set O (optional direct characters)
+// 2 - whitespace characters (optional)
+// 3 - special characters
+//
+static const unsigned char utf7encode[128] =
+{
+ 0, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3,
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+ 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 3,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
+ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1,
+ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3
+};
+
+static inline bool wxIsUTF7Direct(wchar_t wc)
+{
+ return wc < 0x80 && utf7encode[wc] < 1;
+}
+
+size_t wxMBConvUTF7::FromWChar(char *dst, size_t dstLen,
+ const wchar_t *src, size_t srcLen) const
+{
+ EncoderState stateOrig,
+ *statePtr;
+ if ( srcLen == wxNO_LEN )
+ {
+ // we don't apply the stored state when operating on entire strings at
+ // once
+ statePtr = &stateOrig;
+
+ srcLen = wxWcslen(src) + 1;
+ }
+ else // do use the mode we left the output in previously
+ {
+ stateOrig = m_stateEncoder;
+ statePtr = const_cast<EncoderState *>(&m_stateEncoder);
+ }
+
+ EncoderState& state = *statePtr;
+
+
+ size_t len = 0;
+
+ const wchar_t * const srcEnd = src + srcLen;
+ while ( src < srcEnd && (!dst || len < dstLen) )
+ {
+ wchar_t cc = *src++;
+ if ( wxIsUTF7Direct(cc) )
+ {
+ if ( state.IsShifted() )
+ {
+ // pad with zeros the last encoded block if necessary
+ if ( state.bit )
+ {
+ if ( dst )
+ *dst++ = utf7enb64[((state.accum % 16) << (6 - state.bit)) % 64];
+ len++;
+ }
+
+ state.ToDirect();
+
+ if ( dst )
+ *dst++ = '-';
+ len++;
+ }
+
+ if ( dst )
+ *dst++ = (char)cc;
+ len++;
+ }
+ else if ( cc == '+' && state.IsDirect() )
+ {
+ if ( dst )
+ {
+ *dst++ = '+';
+ *dst++ = '-';
+ }
+
+ len += 2;
+ }
+#ifndef WC_UTF16
+ else if (((wxUint32)cc) > 0xffff)
+ {
+ // no surrogate pair generation (yet?)
+ return wxCONV_FAILED;
+ }
+#endif
+ else
+ {
+ if ( state.IsDirect() )
+ {
+ state.ToShifted();
+
+ if ( dst )
+ *dst++ = '+';
+ len++;
+ }
+
+ // BASE64 encode string
+ for ( ;; )
+ {
+ for ( unsigned lsb = 0; lsb < 2; lsb++ )
+ {
+ state.accum <<= 8;
+ state.accum += lsb ? cc & 0xff : (cc & 0xff00) >> 8;
+
+ for (state.bit += 8; state.bit >= 6; )
+ {
+ state.bit -= 6;
+ if ( dst )
+ *dst++ = utf7enb64[(state.accum >> state.bit) % 64];
+ len++;
+ }
+ }
+
+ if ( src == srcEnd || wxIsUTF7Direct(cc = *src) )
+ break;
+
+ src++;
+ }
+ }
+ }
+
+ // we need to restore the original encoder state if we were called just to
+ // calculate the amount of space needed as we will presumably be called
+ // again to really convert the data now
+ if ( !dst )
+ state = stateOrig;
+
+ return len;
+}
+
+// ----------------------------------------------------------------------------
+// UTF-8
+// ----------------------------------------------------------------------------
+
+static const wxUint32 utf8_max[]=
+ { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
+
+// boundaries of the private use area we use to (temporarily) remap invalid
+// characters invalid in a UTF-8 encoded string
+const wxUint32 wxUnicodePUA = 0x100000;
+const wxUint32 wxUnicodePUAEnd = wxUnicodePUA + 256;
+
+// this table gives the length of the UTF-8 encoding from its first character:
+const unsigned char tableUtf8Lengths[256] = {
+ // single-byte sequences (ASCII):
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 00..0F
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 10..1F
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 20..2F
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 30..3F
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 40..4F
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 50..5F
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 60..6F
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 70..7F
+
+ // these are invalid:
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80..8F
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90..9F
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A0..AF
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // B0..BF
+ 0, 0, // C0,C1
+
+ // two-byte sequences:
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // C2..CF
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // D0..DF
+
+ // three-byte sequences:
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // E0..EF
+
+ // four-byte sequences:
+ 4, 4, 4, 4, 4, // F0..F4
+
+ // these are invalid again (5- or 6-byte
+ // sequences and sequences for code points
+ // above U+10FFFF, as restricted by RFC 3629):
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F5..FF
+};
+
+size_t
+wxMBConvStrictUTF8::ToWChar(wchar_t *dst, size_t dstLen,
+ const char *src, size_t srcLen) const
+{
+ wchar_t *out = dstLen ? dst : NULL;
+ size_t written = 0;
+
+ if ( srcLen == wxNO_LEN )
+ srcLen = strlen(src) + 1;
+
+ for ( const char *p = src; ; p++ )
+ {
+ if ( !(srcLen == wxNO_LEN ? *p : srcLen) )
+ {
+ // all done successfully, just add the trailing NULL if we are not
+ // using explicit length
+ if ( srcLen == wxNO_LEN )
+ {
+ if ( out )
+ {
+ if ( !dstLen )
+ break;
+
+ *out = L'\0';
+ }
+
+ written++;
+ }
+
+ return written;
+ }
+
+ if ( out && !dstLen-- )
+ break;
+
+ wxUint32 code;
+ unsigned char c = *p;
+
+ if ( c < 0x80 )
+ {
+ if ( srcLen == 0 ) // the test works for wxNO_LEN too
+ break;
+
+ if ( srcLen != wxNO_LEN )
+ srcLen--;
+
+ code = c;
+ }
+ else
+ {
+ unsigned len = tableUtf8Lengths[c];
+ if ( !len )
+ break;
+
+ if ( srcLen < len ) // the test works for wxNO_LEN too
+ break;
+
+ if ( srcLen != wxNO_LEN )
+ srcLen -= len;
+
+ // Char. number range | UTF-8 octet sequence
+ // (hexadecimal) | (binary)
+ // ----------------------+----------------------------------------
+ // 0000 0000 - 0000 007F | 0xxxxxxx
+ // 0000 0080 - 0000 07FF | 110xxxxx 10xxxxxx
+ // 0000 0800 - 0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
+ // 0001 0000 - 0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
+ //
+ // Code point value is stored in bits marked with 'x',
+ // lowest-order bit of the value on the right side in the diagram
+ // above. (from RFC 3629)
+
+ // mask to extract lead byte's value ('x' bits above), by sequence
+ // length:
+ static const unsigned char leadValueMask[] = { 0x7F, 0x1F, 0x0F, 0x07 };
+
+ // mask and value of lead byte's most significant bits, by length:
+ static const unsigned char leadMarkerMask[] = { 0x80, 0xE0, 0xF0, 0xF8 };
+ static const unsigned char leadMarkerVal[] = { 0x00, 0xC0, 0xE0, 0xF0 };
+
+ len--; // it's more convenient to work with 0-based length here
+
+ // extract the lead byte's value bits:
+ if ( (c & leadMarkerMask[len]) != leadMarkerVal[len] )
+ break;
+
+ code = c & leadValueMask[len];
+
+ // all remaining bytes, if any, are handled in the same way
+ // regardless of sequence's length:
+ for ( ; len; --len )
+ {
+ c = *++p;
+ if ( (c & 0xC0) != 0x80 )
+ return wxCONV_FAILED;
+
+ code <<= 6;
+ code |= c & 0x3F;
+ }
+ }
+
+#ifdef WC_UTF16
+ // cast is ok because wchar_t == wxUint16 if WC_UTF16
+ if ( encode_utf16(code, (wxUint16 *)out) == 2 )
+ {
+ if ( out )
+ out++;
+ written++;
+ }
+#else // !WC_UTF16
+ if ( out )
+ *out = code;
+#endif // WC_UTF16/!WC_UTF16
+
+ if ( out )
+ out++;
+
+ written++;
+ }
+
+ return wxCONV_FAILED;
+}
+
+size_t
+wxMBConvStrictUTF8::FromWChar(char *dst, size_t dstLen,
+ const wchar_t *src, size_t srcLen) const
+{
+ char *out = dstLen ? dst : NULL;
+ size_t written = 0;
+
+ for ( const wchar_t *wp = src; ; wp++ )
+ {
+ if ( !(srcLen == wxNO_LEN ? *wp : srcLen) )
+ {
+ // all done successfully, just add the trailing NULL if we are not
+ // using explicit length
+ if ( srcLen == wxNO_LEN )
+ {
+ if ( out )
+ {
+ if ( !dstLen )
+ break;
+
+ *out = '\0';
+ }
+
+ written++;
+ }
+
+ return written;
+ }
+
+ if ( srcLen != wxNO_LEN )
+ srcLen--;
+
+ wxUint32 code;
+#ifdef WC_UTF16
+ // cast is ok for WC_UTF16
+ if ( decode_utf16((const wxUint16 *)wp, code) == 2 )
+ {
+ // skip the next char too as we decoded a surrogate
+ wp++;
+ }
+#else // wchar_t is UTF-32
+ code = *wp & 0x7fffffff;
+#endif
+
+ unsigned len;
+ if ( code <= 0x7F )
+ {
+ len = 1;
+ if ( out )
+ {
+ if ( dstLen < len )
+ break;
+
+ out[0] = (char)code;
+ }
+ }
+ else if ( code <= 0x07FF )
+ {
+ len = 2;
+ if ( out )
+ {
+ if ( dstLen < len )
+ break;
+
+ // NB: this line takes 6 least significant bits, encodes them as
+ // 10xxxxxx and discards them so that the next byte can be encoded:
+ out[1] = 0x80 | (code & 0x3F); code >>= 6;
+ out[0] = 0xC0 | code;
+ }
+ }
+ else if ( code < 0xFFFF )
+ {
+ len = 3;
+ if ( out )
+ {
+ if ( dstLen < len )
+ break;
+
+ out[2] = 0x80 | (code & 0x3F); code >>= 6;
+ out[1] = 0x80 | (code & 0x3F); code >>= 6;
+ out[0] = 0xE0 | code;
+ }
+ }
+ else if ( code <= 0x10FFFF )
+ {
+ len = 4;
+ if ( out )
+ {
+ if ( dstLen < len )
+ break;
+
+ out[3] = 0x80 | (code & 0x3F); code >>= 6;
+ out[2] = 0x80 | (code & 0x3F); code >>= 6;
+ out[1] = 0x80 | (code & 0x3F); code >>= 6;
+ out[0] = 0xF0 | code;
+ }
+ }
+ else
+ {
+ wxFAIL_MSG( wxT("trying to encode undefined Unicode character") );
+ break;
+ }
+
+ if ( out )
+ {
+ out += len;
+ dstLen -= len;
+ }
+
+ written += len;
+ }
+
+ // we only get here if an error occurs during decoding
+ return wxCONV_FAILED;
+}
+
+size_t wxMBConvUTF8::ToWChar(wchar_t *buf, size_t n,
+ const char *psz, size_t srcLen) const
+{
+ if ( m_options == MAP_INVALID_UTF8_NOT )
+ return wxMBConvStrictUTF8::ToWChar(buf, n, psz, srcLen);
+
+ size_t len = 0;
+
+ while ((srcLen == wxNO_LEN ? *psz : srcLen--) && ((!buf) || (len < n)))
+ {
+ const char *opsz = psz;
+ bool invalid = false;
+ unsigned char cc = *psz++, fc = cc;
+ unsigned cnt;
+ for (cnt = 0; fc & 0x80; cnt++)
+ fc <<= 1;
+
+ if (!cnt)
+ {
+ // plain ASCII char
+ if (buf)
+ *buf++ = cc;
+ len++;
+
+ // escape the escape character for octal escapes
+ if ((m_options & MAP_INVALID_UTF8_TO_OCTAL)
+ && cc == '\\' && (!buf || len < n))
+ {
+ if (buf)
+ *buf++ = cc;
+ len++;
+ }
+ }
+ else
+ {
+ cnt--;
+ if (!cnt)
+ {
+ // invalid UTF-8 sequence
+ invalid = true;
+ }
+ else
+ {
+ unsigned ocnt = cnt - 1;
+ wxUint32 res = cc & (0x3f >> cnt);
+ while (cnt--)
+ {
+ cc = *psz;
+ if ((cc & 0xC0) != 0x80)
+ {
+ // invalid UTF-8 sequence
+ invalid = true;
+ break;
+ }
+
+ psz++;
+ res = (res << 6) | (cc & 0x3f);
+ }
+
+ if (invalid || res <= utf8_max[ocnt])
+ {
+ // illegal UTF-8 encoding
+ invalid = true;
+ }
+ else if ((m_options & MAP_INVALID_UTF8_TO_PUA) &&
+ res >= wxUnicodePUA && res < wxUnicodePUAEnd)
+ {
+ // if one of our PUA characters turns up externally
+ // it must also be treated as an illegal sequence
+ // (a bit like you have to escape an escape character)
+ invalid = true;
+ }
+ else
+ {
+#ifdef WC_UTF16
+ // cast is ok because wchar_t == wxUint16 if WC_UTF16
+ size_t pa = encode_utf16(res, (wxUint16 *)buf);
+ if (pa == wxCONV_FAILED)
+ {
+ invalid = true;
+ }
+ else
+ {
+ if (buf)
+ buf += pa;
+ len += pa;
+ }
+#else // !WC_UTF16
+ if (buf)
+ *buf++ = (wchar_t)res;
+ len++;
+#endif // WC_UTF16/!WC_UTF16
+ }
+ }
+
+ if (invalid)
+ {
+ if (m_options & MAP_INVALID_UTF8_TO_PUA)
+ {
+ while (opsz < psz && (!buf || len < n))
+ {
+#ifdef WC_UTF16
+ // cast is ok because wchar_t == wxUuint16 if WC_UTF16
+ size_t pa = encode_utf16((unsigned char)*opsz + wxUnicodePUA, (wxUint16 *)buf);
+ wxASSERT(pa != wxCONV_FAILED);
+ if (buf)
+ buf += pa;
+ opsz++;
+ len += pa;
+#else
+ if (buf)
+ *buf++ = (wchar_t)(wxUnicodePUA + (unsigned char)*opsz);
+ opsz++;
+ len++;
+#endif
+ }
+ }
+ else if (m_options & MAP_INVALID_UTF8_TO_OCTAL)
+ {
+ while (opsz < psz && (!buf || len < n))
+ {
+ if ( buf && len + 3 < n )
+ {
+ unsigned char on = *opsz;
+ *buf++ = L'\\';
+ *buf++ = (wchar_t)( L'0' + on / 0100 );
+ *buf++ = (wchar_t)( L'0' + (on % 0100) / 010 );
+ *buf++ = (wchar_t)( L'0' + on % 010 );
+ }
+
+ opsz++;
+ len += 4;
+ }
+ }
+ else // MAP_INVALID_UTF8_NOT
+ {
+ return wxCONV_FAILED;
+ }
+ }
+ }
+ }
+
+ if (srcLen == wxNO_LEN && buf && (len < n))
+ *buf = 0;
+
+ return len + 1;
+}
+
+static inline bool isoctal(wchar_t wch)
+{
+ return L'0' <= wch && wch <= L'7';
+}
+
+size_t wxMBConvUTF8::FromWChar(char *buf, size_t n,
+ const wchar_t *psz, size_t srcLen) const
+{
+ if ( m_options == MAP_INVALID_UTF8_NOT )
+ return wxMBConvStrictUTF8::FromWChar(buf, n, psz, srcLen);
+
+ size_t len = 0;
+
+ while ((srcLen == wxNO_LEN ? *psz : srcLen--) && ((!buf) || (len < n)))
+ {
+ wxUint32 cc;
+
+#ifdef WC_UTF16
+ // cast is ok for WC_UTF16
+ size_t pa = decode_utf16((const wxUint16 *)psz, cc);
+ psz += (pa == wxCONV_FAILED) ? 1 : pa;
+#else
+ cc = (*psz++) & 0x7fffffff;
+#endif
+
+ if ( (m_options & MAP_INVALID_UTF8_TO_PUA)
+ && cc >= wxUnicodePUA && cc < wxUnicodePUAEnd )
+ {
+ if (buf)
+ *buf++ = (char)(cc - wxUnicodePUA);
+ len++;
+ }
+ else if ( (m_options & MAP_INVALID_UTF8_TO_OCTAL)
+ && cc == L'\\' && psz[0] == L'\\' )
+ {
+ if (buf)
+ *buf++ = (char)cc;
+ psz++;
+ len++;
+ }
+ else if ( (m_options & MAP_INVALID_UTF8_TO_OCTAL) &&
+ cc == L'\\' &&
+ isoctal(psz[0]) && isoctal(psz[1]) && isoctal(psz[2]) )
+ {
+ if (buf)
+ {
+ *buf++ = (char) ((psz[0] - L'0') * 0100 +
+ (psz[1] - L'0') * 010 +
+ (psz[2] - L'0'));
+ }
+
+ psz += 3;
+ len++;
+ }
+ else
+ {
+ unsigned cnt;
+ for (cnt = 0; cc > utf8_max[cnt]; cnt++)
+ {
+ }
+
+ if (!cnt)
+ {
+ // plain ASCII char
+ if (buf)
+ *buf++ = (char) cc;
+ len++;
+ }
+ else
+ {
+ len += cnt + 1;
+ if (buf)
+ {
+ *buf++ = (char) ((-128 >> cnt) | ((cc >> (cnt * 6)) & (0x3f >> cnt)));
+ while (cnt--)
+ *buf++ = (char) (0x80 | ((cc >> (cnt * 6)) & 0x3f));
+ }
+ }
+ }
+ }
+
+ if (srcLen == wxNO_LEN && buf && (len < n))
+ *buf = 0;
+
+ return len + 1;
+}
+
+// ============================================================================
+// UTF-16
+// ============================================================================
+
+#ifdef WORDS_BIGENDIAN
+ #define wxMBConvUTF16straight wxMBConvUTF16BE
+ #define wxMBConvUTF16swap wxMBConvUTF16LE
+#else
+ #define wxMBConvUTF16swap wxMBConvUTF16BE
+ #define wxMBConvUTF16straight wxMBConvUTF16LE
+#endif
+
+/* static */
+size_t wxMBConvUTF16Base::GetLength(const char *src, size_t srcLen)
+{
+ if ( srcLen == wxNO_LEN )
+ {
+ // count the number of bytes in input, including the trailing NULs
+ const wxUint16 *inBuff = reinterpret_cast<const wxUint16 *>(src);
+ for ( srcLen = 1; *inBuff++; srcLen++ )
+ ;
+
+ srcLen *= BYTES_PER_CHAR;
+ }
+ else // we already have the length
+ {
+ // we can only convert an entire number of UTF-16 characters
+ if ( srcLen % BYTES_PER_CHAR )
+ return wxCONV_FAILED;
+ }
+
+ return srcLen;
+}
+
+// case when in-memory representation is UTF-16 too
+#ifdef WC_UTF16
+
+// ----------------------------------------------------------------------------
+// conversions without endianness change
+// ----------------------------------------------------------------------------
+
+size_t
+wxMBConvUTF16straight::ToWChar(wchar_t *dst, size_t dstLen,
+ const char *src, size_t srcLen) const
+{
+ // set up the scene for using memcpy() (which is presumably more efficient
+ // than copying the bytes one by one)
+ srcLen = GetLength(src, srcLen);
+ if ( srcLen == wxNO_LEN )
+ return wxCONV_FAILED;
+
+ const size_t inLen = srcLen / BYTES_PER_CHAR;
+ if ( dst )
+ {
+ if ( dstLen < inLen )
+ return wxCONV_FAILED;
+
+ memcpy(dst, src, srcLen);
+ }
+
+ return inLen;
+}
+
+size_t
+wxMBConvUTF16straight::FromWChar(char *dst, size_t dstLen,
+ const wchar_t *src, size_t srcLen) const
+{
+ if ( srcLen == wxNO_LEN )
+ srcLen = wxWcslen(src) + 1;
+
+ srcLen *= BYTES_PER_CHAR;
+
+ if ( dst )
+ {
+ if ( dstLen < srcLen )
+ return wxCONV_FAILED;
+
+ memcpy(dst, src, srcLen);
+ }
+
+ return srcLen;
+}
+
+// ----------------------------------------------------------------------------
+// endian-reversing conversions
+// ----------------------------------------------------------------------------
+
+size_t
+wxMBConvUTF16swap::ToWChar(wchar_t *dst, size_t dstLen,
+ const char *src, size_t srcLen) const
+{
+ srcLen = GetLength(src, srcLen);
+ if ( srcLen == wxNO_LEN )
+ return wxCONV_FAILED;
+
+ srcLen /= BYTES_PER_CHAR;
+
+ if ( dst )
+ {
+ if ( dstLen < srcLen )
+ return wxCONV_FAILED;
+
+ const wxUint16 *inBuff = reinterpret_cast<const wxUint16 *>(src);
+ for ( size_t n = 0; n < srcLen; n++, inBuff++ )
+ {
+ *dst++ = wxUINT16_SWAP_ALWAYS(*inBuff);
+ }
+ }
+
+ return srcLen;
+}
+
+size_t
+wxMBConvUTF16swap::FromWChar(char *dst, size_t dstLen,
+ const wchar_t *src, size_t srcLen) const
+{
+ if ( srcLen == wxNO_LEN )
+ srcLen = wxWcslen(src) + 1;
+
+ srcLen *= BYTES_PER_CHAR;
+
+ if ( dst )
+ {
+ if ( dstLen < srcLen )
+ return wxCONV_FAILED;
+
+ wxUint16 *outBuff = reinterpret_cast<wxUint16 *>(dst);
+ for ( size_t n = 0; n < srcLen; n += BYTES_PER_CHAR, src++ )
+ {
+ *outBuff++ = wxUINT16_SWAP_ALWAYS(*src);
+ }
+ }
+
+ return srcLen;
+}
+
+#else // !WC_UTF16: wchar_t is UTF-32
+
+// ----------------------------------------------------------------------------
+// conversions without endianness change
+// ----------------------------------------------------------------------------
+
+size_t
+wxMBConvUTF16straight::ToWChar(wchar_t *dst, size_t dstLen,
+ const char *src, size_t srcLen) const
+{
+ srcLen = GetLength(src, srcLen);
+ if ( srcLen == wxNO_LEN )
+ return wxCONV_FAILED;
+
+ const size_t inLen = srcLen / BYTES_PER_CHAR;
+ if ( !dst )
+ {
+ // optimization: return maximal space which could be needed for this
+ // string even if the real size could be smaller if the buffer contains
+ // any surrogates
+ return inLen;
+ }
+
+ size_t outLen = 0;
+ const wxUint16 *inBuff = reinterpret_cast<const wxUint16 *>(src);
+ for ( const wxUint16 * const inEnd = inBuff + inLen; inBuff < inEnd; )
+ {
+ const wxUint32 ch = wxDecodeSurrogate(&inBuff);
+ if ( !inBuff )
+ return wxCONV_FAILED;
+
+ if ( ++outLen > dstLen )
+ return wxCONV_FAILED;
+
+ *dst++ = ch;
+ }
+
+
+ return outLen;
+}
+
+size_t
+wxMBConvUTF16straight::FromWChar(char *dst, size_t dstLen,
+ const wchar_t *src, size_t srcLen) const
+{
+ if ( srcLen == wxNO_LEN )
+ srcLen = wxWcslen(src) + 1;
+
+ size_t outLen = 0;
+ wxUint16 *outBuff = reinterpret_cast<wxUint16 *>(dst);
+ for ( size_t n = 0; n < srcLen; n++ )
+ {
+ wxUint16 cc[2];
+ const size_t numChars = encode_utf16(*src++, cc);
+ if ( numChars == wxCONV_FAILED )
+ return wxCONV_FAILED;
+
+ outLen += numChars * BYTES_PER_CHAR;
+ if ( outBuff )
+ {
+ if ( outLen > dstLen )
+ return wxCONV_FAILED;
+
+ *outBuff++ = cc[0];
+ if ( numChars == 2 )
+ {
+ // second character of a surrogate
+ *outBuff++ = cc[1];
+ }
+ }
+ }
+
+ return outLen;
+}
+
+// ----------------------------------------------------------------------------
+// endian-reversing conversions
+// ----------------------------------------------------------------------------
+
+size_t
+wxMBConvUTF16swap::ToWChar(wchar_t *dst, size_t dstLen,
+ const char *src, size_t srcLen) const
+{
+ srcLen = GetLength(src, srcLen);
+ if ( srcLen == wxNO_LEN )
+ return wxCONV_FAILED;
+
+ const size_t inLen = srcLen / BYTES_PER_CHAR;
+ if ( !dst )
+ {
+ // optimization: return maximal space which could be needed for this
+ // string even if the real size could be smaller if the buffer contains
+ // any surrogates
+ return inLen;
+ }
+
+ size_t outLen = 0;
+ const wxUint16 *inBuff = reinterpret_cast<const wxUint16 *>(src);
+ for ( const wxUint16 * const inEnd = inBuff + inLen; inBuff < inEnd; )
+ {
+ wxUint32 ch;
+ wxUint16 tmp[2];
+
+ tmp[0] = wxUINT16_SWAP_ALWAYS(*inBuff);
+ inBuff++;
+ tmp[1] = wxUINT16_SWAP_ALWAYS(*inBuff);
+
+ const size_t numChars = decode_utf16(tmp, ch);
+ if ( numChars == wxCONV_FAILED )
+ return wxCONV_FAILED;
+
+ if ( numChars == 2 )
+ inBuff++;
+
+ if ( ++outLen > dstLen )
+ return wxCONV_FAILED;
+
+ *dst++ = ch;
+ }
+
+
+ return outLen;
+}
+
+size_t
+wxMBConvUTF16swap::FromWChar(char *dst, size_t dstLen,
+ const wchar_t *src, size_t srcLen) const
+{
+ if ( srcLen == wxNO_LEN )
+ srcLen = wxWcslen(src) + 1;
+
+ size_t outLen = 0;
+ wxUint16 *outBuff = reinterpret_cast<wxUint16 *>(dst);
+ for ( const wchar_t *srcEnd = src + srcLen; src < srcEnd; src++ )
+ {
+ wxUint16 cc[2];
+ const size_t numChars = encode_utf16(*src, cc);
+ if ( numChars == wxCONV_FAILED )
+ return wxCONV_FAILED;
+
+ outLen += numChars * BYTES_PER_CHAR;
+ if ( outBuff )
+ {
+ if ( outLen > dstLen )
+ return wxCONV_FAILED;
+
+ *outBuff++ = wxUINT16_SWAP_ALWAYS(cc[0]);
+ if ( numChars == 2 )
+ {
+ // second character of a surrogate
+ *outBuff++ = wxUINT16_SWAP_ALWAYS(cc[1]);
+ }
+ }
+ }
+
+ return outLen;
+}
+
+#endif // WC_UTF16/!WC_UTF16
+
+
+// ============================================================================
+// UTF-32
+// ============================================================================
+
+#ifdef WORDS_BIGENDIAN
+ #define wxMBConvUTF32straight wxMBConvUTF32BE
+ #define wxMBConvUTF32swap wxMBConvUTF32LE
+#else
+ #define wxMBConvUTF32swap wxMBConvUTF32BE
+ #define wxMBConvUTF32straight wxMBConvUTF32LE
+#endif
+
+
+WXDLLIMPEXP_DATA_BASE(wxMBConvUTF32LE) wxConvUTF32LE;
+WXDLLIMPEXP_DATA_BASE(wxMBConvUTF32BE) wxConvUTF32BE;
+
+/* static */
+size_t wxMBConvUTF32Base::GetLength(const char *src, size_t srcLen)
+{
+ if ( srcLen == wxNO_LEN )
+ {
+ // count the number of bytes in input, including the trailing NULs
+ const wxUint32 *inBuff = reinterpret_cast<const wxUint32 *>(src);
+ for ( srcLen = 1; *inBuff++; srcLen++ )
+ ;
+
+ srcLen *= BYTES_PER_CHAR;
+ }
+ else // we already have the length
+ {
+ // we can only convert an entire number of UTF-32 characters
+ if ( srcLen % BYTES_PER_CHAR )
+ return wxCONV_FAILED;
+ }
+
+ return srcLen;
+}
+
+// case when in-memory representation is UTF-16
+#ifdef WC_UTF16
+
+// ----------------------------------------------------------------------------
+// conversions without endianness change
+// ----------------------------------------------------------------------------
+
+size_t
+wxMBConvUTF32straight::ToWChar(wchar_t *dst, size_t dstLen,
+ const char *src, size_t srcLen) const
+{
+ srcLen = GetLength(src, srcLen);
+ if ( srcLen == wxNO_LEN )
+ return wxCONV_FAILED;
+
+ const wxUint32 *inBuff = reinterpret_cast<const wxUint32 *>(src);
+ const size_t inLen = srcLen / BYTES_PER_CHAR;
+ size_t outLen = 0;
+ for ( size_t n = 0; n < inLen; n++ )
+ {
+ wxUint16 cc[2];
+ const size_t numChars = encode_utf16(*inBuff++, cc);
+ if ( numChars == wxCONV_FAILED )
+ return wxCONV_FAILED;
+
+ outLen += numChars;
+ if ( dst )
+ {
+ if ( outLen > dstLen )
+ return wxCONV_FAILED;
+
+ *dst++ = cc[0];
+ if ( numChars == 2 )
+ {
+ // second character of a surrogate
+ *dst++ = cc[1];
+ }
+ }
+ }
+
+ return outLen;
+}
+
+size_t
+wxMBConvUTF32straight::FromWChar(char *dst, size_t dstLen,
+ const wchar_t *src, size_t srcLen) const
+{
+ if ( srcLen == wxNO_LEN )
+ srcLen = wxWcslen(src) + 1;
+
+ if ( !dst )
+ {
+ // optimization: return maximal space which could be needed for this
+ // string instead of the exact amount which could be less if there are
+ // any surrogates in the input
+ //
+ // we consider that surrogates are rare enough to make it worthwhile to
+ // avoid running the loop below at the cost of slightly extra memory
+ // consumption
+ return srcLen * BYTES_PER_CHAR;
+ }
+
+ wxUint32 *outBuff = reinterpret_cast<wxUint32 *>(dst);
+ size_t outLen = 0;
+ for ( const wchar_t * const srcEnd = src + srcLen; src < srcEnd; )
+ {
+ const wxUint32 ch = wxDecodeSurrogate(&src);
+ if ( !src )
+ return wxCONV_FAILED;
+
+ outLen += BYTES_PER_CHAR;
+
+ if ( outLen > dstLen )
+ return wxCONV_FAILED;
+
+ *outBuff++ = ch;
+ }
+
+ return outLen;
+}
+
+// ----------------------------------------------------------------------------
+// endian-reversing conversions
+// ----------------------------------------------------------------------------
+
+size_t
+wxMBConvUTF32swap::ToWChar(wchar_t *dst, size_t dstLen,
+ const char *src, size_t srcLen) const
+{
+ srcLen = GetLength(src, srcLen);
+ if ( srcLen == wxNO_LEN )
+ return wxCONV_FAILED;
+
+ const wxUint32 *inBuff = reinterpret_cast<const wxUint32 *>(src);
+ const size_t inLen = srcLen / BYTES_PER_CHAR;
+ size_t outLen = 0;
+ for ( size_t n = 0; n < inLen; n++, inBuff++ )
+ {
+ wxUint16 cc[2];
+ const size_t numChars = encode_utf16(wxUINT32_SWAP_ALWAYS(*inBuff), cc);
+ if ( numChars == wxCONV_FAILED )
+ return wxCONV_FAILED;
+
+ outLen += numChars;
+ if ( dst )
+ {
+ if ( outLen > dstLen )
+ return wxCONV_FAILED;
+
+ *dst++ = cc[0];
+ if ( numChars == 2 )
+ {
+ // second character of a surrogate
+ *dst++ = cc[1];
+ }
+ }
+ }
+
+ return outLen;
+}
+
+size_t
+wxMBConvUTF32swap::FromWChar(char *dst, size_t dstLen,
+ const wchar_t *src, size_t srcLen) const
+{
+ if ( srcLen == wxNO_LEN )
+ srcLen = wxWcslen(src) + 1;
+
+ if ( !dst )
+ {
+ // optimization: return maximal space which could be needed for this
+ // string instead of the exact amount which could be less if there are
+ // any surrogates in the input
+ //
+ // we consider that surrogates are rare enough to make it worthwhile to
+ // avoid running the loop below at the cost of slightly extra memory
+ // consumption
+ return srcLen*BYTES_PER_CHAR;
+ }
+
+ wxUint32 *outBuff = reinterpret_cast<wxUint32 *>(dst);
+ size_t outLen = 0;
+ for ( const wchar_t * const srcEnd = src + srcLen; src < srcEnd; )
+ {
+ const wxUint32 ch = wxDecodeSurrogate(&src);
+ if ( !src )
+ return wxCONV_FAILED;
+
+ outLen += BYTES_PER_CHAR;
+
+ if ( outLen > dstLen )
+ return wxCONV_FAILED;
+
+ *outBuff++ = wxUINT32_SWAP_ALWAYS(ch);
+ }
+
+ return outLen;
+}
+
+#else // !WC_UTF16: wchar_t is UTF-32
+
+// ----------------------------------------------------------------------------
+// conversions without endianness change
+// ----------------------------------------------------------------------------
+
+size_t
+wxMBConvUTF32straight::ToWChar(wchar_t *dst, size_t dstLen,
+ const char *src, size_t srcLen) const
+{
+ // use memcpy() as it should be much faster than hand-written loop
+ srcLen = GetLength(src, srcLen);
+ if ( srcLen == wxNO_LEN )
+ return wxCONV_FAILED;
+
+ const size_t inLen = srcLen/BYTES_PER_CHAR;
+ if ( dst )
+ {
+ if ( dstLen < inLen )
+ return wxCONV_FAILED;
+
+ memcpy(dst, src, srcLen);
+ }
+
+ return inLen;
+}
+
+size_t
+wxMBConvUTF32straight::FromWChar(char *dst, size_t dstLen,
+ const wchar_t *src, size_t srcLen) const
+{
+ if ( srcLen == wxNO_LEN )
+ srcLen = wxWcslen(src) + 1;
+
+ srcLen *= BYTES_PER_CHAR;
+
+ if ( dst )
+ {
+ if ( dstLen < srcLen )
+ return wxCONV_FAILED;
+
+ memcpy(dst, src, srcLen);
+ }
+
+ return srcLen;
+}
+
+// ----------------------------------------------------------------------------
+// endian-reversing conversions
+// ----------------------------------------------------------------------------
+
+size_t
+wxMBConvUTF32swap::ToWChar(wchar_t *dst, size_t dstLen,
+ const char *src, size_t srcLen) const
+{
+ srcLen = GetLength(src, srcLen);
+ if ( srcLen == wxNO_LEN )
+ return wxCONV_FAILED;
+
+ srcLen /= BYTES_PER_CHAR;
+
+ if ( dst )
+ {
+ if ( dstLen < srcLen )
+ return wxCONV_FAILED;
+
+ const wxUint32 *inBuff = reinterpret_cast<const wxUint32 *>(src);
+ for ( size_t n = 0; n < srcLen; n++, inBuff++ )
+ {
+ *dst++ = wxUINT32_SWAP_ALWAYS(*inBuff);
+ }
+ }
+
+ return srcLen;
+}
+
+size_t
+wxMBConvUTF32swap::FromWChar(char *dst, size_t dstLen,
+ const wchar_t *src, size_t srcLen) const
+{
+ if ( srcLen == wxNO_LEN )
+ srcLen = wxWcslen(src) + 1;
+
+ srcLen *= BYTES_PER_CHAR;
+
+ if ( dst )
+ {
+ if ( dstLen < srcLen )
+ return wxCONV_FAILED;
+
+ wxUint32 *outBuff = reinterpret_cast<wxUint32 *>(dst);
+ for ( size_t n = 0; n < srcLen; n += BYTES_PER_CHAR, src++ )
+ {
+ *outBuff++ = wxUINT32_SWAP_ALWAYS(*src);
+ }
+ }
+
+ return srcLen;
+}
+
+#endif // WC_UTF16/!WC_UTF16
+
+
+// ============================================================================
+// The classes doing conversion using the iconv_xxx() functions