SDL_iconv.c 25 KB

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