SDL_iconv.c 27 KB

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