src/stdlib/SDL_iconv.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 28 Jun 2007 05:35:50 +0000
branchSDL-1.2
changeset 3984 b74530a1dad6
parent 3918 f16c15f3bc2b
child 3985 2f8efcf14c83
permissions -rw-r--r--
Better fix for bug 419
slouken@1501
     1
/*
slouken@1501
     2
    SDL - Simple DirectMedia Layer
slouken@1501
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1501
     4
slouken@1501
     5
    This library is free software; you can redistribute it and/or
slouken@1501
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1501
     7
    License as published by the Free Software Foundation; either
slouken@1501
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1501
     9
slouken@1501
    10
    This library is distributed in the hope that it will be useful,
slouken@1501
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1501
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1501
    13
    Lesser General Public License for more details.
slouken@1501
    14
slouken@1501
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1501
    16
    License along with this library; if not, write to the Free Software
slouken@1501
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1501
    18
slouken@1501
    19
    Sam Lantinga
slouken@1501
    20
    slouken@libsdl.org
slouken@1501
    21
*/
slouken@1501
    22
#include "SDL_config.h"
slouken@1501
    23
slouken@1501
    24
/* This file contains portable iconv functions for SDL */
slouken@1501
    25
slouken@1501
    26
#include "SDL_stdinc.h"
slouken@1501
    27
#include "SDL_endian.h"
slouken@1501
    28
slouken@1501
    29
#ifdef HAVE_ICONV
slouken@1501
    30
slouken@1501
    31
#include <errno.h>
slouken@1501
    32
slouken@1501
    33
size_t SDL_iconv(SDL_iconv_t cd,
slouken@3984
    34
                 const char **inbuf, size_t *inbytesleft,
slouken@1501
    35
                 char **outbuf, size_t *outbytesleft)
slouken@1501
    36
{
slouken@1501
    37
	size_t retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
slouken@1501
    38
	if ( retCode == (size_t)-1 ) {
slouken@1501
    39
		switch(errno) {
slouken@1501
    40
		    case E2BIG:
slouken@1501
    41
			return SDL_ICONV_E2BIG;
slouken@1501
    42
		    case EILSEQ:
slouken@1501
    43
			return SDL_ICONV_EILSEQ;
slouken@1501
    44
		    case EINVAL:
slouken@1501
    45
			return SDL_ICONV_EINVAL;
slouken@1501
    46
		    default:
slouken@1501
    47
			return SDL_ICONV_ERROR;
slouken@1501
    48
		}
slouken@1501
    49
	}
slouken@1501
    50
	return retCode;
slouken@1501
    51
}
slouken@1501
    52
slouken@1501
    53
#else
slouken@1501
    54
slouken@1503
    55
/* Lots of useful information on Unicode at:
slouken@1503
    56
	http://www.cl.cam.ac.uk/~mgk25/unicode.html
slouken@1503
    57
*/
slouken@1503
    58
slouken@1501
    59
#define UNICODE_BOM	0xFEFF
slouken@1501
    60
slouken@1501
    61
#define UNKNOWN_ASCII	'?'
slouken@1501
    62
#define UNKNOWN_UNICODE	0xFFFD
slouken@1501
    63
slouken@1501
    64
enum {
slouken@1501
    65
	ENCODING_UNKNOWN,
slouken@1501
    66
	ENCODING_ASCII,
slouken@1501
    67
	ENCODING_LATIN1,
slouken@1501
    68
	ENCODING_UTF8,
slouken@1501
    69
	ENCODING_UTF16,		/* Needs byte order marker */
slouken@1501
    70
	ENCODING_UTF16BE,
slouken@1501
    71
	ENCODING_UTF16LE,
slouken@1501
    72
	ENCODING_UTF32,		/* Needs byte order marker */
slouken@1501
    73
	ENCODING_UTF32BE,
slouken@1501
    74
	ENCODING_UTF32LE,
slouken@1501
    75
	ENCODING_UCS2,		/* Native byte order assumed */
slouken@1501
    76
	ENCODING_UCS4,		/* Native byte order assumed */
slouken@1501
    77
};
slouken@1501
    78
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1501
    79
#define ENCODING_UTF16NATIVE	ENCODING_UTF16BE
slouken@1501
    80
#define ENCODING_UTF32NATIVE	ENCODING_UTF32BE
slouken@1501
    81
#else
slouken@1501
    82
#define ENCODING_UTF16NATIVE	ENCODING_UTF16LE
slouken@1501
    83
#define ENCODING_UTF32NATIVE	ENCODING_UTF32LE
slouken@1501
    84
#endif
slouken@1501
    85
slouken@1501
    86
struct _SDL_iconv_t
slouken@1501
    87
{
slouken@1501
    88
	int src_fmt;
slouken@1501
    89
	int dst_fmt;
slouken@1501
    90
};
slouken@1501
    91
slouken@1501
    92
