22 #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
23 #define SDL_DISABLE_ANALYZE_MACROS 1
26 #include "../SDL_internal.h"
33 #if defined(HAVE_ICONV) && defined(HAVE_ICONV_H)
40 #if defined(_XGP6) || defined(__APPLE__) || \
41 defined(__EMSCRIPTEN__) || \
42 (defined(__GLIBC__) && ((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) || \
43 (defined(_NEWLIB_VERSION)))
44 #define ICONV_INBUF_NONCONST
54 return (SDL_iconv_t) ((
size_t) iconv_open(tocode, fromcode));
60 return iconv_close((iconv_t) ((
size_t) cd));
65 const char **inbuf,
size_t * inbytesleft,
66 char **outbuf,
size_t * outbytesleft)
69 #ifdef ICONV_INBUF_NONCONST
70 retCode = iconv((iconv_t) ((
size_t) cd), (
char **) inbuf, inbytesleft, outbuf, outbytesleft);
72 retCode = iconv((iconv_t) ((
size_t) cd), inbuf, inbytesleft, outbuf, outbytesleft);
74 if (retCode == (
size_t) - 1) {
95 #define UNICODE_BOM 0xFEFF
97 #define UNKNOWN_ASCII '?'
98 #define UNKNOWN_UNICODE 0xFFFD
117 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
118 #define ENCODING_UTF16NATIVE ENCODING_UTF16BE
119 #define ENCODING_UTF32NATIVE ENCODING_UTF32BE
120 #define ENCODING_UCS2NATIVE ENCODING_UCS2BE
121 #define ENCODING_UCS4NATIVE ENCODING_UCS4BE
123 #define ENCODING_UTF16NATIVE ENCODING_UTF16LE
124 #define ENCODING_UTF32NATIVE ENCODING_UTF32LE
125 #define ENCODING_UCS2NATIVE ENCODING_UCS2LE
126 #define ENCODING_UCS4NATIVE ENCODING_UCS4LE
188 if (!lang || !*lang ||
SDL_strcmp(lang,
"C") == 0) {
213 char fromcode_buffer[64];
214 char tocode_buffer[64];
216 if (!fromcode || !*fromcode) {
217 fromcode =
getlocale(fromcode_buffer,
sizeof(fromcode_buffer));
219 if (!tocode || !*tocode) {
220 tocode =
getlocale(tocode_buffer,
sizeof(tocode_buffer));
237 SDL_iconv_t cd = (SDL_iconv_t)
SDL_malloc(
sizeof(*cd));
239 cd->src_fmt = src_fmt;
240 cd->dst_fmt = dst_fmt;
244 return (SDL_iconv_t) - 1;
249 const char **inbuf,
size_t * inbytesleft,
250 char **outbuf,
size_t * outbytesleft)
255 size_t srclen, dstlen;
259 if (!inbuf || !*inbuf) {
263 if (!outbuf || !*outbuf || !outbytesleft || !*outbytesleft) {
267 srclen = (inbytesleft ? *inbytesleft : 0);
269 dstlen = *outbytesleft;
271 switch (cd->src_fmt) {
276 size_t n = srclen / 2;
278 if (
p[0] == 0xFF &&
p[1] == 0xFE) {
281 }
else if (
p[0] == 0xFE &&
p[1] == 0xFF) {
298 size_t n = srclen / 4;
300 if (
p[0] == 0xFF &&
p[1] == 0xFE &&
301 p[2] == 0x00 &&
p[3] == 0x00) {
304 }
else if (
p[0] == 0x00 &&
p[1] == 0x00 &&
305 p[2] == 0xFE &&
p[3] == 0xFF) {
320 switch (cd->dst_fmt) {
346 switch (cd->src_fmt) {
369 if ((
p[0] & 0xFE) != 0xFC) {
375 if (
p[0] == 0xFC && srclen > 1 && (
p[1] & 0xFC) == 0x80) {
381 }
else if (
p[0] >= 0xF8) {
382 if ((
p[0] & 0xFC) != 0xF8) {
388 if (
p[0] == 0xF8 && srclen > 1 && (
p[1] & 0xF8) == 0x80) {
394 }
else if (
p[0] >= 0xF0) {
395 if ((
p[0] & 0xF8) != 0xF0) {
401 if (
p[0] == 0xF0 && srclen > 1 && (
p[1] & 0xF0) == 0x80) {
407 }
else if (
p[0] >= 0xE0) {
408 if ((
p[0] & 0xF0) != 0xE0) {
414 if (
p[0] == 0xE0 && srclen > 1 && (
p[1] & 0xE0) == 0x80) {
420 }
else if (
p[0] >= 0xC0) {
421 if ((
p[0] & 0xE0) != 0xC0) {
427 if ((
p[0] & 0xDE) == 0xC0) {
434 if ((
p[0] & 0x80) != 0x00) {
450 if ((
p[0] & 0xC0) != 0x80) {
468 if ((ch >= 0xD800 && ch <= 0xDFFF) ||
469 (ch == 0xFFFE || ch == 0xFFFF) || ch > 0x10FFFF) {
487 if (W1 < 0xD800 || W1 > 0xDFFF) {
505 if (W2 < 0xDC00 || W2 > 0xDFFF) {
512 ch = (((
Uint32) (W1 & 0x3FF) << 10) |
513 (
Uint32) (W2 & 0x3FF)) + 0x10000;
526 if (W1 < 0xD800 || W1 > 0xDFFF) {
544 if (W2 < 0xDC00 || W2 > 0xDFFF) {
551 ch = (((
Uint32) (W1 & 0x3FF) << 10) |
552 (
Uint32) (W2 & 0x3FF)) + 0x10000;
608 switch (cd->dst_fmt) {
652 }
else if (ch <= 0x7FF) {
656 p[0] = 0xC0 | (
Uint8) ((ch >> 6) & 0x1F);
657 p[1] = 0x80 | (
Uint8) (ch & 0x3F);
660 }
else if (ch <= 0xFFFF) {
664 p[0] = 0xE0 | (
Uint8) ((ch >> 12) & 0x0F);
665 p[1] = 0x80 | (
Uint8) ((ch >> 6) & 0x3F);
666 p[2] = 0x80 | (
Uint8) (ch & 0x3F);
669 }
else if (ch <= 0x1FFFFF) {
673 p[0] = 0xF0 | (
Uint8) ((ch >> 18) & 0x07);
674 p[1] = 0x80 | (
Uint8) ((ch >> 12) & 0x3F);
675 p[2] = 0x80 | (
Uint8) ((ch >> 6) & 0x3F);
676 p[3] = 0x80 | (
Uint8) (ch & 0x3F);
679 }
else if (ch <= 0x3FFFFFF) {
683 p[0] = 0xF8 | (
Uint8) ((ch >> 24) & 0x03);
684 p[1] = 0x80 | (
Uint8) ((ch >> 18) & 0x3F);
685 p[2] = 0x80 | (
Uint8) ((ch >> 12) & 0x3F);
686 p[3] = 0x80 | (
Uint8) ((ch >> 6) & 0x3F);
687 p[4] = 0x80 | (
Uint8) (ch & 0x3F);
694 p[0] = 0xFC | (
Uint8) ((ch >> 30) & 0x01);
695 p[1] = 0x80 | (
Uint8) ((ch >> 24) & 0x3F);
696 p[2] = 0x80 | (
Uint8) ((ch >> 18) & 0x3F);
697 p[3] = 0x80 | (
Uint8) ((ch >> 12) & 0x3F);
698 p[4] = 0x80 | (
Uint8) ((ch >> 6) & 0x3F);
699 p[5] = 0x80 | (
Uint8) (ch & 0x3F);
725 W1 = 0xD800 | (
Uint16) ((ch >> 10) & 0x3FF);
726 W2 = 0xDC00 | (
Uint16) (ch & 0x3FF);
756 W1 = 0xD800 | (
Uint16) ((ch >> 10) & 0x3FF);
757 W2 = 0xDC00 | (
Uint16) (ch & 0x3FF);
803 if (ch > 0x7FFFFFFF) {
811 p[0] = (
Uint8) (ch >> 24);
812 p[1] = (
Uint8) (ch >> 16);
825 if (ch > 0x7FFFFFFF) {
833 p[3] = (
Uint8) (ch >> 24);
834 p[2] = (
Uint8) (ch >> 16);
845 *inbytesleft = srclen;
847 *outbytesleft = dstlen;
856 if (cd != (SDL_iconv_t)-1) {
876 if (cd == (SDL_iconv_t) - 1) {
878 if (!tocode || !*tocode) {
881 if (!fromcode || !*fromcode) {
886 if (cd == (SDL_iconv_t) - 1) {
890 stringsize = inbytesleft > 4 ? inbytesleft : 4;
897 outbytesleft = stringsize;
900 while (inbytesleft > 0) {
901 retCode =
SDL_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
912 outbuf =
string + (outbuf - oldstring);
913 outbytesleft = stringsize - (outbuf -
string);