src/stdlib/SDL_iconv.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5262 b530ef003506
child 5633 21a6e87905e3
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@1501
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@1501
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1501
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1501
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1501
    20
*/
slouken@1501
    21
#include "SDL_config.h"
slouken@1501
    22
slouken@1501
    23
/* This file contains portable iconv functions for SDL */
slouken@1501
    24
slouken@1501
    25
#include "SDL_stdinc.h"
slouken@1501
    26
#include "SDL_endian.h"
slouken@1501
    27
slouken@1501
    28
#ifdef HAVE_ICONV
slouken@1501
    29
slouken@2136
    30
/* Depending on which standard the iconv() was implemented with,
slouken@2136
    31
   iconv() may or may not use const char ** for the inbuf param.
slouken@2136
    32
   If we get this wrong, it's just a warning, so no big deal.
slouken@2136
    33
*/
slouken@2136
    34
#if defined(_XGP6) || \
slouken@2136
    35
    defined(__GLIBC__) && ((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
slouken@2136
    36
#define ICONV_INBUF_NONCONST
slouken@2136
    37
#endif
slouken@2136
    38
slouken@1501
    39
#include <errno.h>
slouken@1501
    40
slouken@1895
    41
size_t
slouken@1895
    42
SDL_iconv(SDL_iconv_t cd,
slouken@2135
    43
          const char **inbuf, size_t * inbytesleft,
slouken@1895
    44
          char **outbuf, size_t * outbytesleft)
slouken@1501
    45
{
slouken@2135
    46
    size_t retCode;
slouken@2136
    47
#ifdef ICONV_INBUF_NONCONST
slouken@2136
    48
    retCode = iconv(cd, (char **) inbuf, inbytesleft, outbuf, outbytesleft);
slouken@2135
    49
#else
slouken@2136
    50
    retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
slouken@2135
    51
#endif
slouken@1895
    52
    if (retCode == (size_t) - 1) {
slouken@1895
    53
        switch (errno) {
slouken@1895
    54
        case E2BIG:
slouken@1895
    55
            return SDL_ICONV_E2BIG;
slouken@1895
    56
        case EILSEQ:
slouken@1895
    57
            return SDL_ICONV_EILSEQ;
slouken@1895
    58
        case EINVAL:
slouken@1895
    59
            return SDL_ICONV_EINVAL;
slouken@1895
    60
        default:
slouken@1895
    61
            return SDL_ICONV_ERROR;
slouken@1895
    62
        }
slouken@1895
    63
    }
slouken@1895
    64
    return retCode;
slouken@1501
    65
}
slouken@1501
    66
slouken@1501
    67
#else
slouken@1501
    68
slouken@1503
    69
/* Lots of useful information on Unicode at:
slouken@1503
    70
	http://www.cl.cam.ac.uk/~mgk25/unicode.html
slouken@1503
    71
*/
slouken@1503
    72
slouken@1501
    73
#define UNICODE_BOM	0xFEFF
slouken@1501
    74
slouken@1501
    75
#define UNKNOWN_ASCII	'?'
slouken@1501
    76
#define UNKNOWN_UNICODE	0xFFFD
slouken@1501
    77
slouken@1895
    78
enum
slouken@1895
    79
{
slouken@1895
    80
    ENCODING_UNKNOWN,
slouken@1895
    81
    ENCODING_ASCII,
slouken@1895
    82
    ENCODING_LATIN1,
slouken@1895
    83
    ENCODING_UTF8,
slouken@1895
    84
    ENCODING_UTF16,             /* Needs byte order marker */
slouken@1895
    85
    ENCODING_UTF16BE,
slouken@1895
    86
    ENCODING_UTF16LE,
slouken@1895
    87
    ENCODING_UTF32,             /* Needs byte order marker */
slouken@1895
    88
    ENCODING_UTF32BE,
slouken@1895
    89
    ENCODING_UTF32LE,
slouken@1895
    90
    ENCODING_UCS2,              /* Native byte order assumed */
slouken@1895
    91
    ENCODING_UCS4,              /* Native byte order assumed */
slouken@1501
    92
};
slouken@1501
    93
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1501
    94
#define ENCODING_UTF16NATIVE	ENCODING_UTF16BE
slouken@1501
    95
#define ENCODING_UTF32NATIVE	ENCODING_UTF32BE
slouken@1501
    96
#else
slouken@1501
    97
#define ENCODING_UTF16NATIVE	ENCODING_UTF16LE
slouken@1501
    98
#define ENCODING_UTF32NATIVE	ENCODING_UTF32LE
slouken@1501
    99
#endif
slouken@1501
   100
slouken@1501
   101
struct _SDL_iconv_t
slouken@1501
   102
{
slouken@1895
   103
    int src_fmt;
slouken@1895
   104
    int dst_fmt;
slouken@1501
   105
};
slouken@1501
   106
slouken@1895
   107
static struct
slouken@1895
   108
{
slouken@1895
   109
    const char *name;
slouken@1895
   110
    int format;
slouken@1501
   111
} encodings[] = {
slouken@2142
   112
/* *INDENT-OFF* */
slouken@2142
   113
    { "ASCII", ENCODING_ASCII },
slouken@2142
   114
    { "US-ASCII", ENCODING_ASCII },
slouken@2142
   115
    { "8859-1", ENCODING_LATIN1 },
slouken@2142
   116
    { "ISO-8859-1", ENCODING_LATIN1 },
slouken@2142
   117
    { "UTF8", ENCODING_UTF8 },
slouken@2142
   118
    { "UTF-8", ENCODING_UTF8 },
slouken@2142
   119
    { "UTF16", ENCODING_UTF16 },
slouken@2142
   120
    { "UTF-16", ENCODING_UTF16 },
slouken@2142
   121
    { "UTF16BE", ENCODING_UTF16BE },
slouken@2142
   122
    { "UTF-16BE", ENCODING_UTF16BE },
slouken@2142
   123
    { "UTF16LE", ENCODING_UTF16LE },
slouken@2142
   124
    { "UTF-16LE", ENCODING_UTF16LE },
slouken@2142
   125
    { "UTF32", ENCODING_UTF32 },
slouken@2142
   126
    { "UTF-32", ENCODING_UTF32 },
slouken@2142
   127
    { "UTF32BE", ENCODING_UTF32BE },
slouken@2142
   128
    { "UTF-32BE", ENCODING_UTF32BE },
slouken@2142
   129
    { "UTF32LE", ENCODING_UTF32LE },
slouken@2142
   130
    { "UTF-32LE", ENCODING_UTF32LE },
slouken@2142
   131
    { "UCS2", ENCODING_UCS2 },
slouken@2142
   132
    { "UCS-2", ENCODING_UCS2 },
slouken@2142
   133
    { "UCS4", ENCODING_UCS4 },
slouken@2142
   134
    { "UCS-4", ENCODING_UCS4 },
slouken@2142
   135
/* *INDENT-ON* */
slouken@2142
   136
};
slouken@1501
   137
slouken@2182
   138
static const char *
slouken@2184
   139
getlocale(char *buffer, size_t bufsize)
slouken@2182
   140
{
slouken@2182
   141
    const char *lang;
slouken@2184
   142
    char *ptr;
slouken@2182
   143
slouken@2182
   144
    lang = SDL_getenv("LC_ALL");
slouken@2182
   145
    if (!lang) {
slouken@2182
   146
        lang = SDL_getenv("LC_CTYPE");
slouken@2182
   147
    }
slouken@2182
   148
    if (!lang) {
slouken@2182
   149
        lang = SDL_getenv("LC_MESSAGES");
slouken@2182
   150
    }
slouken@2182
   151
    if (!lang) {
slouken@2182
   152
        lang = SDL_getenv("LANG");
slouken@2182
   153
    }
slouken@2182
   154
    if (!lang || !*lang || SDL_strcmp(lang, "C") == 0) {
slouken@2182
   155
        lang = "ASCII";
slouken@2182
   156
    }
slouken@2184
   157
slouken@2184
   158
    /* We need to trim down strings like "en_US.UTF-8@blah" to "UTF-8" */
slouken@2184
   159
    ptr = SDL_strchr(lang, '.');
slouken@2184
   160
    if (ptr != NULL) {
slouken@2184
   161
        lang = ptr + 1;
slouken@2184
   162
    }
slouken@2184
   163
slouken@2184
   164
    SDL_strlcpy(buffer, lang, bufsize);
slouken@2184
   165
    ptr = SDL_strchr(buffer, '@');
slouken@2184
   166
    if (ptr != NULL) {
slouken@2184
   167
        *ptr = '\0';            /* chop end of string. */
slouken@2184
   168
    }
slouken@2184
   169
slouken@2184
   170
    return buffer;
slouken@2182
   171
}
slouken@2182
   172
slouken@1895
   173
SDL_iconv_t
slouken@1895
   174
SDL_iconv_open(const char *tocode, const char *fromcode)
slouken@1501
   175
{
slouken@1895
   176
    int src_fmt = ENCODING_UNKNOWN;
slouken@1895
   177
    int dst_fmt = ENCODING_UNKNOWN;
slouken@1895
   178
    int i;
slouken@2184
   179
    char fromcode_buffer[64];
slouken@2184
   180
    char tocode_buffer[64];
slouken@1501
   181
slouken@2182
   182
    if (!fromcode || !*fromcode) {
slouken@2184
   183
        fromcode = getlocale(fromcode_buffer, sizeof(fromcode_buffer));
slouken@2182
   184
    }
slouken@2182
   185
    if (!tocode || !*tocode) {
slouken@2184
   186
        tocode = getlocale(tocode_buffer, sizeof(tocode_buffer));
slouken@2182
   187
    }
slouken@1895
   188
    for (i = 0; i < SDL_arraysize(encodings); ++i) {
slouken@1895
   189
        if (SDL_strcasecmp(fromcode, encodings[i].name) == 0) {
slouken@1895
   190
            src_fmt = encodings[i].format;
slouken@1895
   191
            if (dst_fmt != ENCODING_UNKNOWN) {
slouken@1895
   192
                break;
slouken@1895
   193
            }
slouken@1895
   194
        }
slouken@1895
   195
        if (SDL_strcasecmp(tocode, encodings[i].name) == 0) {
slouken@1895
   196
            dst_fmt = encodings[i].format;
slouken@1895
   197
            if (src_fmt != ENCODING_UNKNOWN) {
slouken@1895
   198
                break;
slouken@1895
   199
            }
slouken@1895
   200
        }
slouken@1895
   201
    }
slouken@1895
   202
    if (src_fmt != ENCODING_UNKNOWN && dst_fmt != ENCODING_UNKNOWN) {
slouken@1895
   203
        SDL_iconv_t cd = (SDL_iconv_t) SDL_malloc(sizeof(*cd));
slouken@1895
   204
        if (cd) {
slouken@1895
   205
            cd->src_fmt = src_fmt;
slouken@1895
   206
            cd->dst_fmt = dst_fmt;
slouken@1895
   207
            return cd;
slouken@1895
   208
        }
slouken@1895
   209
    }
slouken@1895
   210
    return (SDL_iconv_t) - 1;
slouken@1501
   211
}
slouken@1501
   212
slouken@1895
   213
size_t
slouken@1895
   214
SDL_iconv(SDL_iconv_t cd,
slouken@2135
   215
          const char **inbuf, size_t * inbytesleft,
slouken@1895
   216
          char **outbuf, size_t * outbytesleft)
slouken@1501
   217
{
slouken@1895
   218
    /* For simplicity, we'll convert everything to and from UCS-4 */
slouken@2135
   219
    const char *src;
slouken@2135
   220
    char *dst;
slouken@1895
   221
    size_t srclen, dstlen;
slouken@2135
   222
    Uint32 ch = 0;
slouken@1895
   223
    size_t total;
slouken@1501
   224
slouken@1895
   225
    if (!inbuf || !*inbuf) {
slouken@1895
   226
        /* Reset the context */
slouken@1895
   227
        return 0;
slouken@1895
   228
    }
slouken@1895
   229
    if (!outbuf || !*outbuf || !outbytesleft || !*outbytesleft) {
slouken@1895
   230
        return SDL_ICONV_E2BIG;
slouken@1895
   231
    }
slouken@1895
   232
    src = *inbuf;
slouken@1895
   233
    srclen = (inbytesleft ? *inbytesleft : 0);
slouken@1895
   234
    dst = *outbuf;
slouken@1895
   235
    dstlen = *outbytesleft;
slouken@1501
   236
slouken@1895
   237
    switch (cd->src_fmt) {
slouken@1895
   238
    case ENCODING_UTF16:
slouken@1895
   239
        /* Scan for a byte order marker */
slouken@1895
   240
        {
slouken@1895
   241
            Uint8 *p = (Uint8 *) src;
slouken@1895
   242
            size_t n = srclen / 2;
slouken@1895
   243
            while (n) {
slouken@1895
   244
                if (p[0] == 0xFF && p[1] == 0xFE) {
slouken@1895
   245
                    cd->src_fmt = ENCODING_UTF16BE;
slouken@1895
   246
                    break;
slouken@1895
   247
                } else if (p[0] == 0xFE && p[1] == 0xFF) {
slouken@1895
   248
                    cd->src_fmt = ENCODING_UTF16LE;
slouken@1895
   249
                    break;
slouken@1895
   250
                }
slouken@1895
   251
                p += 2;
slouken@1895
   252
                --n;
slouken@1895
   253
            }
slouken@1895
   254
            if (n == 0) {
slouken@1895
   255
                /* We can't tell, default to host order */
slouken@1895
   256
                cd->src_fmt = ENCODING_UTF16NATIVE;
slouken@1895
   257
            }
slouken@1895
   258
        }
slouken@1895
   259
        break;
slouken@1895
   260
    case ENCODING_UTF32:
slouken@1895
   261
        /* Scan for a byte order marker */
slouken@1895
   262
        {
slouken@1895
   263
            Uint8 *p = (Uint8 *) src;
slouken@1895
   264
            size_t n = srclen / 4;
slouken@1895
   265
            while (n) {
slouken@1895
   266
                if (p[0] == 0xFF && p[1] == 0xFE &&
slouken@1895
   267
                    p[2] == 0x00 && p[3] == 0x00) {
slouken@1895
   268
                    cd->src_fmt = ENCODING_UTF32BE;
slouken@1895
   269
                    break;
slouken@1895
   270
                } else if (p[0] == 0x00 && p[1] == 0x00 &&
slouken@1895
   271
                           p[2] == 0xFE && p[3] == 0xFF) {
slouken@1895
   272
                    cd->src_fmt = ENCODING_UTF32LE;
slouken@1895
   273
                    break;
slouken@1895
   274
                }
slouken@1895
   275
                p += 4;
slouken@1895
   276
                --n;
slouken@1895
   277
            }
slouken@1895
   278
            if (n == 0) {
slouken@1895
   279
                /* We can't tell, default to host order */
slouken@1895
   280
                cd->src_fmt = ENCODING_UTF32NATIVE;
slouken@1895
   281
            }
slouken@1895
   282
        }
slouken@1895
   283
        break;
slouken@1895
   284
    }
slouken@1501
   285
slouken@1895
   286
    switch (cd->dst_fmt) {
slouken@1895
   287
    case ENCODING_UTF16:
slouken@1895
   288
        /* Default to host order, need to add byte order marker */
slouken@1895
   289
        if (dstlen < 2) {
slouken@1895
   290
            return SDL_ICONV_E2BIG;
slouken@1895
   291
        }
slouken@1895
   292
        *(Uint16 *) dst = UNICODE_BOM;
slouken@1895
   293
        dst += 2;
slouken@1895
   294
        dstlen -= 2;
slouken@1895
   295
        cd->dst_fmt = ENCODING_UTF16NATIVE;
slouken@1895
   296
        break;
slouken@1895
   297
    case ENCODING_UTF32:
slouken@1895
   298
        /* Default to host order, need to add byte order marker */
slouken@1895
   299
        if (dstlen < 4) {
slouken@1895
   300
            return SDL_ICONV_E2BIG;
slouken@1895
   301
        }
slouken@1895
   302
        *(Uint32 *) dst = UNICODE_BOM;
slouken@1895
   303
        dst += 4;
slouken@1895
   304
        dstlen -= 4;
slouken@1895
   305
        cd->dst_fmt = ENCODING_UTF32NATIVE;
slouken@1895
   306
        break;
slouken@1895
   307
    }
slouken@1501
   308
slouken@1895
   309
    total = 0;
slouken@1895
   310
    while (srclen > 0) {
slouken@1895
   311
        /* Decode a character */
slouken@1895
   312
        switch (cd->src_fmt) {
slouken@1895
   313
        case ENCODING_ASCII:
slouken@1895
   314
            {
slouken@1895
   315
                Uint8 *p = (Uint8 *) src;
slouken@1895
   316
                ch = (Uint32) (p[0] & 0x7F);
slouken@1895
   317
                ++src;
slouken@1895
   318
                --srclen;
slouken@1895
   319
            }
slouken@1895
   320
            break;
slouken@1895
   321
        case ENCODING_LATIN1:
slouken@1895
   322
            {
slouken@1895
   323
                Uint8 *p = (Uint8 *) src;
slouken@1895
   324
                ch = (Uint32) p[0];
slouken@1895
   325
                ++src;
slouken@1895
   326
                --srclen;
slouken@1895
   327
            }
slouken@1895
   328
            break;
slouken@1895
   329
        case ENCODING_UTF8:    /* RFC 3629 */
slouken@1895
   330
            {
slouken@1895
   331
                Uint8 *p = (Uint8 *) src;
slouken@1895
   332
                size_t left = 0;
slouken@1895
   333
                SDL_bool overlong = SDL_FALSE;
slouken@1895
   334
                if (p[0] >= 0xFC) {
slouken@1895
   335
                    if ((p[0] & 0xFE) != 0xFC) {
slouken@1895
   336
                        /* Skip illegal sequences
slouken@1895
   337
                           return SDL_ICONV_EILSEQ;
slouken@1895
   338
                         */
slouken@1895
   339
                        ch = UNKNOWN_UNICODE;
slouken@1895
   340
                    } else {
slouken@1895
   341
                        if (p[0] == 0xFC) {
slouken@1895
   342
                            overlong = SDL_TRUE;
slouken@1895
   343
                        }
slouken@1895
   344
                        ch = (Uint32) (p[0] & 0x01);
slouken@1895
   345
                        left = 5;
slouken@1895
   346
                    }
slouken@1895
   347
                } else if (p[0] >= 0xF8) {
slouken@1895
   348
                    if ((p[0] & 0xFC) != 0xF8) {
slouken@1895
   349
                        /* Skip illegal sequences
slouken@1895
   350
                           return SDL_ICONV_EILSEQ;
slouken@1895
   351
                         */
slouken@1895
   352
                        ch = UNKNOWN_UNICODE;
slouken@1895
   353
                    } else {
slouken@1895
   354
                        if (p[0] == 0xF8) {
slouken@1895
   355
                            overlong = SDL_TRUE;
slouken@1895
   356
                        }
slouken@1895
   357
                        ch = (Uint32) (p[0] & 0x03);
slouken@1895
   358
                        left = 4;
slouken@1895
   359
                    }
slouken@1895
   360
                } else if (p[0] >= 0xF0) {
slouken@1895
   361
                    if ((p[0] & 0xF8) != 0xF0) {
slouken@1895
   362
                        /* Skip illegal sequences
slouken@1895
   363
                           return SDL_ICONV_EILSEQ;
slouken@1895
   364
                         */
slouken@1895
   365
                        ch = UNKNOWN_UNICODE;
slouken@1895
   366
                    } else {
slouken@1895
   367
                        if (p[0] == 0xF0) {
slouken@1895
   368
                            overlong = SDL_TRUE;
slouken@1895
   369
                        }
slouken@1895
   370
                        ch = (Uint32) (p[0] & 0x07);
slouken@1895
   371
                        left = 3;
slouken@1895
   372
                    }
slouken@1895
   373
                } else if (p[0] >= 0xE0) {
slouken@1895
   374
                    if ((p[0] & 0xF0) != 0xE0) {
slouken@1895
   375
                        /* Skip illegal sequences
slouken@1895
   376
                           return SDL_ICONV_EILSEQ;
slouken@1895
   377
                         */
slouken@1895
   378
                        ch = UNKNOWN_UNICODE;
slouken@1895
   379
                    } else {
slouken@1895
   380
                        if (p[0] == 0xE0) {
slouken@1895
   381
                            overlong = SDL_TRUE;
slouken@1895
   382
                        }
slouken@1895
   383
                        ch = (Uint32) (p[0] & 0x0F);
slouken@1895
   384
                        left = 2;
slouken@1895
   385
                    }
slouken@1895
   386
                } else if (p[0] >= 0xC0) {
slouken@1895
   387
                    if ((p[0] & 0xE0) != 0xC0) {
slouken@1895
   388
                        /* Skip illegal sequences
slouken@1895
   389
                           return SDL_ICONV_EILSEQ;
slouken@1895
   390
                         */
slouken@1895
   391
                        ch = UNKNOWN_UNICODE;
slouken@1895
   392
                    } else {
slouken@3539
   393
                        if ((p[0] & 0xDE) == 0xC0) {
slouken@1895
   394
                            overlong = SDL_TRUE;
slouken@1895
   395
                        }
slouken@1895
   396
                        ch = (Uint32) (p[0] & 0x1F);
slouken@1895
   397
                        left = 1;
slouken@1895
   398
                    }
slouken@1895
   399
                } else {
slouken@1895
   400
                    if ((p[0] & 0x80) != 0x00) {
slouken@1895
   401
                        /* Skip illegal sequences
slouken@1895
   402
                           return SDL_ICONV_EILSEQ;
slouken@1895
   403
                         */
slouken@1895
   404
                        ch = UNKNOWN_UNICODE;
slouken@1895
   405
                    } else {
slouken@1895
   406
                        ch = (Uint32) p[0];
slouken@1895
   407
                    }
slouken@1895
   408
                }
slouken@1895
   409
                ++src;
slouken@1895
   410
                --srclen;
slouken@1895
   411
                if (srclen < left) {
slouken@1895
   412
                    return SDL_ICONV_EINVAL;
slouken@1895
   413
                }
slouken@1895
   414
                while (left--) {
slouken@1895
   415
                    ++p;
slouken@1895
   416
                    if ((p[0] & 0xC0) != 0x80) {
slouken@1895
   417
                        /* Skip illegal sequences
slouken@1895
   418
                           return SDL_ICONV_EILSEQ;
slouken@1895
   419
                         */
slouken@1895
   420
                        ch = UNKNOWN_UNICODE;
slouken@1895
   421
                        break;
slouken@1895
   422
                    }
slouken@1895
   423
                    ch <<= 6;
slouken@1895
   424
                    ch |= (p[0] & 0x3F);
slouken@1895
   425
                    ++src;
slouken@1895
   426
                    --srclen;
slouken@1895
   427
                }
slouken@1895
   428
                if (overlong) {
slouken@1895
   429
                    /* Potential security risk
slouken@1895
   430
                       return SDL_ICONV_EILSEQ;
slouken@1895
   431
                     */
slouken@1895
   432
                    ch = UNKNOWN_UNICODE;
slouken@1895
   433
                }
slouken@1895
   434
                if ((ch >= 0xD800 && ch <= 0xDFFF) ||
slouken@1895
   435
                    (ch == 0xFFFE || ch == 0xFFFF) || ch > 0x10FFFF) {
slouken@1895
   436
                    /* Skip illegal sequences
slouken@1895
   437
                       return SDL_ICONV_EILSEQ;
slouken@1895
   438
                     */
slouken@1895
   439
                    ch = UNKNOWN_UNICODE;
slouken@1895
   440
                }
slouken@1895
   441
            }
slouken@1895
   442
            break;
slouken@1895
   443
        case ENCODING_UTF16BE: /* RFC 2781 */
slouken@1895
   444
            {
slouken@1895
   445
                Uint8 *p = (Uint8 *) src;
slouken@1895
   446
                Uint16 W1, W2;
slouken@1895
   447
                if (srclen < 2) {
slouken@1895
   448
                    return SDL_ICONV_EINVAL;
slouken@1895
   449
                }
slouken@1895
   450
                W1 = ((Uint16) p[0] << 8) | (Uint16) p[1];
slouken@1895
   451
                src += 2;
slouken@1895
   452
                srclen -= 2;
slouken@1895
   453
                if (W1 < 0xD800 || W1 > 0xDFFF) {
slouken@1895
   454
                    ch = (Uint32) W1;
slouken@1895
   455
                    break;
slouken@1895
   456
                }
slouken@1895
   457
                if (W1 > 0xDBFF) {
slouken@1895
   458
                    /* Skip illegal sequences
slouken@1895
   459
                       return SDL_ICONV_EILSEQ;
slouken@1895
   460
                     */
slouken@1895
   461
                    ch = UNKNOWN_UNICODE;
slouken@1895
   462
                    break;
slouken@1895
   463
                }
slouken@1895
   464
                if (srclen < 2) {
slouken@1895
   465
                    return SDL_ICONV_EINVAL;
slouken@1895
   466
                }
slouken@1895
   467
                p = (Uint8 *) src;
slouken@1895
   468
                W2 = ((Uint16) p[0] << 8) | (Uint16) p[1];
slouken@1895
   469
                src += 2;
slouken@1895
   470
                srclen -= 2;
slouken@1895
   471
                if (W2 < 0xDC00 || W2 > 0xDFFF) {
slouken@1895
   472
                    /* Skip illegal sequences
slouken@1895
   473
                       return SDL_ICONV_EILSEQ;
slouken@1895
   474
                     */
slouken@1895
   475
                    ch = UNKNOWN_UNICODE;
slouken@1895
   476
                    break;
slouken@1895
   477
                }
slouken@1895
   478
                ch = (((Uint32) (W1 & 0x3FF) << 10) |
slouken@1895
   479
                      (Uint32) (W2 & 0x3FF)) + 0x10000;
slouken@1895
   480
            }
slouken@1895
   481
            break;
slouken@1895
   482
        case ENCODING_UTF16LE: /* RFC 2781 */
slouken@1895
   483
            {
slouken@1895
   484
                Uint8 *p = (Uint8 *) src;
slouken@1895
   485
                Uint16 W1, W2;
slouken@1895
   486
                if (srclen < 2) {
slouken@1895
   487
                    return SDL_ICONV_EINVAL;
slouken@1895
   488
                }
slouken@1895
   489
                W1 = ((Uint16) p[1] << 8) | (Uint16) p[0];
slouken@1895
   490
                src += 2;
slouken@1895
   491
                srclen -= 2;
slouken@1895
   492
                if (W1 < 0xD800 || W1 > 0xDFFF) {
slouken@1895
   493
                    ch = (Uint32) W1;
slouken@1895
   494
                    break;
slouken@1895
   495
                }
slouken@1895
   496
                if (W1 > 0xDBFF) {
slouken@1895
   497
                    /* Skip illegal sequences
slouken@1895
   498
                       return SDL_ICONV_EILSEQ;
slouken@1895
   499
                     */
slouken@1895
   500
                    ch = UNKNOWN_UNICODE;
slouken@1895
   501
                    break;
slouken@1895
   502
                }
slouken@1895
   503
                if (srclen < 2) {
slouken@1895
   504
                    return SDL_ICONV_EINVAL;
slouken@1895
   505
                }
slouken@1895
   506
                p = (Uint8 *) src;
slouken@1895
   507
                W2 = ((Uint16) p[1] << 8) | (Uint16) p[0];
slouken@1895
   508
                src += 2;
slouken@1895
   509
                srclen -= 2;
slouken@1895
   510
                if (W2 < 0xDC00 || W2 > 0xDFFF) {
slouken@1895
   511
                    /* Skip illegal sequences
slouken@1895
   512
                       return SDL_ICONV_EILSEQ;
slouken@1895
   513
                     */
slouken@1895
   514
                    ch = UNKNOWN_UNICODE;
slouken@1895
   515
                    break;
slouken@1895
   516
                }
slouken@1895
   517
                ch = (((Uint32) (W1 & 0x3FF) << 10) |
slouken@1895
   518
                      (Uint32) (W2 & 0x3FF)) + 0x10000;
slouken@1895
   519
            }
slouken@1895
   520
            break;
slouken@1895
   521
        case ENCODING_UTF32BE:
slouken@1895
   522
            {
slouken@1895
   523
                Uint8 *p = (Uint8 *) src;
slouken@1895
   524
                if (srclen < 4) {
slouken@1895
   525
                    return SDL_ICONV_EINVAL;
slouken@1895
   526
                }
slouken@1895
   527
                ch = ((Uint32) p[0] << 24) |
slouken@1895
   528
                    ((Uint32) p[1] << 16) |
slouken@1895
   529
                    ((Uint32) p[2] << 8) | (Uint32) p[3];
slouken@1895
   530
                src += 4;
slouken@1895
   531
                srclen -= 4;
slouken@1895
   532
            }
slouken@1895
   533
            break;
slouken@1895
   534
        case ENCODING_UTF32LE:
slouken@1895
   535
            {
slouken@1895
   536
                Uint8 *p = (Uint8 *) src;
slouken@1895
   537
                if (srclen < 4) {
slouken@1895
   538
                    return SDL_ICONV_EINVAL;
slouken@1895
   539
                }
slouken@1895
   540
                ch = ((Uint32) p[3] << 24) |
slouken@1895
   541
                    ((Uint32) p[2] << 16) |
slouken@1895
   542
                    ((Uint32) p[1] << 8) | (Uint32) p[0];
slouken@1895
   543
                src += 4;
slouken@1895
   544
                srclen -= 4;
slouken@1895
   545
            }
slouken@1895
   546
            break;
slouken@1895
   547
        case ENCODING_UCS2:
slouken@1895
   548
            {
slouken@1895
   549
                Uint16 *p = (Uint16 *) src;
slouken@1895
   550
                if (srclen < 2) {
slouken@1895
   551
                    return SDL_ICONV_EINVAL;
slouken@1895
   552
                }
slouken@1895
   553
                ch = *p;
slouken@1895
   554
                src += 2;
slouken@1895
   555
                srclen -= 2;
slouken@1895
   556
            }
slouken@1895
   557
            break;
slouken@1895
   558
        case ENCODING_UCS4:
slouken@1895
   559
            {
slouken@1895
   560
                Uint32 *p = (Uint32 *) src;
slouken@1895
   561
                if (srclen < 4) {
slouken@1895
   562
                    return SDL_ICONV_EINVAL;
slouken@1895
   563
                }
slouken@1895
   564
                ch = *p;
slouken@1895
   565
                src += 4;
slouken@1895
   566
                srclen -= 4;
slouken@1895
   567
            }
slouken@1895
   568
            break;
slouken@1895
   569
        }
slouken@1501
   570
slouken@1895
   571
        /* Encode a character */
slouken@1895
   572
        switch (cd->dst_fmt) {
slouken@1895
   573
        case ENCODING_ASCII:
slouken@1895
   574
            {
slouken@1895
   575
                Uint8 *p = (Uint8 *) dst;
slouken@1895
   576
                if (dstlen < 1) {
slouken@1895
   577
                    return SDL_ICONV_E2BIG;
slouken@1895
   578
                }
slouken@1895
   579
                if (ch > 0x7F) {
slouken@1895
   580
                    *p = UNKNOWN_ASCII;
slouken@1895
   581
                } else {
slouken@1895
   582
                    *p = (Uint8) ch;
slouken@1895
   583
                }
slouken@1895
   584
                ++dst;
slouken@1895
   585
                --dstlen;
slouken@1895
   586
            }
slouken@1895
   587
            break;
slouken@1895
   588
        case ENCODING_LATIN1:
slouken@1895
   589
            {
slouken@1895
   590
                Uint8 *p = (Uint8 *) dst;
slouken@1895
   591
                if (dstlen < 1) {
slouken@1895
   592
                    return SDL_ICONV_E2BIG;
slouken@1895
   593
                }
slouken@1895
   594
                if (ch > 0xFF) {
slouken@1895
   595
                    *p = UNKNOWN_ASCII;
slouken@1895
   596
                } else {
slouken@1895
   597
                    *p = (Uint8) ch;
slouken@1895
   598
                }
slouken@1895
   599
                ++dst;
slouken@1895
   600
                --dstlen;
slouken@1895
   601
            }
slouken@1895
   602
            break;
slouken@1895
   603
        case ENCODING_UTF8:    /* RFC 3629 */
slouken@1895
   604
            {
slouken@1895
   605
                Uint8 *p = (Uint8 *) dst;
slouken@1895
   606
                if (ch > 0x10FFFF) {
slouken@1895
   607
                    ch = UNKNOWN_UNICODE;
slouken@1895
   608
                }
slouken@1895
   609
                if (ch <= 0x7F) {
slouken@1895
   610
                    if (dstlen < 1) {
slouken@1895
   611
                        return SDL_ICONV_E2BIG;
slouken@1895
   612
                    }
slouken@1895
   613
                    *p = (Uint8) ch;
slouken@1895
   614
                    ++dst;
slouken@1895
   615
                    --dstlen;
slouken@1895
   616
                } else if (ch <= 0x7FF) {
slouken@1895
   617
                    if (dstlen < 2) {
slouken@1895
   618
                        return SDL_ICONV_E2BIG;
slouken@1895
   619
                    }
slouken@1895
   620
                    p[0] = 0xC0 | (Uint8) ((ch >> 6) & 0x1F);
slouken@1895
   621
                    p[1] = 0x80 | (Uint8) (ch & 0x3F);
slouken@1895
   622
                    dst += 2;
slouken@1895
   623
                    dstlen -= 2;
slouken@1895
   624
                } else if (ch <= 0xFFFF) {
slouken@1895
   625
                    if (dstlen < 3) {
slouken@1895
   626
                        return SDL_ICONV_E2BIG;
slouken@1895
   627
                    }
slouken@1895
   628
                    p[0] = 0xE0 | (Uint8) ((ch >> 12) & 0x0F);
slouken@1895
   629
                    p[1] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
slouken@1895
   630
                    p[2] = 0x80 | (Uint8) (ch & 0x3F);
slouken@1895
   631
                    dst += 3;
slouken@1895
   632
                    dstlen -= 3;
slouken@1895
   633
                } else if (ch <= 0x1FFFFF) {
slouken@1895
   634
                    if (dstlen < 4) {
slouken@1895
   635
                        return SDL_ICONV_E2BIG;
slouken@1895
   636
                    }
slouken@1895
   637
                    p[0] = 0xF0 | (Uint8) ((ch >> 18) & 0x07);
slouken@1895
   638
                    p[1] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
slouken@1895
   639
                    p[2] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
slouken@1895
   640
                    p[3] = 0x80 | (Uint8) (ch & 0x3F);
slouken@1895
   641
                    dst += 4;
slouken@1895
   642
                    dstlen -= 4;
slouken@1895
   643
                } else if (ch <= 0x3FFFFFF) {
slouken@1895
   644
                    if (dstlen < 5) {
slouken@1895
   645
                        return SDL_ICONV_E2BIG;
slouken@1895
   646
                    }
slouken@1895
   647
                    p[0] = 0xF8 | (Uint8) ((ch >> 24) & 0x03);
slouken@1895
   648
                    p[1] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
slouken@1895
   649
                    p[2] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
slouken@1895
   650
                    p[3] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
slouken@1895
   651
                    p[4] = 0x80 | (Uint8) (ch & 0x3F);
slouken@1895
   652
                    dst += 5;
slouken@1895
   653
                    dstlen -= 5;
slouken@1895
   654
                } else {
slouken@1895
   655
                    if (dstlen < 6) {
slouken@1895
   656
                        return SDL_ICONV_E2BIG;
slouken@1895
   657
                    }
slouken@1895
   658
                    p[0] = 0xFC | (Uint8) ((ch >> 30) & 0x01);
slouken@1895
   659
                    p[1] = 0x80 | (Uint8) ((ch >> 24) & 0x3F);
slouken@1895
   660
                    p[2] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
slouken@1895
   661
                    p[3] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
slouken@1895
   662
                    p[4] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
slouken@1895
   663
                    p[5] = 0x80 | (Uint8) (ch & 0x3F);
slouken@1895
   664
                    dst += 6;
slouken@1895
   665
                    dstlen -= 6;
slouken@1895
   666
                }
slouken@1895
   667
            }
slouken@1895
   668
            break;
slouken@1895
   669
        case ENCODING_UTF16BE: /* RFC 2781 */
slouken@1895
   670
            {
slouken@1895
   671
                Uint8 *p = (Uint8 *) dst;
slouken@1895
   672
                if (ch > 0x10FFFF) {
slouken@1895
   673
                    ch = UNKNOWN_UNICODE;
slouken@1895
   674
                }
slouken@1895
   675
                if (ch < 0x10000) {
slouken@1895
   676
                    if (dstlen < 2) {
slouken@1895
   677
                        return SDL_ICONV_E2BIG;
slouken@1895
   678
                    }
slouken@1895
   679
                    p[0] = (Uint8) (ch >> 8);
slouken@1895
   680
                    p[1] = (Uint8) ch;
slouken@1895
   681
                    dst += 2;
slouken@1895
   682
                    dstlen -= 2;
slouken@1895
   683
                } else {
slouken@1895
   684
                    Uint16 W1, W2;
slouken@1895
   685
                    if (dstlen < 4) {
slouken@1895
   686
                        return SDL_ICONV_E2BIG;
slouken@1895
   687
                    }
slouken@1895
   688
                    ch = ch - 0x10000;
slouken@1895
   689
                    W1 = 0xD800 | (Uint16) ((ch >> 10) & 0x3FF);
slouken@1895
   690
                    W2 = 0xDC00 | (Uint16) (ch & 0x3FF);
slouken@1895
   691
                    p[0] = (Uint8) (W1 >> 8);
slouken@1895
   692
                    p[1] = (Uint8) W1;
slouken@1895
   693
                    p[2] = (Uint8) (W2 >> 8);
slouken@1895
   694
                    p[3] = (Uint8) W2;
slouken@1895
   695
                    dst += 4;
slouken@1895
   696
                    dstlen -= 4;
slouken@1895
   697
                }
slouken@1895
   698
            }
slouken@1895
   699
            break;
slouken@1895
   700
        case ENCODING_UTF16LE: /* RFC 2781 */
slouken@1895
   701
            {
slouken@1895
   702
                Uint8 *p = (Uint8 *) dst;
slouken@1895
   703
                if (ch > 0x10FFFF) {
slouken@1895
   704
                    ch = UNKNOWN_UNICODE;
slouken@1895
   705
                }
slouken@1895
   706
                if (ch < 0x10000) {
slouken@1895
   707
                    if (dstlen < 2) {
slouken@1895
   708
                        return SDL_ICONV_E2BIG;
slouken@1895
   709
                    }
slouken@1895
   710
                    p[1] = (Uint8) (ch >> 8);
slouken@1895
   711
                    p[0] = (Uint8) ch;
slouken@1895
   712
                    dst += 2;
slouken@1895
   713
                    dstlen -= 2;
slouken@1895
   714
                } else {
slouken@1895
   715
                    Uint16 W1, W2;
slouken@1895
   716
                    if (dstlen < 4) {
slouken@1895
   717
                        return SDL_ICONV_E2BIG;
slouken@1895
   718
                    }
slouken@1895
   719
                    ch = ch - 0x10000;
slouken@1895
   720
                    W1 = 0xD800 | (Uint16) ((ch >> 10) & 0x3FF);
slouken@1895
   721
                    W2 = 0xDC00 | (Uint16) (ch & 0x3FF);
slouken@1895
   722
                    p[1] = (Uint8) (W1 >> 8);
slouken@1895
   723
                    p[0] = (Uint8) W1;
slouken@1895
   724
                    p[3] = (Uint8) (W2 >> 8);
slouken@1895
   725
                    p[2] = (Uint8) W2;
slouken@1895
   726
                    dst += 4;
slouken@1895
   727
                    dstlen -= 4;
slouken@1895
   728
                }
slouken@1895
   729
            }
slouken@1895
   730
            break;
slouken@1895
   731
        case ENCODING_UTF32BE:
slouken@1895
   732
            {
slouken@1895
   733
                Uint8 *p = (Uint8 *) dst;
slouken@1895
   734
                if (ch > 0x10FFFF) {
slouken@1895
   735
                    ch = UNKNOWN_UNICODE;
slouken@1895
   736
                }
slouken@1895
   737
                if (dstlen < 4) {
slouken@1895
   738
                    return SDL_ICONV_E2BIG;
slouken@1895
   739
                }
slouken@1895
   740
                p[0] = (Uint8) (ch >> 24);
slouken@1895
   741
                p[1] = (Uint8) (ch >> 16);
slouken@1895
   742
                p[2] = (Uint8) (ch >> 8);
slouken@1895
   743
                p[3] = (Uint8) ch;
slouken@1895
   744
                dst += 4;
slouken@1895
   745
                dstlen -= 4;
slouken@1895
   746
            }
slouken@1895
   747
            break;
slouken@1895
   748
        case ENCODING_UTF32LE:
slouken@1895
   749
            {
slouken@1895
   750
                Uint8 *p = (Uint8 *) dst;
slouken@1895
   751
                if (ch > 0x10FFFF) {
slouken@1895
   752
                    ch = UNKNOWN_UNICODE;
slouken@1895
   753
                }
slouken@1895
   754
                if (dstlen < 4) {
slouken@1895
   755
                    return SDL_ICONV_E2BIG;
slouken@1895
   756
                }
slouken@1895
   757
                p[3] = (Uint8) (ch >> 24);
slouken@1895
   758
                p[2] = (Uint8) (ch >> 16);
slouken@1895
   759
                p[1] = (Uint8) (ch >> 8);
slouken@1895
   760
                p[0] = (Uint8) ch;
slouken@1895
   761
                dst += 4;
slouken@1895
   762
                dstlen -= 4;
slouken@1895
   763
            }
slouken@1895
   764
            break;
slouken@1895
   765
        case ENCODING_UCS2:
slouken@1895
   766
            {
slouken@1895
   767
                Uint16 *p = (Uint16 *) dst;
slouken@1895
   768
                if (ch > 0xFFFF) {
slouken@1895
   769
                    ch = UNKNOWN_UNICODE;
slouken@1895
   770
                }
slouken@1895
   771
                if (dstlen < 2) {
slouken@1895
   772
                    return SDL_ICONV_E2BIG;
slouken@1895
   773
                }
slouken@1895
   774
                *p = (Uint16) ch;
slouken@1895
   775
                dst += 2;
slouken@1895
   776
                dstlen -= 2;
slouken@1895
   777
            }
slouken@1895
   778
            break;
slouken@1895
   779
        case ENCODING_UCS4:
slouken@1895
   780
            {
slouken@1895
   781
                Uint32 *p = (Uint32 *) dst;
slouken@1895
   782
                if (ch > 0x7FFFFFFF) {
slouken@1895
   783
                    ch = UNKNOWN_UNICODE;
slouken@1895
   784
                }
slouken@1895
   785
                if (dstlen < 4) {
slouken@1895
   786
                    return SDL_ICONV_E2BIG;
slouken@1895
   787
                }
slouken@1895
   788
                *p = ch;
slouken@1895
   789
                dst += 4;
slouken@1895
   790
                dstlen -= 4;
slouken@1895
   791
            }
slouken@1895
   792
            break;
slouken@1895
   793
        }
slouken@1501
   794
slouken@1895
   795
        /* Update state */
slouken@1895
   796
        *inbuf = src;
slouken@1895
   797
        *inbytesleft = srclen;
slouken@1895
   798
        *outbuf = dst;
slouken@1895
   799
        *outbytesleft = dstlen;
slouken@1895
   800
        ++total;
slouken@1895
   801
    }
slouken@1895
   802
    return total;
slouken@1501
   803
}
slouken@1501
   804
slouken@1895
   805
int
slouken@1895
   806
SDL_iconv_close(SDL_iconv_t cd)
slouken@1501
   807
{
slouken@1895
   808
    if (cd && cd != (SDL_iconv_t) - 1) {
slouken@1895
   809
        SDL_free(cd);
slouken@1895
   810
    }
slouken@1895
   811
    return 0;
slouken@1501
   812
}
slouken@1501
   813
slouken@1501
   814
#endif /* !HAVE_ICONV */
slouken@1501
   815
slouken@1895
   816
char *
slouken@2135
   817
SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf,
slouken@1895
   818
                 size_t inbytesleft)
slouken@1501
   819
{
slouken@1895
   820
    SDL_iconv_t cd;
slouken@1895
   821
    char *string;
slouken@1895
   822
    size_t stringsize;
slouken@1895
   823
    char *outbuf;
slouken@1895
   824
    size_t outbytesleft;
slouken@1895
   825
    size_t retCode = 0;
slouken@1501
   826
slouken@2182
   827
    cd = SDL_iconv_open(tocode, fromcode);
slouken@2182
   828
    if (cd == (SDL_iconv_t) - 1) {
slouken@2182
   829
        /* See if we can recover here (fixes iconv on Solaris 11) */
slouken@2182
   830
        if (!tocode || !*tocode) {
slouken@2182
   831
            tocode = "UTF-8";
slouken@2182
   832
        }
slouken@2182
   833
        if (!fromcode || !*fromcode) {
slouken@3320
   834
            fromcode = "UTF-8";
slouken@2182
   835
        }
slouken@2182
   836
        cd = SDL_iconv_open(tocode, fromcode);
slouken@2143
   837
    }
slouken@1895
   838
    if (cd == (SDL_iconv_t) - 1) {
slouken@1895
   839
        return NULL;
slouken@1895
   840
    }
slouken@1501
   841
slouken@1895
   842
    stringsize = inbytesleft > 4 ? inbytesleft : 4;
slouken@1895
   843
    string = SDL_malloc(stringsize);
slouken@1895
   844
    if (!string) {
slouken@1895
   845
        SDL_iconv_close(cd);
slouken@1895
   846
        return NULL;
slouken@1895
   847
    }
slouken@1895
   848
    outbuf = string;
slouken@1895
   849
    outbytesleft = stringsize;
slouken@1895
   850
    SDL_memset(outbuf, 0, 4);
slouken@1501
   851
slouken@1895
   852
    while (inbytesleft > 0) {
slouken@1895
   853
        retCode = SDL_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
slouken@1895
   854
        switch (retCode) {
slouken@1895
   855
        case SDL_ICONV_E2BIG:
slouken@1895
   856
            {
slouken@1895
   857
                char *oldstring = string;
slouken@1895
   858
                stringsize *= 2;
slouken@1895
   859
                string = SDL_realloc(string, stringsize);
slouken@1895
   860
                if (!string) {
slouken@1895
   861
                    SDL_iconv_close(cd);
slouken@1895
   862
                    return NULL;
slouken@1895
   863
                }
slouken@1895
   864
                outbuf = string + (outbuf - oldstring);
slouken@1895
   865
                outbytesleft = stringsize - (outbuf - string);
slouken@1895
   866
                SDL_memset(outbuf, 0, 4);
slouken@1895
   867
            }
slouken@1895
   868
            break;
slouken@1895
   869
        case SDL_ICONV_EILSEQ:
slouken@1895
   870
            /* Try skipping some input data - not perfect, but... */
slouken@1895
   871
            ++inbuf;
slouken@1895
   872
            --inbytesleft;
slouken@1895
   873
            break;
slouken@1895
   874
        case SDL_ICONV_EINVAL:
slouken@1895
   875
        case SDL_ICONV_ERROR:
slouken@1895
   876
            /* We can't continue... */
slouken@1895
   877
            inbytesleft = 0;
slouken@1895
   878
            break;
slouken@1895
   879
        }
slouken@1895
   880
    }
slouken@1895
   881
    SDL_iconv_close(cd);
slouken@1501
   882
slouken@1895
   883
    return string;
slouken@1501
   884
}
slouken@1895
   885
slouken@1895
   886
/* vi: set ts=4 sw=4 expandtab: */