static struct {
slouken@1501
    93
	const char *name;
slouken@1501
    94
	int format;
slouken@1501
    95
} encodings[] = {
slouken@1501
    96
	{ "ASCII",	ENCODING_ASCII },
slouken@1501
    97
	{ "US-ASCII",	ENCODING_ASCII },
slouken@1501
    98
	{ "LATIN1",	ENCODING_LATIN1 },
slouken@1501
    99
	{ "ISO-8859-1",	ENCODING_LATIN1 },
slouken@1501
   100
	{ "UTF8",	ENCODING_UTF8 },
slouken@1501
   101
	{ "UTF-8",	ENCODING_UTF8 },
slouken@1501
   102
	{ "UTF16",	ENCODING_UTF16 },
slouken@1501
   103
	{ "UTF-16",	ENCODING_UTF16 },
slouken@1501
   104
	{ "UTF16BE",	ENCODING_UTF16BE },
slouken@1501
   105
	{ "UTF-16BE",	ENCODING_UTF16BE },
slouken@1501
   106
	{ "UTF16LE",	ENCODING_UTF16LE },
slouken@1501
   107
	{ "UTF-16LE",	ENCODING_UTF16LE },
slouken@1501
   108
	{ "UTF32",	ENCODING_UTF32 },
slouken@1501
   109
	{ "UTF-32",	ENCODING_UTF32 },
slouken@1501
   110
	{ "UTF32BE",	ENCODING_UTF32BE },
slouken@1501
   111
	{ "UTF-32BE",	ENCODING_UTF32BE },
slouken@1501
   112
	{ "UTF32LE",	ENCODING_UTF32LE },
slouken@1501
   113
	{ "UTF-32LE",	ENCODING_UTF32LE },
slouken@1501
   114
	{ "UCS2",	ENCODING_UCS2 },
slouken@1501
   115
	{ "UCS-2",	ENCODING_UCS2 },
slouken@1501
   116
	{ "UCS4",	ENCODING_UCS4 },
slouken@1501
   117
	{ "UCS-4",	ENCODING_UCS4 },
slouken@1501
   118
};
slouken@1501
   119
slouken@1501
   120
SDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode)
slouken@1501
   121
{
slouken@1501
   122
	int src_fmt = ENCODING_UNKNOWN;
slouken@1501
   123
	int dst_fmt = ENCODING_UNKNOWN;
slouken@1501
   124
	int i;
slouken@1501
   125
slouken@1501
   126
	for ( i = 0; i < SDL_arraysize(encodings); ++i ) {
slouken@1501
   127
		if ( SDL_strcasecmp(fromcode, encodings[i].name) == 0 ) {
slouken@1501
   128
			src_fmt = encodings[i].format;
slouken@1501
   129
			if ( dst_fmt != ENCODING_UNKNOWN ) {
slouken@1501
   130
				break;
slouken@1501
   131
			}
slouken@1501
   132
		}
slouken@1501
   133
		if ( SDL_strcasecmp(tocode, encodings[i].name) == 0 ) {
slouken@1501
   134
			dst_fmt = encodings[i].format;
slouken@1501
   135
			if ( src_fmt != ENCODING_UNKNOWN ) {
slouken@1501
   136
				break;
slouken@1501
   137
			}
slouken@1501
   138
		}
slouken@1501
   139
	}
slouken@1501
   140
	if ( src_fmt != ENCODING_UNKNOWN && dst_fmt != ENCODING_UNKNOWN ) {
slouken@1501
   141
		SDL_iconv_t cd = (SDL_iconv_t)SDL_malloc(sizeof(*cd));
slouken@1501
   142
		if ( cd ) {
slouken@1501
   143
			cd->src_fmt = src_fmt;
slouken@1501
   144
			cd->dst_fmt = dst_fmt;
slouken@1501
   145
			return cd;
slouken@1501
   146
		}
slouken@1501
   147
	}
slouken@1501
   148
	return (SDL_iconv_t)-1;
slouken@1501
   149
}
slouken@1501
   150
slouken@1501
   151
size_t SDL_iconv(SDL_iconv_t cd,
icculus@3918
   152
                 const char **inbuf, size_t *inbytesleft,
slouken@1501
   153
                 char **outbuf, size_t *outbytesleft)
