src/stdlib/SDL_iconv.c
changeset 1895 c121d94672cb
parent 1849 b5a4ac87b98c
child 2135 0313af081a84
     1.1 --- a/src/stdlib/SDL_iconv.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/src/stdlib/SDL_iconv.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -30,24 +30,25 @@
     1.4  
     1.5  #include <errno.h>
     1.6  
     1.7 -size_t SDL_iconv(SDL_iconv_t cd,
     1.8 -                 char **inbuf, size_t *inbytesleft,
     1.9 -                 char **outbuf, size_t *outbytesleft)
    1.10 +size_t
    1.11 +SDL_iconv(SDL_iconv_t cd,
    1.12 +          char **inbuf, size_t * inbytesleft,
    1.13 +          char **outbuf, size_t * outbytesleft)
    1.14  {
    1.15 -	size_t retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
    1.16 -	if ( retCode == (size_t)-1 ) {
    1.17 -		switch(errno) {
    1.18 -		    case E2BIG:
    1.19 -			return SDL_ICONV_E2BIG;
    1.20 -		    case EILSEQ:
    1.21 -			return SDL_ICONV_EILSEQ;
    1.22 -		    case EINVAL:
    1.23 -			return SDL_ICONV_EINVAL;
    1.24 -		    default:
    1.25 -			return SDL_ICONV_ERROR;
    1.26 -		}
    1.27 -	}
    1.28 -	return retCode;
    1.29 +    size_t retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
    1.30 +    if (retCode == (size_t) - 1) {
    1.31 +        switch (errno) {
    1.32 +        case E2BIG:
    1.33 +            return SDL_ICONV_E2BIG;
    1.34 +        case EILSEQ:
    1.35 +            return SDL_ICONV_EILSEQ;
    1.36 +        case EINVAL:
    1.37 +            return SDL_ICONV_EINVAL;
    1.38 +        default:
    1.39 +            return SDL_ICONV_ERROR;
    1.40 +        }
    1.41 +    }
    1.42 +    return retCode;
    1.43  }
    1.44  
    1.45  #else
    1.46 @@ -61,19 +62,20 @@
    1.47  #define UNKNOWN_ASCII	'?'
    1.48  #define UNKNOWN_UNICODE	0xFFFD
    1.49  
    1.50 -enum {
    1.51 -	ENCODING_UNKNOWN,
    1.52 -	ENCODING_ASCII,
    1.53 -	ENCODING_LATIN1,
    1.54 -	ENCODING_UTF8,
    1.55 -	ENCODING_UTF16,		/* Needs byte order marker */
    1.56 -	ENCODING_UTF16BE,
    1.57 -	ENCODING_UTF16LE,
    1.58 -	ENCODING_UTF32,		/* Needs byte order marker */
    1.59 -	ENCODING_UTF32BE,
    1.60 -	ENCODING_UTF32LE,
    1.61 -	ENCODING_UCS2,		/* Native byte order assumed */
    1.62 -	ENCODING_UCS4,		/* Native byte order assumed */
    1.63 +enum
    1.64 +{
    1.65 +    ENCODING_UNKNOWN,
    1.66 +    ENCODING_ASCII,
    1.67 +    ENCODING_LATIN1,
    1.68 +    ENCODING_UTF8,
    1.69 +    ENCODING_UTF16,             /* Needs byte order marker */
    1.70 +    ENCODING_UTF16BE,
    1.71 +    ENCODING_UTF16LE,
    1.72 +    ENCODING_UTF32,             /* Needs byte order marker */
    1.73 +    ENCODING_UTF32BE,
    1.74 +    ENCODING_UTF32LE,
    1.75 +    ENCODING_UCS2,              /* Native byte order assumed */
    1.76 +    ENCODING_UCS4,              /* Native byte order assumed */
    1.77  };
    1.78  #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    1.79  #define ENCODING_UTF16NATIVE	ENCODING_UTF16BE
    1.80 @@ -85,730 +87,731 @@
    1.81  
    1.82  struct _SDL_iconv_t
    1.83  {
    1.84 -	int src_fmt;
    1.85 -	int dst_fmt;
    1.86 +    int src_fmt;
    1.87 +    int dst_fmt;
    1.88  };
    1.89  
    1.90 -static struct {
    1.91 -	const char *name;
    1.92 -	int format;
    1.93 +static struct
    1.94 +{
    1.95 +    const char *name;
    1.96 +    int format;
    1.97  } encodings[] = {
    1.98 -	{ "ASCII",	ENCODING_ASCII },
    1.99 -	{ "US-ASCII",	ENCODING_ASCII },
   1.100 -	{ "LATIN1",	ENCODING_LATIN1 },
   1.101 -	{ "ISO-8859-1",	ENCODING_LATIN1 },
   1.102 -	{ "UTF8",	ENCODING_UTF8 },
   1.103 -	{ "UTF-8",	ENCODING_UTF8 },
   1.104 -	{ "UTF16",	ENCODING_UTF16 },
   1.105 -	{ "UTF-16",	ENCODING_UTF16 },
   1.106 -	{ "UTF16BE",	ENCODING_UTF16BE },
   1.107 -	{ "UTF-16BE",	ENCODING_UTF16BE },
   1.108 -	{ "UTF16LE",	ENCODING_UTF16LE },
   1.109 -	{ "UTF-16LE",	ENCODING_UTF16LE },
   1.110 -	{ "UTF32",	ENCODING_UTF32 },
   1.111 -	{ "UTF-32",	ENCODING_UTF32 },
   1.112 -	{ "UTF32BE",	ENCODING_UTF32BE },
   1.113 -	{ "UTF-32BE",	ENCODING_UTF32BE },
   1.114 -	{ "UTF32LE",	ENCODING_UTF32LE },
   1.115 -	{ "UTF-32LE",	ENCODING_UTF32LE },
   1.116 -	{ "UCS2",	ENCODING_UCS2 },
   1.117 -	{ "UCS-2",	ENCODING_UCS2 },
   1.118 -	{ "UCS4",	ENCODING_UCS4 },
   1.119 -	{ "UCS-4",	ENCODING_UCS4 },
   1.120 -};
   1.121 +    {
   1.122 +    "ASCII", ENCODING_ASCII}, {
   1.123 +    "US-ASCII", ENCODING_ASCII}, {
   1.124 +    "LATIN1", ENCODING_LATIN1}, {
   1.125 +    "ISO-8859-1", ENCODING_LATIN1}, {
   1.126 +    "UTF8", ENCODING_UTF8}, {
   1.127 +    "UTF-8", ENCODING_UTF8}, {
   1.128 +    "UTF16", ENCODING_UTF16}, {
   1.129 +    "UTF-16", ENCODING_UTF16}, {
   1.130 +    "UTF16BE", ENCODING_UTF16BE}, {
   1.131 +    "UTF-16BE", ENCODING_UTF16BE}, {
   1.132 +    "UTF16LE", ENCODING_UTF16LE}, {
   1.133 +    "UTF-16LE", ENCODING_UTF16LE}, {
   1.134 +    "UTF32", ENCODING_UTF32}, {
   1.135 +    "UTF-32", ENCODING_UTF32}, {
   1.136 +    "UTF32BE", ENCODING_UTF32BE}, {
   1.137 +    "UTF-32BE", ENCODING_UTF32BE}, {
   1.138 +    "UTF32LE", ENCODING_UTF32LE}, {
   1.139 +    "UTF-32LE", ENCODING_UTF32LE}, {
   1.140 +    "UCS2", ENCODING_UCS2}, {
   1.141 +    "UCS-2", ENCODING_UCS2}, {
   1.142 +    "UCS4", ENCODING_UCS4}, {
   1.143 +"UCS-4", ENCODING_UCS4},};
   1.144  
   1.145 -SDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode)
   1.146 +SDL_iconv_t
   1.147 +SDL_iconv_open(const char *tocode, const char *fromcode)
   1.148  {
   1.149 -	int src_fmt = ENCODING_UNKNOWN;
   1.150 -	int dst_fmt = ENCODING_UNKNOWN;
   1.151 -	int i;
   1.152 +    int src_fmt = ENCODING_UNKNOWN;
   1.153 +    int dst_fmt = ENCODING_UNKNOWN;
   1.154 +    int i;
   1.155  
   1.156 -	for ( i = 0; i < SDL_arraysize(encodings); ++i ) {
   1.157 -		if ( SDL_strcasecmp(fromcode, encodings[i].name) == 0 ) {
   1.158 -			src_fmt = encodings[i].format;
   1.159 -			if ( dst_fmt != ENCODING_UNKNOWN ) {
   1.160 -				break;
   1.161 -			}
   1.162 -		}
   1.163 -		if ( SDL_strcasecmp(tocode, encodings[i].name) == 0 ) {
   1.164 -			dst_fmt = encodings[i].format;
   1.165 -			if ( src_fmt != ENCODING_UNKNOWN ) {
   1.166 -				break;
   1.167 -			}
   1.168 -		}
   1.169 -	}
   1.170 -	if ( src_fmt != ENCODING_UNKNOWN && dst_fmt != ENCODING_UNKNOWN ) {
   1.171 -		SDL_iconv_t cd = (SDL_iconv_t)SDL_malloc(sizeof(*cd));
   1.172 -		if ( cd ) {
   1.173 -			cd->src_fmt = src_fmt;
   1.174 -			cd->dst_fmt = dst_fmt;
   1.175 -			return cd;
   1.176 -		}
   1.177 -	}
   1.178 -	return (SDL_iconv_t)-1;
   1.179 +    for (i = 0; i < SDL_arraysize(encodings); ++i) {
   1.180 +        if (SDL_strcasecmp(fromcode, encodings[i].name) == 0) {
   1.181 +            src_fmt = encodings[i].format;
   1.182 +            if (dst_fmt != ENCODING_UNKNOWN) {
   1.183 +                break;
   1.184 +            }
   1.185 +        }
   1.186 +        if (SDL_strcasecmp(tocode, encodings[i].name) == 0) {
   1.187 +            dst_fmt = encodings[i].format;
   1.188 +            if (src_fmt != ENCODING_UNKNOWN) {
   1.189 +                break;
   1.190 +            }
   1.191 +        }
   1.192 +    }
   1.193 +    if (src_fmt != ENCODING_UNKNOWN && dst_fmt != ENCODING_UNKNOWN) {
   1.194 +        SDL_iconv_t cd = (SDL_iconv_t) SDL_malloc(sizeof(*cd));
   1.195 +        if (cd) {
   1.196 +            cd->src_fmt = src_fmt;
   1.197 +            cd->dst_fmt = dst_fmt;
   1.198 +            return cd;
   1.199 +        }
   1.200 +    }
   1.201 +    return (SDL_iconv_t) - 1;
   1.202  }
   1.203  
   1.204 -size_t SDL_iconv(SDL_iconv_t cd,
   1.205 -                 char **inbuf, size_t *inbytesleft,
   1.206 -                 char **outbuf, size_t *outbytesleft)
   1.207 +size_t
   1.208 +SDL_iconv(SDL_iconv_t cd,
   1.209 +          char **inbuf, size_t * inbytesleft,
   1.210 +          char **outbuf, size_t * outbytesleft)
   1.211  {
   1.212 -	/* For simplicity, we'll convert everything to and from UCS-4 */
   1.213 -	char *src, *dst;
   1.214 -	size_t srclen, dstlen;
   1.215 -	Uint32 ch = 0;
   1.216 -	size_t total;
   1.217 +    /* For simplicity, we'll convert everything to and from UCS-4 */
   1.218 +    char *src, *dst;
   1.219 +    size_t srclen, dstlen;
   1.220 +    Uint32 ch;
   1.221 +    size_t total;
   1.222  
   1.223 -	if ( !inbuf || !*inbuf ) {
   1.224 -		/* Reset the context */
   1.225 -		return 0;
   1.226 -	}
   1.227 -	if ( !outbuf || !*outbuf || !outbytesleft || !*outbytesleft ) {
   1.228 -		return SDL_ICONV_E2BIG;
   1.229 -	}
   1.230 -	src = *inbuf;
   1.231 -	srclen = (inbytesleft ? *inbytesleft : 0);
   1.232 -	dst = *outbuf;
   1.233 -	dstlen = *outbytesleft;
   1.234 +    if (!inbuf || !*inbuf) {
   1.235 +        /* Reset the context */
   1.236 +        return 0;
   1.237 +    }
   1.238 +    if (!outbuf || !*outbuf || !outbytesleft || !*outbytesleft) {
   1.239 +        return SDL_ICONV_E2BIG;
   1.240 +    }
   1.241 +    src = *inbuf;
   1.242 +    srclen = (inbytesleft ? *inbytesleft : 0);
   1.243 +    dst = *outbuf;
   1.244 +    dstlen = *outbytesleft;
   1.245  
   1.246 -	switch ( cd->src_fmt ) {
   1.247 -	    case ENCODING_UTF16:
   1.248 -		/* Scan for a byte order marker */
   1.249 -		{
   1.250 -			Uint8 *p = (Uint8 *)src;
   1.251 -			size_t n = srclen / 2;
   1.252 -			while ( n ) {
   1.253 -				if ( p[0] == 0xFF && p[1] == 0xFE ) {
   1.254 -					cd->src_fmt = ENCODING_UTF16BE;
   1.255 -					break;
   1.256 -				} else if ( p[0] == 0xFE && p[1] == 0xFF ) {
   1.257 -					cd->src_fmt = ENCODING_UTF16LE;
   1.258 -					break;
   1.259 -				}
   1.260 -				p += 2;
   1.261 -				--n;
   1.262 -			}
   1.263 -			if ( n == 0 ) {
   1.264 -				/* We can't tell, default to host order */
   1.265 -				cd->src_fmt = ENCODING_UTF16NATIVE;
   1.266 -			}
   1.267 -		}
   1.268 -		break;
   1.269 -	    case ENCODING_UTF32:
   1.270 -		/* Scan for a byte order marker */
   1.271 -		{
   1.272 -			Uint8 *p = (Uint8 *)src;
   1.273 -			size_t n = srclen / 4;
   1.274 -			while ( n ) {
   1.275 -				if ( p[0] == 0xFF && p[1] == 0xFE &&
   1.276 -				     p[2] == 0x00 && p[3] == 0x00 ) {
   1.277 -					cd->src_fmt = ENCODING_UTF32BE;
   1.278 -					break;
   1.279 -				} else if ( p[0] == 0x00 && p[1] == 0x00 &&
   1.280 -				            p[2] == 0xFE && p[3] == 0xFF ) {
   1.281 -					cd->src_fmt = ENCODING_UTF32LE;
   1.282 -					break;
   1.283 -				}
   1.284 -				p += 4;
   1.285 -				--n;
   1.286 -			}
   1.287 -			if ( n == 0 ) {
   1.288 -				/* We can't tell, default to host order */
   1.289 -				cd->src_fmt = ENCODING_UTF32NATIVE;
   1.290 -			}
   1.291 -		}
   1.292 -		break;
   1.293 -	}
   1.294 +    switch (cd->src_fmt) {
   1.295 +    case ENCODING_UTF16:
   1.296 +        /* Scan for a byte order marker */
   1.297 +        {
   1.298 +            Uint8 *p = (Uint8 *) src;
   1.299 +            size_t n = srclen / 2;
   1.300 +            while (n) {
   1.301 +                if (p[0] == 0xFF && p[1] == 0xFE) {
   1.302 +                    cd->src_fmt = ENCODING_UTF16BE;
   1.303 +                    break;
   1.304 +                } else if (p[0] == 0xFE && p[1] == 0xFF) {
   1.305 +                    cd->src_fmt = ENCODING_UTF16LE;
   1.306 +                    break;
   1.307 +                }
   1.308 +                p += 2;
   1.309 +                --n;
   1.310 +            }
   1.311 +            if (n == 0) {
   1.312 +                /* We can't tell, default to host order */
   1.313 +                cd->src_fmt = ENCODING_UTF16NATIVE;
   1.314 +            }
   1.315 +        }
   1.316 +        break;
   1.317 +    case ENCODING_UTF32:
   1.318 +        /* Scan for a byte order marker */
   1.319 +        {
   1.320 +            Uint8 *p = (Uint8 *) src;
   1.321 +            size_t n = srclen / 4;
   1.322 +            while (n) {
   1.323 +                if (p[0] == 0xFF && p[1] == 0xFE &&
   1.324 +                    p[2] == 0x00 && p[3] == 0x00) {
   1.325 +                    cd->src_fmt = ENCODING_UTF32BE;
   1.326 +                    break;
   1.327 +                } else if (p[0] == 0x00 && p[1] == 0x00 &&
   1.328 +                           p[2] == 0xFE && p[3] == 0xFF) {
   1.329 +                    cd->src_fmt = ENCODING_UTF32LE;
   1.330 +                    break;
   1.331 +                }
   1.332 +                p += 4;
   1.333 +                --n;
   1.334 +            }
   1.335 +            if (n == 0) {
   1.336 +                /* We can't tell, default to host order */
   1.337 +                cd->src_fmt = ENCODING_UTF32NATIVE;
   1.338 +            }
   1.339 +        }
   1.340 +        break;
   1.341 +    }
   1.342  
   1.343 -	switch ( cd->dst_fmt ) {
   1.344 -	    case ENCODING_UTF16:
   1.345 -		/* Default to host order, need to add byte order marker */
   1.346 -		if ( dstlen < 2 ) {
   1.347 -			return SDL_ICONV_E2BIG;
   1.348 -		}
   1.349 -		*(Uint16 *)dst = UNICODE_BOM;
   1.350 -		dst += 2;
   1.351 -		dstlen -= 2;
   1.352 -		cd->dst_fmt = ENCODING_UTF16NATIVE;
   1.353 -		break;
   1.354 -	    case ENCODING_UTF32:
   1.355 -		/* Default to host order, need to add byte order marker */
   1.356 -		if ( dstlen < 4 ) {
   1.357 -			return SDL_ICONV_E2BIG;
   1.358 -		}
   1.359 -		*(Uint32 *)dst = UNICODE_BOM;
   1.360 -		dst += 4;
   1.361 -		dstlen -= 4;
   1.362 -		cd->dst_fmt = ENCODING_UTF32NATIVE;
   1.363 -		break;
   1.364 -	}
   1.365 +    switch (cd->dst_fmt) {
   1.366 +    case ENCODING_UTF16:
   1.367 +        /* Default to host order, need to add byte order marker */
   1.368 +        if (dstlen < 2) {
   1.369 +            return SDL_ICONV_E2BIG;
   1.370 +        }
   1.371 +        *(Uint16 *) dst = UNICODE_BOM;
   1.372 +        dst += 2;
   1.373 +        dstlen -= 2;
   1.374 +        cd->dst_fmt = ENCODING_UTF16NATIVE;
   1.375 +        break;
   1.376 +    case ENCODING_UTF32:
   1.377 +        /* Default to host order, need to add byte order marker */
   1.378 +        if (dstlen < 4) {
   1.379 +            return SDL_ICONV_E2BIG;
   1.380 +        }
   1.381 +        *(Uint32 *) dst = UNICODE_BOM;
   1.382 +        dst += 4;
   1.383 +        dstlen -= 4;
   1.384 +        cd->dst_fmt = ENCODING_UTF32NATIVE;
   1.385 +        break;
   1.386 +    }
   1.387  
   1.388 -	total = 0;
   1.389 -	while ( srclen > 0 ) {
   1.390 -		/* Decode a character */
   1.391 -		switch ( cd->src_fmt ) {
   1.392 -		    case ENCODING_ASCII:
   1.393 -			{
   1.394 -				Uint8 *p = (Uint8 *)src;
   1.395 -				ch = (Uint32)(p[0] & 0x7F);
   1.396 -				++src;
   1.397 -				--srclen;
   1.398 -			}
   1.399 -			break;
   1.400 -		    case ENCODING_LATIN1:
   1.401 -			{
   1.402 -				Uint8 *p = (Uint8 *)src;
   1.403 -				ch = (Uint32)p[0];
   1.404 -				++src;
   1.405 -				--srclen;
   1.406 -			}
   1.407 -			break;
   1.408 -		    case ENCODING_UTF8: /* RFC 3629 */
   1.409 -			{
   1.410 -				Uint8 *p = (Uint8 *)src;
   1.411 -				size_t left = 0;
   1.412 -				SDL_bool overlong = SDL_FALSE;
   1.413 -				if ( p[0] >= 0xFC ) {
   1.414 -					if ( (p[0] & 0xFE) != 0xFC ) {
   1.415 -						/* Skip illegal sequences
   1.416 -						return SDL_ICONV_EILSEQ;
   1.417 -						*/
   1.418 -						ch = UNKNOWN_UNICODE;
   1.419 -					} else {
   1.420 -						if ( p[0] == 0xFC ) {
   1.421 -							overlong = SDL_TRUE;
   1.422 -						}
   1.423 -						ch = (Uint32)(p[0] & 0x01);
   1.424 -						left = 5;
   1.425 -					}
   1.426 -				} else if ( p[0] >= 0xF8 ) {
   1.427 -					if ( (p[0] & 0xFC) != 0xF8 ) {
   1.428 -						/* Skip illegal sequences
   1.429 -						return SDL_ICONV_EILSEQ;
   1.430 -						*/
   1.431 -						ch = UNKNOWN_UNICODE;
   1.432 -					} else {
   1.433 -						if ( p[0] == 0xF8 ) {
   1.434 -							overlong = SDL_TRUE;
   1.435 -						}
   1.436 -						ch = (Uint32)(p[0] & 0x03);
   1.437 -						left = 4;
   1.438 -					}
   1.439 -				} else if ( p[0] >= 0xF0 ) {
   1.440 -					if ( (p[0] & 0xF8) != 0xF0 ) {
   1.441 -						/* Skip illegal sequences
   1.442 -						return SDL_ICONV_EILSEQ;
   1.443 -						*/
   1.444 -						ch = UNKNOWN_UNICODE;
   1.445 -					} else {
   1.446 -						if ( p[0] == 0xF0 ) {
   1.447 -							overlong = SDL_TRUE;
   1.448 -						}
   1.449 -						ch = (Uint32)(p[0] & 0x07);
   1.450 -						left = 3;
   1.451 -					}
   1.452 -				} else if ( p[0] >= 0xE0 ) {
   1.453 -					if ( (p[0] & 0xF0) != 0xE0 ) {
   1.454 -						/* Skip illegal sequences
   1.455 -						return SDL_ICONV_EILSEQ;
   1.456 -						*/
   1.457 -						ch = UNKNOWN_UNICODE;
   1.458 -					} else {
   1.459 -						if ( p[0] == 0xE0 ) {
   1.460 -							overlong = SDL_TRUE;
   1.461 -						}
   1.462 -						ch = (Uint32)(p[0] & 0x0F);
   1.463 -						left = 2;
   1.464 -					}
   1.465 -				} else if ( p[0] >= 0xC0 ) {
   1.466 -					if ( (p[0] & 0xE0) != 0xC0 ) {
   1.467 -						/* Skip illegal sequences
   1.468 -						return SDL_ICONV_EILSEQ;
   1.469 -						*/
   1.470 -						ch = UNKNOWN_UNICODE;
   1.471 -					} else {
   1.472 -						if ( (p[0] & 0xCE) == 0xC0 ) {
   1.473 -							overlong = SDL_TRUE;
   1.474 -						}
   1.475 -						ch = (Uint32)(p[0] & 0x1F);
   1.476 -						left = 1;
   1.477 -					}
   1.478 -				} else {
   1.479 -					if ( (p[0] & 0x80) != 0x00 ) {
   1.480 -						/* Skip illegal sequences
   1.481 -						return SDL_ICONV_EILSEQ;
   1.482 -						*/
   1.483 -						ch = UNKNOWN_UNICODE;
   1.484 -					} else {
   1.485 -						ch = (Uint32)p[0];
   1.486 -					}
   1.487 -				}
   1.488 -				++src;
   1.489 -				--srclen;
   1.490 -				if ( srclen < left ) {
   1.491 -					return SDL_ICONV_EINVAL;
   1.492 -				}
   1.493 -				while ( left-- ) {
   1.494 -					++p;
   1.495 -					if ( (p[0] & 0xC0) != 0x80 ) {
   1.496 -						/* Skip illegal sequences
   1.497 -						return SDL_ICONV_EILSEQ;
   1.498 -						*/
   1.499 -						ch = UNKNOWN_UNICODE;
   1.500 -						break;
   1.501 -					}
   1.502 -					ch <<= 6;
   1.503 -					ch |= (p[0] & 0x3F);
   1.504 -					++src;
   1.505 -					--srclen;
   1.506 -				}
   1.507 -				if ( overlong ) {
   1.508 -					/* Potential security risk
   1.509 -					return SDL_ICONV_EILSEQ;
   1.510 -					*/
   1.511 -					ch = UNKNOWN_UNICODE;
   1.512 -				}
   1.513 -				if ( (ch >= 0xD800 && ch <= 0xDFFF) ||
   1.514 -				     (ch == 0xFFFE || ch == 0xFFFF) ||
   1.515 -				     ch > 0x10FFFF ) {
   1.516 -					/* Skip illegal sequences
   1.517 -					return SDL_ICONV_EILSEQ;
   1.518 -					*/
   1.519 -					ch = UNKNOWN_UNICODE;
   1.520 -				}
   1.521 -			}
   1.522 -			break;
   1.523 -		    case ENCODING_UTF16BE: /* RFC 2781 */
   1.524 -			{
   1.525 -				Uint8 *p = (Uint8 *)src;
   1.526 -				Uint16 W1, W2;
   1.527 -				if ( srclen < 2 ) {
   1.528 -					return SDL_ICONV_EINVAL;
   1.529 -				}
   1.530 -				W1 = ((Uint16)p[0] << 8) |
   1.531 -				      (Uint16)p[1];
   1.532 -				src += 2;
   1.533 -				srclen -= 2;
   1.534 -				if ( W1 < 0xD800 || W1 > 0xDFFF ) {
   1.535 -					ch = (Uint32)W1;
   1.536 -					break;
   1.537 -				}
   1.538 -				if ( W1 > 0xDBFF ) {
   1.539 -					/* Skip illegal sequences
   1.540 -					return SDL_ICONV_EILSEQ;
   1.541 -					*/
   1.542 -					ch = UNKNOWN_UNICODE;
   1.543 -					break;
   1.544 -				}
   1.545 -				if ( srclen < 2 ) {
   1.546 -					return SDL_ICONV_EINVAL;
   1.547 -				}
   1.548 -				p = (Uint8 *)src;
   1.549 -				W2 = ((Uint16)p[0] << 8) |
   1.550 -				      (Uint16)p[1];
   1.551 -				src += 2;
   1.552 -				srclen -= 2;
   1.553 -				if ( W2 < 0xDC00 || W2 > 0xDFFF ) {
   1.554 -					/* Skip illegal sequences
   1.555 -					return SDL_ICONV_EILSEQ;
   1.556 -					*/
   1.557 -					ch = UNKNOWN_UNICODE;
   1.558 -					break;
   1.559 -				}
   1.560 -				ch = (((Uint32)(W1 & 0x3FF) << 10) |
   1.561 -				      (Uint32)(W2 & 0x3FF)) + 0x10000;
   1.562 -			}
   1.563 -			break;
   1.564 -		    case ENCODING_UTF16LE: /* RFC 2781 */
   1.565 -			{
   1.566 -				Uint8 *p = (Uint8 *)src;
   1.567 -				Uint16 W1, W2;
   1.568 -				if ( srclen < 2 ) {
   1.569 -					return SDL_ICONV_EINVAL;
   1.570 -				}
   1.571 -				W1 = ((Uint16)p[1] << 8) |
   1.572 -				      (Uint16)p[0];
   1.573 -				src += 2;
   1.574 -				srclen -= 2;
   1.575 -				if ( W1 < 0xD800 || W1 > 0xDFFF ) {
   1.576 -					ch = (Uint32)W1;
   1.577 -					break;
   1.578 -				}
   1.579 -				if ( W1 > 0xDBFF ) {
   1.580 -					/* Skip illegal sequences
   1.581 -					return SDL_ICONV_EILSEQ;
   1.582 -					*/
   1.583 -					ch = UNKNOWN_UNICODE;
   1.584 -					break;
   1.585 -				}
   1.586 -				if ( srclen < 2 ) {
   1.587 -					return SDL_ICONV_EINVAL;
   1.588 -				}
   1.589 -				p = (Uint8 *)src;
   1.590 -				W2 = ((Uint16)p[1] << 8) |
   1.591 -				      (Uint16)p[0];
   1.592 -				src += 2;
   1.593 -				srclen -= 2;
   1.594 -				if ( W2 < 0xDC00 || W2 > 0xDFFF ) {
   1.595 -					/* Skip illegal sequences
   1.596 -					return SDL_ICONV_EILSEQ;
   1.597 -					*/
   1.598 -					ch = UNKNOWN_UNICODE;
   1.599 -					break;
   1.600 -				}
   1.601 -				ch = (((Uint32)(W1 & 0x3FF) << 10) |
   1.602 -				      (Uint32)(W2 & 0x3FF)) + 0x10000;
   1.603 -			}
   1.604 -			break;
   1.605 -		    case ENCODING_UTF32BE:
   1.606 -			{
   1.607 -				Uint8 *p = (Uint8 *)src;
   1.608 -				if ( srclen < 4 ) {
   1.609 -					return SDL_ICONV_EINVAL;
   1.610 -				}
   1.611 -				ch = ((Uint32)p[0] << 24) |
   1.612 -				     ((Uint32)p[1] << 16) |
   1.613 -				     ((Uint32)p[2] << 8) |
   1.614 -				      (Uint32)p[3];
   1.615 -				src += 4;
   1.616 -				srclen -= 4;
   1.617 -			}
   1.618 -			break;
   1.619 -		    case ENCODING_UTF32LE:
   1.620 -			{
   1.621 -				Uint8 *p = (Uint8 *)src;
   1.622 -				if ( srclen < 4 ) {
   1.623 -					return SDL_ICONV_EINVAL;
   1.624 -				}
   1.625 -				ch = ((Uint32)p[3] << 24) |
   1.626 -				     ((Uint32)p[2] << 16) |
   1.627 -				     ((Uint32)p[1] << 8) |
   1.628 -				      (Uint32)p[0];
   1.629 -				src += 4;
   1.630 -				srclen -= 4;
   1.631 -			}
   1.632 -			break;
   1.633 -		    case ENCODING_UCS2:
   1.634 -			{
   1.635 -				Uint16 *p = (Uint16 *)src;
   1.636 -				if ( srclen < 2 ) {
   1.637 -					return SDL_ICONV_EINVAL;
   1.638 -				}
   1.639 -				ch = *p;
   1.640 -				src += 2;
   1.641 -				srclen -= 2;
   1.642 -			}
   1.643 -			break;
   1.644 -		    case ENCODING_UCS4:
   1.645 -			{
   1.646 -				Uint32 *p = (Uint32 *)src;
   1.647 -				if ( srclen < 4 ) {
   1.648 -					return SDL_ICONV_EINVAL;
   1.649 -				}
   1.650 -				ch = *p;
   1.651 -				src += 4;
   1.652 -				srclen -= 4;
   1.653 -			}
   1.654 -			break;
   1.655 -		}
   1.656 +    total = 0;
   1.657 +    while (srclen > 0) {
   1.658 +        /* Decode a character */
   1.659 +        switch (cd->src_fmt) {
   1.660 +        case ENCODING_ASCII:
   1.661 +            {
   1.662 +                Uint8 *p = (Uint8 *) src;
   1.663 +                ch = (Uint32) (p[0] & 0x7F);
   1.664 +                ++src;
   1.665 +                --srclen;
   1.666 +            }
   1.667 +            break;
   1.668 +        case ENCODING_LATIN1:
   1.669 +            {
   1.670 +                Uint8 *p = (Uint8 *) src;
   1.671 +                ch = (Uint32) p[0];
   1.672 +                ++src;
   1.673 +                --srclen;
   1.674 +            }
   1.675 +            break;
   1.676 +        case ENCODING_UTF8:    /* RFC 3629 */
   1.677 +            {
   1.678 +                Uint8 *p = (Uint8 *) src;
   1.679 +                size_t left = 0;
   1.680 +                SDL_bool overlong = SDL_FALSE;
   1.681 +                if (p[0] >= 0xFC) {
   1.682 +                    if ((p[0] & 0xFE) != 0xFC) {
   1.683 +                        /* Skip illegal sequences
   1.684 +                           return SDL_ICONV_EILSEQ;
   1.685 +                         */
   1.686 +                        ch = UNKNOWN_UNICODE;
   1.687 +                    } else {
   1.688 +                        if (p[0] == 0xFC) {
   1.689 +                            overlong = SDL_TRUE;
   1.690 +                        }
   1.691 +                        ch = (Uint32) (p[0] & 0x01);
   1.692 +                        left = 5;
   1.693 +                    }
   1.694 +                } else if (p[0] >= 0xF8) {
   1.695 +                    if ((p[0] & 0xFC) != 0xF8) {
   1.696 +                        /* Skip illegal sequences
   1.697 +                           return SDL_ICONV_EILSEQ;
   1.698 +                         */
   1.699 +                        ch = UNKNOWN_UNICODE;
   1.700 +                    } else {
   1.701 +                        if (p[0] == 0xF8) {
   1.702 +                            overlong = SDL_TRUE;
   1.703 +                        }
   1.704 +                        ch = (Uint32) (p[0] & 0x03);
   1.705 +                        left = 4;
   1.706 +                    }
   1.707 +                } else if (p[0] >= 0xF0) {
   1.708 +                    if ((p[0] & 0xF8) != 0xF0) {
   1.709 +                        /* Skip illegal sequences
   1.710 +                           return SDL_ICONV_EILSEQ;
   1.711 +                         */
   1.712 +                        ch = UNKNOWN_UNICODE;
   1.713 +                    } else {
   1.714 +                        if (p[0] == 0xF0) {
   1.715 +                            overlong = SDL_TRUE;
   1.716 +                        }
   1.717 +                        ch = (Uint32) (p[0] & 0x07);
   1.718 +                        left = 3;
   1.719 +                    }
   1.720 +                } else if (p[0] >= 0xE0) {
   1.721 +                    if ((p[0] & 0xF0) != 0xE0) {
   1.722 +                        /* Skip illegal sequences
   1.723 +                           return SDL_ICONV_EILSEQ;
   1.724 +                         */
   1.725 +                        ch = UNKNOWN_UNICODE;
   1.726 +                    } else {
   1.727 +                        if (p[0] == 0xE0) {
   1.728 +                            overlong = SDL_TRUE;
   1.729 +                        }
   1.730 +                        ch = (Uint32) (p[0] & 0x0F);
   1.731 +                        left = 2;
   1.732 +                    }
   1.733 +                } else if (p[0] >= 0xC0) {
   1.734 +                    if ((p[0] & 0xE0) != 0xC0) {
   1.735 +                        /* Skip illegal sequences
   1.736 +                           return SDL_ICONV_EILSEQ;
   1.737 +                         */
   1.738 +                        ch = UNKNOWN_UNICODE;
   1.739 +                    } else {
   1.740 +                        if ((p[0] & 0xCE) == 0xC0) {
   1.741 +                            overlong = SDL_TRUE;
   1.742 +                        }
   1.743 +                        ch = (Uint32) (p[0] & 0x1F);
   1.744 +                        left = 1;
   1.745 +                    }
   1.746 +                } else {
   1.747 +                    if ((p[0] & 0x80) != 0x00) {
   1.748 +                        /* Skip illegal sequences
   1.749 +                           return SDL_ICONV_EILSEQ;
   1.750 +                         */
   1.751 +                        ch = UNKNOWN_UNICODE;
   1.752 +                    } else {
   1.753 +                        ch = (Uint32) p[0];
   1.754 +                    }
   1.755 +                }
   1.756 +                ++src;
   1.757 +                --srclen;
   1.758 +                if (srclen < left) {
   1.759 +                    return SDL_ICONV_EINVAL;
   1.760 +                }
   1.761 +                while (left--) {
   1.762 +                    ++p;
   1.763 +                    if ((p[0] & 0xC0) != 0x80) {
   1.764 +                        /* Skip illegal sequences
   1.765 +                           return SDL_ICONV_EILSEQ;
   1.766 +                         */
   1.767 +                        ch = UNKNOWN_UNICODE;
   1.768 +                        break;
   1.769 +                    }
   1.770 +                    ch <<= 6;
   1.771 +                    ch |= (p[0] & 0x3F);
   1.772 +                    ++src;
   1.773 +                    --srclen;
   1.774 +                }
   1.775 +                if (overlong) {
   1.776 +                    /* Potential security risk
   1.777 +                       return SDL_ICONV_EILSEQ;
   1.778 +                     */
   1.779 +                    ch = UNKNOWN_UNICODE;
   1.780 +                }
   1.781 +                if ((ch >= 0xD800 && ch <= 0xDFFF) ||
   1.782 +                    (ch == 0xFFFE || ch == 0xFFFF) || ch > 0x10FFFF) {
   1.783 +                    /* Skip illegal sequences
   1.784 +                       return SDL_ICONV_EILSEQ;
   1.785 +                     */
   1.786 +                    ch = UNKNOWN_UNICODE;
   1.787 +                }
   1.788 +            }
   1.789 +            break;
   1.790 +        case ENCODING_UTF16BE: /* RFC 2781 */
   1.791 +            {
   1.792 +                Uint8 *p = (Uint8 *) src;
   1.793 +                Uint16 W1, W2;
   1.794 +                if (srclen < 2) {
   1.795 +                    return SDL_ICONV_EINVAL;
   1.796 +                }
   1.797 +                W1 = ((Uint16) p[0] << 8) | (Uint16) p[1];
   1.798 +                src += 2;
   1.799 +                srclen -= 2;
   1.800 +                if (W1 < 0xD800 || W1 > 0xDFFF) {
   1.801 +                    ch = (Uint32) W1;
   1.802 +                    break;
   1.803 +                }
   1.804 +                if (W1 > 0xDBFF) {
   1.805 +                    /* Skip illegal sequences
   1.806 +                       return SDL_ICONV_EILSEQ;
   1.807 +                     */
   1.808 +                    ch = UNKNOWN_UNICODE;
   1.809 +                    break;
   1.810 +                }
   1.811 +                if (srclen < 2) {
   1.812 +                    return SDL_ICONV_EINVAL;
   1.813 +                }
   1.814 +                p = (Uint8 *) src;
   1.815 +                W2 = ((Uint16) p[0] << 8) | (Uint16) p[1];
   1.816 +                src += 2;
   1.817 +                srclen -= 2;
   1.818 +                if (W2 < 0xDC00 || W2 > 0xDFFF) {
   1.819 +                    /* Skip illegal sequences
   1.820 +                       return SDL_ICONV_EILSEQ;
   1.821 +                     */
   1.822 +                    ch = UNKNOWN_UNICODE;
   1.823 +                    break;
   1.824 +                }
   1.825 +                ch = (((Uint32) (W1 & 0x3FF) << 10) |
   1.826 +                      (Uint32) (W2 & 0x3FF)) + 0x10000;
   1.827 +            }
   1.828 +            break;
   1.829 +        case ENCODING_UTF16LE: /* RFC 2781 */
   1.830 +            {
   1.831 +                Uint8 *p = (Uint8 *) src;
   1.832 +                Uint16 W1, W2;
   1.833 +                if (srclen < 2) {
   1.834 +                    return SDL_ICONV_EINVAL;
   1.835 +                }
   1.836 +                W1 = ((Uint16) p[1] << 8) | (Uint16) p[0];
   1.837 +                src += 2;
   1.838 +                srclen -= 2;
   1.839 +                if (W1 < 0xD800 || W1 > 0xDFFF) {
   1.840 +                    ch = (Uint32) W1;
   1.841 +                    break;
   1.842 +                }
   1.843 +                if (W1 > 0xDBFF) {
   1.844 +                    /* Skip illegal sequences
   1.845 +                       return SDL_ICONV_EILSEQ;
   1.846 +                     */
   1.847 +                    ch = UNKNOWN_UNICODE;
   1.848 +                    break;
   1.849 +                }
   1.850 +                if (srclen < 2) {
   1.851 +                    return SDL_ICONV_EINVAL;
   1.852 +                }
   1.853 +                p = (Uint8 *) src;
   1.854 +                W2 = ((Uint16) p[1] << 8) | (Uint16) p[0];
   1.855 +                src += 2;
   1.856 +                srclen -= 2;
   1.857 +                if (W2 < 0xDC00 || W2 > 0xDFFF) {
   1.858 +                    /* Skip illegal sequences
   1.859 +                       return SDL_ICONV_EILSEQ;
   1.860 +                     */
   1.861 +                    ch = UNKNOWN_UNICODE;
   1.862 +                    break;
   1.863 +                }
   1.864 +                ch = (((Uint32) (W1 & 0x3FF) << 10) |
   1.865 +                      (Uint32) (W2 & 0x3FF)) + 0x10000;
   1.866 +            }
   1.867 +            break;
   1.868 +        case ENCODING_UTF32BE:
   1.869 +            {
   1.870 +                Uint8 *p = (Uint8 *) src;
   1.871 +                if (srclen < 4) {
   1.872 +                    return SDL_ICONV_EINVAL;
   1.873 +                }
   1.874 +                ch = ((Uint32) p[0] << 24) |
   1.875 +                    ((Uint32) p[1] << 16) |
   1.876 +                    ((Uint32) p[2] << 8) | (Uint32) p[3];
   1.877 +                src += 4;
   1.878 +                srclen -= 4;
   1.879 +            }
   1.880 +            break;
   1.881 +        case ENCODING_UTF32LE:
   1.882 +            {
   1.883 +                Uint8 *p = (Uint8 *) src;
   1.884 +                if (srclen < 4) {
   1.885 +                    return SDL_ICONV_EINVAL;
   1.886 +                }
   1.887 +                ch = ((Uint32) p[3] << 24) |
   1.888 +                    ((Uint32) p[2] << 16) |
   1.889 +                    ((Uint32) p[1] << 8) | (Uint32) p[0];
   1.890 +                src += 4;
   1.891 +                srclen -= 4;
   1.892 +            }
   1.893 +            break;
   1.894 +        case ENCODING_UCS2:
   1.895 +            {
   1.896 +                Uint16 *p = (Uint16 *) src;
   1.897 +                if (srclen < 2) {
   1.898 +                    return SDL_ICONV_EINVAL;
   1.899 +                }
   1.900 +                ch = *p;
   1.901 +                src += 2;
   1.902 +                srclen -= 2;
   1.903 +            }
   1.904 +            break;
   1.905 +        case ENCODING_UCS4:
   1.906 +            {
   1.907 +                Uint32 *p = (Uint32 *) src;
   1.908 +                if (srclen < 4) {
   1.909 +                    return SDL_ICONV_EINVAL;
   1.910 +                }
   1.911 +                ch = *p;
   1.912 +                src += 4;
   1.913 +                srclen -= 4;
   1.914 +            }
   1.915 +            break;
   1.916 +        }
   1.917  
   1.918 -		/* Encode a character */
   1.919 -		switch ( cd->dst_fmt ) {
   1.920 -		    case ENCODING_ASCII:
   1.921 -			{
   1.922 -				Uint8 *p = (Uint8 *)dst;
   1.923 -				if ( dstlen < 1 ) {
   1.924 -					return SDL_ICONV_E2BIG;
   1.925 -				}
   1.926 -				if ( ch > 0x7F ) {
   1.927 -					*p = UNKNOWN_ASCII;
   1.928 -				} else {
   1.929 -					*p = (Uint8)ch;
   1.930 -				}
   1.931 -				++dst;
   1.932 -				--dstlen;
   1.933 -			}
   1.934 -			break;
   1.935 -		    case ENCODING_LATIN1:
   1.936 -			{
   1.937 -				Uint8 *p = (Uint8 *)dst;
   1.938 -				if ( dstlen < 1 ) {
   1.939 -					return SDL_ICONV_E2BIG;
   1.940 -				}
   1.941 -				if ( ch > 0xFF ) {
   1.942 -					*p = UNKNOWN_ASCII;
   1.943 -				} else {
   1.944 -					*p = (Uint8)ch;
   1.945 -				}
   1.946 -				++dst;
   1.947 -				--dstlen;
   1.948 -			}
   1.949 -			break;
   1.950 -		    case ENCODING_UTF8: /* RFC 3629 */
   1.951 -			{
   1.952 -				Uint8 *p = (Uint8 *)dst;
   1.953 -				if ( ch > 0x10FFFF ) {
   1.954 -					ch = UNKNOWN_UNICODE;
   1.955 -				}
   1.956 -				if ( ch <= 0x7F ) {
   1.957 -					if ( dstlen < 1 ) {
   1.958 -						return SDL_ICONV_E2BIG;
   1.959 -					}
   1.960 -					*p = (Uint8)ch;
   1.961 -					++dst;
   1.962 -					--dstlen;
   1.963 -				} else if ( ch <= 0x7FF ) {
   1.964 -					if ( dstlen < 2 ) {
   1.965 -						return SDL_ICONV_E2BIG;
   1.966 -					}
   1.967 -					p[0] = 0xC0 | (Uint8)((ch >> 6) & 0x1F);
   1.968 -					p[1] = 0x80 | (Uint8)(ch & 0x3F);
   1.969 -					dst += 2;
   1.970 -					dstlen -= 2;
   1.971 -				} else if ( ch <= 0xFFFF ) {
   1.972 -					if ( dstlen < 3 ) {
   1.973 -						return SDL_ICONV_E2BIG;
   1.974 -					}
   1.975 -					p[0] = 0xE0 | (Uint8)((ch >> 12) & 0x0F);
   1.976 -					p[1] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
   1.977 -					p[2] = 0x80 | (Uint8)(ch & 0x3F);
   1.978 -					dst += 3;
   1.979 -					dstlen -= 3;
   1.980 -				} else if ( ch <= 0x1FFFFF ) {
   1.981 -					if ( dstlen < 4 ) {
   1.982 -						return SDL_ICONV_E2BIG;
   1.983 -					}
   1.984 -					p[0] = 0xF0 | (Uint8)((ch >> 18) & 0x07);
   1.985 -					p[1] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
   1.986 -					p[2] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
   1.987 -					p[3] = 0x80 | (Uint8)(ch & 0x3F);
   1.988 -					dst += 4;
   1.989 -					dstlen -= 4;
   1.990 -				} else if ( ch <= 0x3FFFFFF ) {
   1.991 -					if ( dstlen < 5 ) {
   1.992 -						return SDL_ICONV_E2BIG;
   1.993 -					}
   1.994 -					p[0] = 0xF8 | (Uint8)((ch >> 24) & 0x03);
   1.995 -					p[1] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
   1.996 -					p[2] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
   1.997 -					p[3] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
   1.998 -					p[4] = 0x80 | (Uint8)(ch & 0x3F);
   1.999 -					dst += 5;
  1.1000 -					dstlen -= 5;
  1.1001 -				} else {
  1.1002 -					if ( dstlen < 6 ) {
  1.1003 -						return SDL_ICONV_E2BIG;
  1.1004 -					}
  1.1005 -					p[0] = 0xFC | (Uint8)((ch >> 30) & 0x01);
  1.1006 -					p[1] = 0x80 | (Uint8)((ch >> 24) & 0x3F);
  1.1007 -					p[2] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
  1.1008 -					p[3] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
  1.1009 -					p[4] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
  1.1010 -					p[5] = 0x80 | (Uint8)(ch & 0x3F);
  1.1011 -					dst += 6;
  1.1012 -					dstlen -= 6;
  1.1013 -				}
  1.1014 -			}
  1.1015 -			break;
  1.1016 -		    case ENCODING_UTF16BE: /* RFC 2781 */
  1.1017 -			{
  1.1018 -				Uint8 *p = (Uint8 *)dst;
  1.1019 -				if ( ch > 0x10FFFF ) {
  1.1020 -					ch = UNKNOWN_UNICODE;
  1.1021 -				}
  1.1022 -				if ( ch < 0x10000 ) {
  1.1023 -					if ( dstlen < 2 ) {
  1.1024 -						return SDL_ICONV_E2BIG;
  1.1025 -					}
  1.1026 -					p[0] = (Uint8)(ch >> 8);
  1.1027 -					p[1] = (Uint8)ch;
  1.1028 -					dst += 2;
  1.1029 -					dstlen -= 2;
  1.1030 -				} else {
  1.1031 -					Uint16 W1, W2;
  1.1032 -					if ( dstlen < 4 ) {
  1.1033 -						return SDL_ICONV_E2BIG;
  1.1034 -					}
  1.1035 -					ch = ch - 0x10000;
  1.1036 -					W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
  1.1037 -					W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
  1.1038 -					p[0] = (Uint8)(W1 >> 8);
  1.1039 -					p[1] = (Uint8)W1;
  1.1040 -					p[2] = (Uint8)(W2 >> 8);
  1.1041 -					p[3] = (Uint8)W2;
  1.1042 -					dst += 4;
  1.1043 -					dstlen -= 4;
  1.1044 -				}
  1.1045 -			}
  1.1046 -			break;
  1.1047 -		    case ENCODING_UTF16LE: /* RFC 2781 */
  1.1048 -			{
  1.1049 -				Uint8 *p = (Uint8 *)dst;
  1.1050 -				if ( ch > 0x10FFFF ) {
  1.1051 -					ch = UNKNOWN_UNICODE;
  1.1052 -				}
  1.1053 -				if ( ch < 0x10000 ) {
  1.1054 -					if ( dstlen < 2 ) {
  1.1055 -						return SDL_ICONV_E2BIG;
  1.1056 -					}
  1.1057 -					p[1] = (Uint8)(ch >> 8);
  1.1058 -					p[0] = (Uint8)ch;
  1.1059 -					dst += 2;
  1.1060 -					dstlen -= 2;
  1.1061 -				} else {
  1.1062 -					Uint16 W1, W2;
  1.1063 -					if ( dstlen < 4 ) {
  1.1064 -						return SDL_ICONV_E2BIG;
  1.1065 -					}
  1.1066 -					ch = ch - 0x10000;
  1.1067 -					W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
  1.1068 -					W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
  1.1069 -					p[1] = (Uint8)(W1 >> 8);
  1.1070 -					p[0] = (Uint8)W1;
  1.1071 -					p[3] = (Uint8)(W2 >> 8);
  1.1072 -					p[2] = (Uint8)W2;
  1.1073 -					dst += 4;
  1.1074 -					dstlen -= 4;
  1.1075 -				}
  1.1076 -			}
  1.1077 -			break;
  1.1078 -		    case ENCODING_UTF32BE:
  1.1079 -			{
  1.1080 -				Uint8 *p = (Uint8 *)dst;
  1.1081 -				if ( ch > 0x10FFFF ) {
  1.1082 -					ch = UNKNOWN_UNICODE;
  1.1083 -				}
  1.1084 -				if ( dstlen < 4 ) {
  1.1085 -					return SDL_ICONV_E2BIG;
  1.1086 -				}
  1.1087 -				p[0] = (Uint8)(ch >> 24);
  1.1088 -				p[1] = (Uint8)(ch >> 16);
  1.1089 -				p[2] = (Uint8)(ch >> 8);
  1.1090 -				p[3] = (Uint8)ch;
  1.1091 -				dst += 4;
  1.1092 -				dstlen -= 4;
  1.1093 -			}
  1.1094 -			break;
  1.1095 -		    case ENCODING_UTF32LE:
  1.1096 -			{
  1.1097 -				Uint8 *p = (Uint8 *)dst;
  1.1098 -				if ( ch > 0x10FFFF ) {
  1.1099 -					ch = UNKNOWN_UNICODE;
  1.1100 -				}
  1.1101 -				if ( dstlen < 4 ) {
  1.1102 -					return SDL_ICONV_E2BIG;
  1.1103 -				}
  1.1104 -				p[3] = (Uint8)(ch >> 24);
  1.1105 -				p[2] = (Uint8)(ch >> 16);
  1.1106 -				p[1] = (Uint8)(ch >> 8);
  1.1107 -				p[0] = (Uint8)ch;
  1.1108 -				dst += 4;
  1.1109 -				dstlen -= 4;
  1.1110 -			}
  1.1111 -			break;
  1.1112 -		    case ENCODING_UCS2:
  1.1113 -			{
  1.1114 -				Uint16 *p = (Uint16 *)dst;
  1.1115 -				if ( ch > 0xFFFF ) {
  1.1116 -					ch = UNKNOWN_UNICODE;
  1.1117 -				}
  1.1118 -				if ( dstlen < 2 ) {
  1.1119 -					return SDL_ICONV_E2BIG;
  1.1120 -				}
  1.1121 -				*p = (Uint16)ch;
  1.1122 -				dst += 2;
  1.1123 -				dstlen -= 2;
  1.1124 -			}
  1.1125 -			break;
  1.1126 -		    case ENCODING_UCS4:
  1.1127 -			{
  1.1128 -				Uint32 *p = (Uint32 *)dst;
  1.1129 -				if ( ch > 0x7FFFFFFF ) {
  1.1130 -					ch = UNKNOWN_UNICODE;
  1.1131 -				}
  1.1132 -				if ( dstlen < 4 ) {
  1.1133 -					return SDL_ICONV_E2BIG;
  1.1134 -				}
  1.1135 -				*p = ch;
  1.1136 -				dst += 4;
  1.1137 -				dstlen -= 4;
  1.1138 -			}
  1.1139 -			break;
  1.1140 -		}
  1.1141 +        /* Encode a character */
  1.1142 +        switch (cd->dst_fmt) {
  1.1143 +        case ENCODING_ASCII:
  1.1144 +            {
  1.1145 +                Uint8 *p = (Uint8 *) dst;
  1.1146 +                if (dstlen < 1) {
  1.1147 +                    return SDL_ICONV_E2BIG;
  1.1148 +                }
  1.1149 +                if (ch > 0x7F) {
  1.1150 +                    *p = UNKNOWN_ASCII;
  1.1151 +                } else {
  1.1152 +                    *p = (Uint8) ch;
  1.1153 +                }
  1.1154 +                ++dst;
  1.1155 +                --dstlen;
  1.1156 +            }
  1.1157 +            break;
  1.1158 +        case ENCODING_LATIN1:
  1.1159 +            {
  1.1160 +                Uint8 *p = (Uint8 *) dst;
  1.1161 +                if (dstlen < 1) {
  1.1162 +                    return SDL_ICONV_E2BIG;
  1.1163 +                }
  1.1164 +                if (ch > 0xFF) {
  1.1165 +                    *p = UNKNOWN_ASCII;
  1.1166 +                } else {
  1.1167 +                    *p = (Uint8) ch;
  1.1168 +                }
  1.1169 +                ++dst;
  1.1170 +                --dstlen;
  1.1171 +            }
  1.1172 +            break;
  1.1173 +        case ENCODING_UTF8:    /* RFC 3629 */
  1.1174 +            {
  1.1175 +                Uint8 *p = (Uint8 *) dst;
  1.1176 +                if (ch > 0x10FFFF) {
  1.1177 +                    ch = UNKNOWN_UNICODE;
  1.1178 +                }
  1.1179 +                if (ch <= 0x7F) {
  1.1180 +                    if (dstlen < 1) {
  1.1181 +                        return SDL_ICONV_E2BIG;
  1.1182 +                    }
  1.1183 +                    *p = (Uint8) ch;
  1.1184 +                    ++dst;
  1.1185 +                    --dstlen;
  1.1186 +                } else if (ch <= 0x7FF) {
  1.1187 +                    if (dstlen < 2) {
  1.1188 +                        return SDL_ICONV_E2BIG;
  1.1189 +                    }
  1.1190 +                    p[0] = 0xC0 | (Uint8) ((ch >> 6) & 0x1F);
  1.1191 +                    p[1] = 0x80 | (Uint8) (ch & 0x3F);
  1.1192 +                    dst += 2;
  1.1193 +                    dstlen -= 2;
  1.1194 +                } else if (ch <= 0xFFFF) {
  1.1195 +                    if (dstlen < 3) {
  1.1196 +                        return SDL_ICONV_E2BIG;
  1.1197 +                    }
  1.1198 +                    p[0] = 0xE0 | (Uint8) ((ch >> 12) & 0x0F);
  1.1199 +                    p[1] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
  1.1200 +                    p[2] = 0x80 | (Uint8) (ch & 0x3F);
  1.1201 +                    dst += 3;
  1.1202 +                    dstlen -= 3;
  1.1203 +                } else if (ch <= 0x1FFFFF) {
  1.1204 +                    if (dstlen < 4) {
  1.1205 +                        return SDL_ICONV_E2BIG;
  1.1206 +                    }
  1.1207 +                    p[0] = 0xF0 | (Uint8) ((ch >> 18) & 0x07);
  1.1208 +                    p[1] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
  1.1209 +                    p[2] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
  1.1210 +                    p[3] = 0x80 | (Uint8) (ch & 0x3F);
  1.1211 +                    dst += 4;
  1.1212 +                    dstlen -= 4;
  1.1213 +                } else if (ch <= 0x3FFFFFF) {
  1.1214 +                    if (dstlen < 5) {
  1.1215 +                        return SDL_ICONV_E2BIG;
  1.1216 +                    }
  1.1217 +                    p[0] = 0xF8 | (Uint8) ((ch >> 24) & 0x03);
  1.1218 +                    p[1] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
  1.1219 +                    p[2] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
  1.1220 +                    p[3] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
  1.1221 +                    p[4] = 0x80 | (Uint8) (ch & 0x3F);
  1.1222 +                    dst += 5;
  1.1223 +                    dstlen -= 5;
  1.1224 +                } else {
  1.1225 +                    if (dstlen < 6) {
  1.1226 +                        return SDL_ICONV_E2BIG;
  1.1227 +                    }
  1.1228 +                    p[0] = 0xFC | (Uint8) ((ch >> 30) & 0x01);
  1.1229 +                    p[1] = 0x80 | (Uint8) ((ch >> 24) & 0x3F);
  1.1230 +                    p[2] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
  1.1231 +                    p[3] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
  1.1232 +                    p[4] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
  1.1233 +                    p[5] = 0x80 | (Uint8) (ch & 0x3F);
  1.1234 +                    dst += 6;
  1.1235 +                    dstlen -= 6;
  1.1236 +                }
  1.1237 +            }
  1.1238 +            break;
  1.1239 +        case ENCODING_UTF16BE: /* RFC 2781 */
  1.1240 +            {
  1.1241 +                Uint8 *p = (Uint8 *) dst;
  1.1242 +                if (ch > 0x10FFFF) {
  1.1243 +                    ch = UNKNOWN_UNICODE;
  1.1244 +                }
  1.1245 +                if (ch < 0x10000) {
  1.1246 +                    if (dstlen < 2) {
  1.1247 +                        return SDL_ICONV_E2BIG;
  1.1248 +                    }
  1.1249 +                    p[0] = (Uint8) (ch >> 8);
  1.1250 +                    p[1] = (Uint8) ch;
  1.1251 +                    dst += 2;
  1.1252 +                    dstlen -= 2;
  1.1253 +                } else {
  1.1254 +                    Uint16 W1, W2;
  1.1255 +                    if (dstlen < 4) {
  1.1256 +                        return SDL_ICONV_E2BIG;
  1.1257 +                    }
  1.1258 +                    ch = ch - 0x10000;
  1.1259 +                    W1 = 0xD800 | (Uint16) ((ch >> 10) & 0x3FF);
  1.1260 +                    W2 = 0xDC00 | (Uint16) (ch & 0x3FF);
  1.1261 +                    p[0] = (Uint8) (W1 >> 8);
  1.1262 +                    p[1] = (Uint8) W1;
  1.1263 +                    p[2] = (Uint8) (W2 >> 8);
  1.1264 +                    p[3] = (Uint8) W2;
  1.1265 +                    dst += 4;
  1.1266 +                    dstlen -= 4;
  1.1267 +                }
  1.1268 +            }
  1.1269 +            break;
  1.1270 +        case ENCODING_UTF16LE: /* RFC 2781 */
  1.1271 +            {
  1.1272 +                Uint8 *p = (Uint8 *) dst;
  1.1273 +                if (ch > 0x10FFFF) {
  1.1274 +                    ch = UNKNOWN_UNICODE;
  1.1275 +                }
  1.1276 +                if (ch < 0x10000) {
  1.1277 +                    if (dstlen < 2) {
  1.1278 +                        return SDL_ICONV_E2BIG;
  1.1279 +                    }
  1.1280 +                    p[1] = (Uint8) (ch >> 8);
  1.1281 +                    p[0] = (Uint8) ch;
  1.1282 +                    dst += 2;
  1.1283 +                    dstlen -= 2;
  1.1284 +                } else {
  1.1285 +                    Uint16 W1, W2;
  1.1286 +                    if (dstlen < 4) {
  1.1287 +                        return SDL_ICONV_E2BIG;
  1.1288 +                    }
  1.1289 +                    ch = ch - 0x10000;
  1.1290 +                    W1 = 0xD800 | (Uint16) ((ch >> 10) & 0x3FF);
  1.1291 +                    W2 = 0xDC00 | (Uint16) (ch & 0x3FF);
  1.1292 +                    p[1] = (Uint8) (W1 >> 8);
  1.1293 +                    p[0] = (Uint8) W1;
  1.1294 +                    p[3] = (Uint8) (W2 >> 8);
  1.1295 +                    p[2] = (Uint8) W2;
  1.1296 +                    dst += 4;
  1.1297 +                    dstlen -= 4;
  1.1298 +                }
  1.1299 +            }
  1.1300 +            break;
  1.1301 +        case ENCODING_UTF32BE:
  1.1302 +            {
  1.1303 +                Uint8 *p = (Uint8 *) dst;
  1.1304 +                if (ch > 0x10FFFF) {
  1.1305 +                    ch = UNKNOWN_UNICODE;
  1.1306 +                }
  1.1307 +                if (dstlen < 4) {
  1.1308 +                    return SDL_ICONV_E2BIG;
  1.1309 +                }
  1.1310 +                p[0] = (Uint8) (ch >> 24);
  1.1311 +                p[1] = (Uint8) (ch >> 16);
  1.1312 +                p[2] = (Uint8) (ch >> 8);
  1.1313 +                p[3] = (Uint8) ch;
  1.1314 +                dst += 4;
  1.1315 +                dstlen -= 4;
  1.1316 +            }
  1.1317 +            break;
  1.1318 +        case ENCODING_UTF32LE:
  1.1319 +            {
  1.1320 +                Uint8 *p = (Uint8 *) dst;
  1.1321 +                if (ch > 0x10FFFF) {
  1.1322 +                    ch = UNKNOWN_UNICODE;
  1.1323 +                }
  1.1324 +                if (dstlen < 4) {
  1.1325 +                    return SDL_ICONV_E2BIG;
  1.1326 +                }
  1.1327 +                p[3] = (Uint8) (ch >> 24);
  1.1328 +                p[2] = (Uint8) (ch >> 16);
  1.1329 +                p[1] = (Uint8) (ch >> 8);
  1.1330 +                p[0] = (Uint8) ch;
  1.1331 +                dst += 4;
  1.1332 +                dstlen -= 4;
  1.1333 +            }
  1.1334 +            break;
  1.1335 +        case ENCODING_UCS2:
  1.1336 +            {
  1.1337 +                Uint16 *p = (Uint16 *) dst;
  1.1338 +                if (ch > 0xFFFF) {
  1.1339 +                    ch = UNKNOWN_UNICODE;
  1.1340 +                }
  1.1341 +                if (dstlen < 2) {
  1.1342 +                    return SDL_ICONV_E2BIG;
  1.1343 +                }
  1.1344 +                *p = (Uint16) ch;
  1.1345 +                dst += 2;
  1.1346 +                dstlen -= 2;
  1.1347 +            }
  1.1348 +            break;
  1.1349 +        case ENCODING_UCS4:
  1.1350 +            {
  1.1351 +                Uint32 *p = (Uint32 *) dst;
  1.1352 +                if (ch > 0x7FFFFFFF) {
  1.1353 +                    ch = UNKNOWN_UNICODE;
  1.1354 +                }
  1.1355 +                if (dstlen < 4) {
  1.1356 +                    return SDL_ICONV_E2BIG;
  1.1357 +                }
  1.1358 +                *p = ch;
  1.1359 +                dst += 4;
  1.1360 +                dstlen -= 4;
  1.1361 +            }
  1.1362 +            break;
  1.1363 +        }
  1.1364  
  1.1365 -		/* Update state */
  1.1366 -		*inbuf = src;
  1.1367 -		*inbytesleft = srclen;
  1.1368 -		*outbuf = dst;
  1.1369 -		*outbytesleft = dstlen;
  1.1370 -		++total;
  1.1371 -	}
  1.1372 -	return total;
  1.1373 +        /* Update state */
  1.1374 +        *inbuf = src;
  1.1375 +        *inbytesleft = srclen;
  1.1376 +        *outbuf = dst;
  1.1377 +        *outbytesleft = dstlen;
  1.1378 +        ++total;
  1.1379 +    }
  1.1380 +    return total;
  1.1381  }
  1.1382  
  1.1383 -int SDL_iconv_close(SDL_iconv_t cd)
  1.1384 +int
  1.1385 +SDL_iconv_close(SDL_iconv_t cd)
  1.1386  {
  1.1387 -	if ( cd && cd != (SDL_iconv_t)-1 ) {
  1.1388 -		SDL_free(cd);
  1.1389 -	}
  1.1390 -	return 0;
  1.1391 +    if (cd && cd != (SDL_iconv_t) - 1) {
  1.1392 +        SDL_free(cd);
  1.1393 +    }
  1.1394 +    return 0;
  1.1395  }
  1.1396  
  1.1397  #endif /* !HAVE_ICONV */
  1.1398  
  1.1399 -char *SDL_iconv_string(const char *tocode, const char *fromcode, char *inbuf, size_t inbytesleft)
  1.1400 +char *
  1.1401 +SDL_iconv_string(const char *tocode, const char *fromcode, char *inbuf,
  1.1402 +                 size_t inbytesleft)
  1.1403  {
  1.1404 -	SDL_iconv_t cd;
  1.1405 -	char *string;
  1.1406 -	size_t stringsize;
  1.1407 -	char *outbuf;
  1.1408 -	size_t outbytesleft;
  1.1409 -	size_t retCode = 0;
  1.1410 +    SDL_iconv_t cd;
  1.1411 +    char *string;
  1.1412 +    size_t stringsize;
  1.1413 +    char *outbuf;
  1.1414 +    size_t outbytesleft;
  1.1415 +    size_t retCode = 0;
  1.1416  
  1.1417 -	cd = SDL_iconv_open(tocode, fromcode);
  1.1418 -	if ( cd == (SDL_iconv_t)-1 ) {
  1.1419 -		return NULL;
  1.1420 -	}
  1.1421 +    cd = SDL_iconv_open(tocode, fromcode);
  1.1422 +    if (cd == (SDL_iconv_t) - 1) {
  1.1423 +        return NULL;
  1.1424 +    }
  1.1425  
  1.1426 -	stringsize = inbytesleft > 4 ? inbytesleft : 4;
  1.1427 -	string = SDL_malloc(stringsize);
  1.1428 -	if ( !string ) {
  1.1429 -		SDL_iconv_close(cd);
  1.1430 -		return NULL;
  1.1431 -	}
  1.1432 -	outbuf = string;
  1.1433 -	outbytesleft = stringsize;
  1.1434 -	SDL_memset(outbuf, 0, 4);
  1.1435 +    stringsize = inbytesleft > 4 ? inbytesleft : 4;
  1.1436 +    string = SDL_malloc(stringsize);
  1.1437 +    if (!string) {
  1.1438 +        SDL_iconv_close(cd);
  1.1439 +        return NULL;
  1.1440 +    }
  1.1441 +    outbuf = string;
  1.1442 +    outbytesleft = stringsize;
  1.1443 +    SDL_memset(outbuf, 0, 4);
  1.1444  
  1.1445 -	while ( inbytesleft > 0 ) {
  1.1446 -		retCode = SDL_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
  1.1447 -		switch (retCode) {
  1.1448 -		    case SDL_ICONV_E2BIG:
  1.1449 -			{
  1.1450 -				char *oldstring = string;
  1.1451 -				stringsize *= 2;
  1.1452 -				string = SDL_realloc(string, stringsize);
  1.1453 -				if ( !string ) {
  1.1454 -					SDL_iconv_close(cd);
  1.1455 -					return NULL;
  1.1456 -				}
  1.1457 -				outbuf = string + (outbuf - oldstring);
  1.1458 -				outbytesleft = stringsize - (outbuf - string);
  1.1459 -				SDL_memset(outbuf, 0, 4);
  1.1460 -			}
  1.1461 -			break;
  1.1462 -		    case SDL_ICONV_EILSEQ:
  1.1463 -			/* Try skipping some input data - not perfect, but... */
  1.1464 -			++inbuf;
  1.1465 -			--inbytesleft;
  1.1466 -			break;
  1.1467 -		    case SDL_ICONV_EINVAL:
  1.1468 -		    case SDL_ICONV_ERROR:
  1.1469 -			/* We can't continue... */
  1.1470 -			inbytesleft = 0;
  1.1471 -			break;
  1.1472 -		}
  1.1473 -	}
  1.1474 -	SDL_iconv_close(cd);
  1.1475 +    while (inbytesleft > 0) {
  1.1476 +        retCode = SDL_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
  1.1477 +        switch (retCode) {
  1.1478 +        case SDL_ICONV_E2BIG:
  1.1479 +            {
  1.1480 +                char *oldstring = string;
  1.1481 +                stringsize *= 2;
  1.1482 +                string = SDL_realloc(string, stringsize);
  1.1483 +                if (!string) {
  1.1484 +                    SDL_iconv_close(cd);
  1.1485 +                    return NULL;
  1.1486 +                }
  1.1487 +                outbuf = string + (outbuf - oldstring);
  1.1488 +                outbytesleft = stringsize - (outbuf - string);
  1.1489 +                SDL_memset(outbuf, 0, 4);
  1.1490 +            }
  1.1491 +            break;
  1.1492 +        case SDL_ICONV_EILSEQ:
  1.1493 +            /* Try skipping some input data - not perfect, but... */
  1.1494 +            ++inbuf;
  1.1495 +            --inbytesleft;
  1.1496 +            break;
  1.1497 +        case SDL_ICONV_EINVAL:
  1.1498 +        case SDL_ICONV_ERROR:
  1.1499 +            /* We can't continue... */
  1.1500 +            inbytesleft = 0;
  1.1501 +            break;
  1.1502 +        }
  1.1503 +    }
  1.1504 +    SDL_iconv_close(cd);
  1.1505  
  1.1506 -	return string;
  1.1507 +    return string;
  1.1508  }
  1.1509 +
  1.1510 +/* vi: set ts=4 sw=4 expandtab: */