2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* This file contains portable iconv functions for SDL */
26 #include "SDL_stdinc.h"
27 #include "SDL_endian.h"
31 /* Depending on which standard the iconv() was implemented with,
32 iconv() may or may not use const char ** for the inbuf param.
33 If we get this wrong, it's just a warning, so no big deal.
35 #if defined(_XGP6) || \
36 defined(__GLIBC__) && ((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
37 #define ICONV_INBUF_NONCONST
42 size_t SDL_iconv(SDL_iconv_t cd,
43 const char **inbuf, size_t *inbytesleft,
44 char **outbuf, size_t *outbytesleft)
47 #ifdef ICONV_INBUF_NONCONST
48 retCode = iconv(cd, (char **)inbuf, inbytesleft, outbuf, outbytesleft);
50 retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
52 if ( retCode == (size_t)-1 ) {
55 return SDL_ICONV_E2BIG;
57 return SDL_ICONV_EILSEQ;
59 return SDL_ICONV_EINVAL;
61 return SDL_ICONV_ERROR;
69 /* Lots of useful information on Unicode at:
70 http://www.cl.cam.ac.uk/~mgk25/unicode.html
73 #define UNICODE_BOM 0xFEFF
75 #define UNKNOWN_ASCII '?'
76 #define UNKNOWN_UNICODE 0xFFFD
83 ENCODING_UTF16, /* Needs byte order marker */
86 ENCODING_UTF32, /* Needs byte order marker */
89 ENCODING_UCS2, /* Native byte order assumed */
90 ENCODING_UCS4, /* Native byte order assumed */
92 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
93 #define ENCODING_UTF16NATIVE ENCODING_UTF16BE
94 #define ENCODING_UTF32NATIVE ENCODING_UTF32BE
96 #define ENCODING_UTF16NATIVE ENCODING_UTF16LE
97 #define ENCODING_UTF32NATIVE ENCODING_UTF32LE
110 { "ASCII", ENCODING_ASCII },
111 { "US-ASCII", ENCODING_ASCII },
112 { "8859-1", ENCODING_LATIN1 },
113 { "ISO-8859-1", ENCODING_LATIN1 },
114 { "UTF8", ENCODING_UTF8 },
115 { "UTF-8", ENCODING_UTF8 },
116 { "UTF16", ENCODING_UTF16 },
117 { "UTF-16", ENCODING_UTF16 },
118 { "UTF16BE", ENCODING_UTF16BE },
119 { "UTF-16BE", ENCODING_UTF16BE },
120 { "UTF16LE", ENCODING_UTF16LE },
121 { "UTF-16LE", ENCODING_UTF16LE },
122 { "UTF32", ENCODING_UTF32 },
123 { "UTF-32", ENCODING_UTF32 },
124 { "UTF32BE", ENCODING_UTF32BE },
125 { "UTF-32BE", ENCODING_UTF32BE },
126 { "UTF32LE", ENCODING_UTF32LE },
127 { "UTF-32LE", ENCODING_UTF32LE },
128 { "UCS2", ENCODING_UCS2 },
129 { "UCS-2", ENCODING_UCS2 },
130 { "UCS4", ENCODING_UCS4 },
131 { "UCS-4", ENCODING_UCS4 },
134 static const char *getlocale(char *buffer, size_t bufsize)
139 lang = SDL_getenv("LC_ALL");
141 lang = SDL_getenv("LC_CTYPE");
144 lang = SDL_getenv("LC_MESSAGES");
147 lang = SDL_getenv("LANG");
149 if ( !lang || !*lang || SDL_strcmp(lang, "C") == 0 ) {
153 /* We need to trim down strings like "en_US.UTF-8@blah" to "UTF-8" */
154 ptr = SDL_strchr(lang, '.');
159 SDL_strlcpy(buffer, lang, bufsize);
160 ptr = SDL_strchr(buffer, '@');
162 *ptr = '\0'; /* chop end of string. */
168 SDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode)
170 int src_fmt = ENCODING_UNKNOWN;
171 int dst_fmt = ENCODING_UNKNOWN;
173 char fromcode_buffer[64];
174 char tocode_buffer[64];
176 if ( !fromcode || !*fromcode ) {
177 fromcode = getlocale(fromcode_buffer, sizeof(fromcode_buffer));
179 if ( !tocode || !*tocode ) {
180 tocode = getlocale(tocode_buffer, sizeof(tocode_buffer));
182 for ( i = 0; i < SDL_arraysize(encodings); ++i ) {
183 if ( SDL_strcasecmp(fromcode, encodings[i].name) == 0 ) {
184 src_fmt = encodings[i].format;
185 if ( dst_fmt != ENCODING_UNKNOWN ) {
189 if ( SDL_strcasecmp(tocode, encodings[i].name) == 0 ) {
190 dst_fmt = encodings[i].format;
191 if ( src_fmt != ENCODING_UNKNOWN ) {
196 if ( src_fmt != ENCODING_UNKNOWN && dst_fmt != ENCODING_UNKNOWN ) {
197 SDL_iconv_t cd = (SDL_iconv_t)SDL_malloc(sizeof(*cd));
199 cd->src_fmt = src_fmt;
200 cd->dst_fmt = dst_fmt;
204 return (SDL_iconv_t)-1;
207 size_t SDL_iconv(SDL_iconv_t cd,
208 const char **inbuf, size_t *inbytesleft,
209 char **outbuf, size_t *outbytesleft)
211 /* For simplicity, we'll convert everything to and from UCS-4 */
214 size_t srclen, dstlen;
218 if ( !inbuf || !*inbuf ) {
219 /* Reset the context */
222 if ( !outbuf || !*outbuf || !outbytesleft || !*outbytesleft ) {
223 return SDL_ICONV_E2BIG;
226 srclen = (inbytesleft ? *inbytesleft : 0);
228 dstlen = *outbytesleft;
230 switch ( cd->src_fmt ) {
232 /* Scan for a byte order marker */
234 Uint8 *p = (Uint8 *)src;
235 size_t n = srclen / 2;
237 if ( p[0] == 0xFF && p[1] == 0xFE ) {
238 cd->src_fmt = ENCODING_UTF16BE;
240 } else if ( p[0] == 0xFE && p[1] == 0xFF ) {
241 cd->src_fmt = ENCODING_UTF16LE;
248 /* We can't tell, default to host order */
249 cd->src_fmt = ENCODING_UTF16NATIVE;
254 /* Scan for a byte order marker */
256 Uint8 *p = (Uint8 *)src;
257 size_t n = srclen / 4;
259 if ( p[0] == 0xFF && p[1] == 0xFE &&
260 p[2] == 0x00 && p[3] == 0x00 ) {
261 cd->src_fmt = ENCODING_UTF32BE;
263 } else if ( p[0] == 0x00 && p[1] == 0x00 &&
264 p[2] == 0xFE && p[3] == 0xFF ) {
265 cd->src_fmt = ENCODING_UTF32LE;
272 /* We can't tell, default to host order */
273 cd->src_fmt = ENCODING_UTF32NATIVE;
279 switch ( cd->dst_fmt ) {
281 /* Default to host order, need to add byte order marker */
283 return SDL_ICONV_E2BIG;
285 *(Uint16 *)dst = UNICODE_BOM;
288 cd->dst_fmt = ENCODING_UTF16NATIVE;
291 /* Default to host order, need to add byte order marker */
293 return SDL_ICONV_E2BIG;
295 *(Uint32 *)dst = UNICODE_BOM;
298 cd->dst_fmt = ENCODING_UTF32NATIVE;
303 while ( srclen > 0 ) {
304 /* Decode a character */
305 switch ( cd->src_fmt ) {
308 Uint8 *p = (Uint8 *)src;
309 ch = (Uint32)(p[0] & 0x7F);
314 case ENCODING_LATIN1:
316 Uint8 *p = (Uint8 *)src;
322 case ENCODING_UTF8: /* RFC 3629 */
324 Uint8 *p = (Uint8 *)src;
326 SDL_bool overlong = SDL_FALSE;
327 if ( p[0] >= 0xFC ) {
328 if ( (p[0] & 0xFE) != 0xFC ) {
329 /* Skip illegal sequences
330 return SDL_ICONV_EILSEQ;
332 ch = UNKNOWN_UNICODE;
334 if ( p[0] == 0xFC ) {
337 ch = (Uint32)(p[0] & 0x01);
340 } else if ( p[0] >= 0xF8 ) {
341 if ( (p[0] & 0xFC) != 0xF8 ) {
342 /* Skip illegal sequences
343 return SDL_ICONV_EILSEQ;
345 ch = UNKNOWN_UNICODE;
347 if ( p[0] == 0xF8 ) {
350 ch = (Uint32)(p[0] & 0x03);
353 } else if ( p[0] >= 0xF0 ) {
354 if ( (p[0] & 0xF8) != 0xF0 ) {
355 /* Skip illegal sequences
356 return SDL_ICONV_EILSEQ;
358 ch = UNKNOWN_UNICODE;
360 if ( p[0] == 0xF0 ) {
363 ch = (Uint32)(p[0] & 0x07);
366 } else if ( p[0] >= 0xE0 ) {
367 if ( (p[0] & 0xF0) != 0xE0 ) {
368 /* Skip illegal sequences
369 return SDL_ICONV_EILSEQ;
371 ch = UNKNOWN_UNICODE;
373 if ( p[0] == 0xE0 ) {
376 ch = (Uint32)(p[0] & 0x0F);
379 } else if ( p[0] >= 0xC0 ) {
380 if ( (p[0] & 0xE0) != 0xC0 ) {
381 /* Skip illegal sequences
382 return SDL_ICONV_EILSEQ;
384 ch = UNKNOWN_UNICODE;
386 if ( (p[0] & 0xCE) == 0xC0 ) {
389 ch = (Uint32)(p[0] & 0x1F);
393 if ( (p[0] & 0x80) != 0x00 ) {
394 /* Skip illegal sequences
395 return SDL_ICONV_EILSEQ;
397 ch = UNKNOWN_UNICODE;
404 if ( srclen < left ) {
405 return SDL_ICONV_EINVAL;
409 if ( (p[0] & 0xC0) != 0x80 ) {
410 /* Skip illegal sequences
411 return SDL_ICONV_EILSEQ;
413 ch = UNKNOWN_UNICODE;
422 /* Potential security risk
423 return SDL_ICONV_EILSEQ;
425 ch = UNKNOWN_UNICODE;
427 if ( (ch >= 0xD800 && ch <= 0xDFFF) ||
428 (ch == 0xFFFE || ch == 0xFFFF) ||
430 /* Skip illegal sequences
431 return SDL_ICONV_EILSEQ;
433 ch = UNKNOWN_UNICODE;
437 case ENCODING_UTF16BE: /* RFC 2781 */
439 Uint8 *p = (Uint8 *)src;
442 return SDL_ICONV_EINVAL;
444 W1 = ((Uint16)p[0] << 8) |
448 if ( W1 < 0xD800 || W1 > 0xDFFF ) {
453 /* Skip illegal sequences
454 return SDL_ICONV_EILSEQ;
456 ch = UNKNOWN_UNICODE;
460 return SDL_ICONV_EINVAL;
463 W2 = ((Uint16)p[0] << 8) |
467 if ( W2 < 0xDC00 || W2 > 0xDFFF ) {
468 /* Skip illegal sequences
469 return SDL_ICONV_EILSEQ;
471 ch = UNKNOWN_UNICODE;
474 ch = (((Uint32)(W1 & 0x3FF) << 10) |
475 (Uint32)(W2 & 0x3FF)) + 0x10000;
478 case ENCODING_UTF16LE: /* RFC 2781 */
480 Uint8 *p = (Uint8 *)src;
483 return SDL_ICONV_EINVAL;
485 W1 = ((Uint16)p[1] << 8) |
489 if ( W1 < 0xD800 || W1 > 0xDFFF ) {
494 /* Skip illegal sequences
495 return SDL_ICONV_EILSEQ;
497 ch = UNKNOWN_UNICODE;
501 return SDL_ICONV_EINVAL;
504 W2 = ((Uint16)p[1] << 8) |
508 if ( W2 < 0xDC00 || W2 > 0xDFFF ) {
509 /* Skip illegal sequences
510 return SDL_ICONV_EILSEQ;
512 ch = UNKNOWN_UNICODE;
515 ch = (((Uint32)(W1 & 0x3FF) << 10) |
516 (Uint32)(W2 & 0x3FF)) + 0x10000;
519 case ENCODING_UTF32BE:
521 Uint8 *p = (Uint8 *)src;
523 return SDL_ICONV_EINVAL;
525 ch = ((Uint32)p[0] << 24) |
526 ((Uint32)p[1] << 16) |
527 ((Uint32)p[2] << 8) |
533 case ENCODING_UTF32LE:
535 Uint8 *p = (Uint8 *)src;
537 return SDL_ICONV_EINVAL;
539 ch = ((Uint32)p[3] << 24) |
540 ((Uint32)p[2] << 16) |
541 ((Uint32)p[1] << 8) |
549 Uint16 *p = (Uint16 *)src;
551 return SDL_ICONV_EINVAL;
560 Uint32 *p = (Uint32 *)src;
562 return SDL_ICONV_EINVAL;
571 /* Encode a character */
572 switch ( cd->dst_fmt ) {
575 Uint8 *p = (Uint8 *)dst;
577 return SDL_ICONV_E2BIG;
588 case ENCODING_LATIN1:
590 Uint8 *p = (Uint8 *)dst;
592 return SDL_ICONV_E2BIG;
603 case ENCODING_UTF8: /* RFC 3629 */
605 Uint8 *p = (Uint8 *)dst;
606 if ( ch > 0x10FFFF ) {
607 ch = UNKNOWN_UNICODE;
611 return SDL_ICONV_E2BIG;
616 } else if ( ch <= 0x7FF ) {
618 return SDL_ICONV_E2BIG;
620 p[0] = 0xC0 | (Uint8)((ch >> 6) & 0x1F);
621 p[1] = 0x80 | (Uint8)(ch & 0x3F);
624 } else if ( ch <= 0xFFFF ) {
626 return SDL_ICONV_E2BIG;
628 p[0] = 0xE0 | (Uint8)((ch >> 12) & 0x0F);
629 p[1] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
630 p[2] = 0x80 | (Uint8)(ch & 0x3F);
633 } else if ( ch <= 0x1FFFFF ) {
635 return SDL_ICONV_E2BIG;
637 p[0] = 0xF0 | (Uint8)((ch >> 18) & 0x07);
638 p[1] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
639 p[2] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
640 p[3] = 0x80 | (Uint8)(ch & 0x3F);
643 } else if ( ch <= 0x3FFFFFF ) {
645 return SDL_ICONV_E2BIG;
647 p[0] = 0xF8 | (Uint8)((ch >> 24) & 0x03);
648 p[1] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
649 p[2] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
650 p[3] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
651 p[4] = 0x80 | (Uint8)(ch & 0x3F);
656 return SDL_ICONV_E2BIG;
658 p[0] = 0xFC | (Uint8)((ch >> 30) & 0x01);
659 p[1] = 0x80 | (Uint8)((ch >> 24) & 0x3F);
660 p[2] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
661 p[3] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
662 p[4] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
663 p[5] = 0x80 | (Uint8)(ch & 0x3F);
669 case ENCODING_UTF16BE: /* RFC 2781 */
671 Uint8 *p = (Uint8 *)dst;
672 if ( ch > 0x10FFFF ) {
673 ch = UNKNOWN_UNICODE;
675 if ( ch < 0x10000 ) {
677 return SDL_ICONV_E2BIG;
679 p[0] = (Uint8)(ch >> 8);
686 return SDL_ICONV_E2BIG;
689 W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
690 W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
691 p[0] = (Uint8)(W1 >> 8);
693 p[2] = (Uint8)(W2 >> 8);
700 case ENCODING_UTF16LE: /* RFC 2781 */
702 Uint8 *p = (Uint8 *)dst;
703 if ( ch > 0x10FFFF ) {
704 ch = UNKNOWN_UNICODE;
706 if ( ch < 0x10000 ) {
708 return SDL_ICONV_E2BIG;
710 p[1] = (Uint8)(ch >> 8);
717 return SDL_ICONV_E2BIG;
720 W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
721 W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
722 p[1] = (Uint8)(W1 >> 8);
724 p[3] = (Uint8)(W2 >> 8);
731 case ENCODING_UTF32BE:
733 Uint8 *p = (Uint8 *)dst;
734 if ( ch > 0x10FFFF ) {
735 ch = UNKNOWN_UNICODE;
738 return SDL_ICONV_E2BIG;
740 p[0] = (Uint8)(ch >> 24);
741 p[1] = (Uint8)(ch >> 16);
742 p[2] = (Uint8)(ch >> 8);
748 case ENCODING_UTF32LE:
750 Uint8 *p = (Uint8 *)dst;
751 if ( ch > 0x10FFFF ) {
752 ch = UNKNOWN_UNICODE;
755 return SDL_ICONV_E2BIG;
757 p[3] = (Uint8)(ch >> 24);
758 p[2] = (Uint8)(ch >> 16);
759 p[1] = (Uint8)(ch >> 8);
767 Uint16 *p = (Uint16 *)dst;
769 ch = UNKNOWN_UNICODE;
772 return SDL_ICONV_E2BIG;
781 Uint32 *p = (Uint32 *)dst;
782 if ( ch > 0x7FFFFFFF ) {
783 ch = UNKNOWN_UNICODE;
786 return SDL_ICONV_E2BIG;
797 *inbytesleft = srclen;
799 *outbytesleft = dstlen;
805 int SDL_iconv_close(SDL_iconv_t cd)
807 if ( cd && cd != (SDL_iconv_t)-1 ) {
813 #endif /* !HAVE_ICONV */
815 char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, size_t inbytesleft)
824 cd = SDL_iconv_open(tocode, fromcode);
825 if ( cd == (SDL_iconv_t)-1 ) {
826 /* See if we can recover here (fixes iconv on Solaris 11) */
827 if ( !tocode || !*tocode ) {
830 if ( !fromcode || !*fromcode ) {
833 cd = SDL_iconv_open(tocode, fromcode);
835 if ( cd == (SDL_iconv_t)-1 ) {
839 stringsize = inbytesleft > 4 ? inbytesleft : 4;
840 string = SDL_malloc(stringsize);
846 outbytesleft = stringsize;
847 SDL_memset(outbuf, 0, 4);
849 while ( inbytesleft > 0 ) {
850 retCode = SDL_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
852 case SDL_ICONV_E2BIG:
854 char *oldstring = string;
856 string = SDL_realloc(string, stringsize);
861 outbuf = string + (outbuf - oldstring);
862 outbytesleft = stringsize - (outbuf - string);
863 SDL_memset(outbuf, 0, 4);
866 case SDL_ICONV_EILSEQ:
867 /* Try skipping some input data - not perfect, but... */
871 case SDL_ICONV_EINVAL:
872 case SDL_ICONV_ERROR:
873 /* We can't continue... */