src/stdlib/SDL_iconv.c
changeset 1501 73dc5d39bbf8
child 1502 d403a39389da
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/stdlib/SDL_iconv.c	Mon Mar 13 01:08:00 2006 +0000
     1.3 @@ -0,0 +1,809 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +/* This file contains portable iconv functions for SDL */
    1.28 +
    1.29 +#include "SDL_stdinc.h"
    1.30 +#include "SDL_endian.h"
    1.31 +
    1.32 +#ifdef HAVE_ICONV
    1.33 +
    1.34 +#include <errno.h>
    1.35 +
    1.36 +size_t SDL_iconv(SDL_iconv_t cd,
    1.37 +                 char **inbuf, size_t *inbytesleft,
    1.38 +                 char **outbuf, size_t *outbytesleft)
    1.39 +{
    1.40 +	size_t retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
    1.41 +	if ( retCode == (size_t)-1 ) {
    1.42 +		switch(errno) {
    1.43 +		    case E2BIG:
    1.44 +			return SDL_ICONV_E2BIG;
    1.45 +		    case EILSEQ:
    1.46 +			return SDL_ICONV_EILSEQ;
    1.47 +		    case EINVAL:
    1.48 +			return SDL_ICONV_EINVAL;
    1.49 +		    default:
    1.50 +			return SDL_ICONV_ERROR;
    1.51 +		}
    1.52 +	}
    1.53 +	return retCode;
    1.54 +}
    1.55 +
    1.56 +#else
    1.57 +
    1.58 +#define UNICODE_BOM	0xFEFF
    1.59 +
    1.60 +#define UNKNOWN_ASCII	'?'
    1.61 +#define UNKNOWN_UNICODE	0xFFFD
    1.62 +
    1.63 +enum {
    1.64 +	ENCODING_UNKNOWN,
    1.65 +	ENCODING_ASCII,
    1.66 +	ENCODING_LATIN1,
    1.67 +	ENCODING_UTF8,
    1.68 +	ENCODING_UTF16,		/* Needs byte order marker */
    1.69 +	ENCODING_UTF16BE,
    1.70 +	ENCODING_UTF16LE,
    1.71 +	ENCODING_UTF32,		/* Needs byte order marker */
    1.72 +	ENCODING_UTF32BE,
    1.73 +	ENCODING_UTF32LE,
    1.74 +	ENCODING_UCS2,		/* Native byte order assumed */
    1.75 +	ENCODING_UCS4,		/* Native byte order assumed */
    1.76 +};
    1.77 +#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    1.78 +#define ENCODING_UTF16NATIVE	ENCODING_UTF16BE
    1.79 +#define ENCODING_UTF32NATIVE	ENCODING_UTF32BE
    1.80 +#else
    1.81 +#define ENCODING_UTF16NATIVE	ENCODING_UTF16LE
    1.82 +#define ENCODING_UTF32NATIVE	ENCODING_UTF32LE
    1.83 +#endif
    1.84 +
    1.85 +struct _SDL_iconv_t
    1.86 +{
    1.87 +	int src_fmt;
    1.88 +	int dst_fmt;
    1.89 +};
    1.90 +
    1.91 +static struct {
    1.92 +	const char *name;
    1.93 +	int format;
    1.94 +} encodings[] = {
    1.95 +	{ "ASCII",	ENCODING_ASCII },
    1.96 +	{ "US-ASCII",	ENCODING_ASCII },
    1.97 +	{ "LATIN1",	ENCODING_LATIN1 },
    1.98 +	{ "ISO-8859-1",	ENCODING_LATIN1 },
    1.99 +	{ "UTF8",	ENCODING_UTF8 },
   1.100 +	{ "UTF-8",	ENCODING_UTF8 },
   1.101 +	{ "UTF16",	ENCODING_UTF16 },
   1.102 +	{ "UTF-16",	ENCODING_UTF16 },
   1.103 +	{ "UTF16BE",	ENCODING_UTF16BE },
   1.104 +	{ "UTF-16BE",	ENCODING_UTF16BE },
   1.105 +	{ "UTF16LE",	ENCODING_UTF16LE },
   1.106 +	{ "UTF-16LE",	ENCODING_UTF16LE },
   1.107 +	{ "UTF32",	ENCODING_UTF32 },
   1.108 +	{ "UTF-32",	ENCODING_UTF32 },
   1.109 +	{ "UTF32BE",	ENCODING_UTF32BE },
   1.110 +	{ "UTF-32BE",	ENCODING_UTF32BE },
   1.111 +	{ "UTF32LE",	ENCODING_UTF32LE },
   1.112 +	{ "UTF-32LE",	ENCODING_UTF32LE },
   1.113 +	{ "UCS2",	ENCODING_UCS2 },
   1.114 +	{ "UCS-2",	ENCODING_UCS2 },
   1.115 +	{ "UCS4",	ENCODING_UCS4 },
   1.116 +	{ "UCS-4",	ENCODING_UCS4 },
   1.117 +};
   1.118 +
   1.119 +SDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode)
   1.120 +{
   1.121 +	int src_fmt = ENCODING_UNKNOWN;
   1.122 +	int dst_fmt = ENCODING_UNKNOWN;
   1.123 +	int i;
   1.124 +
   1.125 +	for ( i = 0; i < SDL_arraysize(encodings); ++i ) {
   1.126 +		if ( SDL_strcasecmp(fromcode, encodings[i].name) == 0 ) {
   1.127 +			src_fmt = encodings[i].format;
   1.128 +			if ( dst_fmt != ENCODING_UNKNOWN ) {
   1.129 +				break;
   1.130 +			}
   1.131 +		}
   1.132 +		if ( SDL_strcasecmp(tocode, encodings[i].name) == 0 ) {
   1.133 +			dst_fmt = encodings[i].format;
   1.134 +			if ( src_fmt != ENCODING_UNKNOWN ) {
   1.135 +				break;
   1.136 +			}
   1.137 +		}
   1.138 +	}
   1.139 +	if ( src_fmt != ENCODING_UNKNOWN && dst_fmt != ENCODING_UNKNOWN ) {
   1.140 +		SDL_iconv_t cd = (SDL_iconv_t)SDL_malloc(sizeof(*cd));
   1.141 +		if ( cd ) {
   1.142 +			cd->src_fmt = src_fmt;
   1.143 +			cd->dst_fmt = dst_fmt;
   1.144 +			return cd;
   1.145 +		}
   1.146 +	}
   1.147 +	return (SDL_iconv_t)-1;
   1.148 +}
   1.149 +
   1.150 +size_t SDL_iconv(SDL_iconv_t cd,
   1.151 +                 char **inbuf, size_t *inbytesleft,
   1.152 +                 char **outbuf, size_t *outbytesleft)
   1.153 +{
   1.154 +	/* For simplicity, we'll convert everything to and from UCS-4 */
   1.155 +	char *src, *dst;
   1.156 +	size_t srclen, dstlen;
   1.157 +	Uint32 ch;
   1.158 +	size_t total;
   1.159 +
   1.160 +	if ( !inbuf || !*inbuf ) {
   1.161 +		/* Reset the context */
   1.162 +		return 0;
   1.163 +	}
   1.164 +	if ( !outbuf || !*outbuf || !outbytesleft || !*outbytesleft ) {
   1.165 +		return SDL_ICONV_E2BIG;
   1.166 +	}
   1.167 +	src = *inbuf;
   1.168 +	srclen = (inbytesleft ? *inbytesleft : 0);
   1.169 +	dst = *outbuf;
   1.170 +	dstlen = *outbytesleft;
   1.171 +
   1.172 +	switch ( cd->src_fmt ) {
   1.173 +	    case ENCODING_UTF16:
   1.174 +		/* Scan for a byte order marker */
   1.175 +		{
   1.176 +			Uint8 *p = (Uint8 *)src;
   1.177 +			size_t n = srclen / 2;
   1.178 +			while ( n ) {
   1.179 +				if ( p[0] == 0xFF && p[1] == 0xFE ) {
   1.180 +					cd->src_fmt = ENCODING_UTF16BE;
   1.181 +					break;
   1.182 +				} else if ( p[0] == 0xFE && p[1] == 0xFF ) {
   1.183 +					cd->src_fmt = ENCODING_UTF16LE;
   1.184 +					break;
   1.185 +				}
   1.186 +				p += 2;
   1.187 +				--n;
   1.188 +			}
   1.189 +			if ( n == 0 ) {
   1.190 +				/* We can't tell, default to host order */
   1.191 +				cd->src_fmt = ENCODING_UTF16NATIVE;
   1.192 +			}
   1.193 +		}
   1.194 +		break;
   1.195 +	    case ENCODING_UTF32:
   1.196 +		/* Scan for a byte order marker */
   1.197 +		{
   1.198 +			Uint8 *p = (Uint8 *)src;
   1.199 +			size_t n = srclen / 4;
   1.200 +			while ( n ) {
   1.201 +				if ( p[0] == 0xFF && p[1] == 0xFE &&
   1.202 +				     p[2] == 0x00 && p[3] == 0x00 ) {
   1.203 +					cd->src_fmt = ENCODING_UTF32BE;
   1.204 +					break;
   1.205 +				} else if ( p[0] == 0x00 && p[1] == 0x00 &&
   1.206 +				            p[2] == 0xFE && p[3] == 0xFF ) {
   1.207 +					cd->src_fmt = ENCODING_UTF32LE;
   1.208 +					break;
   1.209 +				}
   1.210 +				p += 4;
   1.211 +				--n;
   1.212 +			}
   1.213 +			if ( n == 0 ) {
   1.214 +				/* We can't tell, default to host order */
   1.215 +				cd->src_fmt = ENCODING_UTF32NATIVE;
   1.216 +			}
   1.217 +		}
   1.218 +		break;
   1.219 +	}
   1.220 +
   1.221 +	switch ( cd->dst_fmt ) {
   1.222 +	    case ENCODING_UTF16:
   1.223 +		/* Default to host order, need to add byte order marker */
   1.224 +		if ( dstlen < 2 ) {
   1.225 +			return SDL_ICONV_E2BIG;
   1.226 +		}
   1.227 +		*(Uint16 *)dst = UNICODE_BOM;
   1.228 +		dst += 2;
   1.229 +		dstlen -= 2;
   1.230 +		cd->dst_fmt = ENCODING_UTF16NATIVE;
   1.231 +		break;
   1.232 +	    case ENCODING_UTF32:
   1.233 +		/* Default to host order, need to add byte order marker */
   1.234 +		if ( dstlen < 4 ) {
   1.235 +			return SDL_ICONV_E2BIG;
   1.236 +		}
   1.237 +		*(Uint32 *)dst = UNICODE_BOM;
   1.238 +		dst += 4;
   1.239 +		dstlen -= 4;
   1.240 +		cd->dst_fmt = ENCODING_UTF32NATIVE;
   1.241 +		break;
   1.242 +	}
   1.243 +
   1.244 +	total = 0;
   1.245 +	while ( srclen > 0 ) {
   1.246 +		/* Decode a character */
   1.247 +		switch ( cd->src_fmt ) {
   1.248 +		    case ENCODING_ASCII:
   1.249 +			{
   1.250 +				Uint8 *p = (Uint8 *)src;
   1.251 +				ch = (Uint32)(p[0] & 0x7F);
   1.252 +				++src;
   1.253 +				--srclen;
   1.254 +			}
   1.255 +			break;
   1.256 +		    case ENCODING_LATIN1:
   1.257 +			{
   1.258 +				Uint8 *p = (Uint8 *)src;
   1.259 +				ch = (Uint32)p[0];
   1.260 +				++src;
   1.261 +				--srclen;
   1.262 +			}
   1.263 +			break;
   1.264 +		    case ENCODING_UTF8: /* RFC 3629 */
   1.265 +			{
   1.266 +				Uint8 *p = (Uint8 *)src;
   1.267 +				size_t left = 0;
   1.268 +				SDL_bool overlong = SDL_FALSE;
   1.269 +				if ( p[0] >= 0xFC ) {
   1.270 +					if ( (p[0] & 0xFE) != 0xFC ) {
   1.271 +						/* Skip illegal sequences
   1.272 +						return SDL_ICONV_EILSEQ;
   1.273 +						*/
   1.274 +						ch = UNKNOWN_UNICODE;
   1.275 +					} else {
   1.276 +						if ( p[0] == 0xFC ) {
   1.277 +							overlong = SDL_TRUE;
   1.278 +						}
   1.279 +						ch = (Uint32)(p[0] & 0x01);
   1.280 +						left = 5;
   1.281 +					}
   1.282 +				} else if ( p[0] >= 0xF8 ) {
   1.283 +					if ( (p[0] & 0xFC) != 0xF8 ) {
   1.284 +						/* Skip illegal sequences
   1.285 +						return SDL_ICONV_EILSEQ;
   1.286 +						*/
   1.287 +						ch = UNKNOWN_UNICODE;
   1.288 +					} else {
   1.289 +						if ( p[0] == 0xF8 ) {
   1.290 +							overlong = SDL_TRUE;
   1.291 +						}
   1.292 +						ch = (Uint32)(p[0] & 0x03);
   1.293 +						left = 4;
   1.294 +					}
   1.295 +				} else if ( p[0] >= 0xF0 ) {
   1.296 +					if ( (p[0] & 0xF8) != 0xF0 ) {
   1.297 +						/* Skip illegal sequences
   1.298 +						return SDL_ICONV_EILSEQ;
   1.299 +						*/
   1.300 +						ch = UNKNOWN_UNICODE;
   1.301 +					} else {
   1.302 +						if ( p[0] == 0xF0 ) {
   1.303 +							overlong = SDL_TRUE;
   1.304 +						}
   1.305 +						ch = (Uint32)(p[0] & 0x07);
   1.306 +						left = 3;
   1.307 +					}
   1.308 +				} else if ( p[0] >= 0xE0 ) {
   1.309 +					if ( (p[0] & 0xF0) != 0xE0 ) {
   1.310 +						/* Skip illegal sequences
   1.311 +						return SDL_ICONV_EILSEQ;
   1.312 +						*/
   1.313 +						ch = UNKNOWN_UNICODE;
   1.314 +					} else {
   1.315 +						if ( p[0] == 0xE0 ) {
   1.316 +							overlong = SDL_TRUE;
   1.317 +						}
   1.318 +						ch = (Uint32)(p[0] & 0x0F);
   1.319 +						left = 2;
   1.320 +					}
   1.321 +				} else if ( p[0] >= 0xC0 ) {
   1.322 +					if ( (p[0] & 0xE0) != 0xC0 ) {
   1.323 +						/* Skip illegal sequences
   1.324 +						return SDL_ICONV_EILSEQ;
   1.325 +						*/
   1.326 +						ch = UNKNOWN_UNICODE;
   1.327 +					} else {
   1.328 +						if ( (p[0] & 0xCE) == 0xC0 ) {
   1.329 +							overlong = SDL_TRUE;
   1.330 +						}
   1.331 +						ch = (Uint32)(p[0] & 0x1F);
   1.332 +						left = 1;
   1.333 +					}
   1.334 +				} else {
   1.335 +					if ( (p[0] & 0x80) != 0x00 ) {
   1.336 +						/* Skip illegal sequences
   1.337 +						return SDL_ICONV_EILSEQ;
   1.338 +						*/
   1.339 +						ch = UNKNOWN_UNICODE;
   1.340 +					} else {
   1.341 +						ch = (Uint32)p[0];
   1.342 +					}
   1.343 +				}
   1.344 +				++src;
   1.345 +				--srclen;
   1.346 +				if ( srclen < left ) {
   1.347 +					return SDL_ICONV_EINVAL;
   1.348 +				}
   1.349 +				while ( left-- ) {
   1.350 +					++p;
   1.351 +					if ( (p[0] & 0xC0) != 0x80 ) {
   1.352 +						/* Skip illegal sequences
   1.353 +						return SDL_ICONV_EILSEQ;
   1.354 +						*/
   1.355 +						ch = UNKNOWN_UNICODE;
   1.356 +						break;
   1.357 +					}
   1.358 +					ch <<= 6;
   1.359 +					ch |= (p[0] & 0x3F);
   1.360 +					++src;
   1.361 +					--srclen;
   1.362 +				}
   1.363 +				if ( overlong ) {
   1.364 +					/* Potential security risk
   1.365 +					return SDL_ICONV_EILSEQ;
   1.366 +					*/
   1.367 +					ch = UNKNOWN_UNICODE;
   1.368 +				}
   1.369 +				if ( (ch >= 0xD800 && ch <= 0xDFFF) ||
   1.370 +				     (ch == 0xFFFE || ch == 0xFFFF) ) {
   1.371 +					/* Skip illegal sequences
   1.372 +					return SDL_ICONV_EILSEQ;
   1.373 +					*/
   1.374 +					ch = UNKNOWN_UNICODE;
   1.375 +				}
   1.376 +			}
   1.377 +			break;
   1.378 +		    case ENCODING_UTF16BE: /* RFC 2781 */
   1.379 +			{
   1.380 +				Uint8 *p = (Uint8 *)src;
   1.381 +				Uint16 W1, W2;
   1.382 +				if ( srclen < 2 ) {
   1.383 +					return SDL_ICONV_EINVAL;
   1.384 +				}
   1.385 +				W1 = ((Uint32)p[0] << 8) |
   1.386 +				      (Uint32)p[1];
   1.387 +				src += 2;
   1.388 +				srclen -= 2;
   1.389 +				if ( W1 < 0xD800 || W1 > 0xDFFF ) {
   1.390 +					ch = (Uint32)W1;
   1.391 +					break;
   1.392 +				}
   1.393 +				if ( W1 > 0xDBFF ) {
   1.394 +					/* Skip illegal sequences
   1.395 +					return SDL_ICONV_EILSEQ;
   1.396 +					*/
   1.397 +					ch = UNKNOWN_UNICODE;
   1.398 +					break;
   1.399 +				}
   1.400 +				if ( srclen < 2 ) {
   1.401 +					return SDL_ICONV_EINVAL;
   1.402 +				}
   1.403 +				p = src;
   1.404 +				W2 = ((Uint32)p[0] << 8) |
   1.405 +				      (Uint32)p[1];
   1.406 +				src += 2;
   1.407 +				srclen -= 2;
   1.408 +				if ( W2 < 0xDC00 || W2 > 0xDFFF ) {
   1.409 +					/* Skip illegal sequences
   1.410 +					return SDL_ICONV_EILSEQ;
   1.411 +					*/
   1.412 +					ch = UNKNOWN_UNICODE;
   1.413 +					break;
   1.414 +				}
   1.415 +				ch = (((Uint32)(W1 & 0x3FF) << 10) |
   1.416 +				      (Uint32)(W2 & 0x3FF)) + 0x10000;
   1.417 +			}
   1.418 +			break;
   1.419 +		    case ENCODING_UTF16LE: /* RFC 2781 */
   1.420 +			{
   1.421 +				Uint8 *p = (Uint8 *)src;
   1.422 +				Uint16 W1, W2;
   1.423 +				if ( srclen < 2 ) {
   1.424 +					return SDL_ICONV_EINVAL;
   1.425 +				}
   1.426 +				W1 = ((Uint32)p[1] << 8) |
   1.427 +				      (Uint32)p[0];
   1.428 +				src += 2;
   1.429 +				srclen -= 2;
   1.430 +				if ( W1 < 0xD800 || W1 > 0xDFFF ) {
   1.431 +					ch = (Uint32)W1;
   1.432 +					break;
   1.433 +				}
   1.434 +				if ( W1 > 0xDBFF ) {
   1.435 +					/* Skip illegal sequences
   1.436 +					return SDL_ICONV_EILSEQ;
   1.437 +					*/
   1.438 +					ch = UNKNOWN_UNICODE;
   1.439 +					break;
   1.440 +				}
   1.441 +				if ( srclen < 2 ) {
   1.442 +					return SDL_ICONV_EINVAL;
   1.443 +				}
   1.444 +				p = src;
   1.445 +				W2 = ((Uint32)p[1] << 8) |
   1.446 +				      (Uint32)p[0];
   1.447 +				src += 2;
   1.448 +				srclen -= 2;
   1.449 +				if ( W2 < 0xDC00 || W2 > 0xDFFF ) {
   1.450 +					/* Skip illegal sequences
   1.451 +					return SDL_ICONV_EILSEQ;
   1.452 +					*/
   1.453 +					ch = UNKNOWN_UNICODE;
   1.454 +					break;
   1.455 +				}
   1.456 +				ch = (((Uint32)(W1 & 0x3FF) << 10) |
   1.457 +				      (Uint32)(W2 & 0x3FF)) + 0x10000;
   1.458 +			}
   1.459 +			break;
   1.460 +		    case ENCODING_UTF32BE:
   1.461 +			{
   1.462 +				Uint8 *p = (Uint8 *)src;
   1.463 +				if ( srclen < 4 ) {
   1.464 +					return SDL_ICONV_EINVAL;
   1.465 +				}
   1.466 +				ch = ((Uint32)p[0] << 24) |
   1.467 +				     ((Uint32)p[1] << 16) |
   1.468 +				     ((Uint32)p[2] << 8) |
   1.469 +				      (Uint32)p[3];
   1.470 +				src += 4;
   1.471 +				srclen -= 4;
   1.472 +			}
   1.473 +			break;
   1.474 +		    case ENCODING_UTF32LE:
   1.475 +			{
   1.476 +				Uint8 *p = (Uint8 *)src;
   1.477 +				if ( srclen < 4 ) {
   1.478 +					return SDL_ICONV_EINVAL;
   1.479 +				}
   1.480 +				ch = ((Uint32)p[3] << 24) |
   1.481 +				     ((Uint32)p[2] << 16) |
   1.482 +				     ((Uint32)p[1] << 8) |
   1.483 +				      (Uint32)p[0];
   1.484 +				src += 4;
   1.485 +				srclen -= 4;
   1.486 +			}
   1.487 +			break;
   1.488 +		    case ENCODING_UCS2:
   1.489 +			{
   1.490 +				Uint16 *p = (Uint16 *)src;
   1.491 +				if ( srclen < 2 ) {
   1.492 +					return SDL_ICONV_EINVAL;
   1.493 +				}
   1.494 +				ch = *p;
   1.495 +				src += 2;
   1.496 +				srclen -= 2;
   1.497 +			}
   1.498 +			break;
   1.499 +		    case ENCODING_UCS4:
   1.500 +			{
   1.501 +				Uint32 *p = (Uint32 *)src;
   1.502 +				if ( srclen < 4 ) {
   1.503 +					return SDL_ICONV_EINVAL;
   1.504 +				}
   1.505 +				ch = *p;
   1.506 +				src += 4;
   1.507 +				srclen -= 4;
   1.508 +			}
   1.509 +			break;
   1.510 +		}
   1.511 +
   1.512 +		/* Encode a character */
   1.513 +		switch ( cd->dst_fmt ) {
   1.514 +		    case ENCODING_ASCII:
   1.515 +			{
   1.516 +				Uint8 *p = (Uint8 *)dst;
   1.517 +				if ( dstlen < 1 ) {
   1.518 +					return SDL_ICONV_E2BIG;
   1.519 +				}
   1.520 +				if ( ch > 0x7F ) {
   1.521 +					*p = UNKNOWN_ASCII;
   1.522 +				} else {
   1.523 +					*p = (Uint8)ch;
   1.524 +				}
   1.525 +				++dst;
   1.526 +				--dstlen;
   1.527 +			}
   1.528 +			break;
   1.529 +		    case ENCODING_LATIN1:
   1.530 +			{
   1.531 +				Uint8 *p = (Uint8 *)dst;
   1.532 +				if ( dstlen < 1 ) {
   1.533 +					return SDL_ICONV_E2BIG;
   1.534 +				}
   1.535 +				if ( ch > 0xFF ) {
   1.536 +					*p = UNKNOWN_ASCII;
   1.537 +				} else {
   1.538 +					*p = (Uint8)ch;
   1.539 +				}
   1.540 +				++dst;
   1.541 +				--dstlen;
   1.542 +			}
   1.543 +			break;
   1.544 +		    case ENCODING_UTF8: /* RFC 3629 */
   1.545 +			{
   1.546 +				Uint8 *p = (Uint8 *)dst;
   1.547 +				if ( ch > 0x7FFFFFFF ) {
   1.548 +					ch = UNKNOWN_UNICODE;
   1.549 +				}
   1.550 +				if ( ch <= 0x7F ) {
   1.551 +					if ( dstlen < 1 ) {
   1.552 +						return SDL_ICONV_E2BIG;
   1.553 +					}
   1.554 +					*p = (Uint8)ch;
   1.555 +					++dst;
   1.556 +					--dstlen;
   1.557 +				} else if ( ch <= 0x7FF ) {
   1.558 +					if ( dstlen < 2 ) {
   1.559 +						return SDL_ICONV_E2BIG;
   1.560 +					}
   1.561 +					p[0] = 0xC0 | (Uint8)((ch >> 6) & 0x1F);
   1.562 +					p[1] = 0x80 | (Uint8)(ch & 0x3F);
   1.563 +					dst += 2;
   1.564 +					dstlen -= 2;
   1.565 +				} else if ( ch <= 0xFFFF ) {
   1.566 +					if ( dstlen < 3 ) {
   1.567 +						return SDL_ICONV_E2BIG;
   1.568 +					}
   1.569 +					p[0] = 0xE0 | (Uint8)((ch >> 12) & 0x0F);
   1.570 +					p[1] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
   1.571 +					p[2] = 0x80 | (Uint8)(ch & 0x3F);
   1.572 +					dst += 3;
   1.573 +					dstlen -= 3;
   1.574 +				} else if ( ch <= 0x1FFFFF ) {
   1.575 +					if ( dstlen < 4 ) {
   1.576 +						return SDL_ICONV_E2BIG;
   1.577 +					}
   1.578 +					p[0] = 0xF0 | (Uint8)((ch >> 18) & 0x07);
   1.579 +					p[1] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
   1.580 +					p[2] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
   1.581 +					p[3] = 0x80 | (Uint8)(ch & 0x3F);
   1.582 +					dst += 4;
   1.583 +					dstlen -= 4;
   1.584 +				} else if ( ch <= 0x3FFFFFF ) {
   1.585 +					if ( dstlen < 5 ) {
   1.586 +						return SDL_ICONV_E2BIG;
   1.587 +					}
   1.588 +					p[0] = 0xF8 | (Uint8)((ch >> 24) & 0x03);
   1.589 +					p[1] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
   1.590 +					p[2] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
   1.591 +					p[3] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
   1.592 +					p[4] = 0x80 | (Uint8)(ch & 0x3F);
   1.593 +					dst += 5;
   1.594 +					dstlen -= 5;
   1.595 +				} else {
   1.596 +					if ( dstlen < 6 ) {
   1.597 +						return SDL_ICONV_E2BIG;
   1.598 +					}
   1.599 +					p[0] = 0xFC | (Uint8)((ch >> 30) & 0x01);
   1.600 +					p[1] = 0x80 | (Uint8)((ch >> 24) & 0x3F);
   1.601 +					p[2] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
   1.602 +					p[3] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
   1.603 +					p[4] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
   1.604 +					p[5] = 0x80 | (Uint8)(ch & 0x3F);
   1.605 +					dst += 6;
   1.606 +					dstlen -= 6;
   1.607 +				}
   1.608 +			}
   1.609 +			break;
   1.610 +		    case ENCODING_UTF16BE: /* RFC 2781 */
   1.611 +			{
   1.612 +				Uint8 *p = (Uint8 *)dst;
   1.613 +				if ( ch > 0x10FFFF ) {
   1.614 +					ch = UNKNOWN_UNICODE;
   1.615 +				}
   1.616 +				if ( ch < 0x10000 ) {
   1.617 +					if ( dstlen < 2 ) {
   1.618 +						return SDL_ICONV_E2BIG;
   1.619 +					}
   1.620 +					p[0] = (Uint8)(ch >> 8);
   1.621 +					p[1] = (Uint8)ch;
   1.622 +					dst += 2;
   1.623 +					dstlen -= 2;
   1.624 +				} else {
   1.625 +					Uint16 W1, W2;
   1.626 +					if ( dstlen < 4 ) {
   1.627 +						return SDL_ICONV_E2BIG;
   1.628 +					}
   1.629 +					ch = ch - 0x10000;
   1.630 +					W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
   1.631 +					W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
   1.632 +					p[0] = (Uint8)(W1 >> 8);
   1.633 +					p[1] = (Uint8)W1;
   1.634 +					p[2] = (Uint8)(W2 >> 8);
   1.635 +					p[3] = (Uint8)W2;
   1.636 +					dst += 4;
   1.637 +					dstlen -= 4;
   1.638 +				}
   1.639 +			}
   1.640 +			break;
   1.641 +		    case ENCODING_UTF16LE: /* RFC 2781 */
   1.642 +			{
   1.643 +				Uint8 *p = (Uint8 *)dst;
   1.644 +				if ( ch > 0x10FFFF ) {
   1.645 +					ch = UNKNOWN_UNICODE;
   1.646 +				}
   1.647 +				if ( ch < 0x10000 ) {
   1.648 +					if ( dstlen < 2 ) {
   1.649 +						return SDL_ICONV_E2BIG;
   1.650 +					}
   1.651 +					p[1] = (Uint8)(ch >> 8);
   1.652 +					p[0] = (Uint8)ch;
   1.653 +					dst += 2;
   1.654 +					dstlen -= 2;
   1.655 +				} else {
   1.656 +					Uint16 W1, W2;
   1.657 +					if ( dstlen < 4 ) {
   1.658 +						return SDL_ICONV_E2BIG;
   1.659 +					}
   1.660 +					ch = ch - 0x10000;
   1.661 +					W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
   1.662 +					W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
   1.663 +					p[1] = (Uint8)(W1 >> 8);
   1.664 +					p[0] = (Uint8)W1;
   1.665 +					p[3] = (Uint8)(W2 >> 8);
   1.666 +					p[2] = (Uint8)W2;
   1.667 +					dst += 4;
   1.668 +					dstlen -= 4;
   1.669 +				}
   1.670 +			}
   1.671 +			break;
   1.672 +		    case ENCODING_UTF32BE:
   1.673 +			{
   1.674 +				Uint8 *p = (Uint8 *)dst;
   1.675 +				if ( ch > 0x7FFFFFFF ) {
   1.676 +					ch = UNKNOWN_UNICODE;
   1.677 +				}
   1.678 +				if ( dstlen < 4 ) {
   1.679 +					return SDL_ICONV_E2BIG;
   1.680 +				}
   1.681 +				p[0] = (Uint8)(ch >> 24);
   1.682 +				p[1] = (Uint8)(ch >> 16);
   1.683 +				p[2] = (Uint8)(ch >> 8);
   1.684 +				p[3] = (Uint8)ch;
   1.685 +				dst += 4;
   1.686 +				dstlen -= 4;
   1.687 +			}
   1.688 +			break;
   1.689 +		    case ENCODING_UTF32LE:
   1.690 +			{
   1.691 +				Uint8 *p = (Uint8 *)dst;
   1.692 +				if ( ch > 0x7FFFFFFF ) {
   1.693 +					ch = UNKNOWN_UNICODE;
   1.694 +				}
   1.695 +				if ( dstlen < 4 ) {
   1.696 +					return SDL_ICONV_E2BIG;
   1.697 +				}
   1.698 +				p[3] = (Uint8)(ch >> 24);
   1.699 +				p[2] = (Uint8)(ch >> 16);
   1.700 +				p[1] = (Uint8)(ch >> 8);
   1.701 +				p[0] = (Uint8)ch;
   1.702 +				dst += 4;
   1.703 +				dstlen -= 4;
   1.704 +			}
   1.705 +			break;
   1.706 +		    case ENCODING_UCS2:
   1.707 +			{
   1.708 +				Uint16 *p = (Uint16 *)dst;
   1.709 +				if ( ch > 0xFFFF ) {
   1.710 +					ch = UNKNOWN_UNICODE;
   1.711 +				}
   1.712 +				if ( dstlen < 2 ) {
   1.713 +					return SDL_ICONV_E2BIG;
   1.714 +				}
   1.715 +				*p = (Uint16)ch;
   1.716 +				dst += 2;
   1.717 +				dstlen -= 2;
   1.718 +			}
   1.719 +			break;
   1.720 +		    case ENCODING_UCS4:
   1.721 +			{
   1.722 +				Uint32 *p = (Uint32 *)dst;
   1.723 +				if ( ch > 0x7FFFFFFF ) {
   1.724 +					ch = UNKNOWN_UNICODE;
   1.725 +				}
   1.726 +				if ( dstlen < 4 ) {
   1.727 +					return SDL_ICONV_E2BIG;
   1.728 +				}
   1.729 +				*p = ch;
   1.730 +				dst += 4;
   1.731 +				dstlen -= 4;
   1.732 +			}
   1.733 +			break;
   1.734 +		}
   1.735 +
   1.736 +		/* Update state */
   1.737 +		*inbuf = src;
   1.738 +		*inbytesleft = srclen;
   1.739 +		*outbuf = dst;
   1.740 +		*outbytesleft = dstlen;
   1.741 +		++total;
   1.742 +	}
   1.743 +	return total;
   1.744 +}
   1.745 +
   1.746 +int SDL_iconv_close(SDL_iconv_t cd)
   1.747 +{
   1.748 +	if ( cd && cd != (SDL_iconv_t)-1 ) {
   1.749 +		SDL_free(cd);
   1.750 +	}
   1.751 +	return 0;
   1.752 +}
   1.753 +
   1.754 +#endif /* !HAVE_ICONV */
   1.755 +
   1.756 +char *SDL_iconv_string(const char *tocode, const char *fromcode, char *inbuf, size_t inbytesleft)
   1.757 +{
   1.758 +	SDL_iconv_t cd;
   1.759 +	char *string;
   1.760 +	size_t stringsize;
   1.761 +	char *outbuf;
   1.762 +	size_t outbytesleft;
   1.763 +	size_t retCode = 0;
   1.764 +
   1.765 +	cd = SDL_iconv_open(tocode, fromcode);
   1.766 +	if ( cd == (SDL_iconv_t)-1 ) {
   1.767 +		return NULL;
   1.768 +	}
   1.769 +
   1.770 +	stringsize = inbytesleft > 4 ? inbytesleft : 4;
   1.771 +	string = SDL_malloc(stringsize);
   1.772 +	if ( !string ) {
   1.773 +		SDL_iconv_close(cd);
   1.774 +		return NULL;
   1.775 +	}
   1.776 +	outbuf = string;
   1.777 +	outbytesleft = stringsize;
   1.778 +	SDL_memset(outbuf, 0, 4);
   1.779 +
   1.780 +	while ( inbytesleft > 0 ) {
   1.781 +		retCode = SDL_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
   1.782 +		switch (retCode) {
   1.783 +		    case SDL_ICONV_E2BIG:
   1.784 +			{
   1.785 +				char *oldstring = string;
   1.786 +				stringsize *= 2;
   1.787 +				string = SDL_realloc(string, stringsize);
   1.788 +				if ( !string ) {
   1.789 +					SDL_iconv_close(cd);
   1.790 +					return NULL;
   1.791 +				}
   1.792 +				outbuf = string + (outbuf - oldstring);
   1.793 +				outbytesleft = stringsize - (outbuf - string);
   1.794 +				SDL_memset(outbuf, 0, 4);
   1.795 +			}
   1.796 +			break;
   1.797 +		    case SDL_ICONV_EILSEQ:
   1.798 +			/* Try skipping some input data - not perfect, but... */
   1.799 +			++inbuf;
   1.800 +			--inbytesleft;
   1.801 +			break;
   1.802 +		    case SDL_ICONV_EINVAL:
   1.803 +		    case SDL_ICONV_ERROR:
   1.804 +			/* We can't continue... */
   1.805 +			inbytesleft = 0;
   1.806 +			break;
   1.807 +		}
   1.808 +	}
   1.809 +	SDL_iconv_close(cd);
   1.810 +
   1.811 +	return string;
   1.812 +}