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