SDL_iconv.c 28 KB

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