19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This file contains portable iconv functions for SDL */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_stdinc.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_endian.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_ICONV
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Depending on which standard the iconv() was implemented with,
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   iconv() may or may not use const char ** for the inbuf param.
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   If we get this wrong, it's just a warning, so no big deal.
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(_XGP6) || \
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    defined(__GLIBC__) && ((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define ICONV_INBUF_NONCONST
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h>
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallsize_t SDL_iconv(SDL_iconv_t cd,
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 const char **inbuf, size_t *inbytesleft,
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 char **outbuf, size_t *outbytesleft)
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size_t retCode;
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef ICONV_INBUF_NONCONST
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retCode = iconv(cd, (char **)inbuf, inbytesleft, outbuf, outbytesleft);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( retCode == (size_t)-1 ) {
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch(errno) {
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case E2BIG:
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return SDL_ICONV_E2BIG;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case EILSEQ:
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return SDL_ICONV_EILSEQ;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case EINVAL:
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return SDL_ICONV_EINVAL;
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    default:
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return SDL_ICONV_ERROR;
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retCode;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Lots of useful information on Unicode at:
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	http://www.cl.cam.ac.uk/~mgk25/unicode.html
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define UNICODE_BOM	0xFEFF
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define UNKNOWN_ASCII	'?'
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define UNKNOWN_UNICODE	0xFFFD
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallenum {
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ENCODING_UNKNOWN,
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ENCODING_ASCII,
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ENCODING_LATIN1,
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ENCODING_UTF8,
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ENCODING_UTF16,		/* Needs byte order marker */
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ENCODING_UTF16BE,
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ENCODING_UTF16LE,
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ENCODING_UTF32,		/* Needs byte order marker */
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ENCODING_UTF32BE,
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ENCODING_UTF32LE,
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ENCODING_UCS2,		/* Native byte order assumed */
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ENCODING_UCS4,		/* Native byte order assumed */
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_BYTEORDER == SDL_BIG_ENDIAN
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define ENCODING_UTF16NATIVE	ENCODING_UTF16BE
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define ENCODING_UTF32NATIVE	ENCODING_UTF32BE
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define ENCODING_UTF16NATIVE	ENCODING_UTF16LE
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define ENCODING_UTF32NATIVE	ENCODING_UTF32LE
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct _SDL_iconv_t
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int src_fmt;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int dst_fmt;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct {
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *name;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int format;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} encodings[] = {
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "ASCII",	ENCODING_ASCII },
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "US-ASCII",	ENCODING_ASCII },
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "8859-1",	ENCODING_LATIN1 },
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "ISO-8859-1",	ENCODING_LATIN1 },
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF8",	ENCODING_UTF8 },
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF-8",	ENCODING_UTF8 },
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF16",	ENCODING_UTF16 },
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF-16",	ENCODING_UTF16 },
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF16BE",	ENCODING_UTF16BE },
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF-16BE",	ENCODING_UTF16BE },
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF16LE",	ENCODING_UTF16LE },
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF-16LE",	ENCODING_UTF16LE },
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF32",	ENCODING_UTF32 },
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF-32",	ENCODING_UTF32 },
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF32BE",	ENCODING_UTF32BE },
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF-32BE",	ENCODING_UTF32BE },
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF32LE",	ENCODING_UTF32LE },
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UTF-32LE",	ENCODING_UTF32LE },
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UCS2",	ENCODING_UCS2 },
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UCS-2",	ENCODING_UCS2 },
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UCS4",	ENCODING_UCS4 },
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "UCS-4",	ENCODING_UCS4 },
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *getlocale(char *buffer, size_t bufsize)
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *lang;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *ptr;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	lang = SDL_getenv("LC_ALL");
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !lang ) {
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lang = SDL_getenv("LC_CTYPE");
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !lang ) {
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lang = SDL_getenv("LC_MESSAGES");
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !lang ) {
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lang = SDL_getenv("LANG");
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !lang || !*lang || SDL_strcmp(lang, "C") == 0 ) {
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lang = "ASCII";
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We need to trim down strings like "en_US.UTF-8@blah" to "UTF-8" */
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ptr = SDL_strchr(lang, '.');
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (ptr != NULL) {
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lang = ptr + 1;
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_strlcpy(buffer, lang, bufsize);
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ptr = SDL_strchr(buffer, '@');
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (ptr != NULL) {
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*ptr = '\0';  /* chop end of string. */
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return buffer;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode)
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int src_fmt = ENCODING_UNKNOWN;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int dst_fmt = ENCODING_UNKNOWN;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char fromcode_buffer[64];
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char tocode_buffer[64];
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !fromcode || !*fromcode ) {
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fromcode = getlocale(fromcode_buffer, sizeof(fromcode_buffer));
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !tocode || !*tocode ) {
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		tocode = getlocale(tocode_buffer, sizeof(tocode_buffer));
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i = 0; i < SDL_arraysize(encodings); ++i ) {
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_strcasecmp(fromcode, encodings[i].name) == 0 ) {
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			src_fmt = encodings[i].format;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( dst_fmt != ENCODING_UNKNOWN ) {
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_strcasecmp(tocode, encodings[i].name) == 0 ) {
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			dst_fmt = encodings[i].format;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( src_fmt != ENCODING_UNKNOWN ) {
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( src_fmt != ENCODING_UNKNOWN && dst_fmt != ENCODING_UNKNOWN ) {
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_iconv_t cd = (SDL_iconv_t)SDL_malloc(sizeof(*cd));
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( cd ) {
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cd->src_fmt = src_fmt;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cd->dst_fmt = dst_fmt;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return cd;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (SDL_iconv_t)-1;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallsize_t SDL_iconv(SDL_iconv_t cd,
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 const char **inbuf, size_t *inbytesleft,
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 char **outbuf, size_t *outbytesleft)
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* For simplicity, we'll convert everything to and from UCS-4 */
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *src;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *dst;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size_t srclen, dstlen;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 ch = 0;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size_t total;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !inbuf || !*inbuf ) {
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Reset the context */
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !outbuf || !*outbuf || !outbytesleft || !*outbytesleft ) {
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return SDL_ICONV_E2BIG;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	src = *inbuf;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	srclen = (inbytesleft ? *inbytesleft : 0);
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dst = *outbuf;
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dstlen = *outbytesleft;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch ( cd->src_fmt ) {
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case ENCODING_UTF16:
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Scan for a byte order marker */
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Uint8 *p = (Uint8 *)src;
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			size_t n = srclen / 2;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			while ( n ) {
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( p[0] == 0xFF && p[1] == 0xFE ) {
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					cd->src_fmt = ENCODING_UTF16BE;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else if ( p[0] == 0xFE && p[1] == 0xFF ) {
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					cd->src_fmt = ENCODING_UTF16LE;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				p += 2;
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				--n;
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( n == 0 ) {
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* We can't tell, default to host order */
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cd->src_fmt = ENCODING_UTF16NATIVE;
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case ENCODING_UTF32:
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Scan for a byte order marker */
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Uint8 *p = (Uint8 *)src;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			size_t n = srclen / 4;
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			while ( n ) {
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( p[0] == 0xFF && p[1] == 0xFE &&
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				     p[2] == 0x00 && p[3] == 0x00 ) {
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					cd->src_fmt = ENCODING_UTF32BE;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else if ( p[0] == 0x00 && p[1] == 0x00 &&
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				            p[2] == 0xFE && p[3] == 0xFF ) {
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					cd->src_fmt = ENCODING_UTF32LE;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				p += 4;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				--n;
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( n == 0 ) {
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* We can't tell, default to host order */
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cd->src_fmt = ENCODING_UTF32NATIVE;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch ( cd->dst_fmt ) {
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case ENCODING_UTF16:
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Default to host order, need to add byte order marker */
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( dstlen < 2 ) {
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return SDL_ICONV_E2BIG;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*(Uint16 *)dst = UNICODE_BOM;
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dst += 2;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dstlen -= 2;
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cd->dst_fmt = ENCODING_UTF16NATIVE;
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case ENCODING_UTF32:
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Default to host order, need to add byte order marker */
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( dstlen < 4 ) {
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return SDL_ICONV_E2BIG;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*(Uint32 *)dst = UNICODE_BOM;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dst += 4;
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dstlen -= 4;
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cd->dst_fmt = ENCODING_UTF32NATIVE;
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	total = 0;
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( srclen > 0 ) {
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Decode a character */
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch ( cd->src_fmt ) {
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_ASCII:
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)src;
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ch = (Uint32)(p[0] & 0x7F);
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				++src;
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				--srclen;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_LATIN1:
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)src;
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ch = (Uint32)p[0];
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				++src;
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				--srclen;
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UTF8: /* RFC 3629 */
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)src;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				size_t left = 0;
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_bool overlong = SDL_FALSE;
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( p[0] >= 0xFC ) {
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( (p[0] & 0xFE) != 0xFC ) {
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						/* Skip illegal sequences
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_EILSEQ;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						*/
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ch = UNKNOWN_UNICODE;
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					} else {
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						if ( p[0] == 0xFC ) {
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							overlong = SDL_TRUE;
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ch = (Uint32)(p[0] & 0x01);
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						left = 5;
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else if ( p[0] >= 0xF8 ) {
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( (p[0] & 0xFC) != 0xF8 ) {
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						/* Skip illegal sequences
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_EILSEQ;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						*/
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ch = UNKNOWN_UNICODE;
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					} else {
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						if ( p[0] == 0xF8 ) {
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							overlong = SDL_TRUE;
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ch = (Uint32)(p[0] & 0x03);
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						left = 4;
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else if ( p[0] >= 0xF0 ) {
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( (p[0] & 0xF8) != 0xF0 ) {
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						/* Skip illegal sequences
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_EILSEQ;
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						*/
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ch = UNKNOWN_UNICODE;
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					} else {
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						if ( p[0] == 0xF0 ) {
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							overlong = SDL_TRUE;
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ch = (Uint32)(p[0] & 0x07);
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						left = 3;
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else if ( p[0] >= 0xE0 ) {
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( (p[0] & 0xF0) != 0xE0 ) {
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						/* Skip illegal sequences
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_EILSEQ;
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						*/
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ch = UNKNOWN_UNICODE;
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					} else {
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						if ( p[0] == 0xE0 ) {
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							overlong = SDL_TRUE;
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ch = (Uint32)(p[0] & 0x0F);
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						left = 2;
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else if ( p[0] >= 0xC0 ) {
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( (p[0] & 0xE0) != 0xC0 ) {
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						/* Skip illegal sequences
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_EILSEQ;
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						*/
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ch = UNKNOWN_UNICODE;
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					} else {
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						if ( (p[0] & 0xDE) == 0xC0 ) {
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							overlong = SDL_TRUE;
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ch = (Uint32)(p[0] & 0x1F);
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						left = 1;
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( (p[0] & 0x80) != 0x00 ) {
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						/* Skip illegal sequences
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_EILSEQ;
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						*/
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ch = UNKNOWN_UNICODE;
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					} else {
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ch = (Uint32)p[0];
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				++src;
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				--srclen;
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( srclen < left ) {
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EINVAL;
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				while ( left-- ) {
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					++p;
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( (p[0] & 0xC0) != 0x80 ) {
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						/* Skip illegal sequences
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_EILSEQ;
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						*/
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ch = UNKNOWN_UNICODE;
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						break;
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch <<= 6;
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch |= (p[0] & 0x3F);
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					++src;
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					--srclen;
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( overlong ) {
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Potential security risk
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EILSEQ;
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*/
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = UNKNOWN_UNICODE;
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( (ch >= 0xD800 && ch <= 0xDFFF) ||
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				     (ch == 0xFFFE || ch == 0xFFFF) ||
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				     ch > 0x10FFFF ) {
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Skip illegal sequences
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EILSEQ;
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*/
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = UNKNOWN_UNICODE;
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UTF16BE: /* RFC 2781 */
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)src;
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint16 W1, W2;
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( srclen < 2 ) {
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EINVAL;
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				W1 = ((Uint16)p[0] << 8) |
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				      (Uint16)p[1];
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				src += 2;
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				srclen -= 2;
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( W1 < 0xD800 || W1 > 0xDFFF ) {
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = (Uint32)W1;
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( W1 > 0xDBFF ) {
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Skip illegal sequences
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EILSEQ;
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*/
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = UNKNOWN_UNICODE;
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( srclen < 2 ) {
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EINVAL;
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				p = (Uint8 *)src;
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				W2 = ((Uint16)p[0] << 8) |
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				      (Uint16)p[1];
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				src += 2;
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				srclen -= 2;
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( W2 < 0xDC00 || W2 > 0xDFFF ) {
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Skip illegal sequences
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EILSEQ;
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*/
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = UNKNOWN_UNICODE;
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ch = (((Uint32)(W1 & 0x3FF) << 10) |
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				      (Uint32)(W2 & 0x3FF)) + 0x10000;
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UTF16LE: /* RFC 2781 */
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)src;
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint16 W1, W2;
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( srclen < 2 ) {
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EINVAL;
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				W1 = ((Uint16)p[1] << 8) |
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				      (Uint16)p[0];
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				src += 2;
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				srclen -= 2;
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( W1 < 0xD800 || W1 > 0xDFFF ) {
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = (Uint32)W1;
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( W1 > 0xDBFF ) {
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Skip illegal sequences
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EILSEQ;
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*/
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = UNKNOWN_UNICODE;
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( srclen < 2 ) {
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EINVAL;
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				p = (Uint8 *)src;
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				W2 = ((Uint16)p[1] << 8) |
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				      (Uint16)p[0];
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				src += 2;
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				srclen -= 2;
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( W2 < 0xDC00 || W2 > 0xDFFF ) {
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Skip illegal sequences
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EILSEQ;
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*/
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = UNKNOWN_UNICODE;
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ch = (((Uint32)(W1 & 0x3FF) << 10) |
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				      (Uint32)(W2 & 0x3FF)) + 0x10000;
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UTF32BE:
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)src;
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( srclen < 4 ) {
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EINVAL;
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ch = ((Uint32)p[0] << 24) |
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				     ((Uint32)p[1] << 16) |
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				     ((Uint32)p[2] << 8) |
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				      (Uint32)p[3];
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				src += 4;
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				srclen -= 4;
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UTF32LE:
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)src;
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( srclen < 4 ) {
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EINVAL;
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ch = ((Uint32)p[3] << 24) |
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				     ((Uint32)p[2] << 16) |
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				     ((Uint32)p[1] << 8) |
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				      (Uint32)p[0];
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				src += 4;
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				srclen -= 4;
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UCS2:
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint16 *p = (Uint16 *)src;
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( srclen < 2 ) {
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EINVAL;
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ch = *p;
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				src += 2;
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				srclen -= 2;
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UCS4:
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint32 *p = (Uint32 *)src;
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( srclen < 4 ) {
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_EINVAL;
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ch = *p;
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				src += 4;
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				srclen -= 4;
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Encode a character */
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch ( cd->dst_fmt ) {
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_ASCII:
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)dst;
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( dstlen < 1 ) {
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_E2BIG;
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ch > 0x7F ) {
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*p = UNKNOWN_ASCII;
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*p = (Uint8)ch;
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				++dst;
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				--dstlen;
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_LATIN1:
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)dst;
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( dstlen < 1 ) {
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_E2BIG;
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ch > 0xFF ) {
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*p = UNKNOWN_ASCII;
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*p = (Uint8)ch;
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				++dst;
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				--dstlen;
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UTF8: /* RFC 3629 */
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)dst;
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ch > 0x10FFFF ) {
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = UNKNOWN_UNICODE;
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ch <= 0x7F ) {
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( dstlen < 1 ) {
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_E2BIG;
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*p = (Uint8)ch;
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					++dst;
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					--dstlen;
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else if ( ch <= 0x7FF ) {
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( dstlen < 2 ) {
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_E2BIG;
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[0] = 0xC0 | (Uint8)((ch >> 6) & 0x1F);
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[1] = 0x80 | (Uint8)(ch & 0x3F);
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dst += 2;
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dstlen -= 2;
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else if ( ch <= 0xFFFF ) {
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( dstlen < 3 ) {
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_E2BIG;
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[0] = 0xE0 | (Uint8)((ch >> 12) & 0x0F);
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[1] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[2] = 0x80 | (Uint8)(ch & 0x3F);
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dst += 3;
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dstlen -= 3;
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else if ( ch <= 0x1FFFFF ) {
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( dstlen < 4 ) {
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_E2BIG;
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[0] = 0xF0 | (Uint8)((ch >> 18) & 0x07);
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[1] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[2] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[3] = 0x80 | (Uint8)(ch & 0x3F);
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dst += 4;
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dstlen -= 4;
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else if ( ch <= 0x3FFFFFF ) {
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( dstlen < 5 ) {
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_E2BIG;
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[0] = 0xF8 | (Uint8)((ch >> 24) & 0x03);
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[1] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[2] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[3] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[4] = 0x80 | (Uint8)(ch & 0x3F);
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dst += 5;
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dstlen -= 5;
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( dstlen < 6 ) {
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_E2BIG;
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[0] = 0xFC | (Uint8)((ch >> 30) & 0x01);
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[1] = 0x80 | (Uint8)((ch >> 24) & 0x3F);
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[2] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[3] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[4] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[5] = 0x80 | (Uint8)(ch & 0x3F);
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dst += 6;
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dstlen -= 6;
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UTF16BE: /* RFC 2781 */
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)dst;
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ch > 0x10FFFF ) {
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = UNKNOWN_UNICODE;
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ch < 0x10000 ) {
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( dstlen < 2 ) {
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_E2BIG;
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[0] = (Uint8)(ch >> 8);
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[1] = (Uint8)ch;
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dst += 2;
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dstlen -= 2;
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Uint16 W1, W2;
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( dstlen < 4 ) {
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_E2BIG;
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = ch - 0x10000;
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[0] = (Uint8)(W1 >> 8);
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[1] = (Uint8)W1;
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[2] = (Uint8)(W2 >> 8);
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[3] = (Uint8)W2;
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dst += 4;
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dstlen -= 4;
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UTF16LE: /* RFC 2781 */
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)dst;
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ch > 0x10FFFF ) {
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = UNKNOWN_UNICODE;
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ch < 0x10000 ) {
7079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( dstlen < 2 ) {
7089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_E2BIG;
7099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
7109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[1] = (Uint8)(ch >> 8);
7119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[0] = (Uint8)ch;
7129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dst += 2;
7139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dstlen -= 2;
7149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
7159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Uint16 W1, W2;
7169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( dstlen < 4 ) {
7179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						return SDL_ICONV_E2BIG;
7189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
7199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = ch - 0x10000;
7209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
7219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
7229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[1] = (Uint8)(W1 >> 8);
7239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[0] = (Uint8)W1;
7249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[3] = (Uint8)(W2 >> 8);
7259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					p[2] = (Uint8)W2;
7269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dst += 4;
7279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dstlen -= 4;
7289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UTF32BE:
7329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
7339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)dst;
7349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ch > 0x10FFFF ) {
7359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = UNKNOWN_UNICODE;
7369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( dstlen < 4 ) {
7389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_E2BIG;
7399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				p[0] = (Uint8)(ch >> 24);
7419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				p[1] = (Uint8)(ch >> 16);
7429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				p[2] = (Uint8)(ch >> 8);
7439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				p[3] = (Uint8)ch;
7449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dst += 4;
7459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstlen -= 4;
7469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UTF32LE:
7499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
7509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *p = (Uint8 *)dst;
7519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ch > 0x10FFFF ) {
7529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = UNKNOWN_UNICODE;
7539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( dstlen < 4 ) {
7559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_E2BIG;
7569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				p[3] = (Uint8)(ch >> 24);
7589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				p[2] = (Uint8)(ch >> 16);
7599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				p[1] = (Uint8)(ch >> 8);
7609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				p[0] = (Uint8)ch;
7619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dst += 4;
7629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstlen -= 4;
7639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UCS2:
7669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
7679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint16 *p = (Uint16 *)dst;
7689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ch > 0xFFFF ) {
7699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = UNKNOWN_UNICODE;
7709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( dstlen < 2 ) {
7729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_E2BIG;
7739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*p = (Uint16)ch;
7759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dst += 2;
7769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstlen -= 2;
7779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case ENCODING_UCS4:
7809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
7819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint32 *p = (Uint32 *)dst;
7829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ch > 0x7FFFFFFF ) {
7839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ch = UNKNOWN_UNICODE;
7849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( dstlen < 4 ) {
7869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return SDL_ICONV_E2BIG;
7879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*p = ch;
7899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dst += 4;
7909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstlen -= 4;
7919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Update state */
7969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*inbuf = src;
7979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*inbytesleft = srclen;
7989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*outbuf = dst;
7999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*outbytesleft = dstlen;
8009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		++total;
8019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return total;
8039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_iconv_close(SDL_iconv_t cd)
8069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cd && cd != (SDL_iconv_t)-1 ) {
8089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(cd);
8099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
8119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !HAVE_ICONV */
8149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchar *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, size_t inbytesleft)
8169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_iconv_t cd;
8189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *string;
8199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size_t stringsize;
8209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *outbuf;
8219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size_t outbytesleft;
8229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size_t retCode = 0;
8239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cd = SDL_iconv_open(tocode, fromcode);
8259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cd == (SDL_iconv_t)-1 ) {
8269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* See if we can recover here (fixes iconv on Solaris 11) */
8279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( !tocode || !*tocode ) {
8289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tocode = "UTF-8";
8299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( !fromcode || !*fromcode ) {
8319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fromcode = "UTF-8";
8329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cd = SDL_iconv_open(tocode, fromcode);
8349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cd == (SDL_iconv_t)-1 ) {
8369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
8379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	stringsize = inbytesleft > 4 ? inbytesleft : 4;
8409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	string = SDL_malloc(stringsize);
8419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !string ) {
8429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_iconv_close(cd);
8439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
8449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	outbuf = string;
8469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	outbytesleft = stringsize;
8479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(outbuf, 0, 4);
8489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( inbytesleft > 0 ) {
8509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retCode = SDL_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
8519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (retCode) {
8529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case SDL_ICONV_E2BIG:
8539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
8549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				char *oldstring = string;
8559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				stringsize *= 2;
8569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				string = SDL_realloc(string, stringsize);
8579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( !string ) {
8589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_iconv_close(cd);
8599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return NULL;
8609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
8619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				outbuf = string + (outbuf - oldstring);
8629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				outbytesleft = stringsize - (outbuf - string);
8639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_memset(outbuf, 0, 4);
8649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
8659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
8669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case SDL_ICONV_EILSEQ:
8679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Try skipping some input data - not perfect, but... */
8689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			++inbuf;
8699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			--inbytesleft;
8709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
8719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case SDL_ICONV_EINVAL:
8729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case SDL_ICONV_ERROR:
8739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* We can't continue... */
8749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			inbytesleft = 0;
8759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
8769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_iconv_close(cd);
8799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return string;
8819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
882