SDL_iconv.c 25 KB

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