src/stdlib/SDL_iconv.c
changeset 6610 4032f8efdfe7
parent 6138 4c64952a58fb
child 6885 700f1b25f77f
equal deleted inserted replaced
6609:747e2ac35db6 6610:4032f8efdfe7
    85     ENCODING_UTF16BE,
    85     ENCODING_UTF16BE,
    86     ENCODING_UTF16LE,
    86     ENCODING_UTF16LE,
    87     ENCODING_UTF32,             /* Needs byte order marker */
    87     ENCODING_UTF32,             /* Needs byte order marker */
    88     ENCODING_UTF32BE,
    88     ENCODING_UTF32BE,
    89     ENCODING_UTF32LE,
    89     ENCODING_UTF32LE,
    90     ENCODING_UCS2,              /* Native byte order assumed */
    90     ENCODING_UCS2BE,
    91     ENCODING_UCS4,              /* Native byte order assumed */
    91     ENCODING_UCS2LE,
       
    92     ENCODING_UCS4BE,
       
    93     ENCODING_UCS4LE,
    92 };
    94 };
    93 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    95 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    94 #define ENCODING_UTF16NATIVE	ENCODING_UTF16BE
    96 #define ENCODING_UTF16NATIVE	ENCODING_UTF16BE
    95 #define ENCODING_UTF32NATIVE	ENCODING_UTF32BE
    97 #define ENCODING_UTF32NATIVE	ENCODING_UTF32BE
       
    98 #define ENCODING_UCS2NATIVE     ENCODING_UCS2BE
       
    99 #define ENCODING_UCS4NATIVE     ENCODING_UCS4BE
    96 #else
   100 #else
    97 #define ENCODING_UTF16NATIVE	ENCODING_UTF16LE
   101 #define ENCODING_UTF16NATIVE	ENCODING_UTF16LE
    98 #define ENCODING_UTF32NATIVE	ENCODING_UTF32LE
   102 #define ENCODING_UTF32NATIVE	ENCODING_UTF32LE
       
   103 #define ENCODING_UCS2NATIVE     ENCODING_UCS2LE
       
   104 #define ENCODING_UCS4NATIVE     ENCODING_UCS4LE
    99 #endif
   105 #endif
   100 
   106 
   101 struct _SDL_iconv_t
   107 struct _SDL_iconv_t
   102 {
   108 {
   103     int src_fmt;
   109     int src_fmt;
   126     { "UTF-32", ENCODING_UTF32 },
   132     { "UTF-32", ENCODING_UTF32 },
   127     { "UTF32BE", ENCODING_UTF32BE },
   133     { "UTF32BE", ENCODING_UTF32BE },
   128     { "UTF-32BE", ENCODING_UTF32BE },
   134     { "UTF-32BE", ENCODING_UTF32BE },
   129     { "UTF32LE", ENCODING_UTF32LE },
   135     { "UTF32LE", ENCODING_UTF32LE },
   130     { "UTF-32LE", ENCODING_UTF32LE },
   136     { "UTF-32LE", ENCODING_UTF32LE },
   131     { "UCS2", ENCODING_UCS2 },
   137     { "UCS2", ENCODING_UCS2BE },
   132     { "UCS-2", ENCODING_UCS2 },
   138     { "UCS-2", ENCODING_UCS2BE },
   133     { "UCS4", ENCODING_UCS4 },
   139     { "UCS-2LE", ENCODING_UCS2LE },
   134     { "UCS-4", ENCODING_UCS4 },
   140     { "UCS-2BE", ENCODING_UCS2BE },
       
   141     { "UCS-2-INTERNAL", ENCODING_UCS2NATIVE },
       
   142     { "UCS4", ENCODING_UCS4BE },
       
   143     { "UCS-4", ENCODING_UCS4BE },
       
   144     { "UCS-4LE", ENCODING_UCS4LE },
       
   145     { "UCS-4BE", ENCODING_UCS4BE },
       
   146     { "UCS-4-INTERNAL", ENCODING_UCS4NATIVE },
   135 /* *INDENT-ON* */
   147 /* *INDENT-ON* */
   136 };
   148 };
   137 
   149 
   138 static const char *
   150 static const char *
   139 getlocale(char *buffer, size_t bufsize)
   151 getlocale(char *buffer, size_t bufsize)
   516                 }
   528                 }
   517                 ch = (((Uint32) (W1 & 0x3FF) << 10) |
   529                 ch = (((Uint32) (W1 & 0x3FF) << 10) |
   518                       (Uint32) (W2 & 0x3FF)) + 0x10000;
   530                       (Uint32) (W2 & 0x3FF)) + 0x10000;
   519             }
   531             }
   520             break;
   532             break;
       
   533         case ENCODING_UCS2LE:
       
   534             {
       
   535                 Uint8 *p = (Uint8 *) src;
       
   536                 if (srclen < 2) {
       
   537                     return SDL_ICONV_EINVAL;
       
   538                 }
       
   539                 ch = ((Uint32) p[1] << 8) | (Uint32) p[0];
       
   540                 src += 2;
       
   541                 srclen -= 2;
       
   542             }
       
   543             break;
       
   544         case ENCODING_UCS2BE:
       
   545             {
       
   546                 Uint8 *p = (Uint8 *) src;
       
   547                 if (srclen < 2) {
       
   548                     return SDL_ICONV_EINVAL;
       
   549                 }
       
   550                 ch = ((Uint32) p[0] << 8) | (Uint32) p[1];
       
   551                 src += 2;
       
   552                 srclen -= 2;
       
   553             }
       
   554             break;
       
   555         case ENCODING_UCS4BE:
   521         case ENCODING_UTF32BE:
   556         case ENCODING_UTF32BE:
   522             {
   557             {
   523                 Uint8 *p = (Uint8 *) src;
   558                 Uint8 *p = (Uint8 *) src;
   524                 if (srclen < 4) {
   559                 if (srclen < 4) {
   525                     return SDL_ICONV_EINVAL;
   560                     return SDL_ICONV_EINVAL;
   529                     ((Uint32) p[2] << 8) | (Uint32) p[3];
   564                     ((Uint32) p[2] << 8) | (Uint32) p[3];
   530                 src += 4;
   565                 src += 4;
   531                 srclen -= 4;
   566                 srclen -= 4;
   532             }
   567             }
   533             break;
   568             break;
       
   569         case ENCODING_UCS4LE:
   534         case ENCODING_UTF32LE:
   570         case ENCODING_UTF32LE:
   535             {
   571             {
   536                 Uint8 *p = (Uint8 *) src;
   572                 Uint8 *p = (Uint8 *) src;
   537                 if (srclen < 4) {
   573                 if (srclen < 4) {
   538                     return SDL_ICONV_EINVAL;
   574                     return SDL_ICONV_EINVAL;
   539                 }
   575                 }
   540                 ch = ((Uint32) p[3] << 24) |
   576                 ch = ((Uint32) p[3] << 24) |
   541                     ((Uint32) p[2] << 16) |
   577                     ((Uint32) p[2] << 16) |
   542                     ((Uint32) p[1] << 8) | (Uint32) p[0];
   578                     ((Uint32) p[1] << 8) | (Uint32) p[0];
   543                 src += 4;
       
   544                 srclen -= 4;
       
   545             }
       
   546             break;
       
   547         case ENCODING_UCS2:
       
   548             {
       
   549                 Uint16 *p = (Uint16 *) src;
       
   550                 if (srclen < 2) {
       
   551                     return SDL_ICONV_EINVAL;
       
   552                 }
       
   553                 ch = *p;
       
   554                 src += 2;
       
   555                 srclen -= 2;
       
   556             }
       
   557             break;
       
   558         case ENCODING_UCS4:
       
   559             {
       
   560                 Uint32 *p = (Uint32 *) src;
       
   561                 if (srclen < 4) {
       
   562                     return SDL_ICONV_EINVAL;
       
   563                 }
       
   564                 ch = *p;
       
   565                 src += 4;
   579                 src += 4;
   566                 srclen -= 4;
   580                 srclen -= 4;
   567             }
   581             }
   568             break;
   582             break;
   569         }
   583         }
   726                     dst += 4;
   740                     dst += 4;
   727                     dstlen -= 4;
   741                     dstlen -= 4;
   728                 }
   742                 }
   729             }
   743             }
   730             break;
   744             break;
       
   745         case ENCODING_UCS2BE:
       
   746             {
       
   747                 Uint8 *p = (Uint8 *) dst;
       
   748                 if (ch > 0xFFFF) {
       
   749                     ch = UNKNOWN_UNICODE;
       
   750                 }
       
   751                 if (dstlen < 2) {
       
   752                     return SDL_ICONV_E2BIG;
       
   753                 }
       
   754                 p[0] = (Uint8) (ch >> 8);
       
   755                 p[1] = (Uint8) ch;
       
   756                 dst += 2;
       
   757                 dstlen -= 2;
       
   758             }
       
   759             break;
       
   760         case ENCODING_UCS2LE:
       
   761             {
       
   762                 Uint8 *p = (Uint8 *) dst;
       
   763                 if (ch > 0xFFFF) {
       
   764                     ch = UNKNOWN_UNICODE;
       
   765                 }
       
   766                 if (dstlen < 2) {
       
   767                     return SDL_ICONV_E2BIG;
       
   768                 }
       
   769                 p[1] = (Uint8) (ch >> 8);
       
   770                 p[0] = (Uint8) ch;
       
   771                 dst += 2;
       
   772                 dstlen -= 2;
       
   773             }
       
   774             break;
   731         case ENCODING_UTF32BE:
   775         case ENCODING_UTF32BE:
       
   776             if (ch > 0x10FFFF) {
       
   777                 ch = UNKNOWN_UNICODE;
       
   778             }
       
   779         case ENCODING_UCS4BE:
       
   780             if (ch > 0x7FFFFFFF) {
       
   781                 ch = UNKNOWN_UNICODE;
       
   782             }
   732             {
   783             {
   733                 Uint8 *p = (Uint8 *) dst;
   784                 Uint8 *p = (Uint8 *) dst;
   734                 if (ch > 0x10FFFF) {
       
   735                     ch = UNKNOWN_UNICODE;
       
   736                 }
       
   737                 if (dstlen < 4) {
   785                 if (dstlen < 4) {
   738                     return SDL_ICONV_E2BIG;
   786                     return SDL_ICONV_E2BIG;
   739                 }
   787                 }
   740                 p[0] = (Uint8) (ch >> 24);
   788                 p[0] = (Uint8) (ch >> 24);
   741                 p[1] = (Uint8) (ch >> 16);
   789                 p[1] = (Uint8) (ch >> 16);
   744                 dst += 4;
   792                 dst += 4;
   745                 dstlen -= 4;
   793                 dstlen -= 4;
   746             }
   794             }
   747             break;
   795             break;
   748         case ENCODING_UTF32LE:
   796         case ENCODING_UTF32LE:
       
   797             if (ch > 0x10FFFF) {
       
   798                 ch = UNKNOWN_UNICODE;
       
   799             }
       
   800         case ENCODING_UCS4LE:
       
   801             if (ch > 0x7FFFFFFF) {
       
   802                 ch = UNKNOWN_UNICODE;
       
   803             }
   749             {
   804             {
   750                 Uint8 *p = (Uint8 *) dst;
   805                 Uint8 *p = (Uint8 *) dst;
   751                 if (ch > 0x10FFFF) {
       
   752                     ch = UNKNOWN_UNICODE;
       
   753                 }
       
   754                 if (dstlen < 4) {
   806                 if (dstlen < 4) {
   755                     return SDL_ICONV_E2BIG;
   807                     return SDL_ICONV_E2BIG;
   756                 }
   808                 }
   757                 p[3] = (Uint8) (ch >> 24);
   809                 p[3] = (Uint8) (ch >> 24);
   758                 p[2] = (Uint8) (ch >> 16);
   810                 p[2] = (Uint8) (ch >> 16);
   759                 p[1] = (Uint8) (ch >> 8);
   811                 p[1] = (Uint8) (ch >> 8);
   760                 p[0] = (Uint8) ch;
   812                 p[0] = (Uint8) ch;
   761                 dst += 4;
       
   762                 dstlen -= 4;
       
   763             }
       
   764             break;
       
   765         case ENCODING_UCS2:
       
   766             {
       
   767                 Uint16 *p = (Uint16 *) dst;
       
   768                 if (ch > 0xFFFF) {
       
   769                     ch = UNKNOWN_UNICODE;
       
   770                 }
       
   771                 if (dstlen < 2) {
       
   772                     return SDL_ICONV_E2BIG;
       
   773                 }
       
   774                 *p = (Uint16) ch;
       
   775                 dst += 2;
       
   776                 dstlen -= 2;
       
   777             }
       
   778             break;
       
   779         case ENCODING_UCS4:
       
   780             {
       
   781                 Uint32 *p = (Uint32 *) dst;
       
   782                 if (ch > 0x7FFFFFFF) {
       
   783                     ch = UNKNOWN_UNICODE;
       
   784                 }
       
   785                 if (dstlen < 4) {
       
   786                     return SDL_ICONV_E2BIG;
       
   787                 }
       
   788                 *p = ch;
       
   789                 dst += 4;
   813                 dst += 4;
   790                 dstlen -= 4;
   814                 dstlen -= 4;
   791             }
   815             }
   792             break;
   816             break;
   793         }
   817         }