src/stdlib/SDL_iconv.c
changeset 6610 4032f8efdfe7
parent 6138 4c64952a58fb
child 6885 700f1b25f77f
     1.1 --- a/src/stdlib/SDL_iconv.c	Sat Oct 27 02:54:10 2012 -0700
     1.2 +++ b/src/stdlib/SDL_iconv.c	Sun Oct 28 13:03:45 2012 -0700
     1.3 @@ -87,15 +87,21 @@
     1.4      ENCODING_UTF32,             /* Needs byte order marker */
     1.5      ENCODING_UTF32BE,
     1.6      ENCODING_UTF32LE,
     1.7 -    ENCODING_UCS2,              /* Native byte order assumed */
     1.8 -    ENCODING_UCS4,              /* Native byte order assumed */
     1.9 +    ENCODING_UCS2BE,
    1.10 +    ENCODING_UCS2LE,
    1.11 +    ENCODING_UCS4BE,
    1.12 +    ENCODING_UCS4LE,
    1.13  };
    1.14  #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    1.15  #define ENCODING_UTF16NATIVE	ENCODING_UTF16BE
    1.16  #define ENCODING_UTF32NATIVE	ENCODING_UTF32BE
    1.17 +#define ENCODING_UCS2NATIVE     ENCODING_UCS2BE
    1.18 +#define ENCODING_UCS4NATIVE     ENCODING_UCS4BE
    1.19  #else
    1.20  #define ENCODING_UTF16NATIVE	ENCODING_UTF16LE
    1.21  #define ENCODING_UTF32NATIVE	ENCODING_UTF32LE
    1.22 +#define ENCODING_UCS2NATIVE     ENCODING_UCS2LE
    1.23 +#define ENCODING_UCS4NATIVE     ENCODING_UCS4LE
    1.24  #endif
    1.25  
    1.26  struct _SDL_iconv_t
    1.27 @@ -128,10 +134,16 @@
    1.28      { "UTF-32BE", ENCODING_UTF32BE },
    1.29      { "UTF32LE", ENCODING_UTF32LE },
    1.30      { "UTF-32LE", ENCODING_UTF32LE },
    1.31 -    { "UCS2", ENCODING_UCS2 },
    1.32 -    { "UCS-2", ENCODING_UCS2 },
    1.33 -    { "UCS4", ENCODING_UCS4 },
    1.34 -    { "UCS-4", ENCODING_UCS4 },
    1.35 +    { "UCS2", ENCODING_UCS2BE },
    1.36 +    { "UCS-2", ENCODING_UCS2BE },
    1.37 +    { "UCS-2LE", ENCODING_UCS2LE },
    1.38 +    { "UCS-2BE", ENCODING_UCS2BE },
    1.39 +    { "UCS-2-INTERNAL", ENCODING_UCS2NATIVE },
    1.40 +    { "UCS4", ENCODING_UCS4BE },
    1.41 +    { "UCS-4", ENCODING_UCS4BE },
    1.42 +    { "UCS-4LE", ENCODING_UCS4LE },
    1.43 +    { "UCS-4BE", ENCODING_UCS4BE },
    1.44 +    { "UCS-4-INTERNAL", ENCODING_UCS4NATIVE },
    1.45  /* *INDENT-ON* */
    1.46  };
    1.47  
    1.48 @@ -518,6 +530,29 @@
    1.49                        (Uint32) (W2 & 0x3FF)) + 0x10000;
    1.50              }
    1.51              break;
    1.52 +        case ENCODING_UCS2LE:
    1.53 +            {
    1.54 +                Uint8 *p = (Uint8 *) src;
    1.55 +                if (srclen < 2) {
    1.56 +                    return SDL_ICONV_EINVAL;
    1.57 +                }
    1.58 +                ch = ((Uint32) p[1] << 8) | (Uint32) p[0];
    1.59 +                src += 2;
    1.60 +                srclen -= 2;
    1.61 +            }
    1.62 +            break;
    1.63 +        case ENCODING_UCS2BE:
    1.64 +            {
    1.65 +                Uint8 *p = (Uint8 *) src;
    1.66 +                if (srclen < 2) {
    1.67 +                    return SDL_ICONV_EINVAL;
    1.68 +                }
    1.69 +                ch = ((Uint32) p[0] << 8) | (Uint32) p[1];
    1.70 +                src += 2;
    1.71 +                srclen -= 2;
    1.72 +            }
    1.73 +            break;
    1.74 +        case ENCODING_UCS4BE:
    1.75          case ENCODING_UTF32BE:
    1.76              {
    1.77                  Uint8 *p = (Uint8 *) src;
    1.78 @@ -531,6 +566,7 @@
    1.79                  srclen -= 4;
    1.80              }
    1.81              break;
    1.82 +        case ENCODING_UCS4LE:
    1.83          case ENCODING_UTF32LE:
    1.84              {
    1.85                  Uint8 *p = (Uint8 *) src;
    1.86 @@ -544,28 +580,6 @@
    1.87                  srclen -= 4;
    1.88              }
    1.89              break;
    1.90 -        case ENCODING_UCS2:
    1.91 -            {
    1.92 -                Uint16 *p = (Uint16 *) src;
    1.93 -                if (srclen < 2) {
    1.94 -                    return SDL_ICONV_EINVAL;
    1.95 -                }
    1.96 -                ch = *p;
    1.97 -                src += 2;
    1.98 -                srclen -= 2;
    1.99 -            }
   1.100 -            break;
   1.101 -        case ENCODING_UCS4:
   1.102 -            {
   1.103 -                Uint32 *p = (Uint32 *) src;
   1.104 -                if (srclen < 4) {
   1.105 -                    return SDL_ICONV_EINVAL;
   1.106 -                }
   1.107 -                ch = *p;
   1.108 -                src += 4;
   1.109 -                srclen -= 4;
   1.110 -            }
   1.111 -            break;
   1.112          }
   1.113  
   1.114          /* Encode a character */
   1.115 @@ -728,12 +742,46 @@
   1.116                  }
   1.117              }
   1.118              break;
   1.119 -        case ENCODING_UTF32BE:
   1.120 +        case ENCODING_UCS2BE:
   1.121 +            {
   1.122 +                Uint8 *p = (Uint8 *) dst;
   1.123 +                if (ch > 0xFFFF) {
   1.124 +                    ch = UNKNOWN_UNICODE;
   1.125 +                }
   1.126 +                if (dstlen < 2) {
   1.127 +                    return SDL_ICONV_E2BIG;
   1.128 +                }
   1.129 +                p[0] = (Uint8) (ch >> 8);
   1.130 +                p[1] = (Uint8) ch;
   1.131 +                dst += 2;
   1.132 +                dstlen -= 2;
   1.133 +            }
   1.134 +            break;
   1.135 +        case ENCODING_UCS2LE:
   1.136              {
   1.137                  Uint8 *p = (Uint8 *) dst;
   1.138 -                if (ch > 0x10FFFF) {
   1.139 +                if (ch > 0xFFFF) {
   1.140                      ch = UNKNOWN_UNICODE;
   1.141                  }
   1.142 +                if (dstlen < 2) {
   1.143 +                    return SDL_ICONV_E2BIG;
   1.144 +                }
   1.145 +                p[1] = (Uint8) (ch >> 8);
   1.146 +                p[0] = (Uint8) ch;
   1.147 +                dst += 2;
   1.148 +                dstlen -= 2;
   1.149 +            }
   1.150 +            break;
   1.151 +        case ENCODING_UTF32BE:
   1.152 +            if (ch > 0x10FFFF) {
   1.153 +                ch = UNKNOWN_UNICODE;
   1.154 +            }
   1.155 +        case ENCODING_UCS4BE:
   1.156 +            if (ch > 0x7FFFFFFF) {
   1.157 +                ch = UNKNOWN_UNICODE;
   1.158 +            }
   1.159 +            {
   1.160 +                Uint8 *p = (Uint8 *) dst;
   1.161                  if (dstlen < 4) {
   1.162                      return SDL_ICONV_E2BIG;
   1.163                  }
   1.164 @@ -746,11 +794,15 @@
   1.165              }
   1.166              break;
   1.167          case ENCODING_UTF32LE:
   1.168 +            if (ch > 0x10FFFF) {
   1.169 +                ch = UNKNOWN_UNICODE;
   1.170 +            }
   1.171 +        case ENCODING_UCS4LE:
   1.172 +            if (ch > 0x7FFFFFFF) {
   1.173 +                ch = UNKNOWN_UNICODE;
   1.174 +            }
   1.175              {
   1.176                  Uint8 *p = (Uint8 *) dst;
   1.177 -                if (ch > 0x10FFFF) {
   1.178 -                    ch = UNKNOWN_UNICODE;
   1.179 -                }
   1.180                  if (dstlen < 4) {
   1.181                      return SDL_ICONV_E2BIG;
   1.182                  }
   1.183 @@ -762,34 +814,6 @@
   1.184                  dstlen -= 4;
   1.185              }
   1.186              break;
   1.187 -        case ENCODING_UCS2:
   1.188 -            {
   1.189 -                Uint16 *p = (Uint16 *) dst;
   1.190 -                if (ch > 0xFFFF) {
   1.191 -                    ch = UNKNOWN_UNICODE;
   1.192 -                }
   1.193 -                if (dstlen < 2) {
   1.194 -                    return SDL_ICONV_E2BIG;
   1.195 -                }
   1.196 -                *p = (Uint16) ch;
   1.197 -                dst += 2;
   1.198 -                dstlen -= 2;
   1.199 -            }
   1.200 -            break;
   1.201 -        case ENCODING_UCS4:
   1.202 -            {
   1.203 -                Uint32 *p = (Uint32 *) dst;
   1.204 -                if (ch > 0x7FFFFFFF) {
   1.205 -                    ch = UNKNOWN_UNICODE;
   1.206 -                }
   1.207 -                if (dstlen < 4) {
   1.208 -                    return SDL_ICONV_E2BIG;
   1.209 -                }
   1.210 -                *p = ch;
   1.211 -                dst += 4;
   1.212 -                dstlen -= 4;
   1.213 -            }
   1.214 -            break;
   1.215          }
   1.216  
   1.217          /* Update state */