slouken@1501
   154
{
slouken@1501
   155
	/* For simplicity, we'll convert everything to and from UCS-4 */
icculus@3918
   156
	const char *src;
icculus@3918
   157
	char *dst;
slouken@1501
   158
	size_t srclen, dstlen;
slouken@1849
   159
	Uint32 ch = 0;
slouken@1501
   160
	size_t total;
slouken@1501
   161
slouken@1501
   162
	if ( !inbuf || !*inbuf ) {
slouken@1501
   163
		/* Reset the context */
slouken@1501
   164
		return 0;
slouken@1501
   165
	}
slouken@1501
   166
	if ( !outbuf || !*outbuf || !outbytesleft || !*outbytesleft ) {
slouken@1501
   167
		return SDL_ICONV_E2BIG;
slouken@1501
   168
	}
slouken@1501
   169
	src = *inbuf;
slouken@1501
   170
	srclen = (inbytesleft ? *inbytesleft : 0);
slouken@1501
   171
	dst = *outbuf;
slouken@1501
   172
	dstlen = *outbytesleft;
slouken@1501
   173
slouken@1501
   174
	switch ( cd->src_fmt ) {
slouken@1501
   175
	    case ENCODING_UTF16:
slouken@1501
   176
		/* Scan for a byte order marker */
slouken@1501
   177
		{
slouken@1501
   178
			Uint8 *p = (Uint8 *)src;
slouken@1501
   179
			size_t n = srclen / 2;
slouken@1501
   180
			while ( n ) {
slouken@1501
   181
				if ( p[0] == 0xFF && p[1] == 0xFE ) {
slouken@1501
   182
					cd->src_fmt = ENCODING_UTF16BE;
slouken@1501
   183
					break;
slouken@1501
   184
				} else if ( p[0] == 0xFE && p[1] == 0xFF ) {
slouken@1501
   185
					cd->src_fmt = ENCODING_UTF16LE;
slouken@1501
   186
					break;
slouken@1501
   187
				}
slouken@1501
   188
				p += 2;
slouken@1501
   189
				--n;
slouken@1501
   190
			}
slouken@1501
   191
			if ( n == 0 ) {
slouken@1501
   192
				/* We can't tell, default to host order */
slouken@1501
   193
				cd->src_fmt = ENCODING_UTF16NATIVE;
slouken@1501
   194
			}
slouken@1501
   195
		}
slouken@1501
   196
		break;
slouken@1501
   197
	    case ENCODING_UTF32:
slouken@1501
   198
		/* Scan for a byte order marker */
slouken@1501
   199
		{
slouken@1501
   200
			Uint8 *p = (Uint8 *)src;
slouken@1501
   201
			size_t n = srclen / 4;
slouken@1501
   202
			while ( n ) {
slouken@1501
   203
				if ( p[0] == 0xFF && p[1] == 0xFE &&
slouken@1501
   204
				     p[2] == 0x00 && p[3] == 0x00 ) {
slouken@1501
   205
					cd->src_fmt = ENCODING_UTF32BE;
slouken@1501
   206
					break;
slouken@1501
   207
				} else if ( p[0] == 0x00 && p[1] == 0x00 &&
slouken@1501
   208
				            p[2] == 0xFE && p[3] == 0xFF ) {
slouken@1501
   209
					cd->src_fmt = ENCODING_UTF32LE;
slouken@1501
   210
					break;
slouken@1501
   211
				}
slouken@1501
   212
				p += 4;
slouken@1501
   213
				--n;
slouken@1501
   214
			}
slouken@1501
   215
			if ( n == 0 ) {
slouken@1501
   216
				/* We can't tell, default to host order */
slouken@1501
   217
				cd->src_fmt = ENCODING_UTF32NATIVE;
slouken@1501
   218
			}
slouken@1501
   219
		}
slouken@1501
   220
		break;
slouken@1501
   221
	}
slouken@1501
   222
slouken@1501
   223
	switch ( cd->dst_fmt ) {
slouken@1501
   224
	    case ENCODING_UTF16:
slouken@1501
   225
		/* Default to host order, need to add byte order marker */
slouken@1501
   226
		if ( dstlen < 2 ) {
slouken@1501
   227
			return SDL_ICONV_E2BIG;
slouken@1501
   228
		}
slouken@1501
   229
		*(Uint16 *)dst = UNICODE_BOM;
slouken@1501
   230
		dst += 2;
slouken@1501
   231
		dstlen -= 2;
slouken@1501
   232
		cd->dst_fmt = ENCODING_UTF16NATIVE;
slouken@1501
   233
		break;
slouken@1501
   234
	    case ENCODING_UTF32:
slouken@1501
   235
		/* Default to host order, need to add byte order marker */
slouken@1501
   236
		if ( dstlen < 4 ) {
slouken@1501
   237
			return SDL_ICONV_E2BIG;
slouken@1501
   238
		}
slouken@1501
   239
		*(Uint32 *)dst = UNICODE_BOM;
slouken@1501
   240
		dst += 4;
slouken@1501
   241
		dstlen -= 4;
slouken@1501
   242
		cd->dst_fmt = ENCODING_UTF32NATIVE;
slouken@1501
   243
		break;
slouken@1501
   244
	}
slouken@1501
   245
slouken@1501
   246
	total = 0;
slouken@1501
   247
	while ( srclen > 0 ) {
slouken@1501
   248
		/* Decode a character */
slouken@1501
   249
		switch ( cd->src_fmt ) {
slouken@1501
   250
		    case ENCODING_ASCII:
slouken@1501
   251
			{
slouken@1501
   252
				Uint8 *p = (Uint8 *)src;
slouken@1501
   253
				ch = (Uint32)(p[0] & 0x7F);
slouken@1501
   254
				++src;
slouken@1501
   255
				--srclen;
slouken@1501
   256
			}
slouken@1501
   257
			break;
slouken@1501
   258
		    case ENCODING_LATIN1:
slouken@1501
   259
			{
slouken@1501
   260
				Uint8 *p = (Uint8 *)src;
slouken@1501
   261
				ch = (Uint32)p[0];
slouken@1501
   262
				++src;
slouken@1501
   263
				--srclen;
slouken@1501
   264
			}
slouken@1501
   265
			break;
slouken@1501
   266
		    case ENCODING_UTF8: /* RFC 3629 */
slouken@1501
   267
			{
slouken@1501
   268
				Uint8 *p = (Uint8 *)src;
slouken@1501
   269
				size_t left = 0;
slouken@1501
   270
				SDL_bool overlong = SDL_FALSE;
slouken@1501
   271
				if ( p[0] >= 0xFC ) {
slouken@1501
   272
					if ( (p[0] & 0xFE) != 0xFC ) {
slouken@1501
   273
						/* Skip illegal sequences
slouken@1501
   274
						return SDL_ICONV_EILSEQ;
slouken@1501
   275
						*/
slouken@1501
   276
						ch = UNKNOWN_UNICODE;
slouken@1501
   277
					} else {
slouken@1501
   278
						if ( p[0] == 0xFC ) {
slouken@1501
   279
							overlong = SDL_TRUE;
slouken@1501
   280
						}
slouken@1501
   281
						ch = (Uint32)(p[0] & 0x01);
slouken@1501
   282
						left = 5;
slouken@1501
   283
					}
slouken@1501
   284
				} else if ( p[0] >= 0xF8 ) {
slouken@1501
   285
					if ( (p[0] & 0xFC) != 0xF8 ) {
slouken@1501
   286
						/* Skip illegal sequences
slouken@1501
   287
						return SDL_ICONV_EILSEQ;
slouken@1501
   288
						*/
slouken@1501
   289
						ch = UNKNOWN_UNICODE;
slouken@1501
   290
					} else {
slouken@1501
   291
						if ( p[0] == 0xF8 ) {
slouken@1501
   292
							overlong = SDL_TRUE;
slouken@1501
   293
						}
slouken@1501
   294
						ch = (Uint32)(p[0] & 0x03);
slouken@1501
   295
						left = 4;
slouken@1501
   296
					}
slouken@1501
   297
				} else if ( p[0] >= 0xF0 ) {
slouken@1501
   298
					if ( (p[0] & 0xF8) != 0xF0 ) {
slouken@1501
   299
						/* Skip illegal sequences
slouken@1501
   300
						return SDL_ICONV_EILSEQ;
slouken@1501
   301
						*/
slouken@1501
   302
						ch = UNKNOWN_UNICODE;
slouken@1501
   303
					} else {
slouken@1501
   304
						if ( p[0] == 0xF0 ) {
slouken@1501
   305
							overlong = SDL_TRUE;
slouken@1501
   306
						}
slouken@1501
   307
						ch = (Uint32)(p[0] & 0x07);
slouken@1501
   308
						left = 3;
slouken@1501
   309
					}
slouken@1501
   310
				} else if ( p[0] >= 0xE0 ) {
slouken@1501
   311
					if ( (p[0] & 0xF0) != 0xE0 ) {
slouken@1501
   312
						/* Skip illegal sequences
slouken@1501
   313
						return SDL_ICONV_EILSEQ;
slouken@1501
   314
						*/
slouken@1501
   315
						ch = UNKNOWN_UNICODE;
slouken@1501
   316
					} else {
slouken@1501
   317
						if ( p[0] == 0xE0 ) {
slouken@1501
   318
							overlong = SDL_TRUE;
slouken@1501
   319
						}
slouken@1501
   320
						ch = (Uint32)(p[0] & 0x0F);
slouken@1501
   321
						left = 2;
slouken@1501
   322
					}
slouken@1501
   323
				} else if ( p[0] >= 0xC0 ) {
slouken@1501
   324
					if ( (p[0] & 0xE0) != 0xC0 ) {
slouken@1501
   325
						/* Skip illegal sequences
slouken@1501
   326
						return SDL_ICONV_EILSEQ;
slouken@1501
   327
						*/
slouken@1501
   328
						ch = UNKNOWN_UNICODE;
slouken@1501
   329
					} else {
slouken@1501
   330
						if ( (p[0] & 0xCE) == 0xC0 ) {
slouken@1501
   331
							overlong = SDL_TRUE;
slouken@1501
   332
						}
slouken@1501
   333
						ch = (Uint32)(p[0] & 0x1F);
slouken@1501
   334
						left = 1;
slouken@1501
   335
					}
slouken@1501
   336
				} else {
slouken@1501
   337
					if ( (p[0] & 0x80) != 0x00 ) {
slouken@1501
   338
						/* Skip illegal sequences
slouken@1501
   339
						return SDL_ICONV_EILSEQ;
slouken@1501
   340
						*/
slouken@1501
   341
						ch = UNKNOWN_UNICODE;
slouken@1501
   342
					} else {
slouken@1501
   343
						ch = (Uint32)p[0];
slouken@1501
   344
					}
slouken@1501
   345
				}
slouken@1501
   346
				++src;
slouken@1501
   347
				--srclen;
slouken@1501
   348
				if ( srclen < left ) {
slouken@1501
   349
					return SDL_ICONV_EINVAL;
slouken@1501
   350
				}
slouken@1501
   351
				while ( left-- ) {
slouken@1501
   352
					++p;
slouken@1501
   353
					if ( (p[0] & 0xC0) != 0x80 ) {
slouken@1501
   354
						/* Skip illegal sequences
slouken@1501
   355
						return SDL_ICONV_EILSEQ;
slouken@1501
   356
						*/
slouken@1501
   357
						ch = UNKNOWN_UNICODE;
slouken@1501
   358
						break;
slouken@1501
   359
					}
slouken@1501
   360
					ch <<= 6;
slouken@1501
   361
					ch |= (p[0] & 0x3F);
slouken@1501
   362
					++src;
slouken@1501
   363
					--srclen;
slouken@1501
   364
				}
slouken@1501
   365
				if ( overlong ) {
slouken@1501
   366
					/* Potential security risk
slouken@1501
   367
					return SDL_ICONV_EILSEQ;
slouken@1501
   368
					*/
slouken@1501
   369
					ch = UNKNOWN_UNICODE;
slouken@1501
   370
				}
slouken@1501
   371
				if ( (ch >= 0xD800 && ch <= 0xDFFF) ||
slouken@1519
   372
				     (ch == 0xFFFE || ch == 0xFFFF) ||
slouken@1519
   373
				     ch > 0x10FFFF ) {
slouken@1501
   374
					/* Skip illegal sequences
slouken@1501
   375
					return SDL_ICONV_EILSEQ;
slouken@1501
   376
					*/
slouken@1501
   377
					ch = UNKNOWN_UNICODE;
slouken@1501
   378
				}
slouken@1501
   379
			}
slouken@1501
   380
			break;
slouken@1501
   381
		    case ENCODING_UTF16BE: /* RFC 2781 */
slouken@1501
   382
			{
slouken@1501
   383
				Uint8 *p = (Uint8 *)src;
slouken@1501
   384
				Uint16 W1, W2;
slouken@1501
   385
				if ( srclen < 2 ) {
slouken@1501
   386
					return SDL_ICONV_EINVAL;
slouken@1501
   387
				}
slouken@1513
   388
				W1 = ((Uint16)p[0] << 8) |
slouken@1513
   389
				      (Uint16)p[1];
slouken@1501
   390
				src += 2;
slouken@1501
   391
				srclen -= 2;
slouken@1501
   392
				if ( W1 < 0xD800 || W1 > 0xDFFF ) {
slouken@1501
   393
					ch = (Uint32)W1;
slouken@1501
   394
					break;
slouken@1501
   395
				}
slouken@1501
   396
				if ( W1 > 0xDBFF ) {
slouken@1501
   397
					/* Skip illegal sequences
slouken@1501
   398
					return SDL_ICONV_EILSEQ;
slouken@1501
   399
					*/
slouken@1501
   400
					ch = UNKNOWN_UNICODE;
slouken@1501
   401
					break;
slouken@1501
   402
				}
slouken@1501
   403
				if ( srclen < 2 ) {
slouken@1501
   404
					return SDL_ICONV_EINVAL;
slouken@1501
   405
				}
slouken@1510
   406
				p = (Uint8 *)src;
slouken@1513
   407
				W2 = ((Uint16)p[0] << 8) |
slouken@1513
   408
				      (Uint16)p[1];
slouken@1501
   409
				src += 2;
slouken@1501
   410
				srclen -= 2;
slouken@1501
   411
				if ( W2 < 0xDC00 || W2 > 0xDFFF ) {
slouken@1501
   412
					/* Skip illegal sequences
slouken@1501
   413
					return SDL_ICONV_EILSEQ;
slouken@1501
   414
					*/
slouken@1501
   415
					ch = UNKNOWN_UNICODE;
slouken@1501
   416
					break;
slouken@1501
   417
				}
slouken@1501
   418
				ch = (((Uint32)(W1 & 0x3FF) << 10) |
slouken@1501
   419
				      (Uint32)(W2 & 0x3FF)) + 0x10000;
slouken@1501
   420
			}
slouken@1501
   421
			break;
slouken@1501
   422
		    case ENCODING_UTF16LE: /* RFC 2781 */
slouken@1501
   423
			{
slouken@1501
   424
				Uint8 *p = (Uint8 *)src;
slouken@1501
   425
				Uint16 W1, W2;
slouken@1501
   426
				if ( srclen < 2 ) {
slouken@1501
   427
					return SDL_ICONV_EINVAL;
slouken@1501
   428
				}
slouken@1513
   429
				W1 = ((Uint16)p[1] << 8) |
slouken@1513
   430
				      (Uint16)p[0];
slouken@1501
   431
				src += 2;
slouken@1501
   432
				srclen -= 2;
slouken@1501
   433
				if ( W1 < 0xD800 || W1 > 0xDFFF ) {
slouken@1501
   434
					ch = (Uint32)W1;
slouken@1501
   435
					break;
slouken@1501
   436
				}
slouken@1501
   437
				if ( W1 > 0xDBFF ) {
slouken@1501
   438
					/* Skip illegal sequences
slouken@1501
   439
					return SDL_ICONV_EILSEQ;
slouken@1501
   440
					*/
slouken@1501
   441
					ch = UNKNOWN_UNICODE;
slouken@1501
   442
					break;
slouken@1501
   443
				}
slouken@1501
   444
				if ( srclen < 2 ) {
slouken@1501
   445
					return SDL_ICONV_EINVAL;
slouken@1501
   446
				}
slouken@1510
   447
				p = (Uint8 *)src;
slouken@1513
   448
				W2 = ((Uint16)p[1] << 8) |
slouken@1513
   449
				      (Uint16)p[0];
slouken@1501
   450
				src += 2;
slouken@1501
   451
				srclen -= 2;
slouken@1501
   452
				if ( W2 < 0xDC00 || W2 > 0xDFFF ) {
slouken@1501
   453
					/* Skip illegal sequences
slouken@1501
   454
					return SDL_ICONV_EILSEQ;
slouken@1501
   455
					*/
slouken@1501
   456
					ch = UNKNOWN_UNICODE;
slouken@1501
   457
					break;
slouken@1501
   458
				}
slouken@1501
   459
				ch = (((Uint32)(W1 & 0x3FF) << 10) |
slouken@1501
   460
				      (Uint32)(W2 & 0x3FF)) + 0x10000;
slouken@1501
   461
			}
slouken@1501
   462
			break;
slouken@1501
   463
		    case ENCODING_UTF32BE:
slouken@1501
   464
			{
slouken@1501
   465
				Uint8 *p = (Uint8 *)src;
slouken@1501
   466
				if ( srclen < 4 ) {
slouken@1501
   467
					return SDL_ICONV_EINVAL;
slouken@1501
   468
				}
slouken@1501
   469
				ch = ((Uint32)p[0] << 24) |
slouken@1501
   470
				     ((Uint32)p[1] << 16) |
slouken@1501
   471
				     ((Uint32)p[2] << 8) |
slouken@1501
   472
				      (Uint32)p[3];
slouken@1501
   473
				src += 4;
slouken@1501
   474
				srclen -= 4;
slouken@1501
   475
			}
slouken@1501
   476
			break;
slouken@1501
   477
		    case ENCODING_UTF32LE:
slouken@1501
   478
			{
slouken@1501
   479
				Uint8 *p = (Uint8 *)src;
slouken@1501
   480
				if ( srclen < 4 ) {
slouken@1501
   481
					return SDL_ICONV_EINVAL;
slouken@1501
   482
				}
slouken@1501
   483
				ch = ((Uint32)p[3] << 24) |
slouken@1501
   484
				     ((Uint32)p[2] << 16) |
slouken@1501
   485
				     ((Uint32)p[1] << 8) |
slouken@1501
   486
				      (Uint32)p[0];
slouken@1501
   487
				src += 4;
slouken@1501
   488
				srclen -= 4;
slouken@1501
   489
			}
slouken@1501
   490
			break;
slouken@1501
   491
		    case ENCODING_UCS2:
slouken@1501
   492
			{
slouken@1501
   493
				Uint16 *p = (Uint16 *)src;
slouken@1501
   494
				if ( srclen < 2 ) {
slouken@1501
   495
					return SDL_ICONV_EINVAL;
slouken@1501
   496
				}
slouken@1501
   497
				ch = *p;
slouken@1501
   498
				src += 2;
slouken@1501
   499
				srclen -= 2;
slouken@1501
   500
			}
slouken@1501
   501
			break;
slouken@1501
   502
		    case ENCODING_UCS4:
slouken@1501
   503
			{
slouken@1501
   504
				Uint32 *p = (Uint32 *)src;
slouken@1501
   505
				if ( srclen < 4 ) {
slouken@1501
   506
					return SDL_ICONV_EINVAL;
slouken@1501
   507
				}
slouken@1501
   508
				ch = *p;
slouken@1501
   509
				src += 4;
slouken@1501
   510
				srclen -= 4;
slouken@1501
   511
			}
slouken@1501
   512
			break;
slouken@1501
   513
		}
slouken@1501
   514
slouken@1501
   515
		/* Encode a character */
slouken@1501
   516
		switch ( cd->dst_fmt ) {
slouken@1501
   517
		    case ENCODING_ASCII:
slouken@1501
   518
			{
slouken@1501
   519
				Uint8 *p = (Uint8 *)dst;
slouken@1501
   520
				if ( dstlen < 1 ) {
slouken@1501
   521
					return SDL_ICONV_E2BIG;
slouken@1501
   522
				}
slouken@1501
   523
				if ( ch > 0x7F ) {
slouken@1501
   524
					*p = UNKNOWN_ASCII;
slouken@1501
   525
				} else {
slouken@1501
   526
					*p = (Uint8)ch;
slouken@1501
   527
				}
slouken@1501
   528
				++dst;
slouken@1501
   529
				--dstlen;
slouken@1501
   530
			}
slouken@1501
   531
			break;
slouken@1501
   532
		    case ENCODING_LATIN1:
slouken@1501
   533
			{
slouken@1501
   534
				Uint8 *p = (Uint8 *)dst;
slouken@1501
   535
				if ( dstlen < 1 ) {
slouken@1501
   536
					return SDL_ICONV_E2BIG;
slouken@1501
   537
				}
slouken@1501
   538
				if ( ch > 0xFF ) {
slouken@1501
   539
					*p = UNKNOWN_ASCII;
slouken@1501
   540
				} else {
slouken@1501
   541
					*p = (Uint8)ch;
slouken@1501
   542
				}
slouken@1501
   543
				++dst;
slouken@1501
   544
				--dstlen;
slouken@1501
   545
			}
slouken@1501
   546
			break;
slouken@1501
   547
		    case ENCODING_UTF8: /* RFC 3629 */
slouken@1501
   548
			{
slouken@1501
   549
				Uint8 *p = (Uint8 *)dst;
slouken@1519
   550
				if ( ch > 0x10FFFF ) {
slouken@1501
   551
					ch = UNKNOWN_UNICODE;
slouken@1501
   552
				}
slouken@1501
   553
				if ( ch <= 0x7F ) {
slouken@1501
   554
					if ( dstlen < 1 ) {
slouken@1501
   555
						return SDL_ICONV_E2BIG;
slouken@1501
   556
					}
slouken@1501
   557
					*p = (Uint8)ch;
slouken@1501
   558
					++dst;
slouken@1501
   559
					--dstlen;
slouken@1501
   560
				} else if ( ch <= 0x7FF ) {
slouken@1501
   561
					if ( dstlen < 2 ) {
slouken@1501
   562
						return SDL_ICONV_E2BIG;
slouken@1501
   563
					}
slouken@1501
   564
					p[0] = 0xC0 | (Uint8)((ch >> 6) & 0x1F);
slouken@1501
   565
					p[1] = 0x80 | (Uint8)(ch & 0x3F);
slouken@1501
   566
					dst += 2;
slouken@1501
   567
					dstlen -= 2;
slouken@1501
   568
				} else if ( ch <= 0xFFFF ) {
slouken@1501
   569
					if ( dstlen < 3 ) {
slouken@1501
   570
						return SDL_ICONV_E2BIG;
slouken@1501
   571
					}
slouken@1501
   572
					p[0] = 0xE0 | (Uint8)((ch >> 12) & 0x0F);
slouken@1501
   573
					p[1] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
slouken@1501
   574
					p[2] = 0x80 | (Uint8)(ch & 0x3F);
slouken@1501
   575
					dst += 3;
slouken@1501
   576
					dstlen -= 3;
slouken@1501
   577
				} else if ( ch <= 0x1FFFFF ) {
slouken@1501
   578
					if ( dstlen < 4 ) {
slouken@1501
   579
						return SDL_ICONV_E2BIG;
slouken@1501
   580
					}
slouken@1501
   581
					p[0] = 0xF0 | (Uint8)((ch >> 18) & 0x07);
slouken@1501
   582
					p[1] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
slouken@1501
   583
					p[2] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
slouken@1501
   584
					p[3] = 0x80 | (Uint8)(ch & 0x3F);
slouken@1501
   585
					dst += 4;
slouken@1501
   586
					dstlen -= 4;
slouken@1501
   587
				} else if ( ch <= 0x3FFFFFF ) {
slouken@1501
   588
					if ( dstlen < 5 ) {
slouken@1501
   589
						return SDL_ICONV_E2BIG;
slouken@1501
   590
					}
slouken@1501
   591
					p[0] = 0xF8 | (Uint8)((ch >> 24) & 0x03);
slouken@1501
   592
					p[1] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
slouken@1501
   593
					p[2] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
slouken@1501
   594
					p[3] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
slouken@1501
   595
					p[4] = 0x80 | (Uint8)(ch & 0x3F);
slouken@1501
   596
					dst += 5;
slouken@1501
   597
					dstlen -= 5;
slouken@1501
   598
				} else {
slouken@1501
   599
					if ( dstlen < 6 ) {
slouken@1501
   600
						return SDL_ICONV_E2BIG;
slouken@1501
   601
					}
slouken@1501
   602
					p[0] = 0xFC | (Uint8)((ch >> 30) & 0x01);
slouken@1501
   603
					p[1] = 0x80 | (Uint8)((ch >> 24) & 0x3F);
slouken@1501
   604
					p[2] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
slouken@1501
   605
					p[3] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
slouken@1501
   606
					p[4] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
slouken@1501
   607
					p[5] = 0x80 | (Uint8)(ch & 0x3F);
slouken@1501
   608
					dst += 6;
slouken@1501
   609
					dstlen -= 6;
slouken@1501
   610
				}
slouken@1501
   611
			}
slouken@1501
   612
			break;
slouken@1501
   613
		    case ENCODING_UTF16BE: /* RFC 2781 */
slouken@1501
   614
			{
slouken@1501
   615
				Uint8 *p = (Uint8 *)dst;
slouken@1501
   616
				if ( ch > 0x10FFFF ) {
slouken@1501
   617
					ch = UNKNOWN_UNICODE;
slouken@1501
   618
				}
slouken@1501
   619
				if ( ch < 0x10000 ) {
slouken@1501
   620
					if ( dstlen < 2 ) {
slouken@1501
   621
						return SDL_ICONV_E2BIG;
slouken@1501
   622
					}
slouken@1501
   623
					p[0] = (Uint8)(ch >> 8);
slouken@1501
   624
					p[1] = (Uint8)ch;
slouken@1501
   625
					dst += 2;
slouken@1501
   626
					dstlen -= 2;
slouken@1501
   627
				} else {
slouken@1501
   628
					Uint16 W1, W2;
slouken@1501
   629
					if ( dstlen < 4 ) {
slouken@1501
   630
						return SDL_ICONV_E2BIG;
slouken@1501
   631
					}
slouken@1501
   632
					ch = ch - 0x10000;
slouken@1501
   633
					W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
slouken@1501
   634
					W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
slouken@1501
   635
					p[0] = (Uint8)(W1 >> 8);
slouken@1501
   636
					p[1] = (Uint8)W1;
slouken@1501
   637
					p[2] = (Uint8)(W2 >> 8);
slouken@1501
   638
					p[3] = (Uint8)W2;
slouken@1501
   639
					dst += 4;
slouken@1501
   640
					dstlen -= 4;
slouken@1501
   641
				}
slouken@1501
   642
			}
slouken@1501
   643
			break;
slouken@1501
   644
		    case ENCODING_UTF16LE: /* RFC 2781 */
slouken@1501
   645
			{
slouken@1501
   646
				Uint8 *p = (Uint8 *)dst;
slouken@1501
   647
				if ( ch > 0x10FFFF ) {
slouken@1501
   648
					ch = UNKNOWN_UNICODE;
slouken@1501
   649
				}
slouken@1501
   650
				if ( ch < 0x10000 ) {
slouken@1501
   651
					if ( dstlen < 2 ) {
slouken@1501
   652
						return SDL_ICONV_E2BIG;
slouken@1501
   653
					}
slouken@1501
   654
					p[1] = (Uint8)(ch >> 8);
slouken@1501
   655
					p[0] = (Uint8)ch;
slouken@1501
   656
					dst += 2;
slouken@1501
   657
					dstlen -= 2;
slouken@1501
   658
				} else {
slouken@1501
   659
					Uint16 W1, W2;
slouken@1501
   660
					if ( dstlen < 4 ) {
slouken@1501
   661
						return SDL_ICONV_E2BIG;
slouken@1501
   662
					}
slouken@1501
   663
					ch = ch - 0x10000;
slouken@1501
   664
					W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
slouken@1501
   665
					W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
slouken@1501
   666
					p[1] = (Uint8)(W1 >> 8);
slouken@1501
   667
					p[0] = (Uint8)W1;
slouken@1501
   668
					p[3] = (Uint8)(W2 >> 8);
slouken@1501
   669
					p[2] = (Uint8)W2;
slouken@1501
   670
					dst += 4;
slouken@1501
   671
					dstlen -= 4;
slouken@1501
   672
				}
slouken@1501
   673
			}
slouken@1501
   674
			break;
slouken@1501
   675
		    case ENCODING_UTF32BE:
slouken@1501
   676
			{
slouken@1501
   677
				Uint8 *p = (Uint8 *)dst;
slouken@1502
   678
				if ( ch > 0x10FFFF ) {
slouken@1501
   679
					ch = UNKNOWN_UNICODE;
slouken@1501
   680
				}
slouken@1501
   681
				if ( dstlen < 4 ) {
slouken@1501
   682
					return SDL_ICONV_E2BIG;
slouken@1501
   683
				}
slouken@1501
   684
				p[0] = (Uint8)(ch >> 24);
slouken@1501
   685
				p[1] = (Uint8)(ch >> 16);
slouken@1501
   686
				p[2] = (Uint8)(ch >> 8);
slouken@1501
   687
				p[3] = (Uint8)ch;
slouken@1501
   688
				dst += 4;
slouken@1501
   689
				dstlen -= 4;
slouken@1501
   690
			}
slouken@1501
   691
			break;
slouken@1501
   692
		    case ENCODING_UTF32LE:
slouken@1501
   693
			{
slouken@1501
   694
				Uint8 *p = (Uint8 *)dst;
slouken@1502
   695
				if ( ch > 0x10FFFF ) {
slouken@1501
   696
					ch = UNKNOWN_UNICODE;
slouken@1501
   697
				}
slouken@1501
   698
				if ( dstlen < 4 ) {
slouken@1501
   699
					return SDL_ICONV_E2BIG;
slouken@1501
   700
				}
slouken@1501
   701
				p[3] = (Uint8)(ch >> 24);
slouken@1501
   702
				p[2] = (Uint8)(ch >> 16);
slouken@1501
   703
				p[1] = (Uint8)(ch >> 8);
slouken@1501
   704
				p[0] = (Uint8)ch;
slouken@1501
   705
				dst += 4;
slouken@1501
   706
				dstlen -= 4;
slouken@1501
   707
			}
slouken@1501
   708
			break;
slouken@1501
   709
		    case ENCODING_UCS2:
slouken@1501
   710
			{
slouken@1501
   711
				Uint16 *p = (Uint16 *)dst;
slouken@1501
   712
				if ( ch > 0xFFFF ) {
slouken@1501
   713
					ch = UNKNOWN_UNICODE;
slouken@1501
   714
				}
slouken@1501
   715
				if ( dstlen < 2 ) {
slouken@1501
   716
					return SDL_ICONV_E2BIG;
slouken@1501
   717
				}
slouken@1501
   718
				*p = (Uint16)ch;
slouken@1501
   719
				dst += 2;
slouken@1501
   720
				dstlen -= 2;
slouken@1501
   721
			}
slouken@1501
   722
			break;
slouken@1501
   723
		    case ENCODING_UCS4:
slouken@1501
   724
			{
slouken@1501
   725
				Uint32 *p = (Uint32 *)dst;
slouken@1501
   726
				if ( ch > 0x7FFFFFFF ) {
slouken@1501
   727
					ch = UNKNOWN_UNICODE;
slouken@1501
   728
				}
slouken@1501
   729
				if ( dstlen < 4 ) {
slouken@1501
   730
					return SDL_ICONV_E2BIG;
slouken@1501
   731
				}
slouken@1501
   732
				*p = ch;
slouken@1501
   733
				dst += 4;
slouken@1501
   734
				dstlen -= 4;
slouken@1501
   735
			}
slouken@1501
   736
			break;
slouken@1501
   737
		}
slouken@1501
   738
slouken@1501
   739
		/* Update state */
slouken@1501
   740
		*inbuf = src;
slouken@1501
   741
		*inbytesleft = srclen;
slouken@1501
   742
		*outbuf = dst;
slouken@1501
   743
		*outbytesleft = dstlen;
slouken@1501
   744
		++total;
slouken@1501
   745
	}
slouken@1501
   746
	return total;
slouken@1501
   747
}
slouken@1501
   748
