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