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