slouken@1501
   749
int SDL_iconv_close(SDL_iconv_t cd)
slouken@1501
   750
{
slouken@1501
   751
	if ( cd && cd != (SDL_iconv_t)-1 ) {
slouken@1501
   752
		SDL_free(cd);
slouken@1501
   753
	}
slouken@1501
   754
	return 0;
slouken@1501
   755
}
slouken@1501
   756
slouken@1501
   757
#endif /* !HAVE_ICONV */
slouken@1501
   758
icculus@3918
   759
char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, size_t inbytesleft)
slouken@1501
   760
{
slouken@1501
   761
	SDL_iconv_t cd;
slouken@1501
   762
	char *string;
slouken@1501
   763
	size_t stringsize;
slouken@1501
   764
	char *outbuf;
slouken@1501
   765
	size_t outbytesleft;
slouken@1501
   766
	size_t retCode = 0;
slouken@1501
   767
slouken@1501
   768
	cd = SDL_iconv_open(tocode, fromcode);
slouken@1501
   769
	if ( cd == (SDL_iconv_t)-1 ) {
slouken@1501
   770
		return NULL;
slouken@1501
   771
	}
slouken@1501
   772
slouken@1501
   773
	stringsize = inbytesleft > 4 ? inbytesleft : 4;
slouken@1501
   774
	string = SDL_malloc(stringsize);
slouken@1501
   775
	if ( !string ) {
slouken@1501
   776
		SDL_iconv_close(cd);
slouken@1501
   777
		return NULL;
slouken@1501
   778
	}
slouken@1501
   779
	outbuf = string;
slouken@1501
   780
	outbytesleft = stringsize;
slouken@1501
   781
	SDL_memset(outbuf, 0, 4);
slouken@1501
   782
slouken@1501
   783
	while ( inbytesleft > 0 ) {
slouken@1501
   784
		retCode = SDL_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
slouken@1501
   785
		switch (retCode) {
slouken@1501
   786
		    case SDL_ICONV_E2BIG:
slouken@1501
   787
			{
slouken@1501
   788
				char *oldstring = string;
slouken@1501
   789
				stringsize *= 2;
slouken@1501
   790
				string = SDL_realloc(string, stringsize);
slouken@1501
   791
				if ( !string ) {
slouken@1501
   792
					SDL_iconv_close(cd);
slouken@1501
   793
					return NULL;
slouken@1501
   794
				}
slouken@1501
   795
				outbuf = string + (outbuf - oldstring);
slouken@1501
   796
				outbytesleft = stringsize - (outbuf - string);
slouken@1501
   797
				SDL_memset(outbuf, 0, 4);
slouken@1501
   798
			}
slouken@1501
   799
			break;
slouken@1501
   800
		    case SDL_ICONV_EILSEQ:
slouken@1501
   801
			/* Try skipping some input data - not perfect, but... */
slouken@1501
   802
			++inbuf;
slouken@1501
   803
			--inbytesleft;
slouken@1501
   804
			break;
slouken@1501
   805
		    case SDL_ICONV_EINVAL:
slouken@1501
   806
		    case SDL_ICONV_ERROR:
slouken@1501
   807
			/* We can't continue... */
slouken@1501
   808
			inbytesleft = 0;
slouken@1501
   809
			break;
slouken@1501
   810
		}
slouken@1501
   811
	}
slouken@1501
   812
	SDL_iconv_close(cd);
slouken@1501
   813
slouken@1501
   814
	return string;
slouken@1501
   815
}