SDL_iconv.c 27 KB

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